Re: [PATCH bpf-next v4] virtio_net: add XDP meta data support
From: Yuya Kusakabe
Date: Wed Feb 05 2020 - 04:18:24 EST
On 2/5/20 1:10 PM, Jason Wang wrote:
>
> On 2020/2/4 äå3:16, Yuya Kusakabe wrote:
>> Implement support for transferring XDP meta data into skb for
>> virtio_net driver; before calling into the program, xdp.data_meta points
>> to xdp.data and copy vnet header to the front of xdp.data_hard_start
>> to avoid overwriting it, where on program return with pass verdict,
>> we call into skb_metadata_set().
>>
>> Fixes: de8f3a83b0a0 ("bpf: add meta pointer for direct access")
>> Signed-off-by: Yuya Kusakabe <yuya.kusakabe@xxxxxxxxx>
>> ---
>> Â drivers/net/virtio_net.c | 47 ++++++++++++++++++++++++++++------------
>> Â 1 file changed, 33 insertions(+), 14 deletions(-)
>>
>> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
>> index 2fe7a3188282..5fdd6ea0e3f1 100644
>> --- a/drivers/net/virtio_net.c
>> +++ b/drivers/net/virtio_net.c
>> @@ -371,7 +371,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct receive_queue *rq,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct page *page, unsigned int offset,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int len, unsigned int truesize,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ bool hdr_valid)
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ bool hdr_valid, unsigned int metasize)
>> Â {
>> ÂÂÂÂÂ struct sk_buff *skb;
>> ÂÂÂÂÂ struct virtio_net_hdr_mrg_rxbuf *hdr;
>> @@ -393,7 +393,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
>> ÂÂÂÂÂ else
>> ÂÂÂÂÂÂÂÂÂ hdr_padded_len = sizeof(struct padded_vnet_hdr);
>> Â -ÂÂÂ if (hdr_valid)
>> +ÂÂÂ if (hdr_valid && !metasize)
>
>
> hdr_valid means no XDP, so I think we can remove the check for metasize here and add a comment instead?
I will fix it on next patch.
>
>> ÂÂÂÂÂÂÂÂÂ memcpy(hdr, p, hdr_len);
>> Â ÂÂÂÂÂ len -= hdr_len;
>> @@ -405,6 +405,11 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
>> ÂÂÂÂÂÂÂÂÂ copy = skb_tailroom(skb);
>> ÂÂÂÂÂ skb_put_data(skb, p, copy);
>> Â +ÂÂÂ if (metasize) {
>> +ÂÂÂÂÂÂÂ __skb_pull(skb, metasize);
>> +ÂÂÂÂÂÂÂ skb_metadata_set(skb, metasize);
>> +ÂÂÂ }
>> +
>> ÂÂÂÂÂ len -= copy;
>> ÂÂÂÂÂ offset += copy;
>> Â @@ -644,6 +649,7 @@ static struct sk_buff *receive_small(struct net_device *dev,
>> ÂÂÂÂÂ unsigned int delta = 0;
>> ÂÂÂÂÂ struct page *xdp_page;
>> ÂÂÂÂÂ int err;
>> +ÂÂÂ unsigned int metasize = 0;
>> Â ÂÂÂÂÂ len -= vi->hdr_len;
>> ÂÂÂÂÂ stats->bytes += len;
>> @@ -683,10 +689,15 @@ static struct sk_buff *receive_small(struct net_device *dev,
>> Â ÂÂÂÂÂÂÂÂÂ xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
>> ÂÂÂÂÂÂÂÂÂ xdp.data = xdp.data_hard_start + xdp_headroom;
>> -ÂÂÂÂÂÂÂ xdp_set_data_meta_invalid(&xdp);
>> ÂÂÂÂÂÂÂÂÂ xdp.data_end = xdp.data + len;
>> +ÂÂÂÂÂÂÂ xdp.data_meta = xdp.data;
>> ÂÂÂÂÂÂÂÂÂ xdp.rxq = &rq->xdp_rxq;
>> ÂÂÂÂÂÂÂÂÂ orig_data = xdp.data;
>> +ÂÂÂÂÂÂÂ /* Copy the vnet header to the front of data_hard_start to avoid
>> +ÂÂÂÂÂÂÂÂ * overwriting it by XDP meta data.
>> +ÂÂÂÂÂÂÂÂ */
>> +ÂÂÂÂÂÂÂ memcpy(xdp.data_hard_start - vi->hdr_len,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ xdp.data - vi->hdr_len, vi->hdr_len);
>
>
> I think we don't need this. And it looks to me there's a bug in the current code.
>
> Commit 436c9453a1ac0 ("virtio-net: keep vnet header zeroed after processing XDP") leave the a corner case for receive_small() which still use:
>
> ÂÂÂÂÂÂÂ if (!delta) {
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ buf += header_offset;
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
> ÂÂÂÂÂÂÂ } /* keep zeroed vnet hdr since packet was changed by bpf */
>
> Which seems wrong, we need check xdp_prog instead of delta.
>
> With this fixed, there's no need to care about the vnet header here since we don't know whether or not packet is modified by XDP.
I missed this commit. I understand this is the reason for "Awaiting Upstream".
>
>> ÂÂÂÂÂÂÂÂÂ act = bpf_prog_run_xdp(xdp_prog, &xdp);
>> ÂÂÂÂÂÂÂÂÂ stats->xdp_packets++;
>> Â @@ -695,9 +706,11 @@ static struct sk_buff *receive_small(struct net_device *dev,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ /* Recalculate length in case bpf program changed it */
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ delta = orig_data - xdp.data;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ len = xdp.data_end - xdp.data;
>> +ÂÂÂÂÂÂÂÂÂÂÂ metasize = xdp.data - xdp.data_meta;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> ÂÂÂÂÂÂÂÂÂ case XDP_TX:
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ stats->xdp_tx++;
>> +ÂÂÂÂÂÂÂÂÂÂÂ xdp.data_meta = xdp.data;
>
>
> I think we should remove the xdp_set_data_meta_invalid() at least? And move this initialization just after xdp.data is initialized.
>
> Testing receive_small() requires to disable mrg_rxbuf, guest_tso4, guest_tso6 and guest_ufo from qemu command line.
>
>
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ xdpf = convert_to_xdp_frame(&xdp);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ if (unlikely(!xdpf))
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto err_xdp;
>> @@ -736,10 +749,12 @@ static struct sk_buff *receive_small(struct net_device *dev,
>> ÂÂÂÂÂ skb_reserve(skb, headroom - delta);
>> ÂÂÂÂÂ skb_put(skb, len);
>> ÂÂÂÂÂ if (!delta) {
>
>
> Need to check xdp_prog (need another patch).
I will fix it on next patch.
>
>
>> -ÂÂÂÂÂÂÂ buf += header_offset;
>> -ÂÂÂÂÂÂÂ memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
>> +ÂÂÂÂÂÂÂ memcpy(skb_vnet_hdr(skb), buf + VIRTNET_RX_PAD, vi->hdr_len);
>> ÂÂÂÂÂ } /* keep zeroed vnet hdr since packet was changed by bpf */
>> Â +ÂÂÂ if (metasize)
>> +ÂÂÂÂÂÂÂ skb_metadata_set(skb, metasize);
>> +
>> Â err:
>> ÂÂÂÂÂ return skb;
>> Â @@ -760,8 +775,8 @@ static struct sk_buff *receive_big(struct net_device *dev,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct virtnet_rq_stats *stats)
>> Â {
>> ÂÂÂÂÂ struct page *page = buf;
>> -ÂÂÂ struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PAGE_SIZE, true);
>> +ÂÂÂ struct sk_buff *skb =
>> +ÂÂÂÂÂÂÂ page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0);
>> Â ÂÂÂÂÂ stats->bytes += len - vi->hdr_len;
>> ÂÂÂÂÂ if (unlikely(!skb))
>> @@ -793,6 +808,7 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
>> ÂÂÂÂÂ unsigned int truesize;
>> ÂÂÂÂÂ unsigned int headroom = mergeable_ctx_to_headroom(ctx);
>> ÂÂÂÂÂ int err;
>> +ÂÂÂ unsigned int metasize = 0;
>> Â ÂÂÂÂÂ head_skb = NULL;
>> ÂÂÂÂÂ stats->bytes += len - vi->hdr_len;
>> @@ -839,8 +855,8 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
>> ÂÂÂÂÂÂÂÂÂ data = page_address(xdp_page) + offset;
>> ÂÂÂÂÂÂÂÂÂ xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
>> ÂÂÂÂÂÂÂÂÂ xdp.data = data + vi->hdr_len;
>> -ÂÂÂÂÂÂÂ xdp_set_data_meta_invalid(&xdp);
>> ÂÂÂÂÂÂÂÂÂ xdp.data_end = xdp.data + (len - vi->hdr_len);
>> +ÂÂÂÂÂÂÂ xdp.data_meta = xdp.data;
>> ÂÂÂÂÂÂÂÂÂ xdp.rxq = &rq->xdp_rxq;
>> Â ÂÂÂÂÂÂÂÂÂ act = bpf_prog_run_xdp(xdp_prog, &xdp);
>> @@ -852,8 +868,9 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ * adjustments. Note other cases do not build an
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ * skb and avoid using offset
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ */
>> -ÂÂÂÂÂÂÂÂÂÂÂ offset = xdp.data -
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ page_address(xdp_page) - vi->hdr_len;
>> +ÂÂÂÂÂÂÂÂÂÂÂ metasize = xdp.data - xdp.data_meta;
>> +ÂÂÂÂÂÂÂÂÂÂÂ offset = xdp.data - page_address(xdp_page) -
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ vi->hdr_len - metasize;
>> Â ÂÂÂÂÂÂÂÂÂÂÂÂÂ /* recalculate len if xdp.data or xdp.data_end were
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ * adjusted
>> @@ -863,14 +880,15 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ if (unlikely(xdp_page != page)) {
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ rcu_read_unlock();
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ put_page(page);
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ head_skb = page_to_skb(vi, rq, xdp_page,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ offset, len,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PAGE_SIZE, false);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ head_skb = page_to_skb(vi, rq, xdp_page, offset,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ len, PAGE_SIZE, false,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ metasize);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return head_skb;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> ÂÂÂÂÂÂÂÂÂ case XDP_TX:
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ stats->xdp_tx++;
>> +ÂÂÂÂÂÂÂÂÂÂÂ xdp.data_meta = xdp.data;
>
>
> Any reason for doing this?
XDP_TX can not support metadata for now, because if metasize > 0, __virtnet_xdp_xmit_one() returns EOPNOTSUPP.
static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
struct send_queue *sq,
struct xdp_frame *xdpf)
{
struct virtio_net_hdr_mrg_rxbuf *hdr;
int err;
/* virtqueue want to use data area in-front of packet */
if (unlikely(xdpf->metasize > 0))
return -EOPNOTSUPP;
>
> Thanks
>
>
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ xdpf = convert_to_xdp_frame(&xdp);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ if (unlikely(!xdpf))
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto err_xdp;
>> @@ -921,7 +939,8 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
>> ÂÂÂÂÂÂÂÂÂ goto err_skb;
>> ÂÂÂÂÂ }
>> Â -ÂÂÂ head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog);
>> +ÂÂÂ head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ metasize);
>> ÂÂÂÂÂ curr_skb = head_skb;
>> Â ÂÂÂÂÂ if (unlikely(!curr_skb))
>
Thank you for your kind review.