11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 248925e37SRusty Russell /* A network driver using virtio. 3296f96fcSRusty Russell * 4296f96fcSRusty Russell * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation 5296f96fcSRusty Russell */ 6296f96fcSRusty Russell //#define DEBUG 7296f96fcSRusty Russell #include <linux/netdevice.h> 8296f96fcSRusty Russell #include <linux/etherdevice.h> 9a9ea3fc6SHerbert Xu #include <linux/ethtool.h> 10296f96fcSRusty Russell #include <linux/module.h> 11296f96fcSRusty Russell #include <linux/virtio.h> 12296f96fcSRusty Russell #include <linux/virtio_net.h> 13f600b690SJohn Fastabend #include <linux/bpf.h> 14a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h> 15296f96fcSRusty Russell #include <linux/scatterlist.h> 16e918085aSAlex Williamson #include <linux/if_vlan.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 188de4b2f3SWanlong Gao #include <linux/cpu.h> 19ab7db917SMichael Dalton #include <linux/average.h> 20186b3c99SJason Wang #include <linux/filter.h> 212ca653d6SCaleb Raitto #include <linux/kernel.h> 22d85b758fSMichael S. Tsirkin #include <net/route.h> 23754b8a21SJesper Dangaard Brouer #include <net/xdp.h> 24ba5e4426SSridhar Samudrala #include <net/net_failover.h> 25296f96fcSRusty Russell 26d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT; 276c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 286c0cd7c0SDor Laor 2931c03aefSWillem de Bruijn static bool csum = true, gso = true, napi_tx = true; 3034a48579SRusty Russell module_param(csum, bool, 0444); 3134a48579SRusty Russell module_param(gso, bool, 0444); 32b92f1e67SWillem de Bruijn module_param(napi_tx, bool, 0644); 3334a48579SRusty Russell 34296f96fcSRusty Russell /* FIXME: MTU in config. */ 355061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 363f2c31d9SMark McLoughlin #define GOOD_COPY_LEN 128 37296f96fcSRusty Russell 38f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 39f6b10209SJason Wang 402de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ 412de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256 422de2f7f4SJohn Fastabend 432471c75eSJesper Dangaard Brouer /* Separating two types of XDP xmit */ 442471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_TX BIT(0) 452471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_REDIR BIT(1) 462471c75eSJesper Dangaard Brouer 475050471dSToshiaki Makita #define VIRTIO_XDP_FLAG BIT(0) 485050471dSToshiaki Makita 495377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet 505377d758SJohannes Berg * buffer size when refilling RX rings. As the entire RX ring may be refilled 515377d758SJohannes Berg * at once, the weight is chosen so that the EWMA will be insensitive to short- 525377d758SJohannes Berg * term, transient changes in packet size. 53ab7db917SMichael Dalton */ 54eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64) 55ab7db917SMichael Dalton 5666846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0" 572a41f71dSAlex Williamson 587acd4329SColin Ian King static const unsigned long guest_offloads[] = { 597acd4329SColin Ian King VIRTIO_NET_F_GUEST_TSO4, 603f93522fSJason Wang VIRTIO_NET_F_GUEST_TSO6, 613f93522fSJason Wang VIRTIO_NET_F_GUEST_ECN, 62e59ff2c4SJason Wang VIRTIO_NET_F_GUEST_UFO, 63e59ff2c4SJason Wang VIRTIO_NET_F_GUEST_CSUM 647acd4329SColin Ian King }; 653f93522fSJason Wang 66dbcf24d1SJason Wang #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \ 671a03b8a3STonghao Zhang (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \ 681a03b8a3STonghao Zhang (1ULL << VIRTIO_NET_F_GUEST_ECN) | \ 691a03b8a3STonghao Zhang (1ULL << VIRTIO_NET_F_GUEST_UFO)) 701a03b8a3STonghao Zhang 71d7dfc5cfSToshiaki Makita struct virtnet_stat_desc { 72d7dfc5cfSToshiaki Makita char desc[ETH_GSTRING_LEN]; 73d7dfc5cfSToshiaki Makita size_t offset; 743fa2a1dfSstephen hemminger }; 753fa2a1dfSstephen hemminger 76d7dfc5cfSToshiaki Makita struct virtnet_sq_stats { 77d7dfc5cfSToshiaki Makita struct u64_stats_sync syncp; 78d7dfc5cfSToshiaki Makita u64 packets; 79d7dfc5cfSToshiaki Makita u64 bytes; 805b8f3c8dSToshiaki Makita u64 xdp_tx; 815b8f3c8dSToshiaki Makita u64 xdp_tx_drops; 82461f03dcSToshiaki Makita u64 kicks; 83d7dfc5cfSToshiaki Makita }; 84d7dfc5cfSToshiaki Makita 85d46eeeafSJason Wang struct virtnet_rq_stats { 86d46eeeafSJason Wang struct u64_stats_sync syncp; 87d7dfc5cfSToshiaki Makita u64 packets; 88d7dfc5cfSToshiaki Makita u64 bytes; 892c4a2f7dSToshiaki Makita u64 drops; 905b8f3c8dSToshiaki Makita u64 xdp_packets; 915b8f3c8dSToshiaki Makita u64 xdp_tx; 925b8f3c8dSToshiaki Makita u64 xdp_redirects; 935b8f3c8dSToshiaki Makita u64 xdp_drops; 94461f03dcSToshiaki Makita u64 kicks; 95d7dfc5cfSToshiaki Makita }; 96d7dfc5cfSToshiaki Makita 97d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m) 98d46eeeafSJason Wang #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m) 99d7dfc5cfSToshiaki Makita 100d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = { 101d7dfc5cfSToshiaki Makita { "packets", VIRTNET_SQ_STAT(packets) }, 102d7dfc5cfSToshiaki Makita { "bytes", VIRTNET_SQ_STAT(bytes) }, 1035b8f3c8dSToshiaki Makita { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) }, 1045b8f3c8dSToshiaki Makita { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) }, 105461f03dcSToshiaki Makita { "kicks", VIRTNET_SQ_STAT(kicks) }, 106d7dfc5cfSToshiaki Makita }; 107d7dfc5cfSToshiaki Makita 108d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { 109d7dfc5cfSToshiaki Makita { "packets", VIRTNET_RQ_STAT(packets) }, 110d7dfc5cfSToshiaki Makita { "bytes", VIRTNET_RQ_STAT(bytes) }, 1112c4a2f7dSToshiaki Makita { "drops", VIRTNET_RQ_STAT(drops) }, 1125b8f3c8dSToshiaki Makita { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) }, 1135b8f3c8dSToshiaki Makita { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) }, 1145b8f3c8dSToshiaki Makita { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) }, 1155b8f3c8dSToshiaki Makita { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) }, 116461f03dcSToshiaki Makita { "kicks", VIRTNET_RQ_STAT(kicks) }, 117d7dfc5cfSToshiaki Makita }; 118d7dfc5cfSToshiaki Makita 119d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) 120d7dfc5cfSToshiaki Makita #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc) 121d7dfc5cfSToshiaki Makita 122e9d7417bSJason Wang /* Internal representation of a send virtqueue */ 123e9d7417bSJason Wang struct send_queue { 124e9d7417bSJason Wang /* Virtqueue associated with this send _queue */ 125e9d7417bSJason Wang struct virtqueue *vq; 126e9d7417bSJason Wang 127e9d7417bSJason Wang /* TX: fragments + linear part + virtio header */ 128e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 129986a4f4dSJason Wang 130986a4f4dSJason Wang /* Name of the send queue: output.$index */ 131986a4f4dSJason Wang char name[40]; 132b92f1e67SWillem de Bruijn 133d7dfc5cfSToshiaki Makita struct virtnet_sq_stats stats; 134d7dfc5cfSToshiaki Makita 135b92f1e67SWillem de Bruijn struct napi_struct napi; 136e9d7417bSJason Wang }; 137e9d7417bSJason Wang 138e9d7417bSJason Wang /* Internal representation of a receive virtqueue */ 139e9d7417bSJason Wang struct receive_queue { 140e9d7417bSJason Wang /* Virtqueue associated with this receive_queue */ 141e9d7417bSJason Wang struct virtqueue *vq; 142e9d7417bSJason Wang 143296f96fcSRusty Russell struct napi_struct napi; 144296f96fcSRusty Russell 145f600b690SJohn Fastabend struct bpf_prog __rcu *xdp_prog; 146f600b690SJohn Fastabend 147d7dfc5cfSToshiaki Makita struct virtnet_rq_stats stats; 148d7dfc5cfSToshiaki Makita 149e9d7417bSJason Wang /* Chain pages by the private ptr. */ 150e9d7417bSJason Wang struct page *pages; 151e9d7417bSJason Wang 152ab7db917SMichael Dalton /* Average packet length for mergeable receive buffers. */ 1535377d758SJohannes Berg struct ewma_pkt_len mrg_avg_pkt_len; 154ab7db917SMichael Dalton 155fb51879dSMichael Dalton /* Page frag for packet buffer allocation. */ 156fb51879dSMichael Dalton struct page_frag alloc_frag; 157fb51879dSMichael Dalton 158e9d7417bSJason Wang /* RX: fragments + linear part + virtio header */ 159e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 160986a4f4dSJason Wang 161d85b758fSMichael S. Tsirkin /* Min single buffer size for mergeable buffers case. */ 162d85b758fSMichael S. Tsirkin unsigned int min_buf_len; 163d85b758fSMichael S. Tsirkin 164986a4f4dSJason Wang /* Name of this receive queue: input.$index */ 165986a4f4dSJason Wang char name[40]; 166754b8a21SJesper Dangaard Brouer 167754b8a21SJesper Dangaard Brouer struct xdp_rxq_info xdp_rxq; 168e9d7417bSJason Wang }; 169e9d7417bSJason Wang 17012e57169SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */ 17112e57169SMichael S. Tsirkin struct control_buf { 17212e57169SMichael S. Tsirkin struct virtio_net_ctrl_hdr hdr; 17312e57169SMichael S. Tsirkin virtio_net_ctrl_ack status; 17412e57169SMichael S. Tsirkin struct virtio_net_ctrl_mq mq; 17512e57169SMichael S. Tsirkin u8 promisc; 17612e57169SMichael S. Tsirkin u8 allmulti; 177d7fad4c8SMichael S. Tsirkin __virtio16 vid; 178f4ee703aSMichael S. Tsirkin __virtio64 offloads; 17912e57169SMichael S. Tsirkin }; 18012e57169SMichael S. Tsirkin 181e9d7417bSJason Wang struct virtnet_info { 182e9d7417bSJason Wang struct virtio_device *vdev; 183e9d7417bSJason Wang struct virtqueue *cvq; 184e9d7417bSJason Wang struct net_device *dev; 185986a4f4dSJason Wang struct send_queue *sq; 186986a4f4dSJason Wang struct receive_queue *rq; 187e9d7417bSJason Wang unsigned int status; 188e9d7417bSJason Wang 189986a4f4dSJason Wang /* Max # of queue pairs supported by the device */ 190986a4f4dSJason Wang u16 max_queue_pairs; 191986a4f4dSJason Wang 192986a4f4dSJason Wang /* # of queue pairs currently used by the driver */ 193986a4f4dSJason Wang u16 curr_queue_pairs; 194986a4f4dSJason Wang 195672aafd5SJohn Fastabend /* # of XDP queue pairs currently used by the driver */ 196672aafd5SJohn Fastabend u16 xdp_queue_pairs; 197672aafd5SJohn Fastabend 19897c2c69eSXuan Zhuo /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */ 19997c2c69eSXuan Zhuo bool xdp_enabled; 20097c2c69eSXuan Zhuo 20197402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 20297402b96SHerbert Xu bool big_packets; 20397402b96SHerbert Xu 2043f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 2053f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 2063f2c31d9SMark McLoughlin 207986a4f4dSJason Wang /* Has control virtqueue */ 208986a4f4dSJason Wang bool has_cvq; 209986a4f4dSJason Wang 210e7428e95SMichael S. Tsirkin /* Host can handle any s/g split between our header and packet data */ 211e7428e95SMichael S. Tsirkin bool any_header_sg; 212e7428e95SMichael S. Tsirkin 213012873d0SMichael S. Tsirkin /* Packet virtio header size */ 214012873d0SMichael S. Tsirkin u8 hdr_len; 215012873d0SMichael S. Tsirkin 2163161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 2173161e453SRusty Russell struct delayed_work refill; 2183161e453SRusty Russell 219586d17c5SJason Wang /* Work struct for config space updates */ 220586d17c5SJason Wang struct work_struct config_work; 221586d17c5SJason Wang 222986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 223986a4f4dSJason Wang bool affinity_hint_set; 22447be2479SWanlong Gao 2258017c279SSebastian Andrzej Siewior /* CPU hotplug instances for online & dead */ 2268017c279SSebastian Andrzej Siewior struct hlist_node node; 2278017c279SSebastian Andrzej Siewior struct hlist_node node_dead; 2282ac46030SMichael S. Tsirkin 22912e57169SMichael S. Tsirkin struct control_buf *ctrl; 23016032be5SNikolay Aleksandrov 23116032be5SNikolay Aleksandrov /* Ethtool settings */ 23216032be5SNikolay Aleksandrov u8 duplex; 23316032be5SNikolay Aleksandrov u32 speed; 2343f93522fSJason Wang 2353f93522fSJason Wang unsigned long guest_offloads; 236a02e8964SWillem de Bruijn unsigned long guest_offloads_capable; 237ba5e4426SSridhar Samudrala 238ba5e4426SSridhar Samudrala /* failover when STANDBY feature enabled */ 239ba5e4426SSridhar Samudrala struct failover *failover; 240296f96fcSRusty Russell }; 241296f96fcSRusty Russell 2429ab86bbcSShirley Ma struct padded_vnet_hdr { 243012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr; 2449ab86bbcSShirley Ma /* 245012873d0SMichael S. Tsirkin * hdr is in a separate sg buffer, and data sg buffer shares same page 246012873d0SMichael S. Tsirkin * with this header sg. This padding makes next sg 16 byte aligned 247012873d0SMichael S. Tsirkin * after the header. 2489ab86bbcSShirley Ma */ 249012873d0SMichael S. Tsirkin char padding[4]; 2509ab86bbcSShirley Ma }; 2519ab86bbcSShirley Ma 2525050471dSToshiaki Makita static bool is_xdp_frame(void *ptr) 2535050471dSToshiaki Makita { 2545050471dSToshiaki Makita return (unsigned long)ptr & VIRTIO_XDP_FLAG; 2555050471dSToshiaki Makita } 2565050471dSToshiaki Makita 2575050471dSToshiaki Makita static void *xdp_to_ptr(struct xdp_frame *ptr) 2585050471dSToshiaki Makita { 2595050471dSToshiaki Makita return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG); 2605050471dSToshiaki Makita } 2615050471dSToshiaki Makita 2625050471dSToshiaki Makita static struct xdp_frame *ptr_to_xdp(void *ptr) 2635050471dSToshiaki Makita { 2645050471dSToshiaki Makita return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); 2655050471dSToshiaki Makita } 2665050471dSToshiaki Makita 267986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 268986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 269986a4f4dSJason Wang */ 270986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 271986a4f4dSJason Wang { 2729d0ca6edSRusty Russell return (vq->index - 1) / 2; 273986a4f4dSJason Wang } 274986a4f4dSJason Wang 275986a4f4dSJason Wang static int txq2vq(int txq) 276986a4f4dSJason Wang { 277986a4f4dSJason Wang return txq * 2 + 1; 278986a4f4dSJason Wang } 279986a4f4dSJason Wang 280986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 281986a4f4dSJason Wang { 2829d0ca6edSRusty Russell return vq->index / 2; 283986a4f4dSJason Wang } 284986a4f4dSJason Wang 285986a4f4dSJason Wang static int rxq2vq(int rxq) 286986a4f4dSJason Wang { 287986a4f4dSJason Wang return rxq * 2; 288986a4f4dSJason Wang } 289986a4f4dSJason Wang 290012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 291296f96fcSRusty Russell { 292012873d0SMichael S. Tsirkin return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 293296f96fcSRusty Russell } 294296f96fcSRusty Russell 2959ab86bbcSShirley Ma /* 2969ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 2979ab86bbcSShirley Ma * most recent used list in the beginning for reuse 2989ab86bbcSShirley Ma */ 299e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 300fb6813f4SRusty Russell { 3019ab86bbcSShirley Ma struct page *end; 3029ab86bbcSShirley Ma 303e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 3049ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 305e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 306e9d7417bSJason Wang rq->pages = page; 307fb6813f4SRusty Russell } 308fb6813f4SRusty Russell 309e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 310fb6813f4SRusty Russell { 311e9d7417bSJason Wang struct page *p = rq->pages; 312fb6813f4SRusty Russell 3139ab86bbcSShirley Ma if (p) { 314e9d7417bSJason Wang rq->pages = (struct page *)p->private; 3159ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 3169ab86bbcSShirley Ma p->private = 0; 3179ab86bbcSShirley Ma } else 318fb6813f4SRusty Russell p = alloc_page(gfp_mask); 319fb6813f4SRusty Russell return p; 320fb6813f4SRusty Russell } 321fb6813f4SRusty Russell 322e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi, 323e4e8452aSWillem de Bruijn struct virtqueue *vq) 324e4e8452aSWillem de Bruijn { 325e4e8452aSWillem de Bruijn if (napi_schedule_prep(napi)) { 326e4e8452aSWillem de Bruijn virtqueue_disable_cb(vq); 327e4e8452aSWillem de Bruijn __napi_schedule(napi); 328e4e8452aSWillem de Bruijn } 329e4e8452aSWillem de Bruijn } 330e4e8452aSWillem de Bruijn 331e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi, 332e4e8452aSWillem de Bruijn struct virtqueue *vq, int processed) 333e4e8452aSWillem de Bruijn { 334e4e8452aSWillem de Bruijn int opaque; 335e4e8452aSWillem de Bruijn 336e4e8452aSWillem de Bruijn opaque = virtqueue_enable_cb_prepare(vq); 337fdaa767aSToshiaki Makita if (napi_complete_done(napi, processed)) { 338fdaa767aSToshiaki Makita if (unlikely(virtqueue_poll(vq, opaque))) 339e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 340fdaa767aSToshiaki Makita } else { 341fdaa767aSToshiaki Makita virtqueue_disable_cb(vq); 342fdaa767aSToshiaki Makita } 343e4e8452aSWillem de Bruijn } 344e4e8452aSWillem de Bruijn 345e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 346296f96fcSRusty Russell { 347e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 348b92f1e67SWillem de Bruijn struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 349296f96fcSRusty Russell 3502cb9c6baSRusty Russell /* Suppress further interrupts. */ 351e9d7417bSJason Wang virtqueue_disable_cb(vq); 35211a3a154SRusty Russell 353b92f1e67SWillem de Bruijn if (napi->weight) 354b92f1e67SWillem de Bruijn virtqueue_napi_schedule(napi, vq); 355b92f1e67SWillem de Bruijn else 356363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 357986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 358296f96fcSRusty Russell } 359296f96fcSRusty Russell 36028b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22 36128b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize, 36228b39bc7SJason Wang unsigned int headroom) 36328b39bc7SJason Wang { 36428b39bc7SJason Wang return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 36528b39bc7SJason Wang } 36628b39bc7SJason Wang 36728b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 36828b39bc7SJason Wang { 36928b39bc7SJason Wang return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 37028b39bc7SJason Wang } 37128b39bc7SJason Wang 37228b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 37328b39bc7SJason Wang { 37428b39bc7SJason Wang return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 37528b39bc7SJason Wang } 37628b39bc7SJason Wang 3773464645aSMike Waychison /* Called from bottom half context */ 378946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 379946fa564SMichael S. Tsirkin struct receive_queue *rq, 3802613af0eSMichael Dalton struct page *page, unsigned int offset, 381436c9453SJason Wang unsigned int len, unsigned int truesize, 382fb32856bSXuan Zhuo bool hdr_valid, unsigned int metasize, 383c32325b8SJakub Kicinski unsigned int headroom) 3849ab86bbcSShirley Ma { 3859ab86bbcSShirley Ma struct sk_buff *skb; 386012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 3872613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 388af39c8f7SEric Dumazet struct page *page_to_free = NULL; 389fb32856bSXuan Zhuo int tailroom, shinfo_size; 390f80bd740SXuan Zhuo char *p, *hdr_p, *buf; 3919ab86bbcSShirley Ma 3922613af0eSMichael Dalton p = page_address(page) + offset; 393fb32856bSXuan Zhuo hdr_p = p; 3949ab86bbcSShirley Ma 395012873d0SMichael S. Tsirkin hdr_len = vi->hdr_len; 396012873d0SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 397a4a76503Sstephen hemminger hdr_padded_len = sizeof(*hdr); 398012873d0SMichael S. Tsirkin else 3992613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 4003f2c31d9SMark McLoughlin 401c32325b8SJakub Kicinski /* If headroom is not 0, there is an offset between the beginning of the 402fb32856bSXuan Zhuo * data and the allocated space, otherwise the data and the allocated 403fb32856bSXuan Zhuo * space are aligned. 4048fb7da9eSXuan Zhuo * 4058fb7da9eSXuan Zhuo * Buffers with headroom use PAGE_SIZE as alloc size, see 4068fb7da9eSXuan Zhuo * add_recvbuf_mergeable() + get_mergeable_buf_len() 407fb32856bSXuan Zhuo */ 408c32325b8SJakub Kicinski truesize = headroom ? PAGE_SIZE : truesize; 409*fc02e8cbSMichael S. Tsirkin tailroom = truesize - headroom; 410c32325b8SJakub Kicinski buf = p - headroom; 4113f2c31d9SMark McLoughlin 4129ab86bbcSShirley Ma len -= hdr_len; 4132613af0eSMichael Dalton offset += hdr_padded_len; 4142613af0eSMichael Dalton p += hdr_padded_len; 415*fc02e8cbSMichael S. Tsirkin tailroom -= hdr_padded_len + len; 4163f2c31d9SMark McLoughlin 417fb32856bSXuan Zhuo shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 418fb32856bSXuan Zhuo 419f80bd740SXuan Zhuo /* copy small packet so we can reuse these pages */ 420f5d7872aSEric Dumazet if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) { 421f80bd740SXuan Zhuo skb = build_skb(buf, truesize); 422fb32856bSXuan Zhuo if (unlikely(!skb)) 423fb32856bSXuan Zhuo return NULL; 424fb32856bSXuan Zhuo 425f80bd740SXuan Zhuo skb_reserve(skb, p - buf); 426fb32856bSXuan Zhuo skb_put(skb, len); 427afd92d82SJason Wang 428afd92d82SJason Wang page = (struct page *)page->private; 429afd92d82SJason Wang if (page) 430afd92d82SJason Wang give_pages(rq, page); 431fb32856bSXuan Zhuo goto ok; 432fb32856bSXuan Zhuo } 433fb32856bSXuan Zhuo 434fb32856bSXuan Zhuo /* copy small packet so we can reuse these pages for small data */ 435fb32856bSXuan Zhuo skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 436fb32856bSXuan Zhuo if (unlikely(!skb)) 437fb32856bSXuan Zhuo return NULL; 438fb32856bSXuan Zhuo 4390f6925b3SEric Dumazet /* Copy all frame if it fits skb->head, otherwise 4400f6925b3SEric Dumazet * we let virtio_net_hdr_to_skb() and GRO pull headers as needed. 4410f6925b3SEric Dumazet */ 4420f6925b3SEric Dumazet if (len <= skb_tailroom(skb)) 4433f2c31d9SMark McLoughlin copy = len; 4440f6925b3SEric Dumazet else 4450f6925b3SEric Dumazet copy = ETH_HLEN + metasize; 44659ae1d12SJohannes Berg skb_put_data(skb, p, copy); 4473f2c31d9SMark McLoughlin 4483f2c31d9SMark McLoughlin len -= copy; 4499ab86bbcSShirley Ma offset += copy; 4503f2c31d9SMark McLoughlin 4512613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 4522613af0eSMichael Dalton if (len) 4532613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 4542613af0eSMichael Dalton else 455af39c8f7SEric Dumazet page_to_free = page; 456fb32856bSXuan Zhuo goto ok; 4572613af0eSMichael Dalton } 4582613af0eSMichael Dalton 459e878d78bSSasha Levin /* 460e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 461e878d78bSSasha Levin * This is here to handle cases when the device erroneously 462e878d78bSSasha Levin * tries to receive more than is possible. This is usually 463e878d78bSSasha Levin * the case of a broken device. 464e878d78bSSasha Levin */ 465e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 466be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 467e878d78bSSasha Levin dev_kfree_skb(skb); 468e878d78bSSasha Levin return NULL; 469e878d78bSSasha Levin } 4702613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 4719ab86bbcSShirley Ma while (len) { 4722613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 4732613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 4742613af0eSMichael Dalton frag_size, truesize); 4752613af0eSMichael Dalton len -= frag_size; 4769ab86bbcSShirley Ma page = (struct page *)page->private; 4779ab86bbcSShirley Ma offset = 0; 4783f2c31d9SMark McLoughlin } 4793f2c31d9SMark McLoughlin 4809ab86bbcSShirley Ma if (page) 481e9d7417bSJason Wang give_pages(rq, page); 4823f2c31d9SMark McLoughlin 483fb32856bSXuan Zhuo ok: 484fb32856bSXuan Zhuo /* hdr_valid means no XDP, so we can copy the vnet header */ 485fb32856bSXuan Zhuo if (hdr_valid) { 486fb32856bSXuan Zhuo hdr = skb_vnet_hdr(skb); 487fb32856bSXuan Zhuo memcpy(hdr, hdr_p, hdr_len); 488fb32856bSXuan Zhuo } 489af39c8f7SEric Dumazet if (page_to_free) 490af39c8f7SEric Dumazet put_page(page_to_free); 491fb32856bSXuan Zhuo 492fb32856bSXuan Zhuo if (metasize) { 493fb32856bSXuan Zhuo __skb_pull(skb, metasize); 494fb32856bSXuan Zhuo skb_metadata_set(skb, metasize); 495fb32856bSXuan Zhuo } 496fb32856bSXuan Zhuo 4979ab86bbcSShirley Ma return skb; 4989ab86bbcSShirley Ma } 4999ab86bbcSShirley Ma 500735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, 501735fc405SJesper Dangaard Brouer struct send_queue *sq, 50244fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf) 50356434a01SJohn Fastabend { 50456434a01SJohn Fastabend struct virtio_net_hdr_mrg_rxbuf *hdr; 50556434a01SJohn Fastabend int err; 50656434a01SJohn Fastabend 507cac320c8SJesper Dangaard Brouer if (unlikely(xdpf->headroom < vi->hdr_len)) 508cac320c8SJesper Dangaard Brouer return -EOVERFLOW; 509cac320c8SJesper Dangaard Brouer 510cac320c8SJesper Dangaard Brouer /* Make room for virtqueue hdr (also change xdpf->headroom?) */ 511cac320c8SJesper Dangaard Brouer xdpf->data -= vi->hdr_len; 51256434a01SJohn Fastabend /* Zero header and leave csum up to XDP layers */ 513cac320c8SJesper Dangaard Brouer hdr = xdpf->data; 51456434a01SJohn Fastabend memset(hdr, 0, vi->hdr_len); 515cac320c8SJesper Dangaard Brouer xdpf->len += vi->hdr_len; 51656434a01SJohn Fastabend 517cac320c8SJesper Dangaard Brouer sg_init_one(sq->sg, xdpf->data, xdpf->len); 518bb91accfSJason Wang 5195050471dSToshiaki Makita err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf), 5205050471dSToshiaki Makita GFP_ATOMIC); 52111b7d897SJesper Dangaard Brouer if (unlikely(err)) 522cac320c8SJesper Dangaard Brouer return -ENOSPC; /* Caller handle free/refcnt */ 52356434a01SJohn Fastabend 524cac320c8SJesper Dangaard Brouer return 0; 52556434a01SJohn Fastabend } 52656434a01SJohn Fastabend 52797c2c69eSXuan Zhuo /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on 52897c2c69eSXuan Zhuo * the current cpu, so it does not need to be locked. 52997c2c69eSXuan Zhuo * 53097c2c69eSXuan Zhuo * Here we use marco instead of inline functions because we have to deal with 53197c2c69eSXuan Zhuo * three issues at the same time: 1. the choice of sq. 2. judge and execute the 53297c2c69eSXuan Zhuo * lock/unlock of txq 3. make sparse happy. It is difficult for two inline 53397c2c69eSXuan Zhuo * functions to perfectly solve these three problems at the same time. 53497c2c69eSXuan Zhuo */ 53597c2c69eSXuan Zhuo #define virtnet_xdp_get_sq(vi) ({ \ 5363dcc1edcSLi RongQing int cpu = smp_processor_id(); \ 53797c2c69eSXuan Zhuo struct netdev_queue *txq; \ 53897c2c69eSXuan Zhuo typeof(vi) v = (vi); \ 53997c2c69eSXuan Zhuo unsigned int qp; \ 54097c2c69eSXuan Zhuo \ 54197c2c69eSXuan Zhuo if (v->curr_queue_pairs > nr_cpu_ids) { \ 54297c2c69eSXuan Zhuo qp = v->curr_queue_pairs - v->xdp_queue_pairs; \ 5433dcc1edcSLi RongQing qp += cpu; \ 54497c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, qp); \ 54597c2c69eSXuan Zhuo __netif_tx_acquire(txq); \ 54697c2c69eSXuan Zhuo } else { \ 5473dcc1edcSLi RongQing qp = cpu % v->curr_queue_pairs; \ 54897c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, qp); \ 5493dcc1edcSLi RongQing __netif_tx_lock(txq, cpu); \ 55097c2c69eSXuan Zhuo } \ 55197c2c69eSXuan Zhuo v->sq + qp; \ 55297c2c69eSXuan Zhuo }) 5532a43565cSToshiaki Makita 55497c2c69eSXuan Zhuo #define virtnet_xdp_put_sq(vi, q) { \ 55597c2c69eSXuan Zhuo struct netdev_queue *txq; \ 55697c2c69eSXuan Zhuo typeof(vi) v = (vi); \ 55797c2c69eSXuan Zhuo \ 55897c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \ 55997c2c69eSXuan Zhuo if (v->curr_queue_pairs > nr_cpu_ids) \ 56097c2c69eSXuan Zhuo __netif_tx_release(txq); \ 56197c2c69eSXuan Zhuo else \ 56297c2c69eSXuan Zhuo __netif_tx_unlock(txq); \ 5632a43565cSToshiaki Makita } 5642a43565cSToshiaki Makita 565735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev, 56642b33468SJesper Dangaard Brouer int n, struct xdp_frame **frames, u32 flags) 567186b3c99SJason Wang { 568186b3c99SJason Wang struct virtnet_info *vi = netdev_priv(dev); 5698dcc5b0aSJesper Dangaard Brouer struct receive_queue *rq = vi->rq; 5708dcc5b0aSJesper Dangaard Brouer struct bpf_prog *xdp_prog; 571735fc405SJesper Dangaard Brouer struct send_queue *sq; 572735fc405SJesper Dangaard Brouer unsigned int len; 573546f2897SToshiaki Makita int packets = 0; 574546f2897SToshiaki Makita int bytes = 0; 575fdc13979SLorenzo Bianconi int nxmit = 0; 576461f03dcSToshiaki Makita int kicks = 0; 5775050471dSToshiaki Makita void *ptr; 578fdc13979SLorenzo Bianconi int ret; 579735fc405SJesper Dangaard Brouer int i; 580735fc405SJesper Dangaard Brouer 5818dcc5b0aSJesper Dangaard Brouer /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 5828dcc5b0aSJesper Dangaard Brouer * indicate XDP resources have been successfully allocated. 5838dcc5b0aSJesper Dangaard Brouer */ 5849719c6b9SJohn Fastabend xdp_prog = rcu_access_pointer(rq->xdp_prog); 5851667c08aSToshiaki Makita if (!xdp_prog) 5861667c08aSToshiaki Makita return -ENXIO; 5871667c08aSToshiaki Makita 58897c2c69eSXuan Zhuo sq = virtnet_xdp_get_sq(vi); 5899ab86bbcSShirley Ma 5909ab86bbcSShirley Ma if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { 591186b3c99SJason Wang ret = -EINVAL; 592186b3c99SJason Wang goto out; 593186b3c99SJason Wang } 594186b3c99SJason Wang 595735fc405SJesper Dangaard Brouer /* Free up any pending old buffers before queueing new ones. */ 5965050471dSToshiaki Makita while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 597546f2897SToshiaki Makita if (likely(is_xdp_frame(ptr))) { 598546f2897SToshiaki Makita struct xdp_frame *frame = ptr_to_xdp(ptr); 599546f2897SToshiaki Makita 600546f2897SToshiaki Makita bytes += frame->len; 601546f2897SToshiaki Makita xdp_return_frame(frame); 602546f2897SToshiaki Makita } else { 603546f2897SToshiaki Makita struct sk_buff *skb = ptr; 604546f2897SToshiaki Makita 605546f2897SToshiaki Makita bytes += skb->len; 606546f2897SToshiaki Makita napi_consume_skb(skb, false); 607546f2897SToshiaki Makita } 608546f2897SToshiaki Makita packets++; 6095050471dSToshiaki Makita } 610735fc405SJesper Dangaard Brouer 611735fc405SJesper Dangaard Brouer for (i = 0; i < n; i++) { 612735fc405SJesper Dangaard Brouer struct xdp_frame *xdpf = frames[i]; 613735fc405SJesper Dangaard Brouer 614fdc13979SLorenzo Bianconi if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) 615fdc13979SLorenzo Bianconi break; 616fdc13979SLorenzo Bianconi nxmit++; 617735fc405SJesper Dangaard Brouer } 618fdc13979SLorenzo Bianconi ret = nxmit; 6195d274cb4SJesper Dangaard Brouer 620461f03dcSToshiaki Makita if (flags & XDP_XMIT_FLUSH) { 621461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) 622461f03dcSToshiaki Makita kicks = 1; 623461f03dcSToshiaki Makita } 6245b8f3c8dSToshiaki Makita out: 6255b8f3c8dSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 626546f2897SToshiaki Makita sq->stats.bytes += bytes; 627546f2897SToshiaki Makita sq->stats.packets += packets; 6285b8f3c8dSToshiaki Makita sq->stats.xdp_tx += n; 629fdc13979SLorenzo Bianconi sq->stats.xdp_tx_drops += n - nxmit; 630461f03dcSToshiaki Makita sq->stats.kicks += kicks; 6315b8f3c8dSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 6325d274cb4SJesper Dangaard Brouer 63397c2c69eSXuan Zhuo virtnet_xdp_put_sq(vi, sq); 6345b8f3c8dSToshiaki Makita return ret; 635186b3c99SJason Wang } 636186b3c99SJason Wang 637f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 638f6b10209SJason Wang { 63997c2c69eSXuan Zhuo return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; 640f6b10209SJason Wang } 641f6b10209SJason Wang 6424941d472SJason Wang /* We copy the packet for XDP in the following cases: 6434941d472SJason Wang * 6444941d472SJason Wang * 1) Packet is scattered across multiple rx buffers. 6454941d472SJason Wang * 2) Headroom space is insufficient. 6464941d472SJason Wang * 6474941d472SJason Wang * This is inefficient but it's a temporary condition that 6484941d472SJason Wang * we hit right after XDP is enabled and until queue is refilled 6494941d472SJason Wang * with large buffers with sufficient headroom - so it should affect 6504941d472SJason Wang * at most queue size packets. 6514941d472SJason Wang * Afterwards, the conditions to enable 6524941d472SJason Wang * XDP should preclude the underlying device from sending packets 6534941d472SJason Wang * across multiple buffers (num_buf > 1), and we make sure buffers 6544941d472SJason Wang * have enough headroom. 65572979a6cSJohn Fastabend */ 65672979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq, 65756a86f84SJason Wang u16 *num_buf, 65872979a6cSJohn Fastabend struct page *p, 65972979a6cSJohn Fastabend int offset, 6604941d472SJason Wang int page_off, 66172979a6cSJohn Fastabend unsigned int *len) 66272979a6cSJohn Fastabend { 66372979a6cSJohn Fastabend struct page *page = alloc_page(GFP_ATOMIC); 66472979a6cSJohn Fastabend 66572979a6cSJohn Fastabend if (!page) 66672979a6cSJohn Fastabend return NULL; 66772979a6cSJohn Fastabend 66872979a6cSJohn Fastabend memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 66972979a6cSJohn Fastabend page_off += *len; 67072979a6cSJohn Fastabend 67156a86f84SJason Wang while (--*num_buf) { 6723cc81a9aSJason Wang int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 67372979a6cSJohn Fastabend unsigned int buflen; 67472979a6cSJohn Fastabend void *buf; 67572979a6cSJohn Fastabend int off; 67672979a6cSJohn Fastabend 677680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &buflen); 678680557cfSMichael S. Tsirkin if (unlikely(!buf)) 67972979a6cSJohn Fastabend goto err_buf; 68072979a6cSJohn Fastabend 68172979a6cSJohn Fastabend p = virt_to_head_page(buf); 68272979a6cSJohn Fastabend off = buf - page_address(p); 68372979a6cSJohn Fastabend 68456a86f84SJason Wang /* guard against a misconfigured or uncooperative backend that 68556a86f84SJason Wang * is sending packet larger than the MTU. 68656a86f84SJason Wang */ 6873cc81a9aSJason Wang if ((page_off + buflen + tailroom) > PAGE_SIZE) { 68856a86f84SJason Wang put_page(p); 68956a86f84SJason Wang goto err_buf; 69056a86f84SJason Wang } 69156a86f84SJason Wang 69272979a6cSJohn Fastabend memcpy(page_address(page) + page_off, 69372979a6cSJohn Fastabend page_address(p) + off, buflen); 69472979a6cSJohn Fastabend page_off += buflen; 69556a86f84SJason Wang put_page(p); 69672979a6cSJohn Fastabend } 69772979a6cSJohn Fastabend 6982de2f7f4SJohn Fastabend /* Headroom does not contribute to packet length */ 6992de2f7f4SJohn Fastabend *len = page_off - VIRTIO_XDP_HEADROOM; 70072979a6cSJohn Fastabend return page; 70172979a6cSJohn Fastabend err_buf: 70272979a6cSJohn Fastabend __free_pages(page, 0); 70372979a6cSJohn Fastabend return NULL; 70472979a6cSJohn Fastabend } 70572979a6cSJohn Fastabend 7064941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev, 7074941d472SJason Wang struct virtnet_info *vi, 7084941d472SJason Wang struct receive_queue *rq, 7094941d472SJason Wang void *buf, void *ctx, 710186b3c99SJason Wang unsigned int len, 7117d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 712d46eeeafSJason Wang struct virtnet_rq_stats *stats) 7134941d472SJason Wang { 7144941d472SJason Wang struct sk_buff *skb; 7154941d472SJason Wang struct bpf_prog *xdp_prog; 7164941d472SJason Wang unsigned int xdp_headroom = (unsigned long)ctx; 7174941d472SJason Wang unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 7184941d472SJason Wang unsigned int headroom = vi->hdr_len + header_offset; 7194941d472SJason Wang unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 7204941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 7214941d472SJason Wang struct page *page = virt_to_head_page(buf); 72211b7d897SJesper Dangaard Brouer unsigned int delta = 0; 7234941d472SJason Wang struct page *xdp_page; 72411b7d897SJesper Dangaard Brouer int err; 725503d539aSYuya Kusakabe unsigned int metasize = 0; 72611b7d897SJesper Dangaard Brouer 7274941d472SJason Wang len -= vi->hdr_len; 728d46eeeafSJason Wang stats->bytes += len; 7294941d472SJason Wang 730ad993a95SXie Yongji if (unlikely(len > GOOD_PACKET_LEN)) { 731ad993a95SXie Yongji pr_debug("%s: rx error: len %u exceeds max size %d\n", 732ad993a95SXie Yongji dev->name, len, GOOD_PACKET_LEN); 733ad993a95SXie Yongji dev->stats.rx_length_errors++; 734ad993a95SXie Yongji goto err_len; 735ad993a95SXie Yongji } 7364941d472SJason Wang rcu_read_lock(); 7374941d472SJason Wang xdp_prog = rcu_dereference(rq->xdp_prog); 7384941d472SJason Wang if (xdp_prog) { 7394941d472SJason Wang struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 74044fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 7414941d472SJason Wang struct xdp_buff xdp; 7424941d472SJason Wang void *orig_data; 7434941d472SJason Wang u32 act; 7444941d472SJason Wang 74595dbe9e7SJesper Dangaard Brouer if (unlikely(hdr->hdr.gso_type)) 7464941d472SJason Wang goto err_xdp; 7474941d472SJason Wang 7484941d472SJason Wang if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 7494941d472SJason Wang int offset = buf - page_address(page) + header_offset; 7504941d472SJason Wang unsigned int tlen = len + vi->hdr_len; 7514941d472SJason Wang u16 num_buf = 1; 7524941d472SJason Wang 7534941d472SJason Wang xdp_headroom = virtnet_get_headroom(vi); 7544941d472SJason Wang header_offset = VIRTNET_RX_PAD + xdp_headroom; 7554941d472SJason Wang headroom = vi->hdr_len + header_offset; 7564941d472SJason Wang buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 7574941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 7584941d472SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, page, 7594941d472SJason Wang offset, header_offset, 7604941d472SJason Wang &tlen); 7614941d472SJason Wang if (!xdp_page) 7624941d472SJason Wang goto err_xdp; 7634941d472SJason Wang 7644941d472SJason Wang buf = page_address(xdp_page); 7654941d472SJason Wang put_page(page); 7664941d472SJason Wang page = xdp_page; 7674941d472SJason Wang } 7684941d472SJason Wang 76943b5169dSLorenzo Bianconi xdp_init_buff(&xdp, buflen, &rq->xdp_rxq); 770be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, 771be9df4afSLorenzo Bianconi xdp_headroom, len, true); 7724941d472SJason Wang orig_data = xdp.data; 7734941d472SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 774d46eeeafSJason Wang stats->xdp_packets++; 7754941d472SJason Wang 7764941d472SJason Wang switch (act) { 7774941d472SJason Wang case XDP_PASS: 7784941d472SJason Wang /* Recalculate length in case bpf program changed it */ 7794941d472SJason Wang delta = orig_data - xdp.data; 7806870de43SNikita V. Shirokov len = xdp.data_end - xdp.data; 781503d539aSYuya Kusakabe metasize = xdp.data - xdp.data_meta; 7824941d472SJason Wang break; 7834941d472SJason Wang case XDP_TX: 784d46eeeafSJason Wang stats->xdp_tx++; 7851b698fa5SLorenzo Bianconi xdpf = xdp_convert_buff_to_frame(&xdp); 78644fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 78744fa2dbdSJesper Dangaard Brouer goto err_xdp; 788ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 789fdc13979SLorenzo Bianconi if (unlikely(!err)) { 790fdc13979SLorenzo Bianconi xdp_return_frame_rx_napi(xdpf); 791fdc13979SLorenzo Bianconi } else if (unlikely(err < 0)) { 7924941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 79311b7d897SJesper Dangaard Brouer goto err_xdp; 79411b7d897SJesper Dangaard Brouer } 7952471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 796186b3c99SJason Wang rcu_read_unlock(); 797186b3c99SJason Wang goto xdp_xmit; 798186b3c99SJason Wang case XDP_REDIRECT: 799d46eeeafSJason Wang stats->xdp_redirects++; 800186b3c99SJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 80111b7d897SJesper Dangaard Brouer if (err) 80211b7d897SJesper Dangaard Brouer goto err_xdp; 8032471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 8044941d472SJason Wang rcu_read_unlock(); 8054941d472SJason Wang goto xdp_xmit; 8064941d472SJason Wang default: 8074941d472SJason Wang bpf_warn_invalid_xdp_action(act); 808df561f66SGustavo A. R. Silva fallthrough; 8094941d472SJason Wang case XDP_ABORTED: 8104941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 81195efabf0SGustavo A. R. Silva goto err_xdp; 8124941d472SJason Wang case XDP_DROP: 8134941d472SJason Wang goto err_xdp; 8144941d472SJason Wang } 8154941d472SJason Wang } 8164941d472SJason Wang rcu_read_unlock(); 8174941d472SJason Wang 8184941d472SJason Wang skb = build_skb(buf, buflen); 8194941d472SJason Wang if (!skb) { 8204941d472SJason Wang put_page(page); 8214941d472SJason Wang goto err; 8224941d472SJason Wang } 8234941d472SJason Wang skb_reserve(skb, headroom - delta); 8246870de43SNikita V. Shirokov skb_put(skb, len); 825f1d4884dSYuya Kusakabe if (!xdp_prog) { 8264941d472SJason Wang buf += header_offset; 8274941d472SJason Wang memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 828f1d4884dSYuya Kusakabe } /* keep zeroed vnet hdr since XDP is loaded */ 8294941d472SJason Wang 830503d539aSYuya Kusakabe if (metasize) 831503d539aSYuya Kusakabe skb_metadata_set(skb, metasize); 832503d539aSYuya Kusakabe 8334941d472SJason Wang err: 8344941d472SJason Wang return skb; 8354941d472SJason Wang 8364941d472SJason Wang err_xdp: 8374941d472SJason Wang rcu_read_unlock(); 838d46eeeafSJason Wang stats->xdp_drops++; 839ad993a95SXie Yongji err_len: 840d46eeeafSJason Wang stats->drops++; 8414941d472SJason Wang put_page(page); 8424941d472SJason Wang xdp_xmit: 8434941d472SJason Wang return NULL; 8444941d472SJason Wang } 8454941d472SJason Wang 8464941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev, 8474941d472SJason Wang struct virtnet_info *vi, 8484941d472SJason Wang struct receive_queue *rq, 8494941d472SJason Wang void *buf, 8507d9d60fdSToshiaki Makita unsigned int len, 851d46eeeafSJason Wang struct virtnet_rq_stats *stats) 8524941d472SJason Wang { 8534941d472SJason Wang struct page *page = buf; 854503d539aSYuya Kusakabe struct sk_buff *skb = 855fb32856bSXuan Zhuo page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0); 8564941d472SJason Wang 857d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 8584941d472SJason Wang if (unlikely(!skb)) 8594941d472SJason Wang goto err; 8604941d472SJason Wang 8614941d472SJason Wang return skb; 8624941d472SJason Wang 8634941d472SJason Wang err: 864d46eeeafSJason Wang stats->drops++; 8654941d472SJason Wang give_pages(rq, page); 8664941d472SJason Wang return NULL; 8674941d472SJason Wang } 8684941d472SJason Wang 8698fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 870fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 8718fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 872680557cfSMichael S. Tsirkin void *buf, 873680557cfSMichael S. Tsirkin void *ctx, 874186b3c99SJason Wang unsigned int len, 8757d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 876d46eeeafSJason Wang struct virtnet_rq_stats *stats) 8779ab86bbcSShirley Ma { 878012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 879012873d0SMichael S. Tsirkin u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 8808fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 8818fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 882f600b690SJohn Fastabend struct sk_buff *head_skb, *curr_skb; 883f600b690SJohn Fastabend struct bpf_prog *xdp_prog; 8849ce6146eSJesper Dangaard Brouer unsigned int truesize = mergeable_ctx_to_truesize(ctx); 8854941d472SJason Wang unsigned int headroom = mergeable_ctx_to_headroom(ctx); 886503d539aSYuya Kusakabe unsigned int metasize = 0; 8879ce6146eSJesper Dangaard Brouer unsigned int frame_sz; 8889ce6146eSJesper Dangaard Brouer int err; 889ab7db917SMichael Dalton 89056434a01SJohn Fastabend head_skb = NULL; 891d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 89256434a01SJohn Fastabend 893ad993a95SXie Yongji if (unlikely(len > truesize)) { 894ad993a95SXie Yongji pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 895ad993a95SXie Yongji dev->name, len, (unsigned long)ctx); 896ad993a95SXie Yongji dev->stats.rx_length_errors++; 897ad993a95SXie Yongji goto err_skb; 898ad993a95SXie Yongji } 899f600b690SJohn Fastabend rcu_read_lock(); 900f600b690SJohn Fastabend xdp_prog = rcu_dereference(rq->xdp_prog); 901f600b690SJohn Fastabend if (xdp_prog) { 90244fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 90372979a6cSJohn Fastabend struct page *xdp_page; 9040354e4d1SJohn Fastabend struct xdp_buff xdp; 9050354e4d1SJohn Fastabend void *data; 906f600b690SJohn Fastabend u32 act; 907f600b690SJohn Fastabend 9083d62b2a0SJason Wang /* Transient failure which in theory could occur if 9093d62b2a0SJason Wang * in-flight packets from before XDP was enabled reach 9103d62b2a0SJason Wang * the receive path after XDP is loaded. 9113d62b2a0SJason Wang */ 9123d62b2a0SJason Wang if (unlikely(hdr->hdr.gso_type)) 9133d62b2a0SJason Wang goto err_xdp; 9143d62b2a0SJason Wang 9159ce6146eSJesper Dangaard Brouer /* Buffers with headroom use PAGE_SIZE as alloc size, 9169ce6146eSJesper Dangaard Brouer * see add_recvbuf_mergeable() + get_mergeable_buf_len() 9179ce6146eSJesper Dangaard Brouer */ 9189ce6146eSJesper Dangaard Brouer frame_sz = headroom ? PAGE_SIZE : truesize; 9199ce6146eSJesper Dangaard Brouer 9203cc81a9aSJason Wang /* This happens when rx buffer size is underestimated 9213cc81a9aSJason Wang * or headroom is not enough because of the buffer 9223cc81a9aSJason Wang * was refilled before XDP is set. This should only 9233cc81a9aSJason Wang * happen for the first several packets, so we don't 9243cc81a9aSJason Wang * care much about its performance. 9253cc81a9aSJason Wang */ 9264941d472SJason Wang if (unlikely(num_buf > 1 || 9274941d472SJason Wang headroom < virtnet_get_headroom(vi))) { 92872979a6cSJohn Fastabend /* linearize data for XDP */ 92956a86f84SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, 9304941d472SJason Wang page, offset, 9314941d472SJason Wang VIRTIO_XDP_HEADROOM, 9324941d472SJason Wang &len); 9339ce6146eSJesper Dangaard Brouer frame_sz = PAGE_SIZE; 9349ce6146eSJesper Dangaard Brouer 93572979a6cSJohn Fastabend if (!xdp_page) 936f600b690SJohn Fastabend goto err_xdp; 9372de2f7f4SJohn Fastabend offset = VIRTIO_XDP_HEADROOM; 93872979a6cSJohn Fastabend } else { 93972979a6cSJohn Fastabend xdp_page = page; 940f600b690SJohn Fastabend } 941f600b690SJohn Fastabend 9422de2f7f4SJohn Fastabend /* Allow consuming headroom but reserve enough space to push 9432de2f7f4SJohn Fastabend * the descriptor on if we get an XDP_TX return code. 9442de2f7f4SJohn Fastabend */ 9450354e4d1SJohn Fastabend data = page_address(xdp_page) + offset; 94643b5169dSLorenzo Bianconi xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq); 947be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len, 948be9df4afSLorenzo Bianconi VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true); 949754b8a21SJesper Dangaard Brouer 9500354e4d1SJohn Fastabend act = bpf_prog_run_xdp(xdp_prog, &xdp); 951d46eeeafSJason Wang stats->xdp_packets++; 9520354e4d1SJohn Fastabend 95356434a01SJohn Fastabend switch (act) { 95456434a01SJohn Fastabend case XDP_PASS: 955503d539aSYuya Kusakabe metasize = xdp.data - xdp.data_meta; 9562de2f7f4SJohn Fastabend 957503d539aSYuya Kusakabe /* recalculate offset to account for any header 958503d539aSYuya Kusakabe * adjustments and minus the metasize to copy the 959503d539aSYuya Kusakabe * metadata in page_to_skb(). Note other cases do not 960503d539aSYuya Kusakabe * build an skb and avoid using offset 9616870de43SNikita V. Shirokov */ 962503d539aSYuya Kusakabe offset = xdp.data - page_address(xdp_page) - 963503d539aSYuya Kusakabe vi->hdr_len - metasize; 964503d539aSYuya Kusakabe 965503d539aSYuya Kusakabe /* recalculate len if xdp.data, xdp.data_end or 966503d539aSYuya Kusakabe * xdp.data_meta were adjusted 967503d539aSYuya Kusakabe */ 968503d539aSYuya Kusakabe len = xdp.data_end - xdp.data + vi->hdr_len + metasize; 9691830f893SJason Wang /* We can only create skb based on xdp_page. */ 9701830f893SJason Wang if (unlikely(xdp_page != page)) { 9711830f893SJason Wang rcu_read_unlock(); 9721830f893SJason Wang put_page(page); 973503d539aSYuya Kusakabe head_skb = page_to_skb(vi, rq, xdp_page, offset, 974503d539aSYuya Kusakabe len, PAGE_SIZE, false, 975c32325b8SJakub Kicinski metasize, 976c32325b8SJakub Kicinski VIRTIO_XDP_HEADROOM); 9771830f893SJason Wang return head_skb; 9781830f893SJason Wang } 97956434a01SJohn Fastabend break; 98056434a01SJohn Fastabend case XDP_TX: 981d46eeeafSJason Wang stats->xdp_tx++; 9821b698fa5SLorenzo Bianconi xdpf = xdp_convert_buff_to_frame(&xdp); 98344fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 98444fa2dbdSJesper Dangaard Brouer goto err_xdp; 985ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 986fdc13979SLorenzo Bianconi if (unlikely(!err)) { 987fdc13979SLorenzo Bianconi xdp_return_frame_rx_napi(xdpf); 988fdc13979SLorenzo Bianconi } else if (unlikely(err < 0)) { 9890354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 99011b7d897SJesper Dangaard Brouer if (unlikely(xdp_page != page)) 99111b7d897SJesper Dangaard Brouer put_page(xdp_page); 99211b7d897SJesper Dangaard Brouer goto err_xdp; 99311b7d897SJesper Dangaard Brouer } 9942471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 99572979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 9965d458a13SJason Wang put_page(page); 99756434a01SJohn Fastabend rcu_read_unlock(); 99856434a01SJohn Fastabend goto xdp_xmit; 9993cc81a9aSJason Wang case XDP_REDIRECT: 1000d46eeeafSJason Wang stats->xdp_redirects++; 10013cc81a9aSJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 10023cc81a9aSJason Wang if (err) { 10033cc81a9aSJason Wang if (unlikely(xdp_page != page)) 10043cc81a9aSJason Wang put_page(xdp_page); 10053cc81a9aSJason Wang goto err_xdp; 10063cc81a9aSJason Wang } 10072471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 10083cc81a9aSJason Wang if (unlikely(xdp_page != page)) 10096890418bSJason Wang put_page(page); 10103cc81a9aSJason Wang rcu_read_unlock(); 10113cc81a9aSJason Wang goto xdp_xmit; 101256434a01SJohn Fastabend default: 10130354e4d1SJohn Fastabend bpf_warn_invalid_xdp_action(act); 1014df561f66SGustavo A. R. Silva fallthrough; 10150354e4d1SJohn Fastabend case XDP_ABORTED: 10160354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 1017df561f66SGustavo A. R. Silva fallthrough; 10180354e4d1SJohn Fastabend case XDP_DROP: 101972979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 102072979a6cSJohn Fastabend __free_pages(xdp_page, 0); 1021f600b690SJohn Fastabend goto err_xdp; 1022f600b690SJohn Fastabend } 102356434a01SJohn Fastabend } 1024f600b690SJohn Fastabend rcu_read_unlock(); 1025f600b690SJohn Fastabend 1026503d539aSYuya Kusakabe head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog, 1027c32325b8SJakub Kicinski metasize, headroom); 1028f600b690SJohn Fastabend curr_skb = head_skb; 10299ab86bbcSShirley Ma 10308fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 10318fc3b9e9SMichael S. Tsirkin goto err_skb; 10329ab86bbcSShirley Ma while (--num_buf) { 10338fc3b9e9SMichael S. Tsirkin int num_skb_frags; 10348fc3b9e9SMichael S. Tsirkin 1035680557cfSMichael S. Tsirkin buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 103603e9f8a0SYunjian Wang if (unlikely(!buf)) { 10378fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 1038fdd819b2SMichael S. Tsirkin dev->name, num_buf, 1039012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 1040012873d0SMichael S. Tsirkin hdr->num_buffers)); 10418fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 10428fc3b9e9SMichael S. Tsirkin goto err_buf; 10433f2c31d9SMark McLoughlin } 10448fc3b9e9SMichael S. Tsirkin 1045d46eeeafSJason Wang stats->bytes += len; 10468fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 104728b39bc7SJason Wang 104828b39bc7SJason Wang truesize = mergeable_ctx_to_truesize(ctx); 104928b39bc7SJason Wang if (unlikely(len > truesize)) { 105056da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1051680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 1052680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 1053680557cfSMichael S. Tsirkin goto err_skb; 1054680557cfSMichael S. Tsirkin } 10558fc3b9e9SMichael S. Tsirkin 10568fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 10572613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 10582613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 10598fc3b9e9SMichael S. Tsirkin 10608fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 10618fc3b9e9SMichael S. Tsirkin goto err_skb; 10622613af0eSMichael Dalton if (curr_skb == head_skb) 10632613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 10642613af0eSMichael Dalton else 10652613af0eSMichael Dalton curr_skb->next = nskb; 10662613af0eSMichael Dalton curr_skb = nskb; 10672613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 10682613af0eSMichael Dalton num_skb_frags = 0; 10692613af0eSMichael Dalton } 10702613af0eSMichael Dalton if (curr_skb != head_skb) { 10712613af0eSMichael Dalton head_skb->data_len += len; 10722613af0eSMichael Dalton head_skb->len += len; 1073fb51879dSMichael Dalton head_skb->truesize += truesize; 10742613af0eSMichael Dalton } 10758fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 1076ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 1077ba275241SJason Wang put_page(page); 1078ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 1079fb51879dSMichael Dalton len, truesize); 1080ba275241SJason Wang } else { 10812613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 1082fb51879dSMichael Dalton offset, len, truesize); 1083ba275241SJason Wang } 10848fc3b9e9SMichael S. Tsirkin } 10858fc3b9e9SMichael S. Tsirkin 10865377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 10878fc3b9e9SMichael S. Tsirkin return head_skb; 10888fc3b9e9SMichael S. Tsirkin 1089f600b690SJohn Fastabend err_xdp: 1090f600b690SJohn Fastabend rcu_read_unlock(); 1091d46eeeafSJason Wang stats->xdp_drops++; 10928fc3b9e9SMichael S. Tsirkin err_skb: 10938fc3b9e9SMichael S. Tsirkin put_page(page); 1094850e088dSJason Wang while (num_buf-- > 1) { 1095680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &len); 1096680557cfSMichael S. Tsirkin if (unlikely(!buf)) { 10978fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 10988fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 10998fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 11008fc3b9e9SMichael S. Tsirkin break; 11018fc3b9e9SMichael S. Tsirkin } 1102d46eeeafSJason Wang stats->bytes += len; 1103680557cfSMichael S. Tsirkin page = virt_to_head_page(buf); 11048fc3b9e9SMichael S. Tsirkin put_page(page); 11053f2c31d9SMark McLoughlin } 11068fc3b9e9SMichael S. Tsirkin err_buf: 1107d46eeeafSJason Wang stats->drops++; 11088fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 110956434a01SJohn Fastabend xdp_xmit: 11108fc3b9e9SMichael S. Tsirkin return NULL; 11119ab86bbcSShirley Ma } 11129ab86bbcSShirley Ma 11137d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 11142471c75eSJesper Dangaard Brouer void *buf, unsigned int len, void **ctx, 1115a0929a44SToshiaki Makita unsigned int *xdp_xmit, 1116d46eeeafSJason Wang struct virtnet_rq_stats *stats) 11179ab86bbcSShirley Ma { 1118e9d7417bSJason Wang struct net_device *dev = vi->dev; 11199ab86bbcSShirley Ma struct sk_buff *skb; 1120012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 11219ab86bbcSShirley Ma 1122bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 11239ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 11249ab86bbcSShirley Ma dev->stats.rx_length_errors++; 1125ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 1126680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 1127ab7db917SMichael Dalton } else if (vi->big_packets) { 112898bfd23cSMichael Dalton give_pages(rq, buf); 1129ab7db917SMichael Dalton } else { 1130f6b10209SJason Wang put_page(virt_to_head_page(buf)); 1131ab7db917SMichael Dalton } 11327d9d60fdSToshiaki Makita return; 11339ab86bbcSShirley Ma } 11349ab86bbcSShirley Ma 1135f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 11367d9d60fdSToshiaki Makita skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, 1137a0929a44SToshiaki Makita stats); 1138f121159dSMichael S. Tsirkin else if (vi->big_packets) 1139a0929a44SToshiaki Makita skb = receive_big(dev, vi, rq, buf, len, stats); 1140f121159dSMichael S. Tsirkin else 1141a0929a44SToshiaki Makita skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); 1142f121159dSMichael S. Tsirkin 11438fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 11447d9d60fdSToshiaki Makita return; 11453f2c31d9SMark McLoughlin 11469ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 11473fa2a1dfSstephen hemminger 1148e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 114910a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 1150296f96fcSRusty Russell 1151e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 1152e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 1153e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 1154e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 1155fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 1156296f96fcSRusty Russell goto frame_err; 1157296f96fcSRusty Russell } 1158296f96fcSRusty Russell 1159133bbb18SWillem de Bruijn skb_record_rx_queue(skb, vq2rxq(rq->vq)); 1160d1dc06dcSMike Rapoport skb->protocol = eth_type_trans(skb, dev); 1161d1dc06dcSMike Rapoport pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 1162d1dc06dcSMike Rapoport ntohs(skb->protocol), skb->len, skb->pkt_type); 1163d1dc06dcSMike Rapoport 11640fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 11657d9d60fdSToshiaki Makita return; 1166296f96fcSRusty Russell 1167296f96fcSRusty Russell frame_err: 1168296f96fcSRusty Russell dev->stats.rx_frame_errors++; 1169296f96fcSRusty Russell dev_kfree_skb(skb); 1170296f96fcSRusty Russell } 1171296f96fcSRusty Russell 1172192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the 1173192f68cfSJason Wang * same size, except for the headroom. For this reason we do 1174192f68cfSJason Wang * not need to use mergeable_len_to_ctx here - it is enough 1175192f68cfSJason Wang * to store the headroom as the context ignoring the truesize. 1176192f68cfSJason Wang */ 1177946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 1178946fa564SMichael S. Tsirkin gfp_t gfp) 1179296f96fcSRusty Russell { 1180f6b10209SJason Wang struct page_frag *alloc_frag = &rq->alloc_frag; 1181f6b10209SJason Wang char *buf; 11822de2f7f4SJohn Fastabend unsigned int xdp_headroom = virtnet_get_headroom(vi); 1183192f68cfSJason Wang void *ctx = (void *)(unsigned long)xdp_headroom; 1184f6b10209SJason Wang int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 11859ab86bbcSShirley Ma int err; 11863f2c31d9SMark McLoughlin 1187f6b10209SJason Wang len = SKB_DATA_ALIGN(len) + 1188f6b10209SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1189f6b10209SJason Wang if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 11909ab86bbcSShirley Ma return -ENOMEM; 1191296f96fcSRusty Russell 1192f6b10209SJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1193f6b10209SJason Wang get_page(alloc_frag->page); 1194f6b10209SJason Wang alloc_frag->offset += len; 1195f6b10209SJason Wang sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 1196f6b10209SJason Wang vi->hdr_len + GOOD_PACKET_LEN); 1197192f68cfSJason Wang err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 11989ab86bbcSShirley Ma if (err < 0) 1199f6b10209SJason Wang put_page(virt_to_head_page(buf)); 12009ab86bbcSShirley Ma return err; 120197402b96SHerbert Xu } 120297402b96SHerbert Xu 1203012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 1204012873d0SMichael S. Tsirkin gfp_t gfp) 12059ab86bbcSShirley Ma { 12069ab86bbcSShirley Ma struct page *first, *list = NULL; 12079ab86bbcSShirley Ma char *p; 12089ab86bbcSShirley Ma int i, err, offset; 1209296f96fcSRusty Russell 1210a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 1211a5835440SRusty Russell 1212e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 12139ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 1214e9d7417bSJason Wang first = get_a_page(rq, gfp); 12159ab86bbcSShirley Ma if (!first) { 12169ab86bbcSShirley Ma if (list) 1217e9d7417bSJason Wang give_pages(rq, list); 12189ab86bbcSShirley Ma return -ENOMEM; 1219296f96fcSRusty Russell } 1220e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 12219ab86bbcSShirley Ma 12229ab86bbcSShirley Ma /* chain new page in list head to match sg */ 12239ab86bbcSShirley Ma first->private = (unsigned long)list; 12249ab86bbcSShirley Ma list = first; 12259ab86bbcSShirley Ma } 12269ab86bbcSShirley Ma 1227e9d7417bSJason Wang first = get_a_page(rq, gfp); 12289ab86bbcSShirley Ma if (!first) { 1229e9d7417bSJason Wang give_pages(rq, list); 12309ab86bbcSShirley Ma return -ENOMEM; 12319ab86bbcSShirley Ma } 12329ab86bbcSShirley Ma p = page_address(first); 12339ab86bbcSShirley Ma 1234e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 1235012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 1236012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 12379ab86bbcSShirley Ma 1238e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 12399ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 1240e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 12419ab86bbcSShirley Ma 12429ab86bbcSShirley Ma /* chain first in list head */ 12439ab86bbcSShirley Ma first->private = (unsigned long)list; 12449dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 1245aa989f5eSMichael S. Tsirkin first, gfp); 12469ab86bbcSShirley Ma if (err < 0) 1247e9d7417bSJason Wang give_pages(rq, first); 12489ab86bbcSShirley Ma 12499ab86bbcSShirley Ma return err; 12509ab86bbcSShirley Ma } 12519ab86bbcSShirley Ma 1252d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 12533cc81a9aSJason Wang struct ewma_pkt_len *avg_pkt_len, 12543cc81a9aSJason Wang unsigned int room) 12559ab86bbcSShirley Ma { 1256ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1257fbf28d78SMichael Dalton unsigned int len; 1258fbf28d78SMichael Dalton 12593cc81a9aSJason Wang if (room) 12603cc81a9aSJason Wang return PAGE_SIZE - room; 12613cc81a9aSJason Wang 12625377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 1263f0c3192cSMichael S. Tsirkin rq->min_buf_len, PAGE_SIZE - hdr_len); 12643cc81a9aSJason Wang 1265e377fcc8SMichael S. Tsirkin return ALIGN(len, L1_CACHE_BYTES); 1266fbf28d78SMichael Dalton } 1267fbf28d78SMichael Dalton 12682de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi, 12692de2f7f4SJohn Fastabend struct receive_queue *rq, gfp_t gfp) 1270fbf28d78SMichael Dalton { 1271fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 12722de2f7f4SJohn Fastabend unsigned int headroom = virtnet_get_headroom(vi); 12733cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 12743cc81a9aSJason Wang unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1275fb51879dSMichael Dalton char *buf; 1276680557cfSMichael S. Tsirkin void *ctx; 12779ab86bbcSShirley Ma int err; 1278fb51879dSMichael Dalton unsigned int len, hole; 12799ab86bbcSShirley Ma 12803cc81a9aSJason Wang /* Extra tailroom is needed to satisfy XDP's assumption. This 12813cc81a9aSJason Wang * means rx frags coalescing won't work, but consider we've 12823cc81a9aSJason Wang * disabled GSO for XDP, it won't be a big issue. 12833cc81a9aSJason Wang */ 12843cc81a9aSJason Wang len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 12853cc81a9aSJason Wang if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp))) 12869ab86bbcSShirley Ma return -ENOMEM; 1287ab7db917SMichael Dalton 1288fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 12892de2f7f4SJohn Fastabend buf += headroom; /* advance address leaving hole at front of pkt */ 1290fb51879dSMichael Dalton get_page(alloc_frag->page); 12913cc81a9aSJason Wang alloc_frag->offset += len + room; 1292fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 12933cc81a9aSJason Wang if (hole < len + room) { 1294ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 1295ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 12961daa8790SMichael S. Tsirkin * the current buffer. 1297ab7db917SMichael Dalton */ 1298fb51879dSMichael Dalton len += hole; 1299fb51879dSMichael Dalton alloc_frag->offset += hole; 1300fb51879dSMichael Dalton } 13019ab86bbcSShirley Ma 1302fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 130329fda25aSDavid S. Miller ctx = mergeable_len_to_ctx(len, headroom); 1304680557cfSMichael S. Tsirkin err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 13059ab86bbcSShirley Ma if (err < 0) 13062613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 13079ab86bbcSShirley Ma 13089ab86bbcSShirley Ma return err; 1309296f96fcSRusty Russell } 1310296f96fcSRusty Russell 1311b2baed69SRusty Russell /* 1312b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 1313b2baed69SRusty Russell * 1314b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 1315b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 1316b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 1317b2baed69SRusty Russell */ 1318946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 1319946fa564SMichael S. Tsirkin gfp_t gfp) 13203f2c31d9SMark McLoughlin { 13213f2c31d9SMark McLoughlin int err; 13221788f495SMichael S. Tsirkin bool oom; 13233f2c31d9SMark McLoughlin 13240aea51c3SAmit Shah do { 13259ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 13262de2f7f4SJohn Fastabend err = add_recvbuf_mergeable(vi, rq, gfp); 13279ab86bbcSShirley Ma else if (vi->big_packets) 1328012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 13299ab86bbcSShirley Ma else 1330946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 13313f2c31d9SMark McLoughlin 13321788f495SMichael S. Tsirkin oom = err == -ENOMEM; 13339ed4cb07SRusty Russell if (err) 13343f2c31d9SMark McLoughlin break; 1335b7dfde95SLinus Torvalds } while (rq->vq->num_free); 1336461f03dcSToshiaki Makita if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) { 133701c32598SMichael S. Tsirkin unsigned long flags; 133801c32598SMichael S. Tsirkin 133901c32598SMichael S. Tsirkin flags = u64_stats_update_begin_irqsave(&rq->stats.syncp); 1340d46eeeafSJason Wang rq->stats.kicks++; 134101c32598SMichael S. Tsirkin u64_stats_update_end_irqrestore(&rq->stats.syncp, flags); 1342461f03dcSToshiaki Makita } 1343461f03dcSToshiaki Makita 13443161e453SRusty Russell return !oom; 13453f2c31d9SMark McLoughlin } 13463f2c31d9SMark McLoughlin 134718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 1348296f96fcSRusty Russell { 1349296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 1350986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1351e9d7417bSJason Wang 1352e4e8452aSWillem de Bruijn virtqueue_napi_schedule(&rq->napi, rvq); 1353296f96fcSRusty Russell } 1354296f96fcSRusty Russell 1355e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 13563e9d08ecSBruce Rogers { 1357e4e8452aSWillem de Bruijn napi_enable(napi); 13583e9d08ecSBruce Rogers 13593e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 1360e4e8452aSWillem de Bruijn * won't get another interrupt, so process any outstanding packets now. 1361e4e8452aSWillem de Bruijn * Call local_bh_enable after to trigger softIRQ processing. 1362e4e8452aSWillem de Bruijn */ 1363ec13ee80SMichael S. Tsirkin local_bh_disable(); 1364e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 1365ec13ee80SMichael S. Tsirkin local_bh_enable(); 13663e9d08ecSBruce Rogers } 13673e9d08ecSBruce Rogers 1368b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1369b92f1e67SWillem de Bruijn struct virtqueue *vq, 1370b92f1e67SWillem de Bruijn struct napi_struct *napi) 1371b92f1e67SWillem de Bruijn { 1372b92f1e67SWillem de Bruijn if (!napi->weight) 1373b92f1e67SWillem de Bruijn return; 1374b92f1e67SWillem de Bruijn 1375b92f1e67SWillem de Bruijn /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1376b92f1e67SWillem de Bruijn * enable the feature if this is likely affine with the transmit path. 1377b92f1e67SWillem de Bruijn */ 1378b92f1e67SWillem de Bruijn if (!vi->affinity_hint_set) { 1379b92f1e67SWillem de Bruijn napi->weight = 0; 1380b92f1e67SWillem de Bruijn return; 1381b92f1e67SWillem de Bruijn } 1382b92f1e67SWillem de Bruijn 1383b92f1e67SWillem de Bruijn return virtnet_napi_enable(vq, napi); 1384b92f1e67SWillem de Bruijn } 1385b92f1e67SWillem de Bruijn 138678a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi) 138778a57b48SWillem de Bruijn { 138878a57b48SWillem de Bruijn if (napi->weight) 138978a57b48SWillem de Bruijn napi_disable(napi); 139078a57b48SWillem de Bruijn } 139178a57b48SWillem de Bruijn 13923161e453SRusty Russell static void refill_work(struct work_struct *work) 13933161e453SRusty Russell { 1394e9d7417bSJason Wang struct virtnet_info *vi = 1395e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 13963161e453SRusty Russell bool still_empty; 1397986a4f4dSJason Wang int i; 13983161e453SRusty Russell 139955257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1400986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 1401986a4f4dSJason Wang 1402986a4f4dSJason Wang napi_disable(&rq->napi); 1403946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1404e4e8452aSWillem de Bruijn virtnet_napi_enable(rq->vq, &rq->napi); 14053161e453SRusty Russell 14063161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 1407986a4f4dSJason Wang * we will *never* try to fill again. 1408986a4f4dSJason Wang */ 14093161e453SRusty Russell if (still_empty) 14103b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 14113161e453SRusty Russell } 1412986a4f4dSJason Wang } 14133161e453SRusty Russell 14142471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget, 14152471c75eSJesper Dangaard Brouer unsigned int *xdp_xmit) 1416296f96fcSRusty Russell { 1417e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 1418d46eeeafSJason Wang struct virtnet_rq_stats stats = {}; 1419a0929a44SToshiaki Makita unsigned int len; 14209ab86bbcSShirley Ma void *buf; 1421a0929a44SToshiaki Makita int i; 1422296f96fcSRusty Russell 1423192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 1424680557cfSMichael S. Tsirkin void *ctx; 1425680557cfSMichael S. Tsirkin 1426d46eeeafSJason Wang while (stats.packets < budget && 1427680557cfSMichael S. Tsirkin (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1428a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); 1429d46eeeafSJason Wang stats.packets++; 1430680557cfSMichael S. Tsirkin } 1431680557cfSMichael S. Tsirkin } else { 1432d46eeeafSJason Wang while (stats.packets < budget && 1433e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1434a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); 1435d46eeeafSJason Wang stats.packets++; 1436296f96fcSRusty Russell } 1437680557cfSMichael S. Tsirkin } 1438296f96fcSRusty Russell 1439718be6baS? jiang if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { 1440946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 14413b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 14423161e453SRusty Russell } 1443296f96fcSRusty Russell 1444d7dfc5cfSToshiaki Makita u64_stats_update_begin(&rq->stats.syncp); 1445a0929a44SToshiaki Makita for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) { 1446a0929a44SToshiaki Makita size_t offset = virtnet_rq_stats_desc[i].offset; 1447a0929a44SToshiaki Makita u64 *item; 1448a0929a44SToshiaki Makita 1449d46eeeafSJason Wang item = (u64 *)((u8 *)&rq->stats + offset); 1450d46eeeafSJason Wang *item += *(u64 *)((u8 *)&stats + offset); 1451a0929a44SToshiaki Makita } 1452d7dfc5cfSToshiaki Makita u64_stats_update_end(&rq->stats.syncp); 145361845d20SJason Wang 1454d46eeeafSJason Wang return stats.packets; 14552ffa7598SJason Wang } 14562ffa7598SJason Wang 1457df133f3fSMichael S. Tsirkin static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi) 1458ea7735d9SWillem de Bruijn { 1459ea7735d9SWillem de Bruijn unsigned int len; 1460ea7735d9SWillem de Bruijn unsigned int packets = 0; 1461ea7735d9SWillem de Bruijn unsigned int bytes = 0; 14625050471dSToshiaki Makita void *ptr; 1463ea7735d9SWillem de Bruijn 14645050471dSToshiaki Makita while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 14655050471dSToshiaki Makita if (likely(!is_xdp_frame(ptr))) { 14665050471dSToshiaki Makita struct sk_buff *skb = ptr; 14675050471dSToshiaki Makita 1468ea7735d9SWillem de Bruijn pr_debug("Sent skb %p\n", skb); 1469ea7735d9SWillem de Bruijn 1470ea7735d9SWillem de Bruijn bytes += skb->len; 1471df133f3fSMichael S. Tsirkin napi_consume_skb(skb, in_napi); 14725050471dSToshiaki Makita } else { 14735050471dSToshiaki Makita struct xdp_frame *frame = ptr_to_xdp(ptr); 14745050471dSToshiaki Makita 14755050471dSToshiaki Makita bytes += frame->len; 14765050471dSToshiaki Makita xdp_return_frame(frame); 14775050471dSToshiaki Makita } 14785050471dSToshiaki Makita packets++; 1479ea7735d9SWillem de Bruijn } 1480ea7735d9SWillem de Bruijn 1481ea7735d9SWillem de Bruijn /* Avoid overhead when no packets have been processed 1482ea7735d9SWillem de Bruijn * happens when called speculatively from start_xmit. 1483ea7735d9SWillem de Bruijn */ 1484ea7735d9SWillem de Bruijn if (!packets) 1485ea7735d9SWillem de Bruijn return; 1486ea7735d9SWillem de Bruijn 1487d7dfc5cfSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1488d7dfc5cfSToshiaki Makita sq->stats.bytes += bytes; 1489d7dfc5cfSToshiaki Makita sq->stats.packets += packets; 1490d7dfc5cfSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1491ea7735d9SWillem de Bruijn } 1492ea7735d9SWillem de Bruijn 1493534da5e8SToshiaki Makita static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 1494534da5e8SToshiaki Makita { 1495534da5e8SToshiaki Makita if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 1496534da5e8SToshiaki Makita return false; 1497534da5e8SToshiaki Makita else if (q < vi->curr_queue_pairs) 1498534da5e8SToshiaki Makita return true; 1499534da5e8SToshiaki Makita else 1500534da5e8SToshiaki Makita return false; 1501534da5e8SToshiaki Makita } 1502534da5e8SToshiaki Makita 15037b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq) 15047b0411efSWillem de Bruijn { 15057b0411efSWillem de Bruijn struct virtnet_info *vi = rq->vq->vdev->priv; 15067b0411efSWillem de Bruijn unsigned int index = vq2rxq(rq->vq); 15077b0411efSWillem de Bruijn struct send_queue *sq = &vi->sq[index]; 15087b0411efSWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 15097b0411efSWillem de Bruijn 1510534da5e8SToshiaki Makita if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) 15117b0411efSWillem de Bruijn return; 15127b0411efSWillem de Bruijn 15137b0411efSWillem de Bruijn if (__netif_tx_trylock(txq)) { 1514a7766ef1SMichael S. Tsirkin do { 1515a7766ef1SMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 1516df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, true); 1517a7766ef1SMichael S. Tsirkin } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq))); 15187b0411efSWillem de Bruijn 15197b0411efSWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 15207b0411efSWillem de Bruijn netif_tx_wake_queue(txq); 152122bc63c5SMichael S. Tsirkin 152222bc63c5SMichael S. Tsirkin __netif_tx_unlock(txq); 152322bc63c5SMichael S. Tsirkin } 15247b0411efSWillem de Bruijn } 15257b0411efSWillem de Bruijn 15262ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 15272ffa7598SJason Wang { 15282ffa7598SJason Wang struct receive_queue *rq = 15292ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 15309267c430SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 15319267c430SJason Wang struct send_queue *sq; 15322a43565cSToshiaki Makita unsigned int received; 15332471c75eSJesper Dangaard Brouer unsigned int xdp_xmit = 0; 15342ffa7598SJason Wang 15357b0411efSWillem de Bruijn virtnet_poll_cleantx(rq); 15367b0411efSWillem de Bruijn 1537186b3c99SJason Wang received = virtnet_receive(rq, budget, &xdp_xmit); 15382ffa7598SJason Wang 15398329d98eSRusty Russell /* Out of packets? */ 1540e4e8452aSWillem de Bruijn if (received < budget) 1541e4e8452aSWillem de Bruijn virtqueue_napi_complete(napi, rq->vq, received); 1542296f96fcSRusty Russell 15432471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_REDIR) 15441d233886SToke Høiland-Jørgensen xdp_do_flush(); 15452471c75eSJesper Dangaard Brouer 15462471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_TX) { 154797c2c69eSXuan Zhuo sq = virtnet_xdp_get_sq(vi); 1548461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1549461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1550461f03dcSToshiaki Makita sq->stats.kicks++; 1551461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1552461f03dcSToshiaki Makita } 155397c2c69eSXuan Zhuo virtnet_xdp_put_sq(vi, sq); 15549267c430SJason Wang } 1555186b3c99SJason Wang 1556296f96fcSRusty Russell return received; 1557296f96fcSRusty Russell } 1558296f96fcSRusty Russell 1559986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 1560986a4f4dSJason Wang { 1561986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1562754b8a21SJesper Dangaard Brouer int i, err; 1563986a4f4dSJason Wang 1564e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1565e4166625SJason Wang if (i < vi->curr_queue_pairs) 1566986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 1567946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1568986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 1569754b8a21SJesper Dangaard Brouer 1570b02e5a0eSBjörn Töpel err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); 1571754b8a21SJesper Dangaard Brouer if (err < 0) 1572754b8a21SJesper Dangaard Brouer return err; 1573754b8a21SJesper Dangaard Brouer 15748d5d8852SJesper Dangaard Brouer err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq, 15758d5d8852SJesper Dangaard Brouer MEM_TYPE_PAGE_SHARED, NULL); 15768d5d8852SJesper Dangaard Brouer if (err < 0) { 15778d5d8852SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 15788d5d8852SJesper Dangaard Brouer return err; 15798d5d8852SJesper Dangaard Brouer } 15808d5d8852SJesper Dangaard Brouer 1581e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1582b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1583986a4f4dSJason Wang } 1584986a4f4dSJason Wang 1585986a4f4dSJason Wang return 0; 1586986a4f4dSJason Wang } 1587986a4f4dSJason Wang 1588b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1589b92f1e67SWillem de Bruijn { 1590b92f1e67SWillem de Bruijn struct send_queue *sq = container_of(napi, struct send_queue, napi); 1591b92f1e67SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1592534da5e8SToshiaki Makita unsigned int index = vq2txq(sq->vq); 1593534da5e8SToshiaki Makita struct netdev_queue *txq; 15945a2f966dSMichael S. Tsirkin int opaque; 15955a2f966dSMichael S. Tsirkin bool done; 1596b92f1e67SWillem de Bruijn 1597534da5e8SToshiaki Makita if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { 1598534da5e8SToshiaki Makita /* We don't need to enable cb for XDP */ 1599534da5e8SToshiaki Makita napi_complete_done(napi, 0); 1600534da5e8SToshiaki Makita return 0; 1601534da5e8SToshiaki Makita } 1602534da5e8SToshiaki Makita 1603534da5e8SToshiaki Makita txq = netdev_get_tx_queue(vi->dev, index); 1604b92f1e67SWillem de Bruijn __netif_tx_lock(txq, raw_smp_processor_id()); 16055a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 1606df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, true); 16075a2f966dSMichael S. Tsirkin 160822bc63c5SMichael S. Tsirkin if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 160922bc63c5SMichael S. Tsirkin netif_tx_wake_queue(txq); 161022bc63c5SMichael S. Tsirkin 16115a2f966dSMichael S. Tsirkin opaque = virtqueue_enable_cb_prepare(sq->vq); 16125a2f966dSMichael S. Tsirkin 16135a2f966dSMichael S. Tsirkin done = napi_complete_done(napi, 0); 16145a2f966dSMichael S. Tsirkin 16155a2f966dSMichael S. Tsirkin if (!done) 16165a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 16175a2f966dSMichael S. Tsirkin 1618b92f1e67SWillem de Bruijn __netif_tx_unlock(txq); 1619b92f1e67SWillem de Bruijn 16205a2f966dSMichael S. Tsirkin if (done) { 16215a2f966dSMichael S. Tsirkin if (unlikely(virtqueue_poll(sq->vq, opaque))) { 16225a2f966dSMichael S. Tsirkin if (napi_schedule_prep(napi)) { 16235a2f966dSMichael S. Tsirkin __netif_tx_lock(txq, raw_smp_processor_id()); 16245a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 16255a2f966dSMichael S. Tsirkin __netif_tx_unlock(txq); 16265a2f966dSMichael S. Tsirkin __napi_schedule(napi); 16275a2f966dSMichael S. Tsirkin } 16285a2f966dSMichael S. Tsirkin } 16295a2f966dSMichael S. Tsirkin } 1630b92f1e67SWillem de Bruijn 1631b92f1e67SWillem de Bruijn return 0; 1632b92f1e67SWillem de Bruijn } 1633b92f1e67SWillem de Bruijn 1634e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1635296f96fcSRusty Russell { 1636012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 1637296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1638e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 1639e2fcad58SJason A. Donenfeld int num_sg; 1640012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 1641e7428e95SMichael S. Tsirkin bool can_push; 1642296f96fcSRusty Russell 1643e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1644e7428e95SMichael S. Tsirkin 1645e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 1646e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1647e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1648e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 1649e7428e95SMichael S. Tsirkin * csum_start offset below. */ 1650e7428e95SMichael S. Tsirkin if (can_push) 1651012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1652e7428e95SMichael S. Tsirkin else 1653e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 1654296f96fcSRusty Russell 1655e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 1656fd3a8862SWillem de Bruijn virtio_is_little_endian(vi->vdev), false, 1657fd3a8862SWillem de Bruijn 0)) 165885eb1389SXianting Tian return -EPROTO; 1659296f96fcSRusty Russell 1660e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 1661012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 16623f2c31d9SMark McLoughlin 1663547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1664e7428e95SMichael S. Tsirkin if (can_push) { 1665e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 1666e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1667e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1668e2fcad58SJason A. Donenfeld return num_sg; 1669e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 1670e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 1671e7428e95SMichael S. Tsirkin } else { 1672e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 1673e2fcad58SJason A. Donenfeld num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1674e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1675e2fcad58SJason A. Donenfeld return num_sg; 1676e2fcad58SJason A. Donenfeld num_sg++; 1677e7428e95SMichael S. Tsirkin } 16789dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 167911a3a154SRusty Russell } 168011a3a154SRusty Russell 1681424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 168299ffc696SRusty Russell { 168399ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1684986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 1685986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 16869ed4cb07SRusty Russell int err; 16874b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 16886b16f9eeSFlorian Westphal bool kick = !netdev_xmit_more(); 1689b92f1e67SWillem de Bruijn bool use_napi = sq->napi.weight; 16902cb9c6baSRusty Russell 16912cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 1692a7766ef1SMichael S. Tsirkin do { 1693a7766ef1SMichael S. Tsirkin if (use_napi) 1694a7766ef1SMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 1695a7766ef1SMichael S. Tsirkin 1696df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, false); 169799ffc696SRusty Russell 1698a7766ef1SMichael S. Tsirkin } while (use_napi && kick && 1699a7766ef1SMichael S. Tsirkin unlikely(!virtqueue_enable_cb_delayed(sq->vq))); 1700bdb12e0dSWillem de Bruijn 1701074c3582SJacob Keller /* timestamp packet in software */ 1702074c3582SJacob Keller skb_tx_timestamp(skb); 1703074c3582SJacob Keller 170403f191baSMichael S. Tsirkin /* Try to transmit */ 1705b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 170699ffc696SRusty Russell 17079ed4cb07SRusty Russell /* This should not happen! */ 1708681daee2SJason Wang if (unlikely(err)) { 170958eba97dSRusty Russell dev->stats.tx_fifo_errors++; 17102e57b79cSRick Jones if (net_ratelimit()) 171158eba97dSRusty Russell dev_warn(&dev->dev, 17127934b481SYuval Shaia "Unexpected TXQ (%d) queue failure: %d\n", 17137934b481SYuval Shaia qnum, err); 171458eba97dSRusty Russell dev->stats.tx_dropped++; 171585e94525SEric W. Biederman dev_kfree_skb_any(skb); 171658eba97dSRusty Russell return NETDEV_TX_OK; 1717296f96fcSRusty Russell } 171803f191baSMichael S. Tsirkin 171948925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 1720b92f1e67SWillem de Bruijn if (!use_napi) { 172148925e37SRusty Russell skb_orphan(skb); 1722895b5c9fSFlorian Westphal nf_reset_ct(skb); 1723b92f1e67SWillem de Bruijn } 172448925e37SRusty Russell 172560302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 172660302ff6SMichael S. Tsirkin * are then unable to transmit. 172760302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 172860302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 172960302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 173060302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 173160302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 173260302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 173360302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 1734d631b94eSstephen hemminger */ 1735b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1736986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 1737b92f1e67SWillem de Bruijn if (!use_napi && 1738b92f1e67SWillem de Bruijn unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 173948925e37SRusty Russell /* More just got used, free them then recheck. */ 1740df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, false); 1741b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1742986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 1743e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 174448925e37SRusty Russell } 174548925e37SRusty Russell } 174648925e37SRusty Russell } 174748925e37SRusty Russell 1748461f03dcSToshiaki Makita if (kick || netif_xmit_stopped(txq)) { 1749461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1750461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1751461f03dcSToshiaki Makita sq->stats.kicks++; 1752461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1753461f03dcSToshiaki Makita } 1754461f03dcSToshiaki Makita } 17550b725a2cSDavid S. Miller 17560b725a2cSDavid S. Miller return NETDEV_TX_OK; 1757c223a078SDavid S. Miller } 1758c223a078SDavid S. Miller 175940cbfc37SAmos Kong /* 176040cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 176140cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 1762788a8b6dSstephen hemminger * never fail unless improperly formatted. 176340cbfc37SAmos Kong */ 176440cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 1765d24bae32Sstephen hemminger struct scatterlist *out) 176640cbfc37SAmos Kong { 1767f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 1768d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 1769222722bcSYunjian Wang int ret; 177040cbfc37SAmos Kong 177140cbfc37SAmos Kong /* Caller should know better */ 1772f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 177340cbfc37SAmos Kong 177412e57169SMichael S. Tsirkin vi->ctrl->status = ~0; 177512e57169SMichael S. Tsirkin vi->ctrl->hdr.class = class; 177612e57169SMichael S. Tsirkin vi->ctrl->hdr.cmd = cmd; 1777f7bc9594SRusty Russell /* Add header */ 177812e57169SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 1779f7bc9594SRusty Russell sgs[out_num++] = &hdr; 178040cbfc37SAmos Kong 1781f7bc9594SRusty Russell if (out) 1782f7bc9594SRusty Russell sgs[out_num++] = out; 178340cbfc37SAmos Kong 1784f7bc9594SRusty Russell /* Add return status. */ 178512e57169SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 1786d24bae32Sstephen hemminger sgs[out_num] = &stat; 178740cbfc37SAmos Kong 1788d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1789222722bcSYunjian Wang ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 1790222722bcSYunjian Wang if (ret < 0) { 1791222722bcSYunjian Wang dev_warn(&vi->vdev->dev, 1792222722bcSYunjian Wang "Failed to add sgs for command vq: %d\n.", ret); 1793222722bcSYunjian Wang return false; 1794222722bcSYunjian Wang } 179540cbfc37SAmos Kong 179667975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 179712e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 179840cbfc37SAmos Kong 179940cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 180040cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 180140cbfc37SAmos Kong */ 1802047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 1803047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 180440cbfc37SAmos Kong cpu_relax(); 180540cbfc37SAmos Kong 180612e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 180740cbfc37SAmos Kong } 180840cbfc37SAmos Kong 18099c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 18109c46f6d4SAlex Williamson { 18119c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 18129c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 1813f2f2c8b4SJiri Pirko int ret; 1814e37e2ff3SAndy Lutomirski struct sockaddr *addr; 18157e58d5aeSAmos Kong struct scatterlist sg; 18169c46f6d4SAlex Williamson 1817ba5e4426SSridhar Samudrala if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 1818ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 1819ba5e4426SSridhar Samudrala 1820801822d1SShyam Saini addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 1821e37e2ff3SAndy Lutomirski if (!addr) 1822e37e2ff3SAndy Lutomirski return -ENOMEM; 1823e37e2ff3SAndy Lutomirski 1824e37e2ff3SAndy Lutomirski ret = eth_prepare_mac_addr_change(dev, addr); 1825f2f2c8b4SJiri Pirko if (ret) 1826e37e2ff3SAndy Lutomirski goto out; 18279c46f6d4SAlex Williamson 18287e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 18297e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 18307e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1831d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 18327e58d5aeSAmos Kong dev_warn(&vdev->dev, 18337e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 1834e37e2ff3SAndy Lutomirski ret = -EINVAL; 1835e37e2ff3SAndy Lutomirski goto out; 18367e58d5aeSAmos Kong } 18377e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 18387e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1839855e0c52SRusty Russell unsigned int i; 1840855e0c52SRusty Russell 1841855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 1842855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 1843855e0c52SRusty Russell virtio_cwrite8(vdev, 1844855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1845855e0c52SRusty Russell i, addr->sa_data[i]); 18467e58d5aeSAmos Kong } 18477e58d5aeSAmos Kong 18487e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 1849e37e2ff3SAndy Lutomirski ret = 0; 18509c46f6d4SAlex Williamson 1851e37e2ff3SAndy Lutomirski out: 1852e37e2ff3SAndy Lutomirski kfree(addr); 1853e37e2ff3SAndy Lutomirski return ret; 18549c46f6d4SAlex Williamson } 18559c46f6d4SAlex Williamson 1856bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev, 18573fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 18583fa2a1dfSstephen hemminger { 18593fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 18603fa2a1dfSstephen hemminger unsigned int start; 1861d7dfc5cfSToshiaki Makita int i; 18623fa2a1dfSstephen hemminger 1863d7dfc5cfSToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 18642c4a2f7dSToshiaki Makita u64 tpackets, tbytes, rpackets, rbytes, rdrops; 1865d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 1866d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 18673fa2a1dfSstephen hemminger 18683fa2a1dfSstephen hemminger do { 1869d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 1870d7dfc5cfSToshiaki Makita tpackets = sq->stats.packets; 1871d7dfc5cfSToshiaki Makita tbytes = sq->stats.bytes; 1872d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 187383a27052SEric Dumazet 187483a27052SEric Dumazet do { 1875d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 1876d46eeeafSJason Wang rpackets = rq->stats.packets; 1877d46eeeafSJason Wang rbytes = rq->stats.bytes; 1878d46eeeafSJason Wang rdrops = rq->stats.drops; 1879d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 18803fa2a1dfSstephen hemminger 18813fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 18823fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 18833fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 18843fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 18852c4a2f7dSToshiaki Makita tot->rx_dropped += rdrops; 18863fa2a1dfSstephen hemminger } 18873fa2a1dfSstephen hemminger 18883fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1889021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 18903fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 18913fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 18923fa2a1dfSstephen hemminger } 18933fa2a1dfSstephen hemminger 1894586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1895586d17c5SJason Wang { 1896586d17c5SJason Wang rtnl_lock(); 1897586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1898d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1899586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1900586d17c5SJason Wang rtnl_unlock(); 1901586d17c5SJason Wang } 1902586d17c5SJason Wang 190347315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1904986a4f4dSJason Wang { 1905986a4f4dSJason Wang struct scatterlist sg; 1906986a4f4dSJason Wang struct net_device *dev = vi->dev; 1907986a4f4dSJason Wang 1908986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1909986a4f4dSJason Wang return 0; 1910986a4f4dSJason Wang 191112e57169SMichael S. Tsirkin vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 191212e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); 1913986a4f4dSJason Wang 1914986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1915d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1916986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1917986a4f4dSJason Wang queue_pairs); 1918986a4f4dSJason Wang return -EINVAL; 191955257d72SSasha Levin } else { 1920986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 192135ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 192235ed159bSJason Wang if (dev->flags & IFF_UP) 19239b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 192455257d72SSasha Levin } 1925986a4f4dSJason Wang 1926986a4f4dSJason Wang return 0; 1927986a4f4dSJason Wang } 1928986a4f4dSJason Wang 192947315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 193047315329SJohn Fastabend { 193147315329SJohn Fastabend int err; 193247315329SJohn Fastabend 193347315329SJohn Fastabend rtnl_lock(); 193447315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 193547315329SJohn Fastabend rtnl_unlock(); 193647315329SJohn Fastabend return err; 193747315329SJohn Fastabend } 193847315329SJohn Fastabend 1939296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1940296f96fcSRusty Russell { 1941296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1942986a4f4dSJason Wang int i; 1943296f96fcSRusty Russell 1944b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1945b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1946986a4f4dSJason Wang 1947b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 1948754b8a21SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 1949986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 195078a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 1951b92f1e67SWillem de Bruijn } 1952296f96fcSRusty Russell 1953296f96fcSRusty Russell return 0; 1954296f96fcSRusty Russell } 1955296f96fcSRusty Russell 19562af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 19572af7698eSAlex Williamson { 19582af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1959f565a7c2SAlex Williamson struct scatterlist sg[2]; 1960f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1961ccffad25SJiri Pirko struct netdev_hw_addr *ha; 196232e7bfc4SJiri Pirko int uc_count; 19634cd24eafSJiri Pirko int mc_count; 1964f565a7c2SAlex Williamson void *buf; 1965f565a7c2SAlex Williamson int i; 19662af7698eSAlex Williamson 1967788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 19682af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 19692af7698eSAlex Williamson return; 19702af7698eSAlex Williamson 197112e57169SMichael S. Tsirkin vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); 197212e57169SMichael S. Tsirkin vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 19732af7698eSAlex Williamson 197412e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); 19752af7698eSAlex Williamson 19762af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1977d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 19782af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 197912e57169SMichael S. Tsirkin vi->ctrl->promisc ? "en" : "dis"); 19802af7698eSAlex Williamson 198112e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); 19822af7698eSAlex Williamson 19832af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1984d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 19852af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 198612e57169SMichael S. Tsirkin vi->ctrl->allmulti ? "en" : "dis"); 1987f565a7c2SAlex Williamson 198832e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 19894cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1990f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 19914cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1992f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 19934cd24eafSJiri Pirko mac_data = buf; 1994e68ed8f0SJoe Perches if (!buf) 1995f565a7c2SAlex Williamson return; 1996f565a7c2SAlex Williamson 199723e258e1SAlex Williamson sg_init_table(sg, 2); 199823e258e1SAlex Williamson 1999f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 2000fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 2001ccffad25SJiri Pirko i = 0; 200232e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 2003ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2004f565a7c2SAlex Williamson 2005f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 200632e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 2007f565a7c2SAlex Williamson 2008f565a7c2SAlex Williamson /* multicast list and count fill the end */ 200932e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 2010f565a7c2SAlex Williamson 2011fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 2012567ec874SJiri Pirko i = 0; 201322bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 201422bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2015f565a7c2SAlex Williamson 2016f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 20174cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 2018f565a7c2SAlex Williamson 2019f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 2020d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 202199e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 2022f565a7c2SAlex Williamson 2023f565a7c2SAlex Williamson kfree(buf); 20242af7698eSAlex Williamson } 20252af7698eSAlex Williamson 202680d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 202780d5c368SPatrick McHardy __be16 proto, u16 vid) 20280bde9569SAlex Williamson { 20290bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 20300bde9569SAlex Williamson struct scatterlist sg; 20310bde9569SAlex Williamson 2032d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 203312e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 20340bde9569SAlex Williamson 20350bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 2036d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 20370bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 20388e586137SJiri Pirko return 0; 20390bde9569SAlex Williamson } 20400bde9569SAlex Williamson 204180d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 204280d5c368SPatrick McHardy __be16 proto, u16 vid) 20430bde9569SAlex Williamson { 20440bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 20450bde9569SAlex Williamson struct scatterlist sg; 20460bde9569SAlex Williamson 2047d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 204812e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 20490bde9569SAlex Williamson 20500bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 2051d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 20520bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 20538e586137SJiri Pirko return 0; 20540bde9569SAlex Williamson } 20550bde9569SAlex Williamson 2056310974faSPeter Xu static void virtnet_clean_affinity(struct virtnet_info *vi) 2057986a4f4dSJason Wang { 2058986a4f4dSJason Wang int i; 20598898c21cSWanlong Gao 20608898c21cSWanlong Gao if (vi->affinity_hint_set) { 20618898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 206219e226e8SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, NULL); 206319e226e8SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, NULL); 20648898c21cSWanlong Gao } 20658898c21cSWanlong Gao 20668898c21cSWanlong Gao vi->affinity_hint_set = false; 20678898c21cSWanlong Gao } 20688898c21cSWanlong Gao } 20698898c21cSWanlong Gao 20708898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 2071986a4f4dSJason Wang { 20722ca653d6SCaleb Raitto cpumask_var_t mask; 20732ca653d6SCaleb Raitto int stragglers; 20742ca653d6SCaleb Raitto int group_size; 20752ca653d6SCaleb Raitto int i, j, cpu; 20762ca653d6SCaleb Raitto int num_cpu; 20772ca653d6SCaleb Raitto int stride; 2078986a4f4dSJason Wang 20792ca653d6SCaleb Raitto if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { 2080310974faSPeter Xu virtnet_clean_affinity(vi); 2081986a4f4dSJason Wang return; 2082986a4f4dSJason Wang } 2083986a4f4dSJason Wang 20842ca653d6SCaleb Raitto num_cpu = num_online_cpus(); 20852ca653d6SCaleb Raitto stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); 20862ca653d6SCaleb Raitto stragglers = num_cpu >= vi->curr_queue_pairs ? 20872ca653d6SCaleb Raitto num_cpu % vi->curr_queue_pairs : 20882ca653d6SCaleb Raitto 0; 20892ca653d6SCaleb Raitto cpu = cpumask_next(-1, cpu_online_mask); 20904d99f660SAndrei Vagin 20912ca653d6SCaleb Raitto for (i = 0; i < vi->curr_queue_pairs; i++) { 20922ca653d6SCaleb Raitto group_size = stride + (i < stragglers ? 1 : 0); 20932ca653d6SCaleb Raitto 20942ca653d6SCaleb Raitto for (j = 0; j < group_size; j++) { 20952ca653d6SCaleb Raitto cpumask_set_cpu(cpu, mask); 20962ca653d6SCaleb Raitto cpu = cpumask_next_wrap(cpu, cpu_online_mask, 20972ca653d6SCaleb Raitto nr_cpu_ids, false); 20982ca653d6SCaleb Raitto } 20992ca653d6SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, mask); 21002ca653d6SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, mask); 2101044ab86dSAntoine Tenart __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); 21022ca653d6SCaleb Raitto cpumask_clear(mask); 2103986a4f4dSJason Wang } 2104986a4f4dSJason Wang 2105986a4f4dSJason Wang vi->affinity_hint_set = true; 21062ca653d6SCaleb Raitto free_cpumask_var(mask); 210747be2479SWanlong Gao } 2108986a4f4dSJason Wang 21098017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 21108de4b2f3SWanlong Gao { 21118017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 21128017c279SSebastian Andrzej Siewior node); 21138de4b2f3SWanlong Gao virtnet_set_affinity(vi); 21148017c279SSebastian Andrzej Siewior return 0; 21158de4b2f3SWanlong Gao } 21163ab098dfSJason Wang 21178017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 21188017c279SSebastian Andrzej Siewior { 21198017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 21208017c279SSebastian Andrzej Siewior node_dead); 21218017c279SSebastian Andrzej Siewior virtnet_set_affinity(vi); 21228017c279SSebastian Andrzej Siewior return 0; 21238017c279SSebastian Andrzej Siewior } 21248017c279SSebastian Andrzej Siewior 21258017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 21268017c279SSebastian Andrzej Siewior { 21278017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 21288017c279SSebastian Andrzej Siewior node); 21298017c279SSebastian Andrzej Siewior 2130310974faSPeter Xu virtnet_clean_affinity(vi); 21318017c279SSebastian Andrzej Siewior return 0; 21328017c279SSebastian Andrzej Siewior } 21338017c279SSebastian Andrzej Siewior 21348017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online; 21358017c279SSebastian Andrzej Siewior 21368017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi) 21378017c279SSebastian Andrzej Siewior { 21388017c279SSebastian Andrzej Siewior int ret; 21398017c279SSebastian Andrzej Siewior 21408017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 21418017c279SSebastian Andrzej Siewior if (ret) 21428017c279SSebastian Andrzej Siewior return ret; 21438017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 21448017c279SSebastian Andrzej Siewior &vi->node_dead); 21458017c279SSebastian Andrzej Siewior if (!ret) 21468017c279SSebastian Andrzej Siewior return ret; 21478017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 21488017c279SSebastian Andrzej Siewior return ret; 21498017c279SSebastian Andrzej Siewior } 21508017c279SSebastian Andrzej Siewior 21518017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 21528017c279SSebastian Andrzej Siewior { 21538017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 21548017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 21558017c279SSebastian Andrzej Siewior &vi->node_dead); 2156a9ea3fc6SHerbert Xu } 2157a9ea3fc6SHerbert Xu 21588f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 21598f9f4668SRick Jones struct ethtool_ringparam *ring) 21608f9f4668SRick Jones { 21618f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 21628f9f4668SRick Jones 2163986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 2164986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 21658f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 21668f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 21678f9f4668SRick Jones } 21688f9f4668SRick Jones 216966846048SRick Jones 217066846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 217166846048SRick Jones struct ethtool_drvinfo *info) 217266846048SRick Jones { 217366846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 217466846048SRick Jones struct virtio_device *vdev = vi->vdev; 217566846048SRick Jones 217666846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 217766846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 217866846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 217966846048SRick Jones 218066846048SRick Jones } 218166846048SRick Jones 2182d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 2183d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 2184d73bcd2cSJason Wang struct ethtool_channels *channels) 2185d73bcd2cSJason Wang { 2186d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2187d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 2188d73bcd2cSJason Wang int err; 2189d73bcd2cSJason Wang 2190d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 2191d73bcd2cSJason Wang * We don't allow setting 'other' channels. 2192d73bcd2cSJason Wang */ 2193d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 2194d73bcd2cSJason Wang return -EINVAL; 2195d73bcd2cSJason Wang 2196c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 2197d73bcd2cSJason Wang return -EINVAL; 2198d73bcd2cSJason Wang 2199f600b690SJohn Fastabend /* For now we don't support modifying channels while XDP is loaded 2200f600b690SJohn Fastabend * also when XDP is loaded all RX queues have XDP programs so we only 2201f600b690SJohn Fastabend * need to check a single RX queue. 2202f600b690SJohn Fastabend */ 2203f600b690SJohn Fastabend if (vi->rq[0].xdp_prog) 2204f600b690SJohn Fastabend return -EINVAL; 2205f600b690SJohn Fastabend 2206a0d1d0f4SSebastian Andrzej Siewior cpus_read_lock(); 220747315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 2208de33212fSJeff Dike if (err) { 2209a0d1d0f4SSebastian Andrzej Siewior cpus_read_unlock(); 2210de33212fSJeff Dike goto err; 2211d73bcd2cSJason Wang } 2212de33212fSJeff Dike virtnet_set_affinity(vi); 2213a0d1d0f4SSebastian Andrzej Siewior cpus_read_unlock(); 2214d73bcd2cSJason Wang 2215de33212fSJeff Dike netif_set_real_num_tx_queues(dev, queue_pairs); 2216de33212fSJeff Dike netif_set_real_num_rx_queues(dev, queue_pairs); 2217de33212fSJeff Dike err: 2218d73bcd2cSJason Wang return err; 2219d73bcd2cSJason Wang } 2220d73bcd2cSJason Wang 2221d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 2222d7dfc5cfSToshiaki Makita { 2223d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2224d7dfc5cfSToshiaki Makita unsigned int i, j; 2225d7a9a01bSAlexander Duyck u8 *p = data; 2226d7dfc5cfSToshiaki Makita 2227d7dfc5cfSToshiaki Makita switch (stringset) { 2228d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2229d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2230d7a9a01bSAlexander Duyck for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) 2231d7a9a01bSAlexander Duyck ethtool_sprintf(&p, "rx_queue_%u_%s", i, 2232d7a9a01bSAlexander Duyck virtnet_rq_stats_desc[j].desc); 2233d7dfc5cfSToshiaki Makita } 2234d7dfc5cfSToshiaki Makita 2235d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2236d7a9a01bSAlexander Duyck for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) 2237d7a9a01bSAlexander Duyck ethtool_sprintf(&p, "tx_queue_%u_%s", i, 2238d7a9a01bSAlexander Duyck virtnet_sq_stats_desc[j].desc); 2239d7dfc5cfSToshiaki Makita } 2240d7dfc5cfSToshiaki Makita break; 2241d7dfc5cfSToshiaki Makita } 2242d7dfc5cfSToshiaki Makita } 2243d7dfc5cfSToshiaki Makita 2244d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset) 2245d7dfc5cfSToshiaki Makita { 2246d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2247d7dfc5cfSToshiaki Makita 2248d7dfc5cfSToshiaki Makita switch (sset) { 2249d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2250d7dfc5cfSToshiaki Makita return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + 2251d7dfc5cfSToshiaki Makita VIRTNET_SQ_STATS_LEN); 2252d7dfc5cfSToshiaki Makita default: 2253d7dfc5cfSToshiaki Makita return -EOPNOTSUPP; 2254d7dfc5cfSToshiaki Makita } 2255d7dfc5cfSToshiaki Makita } 2256d7dfc5cfSToshiaki Makita 2257d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev, 2258d7dfc5cfSToshiaki Makita struct ethtool_stats *stats, u64 *data) 2259d7dfc5cfSToshiaki Makita { 2260d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2261d7dfc5cfSToshiaki Makita unsigned int idx = 0, start, i, j; 2262d7dfc5cfSToshiaki Makita const u8 *stats_base; 2263d7dfc5cfSToshiaki Makita size_t offset; 2264d7dfc5cfSToshiaki Makita 2265d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2266d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 2267d7dfc5cfSToshiaki Makita 2268d46eeeafSJason Wang stats_base = (u8 *)&rq->stats; 2269d7dfc5cfSToshiaki Makita do { 2270d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 2271d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2272d7dfc5cfSToshiaki Makita offset = virtnet_rq_stats_desc[j].offset; 2273d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2274d7dfc5cfSToshiaki Makita } 2275d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 2276d7dfc5cfSToshiaki Makita idx += VIRTNET_RQ_STATS_LEN; 2277d7dfc5cfSToshiaki Makita } 2278d7dfc5cfSToshiaki Makita 2279d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2280d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 2281d7dfc5cfSToshiaki Makita 2282d7dfc5cfSToshiaki Makita stats_base = (u8 *)&sq->stats; 2283d7dfc5cfSToshiaki Makita do { 2284d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 2285d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 2286d7dfc5cfSToshiaki Makita offset = virtnet_sq_stats_desc[j].offset; 2287d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2288d7dfc5cfSToshiaki Makita } 2289d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 2290d7dfc5cfSToshiaki Makita idx += VIRTNET_SQ_STATS_LEN; 2291d7dfc5cfSToshiaki Makita } 2292d7dfc5cfSToshiaki Makita } 2293d7dfc5cfSToshiaki Makita 2294d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 2295d73bcd2cSJason Wang struct ethtool_channels *channels) 2296d73bcd2cSJason Wang { 2297d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2298d73bcd2cSJason Wang 2299d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 2300d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 2301d73bcd2cSJason Wang channels->max_other = 0; 2302d73bcd2cSJason Wang channels->rx_count = 0; 2303d73bcd2cSJason Wang channels->tx_count = 0; 2304d73bcd2cSJason Wang channels->other_count = 0; 2305d73bcd2cSJason Wang } 2306d73bcd2cSJason Wang 2307ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev, 2308ebb6b4b1SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 230916032be5SNikolay Aleksandrov { 231016032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 231116032be5SNikolay Aleksandrov 23129aedc6e2SCris Forno return ethtool_virtdev_set_link_ksettings(dev, cmd, 23139aedc6e2SCris Forno &vi->speed, &vi->duplex); 231416032be5SNikolay Aleksandrov } 231516032be5SNikolay Aleksandrov 2316ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev, 2317ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings *cmd) 231816032be5SNikolay Aleksandrov { 231916032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 232016032be5SNikolay Aleksandrov 2321ebb6b4b1SPhilippe Reynes cmd->base.speed = vi->speed; 2322ebb6b4b1SPhilippe Reynes cmd->base.duplex = vi->duplex; 2323ebb6b4b1SPhilippe Reynes cmd->base.port = PORT_OTHER; 232416032be5SNikolay Aleksandrov 232516032be5SNikolay Aleksandrov return 0; 232616032be5SNikolay Aleksandrov } 232716032be5SNikolay Aleksandrov 23280c465be1SJason Wang static int virtnet_set_coalesce(struct net_device *dev, 2329f3ccfda1SYufeng Mo struct ethtool_coalesce *ec, 2330f3ccfda1SYufeng Mo struct kernel_ethtool_coalesce *kernel_coal, 2331f3ccfda1SYufeng Mo struct netlink_ext_ack *extack) 23320c465be1SJason Wang { 23330c465be1SJason Wang struct virtnet_info *vi = netdev_priv(dev); 23340c465be1SJason Wang int i, napi_weight; 23350c465be1SJason Wang 2336a51e5206SJakub Kicinski if (ec->tx_max_coalesced_frames > 1 || 2337a51e5206SJakub Kicinski ec->rx_max_coalesced_frames != 1) 23380c465be1SJason Wang return -EINVAL; 23390c465be1SJason Wang 23400c465be1SJason Wang napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 23410c465be1SJason Wang if (napi_weight ^ vi->sq[0].napi.weight) { 23420c465be1SJason Wang if (dev->flags & IFF_UP) 23430c465be1SJason Wang return -EBUSY; 23440c465be1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 23450c465be1SJason Wang vi->sq[i].napi.weight = napi_weight; 23460c465be1SJason Wang } 23470c465be1SJason Wang 23480c465be1SJason Wang return 0; 23490c465be1SJason Wang } 23500c465be1SJason Wang 23510c465be1SJason Wang static int virtnet_get_coalesce(struct net_device *dev, 2352f3ccfda1SYufeng Mo struct ethtool_coalesce *ec, 2353f3ccfda1SYufeng Mo struct kernel_ethtool_coalesce *kernel_coal, 2354f3ccfda1SYufeng Mo struct netlink_ext_ack *extack) 23550c465be1SJason Wang { 23560c465be1SJason Wang struct ethtool_coalesce ec_default = { 23570c465be1SJason Wang .cmd = ETHTOOL_GCOALESCE, 23580c465be1SJason Wang .rx_max_coalesced_frames = 1, 23590c465be1SJason Wang }; 23600c465be1SJason Wang struct virtnet_info *vi = netdev_priv(dev); 23610c465be1SJason Wang 23620c465be1SJason Wang memcpy(ec, &ec_default, sizeof(ec_default)); 23630c465be1SJason Wang 23640c465be1SJason Wang if (vi->sq[0].napi.weight) 23650c465be1SJason Wang ec->tx_max_coalesced_frames = 1; 23660c465be1SJason Wang 23670c465be1SJason Wang return 0; 23680c465be1SJason Wang } 23690c465be1SJason Wang 237016032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 237116032be5SNikolay Aleksandrov { 237216032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 237316032be5SNikolay Aleksandrov 237416032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 237516032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 237616032be5SNikolay Aleksandrov } 237716032be5SNikolay Aleksandrov 2378faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi) 2379faa9b39fSJason Baron { 2380faa9b39fSJason Baron u32 speed; 2381faa9b39fSJason Baron u8 duplex; 2382faa9b39fSJason Baron 2383faa9b39fSJason Baron if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 2384faa9b39fSJason Baron return; 2385faa9b39fSJason Baron 238664ffa39dSMichael S. Tsirkin virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); 238764ffa39dSMichael S. Tsirkin 2388faa9b39fSJason Baron if (ethtool_validate_speed(speed)) 2389faa9b39fSJason Baron vi->speed = speed; 239064ffa39dSMichael S. Tsirkin 239164ffa39dSMichael S. Tsirkin virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); 239264ffa39dSMichael S. Tsirkin 2393faa9b39fSJason Baron if (ethtool_validate_duplex(duplex)) 2394faa9b39fSJason Baron vi->duplex = duplex; 2395faa9b39fSJason Baron } 2396faa9b39fSJason Baron 23970fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 2398a51e5206SJakub Kicinski .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES, 239966846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 24009f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 24018f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 2402d7dfc5cfSToshiaki Makita .get_strings = virtnet_get_strings, 2403d7dfc5cfSToshiaki Makita .get_sset_count = virtnet_get_sset_count, 2404d7dfc5cfSToshiaki Makita .get_ethtool_stats = virtnet_get_ethtool_stats, 2405d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 2406d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 2407074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 2408ebb6b4b1SPhilippe Reynes .get_link_ksettings = virtnet_get_link_ksettings, 2409ebb6b4b1SPhilippe Reynes .set_link_ksettings = virtnet_set_link_ksettings, 24100c465be1SJason Wang .set_coalesce = virtnet_set_coalesce, 24110c465be1SJason Wang .get_coalesce = virtnet_get_coalesce, 2412a9ea3fc6SHerbert Xu }; 2413a9ea3fc6SHerbert Xu 24149fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev) 24159fe7bfceSJohn Fastabend { 24169fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 24179fe7bfceSJohn Fastabend int i; 24189fe7bfceSJohn Fastabend 24199fe7bfceSJohn Fastabend /* Make sure no work handler is accessing the device */ 24209fe7bfceSJohn Fastabend flush_work(&vi->config_work); 24219fe7bfceSJohn Fastabend 242205c998b7SAke Koomsin netif_tx_lock_bh(vi->dev); 24239fe7bfceSJohn Fastabend netif_device_detach(vi->dev); 242405c998b7SAke Koomsin netif_tx_unlock_bh(vi->dev); 24259fe7bfceSJohn Fastabend cancel_delayed_work_sync(&vi->refill); 24269fe7bfceSJohn Fastabend 24279fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 2428b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 24299fe7bfceSJohn Fastabend napi_disable(&vi->rq[i].napi); 243078a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 2431b92f1e67SWillem de Bruijn } 24329fe7bfceSJohn Fastabend } 24339fe7bfceSJohn Fastabend } 24349fe7bfceSJohn Fastabend 24359fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi); 24369fe7bfceSJohn Fastabend 24379fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev) 24389fe7bfceSJohn Fastabend { 24399fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 24409fe7bfceSJohn Fastabend int err, i; 24419fe7bfceSJohn Fastabend 24429fe7bfceSJohn Fastabend err = init_vqs(vi); 24439fe7bfceSJohn Fastabend if (err) 24449fe7bfceSJohn Fastabend return err; 24459fe7bfceSJohn Fastabend 24469fe7bfceSJohn Fastabend virtio_device_ready(vdev); 24479fe7bfceSJohn Fastabend 24489fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 24499fe7bfceSJohn Fastabend for (i = 0; i < vi->curr_queue_pairs; i++) 24509fe7bfceSJohn Fastabend if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 24519fe7bfceSJohn Fastabend schedule_delayed_work(&vi->refill, 0); 24529fe7bfceSJohn Fastabend 2453b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 2454e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2455b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2456b92f1e67SWillem de Bruijn &vi->sq[i].napi); 2457b92f1e67SWillem de Bruijn } 24589fe7bfceSJohn Fastabend } 24599fe7bfceSJohn Fastabend 246005c998b7SAke Koomsin netif_tx_lock_bh(vi->dev); 24619fe7bfceSJohn Fastabend netif_device_attach(vi->dev); 246205c998b7SAke Koomsin netif_tx_unlock_bh(vi->dev); 24639fe7bfceSJohn Fastabend return err; 24649fe7bfceSJohn Fastabend } 24659fe7bfceSJohn Fastabend 24663f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 24673f93522fSJason Wang { 24683f93522fSJason Wang struct scatterlist sg; 246912e57169SMichael S. Tsirkin vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); 24703f93522fSJason Wang 247112e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); 24723f93522fSJason Wang 24733f93522fSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 24743f93522fSJason Wang VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 24753f93522fSJason Wang dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); 24763f93522fSJason Wang return -EINVAL; 24773f93522fSJason Wang } 24783f93522fSJason Wang 24793f93522fSJason Wang return 0; 24803f93522fSJason Wang } 24813f93522fSJason Wang 24823f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 24833f93522fSJason Wang { 24843f93522fSJason Wang u64 offloads = 0; 24853f93522fSJason Wang 24863f93522fSJason Wang if (!vi->guest_offloads) 24873f93522fSJason Wang return 0; 24883f93522fSJason Wang 24893f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 24903f93522fSJason Wang } 24913f93522fSJason Wang 24923f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 24933f93522fSJason Wang { 24943f93522fSJason Wang u64 offloads = vi->guest_offloads; 24953f93522fSJason Wang 24963f93522fSJason Wang if (!vi->guest_offloads) 24973f93522fSJason Wang return 0; 24983f93522fSJason Wang 24993f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 25003f93522fSJason Wang } 25013f93522fSJason Wang 25029861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 25039861ce03SJakub Kicinski struct netlink_ext_ack *extack) 2504f600b690SJohn Fastabend { 2505f600b690SJohn Fastabend unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); 2506f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 2507f600b690SJohn Fastabend struct bpf_prog *old_prog; 2508017b29c3SJason Wang u16 xdp_qp = 0, curr_qp; 2509672aafd5SJohn Fastabend int i, err; 2510f600b690SJohn Fastabend 25113f93522fSJason Wang if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 25123f93522fSJason Wang && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 251392502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 251492502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 251518ba58e1SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 251618ba58e1SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) { 2517dbcf24d1SJason Wang NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first"); 2518f600b690SJohn Fastabend return -EOPNOTSUPP; 2519f600b690SJohn Fastabend } 2520f600b690SJohn Fastabend 2521f600b690SJohn Fastabend if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 25224d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 2523f600b690SJohn Fastabend return -EINVAL; 2524f600b690SJohn Fastabend } 2525f600b690SJohn Fastabend 2526f600b690SJohn Fastabend if (dev->mtu > max_sz) { 25274d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); 2528f600b690SJohn Fastabend netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); 2529f600b690SJohn Fastabend return -EINVAL; 2530f600b690SJohn Fastabend } 2531f600b690SJohn Fastabend 2532672aafd5SJohn Fastabend curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 2533672aafd5SJohn Fastabend if (prog) 2534672aafd5SJohn Fastabend xdp_qp = nr_cpu_ids; 2535672aafd5SJohn Fastabend 2536672aafd5SJohn Fastabend /* XDP requires extra queues for XDP_TX */ 2537672aafd5SJohn Fastabend if (curr_qp + xdp_qp > vi->max_queue_pairs) { 253897c2c69eSXuan Zhuo netdev_warn(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n", 2539672aafd5SJohn Fastabend curr_qp + xdp_qp, vi->max_queue_pairs); 254097c2c69eSXuan Zhuo xdp_qp = 0; 2541672aafd5SJohn Fastabend } 2542672aafd5SJohn Fastabend 254303aa6d34SToshiaki Makita old_prog = rtnl_dereference(vi->rq[0].xdp_prog); 254403aa6d34SToshiaki Makita if (!prog && !old_prog) 254503aa6d34SToshiaki Makita return 0; 254603aa6d34SToshiaki Makita 254785192dbfSAndrii Nakryiko if (prog) 254885192dbfSAndrii Nakryiko bpf_prog_add(prog, vi->max_queue_pairs - 1); 25492de2f7f4SJohn Fastabend 25504941d472SJason Wang /* Make sure NAPI is not using any XDP TX queues for RX. */ 2551534da5e8SToshiaki Makita if (netif_running(dev)) { 2552534da5e8SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 25534941d472SJason Wang napi_disable(&vi->rq[i].napi); 2554534da5e8SToshiaki Makita virtnet_napi_tx_disable(&vi->sq[i].napi); 2555534da5e8SToshiaki Makita } 2556534da5e8SToshiaki Makita } 25572de2f7f4SJohn Fastabend 255803aa6d34SToshiaki Makita if (!prog) { 255903aa6d34SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 256003aa6d34SToshiaki Makita rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 256103aa6d34SToshiaki Makita if (i == 0) 256203aa6d34SToshiaki Makita virtnet_restore_guest_offloads(vi); 256303aa6d34SToshiaki Makita } 256403aa6d34SToshiaki Makita synchronize_net(); 256503aa6d34SToshiaki Makita } 256603aa6d34SToshiaki Makita 25674941d472SJason Wang err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 25684941d472SJason Wang if (err) 25694941d472SJason Wang goto err; 2570188313c1SToshiaki Makita netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 25714941d472SJason Wang vi->xdp_queue_pairs = xdp_qp; 2572f600b690SJohn Fastabend 257303aa6d34SToshiaki Makita if (prog) { 257497c2c69eSXuan Zhuo vi->xdp_enabled = true; 2575f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 2576f600b690SJohn Fastabend rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 257703aa6d34SToshiaki Makita if (i == 0 && !old_prog) 25783f93522fSJason Wang virtnet_clear_guest_offloads(vi); 25793f93522fSJason Wang } 258097c2c69eSXuan Zhuo } else { 258197c2c69eSXuan Zhuo vi->xdp_enabled = false; 258203aa6d34SToshiaki Makita } 258303aa6d34SToshiaki Makita 258403aa6d34SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 2585f600b690SJohn Fastabend if (old_prog) 2586f600b690SJohn Fastabend bpf_prog_put(old_prog); 2587534da5e8SToshiaki Makita if (netif_running(dev)) { 25884941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2589534da5e8SToshiaki Makita virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2590534da5e8SToshiaki Makita &vi->sq[i].napi); 2591534da5e8SToshiaki Makita } 2592f600b690SJohn Fastabend } 2593f600b690SJohn Fastabend 2594f600b690SJohn Fastabend return 0; 25952de2f7f4SJohn Fastabend 25964941d472SJason Wang err: 259703aa6d34SToshiaki Makita if (!prog) { 259803aa6d34SToshiaki Makita virtnet_clear_guest_offloads(vi); 25994941d472SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 260003aa6d34SToshiaki Makita rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); 260103aa6d34SToshiaki Makita } 260203aa6d34SToshiaki Makita 26038be4d9a4SToshiaki Makita if (netif_running(dev)) { 2604534da5e8SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 26054941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2606534da5e8SToshiaki Makita virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2607534da5e8SToshiaki Makita &vi->sq[i].napi); 2608534da5e8SToshiaki Makita } 26098be4d9a4SToshiaki Makita } 26102de2f7f4SJohn Fastabend if (prog) 26112de2f7f4SJohn Fastabend bpf_prog_sub(prog, vi->max_queue_pairs - 1); 26122de2f7f4SJohn Fastabend return err; 2613f600b690SJohn Fastabend } 2614f600b690SJohn Fastabend 2615f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 2616f600b690SJohn Fastabend { 2617f600b690SJohn Fastabend switch (xdp->command) { 2618f600b690SJohn Fastabend case XDP_SETUP_PROG: 26199861ce03SJakub Kicinski return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 2620f600b690SJohn Fastabend default: 2621f600b690SJohn Fastabend return -EINVAL; 2622f600b690SJohn Fastabend } 2623f600b690SJohn Fastabend } 2624f600b690SJohn Fastabend 2625ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 2626ba5e4426SSridhar Samudrala size_t len) 2627ba5e4426SSridhar Samudrala { 2628ba5e4426SSridhar Samudrala struct virtnet_info *vi = netdev_priv(dev); 2629ba5e4426SSridhar Samudrala int ret; 2630ba5e4426SSridhar Samudrala 2631ba5e4426SSridhar Samudrala if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 2632ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2633ba5e4426SSridhar Samudrala 2634ba5e4426SSridhar Samudrala ret = snprintf(buf, len, "sby"); 2635ba5e4426SSridhar Samudrala if (ret >= len) 2636ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2637ba5e4426SSridhar Samudrala 2638ba5e4426SSridhar Samudrala return 0; 2639ba5e4426SSridhar Samudrala } 2640ba5e4426SSridhar Samudrala 2641a02e8964SWillem de Bruijn static int virtnet_set_features(struct net_device *dev, 2642a02e8964SWillem de Bruijn netdev_features_t features) 2643a02e8964SWillem de Bruijn { 2644a02e8964SWillem de Bruijn struct virtnet_info *vi = netdev_priv(dev); 2645cf8691cbSMichael S. Tsirkin u64 offloads; 2646a02e8964SWillem de Bruijn int err; 2647a02e8964SWillem de Bruijn 2648dbcf24d1SJason Wang if ((dev->features ^ features) & NETIF_F_GRO_HW) { 264997c2c69eSXuan Zhuo if (vi->xdp_enabled) 2650a02e8964SWillem de Bruijn return -EBUSY; 2651a02e8964SWillem de Bruijn 2652dbcf24d1SJason Wang if (features & NETIF_F_GRO_HW) 2653cf8691cbSMichael S. Tsirkin offloads = vi->guest_offloads_capable; 2654a02e8964SWillem de Bruijn else 2655cf8691cbSMichael S. Tsirkin offloads = vi->guest_offloads_capable & 2656dbcf24d1SJason Wang ~GUEST_OFFLOAD_GRO_HW_MASK; 2657a02e8964SWillem de Bruijn 2658a02e8964SWillem de Bruijn err = virtnet_set_guest_offloads(vi, offloads); 2659a02e8964SWillem de Bruijn if (err) 2660a02e8964SWillem de Bruijn return err; 26613618ad2aSTonghao Zhang vi->guest_offloads = offloads; 2662cf8691cbSMichael S. Tsirkin } 2663cf8691cbSMichael S. Tsirkin 2664a02e8964SWillem de Bruijn return 0; 2665a02e8964SWillem de Bruijn } 2666a02e8964SWillem de Bruijn 266776288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 266876288b4eSStephen Hemminger .ndo_open = virtnet_open, 266976288b4eSStephen Hemminger .ndo_stop = virtnet_close, 267076288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 267176288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 26729c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 26732af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 26743fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 26751824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 26761824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 2677f4e63525SJakub Kicinski .ndo_bpf = virtnet_xdp, 2678186b3c99SJason Wang .ndo_xdp_xmit = virtnet_xdp_xmit, 26792836b4f2SVlad Yasevich .ndo_features_check = passthru_features_check, 2680ba5e4426SSridhar Samudrala .ndo_get_phys_port_name = virtnet_get_phys_port_name, 2681a02e8964SWillem de Bruijn .ndo_set_features = virtnet_set_features, 268276288b4eSStephen Hemminger }; 268376288b4eSStephen Hemminger 2684586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 26859f4d26d0SMark McLoughlin { 2686586d17c5SJason Wang struct virtnet_info *vi = 2687586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 26889f4d26d0SMark McLoughlin u16 v; 26899f4d26d0SMark McLoughlin 2690855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 2691855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 2692507613bfSMichael S. Tsirkin return; 2693586d17c5SJason Wang 2694586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 2695ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 2696586d17c5SJason Wang virtnet_ack_link_announce(vi); 2697586d17c5SJason Wang } 26989f4d26d0SMark McLoughlin 26999f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 27009f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 27019f4d26d0SMark McLoughlin 27029f4d26d0SMark McLoughlin if (vi->status == v) 2703507613bfSMichael S. Tsirkin return; 27049f4d26d0SMark McLoughlin 27059f4d26d0SMark McLoughlin vi->status = v; 27069f4d26d0SMark McLoughlin 27079f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 2708faa9b39fSJason Baron virtnet_update_settings(vi); 27099f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 2710986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 27119f4d26d0SMark McLoughlin } else { 27129f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 2713986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 27149f4d26d0SMark McLoughlin } 27159f4d26d0SMark McLoughlin } 27169f4d26d0SMark McLoughlin 27179f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 27189f4d26d0SMark McLoughlin { 27199f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 27209f4d26d0SMark McLoughlin 27213b07e9caSTejun Heo schedule_work(&vi->config_work); 27229f4d26d0SMark McLoughlin } 27239f4d26d0SMark McLoughlin 2724986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 2725986a4f4dSJason Wang { 2726d4fb84eeSAndrey Vagin int i; 2727d4fb84eeSAndrey Vagin 2728ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 27295198d545SJakub Kicinski __netif_napi_del(&vi->rq[i].napi); 27305198d545SJakub Kicinski __netif_napi_del(&vi->sq[i].napi); 2731ab3971b1SJason Wang } 2732d4fb84eeSAndrey Vagin 27335198d545SJakub Kicinski /* We called __netif_napi_del(), 2734963abe5cSEric Dumazet * we need to respect an RCU grace period before freeing vi->rq 2735963abe5cSEric Dumazet */ 2736963abe5cSEric Dumazet synchronize_net(); 2737963abe5cSEric Dumazet 2738986a4f4dSJason Wang kfree(vi->rq); 2739986a4f4dSJason Wang kfree(vi->sq); 274012e57169SMichael S. Tsirkin kfree(vi->ctrl); 2741986a4f4dSJason Wang } 2742986a4f4dSJason Wang 274347315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi) 2744986a4f4dSJason Wang { 2745f600b690SJohn Fastabend struct bpf_prog *old_prog; 2746986a4f4dSJason Wang int i; 2747986a4f4dSJason Wang 2748986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2749986a4f4dSJason Wang while (vi->rq[i].pages) 2750986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 2751f600b690SJohn Fastabend 2752f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2753f600b690SJohn Fastabend RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 2754f600b690SJohn Fastabend if (old_prog) 2755f600b690SJohn Fastabend bpf_prog_put(old_prog); 2756986a4f4dSJason Wang } 275747315329SJohn Fastabend } 275847315329SJohn Fastabend 275947315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi) 276047315329SJohn Fastabend { 276147315329SJohn Fastabend rtnl_lock(); 276247315329SJohn Fastabend _free_receive_bufs(vi); 2763f600b690SJohn Fastabend rtnl_unlock(); 2764986a4f4dSJason Wang } 2765986a4f4dSJason Wang 2766fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 2767fb51879dSMichael Dalton { 2768fb51879dSMichael Dalton int i; 2769fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 2770fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 2771fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 2772fb51879dSMichael Dalton } 2773fb51879dSMichael Dalton 2774986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 2775986a4f4dSJason Wang { 2776986a4f4dSJason Wang void *buf; 2777986a4f4dSJason Wang int i; 2778986a4f4dSJason Wang 2779986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2780986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 278156434a01SJohn Fastabend while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 27825050471dSToshiaki Makita if (!is_xdp_frame(buf)) 2783986a4f4dSJason Wang dev_kfree_skb(buf); 278456434a01SJohn Fastabend else 27855050471dSToshiaki Makita xdp_return_frame(ptr_to_xdp(buf)); 278656434a01SJohn Fastabend } 2787986a4f4dSJason Wang } 2788986a4f4dSJason Wang 2789986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2790986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 2791986a4f4dSJason Wang 2792986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2793ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 2794680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 2795ab7db917SMichael Dalton } else if (vi->big_packets) { 2796fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 2797ab7db917SMichael Dalton } else { 2798f6b10209SJason Wang put_page(virt_to_head_page(buf)); 2799986a4f4dSJason Wang } 2800986a4f4dSJason Wang } 2801986a4f4dSJason Wang } 2802ab7db917SMichael Dalton } 2803986a4f4dSJason Wang 2804e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 2805e9d7417bSJason Wang { 2806e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 2807e9d7417bSJason Wang 2808310974faSPeter Xu virtnet_clean_affinity(vi); 2809986a4f4dSJason Wang 2810e9d7417bSJason Wang vdev->config->del_vqs(vdev); 2811986a4f4dSJason Wang 2812986a4f4dSJason Wang virtnet_free_queues(vi); 2813986a4f4dSJason Wang } 2814986a4f4dSJason Wang 2815d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at 2816d85b758fSMichael S. Tsirkin * least one full packet? 2817d85b758fSMichael S. Tsirkin * Logic below assumes the mergeable buffer header is used. 2818d85b758fSMichael S. Tsirkin */ 2819d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 2820d85b758fSMichael S. Tsirkin { 2821d85b758fSMichael S. Tsirkin const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2822d85b758fSMichael S. Tsirkin unsigned int rq_size = virtqueue_get_vring_size(vq); 2823d85b758fSMichael S. Tsirkin unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 2824d85b758fSMichael S. Tsirkin unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 2825d85b758fSMichael S. Tsirkin unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 2826d85b758fSMichael S. Tsirkin 2827f0c3192cSMichael S. Tsirkin return max(max(min_buf_len, hdr_len) - hdr_len, 2828f0c3192cSMichael S. Tsirkin (unsigned int)GOOD_PACKET_LEN); 2829d85b758fSMichael S. Tsirkin } 2830d85b758fSMichael S. Tsirkin 2831986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 2832986a4f4dSJason Wang { 2833986a4f4dSJason Wang vq_callback_t **callbacks; 2834986a4f4dSJason Wang struct virtqueue **vqs; 2835986a4f4dSJason Wang int ret = -ENOMEM; 2836986a4f4dSJason Wang int i, total_vqs; 2837986a4f4dSJason Wang const char **names; 2838d45b897bSMichael S. Tsirkin bool *ctx; 2839986a4f4dSJason Wang 2840986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 2841986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 2842986a4f4dSJason Wang * possible control vq. 2843986a4f4dSJason Wang */ 2844986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 2845986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 2846986a4f4dSJason Wang 2847986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 28486396bb22SKees Cook vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 2849986a4f4dSJason Wang if (!vqs) 2850986a4f4dSJason Wang goto err_vq; 28516da2ec56SKees Cook callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL); 2852986a4f4dSJason Wang if (!callbacks) 2853986a4f4dSJason Wang goto err_callback; 28546da2ec56SKees Cook names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL); 2855986a4f4dSJason Wang if (!names) 2856986a4f4dSJason Wang goto err_names; 2857192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 28586396bb22SKees Cook ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 2859d45b897bSMichael S. Tsirkin if (!ctx) 2860d45b897bSMichael S. Tsirkin goto err_ctx; 2861d45b897bSMichael S. Tsirkin } else { 2862d45b897bSMichael S. Tsirkin ctx = NULL; 2863d45b897bSMichael S. Tsirkin } 2864986a4f4dSJason Wang 2865986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 2866986a4f4dSJason Wang if (vi->has_cvq) { 2867986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 2868986a4f4dSJason Wang names[total_vqs - 1] = "control"; 2869986a4f4dSJason Wang } 2870986a4f4dSJason Wang 2871986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 2872986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2873986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 2874986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 2875986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 2876986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 2877986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 2878986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 2879d45b897bSMichael S. Tsirkin if (ctx) 2880d45b897bSMichael S. Tsirkin ctx[rxq2vq(i)] = true; 2881986a4f4dSJason Wang } 2882986a4f4dSJason Wang 2883a2f7dc00SXianting Tian ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks, 2884d45b897bSMichael S. Tsirkin names, ctx, NULL); 2885986a4f4dSJason Wang if (ret) 2886986a4f4dSJason Wang goto err_find; 2887986a4f4dSJason Wang 2888986a4f4dSJason Wang if (vi->has_cvq) { 2889986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 2890986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 2891f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2892986a4f4dSJason Wang } 2893986a4f4dSJason Wang 2894986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2895986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 2896d85b758fSMichael S. Tsirkin vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 2897986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 2898986a4f4dSJason Wang } 2899986a4f4dSJason Wang 29002fa3c8a8STonghao Zhang /* run here: ret == 0. */ 2901986a4f4dSJason Wang 2902986a4f4dSJason Wang 2903986a4f4dSJason Wang err_find: 2904d45b897bSMichael S. Tsirkin kfree(ctx); 2905d45b897bSMichael S. Tsirkin err_ctx: 2906986a4f4dSJason Wang kfree(names); 2907986a4f4dSJason Wang err_names: 2908986a4f4dSJason Wang kfree(callbacks); 2909986a4f4dSJason Wang err_callback: 2910986a4f4dSJason Wang kfree(vqs); 2911986a4f4dSJason Wang err_vq: 2912986a4f4dSJason Wang return ret; 2913986a4f4dSJason Wang } 2914986a4f4dSJason Wang 2915986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 2916986a4f4dSJason Wang { 2917986a4f4dSJason Wang int i; 2918986a4f4dSJason Wang 2919122b84a1SMax Gurtovoy if (vi->has_cvq) { 292012e57169SMichael S. Tsirkin vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 292112e57169SMichael S. Tsirkin if (!vi->ctrl) 292212e57169SMichael S. Tsirkin goto err_ctrl; 2923122b84a1SMax Gurtovoy } else { 2924122b84a1SMax Gurtovoy vi->ctrl = NULL; 2925122b84a1SMax Gurtovoy } 29266396bb22SKees Cook vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 2927986a4f4dSJason Wang if (!vi->sq) 2928986a4f4dSJason Wang goto err_sq; 29296396bb22SKees Cook vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 2930008d4278SAmerigo Wang if (!vi->rq) 2931986a4f4dSJason Wang goto err_rq; 2932986a4f4dSJason Wang 2933986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 2934986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2935986a4f4dSJason Wang vi->rq[i].pages = NULL; 2936986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2937986a4f4dSJason Wang napi_weight); 29381d11e732SWillem de Bruijn netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2939b92f1e67SWillem de Bruijn napi_tx ? napi_weight : 0); 2940986a4f4dSJason Wang 2941986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 29425377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 2943986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 2944d7dfc5cfSToshiaki Makita 2945d7dfc5cfSToshiaki Makita u64_stats_init(&vi->rq[i].stats.syncp); 2946d7dfc5cfSToshiaki Makita u64_stats_init(&vi->sq[i].stats.syncp); 2947986a4f4dSJason Wang } 2948986a4f4dSJason Wang 2949986a4f4dSJason Wang return 0; 2950986a4f4dSJason Wang 2951986a4f4dSJason Wang err_rq: 2952986a4f4dSJason Wang kfree(vi->sq); 2953986a4f4dSJason Wang err_sq: 295412e57169SMichael S. Tsirkin kfree(vi->ctrl); 295512e57169SMichael S. Tsirkin err_ctrl: 2956986a4f4dSJason Wang return -ENOMEM; 2957e9d7417bSJason Wang } 2958e9d7417bSJason Wang 29593f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 29603f9c10b0SAmit Shah { 2961986a4f4dSJason Wang int ret; 29623f9c10b0SAmit Shah 2963986a4f4dSJason Wang /* Allocate send & receive queues */ 2964986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 2965986a4f4dSJason Wang if (ret) 2966986a4f4dSJason Wang goto err; 29673f9c10b0SAmit Shah 2968986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 2969986a4f4dSJason Wang if (ret) 2970986a4f4dSJason Wang goto err_free; 29713f9c10b0SAmit Shah 2972a0d1d0f4SSebastian Andrzej Siewior cpus_read_lock(); 29738898c21cSWanlong Gao virtnet_set_affinity(vi); 2974a0d1d0f4SSebastian Andrzej Siewior cpus_read_unlock(); 297547be2479SWanlong Gao 29763f9c10b0SAmit Shah return 0; 2977986a4f4dSJason Wang 2978986a4f4dSJason Wang err_free: 2979986a4f4dSJason Wang virtnet_free_queues(vi); 2980986a4f4dSJason Wang err: 2981986a4f4dSJason Wang return ret; 29823f9c10b0SAmit Shah } 29833f9c10b0SAmit Shah 2984fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2985fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 2986718ad681Sstephen hemminger char *buf) 2987fbf28d78SMichael Dalton { 2988fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 2989fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 29903cc81a9aSJason Wang unsigned int headroom = virtnet_get_headroom(vi); 29913cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 29925377d758SJohannes Berg struct ewma_pkt_len *avg; 2993fbf28d78SMichael Dalton 2994fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 2995fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 2996d85b758fSMichael S. Tsirkin return sprintf(buf, "%u\n", 29973cc81a9aSJason Wang get_mergeable_buf_len(&vi->rq[queue_index], avg, 29983cc81a9aSJason Wang SKB_DATA_ALIGN(headroom + tailroom))); 2999fbf28d78SMichael Dalton } 3000fbf28d78SMichael Dalton 3001fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 3002fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 3003fbf28d78SMichael Dalton 3004fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 3005fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 3006fbf28d78SMichael Dalton NULL 3007fbf28d78SMichael Dalton }; 3008fbf28d78SMichael Dalton 3009fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 3010fbf28d78SMichael Dalton .name = "virtio_net", 3011fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 3012fbf28d78SMichael Dalton }; 3013fbf28d78SMichael Dalton #endif 3014fbf28d78SMichael Dalton 3015892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 3016892d6eb1SJason Wang unsigned int fbit, 3017892d6eb1SJason Wang const char *fname, const char *dname) 3018892d6eb1SJason Wang { 3019892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 3020892d6eb1SJason Wang return false; 3021892d6eb1SJason Wang 3022892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 3023892d6eb1SJason Wang fname, dname); 3024892d6eb1SJason Wang 3025892d6eb1SJason Wang return true; 3026892d6eb1SJason Wang } 3027892d6eb1SJason Wang 3028892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 3029892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 3030892d6eb1SJason Wang 3031892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 3032892d6eb1SJason Wang { 3033892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 3034892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 3035892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3036892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 3037892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3038892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 3039892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3040892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 3041892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 3042892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 3043892d6eb1SJason Wang return false; 3044892d6eb1SJason Wang } 3045892d6eb1SJason Wang 3046892d6eb1SJason Wang return true; 3047892d6eb1SJason Wang } 3048892d6eb1SJason Wang 3049d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU 3050d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU 3051d0c2c997SJarod Wilson 3052fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev) 3053296f96fcSRusty Russell { 30546ba42248SMichael S. Tsirkin if (!vdev->config->get) { 30556ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 30566ba42248SMichael S. Tsirkin __func__); 30576ba42248SMichael S. Tsirkin return -EINVAL; 30586ba42248SMichael S. Tsirkin } 30596ba42248SMichael S. Tsirkin 3060892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 3061892d6eb1SJason Wang return -EINVAL; 3062892d6eb1SJason Wang 3063fe36cbe0SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 3064fe36cbe0SMichael S. Tsirkin int mtu = virtio_cread16(vdev, 3065fe36cbe0SMichael S. Tsirkin offsetof(struct virtio_net_config, 3066fe36cbe0SMichael S. Tsirkin mtu)); 3067fe36cbe0SMichael S. Tsirkin if (mtu < MIN_MTU) 3068fe36cbe0SMichael S. Tsirkin __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 3069fe36cbe0SMichael S. Tsirkin } 3070fe36cbe0SMichael S. Tsirkin 3071fe36cbe0SMichael S. Tsirkin return 0; 3072fe36cbe0SMichael S. Tsirkin } 3073fe36cbe0SMichael S. Tsirkin 3074fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev) 3075fe36cbe0SMichael S. Tsirkin { 3076d7dfc5cfSToshiaki Makita int i, err = -ENOMEM; 3077fe36cbe0SMichael S. Tsirkin struct net_device *dev; 3078fe36cbe0SMichael S. Tsirkin struct virtnet_info *vi; 3079fe36cbe0SMichael S. Tsirkin u16 max_queue_pairs; 3080fe36cbe0SMichael S. Tsirkin int mtu; 3081fe36cbe0SMichael S. Tsirkin 3082986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 3083855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 3084855e0c52SRusty Russell struct virtio_net_config, 3085855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 3086986a4f4dSJason Wang 3087986a4f4dSJason Wang /* We need at least 2 queue's */ 3088986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 3089986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 3090986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 3091986a4f4dSJason Wang max_queue_pairs = 1; 3092296f96fcSRusty Russell 3093296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 3094986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 3095296f96fcSRusty Russell if (!dev) 3096296f96fcSRusty Russell return -ENOMEM; 3097296f96fcSRusty Russell 3098296f96fcSRusty Russell /* Set up network device as normal. */ 3099ab5bd583SXuan Zhuo dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE | 3100ab5bd583SXuan Zhuo IFF_TX_SKB_NO_LINEAR; 310176288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 3102296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 31033fa2a1dfSstephen hemminger 31047ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 3105296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 3106296f96fcSRusty Russell 3107296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 310898e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 3109296f96fcSRusty Russell /* This opens up the world of extra features. */ 311048900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 311198e778c9SMichał Mirosław if (csum) 311248900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 311398e778c9SMichał Mirosław 311498e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 3115e078de03SDavid S. Miller dev->hw_features |= NETIF_F_TSO 311634a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 311734a48579SRusty Russell } 31185539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 311998e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 312098e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 312198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 312298e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 312398e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 312498e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 312598e778c9SMichał Mirosław 312641f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 312741f2f127SJason Wang 312898e778c9SMichał Mirosław if (gso) 3129e078de03SDavid S. Miller dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 313098e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 3131296f96fcSRusty Russell } 31324f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 31334f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 3134a02e8964SWillem de Bruijn if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 3135a02e8964SWillem de Bruijn virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)) 3136dbcf24d1SJason Wang dev->features |= NETIF_F_GRO_HW; 3137cf8691cbSMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) 3138dbcf24d1SJason Wang dev->hw_features |= NETIF_F_GRO_HW; 3139296f96fcSRusty Russell 31404fda8302SJason Wang dev->vlan_features = dev->features; 31414fda8302SJason Wang 3142d0c2c997SJarod Wilson /* MTU range: 68 - 65535 */ 3143d0c2c997SJarod Wilson dev->min_mtu = MIN_MTU; 3144d0c2c997SJarod Wilson dev->max_mtu = MAX_MTU; 3145d0c2c997SJarod Wilson 3146296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 3147855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 3148855e0c52SRusty Russell virtio_cread_bytes(vdev, 3149a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 3150855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 3151855e0c52SRusty Russell else 3152f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 3153296f96fcSRusty Russell 3154296f96fcSRusty Russell /* Set up our device-specific information */ 3155296f96fcSRusty Russell vi = netdev_priv(dev); 3156296f96fcSRusty Russell vi->dev = dev; 3157296f96fcSRusty Russell vi->vdev = vdev; 3158d9d5dcc8SChristian Borntraeger vdev->priv = vi; 3159827da44cSJohn Stultz 3160586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 3161296f96fcSRusty Russell 316297402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 31638e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 31648e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 3165e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 3166e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 316797402b96SHerbert Xu vi->big_packets = true; 316897402b96SHerbert Xu 31693f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 31703f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 31713f2c31d9SMark McLoughlin 3172d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 3173d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 3174012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 3175012873d0SMichael S. Tsirkin else 3176012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 3177012873d0SMichael S. Tsirkin 317875993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 317975993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 3180e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 3181e7428e95SMichael S. Tsirkin 3182986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 3183986a4f4dSJason Wang vi->has_cvq = true; 3184986a4f4dSJason Wang 318514de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 318614de9d11SAaron Conole mtu = virtio_cread16(vdev, 318714de9d11SAaron Conole offsetof(struct virtio_net_config, 318814de9d11SAaron Conole mtu)); 318993a205eeSAaron Conole if (mtu < dev->min_mtu) { 3190fe36cbe0SMichael S. Tsirkin /* Should never trigger: MTU was previously validated 3191fe36cbe0SMichael S. Tsirkin * in virtnet_validate. 3192fe36cbe0SMichael S. Tsirkin */ 31937934b481SYuval Shaia dev_err(&vdev->dev, 31947934b481SYuval Shaia "device MTU appears to have changed it is now %d < %d", 31957934b481SYuval Shaia mtu, dev->min_mtu); 3196411ea23aSDan Carpenter err = -EINVAL; 3197d7dfc5cfSToshiaki Makita goto free; 3198fe36cbe0SMichael S. Tsirkin } 3199fe36cbe0SMichael S. Tsirkin 3200d0c2c997SJarod Wilson dev->mtu = mtu; 320193a205eeSAaron Conole dev->max_mtu = mtu; 32022e123b44SMichael S. Tsirkin 32032e123b44SMichael S. Tsirkin /* TODO: size buffers correctly in this case. */ 32042e123b44SMichael S. Tsirkin if (dev->mtu > ETH_DATA_LEN) 32052e123b44SMichael S. Tsirkin vi->big_packets = true; 320614de9d11SAaron Conole } 320714de9d11SAaron Conole 3208012873d0SMichael S. Tsirkin if (vi->any_header_sg) 3209012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 32106ebbc1a6SZhangjie \(HZ\) 321144900010SJason Wang /* Enable multiqueue by default */ 321244900010SJason Wang if (num_online_cpus() >= max_queue_pairs) 321344900010SJason Wang vi->curr_queue_pairs = max_queue_pairs; 321444900010SJason Wang else 321544900010SJason Wang vi->curr_queue_pairs = num_online_cpus(); 3216986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 3217986a4f4dSJason Wang 3218986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 32193f9c10b0SAmit Shah err = init_vqs(vi); 3220d2a7dddaSMichael S. Tsirkin if (err) 3221d7dfc5cfSToshiaki Makita goto free; 3222d2a7dddaSMichael S. Tsirkin 3223fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 3224fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 3225fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 3226fbf28d78SMichael Dalton #endif 32270f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 32280f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 3229986a4f4dSJason Wang 323016032be5SNikolay Aleksandrov virtnet_init_settings(dev); 323116032be5SNikolay Aleksandrov 3232ba5e4426SSridhar Samudrala if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3233ba5e4426SSridhar Samudrala vi->failover = net_failover_create(vi->dev); 32344b8e6ac4SWei Yongjun if (IS_ERR(vi->failover)) { 32354b8e6ac4SWei Yongjun err = PTR_ERR(vi->failover); 3236ba5e4426SSridhar Samudrala goto free_vqs; 3237ba5e4426SSridhar Samudrala } 32384b8e6ac4SWei Yongjun } 3239ba5e4426SSridhar Samudrala 3240296f96fcSRusty Russell err = register_netdev(dev); 3241296f96fcSRusty Russell if (err) { 3242296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 3243ba5e4426SSridhar Samudrala goto free_failover; 3244296f96fcSRusty Russell } 3245b3369c1fSRusty Russell 32464baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 32474baf1e33SMichael S. Tsirkin 32488017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 32498de4b2f3SWanlong Gao if (err) { 32508de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 3251f00e35e2Swangyunjian goto free_unregister_netdev; 32528de4b2f3SWanlong Gao } 32538de4b2f3SWanlong Gao 3254a220871bSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 325544900010SJason Wang 3256167c25e4SJason Wang /* Assume link up if device can't report link status, 3257167c25e4SJason Wang otherwise get link status from config. */ 3258167c25e4SJason Wang netif_carrier_off(dev); 3259bda7fab5SJay Vosburgh if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 32603b07e9caSTejun Heo schedule_work(&vi->config_work); 3261167c25e4SJason Wang } else { 3262167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 3263faa9b39fSJason Baron virtnet_update_settings(vi); 32644783256eSPantelis Koukousoulas netif_carrier_on(dev); 3265167c25e4SJason Wang } 32669f4d26d0SMark McLoughlin 32673f93522fSJason Wang for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 32683f93522fSJason Wang if (virtio_has_feature(vi->vdev, guest_offloads[i])) 32693f93522fSJason Wang set_bit(guest_offloads[i], &vi->guest_offloads); 3270a02e8964SWillem de Bruijn vi->guest_offloads_capable = vi->guest_offloads; 32713f93522fSJason Wang 3272986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 3273986a4f4dSJason Wang dev->name, max_queue_pairs); 3274986a4f4dSJason Wang 3275296f96fcSRusty Russell return 0; 3276296f96fcSRusty Russell 3277f00e35e2Swangyunjian free_unregister_netdev: 327802465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 327902465555SMichael S. Tsirkin 3280b3369c1fSRusty Russell unregister_netdev(dev); 3281ba5e4426SSridhar Samudrala free_failover: 3282ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 3283d2a7dddaSMichael S. Tsirkin free_vqs: 3284986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 3285fb51879dSMichael Dalton free_receive_page_frags(vi); 3286e9d7417bSJason Wang virtnet_del_vqs(vi); 3287296f96fcSRusty Russell free: 3288296f96fcSRusty Russell free_netdev(dev); 3289296f96fcSRusty Russell return err; 3290296f96fcSRusty Russell } 3291296f96fcSRusty Russell 329204486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 3293296f96fcSRusty Russell { 329404486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 3295830a8a97SShirley Ma 3296830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 32979ab86bbcSShirley Ma free_unused_bufs(vi); 3298fb6813f4SRusty Russell 3299986a4f4dSJason Wang free_receive_bufs(vi); 3300d2a7dddaSMichael S. Tsirkin 3301fb51879dSMichael Dalton free_receive_page_frags(vi); 3302fb51879dSMichael Dalton 3303986a4f4dSJason Wang virtnet_del_vqs(vi); 330404486ed0SAmit Shah } 330504486ed0SAmit Shah 33068cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 330704486ed0SAmit Shah { 330804486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 330904486ed0SAmit Shah 33108017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 33118de4b2f3SWanlong Gao 3312102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 3313102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 3314586d17c5SJason Wang 331504486ed0SAmit Shah unregister_netdev(vi->dev); 331604486ed0SAmit Shah 3317ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 3318ba5e4426SSridhar Samudrala 331904486ed0SAmit Shah remove_vq_common(vi); 3320fb6813f4SRusty Russell 332174b2553fSRusty Russell free_netdev(vi->dev); 3322296f96fcSRusty Russell } 3323296f96fcSRusty Russell 332467a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 33250741bcb5SAmit Shah { 33260741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 33270741bcb5SAmit Shah 33288017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 33299fe7bfceSJohn Fastabend virtnet_freeze_down(vdev); 33300741bcb5SAmit Shah remove_vq_common(vi); 33310741bcb5SAmit Shah 33320741bcb5SAmit Shah return 0; 33330741bcb5SAmit Shah } 33340741bcb5SAmit Shah 333567a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 33360741bcb5SAmit Shah { 33370741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 33389fe7bfceSJohn Fastabend int err; 33390741bcb5SAmit Shah 33409fe7bfceSJohn Fastabend err = virtnet_restore_up(vdev); 33410741bcb5SAmit Shah if (err) 33420741bcb5SAmit Shah return err; 3343986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 3344986a4f4dSJason Wang 33458017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 33463f2869caSXie Yongji if (err) { 33473f2869caSXie Yongji virtnet_freeze_down(vdev); 33483f2869caSXie Yongji remove_vq_common(vi); 3349ec9debbdSJason Wang return err; 33503f2869caSXie Yongji } 3351ec9debbdSJason Wang 33520741bcb5SAmit Shah return 0; 33530741bcb5SAmit Shah } 33540741bcb5SAmit Shah 3355296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 3356296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 3357296f96fcSRusty Russell { 0 }, 3358296f96fcSRusty Russell }; 3359296f96fcSRusty Russell 3360f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \ 3361f3358507SMichael S. Tsirkin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 3362f3358507SMichael S. Tsirkin VIRTIO_NET_F_MAC, \ 3363f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 3364f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 3365f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 3366f3358507SMichael S. Tsirkin VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 3367f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 3368f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 3369f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_MAC_ADDR, \ 3370faa9b39fSJason Baron VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 33719805069dSSridhar Samudrala VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY 3372f3358507SMichael S. Tsirkin 3373c45a6816SRusty Russell static unsigned int features[] = { 3374f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 3375f3358507SMichael S. Tsirkin }; 3376f3358507SMichael S. Tsirkin 3377f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = { 3378f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 3379f3358507SMichael S. Tsirkin VIRTIO_NET_F_GSO, 3380e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 3381c45a6816SRusty Russell }; 3382c45a6816SRusty Russell 338322402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 3384c45a6816SRusty Russell .feature_table = features, 3385c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 3386f3358507SMichael S. Tsirkin .feature_table_legacy = features_legacy, 3387f3358507SMichael S. Tsirkin .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 3388296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 3389296f96fcSRusty Russell .driver.owner = THIS_MODULE, 3390296f96fcSRusty Russell .id_table = id_table, 3391fe36cbe0SMichael S. Tsirkin .validate = virtnet_validate, 3392296f96fcSRusty Russell .probe = virtnet_probe, 33938cc085d6SBill Pemberton .remove = virtnet_remove, 33949f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 339589107000SAaron Lu #ifdef CONFIG_PM_SLEEP 33960741bcb5SAmit Shah .freeze = virtnet_freeze, 33970741bcb5SAmit Shah .restore = virtnet_restore, 33980741bcb5SAmit Shah #endif 3399296f96fcSRusty Russell }; 3400296f96fcSRusty Russell 34018017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void) 34028017c279SSebastian Andrzej Siewior { 34038017c279SSebastian Andrzej Siewior int ret; 34048017c279SSebastian Andrzej Siewior 340573c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 34068017c279SSebastian Andrzej Siewior virtnet_cpu_online, 34078017c279SSebastian Andrzej Siewior virtnet_cpu_down_prep); 34088017c279SSebastian Andrzej Siewior if (ret < 0) 34098017c279SSebastian Andrzej Siewior goto out; 34108017c279SSebastian Andrzej Siewior virtionet_online = ret; 341173c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 34128017c279SSebastian Andrzej Siewior NULL, virtnet_cpu_dead); 34138017c279SSebastian Andrzej Siewior if (ret) 34148017c279SSebastian Andrzej Siewior goto err_dead; 34158017c279SSebastian Andrzej Siewior 34168017c279SSebastian Andrzej Siewior ret = register_virtio_driver(&virtio_net_driver); 34178017c279SSebastian Andrzej Siewior if (ret) 34188017c279SSebastian Andrzej Siewior goto err_virtio; 34198017c279SSebastian Andrzej Siewior return 0; 34208017c279SSebastian Andrzej Siewior err_virtio: 34218017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 34228017c279SSebastian Andrzej Siewior err_dead: 34238017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 34248017c279SSebastian Andrzej Siewior out: 34258017c279SSebastian Andrzej Siewior return ret; 34268017c279SSebastian Andrzej Siewior } 34278017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init); 34288017c279SSebastian Andrzej Siewior 34298017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void) 34308017c279SSebastian Andrzej Siewior { 3431cfa0ebc9SAndrew Jones unregister_virtio_driver(&virtio_net_driver); 34328017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 34338017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 34348017c279SSebastian Andrzej Siewior } 34358017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit); 3436296f96fcSRusty Russell 3437296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 3438296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 3439296f96fcSRusty Russell MODULE_LICENSE("GPL"); 3440