Skip to content

Commit 703eec1

Browse files
Heng Qidavem330
authored andcommitted
virtio_net: fixing XDP for fully checksummed packets handling
The XDP program can't correctly handle partially checksummed packets, but works fine with fully checksummed packets. If the device has already validated fully checksummed packets, then the driver doesn't need to re-validate them, saving CPU resources. Additionally, the driver does not drop all partially checksummed packets when VIRTIO_NET_F_GUEST_CSUM is not negotiated. This is not a bug, as the driver has always done this. Fixes: 436c945 ("virtio-net: keep vnet header zeroed after processing XDP") Signed-off-by: Heng Qi <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 604141c commit 703eec1

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

drivers/net/virtio_net.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,10 @@ static struct sk_buff *receive_small_xdp(struct net_device *dev,
13601360
if (unlikely(hdr->hdr.gso_type))
13611361
goto err_xdp;
13621362

1363+
/* Partially checksummed packets must be dropped. */
1364+
if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1365+
goto err_xdp;
1366+
13631367
buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
13641368
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
13651369

@@ -1677,6 +1681,10 @@ static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
16771681
if (unlikely(hdr->hdr.gso_type))
16781682
return NULL;
16791683

1684+
/* Partially checksummed packets must be dropped. */
1685+
if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1686+
return NULL;
1687+
16801688
/* Now XDP core assumes frag size is PAGE_SIZE, but buffers
16811689
* with headroom may add hole in truesize, which
16821690
* make their length exceed PAGE_SIZE. So we disabled the
@@ -1943,6 +1951,7 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
19431951
struct net_device *dev = vi->dev;
19441952
struct sk_buff *skb;
19451953
struct virtio_net_common_hdr *hdr;
1954+
u8 flags;
19461955

19471956
if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
19481957
pr_debug("%s: short packet %i\n", dev->name, len);
@@ -1951,6 +1960,15 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
19511960
return;
19521961
}
19531962

1963+
/* 1. Save the flags early, as the XDP program might overwrite them.
1964+
* These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
1965+
* stay valid after XDP processing.
1966+
* 2. XDP doesn't work with partially checksummed packets (refer to
1967+
* virtnet_xdp_set()), so packets marked as
1968+
* VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
1969+
*/
1970+
flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
1971+
19541972
if (vi->mergeable_rx_bufs)
19551973
skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
19561974
stats);
@@ -1966,7 +1984,7 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
19661984
if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
19671985
virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
19681986

1969-
if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1987+
if (flags & VIRTIO_NET_HDR_F_DATA_VALID)
19701988
skb->ip_summed = CHECKSUM_UNNECESSARY;
19711989

19721990
if (virtio_net_hdr_to_skb(skb, &hdr->hdr,

0 commit comments

Comments
 (0)