virtio-net: fix calculation of MTU for single-buffer xdp
authorHeng Qi <hengqi@linux.alibaba.com>
Sat, 14 Jan 2023 08:22:21 +0000 (16:22 +0800)
committerDavid S. Miller <davem@davemloft.net>
Mon, 16 Jan 2023 11:15:48 +0000 (11:15 +0000)
When single-buffer xdp is loaded, the size of the buffer filled each time
is 'sz = (PAGE_SIZE - headroom - tailroom)', which is the maximum packet
length that the driver allows the device to pass in. Otherwise, the packet
with a length greater than sz will come in, so num_buf will be greater than
or equal to 2, and xdp_linearize_page() will be performed and the packet
will be dropped because the total length is greater than PAGE_SIZE. So the
maximum value of MTU for single-buffer xdp is 'max_sz = sz - ETH_HLEN'.

Signed-off-by: Heng Qi <hengqi@linux.alibaba.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/virtio_net.c

index a3271d2c7c0229c54e31f3365b7f3825e00b1844..81d92978cc92fa261cb9dbeb07fbc499d5e84fcf 100644 (file)
@@ -3081,7 +3081,9 @@ static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
                           struct netlink_ext_ack *extack)
 {
-       unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
+       unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
+                                          sizeof(struct skb_shared_info));
+       unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
        struct virtnet_info *vi = netdev_priv(dev);
        struct bpf_prog *old_prog;
        u16 xdp_qp = 0, curr_qp;
@@ -3106,7 +3108,7 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
 
        if (dev->mtu > max_sz) {
                NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
-               netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
+               netdev_warn(dev, "XDP requires MTU less than %u\n", max_sz);
                return -EINVAL;
        }