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 661a03b8a3STonghao Zhang #define GUEST_OFFLOAD_LRO_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 19897402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 19997402b96SHerbert Xu bool big_packets; 20097402b96SHerbert Xu 2013f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 2023f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 2033f2c31d9SMark McLoughlin 204986a4f4dSJason Wang /* Has control virtqueue */ 205986a4f4dSJason Wang bool has_cvq; 206986a4f4dSJason Wang 207e7428e95SMichael S. Tsirkin /* Host can handle any s/g split between our header and packet data */ 208e7428e95SMichael S. Tsirkin bool any_header_sg; 209e7428e95SMichael S. Tsirkin 210012873d0SMichael S. Tsirkin /* Packet virtio header size */ 211012873d0SMichael S. Tsirkin u8 hdr_len; 212012873d0SMichael S. Tsirkin 2133161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 2143161e453SRusty Russell struct delayed_work refill; 2153161e453SRusty Russell 216586d17c5SJason Wang /* Work struct for config space updates */ 217586d17c5SJason Wang struct work_struct config_work; 218586d17c5SJason Wang 219986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 220986a4f4dSJason Wang bool affinity_hint_set; 22147be2479SWanlong Gao 2228017c279SSebastian Andrzej Siewior /* CPU hotplug instances for online & dead */ 2238017c279SSebastian Andrzej Siewior struct hlist_node node; 2248017c279SSebastian Andrzej Siewior struct hlist_node node_dead; 2252ac46030SMichael S. Tsirkin 22612e57169SMichael S. Tsirkin struct control_buf *ctrl; 22716032be5SNikolay Aleksandrov 22816032be5SNikolay Aleksandrov /* Ethtool settings */ 22916032be5SNikolay Aleksandrov u8 duplex; 23016032be5SNikolay Aleksandrov u32 speed; 2313f93522fSJason Wang 2323f93522fSJason Wang unsigned long guest_offloads; 233a02e8964SWillem de Bruijn unsigned long guest_offloads_capable; 234ba5e4426SSridhar Samudrala 235ba5e4426SSridhar Samudrala /* failover when STANDBY feature enabled */ 236ba5e4426SSridhar Samudrala struct failover *failover; 237296f96fcSRusty Russell }; 238296f96fcSRusty Russell 2399ab86bbcSShirley Ma struct padded_vnet_hdr { 240012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr; 2419ab86bbcSShirley Ma /* 242012873d0SMichael S. Tsirkin * hdr is in a separate sg buffer, and data sg buffer shares same page 243012873d0SMichael S. Tsirkin * with this header sg. This padding makes next sg 16 byte aligned 244012873d0SMichael S. Tsirkin * after the header. 2459ab86bbcSShirley Ma */ 246012873d0SMichael S. Tsirkin char padding[4]; 2479ab86bbcSShirley Ma }; 2489ab86bbcSShirley Ma 2495050471dSToshiaki Makita static bool is_xdp_frame(void *ptr) 2505050471dSToshiaki Makita { 2515050471dSToshiaki Makita return (unsigned long)ptr & VIRTIO_XDP_FLAG; 2525050471dSToshiaki Makita } 2535050471dSToshiaki Makita 2545050471dSToshiaki Makita static void *xdp_to_ptr(struct xdp_frame *ptr) 2555050471dSToshiaki Makita { 2565050471dSToshiaki Makita return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG); 2575050471dSToshiaki Makita } 2585050471dSToshiaki Makita 2595050471dSToshiaki Makita static struct xdp_frame *ptr_to_xdp(void *ptr) 2605050471dSToshiaki Makita { 2615050471dSToshiaki Makita return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); 2625050471dSToshiaki Makita } 2635050471dSToshiaki Makita 264986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 265986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 266986a4f4dSJason Wang */ 267986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 268986a4f4dSJason Wang { 2699d0ca6edSRusty Russell return (vq->index - 1) / 2; 270986a4f4dSJason Wang } 271986a4f4dSJason Wang 272986a4f4dSJason Wang static int txq2vq(int txq) 273986a4f4dSJason Wang { 274986a4f4dSJason Wang return txq * 2 + 1; 275986a4f4dSJason Wang } 276986a4f4dSJason Wang 277986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 278986a4f4dSJason Wang { 2799d0ca6edSRusty Russell return vq->index / 2; 280986a4f4dSJason Wang } 281986a4f4dSJason Wang 282986a4f4dSJason Wang static int rxq2vq(int rxq) 283986a4f4dSJason Wang { 284986a4f4dSJason Wang return rxq * 2; 285986a4f4dSJason Wang } 286986a4f4dSJason Wang 287012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 288296f96fcSRusty Russell { 289012873d0SMichael S. Tsirkin return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 290296f96fcSRusty Russell } 291296f96fcSRusty Russell 2929ab86bbcSShirley Ma /* 2939ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 2949ab86bbcSShirley Ma * most recent used list in the beginning for reuse 2959ab86bbcSShirley Ma */ 296e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 297fb6813f4SRusty Russell { 2989ab86bbcSShirley Ma struct page *end; 2999ab86bbcSShirley Ma 300e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 3019ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 302e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 303e9d7417bSJason Wang rq->pages = page; 304fb6813f4SRusty Russell } 305fb6813f4SRusty Russell 306e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 307fb6813f4SRusty Russell { 308e9d7417bSJason Wang struct page *p = rq->pages; 309fb6813f4SRusty Russell 3109ab86bbcSShirley Ma if (p) { 311e9d7417bSJason Wang rq->pages = (struct page *)p->private; 3129ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 3139ab86bbcSShirley Ma p->private = 0; 3149ab86bbcSShirley Ma } else 315fb6813f4SRusty Russell p = alloc_page(gfp_mask); 316fb6813f4SRusty Russell return p; 317fb6813f4SRusty Russell } 318fb6813f4SRusty Russell 319e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi, 320e4e8452aSWillem de Bruijn struct virtqueue *vq) 321e4e8452aSWillem de Bruijn { 322e4e8452aSWillem de Bruijn if (napi_schedule_prep(napi)) { 323e4e8452aSWillem de Bruijn virtqueue_disable_cb(vq); 324e4e8452aSWillem de Bruijn __napi_schedule(napi); 325e4e8452aSWillem de Bruijn } 326e4e8452aSWillem de Bruijn } 327e4e8452aSWillem de Bruijn 328e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi, 329e4e8452aSWillem de Bruijn struct virtqueue *vq, int processed) 330e4e8452aSWillem de Bruijn { 331e4e8452aSWillem de Bruijn int opaque; 332e4e8452aSWillem de Bruijn 333e4e8452aSWillem de Bruijn opaque = virtqueue_enable_cb_prepare(vq); 334fdaa767aSToshiaki Makita if (napi_complete_done(napi, processed)) { 335fdaa767aSToshiaki Makita if (unlikely(virtqueue_poll(vq, opaque))) 336e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 337fdaa767aSToshiaki Makita } else { 338fdaa767aSToshiaki Makita virtqueue_disable_cb(vq); 339fdaa767aSToshiaki Makita } 340e4e8452aSWillem de Bruijn } 341e4e8452aSWillem de Bruijn 342e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 343296f96fcSRusty Russell { 344e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 345b92f1e67SWillem de Bruijn struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 346296f96fcSRusty Russell 3472cb9c6baSRusty Russell /* Suppress further interrupts. */ 348e9d7417bSJason Wang virtqueue_disable_cb(vq); 34911a3a154SRusty Russell 350b92f1e67SWillem de Bruijn if (napi->weight) 351b92f1e67SWillem de Bruijn virtqueue_napi_schedule(napi, vq); 352b92f1e67SWillem de Bruijn else 353363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 354986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 355296f96fcSRusty Russell } 356296f96fcSRusty Russell 35728b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22 35828b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize, 35928b39bc7SJason Wang unsigned int headroom) 36028b39bc7SJason Wang { 36128b39bc7SJason Wang return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 36228b39bc7SJason Wang } 36328b39bc7SJason Wang 36428b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 36528b39bc7SJason Wang { 36628b39bc7SJason Wang return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 36728b39bc7SJason Wang } 36828b39bc7SJason Wang 36928b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 37028b39bc7SJason Wang { 37128b39bc7SJason Wang return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 37228b39bc7SJason Wang } 37328b39bc7SJason Wang 3743464645aSMike Waychison /* Called from bottom half context */ 375946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 376946fa564SMichael S. Tsirkin struct receive_queue *rq, 3772613af0eSMichael Dalton struct page *page, unsigned int offset, 378436c9453SJason Wang unsigned int len, unsigned int truesize, 379503d539aSYuya Kusakabe bool hdr_valid, unsigned int metasize) 3809ab86bbcSShirley Ma { 3819ab86bbcSShirley Ma struct sk_buff *skb; 382012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 3832613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 3849ab86bbcSShirley Ma char *p; 3859ab86bbcSShirley Ma 3862613af0eSMichael Dalton p = page_address(page) + offset; 3879ab86bbcSShirley Ma 3889ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 389c67f5db8SPaolo Abeni skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 3909ab86bbcSShirley Ma if (unlikely(!skb)) 3919ab86bbcSShirley Ma return NULL; 3929ab86bbcSShirley Ma 3939ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 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 401503d539aSYuya Kusakabe /* hdr_valid means no XDP, so we can copy the vnet header */ 402436c9453SJason Wang if (hdr_valid) 4039ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 4043f2c31d9SMark McLoughlin 4059ab86bbcSShirley Ma len -= hdr_len; 4062613af0eSMichael Dalton offset += hdr_padded_len; 4072613af0eSMichael Dalton p += hdr_padded_len; 4083f2c31d9SMark McLoughlin 4093f2c31d9SMark McLoughlin copy = len; 4103f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 4113f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 41259ae1d12SJohannes Berg skb_put_data(skb, p, copy); 4133f2c31d9SMark McLoughlin 414503d539aSYuya Kusakabe if (metasize) { 415503d539aSYuya Kusakabe __skb_pull(skb, metasize); 416503d539aSYuya Kusakabe skb_metadata_set(skb, metasize); 417503d539aSYuya Kusakabe } 418503d539aSYuya Kusakabe 4193f2c31d9SMark McLoughlin len -= copy; 4209ab86bbcSShirley Ma offset += copy; 4213f2c31d9SMark McLoughlin 4222613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 4232613af0eSMichael Dalton if (len) 4242613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 4252613af0eSMichael Dalton else 4262613af0eSMichael Dalton put_page(page); 4272613af0eSMichael Dalton return skb; 4282613af0eSMichael Dalton } 4292613af0eSMichael Dalton 430e878d78bSSasha Levin /* 431e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 432e878d78bSSasha Levin * This is here to handle cases when the device erroneously 433e878d78bSSasha Levin * tries to receive more than is possible. This is usually 434e878d78bSSasha Levin * the case of a broken device. 435e878d78bSSasha Levin */ 436e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 437be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 438e878d78bSSasha Levin dev_kfree_skb(skb); 439e878d78bSSasha Levin return NULL; 440e878d78bSSasha Levin } 4412613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 4429ab86bbcSShirley Ma while (len) { 4432613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 4442613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 4452613af0eSMichael Dalton frag_size, truesize); 4462613af0eSMichael Dalton len -= frag_size; 4479ab86bbcSShirley Ma page = (struct page *)page->private; 4489ab86bbcSShirley Ma offset = 0; 4493f2c31d9SMark McLoughlin } 4503f2c31d9SMark McLoughlin 4519ab86bbcSShirley Ma if (page) 452e9d7417bSJason Wang give_pages(rq, page); 4533f2c31d9SMark McLoughlin 4549ab86bbcSShirley Ma return skb; 4559ab86bbcSShirley Ma } 4569ab86bbcSShirley Ma 457735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, 458735fc405SJesper Dangaard Brouer struct send_queue *sq, 45944fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf) 46056434a01SJohn Fastabend { 46156434a01SJohn Fastabend struct virtio_net_hdr_mrg_rxbuf *hdr; 46256434a01SJohn Fastabend int err; 46356434a01SJohn Fastabend 464cac320c8SJesper Dangaard Brouer if (unlikely(xdpf->headroom < vi->hdr_len)) 465cac320c8SJesper Dangaard Brouer return -EOVERFLOW; 466cac320c8SJesper Dangaard Brouer 467cac320c8SJesper Dangaard Brouer /* Make room for virtqueue hdr (also change xdpf->headroom?) */ 468cac320c8SJesper Dangaard Brouer xdpf->data -= vi->hdr_len; 46956434a01SJohn Fastabend /* Zero header and leave csum up to XDP layers */ 470cac320c8SJesper Dangaard Brouer hdr = xdpf->data; 47156434a01SJohn Fastabend memset(hdr, 0, vi->hdr_len); 472cac320c8SJesper Dangaard Brouer xdpf->len += vi->hdr_len; 47356434a01SJohn Fastabend 474cac320c8SJesper Dangaard Brouer sg_init_one(sq->sg, xdpf->data, xdpf->len); 475bb91accfSJason Wang 4765050471dSToshiaki Makita err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf), 4775050471dSToshiaki Makita GFP_ATOMIC); 47811b7d897SJesper Dangaard Brouer if (unlikely(err)) 479cac320c8SJesper Dangaard Brouer return -ENOSPC; /* Caller handle free/refcnt */ 48056434a01SJohn Fastabend 481cac320c8SJesper Dangaard Brouer return 0; 48256434a01SJohn Fastabend } 48356434a01SJohn Fastabend 4842a43565cSToshiaki Makita static struct send_queue *virtnet_xdp_sq(struct virtnet_info *vi) 4852a43565cSToshiaki Makita { 4862a43565cSToshiaki Makita unsigned int qp; 4872a43565cSToshiaki Makita 4882a43565cSToshiaki Makita qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); 4892a43565cSToshiaki Makita return &vi->sq[qp]; 4902a43565cSToshiaki Makita } 4912a43565cSToshiaki Makita 492735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev, 49342b33468SJesper Dangaard Brouer int n, struct xdp_frame **frames, u32 flags) 494186b3c99SJason Wang { 495186b3c99SJason Wang struct virtnet_info *vi = netdev_priv(dev); 4968dcc5b0aSJesper Dangaard Brouer struct receive_queue *rq = vi->rq; 4978dcc5b0aSJesper Dangaard Brouer struct bpf_prog *xdp_prog; 498735fc405SJesper Dangaard Brouer struct send_queue *sq; 499735fc405SJesper Dangaard Brouer unsigned int len; 500546f2897SToshiaki Makita int packets = 0; 501546f2897SToshiaki Makita int bytes = 0; 502*fdc13979SLorenzo Bianconi int nxmit = 0; 503461f03dcSToshiaki Makita int kicks = 0; 5045050471dSToshiaki Makita void *ptr; 505*fdc13979SLorenzo Bianconi int ret; 506735fc405SJesper Dangaard Brouer int i; 507735fc405SJesper Dangaard Brouer 5088dcc5b0aSJesper Dangaard Brouer /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 5098dcc5b0aSJesper Dangaard Brouer * indicate XDP resources have been successfully allocated. 5108dcc5b0aSJesper Dangaard Brouer */ 5119719c6b9SJohn Fastabend xdp_prog = rcu_access_pointer(rq->xdp_prog); 5121667c08aSToshiaki Makita if (!xdp_prog) 5131667c08aSToshiaki Makita return -ENXIO; 5141667c08aSToshiaki Makita 5159ab86bbcSShirley Ma sq = virtnet_xdp_sq(vi); 5169ab86bbcSShirley Ma 5179ab86bbcSShirley Ma if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { 518186b3c99SJason Wang ret = -EINVAL; 519186b3c99SJason Wang goto out; 520186b3c99SJason Wang } 521186b3c99SJason Wang 522735fc405SJesper Dangaard Brouer /* Free up any pending old buffers before queueing new ones. */ 5235050471dSToshiaki Makita while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 524546f2897SToshiaki Makita if (likely(is_xdp_frame(ptr))) { 525546f2897SToshiaki Makita struct xdp_frame *frame = ptr_to_xdp(ptr); 526546f2897SToshiaki Makita 527546f2897SToshiaki Makita bytes += frame->len; 528546f2897SToshiaki Makita xdp_return_frame(frame); 529546f2897SToshiaki Makita } else { 530546f2897SToshiaki Makita struct sk_buff *skb = ptr; 531546f2897SToshiaki Makita 532546f2897SToshiaki Makita bytes += skb->len; 533546f2897SToshiaki Makita napi_consume_skb(skb, false); 534546f2897SToshiaki Makita } 535546f2897SToshiaki Makita packets++; 5365050471dSToshiaki Makita } 537735fc405SJesper Dangaard Brouer 538735fc405SJesper Dangaard Brouer for (i = 0; i < n; i++) { 539735fc405SJesper Dangaard Brouer struct xdp_frame *xdpf = frames[i]; 540735fc405SJesper Dangaard Brouer 541*fdc13979SLorenzo Bianconi if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) 542*fdc13979SLorenzo Bianconi break; 543*fdc13979SLorenzo Bianconi nxmit++; 544735fc405SJesper Dangaard Brouer } 545*fdc13979SLorenzo Bianconi ret = nxmit; 5465d274cb4SJesper Dangaard Brouer 547461f03dcSToshiaki Makita if (flags & XDP_XMIT_FLUSH) { 548461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) 549461f03dcSToshiaki Makita kicks = 1; 550461f03dcSToshiaki Makita } 5515b8f3c8dSToshiaki Makita out: 5525b8f3c8dSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 553546f2897SToshiaki Makita sq->stats.bytes += bytes; 554546f2897SToshiaki Makita sq->stats.packets += packets; 5555b8f3c8dSToshiaki Makita sq->stats.xdp_tx += n; 556*fdc13979SLorenzo Bianconi sq->stats.xdp_tx_drops += n - nxmit; 557461f03dcSToshiaki Makita sq->stats.kicks += kicks; 5585b8f3c8dSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 5595d274cb4SJesper Dangaard Brouer 5605b8f3c8dSToshiaki Makita return ret; 561186b3c99SJason Wang } 562186b3c99SJason Wang 563f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 564f6b10209SJason Wang { 565f6b10209SJason Wang return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0; 566f6b10209SJason Wang } 567f6b10209SJason Wang 5684941d472SJason Wang /* We copy the packet for XDP in the following cases: 5694941d472SJason Wang * 5704941d472SJason Wang * 1) Packet is scattered across multiple rx buffers. 5714941d472SJason Wang * 2) Headroom space is insufficient. 5724941d472SJason Wang * 5734941d472SJason Wang * This is inefficient but it's a temporary condition that 5744941d472SJason Wang * we hit right after XDP is enabled and until queue is refilled 5754941d472SJason Wang * with large buffers with sufficient headroom - so it should affect 5764941d472SJason Wang * at most queue size packets. 5774941d472SJason Wang * Afterwards, the conditions to enable 5784941d472SJason Wang * XDP should preclude the underlying device from sending packets 5794941d472SJason Wang * across multiple buffers (num_buf > 1), and we make sure buffers 5804941d472SJason Wang * have enough headroom. 58172979a6cSJohn Fastabend */ 58272979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq, 58356a86f84SJason Wang u16 *num_buf, 58472979a6cSJohn Fastabend struct page *p, 58572979a6cSJohn Fastabend int offset, 5864941d472SJason Wang int page_off, 58772979a6cSJohn Fastabend unsigned int *len) 58872979a6cSJohn Fastabend { 58972979a6cSJohn Fastabend struct page *page = alloc_page(GFP_ATOMIC); 59072979a6cSJohn Fastabend 59172979a6cSJohn Fastabend if (!page) 59272979a6cSJohn Fastabend return NULL; 59372979a6cSJohn Fastabend 59472979a6cSJohn Fastabend memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 59572979a6cSJohn Fastabend page_off += *len; 59672979a6cSJohn Fastabend 59756a86f84SJason Wang while (--*num_buf) { 5983cc81a9aSJason Wang int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 59972979a6cSJohn Fastabend unsigned int buflen; 60072979a6cSJohn Fastabend void *buf; 60172979a6cSJohn Fastabend int off; 60272979a6cSJohn Fastabend 603680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &buflen); 604680557cfSMichael S. Tsirkin if (unlikely(!buf)) 60572979a6cSJohn Fastabend goto err_buf; 60672979a6cSJohn Fastabend 60772979a6cSJohn Fastabend p = virt_to_head_page(buf); 60872979a6cSJohn Fastabend off = buf - page_address(p); 60972979a6cSJohn Fastabend 61056a86f84SJason Wang /* guard against a misconfigured or uncooperative backend that 61156a86f84SJason Wang * is sending packet larger than the MTU. 61256a86f84SJason Wang */ 6133cc81a9aSJason Wang if ((page_off + buflen + tailroom) > PAGE_SIZE) { 61456a86f84SJason Wang put_page(p); 61556a86f84SJason Wang goto err_buf; 61656a86f84SJason Wang } 61756a86f84SJason Wang 61872979a6cSJohn Fastabend memcpy(page_address(page) + page_off, 61972979a6cSJohn Fastabend page_address(p) + off, buflen); 62072979a6cSJohn Fastabend page_off += buflen; 62156a86f84SJason Wang put_page(p); 62272979a6cSJohn Fastabend } 62372979a6cSJohn Fastabend 6242de2f7f4SJohn Fastabend /* Headroom does not contribute to packet length */ 6252de2f7f4SJohn Fastabend *len = page_off - VIRTIO_XDP_HEADROOM; 62672979a6cSJohn Fastabend return page; 62772979a6cSJohn Fastabend err_buf: 62872979a6cSJohn Fastabend __free_pages(page, 0); 62972979a6cSJohn Fastabend return NULL; 63072979a6cSJohn Fastabend } 63172979a6cSJohn Fastabend 6324941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev, 6334941d472SJason Wang struct virtnet_info *vi, 6344941d472SJason Wang struct receive_queue *rq, 6354941d472SJason Wang void *buf, void *ctx, 636186b3c99SJason Wang unsigned int len, 6377d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 638d46eeeafSJason Wang struct virtnet_rq_stats *stats) 6394941d472SJason Wang { 6404941d472SJason Wang struct sk_buff *skb; 6414941d472SJason Wang struct bpf_prog *xdp_prog; 6424941d472SJason Wang unsigned int xdp_headroom = (unsigned long)ctx; 6434941d472SJason Wang unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 6444941d472SJason Wang unsigned int headroom = vi->hdr_len + header_offset; 6454941d472SJason Wang unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 6464941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 6474941d472SJason Wang struct page *page = virt_to_head_page(buf); 64811b7d897SJesper Dangaard Brouer unsigned int delta = 0; 6494941d472SJason Wang struct page *xdp_page; 65011b7d897SJesper Dangaard Brouer int err; 651503d539aSYuya Kusakabe unsigned int metasize = 0; 65211b7d897SJesper Dangaard Brouer 6534941d472SJason Wang len -= vi->hdr_len; 654d46eeeafSJason Wang stats->bytes += len; 6554941d472SJason Wang 6564941d472SJason Wang rcu_read_lock(); 6574941d472SJason Wang xdp_prog = rcu_dereference(rq->xdp_prog); 6584941d472SJason Wang if (xdp_prog) { 6594941d472SJason Wang struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 66044fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 6614941d472SJason Wang struct xdp_buff xdp; 6624941d472SJason Wang void *orig_data; 6634941d472SJason Wang u32 act; 6644941d472SJason Wang 66595dbe9e7SJesper Dangaard Brouer if (unlikely(hdr->hdr.gso_type)) 6664941d472SJason Wang goto err_xdp; 6674941d472SJason Wang 6684941d472SJason Wang if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 6694941d472SJason Wang int offset = buf - page_address(page) + header_offset; 6704941d472SJason Wang unsigned int tlen = len + vi->hdr_len; 6714941d472SJason Wang u16 num_buf = 1; 6724941d472SJason Wang 6734941d472SJason Wang xdp_headroom = virtnet_get_headroom(vi); 6744941d472SJason Wang header_offset = VIRTNET_RX_PAD + xdp_headroom; 6754941d472SJason Wang headroom = vi->hdr_len + header_offset; 6764941d472SJason Wang buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 6774941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 6784941d472SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, page, 6794941d472SJason Wang offset, header_offset, 6804941d472SJason Wang &tlen); 6814941d472SJason Wang if (!xdp_page) 6824941d472SJason Wang goto err_xdp; 6834941d472SJason Wang 6844941d472SJason Wang buf = page_address(xdp_page); 6854941d472SJason Wang put_page(page); 6864941d472SJason Wang page = xdp_page; 6874941d472SJason Wang } 6884941d472SJason Wang 68943b5169dSLorenzo Bianconi xdp_init_buff(&xdp, buflen, &rq->xdp_rxq); 690be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, 691be9df4afSLorenzo Bianconi xdp_headroom, len, true); 6924941d472SJason Wang orig_data = xdp.data; 6934941d472SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 694d46eeeafSJason Wang stats->xdp_packets++; 6954941d472SJason Wang 6964941d472SJason Wang switch (act) { 6974941d472SJason Wang case XDP_PASS: 6984941d472SJason Wang /* Recalculate length in case bpf program changed it */ 6994941d472SJason Wang delta = orig_data - xdp.data; 7006870de43SNikita V. Shirokov len = xdp.data_end - xdp.data; 701503d539aSYuya Kusakabe metasize = xdp.data - xdp.data_meta; 7024941d472SJason Wang break; 7034941d472SJason Wang case XDP_TX: 704d46eeeafSJason Wang stats->xdp_tx++; 7051b698fa5SLorenzo Bianconi xdpf = xdp_convert_buff_to_frame(&xdp); 70644fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 70744fa2dbdSJesper Dangaard Brouer goto err_xdp; 708ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 709*fdc13979SLorenzo Bianconi if (unlikely(!err)) { 710*fdc13979SLorenzo Bianconi xdp_return_frame_rx_napi(xdpf); 711*fdc13979SLorenzo Bianconi } else if (unlikely(err < 0)) { 7124941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 71311b7d897SJesper Dangaard Brouer goto err_xdp; 71411b7d897SJesper Dangaard Brouer } 7152471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 716186b3c99SJason Wang rcu_read_unlock(); 717186b3c99SJason Wang goto xdp_xmit; 718186b3c99SJason Wang case XDP_REDIRECT: 719d46eeeafSJason Wang stats->xdp_redirects++; 720186b3c99SJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 72111b7d897SJesper Dangaard Brouer if (err) 72211b7d897SJesper Dangaard Brouer goto err_xdp; 7232471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 7244941d472SJason Wang rcu_read_unlock(); 7254941d472SJason Wang goto xdp_xmit; 7264941d472SJason Wang default: 7274941d472SJason Wang bpf_warn_invalid_xdp_action(act); 728df561f66SGustavo A. R. Silva fallthrough; 7294941d472SJason Wang case XDP_ABORTED: 7304941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 73195efabf0SGustavo A. R. Silva goto err_xdp; 7324941d472SJason Wang case XDP_DROP: 7334941d472SJason Wang goto err_xdp; 7344941d472SJason Wang } 7354941d472SJason Wang } 7364941d472SJason Wang rcu_read_unlock(); 7374941d472SJason Wang 7384941d472SJason Wang skb = build_skb(buf, buflen); 7394941d472SJason Wang if (!skb) { 7404941d472SJason Wang put_page(page); 7414941d472SJason Wang goto err; 7424941d472SJason Wang } 7434941d472SJason Wang skb_reserve(skb, headroom - delta); 7446870de43SNikita V. Shirokov skb_put(skb, len); 745f1d4884dSYuya Kusakabe if (!xdp_prog) { 7464941d472SJason Wang buf += header_offset; 7474941d472SJason Wang memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 748f1d4884dSYuya Kusakabe } /* keep zeroed vnet hdr since XDP is loaded */ 7494941d472SJason Wang 750503d539aSYuya Kusakabe if (metasize) 751503d539aSYuya Kusakabe skb_metadata_set(skb, metasize); 752503d539aSYuya Kusakabe 7534941d472SJason Wang err: 7544941d472SJason Wang return skb; 7554941d472SJason Wang 7564941d472SJason Wang err_xdp: 7574941d472SJason Wang rcu_read_unlock(); 758d46eeeafSJason Wang stats->xdp_drops++; 759d46eeeafSJason Wang stats->drops++; 7604941d472SJason Wang put_page(page); 7614941d472SJason Wang xdp_xmit: 7624941d472SJason Wang return NULL; 7634941d472SJason Wang } 7644941d472SJason Wang 7654941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev, 7664941d472SJason Wang struct virtnet_info *vi, 7674941d472SJason Wang struct receive_queue *rq, 7684941d472SJason Wang void *buf, 7697d9d60fdSToshiaki Makita unsigned int len, 770d46eeeafSJason Wang struct virtnet_rq_stats *stats) 7714941d472SJason Wang { 7724941d472SJason Wang struct page *page = buf; 773503d539aSYuya Kusakabe struct sk_buff *skb = 774503d539aSYuya Kusakabe page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0); 7754941d472SJason Wang 776d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 7774941d472SJason Wang if (unlikely(!skb)) 7784941d472SJason Wang goto err; 7794941d472SJason Wang 7804941d472SJason Wang return skb; 7814941d472SJason Wang 7824941d472SJason Wang err: 783d46eeeafSJason Wang stats->drops++; 7844941d472SJason Wang give_pages(rq, page); 7854941d472SJason Wang return NULL; 7864941d472SJason Wang } 7874941d472SJason Wang 7888fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 789fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 7908fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 791680557cfSMichael S. Tsirkin void *buf, 792680557cfSMichael S. Tsirkin void *ctx, 793186b3c99SJason Wang unsigned int len, 7947d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 795d46eeeafSJason Wang struct virtnet_rq_stats *stats) 7969ab86bbcSShirley Ma { 797012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 798012873d0SMichael S. Tsirkin u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 7998fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 8008fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 801f600b690SJohn Fastabend struct sk_buff *head_skb, *curr_skb; 802f600b690SJohn Fastabend struct bpf_prog *xdp_prog; 8039ce6146eSJesper Dangaard Brouer unsigned int truesize = mergeable_ctx_to_truesize(ctx); 8044941d472SJason Wang unsigned int headroom = mergeable_ctx_to_headroom(ctx); 805503d539aSYuya Kusakabe unsigned int metasize = 0; 8069ce6146eSJesper Dangaard Brouer unsigned int frame_sz; 8079ce6146eSJesper Dangaard Brouer int err; 808ab7db917SMichael Dalton 80956434a01SJohn Fastabend head_skb = NULL; 810d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 81156434a01SJohn Fastabend 812f600b690SJohn Fastabend rcu_read_lock(); 813f600b690SJohn Fastabend xdp_prog = rcu_dereference(rq->xdp_prog); 814f600b690SJohn Fastabend if (xdp_prog) { 81544fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 81672979a6cSJohn Fastabend struct page *xdp_page; 8170354e4d1SJohn Fastabend struct xdp_buff xdp; 8180354e4d1SJohn Fastabend void *data; 819f600b690SJohn Fastabend u32 act; 820f600b690SJohn Fastabend 8213d62b2a0SJason Wang /* Transient failure which in theory could occur if 8223d62b2a0SJason Wang * in-flight packets from before XDP was enabled reach 8233d62b2a0SJason Wang * the receive path after XDP is loaded. 8243d62b2a0SJason Wang */ 8253d62b2a0SJason Wang if (unlikely(hdr->hdr.gso_type)) 8263d62b2a0SJason Wang goto err_xdp; 8273d62b2a0SJason Wang 8289ce6146eSJesper Dangaard Brouer /* Buffers with headroom use PAGE_SIZE as alloc size, 8299ce6146eSJesper Dangaard Brouer * see add_recvbuf_mergeable() + get_mergeable_buf_len() 8309ce6146eSJesper Dangaard Brouer */ 8319ce6146eSJesper Dangaard Brouer frame_sz = headroom ? PAGE_SIZE : truesize; 8329ce6146eSJesper Dangaard Brouer 8333cc81a9aSJason Wang /* This happens when rx buffer size is underestimated 8343cc81a9aSJason Wang * or headroom is not enough because of the buffer 8353cc81a9aSJason Wang * was refilled before XDP is set. This should only 8363cc81a9aSJason Wang * happen for the first several packets, so we don't 8373cc81a9aSJason Wang * care much about its performance. 8383cc81a9aSJason Wang */ 8394941d472SJason Wang if (unlikely(num_buf > 1 || 8404941d472SJason Wang headroom < virtnet_get_headroom(vi))) { 84172979a6cSJohn Fastabend /* linearize data for XDP */ 84256a86f84SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, 8434941d472SJason Wang page, offset, 8444941d472SJason Wang VIRTIO_XDP_HEADROOM, 8454941d472SJason Wang &len); 8469ce6146eSJesper Dangaard Brouer frame_sz = PAGE_SIZE; 8479ce6146eSJesper Dangaard Brouer 84872979a6cSJohn Fastabend if (!xdp_page) 849f600b690SJohn Fastabend goto err_xdp; 8502de2f7f4SJohn Fastabend offset = VIRTIO_XDP_HEADROOM; 85172979a6cSJohn Fastabend } else { 85272979a6cSJohn Fastabend xdp_page = page; 853f600b690SJohn Fastabend } 854f600b690SJohn Fastabend 8552de2f7f4SJohn Fastabend /* Allow consuming headroom but reserve enough space to push 8562de2f7f4SJohn Fastabend * the descriptor on if we get an XDP_TX return code. 8572de2f7f4SJohn Fastabend */ 8580354e4d1SJohn Fastabend data = page_address(xdp_page) + offset; 85943b5169dSLorenzo Bianconi xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq); 860be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len, 861be9df4afSLorenzo Bianconi VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true); 862754b8a21SJesper Dangaard Brouer 8630354e4d1SJohn Fastabend act = bpf_prog_run_xdp(xdp_prog, &xdp); 864d46eeeafSJason Wang stats->xdp_packets++; 8650354e4d1SJohn Fastabend 86656434a01SJohn Fastabend switch (act) { 86756434a01SJohn Fastabend case XDP_PASS: 868503d539aSYuya Kusakabe metasize = xdp.data - xdp.data_meta; 8692de2f7f4SJohn Fastabend 870503d539aSYuya Kusakabe /* recalculate offset to account for any header 871503d539aSYuya Kusakabe * adjustments and minus the metasize to copy the 872503d539aSYuya Kusakabe * metadata in page_to_skb(). Note other cases do not 873503d539aSYuya Kusakabe * build an skb and avoid using offset 8746870de43SNikita V. Shirokov */ 875503d539aSYuya Kusakabe offset = xdp.data - page_address(xdp_page) - 876503d539aSYuya Kusakabe vi->hdr_len - metasize; 877503d539aSYuya Kusakabe 878503d539aSYuya Kusakabe /* recalculate len if xdp.data, xdp.data_end or 879503d539aSYuya Kusakabe * xdp.data_meta were adjusted 880503d539aSYuya Kusakabe */ 881503d539aSYuya Kusakabe len = xdp.data_end - xdp.data + vi->hdr_len + metasize; 8821830f893SJason Wang /* We can only create skb based on xdp_page. */ 8831830f893SJason Wang if (unlikely(xdp_page != page)) { 8841830f893SJason Wang rcu_read_unlock(); 8851830f893SJason Wang put_page(page); 886503d539aSYuya Kusakabe head_skb = page_to_skb(vi, rq, xdp_page, offset, 887503d539aSYuya Kusakabe len, PAGE_SIZE, false, 888503d539aSYuya Kusakabe metasize); 8891830f893SJason Wang return head_skb; 8901830f893SJason Wang } 89156434a01SJohn Fastabend break; 89256434a01SJohn Fastabend case XDP_TX: 893d46eeeafSJason Wang stats->xdp_tx++; 8941b698fa5SLorenzo Bianconi xdpf = xdp_convert_buff_to_frame(&xdp); 89544fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 89644fa2dbdSJesper Dangaard Brouer goto err_xdp; 897ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 898*fdc13979SLorenzo Bianconi if (unlikely(!err)) { 899*fdc13979SLorenzo Bianconi xdp_return_frame_rx_napi(xdpf); 900*fdc13979SLorenzo Bianconi } else if (unlikely(err < 0)) { 9010354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 90211b7d897SJesper Dangaard Brouer if (unlikely(xdp_page != page)) 90311b7d897SJesper Dangaard Brouer put_page(xdp_page); 90411b7d897SJesper Dangaard Brouer goto err_xdp; 90511b7d897SJesper Dangaard Brouer } 9062471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 90772979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 9085d458a13SJason Wang put_page(page); 90956434a01SJohn Fastabend rcu_read_unlock(); 91056434a01SJohn Fastabend goto xdp_xmit; 9113cc81a9aSJason Wang case XDP_REDIRECT: 912d46eeeafSJason Wang stats->xdp_redirects++; 9133cc81a9aSJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 9143cc81a9aSJason Wang if (err) { 9153cc81a9aSJason Wang if (unlikely(xdp_page != page)) 9163cc81a9aSJason Wang put_page(xdp_page); 9173cc81a9aSJason Wang goto err_xdp; 9183cc81a9aSJason Wang } 9192471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 9203cc81a9aSJason Wang if (unlikely(xdp_page != page)) 9216890418bSJason Wang put_page(page); 9223cc81a9aSJason Wang rcu_read_unlock(); 9233cc81a9aSJason Wang goto xdp_xmit; 92456434a01SJohn Fastabend default: 9250354e4d1SJohn Fastabend bpf_warn_invalid_xdp_action(act); 926df561f66SGustavo A. R. Silva fallthrough; 9270354e4d1SJohn Fastabend case XDP_ABORTED: 9280354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 929df561f66SGustavo A. R. Silva fallthrough; 9300354e4d1SJohn Fastabend case XDP_DROP: 93172979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 93272979a6cSJohn Fastabend __free_pages(xdp_page, 0); 933f600b690SJohn Fastabend goto err_xdp; 934f600b690SJohn Fastabend } 93556434a01SJohn Fastabend } 936f600b690SJohn Fastabend rcu_read_unlock(); 937f600b690SJohn Fastabend 93828b39bc7SJason Wang if (unlikely(len > truesize)) { 93956da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 940680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 941680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 942680557cfSMichael S. Tsirkin goto err_skb; 943680557cfSMichael S. Tsirkin } 94428b39bc7SJason Wang 945503d539aSYuya Kusakabe head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog, 946503d539aSYuya Kusakabe metasize); 947f600b690SJohn Fastabend curr_skb = head_skb; 9489ab86bbcSShirley Ma 9498fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 9508fc3b9e9SMichael S. Tsirkin goto err_skb; 9519ab86bbcSShirley Ma while (--num_buf) { 9528fc3b9e9SMichael S. Tsirkin int num_skb_frags; 9538fc3b9e9SMichael S. Tsirkin 954680557cfSMichael S. Tsirkin buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 95503e9f8a0SYunjian Wang if (unlikely(!buf)) { 9568fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 957fdd819b2SMichael S. Tsirkin dev->name, num_buf, 958012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 959012873d0SMichael S. Tsirkin hdr->num_buffers)); 9608fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 9618fc3b9e9SMichael S. Tsirkin goto err_buf; 9623f2c31d9SMark McLoughlin } 9638fc3b9e9SMichael S. Tsirkin 964d46eeeafSJason Wang stats->bytes += len; 9658fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 96628b39bc7SJason Wang 96728b39bc7SJason Wang truesize = mergeable_ctx_to_truesize(ctx); 96828b39bc7SJason Wang if (unlikely(len > truesize)) { 96956da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 970680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 971680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 972680557cfSMichael S. Tsirkin goto err_skb; 973680557cfSMichael S. Tsirkin } 9748fc3b9e9SMichael S. Tsirkin 9758fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 9762613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 9772613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 9788fc3b9e9SMichael S. Tsirkin 9798fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 9808fc3b9e9SMichael S. Tsirkin goto err_skb; 9812613af0eSMichael Dalton if (curr_skb == head_skb) 9822613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 9832613af0eSMichael Dalton else 9842613af0eSMichael Dalton curr_skb->next = nskb; 9852613af0eSMichael Dalton curr_skb = nskb; 9862613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 9872613af0eSMichael Dalton num_skb_frags = 0; 9882613af0eSMichael Dalton } 9892613af0eSMichael Dalton if (curr_skb != head_skb) { 9902613af0eSMichael Dalton head_skb->data_len += len; 9912613af0eSMichael Dalton head_skb->len += len; 992fb51879dSMichael Dalton head_skb->truesize += truesize; 9932613af0eSMichael Dalton } 9948fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 995ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 996ba275241SJason Wang put_page(page); 997ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 998fb51879dSMichael Dalton len, truesize); 999ba275241SJason Wang } else { 10002613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 1001fb51879dSMichael Dalton offset, len, truesize); 1002ba275241SJason Wang } 10038fc3b9e9SMichael S. Tsirkin } 10048fc3b9e9SMichael S. Tsirkin 10055377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 10068fc3b9e9SMichael S. Tsirkin return head_skb; 10078fc3b9e9SMichael S. Tsirkin 1008f600b690SJohn Fastabend err_xdp: 1009f600b690SJohn Fastabend rcu_read_unlock(); 1010d46eeeafSJason Wang stats->xdp_drops++; 10118fc3b9e9SMichael S. Tsirkin err_skb: 10128fc3b9e9SMichael S. Tsirkin put_page(page); 1013850e088dSJason Wang while (num_buf-- > 1) { 1014680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &len); 1015680557cfSMichael S. Tsirkin if (unlikely(!buf)) { 10168fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 10178fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 10188fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 10198fc3b9e9SMichael S. Tsirkin break; 10208fc3b9e9SMichael S. Tsirkin } 1021d46eeeafSJason Wang stats->bytes += len; 1022680557cfSMichael S. Tsirkin page = virt_to_head_page(buf); 10238fc3b9e9SMichael S. Tsirkin put_page(page); 10243f2c31d9SMark McLoughlin } 10258fc3b9e9SMichael S. Tsirkin err_buf: 1026d46eeeafSJason Wang stats->drops++; 10278fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 102856434a01SJohn Fastabend xdp_xmit: 10298fc3b9e9SMichael S. Tsirkin return NULL; 10309ab86bbcSShirley Ma } 10319ab86bbcSShirley Ma 10327d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 10332471c75eSJesper Dangaard Brouer void *buf, unsigned int len, void **ctx, 1034a0929a44SToshiaki Makita unsigned int *xdp_xmit, 1035d46eeeafSJason Wang struct virtnet_rq_stats *stats) 10369ab86bbcSShirley Ma { 1037e9d7417bSJason Wang struct net_device *dev = vi->dev; 10389ab86bbcSShirley Ma struct sk_buff *skb; 1039012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 10409ab86bbcSShirley Ma 1041bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 10429ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 10439ab86bbcSShirley Ma dev->stats.rx_length_errors++; 1044ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 1045680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 1046ab7db917SMichael Dalton } else if (vi->big_packets) { 104798bfd23cSMichael Dalton give_pages(rq, buf); 1048ab7db917SMichael Dalton } else { 1049f6b10209SJason Wang put_page(virt_to_head_page(buf)); 1050ab7db917SMichael Dalton } 10517d9d60fdSToshiaki Makita return; 10529ab86bbcSShirley Ma } 10539ab86bbcSShirley Ma 1054f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 10557d9d60fdSToshiaki Makita skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, 1056a0929a44SToshiaki Makita stats); 1057f121159dSMichael S. Tsirkin else if (vi->big_packets) 1058a0929a44SToshiaki Makita skb = receive_big(dev, vi, rq, buf, len, stats); 1059f121159dSMichael S. Tsirkin else 1060a0929a44SToshiaki Makita skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); 1061f121159dSMichael S. Tsirkin 10628fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 10637d9d60fdSToshiaki Makita return; 10643f2c31d9SMark McLoughlin 10659ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 10663fa2a1dfSstephen hemminger 1067e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 106810a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 1069296f96fcSRusty Russell 1070e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 1071e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 1072e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 1073e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 1074fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 1075296f96fcSRusty Russell goto frame_err; 1076296f96fcSRusty Russell } 1077296f96fcSRusty Russell 1078133bbb18SWillem de Bruijn skb_record_rx_queue(skb, vq2rxq(rq->vq)); 1079d1dc06dcSMike Rapoport skb->protocol = eth_type_trans(skb, dev); 1080d1dc06dcSMike Rapoport pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 1081d1dc06dcSMike Rapoport ntohs(skb->protocol), skb->len, skb->pkt_type); 1082d1dc06dcSMike Rapoport 10830fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 10847d9d60fdSToshiaki Makita return; 1085296f96fcSRusty Russell 1086296f96fcSRusty Russell frame_err: 1087296f96fcSRusty Russell dev->stats.rx_frame_errors++; 1088296f96fcSRusty Russell dev_kfree_skb(skb); 1089296f96fcSRusty Russell } 1090296f96fcSRusty Russell 1091192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the 1092192f68cfSJason Wang * same size, except for the headroom. For this reason we do 1093192f68cfSJason Wang * not need to use mergeable_len_to_ctx here - it is enough 1094192f68cfSJason Wang * to store the headroom as the context ignoring the truesize. 1095192f68cfSJason Wang */ 1096946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 1097946fa564SMichael S. Tsirkin gfp_t gfp) 1098296f96fcSRusty Russell { 1099f6b10209SJason Wang struct page_frag *alloc_frag = &rq->alloc_frag; 1100f6b10209SJason Wang char *buf; 11012de2f7f4SJohn Fastabend unsigned int xdp_headroom = virtnet_get_headroom(vi); 1102192f68cfSJason Wang void *ctx = (void *)(unsigned long)xdp_headroom; 1103f6b10209SJason Wang int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 11049ab86bbcSShirley Ma int err; 11053f2c31d9SMark McLoughlin 1106f6b10209SJason Wang len = SKB_DATA_ALIGN(len) + 1107f6b10209SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1108f6b10209SJason Wang if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 11099ab86bbcSShirley Ma return -ENOMEM; 1110296f96fcSRusty Russell 1111f6b10209SJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1112f6b10209SJason Wang get_page(alloc_frag->page); 1113f6b10209SJason Wang alloc_frag->offset += len; 1114f6b10209SJason Wang sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 1115f6b10209SJason Wang vi->hdr_len + GOOD_PACKET_LEN); 1116192f68cfSJason Wang err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 11179ab86bbcSShirley Ma if (err < 0) 1118f6b10209SJason Wang put_page(virt_to_head_page(buf)); 11199ab86bbcSShirley Ma return err; 112097402b96SHerbert Xu } 112197402b96SHerbert Xu 1122012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 1123012873d0SMichael S. Tsirkin gfp_t gfp) 11249ab86bbcSShirley Ma { 11259ab86bbcSShirley Ma struct page *first, *list = NULL; 11269ab86bbcSShirley Ma char *p; 11279ab86bbcSShirley Ma int i, err, offset; 1128296f96fcSRusty Russell 1129a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 1130a5835440SRusty Russell 1131e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 11329ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 1133e9d7417bSJason Wang first = get_a_page(rq, gfp); 11349ab86bbcSShirley Ma if (!first) { 11359ab86bbcSShirley Ma if (list) 1136e9d7417bSJason Wang give_pages(rq, list); 11379ab86bbcSShirley Ma return -ENOMEM; 1138296f96fcSRusty Russell } 1139e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 11409ab86bbcSShirley Ma 11419ab86bbcSShirley Ma /* chain new page in list head to match sg */ 11429ab86bbcSShirley Ma first->private = (unsigned long)list; 11439ab86bbcSShirley Ma list = first; 11449ab86bbcSShirley Ma } 11459ab86bbcSShirley Ma 1146e9d7417bSJason Wang first = get_a_page(rq, gfp); 11479ab86bbcSShirley Ma if (!first) { 1148e9d7417bSJason Wang give_pages(rq, list); 11499ab86bbcSShirley Ma return -ENOMEM; 11509ab86bbcSShirley Ma } 11519ab86bbcSShirley Ma p = page_address(first); 11529ab86bbcSShirley Ma 1153e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 1154012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 1155012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 11569ab86bbcSShirley Ma 1157e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 11589ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 1159e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 11609ab86bbcSShirley Ma 11619ab86bbcSShirley Ma /* chain first in list head */ 11629ab86bbcSShirley Ma first->private = (unsigned long)list; 11639dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 1164aa989f5eSMichael S. Tsirkin first, gfp); 11659ab86bbcSShirley Ma if (err < 0) 1166e9d7417bSJason Wang give_pages(rq, first); 11679ab86bbcSShirley Ma 11689ab86bbcSShirley Ma return err; 11699ab86bbcSShirley Ma } 11709ab86bbcSShirley Ma 1171d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 11723cc81a9aSJason Wang struct ewma_pkt_len *avg_pkt_len, 11733cc81a9aSJason Wang unsigned int room) 11749ab86bbcSShirley Ma { 1175ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1176fbf28d78SMichael Dalton unsigned int len; 1177fbf28d78SMichael Dalton 11783cc81a9aSJason Wang if (room) 11793cc81a9aSJason Wang return PAGE_SIZE - room; 11803cc81a9aSJason Wang 11815377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 1182f0c3192cSMichael S. Tsirkin rq->min_buf_len, PAGE_SIZE - hdr_len); 11833cc81a9aSJason Wang 1184e377fcc8SMichael S. Tsirkin return ALIGN(len, L1_CACHE_BYTES); 1185fbf28d78SMichael Dalton } 1186fbf28d78SMichael Dalton 11872de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi, 11882de2f7f4SJohn Fastabend struct receive_queue *rq, gfp_t gfp) 1189fbf28d78SMichael Dalton { 1190fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 11912de2f7f4SJohn Fastabend unsigned int headroom = virtnet_get_headroom(vi); 11923cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 11933cc81a9aSJason Wang unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1194fb51879dSMichael Dalton char *buf; 1195680557cfSMichael S. Tsirkin void *ctx; 11969ab86bbcSShirley Ma int err; 1197fb51879dSMichael Dalton unsigned int len, hole; 11989ab86bbcSShirley Ma 11993cc81a9aSJason Wang /* Extra tailroom is needed to satisfy XDP's assumption. This 12003cc81a9aSJason Wang * means rx frags coalescing won't work, but consider we've 12013cc81a9aSJason Wang * disabled GSO for XDP, it won't be a big issue. 12023cc81a9aSJason Wang */ 12033cc81a9aSJason Wang len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 12043cc81a9aSJason Wang if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp))) 12059ab86bbcSShirley Ma return -ENOMEM; 1206ab7db917SMichael Dalton 1207fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 12082de2f7f4SJohn Fastabend buf += headroom; /* advance address leaving hole at front of pkt */ 1209fb51879dSMichael Dalton get_page(alloc_frag->page); 12103cc81a9aSJason Wang alloc_frag->offset += len + room; 1211fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 12123cc81a9aSJason Wang if (hole < len + room) { 1213ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 1214ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 12151daa8790SMichael S. Tsirkin * the current buffer. 1216ab7db917SMichael Dalton */ 1217fb51879dSMichael Dalton len += hole; 1218fb51879dSMichael Dalton alloc_frag->offset += hole; 1219fb51879dSMichael Dalton } 12209ab86bbcSShirley Ma 1221fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 122229fda25aSDavid S. Miller ctx = mergeable_len_to_ctx(len, headroom); 1223680557cfSMichael S. Tsirkin err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 12249ab86bbcSShirley Ma if (err < 0) 12252613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 12269ab86bbcSShirley Ma 12279ab86bbcSShirley Ma return err; 1228296f96fcSRusty Russell } 1229296f96fcSRusty Russell 1230b2baed69SRusty Russell /* 1231b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 1232b2baed69SRusty Russell * 1233b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 1234b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 1235b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 1236b2baed69SRusty Russell */ 1237946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 1238946fa564SMichael S. Tsirkin gfp_t gfp) 12393f2c31d9SMark McLoughlin { 12403f2c31d9SMark McLoughlin int err; 12411788f495SMichael S. Tsirkin bool oom; 12423f2c31d9SMark McLoughlin 12430aea51c3SAmit Shah do { 12449ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 12452de2f7f4SJohn Fastabend err = add_recvbuf_mergeable(vi, rq, gfp); 12469ab86bbcSShirley Ma else if (vi->big_packets) 1247012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 12489ab86bbcSShirley Ma else 1249946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 12503f2c31d9SMark McLoughlin 12511788f495SMichael S. Tsirkin oom = err == -ENOMEM; 12529ed4cb07SRusty Russell if (err) 12533f2c31d9SMark McLoughlin break; 1254b7dfde95SLinus Torvalds } while (rq->vq->num_free); 1255461f03dcSToshiaki Makita if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) { 125601c32598SMichael S. Tsirkin unsigned long flags; 125701c32598SMichael S. Tsirkin 125801c32598SMichael S. Tsirkin flags = u64_stats_update_begin_irqsave(&rq->stats.syncp); 1259d46eeeafSJason Wang rq->stats.kicks++; 126001c32598SMichael S. Tsirkin u64_stats_update_end_irqrestore(&rq->stats.syncp, flags); 1261461f03dcSToshiaki Makita } 1262461f03dcSToshiaki Makita 12633161e453SRusty Russell return !oom; 12643f2c31d9SMark McLoughlin } 12653f2c31d9SMark McLoughlin 126618445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 1267296f96fcSRusty Russell { 1268296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 1269986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1270e9d7417bSJason Wang 1271e4e8452aSWillem de Bruijn virtqueue_napi_schedule(&rq->napi, rvq); 1272296f96fcSRusty Russell } 1273296f96fcSRusty Russell 1274e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 12753e9d08ecSBruce Rogers { 1276e4e8452aSWillem de Bruijn napi_enable(napi); 12773e9d08ecSBruce Rogers 12783e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 1279e4e8452aSWillem de Bruijn * won't get another interrupt, so process any outstanding packets now. 1280e4e8452aSWillem de Bruijn * Call local_bh_enable after to trigger softIRQ processing. 1281e4e8452aSWillem de Bruijn */ 1282ec13ee80SMichael S. Tsirkin local_bh_disable(); 1283e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 1284ec13ee80SMichael S. Tsirkin local_bh_enable(); 12853e9d08ecSBruce Rogers } 12863e9d08ecSBruce Rogers 1287b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1288b92f1e67SWillem de Bruijn struct virtqueue *vq, 1289b92f1e67SWillem de Bruijn struct napi_struct *napi) 1290b92f1e67SWillem de Bruijn { 1291b92f1e67SWillem de Bruijn if (!napi->weight) 1292b92f1e67SWillem de Bruijn return; 1293b92f1e67SWillem de Bruijn 1294b92f1e67SWillem de Bruijn /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1295b92f1e67SWillem de Bruijn * enable the feature if this is likely affine with the transmit path. 1296b92f1e67SWillem de Bruijn */ 1297b92f1e67SWillem de Bruijn if (!vi->affinity_hint_set) { 1298b92f1e67SWillem de Bruijn napi->weight = 0; 1299b92f1e67SWillem de Bruijn return; 1300b92f1e67SWillem de Bruijn } 1301b92f1e67SWillem de Bruijn 1302b92f1e67SWillem de Bruijn return virtnet_napi_enable(vq, napi); 1303b92f1e67SWillem de Bruijn } 1304b92f1e67SWillem de Bruijn 130578a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi) 130678a57b48SWillem de Bruijn { 130778a57b48SWillem de Bruijn if (napi->weight) 130878a57b48SWillem de Bruijn napi_disable(napi); 130978a57b48SWillem de Bruijn } 131078a57b48SWillem de Bruijn 13113161e453SRusty Russell static void refill_work(struct work_struct *work) 13123161e453SRusty Russell { 1313e9d7417bSJason Wang struct virtnet_info *vi = 1314e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 13153161e453SRusty Russell bool still_empty; 1316986a4f4dSJason Wang int i; 13173161e453SRusty Russell 131855257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1319986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 1320986a4f4dSJason Wang 1321986a4f4dSJason Wang napi_disable(&rq->napi); 1322946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1323e4e8452aSWillem de Bruijn virtnet_napi_enable(rq->vq, &rq->napi); 13243161e453SRusty Russell 13253161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 1326986a4f4dSJason Wang * we will *never* try to fill again. 1327986a4f4dSJason Wang */ 13283161e453SRusty Russell if (still_empty) 13293b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 13303161e453SRusty Russell } 1331986a4f4dSJason Wang } 13323161e453SRusty Russell 13332471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget, 13342471c75eSJesper Dangaard Brouer unsigned int *xdp_xmit) 1335296f96fcSRusty Russell { 1336e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 1337d46eeeafSJason Wang struct virtnet_rq_stats stats = {}; 1338a0929a44SToshiaki Makita unsigned int len; 13399ab86bbcSShirley Ma void *buf; 1340a0929a44SToshiaki Makita int i; 1341296f96fcSRusty Russell 1342192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 1343680557cfSMichael S. Tsirkin void *ctx; 1344680557cfSMichael S. Tsirkin 1345d46eeeafSJason Wang while (stats.packets < budget && 1346680557cfSMichael S. Tsirkin (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1347a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); 1348d46eeeafSJason Wang stats.packets++; 1349680557cfSMichael S. Tsirkin } 1350680557cfSMichael S. Tsirkin } else { 1351d46eeeafSJason Wang while (stats.packets < budget && 1352e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1353a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); 1354d46eeeafSJason Wang stats.packets++; 1355296f96fcSRusty Russell } 1356680557cfSMichael S. Tsirkin } 1357296f96fcSRusty Russell 1358718be6baS? jiang if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { 1359946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 13603b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 13613161e453SRusty Russell } 1362296f96fcSRusty Russell 1363d7dfc5cfSToshiaki Makita u64_stats_update_begin(&rq->stats.syncp); 1364a0929a44SToshiaki Makita for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) { 1365a0929a44SToshiaki Makita size_t offset = virtnet_rq_stats_desc[i].offset; 1366a0929a44SToshiaki Makita u64 *item; 1367a0929a44SToshiaki Makita 1368d46eeeafSJason Wang item = (u64 *)((u8 *)&rq->stats + offset); 1369d46eeeafSJason Wang *item += *(u64 *)((u8 *)&stats + offset); 1370a0929a44SToshiaki Makita } 1371d7dfc5cfSToshiaki Makita u64_stats_update_end(&rq->stats.syncp); 137261845d20SJason Wang 1373d46eeeafSJason Wang return stats.packets; 13742ffa7598SJason Wang } 13752ffa7598SJason Wang 1376df133f3fSMichael S. Tsirkin static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi) 1377ea7735d9SWillem de Bruijn { 1378ea7735d9SWillem de Bruijn unsigned int len; 1379ea7735d9SWillem de Bruijn unsigned int packets = 0; 1380ea7735d9SWillem de Bruijn unsigned int bytes = 0; 13815050471dSToshiaki Makita void *ptr; 1382ea7735d9SWillem de Bruijn 13835050471dSToshiaki Makita while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 13845050471dSToshiaki Makita if (likely(!is_xdp_frame(ptr))) { 13855050471dSToshiaki Makita struct sk_buff *skb = ptr; 13865050471dSToshiaki Makita 1387ea7735d9SWillem de Bruijn pr_debug("Sent skb %p\n", skb); 1388ea7735d9SWillem de Bruijn 1389ea7735d9SWillem de Bruijn bytes += skb->len; 1390df133f3fSMichael S. Tsirkin napi_consume_skb(skb, in_napi); 13915050471dSToshiaki Makita } else { 13925050471dSToshiaki Makita struct xdp_frame *frame = ptr_to_xdp(ptr); 13935050471dSToshiaki Makita 13945050471dSToshiaki Makita bytes += frame->len; 13955050471dSToshiaki Makita xdp_return_frame(frame); 13965050471dSToshiaki Makita } 13975050471dSToshiaki Makita packets++; 1398ea7735d9SWillem de Bruijn } 1399ea7735d9SWillem de Bruijn 1400ea7735d9SWillem de Bruijn /* Avoid overhead when no packets have been processed 1401ea7735d9SWillem de Bruijn * happens when called speculatively from start_xmit. 1402ea7735d9SWillem de Bruijn */ 1403ea7735d9SWillem de Bruijn if (!packets) 1404ea7735d9SWillem de Bruijn return; 1405ea7735d9SWillem de Bruijn 1406d7dfc5cfSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1407d7dfc5cfSToshiaki Makita sq->stats.bytes += bytes; 1408d7dfc5cfSToshiaki Makita sq->stats.packets += packets; 1409d7dfc5cfSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1410ea7735d9SWillem de Bruijn } 1411ea7735d9SWillem de Bruijn 1412534da5e8SToshiaki Makita static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 1413534da5e8SToshiaki Makita { 1414534da5e8SToshiaki Makita if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 1415534da5e8SToshiaki Makita return false; 1416534da5e8SToshiaki Makita else if (q < vi->curr_queue_pairs) 1417534da5e8SToshiaki Makita return true; 1418534da5e8SToshiaki Makita else 1419534da5e8SToshiaki Makita return false; 1420534da5e8SToshiaki Makita } 1421534da5e8SToshiaki Makita 14227b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq) 14237b0411efSWillem de Bruijn { 14247b0411efSWillem de Bruijn struct virtnet_info *vi = rq->vq->vdev->priv; 14257b0411efSWillem de Bruijn unsigned int index = vq2rxq(rq->vq); 14267b0411efSWillem de Bruijn struct send_queue *sq = &vi->sq[index]; 14277b0411efSWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 14287b0411efSWillem de Bruijn 1429534da5e8SToshiaki Makita if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) 14307b0411efSWillem de Bruijn return; 14317b0411efSWillem de Bruijn 14327b0411efSWillem de Bruijn if (__netif_tx_trylock(txq)) { 1433df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, true); 14347b0411efSWillem de Bruijn __netif_tx_unlock(txq); 14357b0411efSWillem de Bruijn } 14367b0411efSWillem de Bruijn 14377b0411efSWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 14387b0411efSWillem de Bruijn netif_tx_wake_queue(txq); 14397b0411efSWillem de Bruijn } 14407b0411efSWillem de Bruijn 14412ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 14422ffa7598SJason Wang { 14432ffa7598SJason Wang struct receive_queue *rq = 14442ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 14459267c430SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 14469267c430SJason Wang struct send_queue *sq; 14472a43565cSToshiaki Makita unsigned int received; 14482471c75eSJesper Dangaard Brouer unsigned int xdp_xmit = 0; 14492ffa7598SJason Wang 14507b0411efSWillem de Bruijn virtnet_poll_cleantx(rq); 14517b0411efSWillem de Bruijn 1452186b3c99SJason Wang received = virtnet_receive(rq, budget, &xdp_xmit); 14532ffa7598SJason Wang 14548329d98eSRusty Russell /* Out of packets? */ 1455e4e8452aSWillem de Bruijn if (received < budget) 1456e4e8452aSWillem de Bruijn virtqueue_napi_complete(napi, rq->vq, received); 1457296f96fcSRusty Russell 14582471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_REDIR) 14591d233886SToke Høiland-Jørgensen xdp_do_flush(); 14602471c75eSJesper Dangaard Brouer 14612471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_TX) { 14622a43565cSToshiaki Makita sq = virtnet_xdp_sq(vi); 1463461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1464461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1465461f03dcSToshiaki Makita sq->stats.kicks++; 1466461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1467461f03dcSToshiaki Makita } 14689267c430SJason Wang } 1469186b3c99SJason Wang 1470296f96fcSRusty Russell return received; 1471296f96fcSRusty Russell } 1472296f96fcSRusty Russell 1473986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 1474986a4f4dSJason Wang { 1475986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1476754b8a21SJesper Dangaard Brouer int i, err; 1477986a4f4dSJason Wang 1478e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1479e4166625SJason Wang if (i < vi->curr_queue_pairs) 1480986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 1481946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1482986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 1483754b8a21SJesper Dangaard Brouer 1484b02e5a0eSBjörn Töpel err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); 1485754b8a21SJesper Dangaard Brouer if (err < 0) 1486754b8a21SJesper Dangaard Brouer return err; 1487754b8a21SJesper Dangaard Brouer 14888d5d8852SJesper Dangaard Brouer err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq, 14898d5d8852SJesper Dangaard Brouer MEM_TYPE_PAGE_SHARED, NULL); 14908d5d8852SJesper Dangaard Brouer if (err < 0) { 14918d5d8852SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 14928d5d8852SJesper Dangaard Brouer return err; 14938d5d8852SJesper Dangaard Brouer } 14948d5d8852SJesper Dangaard Brouer 1495e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1496b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1497986a4f4dSJason Wang } 1498986a4f4dSJason Wang 1499986a4f4dSJason Wang return 0; 1500986a4f4dSJason Wang } 1501986a4f4dSJason Wang 1502b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1503b92f1e67SWillem de Bruijn { 1504b92f1e67SWillem de Bruijn struct send_queue *sq = container_of(napi, struct send_queue, napi); 1505b92f1e67SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1506534da5e8SToshiaki Makita unsigned int index = vq2txq(sq->vq); 1507534da5e8SToshiaki Makita struct netdev_queue *txq; 1508b92f1e67SWillem de Bruijn 1509534da5e8SToshiaki Makita if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { 1510534da5e8SToshiaki Makita /* We don't need to enable cb for XDP */ 1511534da5e8SToshiaki Makita napi_complete_done(napi, 0); 1512534da5e8SToshiaki Makita return 0; 1513534da5e8SToshiaki Makita } 1514534da5e8SToshiaki Makita 1515534da5e8SToshiaki Makita txq = netdev_get_tx_queue(vi->dev, index); 1516b92f1e67SWillem de Bruijn __netif_tx_lock(txq, raw_smp_processor_id()); 1517df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, true); 1518b92f1e67SWillem de Bruijn __netif_tx_unlock(txq); 1519b92f1e67SWillem de Bruijn 1520b92f1e67SWillem de Bruijn virtqueue_napi_complete(napi, sq->vq, 0); 1521b92f1e67SWillem de Bruijn 1522b92f1e67SWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1523b92f1e67SWillem de Bruijn netif_tx_wake_queue(txq); 1524b92f1e67SWillem de Bruijn 1525b92f1e67SWillem de Bruijn return 0; 1526b92f1e67SWillem de Bruijn } 1527b92f1e67SWillem de Bruijn 1528e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1529296f96fcSRusty Russell { 1530012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 1531296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1532e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 1533e2fcad58SJason A. Donenfeld int num_sg; 1534012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 1535e7428e95SMichael S. Tsirkin bool can_push; 1536296f96fcSRusty Russell 1537e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1538e7428e95SMichael S. Tsirkin 1539e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 1540e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1541e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1542e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 1543e7428e95SMichael S. Tsirkin * csum_start offset below. */ 1544e7428e95SMichael S. Tsirkin if (can_push) 1545012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1546e7428e95SMichael S. Tsirkin else 1547e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 1548296f96fcSRusty Russell 1549e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 1550fd3a8862SWillem de Bruijn virtio_is_little_endian(vi->vdev), false, 1551fd3a8862SWillem de Bruijn 0)) 1552296f96fcSRusty Russell BUG(); 1553296f96fcSRusty Russell 1554e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 1555012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 15563f2c31d9SMark McLoughlin 1557547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1558e7428e95SMichael S. Tsirkin if (can_push) { 1559e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 1560e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1561e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1562e2fcad58SJason A. Donenfeld return num_sg; 1563e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 1564e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 1565e7428e95SMichael S. Tsirkin } else { 1566e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 1567e2fcad58SJason A. Donenfeld num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1568e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1569e2fcad58SJason A. Donenfeld return num_sg; 1570e2fcad58SJason A. Donenfeld num_sg++; 1571e7428e95SMichael S. Tsirkin } 15729dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 157311a3a154SRusty Russell } 157411a3a154SRusty Russell 1575424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 157699ffc696SRusty Russell { 157799ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1578986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 1579986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 15809ed4cb07SRusty Russell int err; 15814b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 15826b16f9eeSFlorian Westphal bool kick = !netdev_xmit_more(); 1583b92f1e67SWillem de Bruijn bool use_napi = sq->napi.weight; 15842cb9c6baSRusty Russell 15852cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 1586df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, false); 158799ffc696SRusty Russell 1588bdb12e0dSWillem de Bruijn if (use_napi && kick) 1589bdb12e0dSWillem de Bruijn virtqueue_enable_cb_delayed(sq->vq); 1590bdb12e0dSWillem de Bruijn 1591074c3582SJacob Keller /* timestamp packet in software */ 1592074c3582SJacob Keller skb_tx_timestamp(skb); 1593074c3582SJacob Keller 159403f191baSMichael S. Tsirkin /* Try to transmit */ 1595b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 159699ffc696SRusty Russell 15979ed4cb07SRusty Russell /* This should not happen! */ 1598681daee2SJason Wang if (unlikely(err)) { 159958eba97dSRusty Russell dev->stats.tx_fifo_errors++; 16002e57b79cSRick Jones if (net_ratelimit()) 160158eba97dSRusty Russell dev_warn(&dev->dev, 16027934b481SYuval Shaia "Unexpected TXQ (%d) queue failure: %d\n", 16037934b481SYuval Shaia qnum, err); 160458eba97dSRusty Russell dev->stats.tx_dropped++; 160585e94525SEric W. Biederman dev_kfree_skb_any(skb); 160658eba97dSRusty Russell return NETDEV_TX_OK; 1607296f96fcSRusty Russell } 160803f191baSMichael S. Tsirkin 160948925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 1610b92f1e67SWillem de Bruijn if (!use_napi) { 161148925e37SRusty Russell skb_orphan(skb); 1612895b5c9fSFlorian Westphal nf_reset_ct(skb); 1613b92f1e67SWillem de Bruijn } 161448925e37SRusty Russell 161560302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 161660302ff6SMichael S. Tsirkin * are then unable to transmit. 161760302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 161860302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 161960302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 162060302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 162160302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 162260302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 162360302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 1624d631b94eSstephen hemminger */ 1625b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1626986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 1627b92f1e67SWillem de Bruijn if (!use_napi && 1628b92f1e67SWillem de Bruijn unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 162948925e37SRusty Russell /* More just got used, free them then recheck. */ 1630df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, false); 1631b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1632986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 1633e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 163448925e37SRusty Russell } 163548925e37SRusty Russell } 163648925e37SRusty Russell } 163748925e37SRusty Russell 1638461f03dcSToshiaki Makita if (kick || netif_xmit_stopped(txq)) { 1639461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1640461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1641461f03dcSToshiaki Makita sq->stats.kicks++; 1642461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1643461f03dcSToshiaki Makita } 1644461f03dcSToshiaki Makita } 16450b725a2cSDavid S. Miller 16460b725a2cSDavid S. Miller return NETDEV_TX_OK; 1647c223a078SDavid S. Miller } 1648c223a078SDavid S. Miller 164940cbfc37SAmos Kong /* 165040cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 165140cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 1652788a8b6dSstephen hemminger * never fail unless improperly formatted. 165340cbfc37SAmos Kong */ 165440cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 1655d24bae32Sstephen hemminger struct scatterlist *out) 165640cbfc37SAmos Kong { 1657f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 1658d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 165940cbfc37SAmos Kong 166040cbfc37SAmos Kong /* Caller should know better */ 1661f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 166240cbfc37SAmos Kong 166312e57169SMichael S. Tsirkin vi->ctrl->status = ~0; 166412e57169SMichael S. Tsirkin vi->ctrl->hdr.class = class; 166512e57169SMichael S. Tsirkin vi->ctrl->hdr.cmd = cmd; 1666f7bc9594SRusty Russell /* Add header */ 166712e57169SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 1668f7bc9594SRusty Russell sgs[out_num++] = &hdr; 166940cbfc37SAmos Kong 1670f7bc9594SRusty Russell if (out) 1671f7bc9594SRusty Russell sgs[out_num++] = out; 167240cbfc37SAmos Kong 1673f7bc9594SRusty Russell /* Add return status. */ 167412e57169SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 1675d24bae32Sstephen hemminger sgs[out_num] = &stat; 167640cbfc37SAmos Kong 1677d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1678a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 167940cbfc37SAmos Kong 168067975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 168112e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 168240cbfc37SAmos Kong 168340cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 168440cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 168540cbfc37SAmos Kong */ 1686047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 1687047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 168840cbfc37SAmos Kong cpu_relax(); 168940cbfc37SAmos Kong 169012e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 169140cbfc37SAmos Kong } 169240cbfc37SAmos Kong 16939c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 16949c46f6d4SAlex Williamson { 16959c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 16969c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 1697f2f2c8b4SJiri Pirko int ret; 1698e37e2ff3SAndy Lutomirski struct sockaddr *addr; 16997e58d5aeSAmos Kong struct scatterlist sg; 17009c46f6d4SAlex Williamson 1701ba5e4426SSridhar Samudrala if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 1702ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 1703ba5e4426SSridhar Samudrala 1704801822d1SShyam Saini addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 1705e37e2ff3SAndy Lutomirski if (!addr) 1706e37e2ff3SAndy Lutomirski return -ENOMEM; 1707e37e2ff3SAndy Lutomirski 1708e37e2ff3SAndy Lutomirski ret = eth_prepare_mac_addr_change(dev, addr); 1709f2f2c8b4SJiri Pirko if (ret) 1710e37e2ff3SAndy Lutomirski goto out; 17119c46f6d4SAlex Williamson 17127e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 17137e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 17147e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1715d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 17167e58d5aeSAmos Kong dev_warn(&vdev->dev, 17177e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 1718e37e2ff3SAndy Lutomirski ret = -EINVAL; 1719e37e2ff3SAndy Lutomirski goto out; 17207e58d5aeSAmos Kong } 17217e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 17227e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1723855e0c52SRusty Russell unsigned int i; 1724855e0c52SRusty Russell 1725855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 1726855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 1727855e0c52SRusty Russell virtio_cwrite8(vdev, 1728855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1729855e0c52SRusty Russell i, addr->sa_data[i]); 17307e58d5aeSAmos Kong } 17317e58d5aeSAmos Kong 17327e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 1733e37e2ff3SAndy Lutomirski ret = 0; 17349c46f6d4SAlex Williamson 1735e37e2ff3SAndy Lutomirski out: 1736e37e2ff3SAndy Lutomirski kfree(addr); 1737e37e2ff3SAndy Lutomirski return ret; 17389c46f6d4SAlex Williamson } 17399c46f6d4SAlex Williamson 1740bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev, 17413fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 17423fa2a1dfSstephen hemminger { 17433fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 17443fa2a1dfSstephen hemminger unsigned int start; 1745d7dfc5cfSToshiaki Makita int i; 17463fa2a1dfSstephen hemminger 1747d7dfc5cfSToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 17482c4a2f7dSToshiaki Makita u64 tpackets, tbytes, rpackets, rbytes, rdrops; 1749d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 1750d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 17513fa2a1dfSstephen hemminger 17523fa2a1dfSstephen hemminger do { 1753d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 1754d7dfc5cfSToshiaki Makita tpackets = sq->stats.packets; 1755d7dfc5cfSToshiaki Makita tbytes = sq->stats.bytes; 1756d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 175783a27052SEric Dumazet 175883a27052SEric Dumazet do { 1759d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 1760d46eeeafSJason Wang rpackets = rq->stats.packets; 1761d46eeeafSJason Wang rbytes = rq->stats.bytes; 1762d46eeeafSJason Wang rdrops = rq->stats.drops; 1763d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 17643fa2a1dfSstephen hemminger 17653fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 17663fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 17673fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 17683fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 17692c4a2f7dSToshiaki Makita tot->rx_dropped += rdrops; 17703fa2a1dfSstephen hemminger } 17713fa2a1dfSstephen hemminger 17723fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1773021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 17743fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 17753fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 17763fa2a1dfSstephen hemminger } 17773fa2a1dfSstephen hemminger 1778586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1779586d17c5SJason Wang { 1780586d17c5SJason Wang rtnl_lock(); 1781586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1782d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1783586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1784586d17c5SJason Wang rtnl_unlock(); 1785586d17c5SJason Wang } 1786586d17c5SJason Wang 178747315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1788986a4f4dSJason Wang { 1789986a4f4dSJason Wang struct scatterlist sg; 1790986a4f4dSJason Wang struct net_device *dev = vi->dev; 1791986a4f4dSJason Wang 1792986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1793986a4f4dSJason Wang return 0; 1794986a4f4dSJason Wang 179512e57169SMichael S. Tsirkin vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 179612e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); 1797986a4f4dSJason Wang 1798986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1799d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1800986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1801986a4f4dSJason Wang queue_pairs); 1802986a4f4dSJason Wang return -EINVAL; 180355257d72SSasha Levin } else { 1804986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 180535ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 180635ed159bSJason Wang if (dev->flags & IFF_UP) 18079b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 180855257d72SSasha Levin } 1809986a4f4dSJason Wang 1810986a4f4dSJason Wang return 0; 1811986a4f4dSJason Wang } 1812986a4f4dSJason Wang 181347315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 181447315329SJohn Fastabend { 181547315329SJohn Fastabend int err; 181647315329SJohn Fastabend 181747315329SJohn Fastabend rtnl_lock(); 181847315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 181947315329SJohn Fastabend rtnl_unlock(); 182047315329SJohn Fastabend return err; 182147315329SJohn Fastabend } 182247315329SJohn Fastabend 1823296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1824296f96fcSRusty Russell { 1825296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1826986a4f4dSJason Wang int i; 1827296f96fcSRusty Russell 1828b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1829b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1830986a4f4dSJason Wang 1831b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 1832754b8a21SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 1833986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 183478a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 1835b92f1e67SWillem de Bruijn } 1836296f96fcSRusty Russell 1837296f96fcSRusty Russell return 0; 1838296f96fcSRusty Russell } 1839296f96fcSRusty Russell 18402af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 18412af7698eSAlex Williamson { 18422af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1843f565a7c2SAlex Williamson struct scatterlist sg[2]; 1844f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1845ccffad25SJiri Pirko struct netdev_hw_addr *ha; 184632e7bfc4SJiri Pirko int uc_count; 18474cd24eafSJiri Pirko int mc_count; 1848f565a7c2SAlex Williamson void *buf; 1849f565a7c2SAlex Williamson int i; 18502af7698eSAlex Williamson 1851788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 18522af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 18532af7698eSAlex Williamson return; 18542af7698eSAlex Williamson 185512e57169SMichael S. Tsirkin vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); 185612e57169SMichael S. Tsirkin vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 18572af7698eSAlex Williamson 185812e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); 18592af7698eSAlex Williamson 18602af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1861d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 18622af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 186312e57169SMichael S. Tsirkin vi->ctrl->promisc ? "en" : "dis"); 18642af7698eSAlex Williamson 186512e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); 18662af7698eSAlex Williamson 18672af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1868d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 18692af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 187012e57169SMichael S. Tsirkin vi->ctrl->allmulti ? "en" : "dis"); 1871f565a7c2SAlex Williamson 187232e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 18734cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1874f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 18754cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1876f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 18774cd24eafSJiri Pirko mac_data = buf; 1878e68ed8f0SJoe Perches if (!buf) 1879f565a7c2SAlex Williamson return; 1880f565a7c2SAlex Williamson 188123e258e1SAlex Williamson sg_init_table(sg, 2); 188223e258e1SAlex Williamson 1883f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 1884fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1885ccffad25SJiri Pirko i = 0; 188632e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1887ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1888f565a7c2SAlex Williamson 1889f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 189032e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1891f565a7c2SAlex Williamson 1892f565a7c2SAlex Williamson /* multicast list and count fill the end */ 189332e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1894f565a7c2SAlex Williamson 1895fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1896567ec874SJiri Pirko i = 0; 189722bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 189822bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1899f565a7c2SAlex Williamson 1900f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 19014cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1902f565a7c2SAlex Williamson 1903f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1904d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 190599e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1906f565a7c2SAlex Williamson 1907f565a7c2SAlex Williamson kfree(buf); 19082af7698eSAlex Williamson } 19092af7698eSAlex Williamson 191080d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 191180d5c368SPatrick McHardy __be16 proto, u16 vid) 19120bde9569SAlex Williamson { 19130bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 19140bde9569SAlex Williamson struct scatterlist sg; 19150bde9569SAlex Williamson 1916d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 191712e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 19180bde9569SAlex Williamson 19190bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1920d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 19210bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 19228e586137SJiri Pirko return 0; 19230bde9569SAlex Williamson } 19240bde9569SAlex Williamson 192580d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 192680d5c368SPatrick McHardy __be16 proto, u16 vid) 19270bde9569SAlex Williamson { 19280bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 19290bde9569SAlex Williamson struct scatterlist sg; 19300bde9569SAlex Williamson 1931d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 193212e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 19330bde9569SAlex Williamson 19340bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1935d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 19360bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 19378e586137SJiri Pirko return 0; 19380bde9569SAlex Williamson } 19390bde9569SAlex Williamson 1940310974faSPeter Xu static void virtnet_clean_affinity(struct virtnet_info *vi) 1941986a4f4dSJason Wang { 1942986a4f4dSJason Wang int i; 19438898c21cSWanlong Gao 19448898c21cSWanlong Gao if (vi->affinity_hint_set) { 19458898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 194619e226e8SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, NULL); 194719e226e8SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, NULL); 19488898c21cSWanlong Gao } 19498898c21cSWanlong Gao 19508898c21cSWanlong Gao vi->affinity_hint_set = false; 19518898c21cSWanlong Gao } 19528898c21cSWanlong Gao } 19538898c21cSWanlong Gao 19548898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1955986a4f4dSJason Wang { 19562ca653d6SCaleb Raitto cpumask_var_t mask; 19572ca653d6SCaleb Raitto int stragglers; 19582ca653d6SCaleb Raitto int group_size; 19592ca653d6SCaleb Raitto int i, j, cpu; 19602ca653d6SCaleb Raitto int num_cpu; 19612ca653d6SCaleb Raitto int stride; 1962986a4f4dSJason Wang 19632ca653d6SCaleb Raitto if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { 1964310974faSPeter Xu virtnet_clean_affinity(vi); 1965986a4f4dSJason Wang return; 1966986a4f4dSJason Wang } 1967986a4f4dSJason Wang 19682ca653d6SCaleb Raitto num_cpu = num_online_cpus(); 19692ca653d6SCaleb Raitto stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); 19702ca653d6SCaleb Raitto stragglers = num_cpu >= vi->curr_queue_pairs ? 19712ca653d6SCaleb Raitto num_cpu % vi->curr_queue_pairs : 19722ca653d6SCaleb Raitto 0; 19732ca653d6SCaleb Raitto cpu = cpumask_next(-1, cpu_online_mask); 19744d99f660SAndrei Vagin 19752ca653d6SCaleb Raitto for (i = 0; i < vi->curr_queue_pairs; i++) { 19762ca653d6SCaleb Raitto group_size = stride + (i < stragglers ? 1 : 0); 19772ca653d6SCaleb Raitto 19782ca653d6SCaleb Raitto for (j = 0; j < group_size; j++) { 19792ca653d6SCaleb Raitto cpumask_set_cpu(cpu, mask); 19802ca653d6SCaleb Raitto cpu = cpumask_next_wrap(cpu, cpu_online_mask, 19812ca653d6SCaleb Raitto nr_cpu_ids, false); 19822ca653d6SCaleb Raitto } 19832ca653d6SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, mask); 19842ca653d6SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, mask); 19852ca653d6SCaleb Raitto __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, false); 19862ca653d6SCaleb Raitto cpumask_clear(mask); 1987986a4f4dSJason Wang } 1988986a4f4dSJason Wang 1989986a4f4dSJason Wang vi->affinity_hint_set = true; 19902ca653d6SCaleb Raitto free_cpumask_var(mask); 199147be2479SWanlong Gao } 1992986a4f4dSJason Wang 19938017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 19948de4b2f3SWanlong Gao { 19958017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 19968017c279SSebastian Andrzej Siewior node); 19978de4b2f3SWanlong Gao virtnet_set_affinity(vi); 19988017c279SSebastian Andrzej Siewior return 0; 19998de4b2f3SWanlong Gao } 20003ab098dfSJason Wang 20018017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 20028017c279SSebastian Andrzej Siewior { 20038017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 20048017c279SSebastian Andrzej Siewior node_dead); 20058017c279SSebastian Andrzej Siewior virtnet_set_affinity(vi); 20068017c279SSebastian Andrzej Siewior return 0; 20078017c279SSebastian Andrzej Siewior } 20088017c279SSebastian Andrzej Siewior 20098017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 20108017c279SSebastian Andrzej Siewior { 20118017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 20128017c279SSebastian Andrzej Siewior node); 20138017c279SSebastian Andrzej Siewior 2014310974faSPeter Xu virtnet_clean_affinity(vi); 20158017c279SSebastian Andrzej Siewior return 0; 20168017c279SSebastian Andrzej Siewior } 20178017c279SSebastian Andrzej Siewior 20188017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online; 20198017c279SSebastian Andrzej Siewior 20208017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi) 20218017c279SSebastian Andrzej Siewior { 20228017c279SSebastian Andrzej Siewior int ret; 20238017c279SSebastian Andrzej Siewior 20248017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 20258017c279SSebastian Andrzej Siewior if (ret) 20268017c279SSebastian Andrzej Siewior return ret; 20278017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 20288017c279SSebastian Andrzej Siewior &vi->node_dead); 20298017c279SSebastian Andrzej Siewior if (!ret) 20308017c279SSebastian Andrzej Siewior return ret; 20318017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 20328017c279SSebastian Andrzej Siewior return ret; 20338017c279SSebastian Andrzej Siewior } 20348017c279SSebastian Andrzej Siewior 20358017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 20368017c279SSebastian Andrzej Siewior { 20378017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 20388017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 20398017c279SSebastian Andrzej Siewior &vi->node_dead); 2040a9ea3fc6SHerbert Xu } 2041a9ea3fc6SHerbert Xu 20428f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 20438f9f4668SRick Jones struct ethtool_ringparam *ring) 20448f9f4668SRick Jones { 20458f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 20468f9f4668SRick Jones 2047986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 2048986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 20498f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 20508f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 20518f9f4668SRick Jones } 20528f9f4668SRick Jones 205366846048SRick Jones 205466846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 205566846048SRick Jones struct ethtool_drvinfo *info) 205666846048SRick Jones { 205766846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 205866846048SRick Jones struct virtio_device *vdev = vi->vdev; 205966846048SRick Jones 206066846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 206166846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 206266846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 206366846048SRick Jones 206466846048SRick Jones } 206566846048SRick Jones 2066d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 2067d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 2068d73bcd2cSJason Wang struct ethtool_channels *channels) 2069d73bcd2cSJason Wang { 2070d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2071d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 2072d73bcd2cSJason Wang int err; 2073d73bcd2cSJason Wang 2074d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 2075d73bcd2cSJason Wang * We don't allow setting 'other' channels. 2076d73bcd2cSJason Wang */ 2077d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 2078d73bcd2cSJason Wang return -EINVAL; 2079d73bcd2cSJason Wang 2080c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 2081d73bcd2cSJason Wang return -EINVAL; 2082d73bcd2cSJason Wang 2083f600b690SJohn Fastabend /* For now we don't support modifying channels while XDP is loaded 2084f600b690SJohn Fastabend * also when XDP is loaded all RX queues have XDP programs so we only 2085f600b690SJohn Fastabend * need to check a single RX queue. 2086f600b690SJohn Fastabend */ 2087f600b690SJohn Fastabend if (vi->rq[0].xdp_prog) 2088f600b690SJohn Fastabend return -EINVAL; 2089f600b690SJohn Fastabend 209047be2479SWanlong Gao get_online_cpus(); 209147315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 2092de33212fSJeff Dike if (err) { 2093de33212fSJeff Dike put_online_cpus(); 2094de33212fSJeff Dike goto err; 2095d73bcd2cSJason Wang } 2096de33212fSJeff Dike virtnet_set_affinity(vi); 209747be2479SWanlong Gao put_online_cpus(); 2098d73bcd2cSJason Wang 2099de33212fSJeff Dike netif_set_real_num_tx_queues(dev, queue_pairs); 2100de33212fSJeff Dike netif_set_real_num_rx_queues(dev, queue_pairs); 2101de33212fSJeff Dike err: 2102d73bcd2cSJason Wang return err; 2103d73bcd2cSJason Wang } 2104d73bcd2cSJason Wang 2105d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 2106d7dfc5cfSToshiaki Makita { 2107d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2108d7dfc5cfSToshiaki Makita char *p = (char *)data; 2109d7dfc5cfSToshiaki Makita unsigned int i, j; 2110d7dfc5cfSToshiaki Makita 2111d7dfc5cfSToshiaki Makita switch (stringset) { 2112d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2113d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2114d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2115d7dfc5cfSToshiaki Makita snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s", 2116d7dfc5cfSToshiaki Makita i, virtnet_rq_stats_desc[j].desc); 2117d7dfc5cfSToshiaki Makita p += ETH_GSTRING_LEN; 2118d7dfc5cfSToshiaki Makita } 2119d7dfc5cfSToshiaki Makita } 2120d7dfc5cfSToshiaki Makita 2121d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2122d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 2123d7dfc5cfSToshiaki Makita snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s", 2124d7dfc5cfSToshiaki Makita i, virtnet_sq_stats_desc[j].desc); 2125d7dfc5cfSToshiaki Makita p += ETH_GSTRING_LEN; 2126d7dfc5cfSToshiaki Makita } 2127d7dfc5cfSToshiaki Makita } 2128d7dfc5cfSToshiaki Makita break; 2129d7dfc5cfSToshiaki Makita } 2130d7dfc5cfSToshiaki Makita } 2131d7dfc5cfSToshiaki Makita 2132d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset) 2133d7dfc5cfSToshiaki Makita { 2134d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2135d7dfc5cfSToshiaki Makita 2136d7dfc5cfSToshiaki Makita switch (sset) { 2137d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2138d7dfc5cfSToshiaki Makita return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + 2139d7dfc5cfSToshiaki Makita VIRTNET_SQ_STATS_LEN); 2140d7dfc5cfSToshiaki Makita default: 2141d7dfc5cfSToshiaki Makita return -EOPNOTSUPP; 2142d7dfc5cfSToshiaki Makita } 2143d7dfc5cfSToshiaki Makita } 2144d7dfc5cfSToshiaki Makita 2145d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev, 2146d7dfc5cfSToshiaki Makita struct ethtool_stats *stats, u64 *data) 2147d7dfc5cfSToshiaki Makita { 2148d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2149d7dfc5cfSToshiaki Makita unsigned int idx = 0, start, i, j; 2150d7dfc5cfSToshiaki Makita const u8 *stats_base; 2151d7dfc5cfSToshiaki Makita size_t offset; 2152d7dfc5cfSToshiaki Makita 2153d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2154d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 2155d7dfc5cfSToshiaki Makita 2156d46eeeafSJason Wang stats_base = (u8 *)&rq->stats; 2157d7dfc5cfSToshiaki Makita do { 2158d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 2159d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2160d7dfc5cfSToshiaki Makita offset = virtnet_rq_stats_desc[j].offset; 2161d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2162d7dfc5cfSToshiaki Makita } 2163d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 2164d7dfc5cfSToshiaki Makita idx += VIRTNET_RQ_STATS_LEN; 2165d7dfc5cfSToshiaki Makita } 2166d7dfc5cfSToshiaki Makita 2167d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2168d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 2169d7dfc5cfSToshiaki Makita 2170d7dfc5cfSToshiaki Makita stats_base = (u8 *)&sq->stats; 2171d7dfc5cfSToshiaki Makita do { 2172d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 2173d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 2174d7dfc5cfSToshiaki Makita offset = virtnet_sq_stats_desc[j].offset; 2175d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2176d7dfc5cfSToshiaki Makita } 2177d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 2178d7dfc5cfSToshiaki Makita idx += VIRTNET_SQ_STATS_LEN; 2179d7dfc5cfSToshiaki Makita } 2180d7dfc5cfSToshiaki Makita } 2181d7dfc5cfSToshiaki Makita 2182d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 2183d73bcd2cSJason Wang struct ethtool_channels *channels) 2184d73bcd2cSJason Wang { 2185d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2186d73bcd2cSJason Wang 2187d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 2188d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 2189d73bcd2cSJason Wang channels->max_other = 0; 2190d73bcd2cSJason Wang channels->rx_count = 0; 2191d73bcd2cSJason Wang channels->tx_count = 0; 2192d73bcd2cSJason Wang channels->other_count = 0; 2193d73bcd2cSJason Wang } 2194d73bcd2cSJason Wang 2195ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev, 2196ebb6b4b1SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 219716032be5SNikolay Aleksandrov { 219816032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 219916032be5SNikolay Aleksandrov 22009aedc6e2SCris Forno return ethtool_virtdev_set_link_ksettings(dev, cmd, 22019aedc6e2SCris Forno &vi->speed, &vi->duplex); 220216032be5SNikolay Aleksandrov } 220316032be5SNikolay Aleksandrov 2204ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev, 2205ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings *cmd) 220616032be5SNikolay Aleksandrov { 220716032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 220816032be5SNikolay Aleksandrov 2209ebb6b4b1SPhilippe Reynes cmd->base.speed = vi->speed; 2210ebb6b4b1SPhilippe Reynes cmd->base.duplex = vi->duplex; 2211ebb6b4b1SPhilippe Reynes cmd->base.port = PORT_OTHER; 221216032be5SNikolay Aleksandrov 221316032be5SNikolay Aleksandrov return 0; 221416032be5SNikolay Aleksandrov } 221516032be5SNikolay Aleksandrov 22160c465be1SJason Wang static int virtnet_set_coalesce(struct net_device *dev, 22170c465be1SJason Wang struct ethtool_coalesce *ec) 22180c465be1SJason Wang { 22190c465be1SJason Wang struct virtnet_info *vi = netdev_priv(dev); 22200c465be1SJason Wang int i, napi_weight; 22210c465be1SJason Wang 2222a51e5206SJakub Kicinski if (ec->tx_max_coalesced_frames > 1 || 2223a51e5206SJakub Kicinski ec->rx_max_coalesced_frames != 1) 22240c465be1SJason Wang return -EINVAL; 22250c465be1SJason Wang 22260c465be1SJason Wang napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 22270c465be1SJason Wang if (napi_weight ^ vi->sq[0].napi.weight) { 22280c465be1SJason Wang if (dev->flags & IFF_UP) 22290c465be1SJason Wang return -EBUSY; 22300c465be1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 22310c465be1SJason Wang vi->sq[i].napi.weight = napi_weight; 22320c465be1SJason Wang } 22330c465be1SJason Wang 22340c465be1SJason Wang return 0; 22350c465be1SJason Wang } 22360c465be1SJason Wang 22370c465be1SJason Wang static int virtnet_get_coalesce(struct net_device *dev, 22380c465be1SJason Wang struct ethtool_coalesce *ec) 22390c465be1SJason Wang { 22400c465be1SJason Wang struct ethtool_coalesce ec_default = { 22410c465be1SJason Wang .cmd = ETHTOOL_GCOALESCE, 22420c465be1SJason Wang .rx_max_coalesced_frames = 1, 22430c465be1SJason Wang }; 22440c465be1SJason Wang struct virtnet_info *vi = netdev_priv(dev); 22450c465be1SJason Wang 22460c465be1SJason Wang memcpy(ec, &ec_default, sizeof(ec_default)); 22470c465be1SJason Wang 22480c465be1SJason Wang if (vi->sq[0].napi.weight) 22490c465be1SJason Wang ec->tx_max_coalesced_frames = 1; 22500c465be1SJason Wang 22510c465be1SJason Wang return 0; 22520c465be1SJason Wang } 22530c465be1SJason Wang 225416032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 225516032be5SNikolay Aleksandrov { 225616032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 225716032be5SNikolay Aleksandrov 225816032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 225916032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 226016032be5SNikolay Aleksandrov } 226116032be5SNikolay Aleksandrov 2262faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi) 2263faa9b39fSJason Baron { 2264faa9b39fSJason Baron u32 speed; 2265faa9b39fSJason Baron u8 duplex; 2266faa9b39fSJason Baron 2267faa9b39fSJason Baron if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 2268faa9b39fSJason Baron return; 2269faa9b39fSJason Baron 227064ffa39dSMichael S. Tsirkin virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); 227164ffa39dSMichael S. Tsirkin 2272faa9b39fSJason Baron if (ethtool_validate_speed(speed)) 2273faa9b39fSJason Baron vi->speed = speed; 227464ffa39dSMichael S. Tsirkin 227564ffa39dSMichael S. Tsirkin virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); 227664ffa39dSMichael S. Tsirkin 2277faa9b39fSJason Baron if (ethtool_validate_duplex(duplex)) 2278faa9b39fSJason Baron vi->duplex = duplex; 2279faa9b39fSJason Baron } 2280faa9b39fSJason Baron 22810fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 2282a51e5206SJakub Kicinski .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES, 228366846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 22849f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 22858f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 2286d7dfc5cfSToshiaki Makita .get_strings = virtnet_get_strings, 2287d7dfc5cfSToshiaki Makita .get_sset_count = virtnet_get_sset_count, 2288d7dfc5cfSToshiaki Makita .get_ethtool_stats = virtnet_get_ethtool_stats, 2289d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 2290d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 2291074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 2292ebb6b4b1SPhilippe Reynes .get_link_ksettings = virtnet_get_link_ksettings, 2293ebb6b4b1SPhilippe Reynes .set_link_ksettings = virtnet_set_link_ksettings, 22940c465be1SJason Wang .set_coalesce = virtnet_set_coalesce, 22950c465be1SJason Wang .get_coalesce = virtnet_get_coalesce, 2296a9ea3fc6SHerbert Xu }; 2297a9ea3fc6SHerbert Xu 22989fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev) 22999fe7bfceSJohn Fastabend { 23009fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 23019fe7bfceSJohn Fastabend int i; 23029fe7bfceSJohn Fastabend 23039fe7bfceSJohn Fastabend /* Make sure no work handler is accessing the device */ 23049fe7bfceSJohn Fastabend flush_work(&vi->config_work); 23059fe7bfceSJohn Fastabend 230605c998b7SAke Koomsin netif_tx_lock_bh(vi->dev); 23079fe7bfceSJohn Fastabend netif_device_detach(vi->dev); 230805c998b7SAke Koomsin netif_tx_unlock_bh(vi->dev); 23099fe7bfceSJohn Fastabend cancel_delayed_work_sync(&vi->refill); 23109fe7bfceSJohn Fastabend 23119fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 2312b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 23139fe7bfceSJohn Fastabend napi_disable(&vi->rq[i].napi); 231478a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 2315b92f1e67SWillem de Bruijn } 23169fe7bfceSJohn Fastabend } 23179fe7bfceSJohn Fastabend } 23189fe7bfceSJohn Fastabend 23199fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi); 23209fe7bfceSJohn Fastabend 23219fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev) 23229fe7bfceSJohn Fastabend { 23239fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 23249fe7bfceSJohn Fastabend int err, i; 23259fe7bfceSJohn Fastabend 23269fe7bfceSJohn Fastabend err = init_vqs(vi); 23279fe7bfceSJohn Fastabend if (err) 23289fe7bfceSJohn Fastabend return err; 23299fe7bfceSJohn Fastabend 23309fe7bfceSJohn Fastabend virtio_device_ready(vdev); 23319fe7bfceSJohn Fastabend 23329fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 23339fe7bfceSJohn Fastabend for (i = 0; i < vi->curr_queue_pairs; i++) 23349fe7bfceSJohn Fastabend if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 23359fe7bfceSJohn Fastabend schedule_delayed_work(&vi->refill, 0); 23369fe7bfceSJohn Fastabend 2337b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 2338e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2339b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2340b92f1e67SWillem de Bruijn &vi->sq[i].napi); 2341b92f1e67SWillem de Bruijn } 23429fe7bfceSJohn Fastabend } 23439fe7bfceSJohn Fastabend 234405c998b7SAke Koomsin netif_tx_lock_bh(vi->dev); 23459fe7bfceSJohn Fastabend netif_device_attach(vi->dev); 234605c998b7SAke Koomsin netif_tx_unlock_bh(vi->dev); 23479fe7bfceSJohn Fastabend return err; 23489fe7bfceSJohn Fastabend } 23499fe7bfceSJohn Fastabend 23503f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 23513f93522fSJason Wang { 23523f93522fSJason Wang struct scatterlist sg; 235312e57169SMichael S. Tsirkin vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); 23543f93522fSJason Wang 235512e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); 23563f93522fSJason Wang 23573f93522fSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 23583f93522fSJason Wang VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 23593f93522fSJason Wang dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); 23603f93522fSJason Wang return -EINVAL; 23613f93522fSJason Wang } 23623f93522fSJason Wang 23633f93522fSJason Wang return 0; 23643f93522fSJason Wang } 23653f93522fSJason Wang 23663f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 23673f93522fSJason Wang { 23683f93522fSJason Wang u64 offloads = 0; 23693f93522fSJason Wang 23703f93522fSJason Wang if (!vi->guest_offloads) 23713f93522fSJason Wang return 0; 23723f93522fSJason Wang 23733f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 23743f93522fSJason Wang } 23753f93522fSJason Wang 23763f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 23773f93522fSJason Wang { 23783f93522fSJason Wang u64 offloads = vi->guest_offloads; 23793f93522fSJason Wang 23803f93522fSJason Wang if (!vi->guest_offloads) 23813f93522fSJason Wang return 0; 23823f93522fSJason Wang 23833f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 23843f93522fSJason Wang } 23853f93522fSJason Wang 23869861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 23879861ce03SJakub Kicinski struct netlink_ext_ack *extack) 2388f600b690SJohn Fastabend { 2389f600b690SJohn Fastabend unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); 2390f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 2391f600b690SJohn Fastabend struct bpf_prog *old_prog; 2392017b29c3SJason Wang u16 xdp_qp = 0, curr_qp; 2393672aafd5SJohn Fastabend int i, err; 2394f600b690SJohn Fastabend 23953f93522fSJason Wang if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 23963f93522fSJason Wang && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 239792502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 239892502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 239918ba58e1SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 240018ba58e1SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) { 240118ba58e1SJason Wang NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first"); 2402f600b690SJohn Fastabend return -EOPNOTSUPP; 2403f600b690SJohn Fastabend } 2404f600b690SJohn Fastabend 2405f600b690SJohn Fastabend if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 24064d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 2407f600b690SJohn Fastabend return -EINVAL; 2408f600b690SJohn Fastabend } 2409f600b690SJohn Fastabend 2410f600b690SJohn Fastabend if (dev->mtu > max_sz) { 24114d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); 2412f600b690SJohn Fastabend netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); 2413f600b690SJohn Fastabend return -EINVAL; 2414f600b690SJohn Fastabend } 2415f600b690SJohn Fastabend 2416672aafd5SJohn Fastabend curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 2417672aafd5SJohn Fastabend if (prog) 2418672aafd5SJohn Fastabend xdp_qp = nr_cpu_ids; 2419672aafd5SJohn Fastabend 2420672aafd5SJohn Fastabend /* XDP requires extra queues for XDP_TX */ 2421672aafd5SJohn Fastabend if (curr_qp + xdp_qp > vi->max_queue_pairs) { 24224d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available"); 2423672aafd5SJohn Fastabend netdev_warn(dev, "request %i queues but max is %i\n", 2424672aafd5SJohn Fastabend curr_qp + xdp_qp, vi->max_queue_pairs); 2425672aafd5SJohn Fastabend return -ENOMEM; 2426672aafd5SJohn Fastabend } 2427672aafd5SJohn Fastabend 242803aa6d34SToshiaki Makita old_prog = rtnl_dereference(vi->rq[0].xdp_prog); 242903aa6d34SToshiaki Makita if (!prog && !old_prog) 243003aa6d34SToshiaki Makita return 0; 243103aa6d34SToshiaki Makita 243285192dbfSAndrii Nakryiko if (prog) 243385192dbfSAndrii Nakryiko bpf_prog_add(prog, vi->max_queue_pairs - 1); 24342de2f7f4SJohn Fastabend 24354941d472SJason Wang /* Make sure NAPI is not using any XDP TX queues for RX. */ 2436534da5e8SToshiaki Makita if (netif_running(dev)) { 2437534da5e8SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 24384941d472SJason Wang napi_disable(&vi->rq[i].napi); 2439534da5e8SToshiaki Makita virtnet_napi_tx_disable(&vi->sq[i].napi); 2440534da5e8SToshiaki Makita } 2441534da5e8SToshiaki Makita } 24422de2f7f4SJohn Fastabend 244303aa6d34SToshiaki Makita if (!prog) { 244403aa6d34SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 244503aa6d34SToshiaki Makita rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 244603aa6d34SToshiaki Makita if (i == 0) 244703aa6d34SToshiaki Makita virtnet_restore_guest_offloads(vi); 244803aa6d34SToshiaki Makita } 244903aa6d34SToshiaki Makita synchronize_net(); 245003aa6d34SToshiaki Makita } 245103aa6d34SToshiaki Makita 24524941d472SJason Wang err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 24534941d472SJason Wang if (err) 24544941d472SJason Wang goto err; 2455188313c1SToshiaki Makita netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 24564941d472SJason Wang vi->xdp_queue_pairs = xdp_qp; 2457f600b690SJohn Fastabend 245803aa6d34SToshiaki Makita if (prog) { 2459f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 2460f600b690SJohn Fastabend rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 246103aa6d34SToshiaki Makita if (i == 0 && !old_prog) 24623f93522fSJason Wang virtnet_clear_guest_offloads(vi); 24633f93522fSJason Wang } 246403aa6d34SToshiaki Makita } 246503aa6d34SToshiaki Makita 246603aa6d34SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 2467f600b690SJohn Fastabend if (old_prog) 2468f600b690SJohn Fastabend bpf_prog_put(old_prog); 2469534da5e8SToshiaki Makita if (netif_running(dev)) { 24704941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2471534da5e8SToshiaki Makita virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2472534da5e8SToshiaki Makita &vi->sq[i].napi); 2473534da5e8SToshiaki Makita } 2474f600b690SJohn Fastabend } 2475f600b690SJohn Fastabend 2476f600b690SJohn Fastabend return 0; 24772de2f7f4SJohn Fastabend 24784941d472SJason Wang err: 247903aa6d34SToshiaki Makita if (!prog) { 248003aa6d34SToshiaki Makita virtnet_clear_guest_offloads(vi); 24814941d472SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 248203aa6d34SToshiaki Makita rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); 248303aa6d34SToshiaki Makita } 248403aa6d34SToshiaki Makita 24858be4d9a4SToshiaki Makita if (netif_running(dev)) { 2486534da5e8SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 24874941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2488534da5e8SToshiaki Makita virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2489534da5e8SToshiaki Makita &vi->sq[i].napi); 2490534da5e8SToshiaki Makita } 24918be4d9a4SToshiaki Makita } 24922de2f7f4SJohn Fastabend if (prog) 24932de2f7f4SJohn Fastabend bpf_prog_sub(prog, vi->max_queue_pairs - 1); 24942de2f7f4SJohn Fastabend return err; 2495f600b690SJohn Fastabend } 2496f600b690SJohn Fastabend 2497f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 2498f600b690SJohn Fastabend { 2499f600b690SJohn Fastabend switch (xdp->command) { 2500f600b690SJohn Fastabend case XDP_SETUP_PROG: 25019861ce03SJakub Kicinski return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 2502f600b690SJohn Fastabend default: 2503f600b690SJohn Fastabend return -EINVAL; 2504f600b690SJohn Fastabend } 2505f600b690SJohn Fastabend } 2506f600b690SJohn Fastabend 2507ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 2508ba5e4426SSridhar Samudrala size_t len) 2509ba5e4426SSridhar Samudrala { 2510ba5e4426SSridhar Samudrala struct virtnet_info *vi = netdev_priv(dev); 2511ba5e4426SSridhar Samudrala int ret; 2512ba5e4426SSridhar Samudrala 2513ba5e4426SSridhar Samudrala if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 2514ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2515ba5e4426SSridhar Samudrala 2516ba5e4426SSridhar Samudrala ret = snprintf(buf, len, "sby"); 2517ba5e4426SSridhar Samudrala if (ret >= len) 2518ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2519ba5e4426SSridhar Samudrala 2520ba5e4426SSridhar Samudrala return 0; 2521ba5e4426SSridhar Samudrala } 2522ba5e4426SSridhar Samudrala 2523a02e8964SWillem de Bruijn static int virtnet_set_features(struct net_device *dev, 2524a02e8964SWillem de Bruijn netdev_features_t features) 2525a02e8964SWillem de Bruijn { 2526a02e8964SWillem de Bruijn struct virtnet_info *vi = netdev_priv(dev); 2527cf8691cbSMichael S. Tsirkin u64 offloads; 2528a02e8964SWillem de Bruijn int err; 2529a02e8964SWillem de Bruijn 2530cf8691cbSMichael S. Tsirkin if ((dev->features ^ features) & NETIF_F_LRO) { 2531a02e8964SWillem de Bruijn if (vi->xdp_queue_pairs) 2532a02e8964SWillem de Bruijn return -EBUSY; 2533a02e8964SWillem de Bruijn 2534a02e8964SWillem de Bruijn if (features & NETIF_F_LRO) 2535cf8691cbSMichael S. Tsirkin offloads = vi->guest_offloads_capable; 2536a02e8964SWillem de Bruijn else 2537cf8691cbSMichael S. Tsirkin offloads = vi->guest_offloads_capable & 2538cf8691cbSMichael S. Tsirkin ~GUEST_OFFLOAD_LRO_MASK; 2539a02e8964SWillem de Bruijn 2540a02e8964SWillem de Bruijn err = virtnet_set_guest_offloads(vi, offloads); 2541a02e8964SWillem de Bruijn if (err) 2542a02e8964SWillem de Bruijn return err; 25433618ad2aSTonghao Zhang vi->guest_offloads = offloads; 2544cf8691cbSMichael S. Tsirkin } 2545cf8691cbSMichael S. Tsirkin 2546a02e8964SWillem de Bruijn return 0; 2547a02e8964SWillem de Bruijn } 2548a02e8964SWillem de Bruijn 254976288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 255076288b4eSStephen Hemminger .ndo_open = virtnet_open, 255176288b4eSStephen Hemminger .ndo_stop = virtnet_close, 255276288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 255376288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 25549c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 25552af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 25563fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 25571824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 25581824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 2559f4e63525SJakub Kicinski .ndo_bpf = virtnet_xdp, 2560186b3c99SJason Wang .ndo_xdp_xmit = virtnet_xdp_xmit, 25612836b4f2SVlad Yasevich .ndo_features_check = passthru_features_check, 2562ba5e4426SSridhar Samudrala .ndo_get_phys_port_name = virtnet_get_phys_port_name, 2563a02e8964SWillem de Bruijn .ndo_set_features = virtnet_set_features, 256476288b4eSStephen Hemminger }; 256576288b4eSStephen Hemminger 2566586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 25679f4d26d0SMark McLoughlin { 2568586d17c5SJason Wang struct virtnet_info *vi = 2569586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 25709f4d26d0SMark McLoughlin u16 v; 25719f4d26d0SMark McLoughlin 2572855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 2573855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 2574507613bfSMichael S. Tsirkin return; 2575586d17c5SJason Wang 2576586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 2577ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 2578586d17c5SJason Wang virtnet_ack_link_announce(vi); 2579586d17c5SJason Wang } 25809f4d26d0SMark McLoughlin 25819f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 25829f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 25839f4d26d0SMark McLoughlin 25849f4d26d0SMark McLoughlin if (vi->status == v) 2585507613bfSMichael S. Tsirkin return; 25869f4d26d0SMark McLoughlin 25879f4d26d0SMark McLoughlin vi->status = v; 25889f4d26d0SMark McLoughlin 25899f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 2590faa9b39fSJason Baron virtnet_update_settings(vi); 25919f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 2592986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 25939f4d26d0SMark McLoughlin } else { 25949f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 2595986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 25969f4d26d0SMark McLoughlin } 25979f4d26d0SMark McLoughlin } 25989f4d26d0SMark McLoughlin 25999f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 26009f4d26d0SMark McLoughlin { 26019f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 26029f4d26d0SMark McLoughlin 26033b07e9caSTejun Heo schedule_work(&vi->config_work); 26049f4d26d0SMark McLoughlin } 26059f4d26d0SMark McLoughlin 2606986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 2607986a4f4dSJason Wang { 2608d4fb84eeSAndrey Vagin int i; 2609d4fb84eeSAndrey Vagin 2610ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 26115198d545SJakub Kicinski __netif_napi_del(&vi->rq[i].napi); 26125198d545SJakub Kicinski __netif_napi_del(&vi->sq[i].napi); 2613ab3971b1SJason Wang } 2614d4fb84eeSAndrey Vagin 26155198d545SJakub Kicinski /* We called __netif_napi_del(), 2616963abe5cSEric Dumazet * we need to respect an RCU grace period before freeing vi->rq 2617963abe5cSEric Dumazet */ 2618963abe5cSEric Dumazet synchronize_net(); 2619963abe5cSEric Dumazet 2620986a4f4dSJason Wang kfree(vi->rq); 2621986a4f4dSJason Wang kfree(vi->sq); 262212e57169SMichael S. Tsirkin kfree(vi->ctrl); 2623986a4f4dSJason Wang } 2624986a4f4dSJason Wang 262547315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi) 2626986a4f4dSJason Wang { 2627f600b690SJohn Fastabend struct bpf_prog *old_prog; 2628986a4f4dSJason Wang int i; 2629986a4f4dSJason Wang 2630986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2631986a4f4dSJason Wang while (vi->rq[i].pages) 2632986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 2633f600b690SJohn Fastabend 2634f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2635f600b690SJohn Fastabend RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 2636f600b690SJohn Fastabend if (old_prog) 2637f600b690SJohn Fastabend bpf_prog_put(old_prog); 2638986a4f4dSJason Wang } 263947315329SJohn Fastabend } 264047315329SJohn Fastabend 264147315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi) 264247315329SJohn Fastabend { 264347315329SJohn Fastabend rtnl_lock(); 264447315329SJohn Fastabend _free_receive_bufs(vi); 2645f600b690SJohn Fastabend rtnl_unlock(); 2646986a4f4dSJason Wang } 2647986a4f4dSJason Wang 2648fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 2649fb51879dSMichael Dalton { 2650fb51879dSMichael Dalton int i; 2651fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 2652fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 2653fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 2654fb51879dSMichael Dalton } 2655fb51879dSMichael Dalton 2656986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 2657986a4f4dSJason Wang { 2658986a4f4dSJason Wang void *buf; 2659986a4f4dSJason Wang int i; 2660986a4f4dSJason Wang 2661986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2662986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 266356434a01SJohn Fastabend while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 26645050471dSToshiaki Makita if (!is_xdp_frame(buf)) 2665986a4f4dSJason Wang dev_kfree_skb(buf); 266656434a01SJohn Fastabend else 26675050471dSToshiaki Makita xdp_return_frame(ptr_to_xdp(buf)); 266856434a01SJohn Fastabend } 2669986a4f4dSJason Wang } 2670986a4f4dSJason Wang 2671986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2672986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 2673986a4f4dSJason Wang 2674986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2675ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 2676680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 2677ab7db917SMichael Dalton } else if (vi->big_packets) { 2678fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 2679ab7db917SMichael Dalton } else { 2680f6b10209SJason Wang put_page(virt_to_head_page(buf)); 2681986a4f4dSJason Wang } 2682986a4f4dSJason Wang } 2683986a4f4dSJason Wang } 2684ab7db917SMichael Dalton } 2685986a4f4dSJason Wang 2686e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 2687e9d7417bSJason Wang { 2688e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 2689e9d7417bSJason Wang 2690310974faSPeter Xu virtnet_clean_affinity(vi); 2691986a4f4dSJason Wang 2692e9d7417bSJason Wang vdev->config->del_vqs(vdev); 2693986a4f4dSJason Wang 2694986a4f4dSJason Wang virtnet_free_queues(vi); 2695986a4f4dSJason Wang } 2696986a4f4dSJason Wang 2697d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at 2698d85b758fSMichael S. Tsirkin * least one full packet? 2699d85b758fSMichael S. Tsirkin * Logic below assumes the mergeable buffer header is used. 2700d85b758fSMichael S. Tsirkin */ 2701d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 2702d85b758fSMichael S. Tsirkin { 2703d85b758fSMichael S. Tsirkin const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2704d85b758fSMichael S. Tsirkin unsigned int rq_size = virtqueue_get_vring_size(vq); 2705d85b758fSMichael S. Tsirkin unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 2706d85b758fSMichael S. Tsirkin unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 2707d85b758fSMichael S. Tsirkin unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 2708d85b758fSMichael S. Tsirkin 2709f0c3192cSMichael S. Tsirkin return max(max(min_buf_len, hdr_len) - hdr_len, 2710f0c3192cSMichael S. Tsirkin (unsigned int)GOOD_PACKET_LEN); 2711d85b758fSMichael S. Tsirkin } 2712d85b758fSMichael S. Tsirkin 2713986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 2714986a4f4dSJason Wang { 2715986a4f4dSJason Wang vq_callback_t **callbacks; 2716986a4f4dSJason Wang struct virtqueue **vqs; 2717986a4f4dSJason Wang int ret = -ENOMEM; 2718986a4f4dSJason Wang int i, total_vqs; 2719986a4f4dSJason Wang const char **names; 2720d45b897bSMichael S. Tsirkin bool *ctx; 2721986a4f4dSJason Wang 2722986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 2723986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 2724986a4f4dSJason Wang * possible control vq. 2725986a4f4dSJason Wang */ 2726986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 2727986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 2728986a4f4dSJason Wang 2729986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 27306396bb22SKees Cook vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 2731986a4f4dSJason Wang if (!vqs) 2732986a4f4dSJason Wang goto err_vq; 27336da2ec56SKees Cook callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL); 2734986a4f4dSJason Wang if (!callbacks) 2735986a4f4dSJason Wang goto err_callback; 27366da2ec56SKees Cook names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL); 2737986a4f4dSJason Wang if (!names) 2738986a4f4dSJason Wang goto err_names; 2739192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 27406396bb22SKees Cook ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 2741d45b897bSMichael S. Tsirkin if (!ctx) 2742d45b897bSMichael S. Tsirkin goto err_ctx; 2743d45b897bSMichael S. Tsirkin } else { 2744d45b897bSMichael S. Tsirkin ctx = NULL; 2745d45b897bSMichael S. Tsirkin } 2746986a4f4dSJason Wang 2747986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 2748986a4f4dSJason Wang if (vi->has_cvq) { 2749986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 2750986a4f4dSJason Wang names[total_vqs - 1] = "control"; 2751986a4f4dSJason Wang } 2752986a4f4dSJason Wang 2753986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 2754986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2755986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 2756986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 2757986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 2758986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 2759986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 2760986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 2761d45b897bSMichael S. Tsirkin if (ctx) 2762d45b897bSMichael S. Tsirkin ctx[rxq2vq(i)] = true; 2763986a4f4dSJason Wang } 2764986a4f4dSJason Wang 2765986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 2766d45b897bSMichael S. Tsirkin names, ctx, NULL); 2767986a4f4dSJason Wang if (ret) 2768986a4f4dSJason Wang goto err_find; 2769986a4f4dSJason Wang 2770986a4f4dSJason Wang if (vi->has_cvq) { 2771986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 2772986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 2773f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2774986a4f4dSJason Wang } 2775986a4f4dSJason Wang 2776986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2777986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 2778d85b758fSMichael S. Tsirkin vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 2779986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 2780986a4f4dSJason Wang } 2781986a4f4dSJason Wang 27822fa3c8a8STonghao Zhang /* run here: ret == 0. */ 2783986a4f4dSJason Wang 2784986a4f4dSJason Wang 2785986a4f4dSJason Wang err_find: 2786d45b897bSMichael S. Tsirkin kfree(ctx); 2787d45b897bSMichael S. Tsirkin err_ctx: 2788986a4f4dSJason Wang kfree(names); 2789986a4f4dSJason Wang err_names: 2790986a4f4dSJason Wang kfree(callbacks); 2791986a4f4dSJason Wang err_callback: 2792986a4f4dSJason Wang kfree(vqs); 2793986a4f4dSJason Wang err_vq: 2794986a4f4dSJason Wang return ret; 2795986a4f4dSJason Wang } 2796986a4f4dSJason Wang 2797986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 2798986a4f4dSJason Wang { 2799986a4f4dSJason Wang int i; 2800986a4f4dSJason Wang 280112e57169SMichael S. Tsirkin vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 280212e57169SMichael S. Tsirkin if (!vi->ctrl) 280312e57169SMichael S. Tsirkin goto err_ctrl; 28046396bb22SKees Cook vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 2805986a4f4dSJason Wang if (!vi->sq) 2806986a4f4dSJason Wang goto err_sq; 28076396bb22SKees Cook vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 2808008d4278SAmerigo Wang if (!vi->rq) 2809986a4f4dSJason Wang goto err_rq; 2810986a4f4dSJason Wang 2811986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 2812986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2813986a4f4dSJason Wang vi->rq[i].pages = NULL; 2814986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2815986a4f4dSJason Wang napi_weight); 28161d11e732SWillem de Bruijn netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2817b92f1e67SWillem de Bruijn napi_tx ? napi_weight : 0); 2818986a4f4dSJason Wang 2819986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 28205377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 2821986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 2822d7dfc5cfSToshiaki Makita 2823d7dfc5cfSToshiaki Makita u64_stats_init(&vi->rq[i].stats.syncp); 2824d7dfc5cfSToshiaki Makita u64_stats_init(&vi->sq[i].stats.syncp); 2825986a4f4dSJason Wang } 2826986a4f4dSJason Wang 2827986a4f4dSJason Wang return 0; 2828986a4f4dSJason Wang 2829986a4f4dSJason Wang err_rq: 2830986a4f4dSJason Wang kfree(vi->sq); 2831986a4f4dSJason Wang err_sq: 283212e57169SMichael S. Tsirkin kfree(vi->ctrl); 283312e57169SMichael S. Tsirkin err_ctrl: 2834986a4f4dSJason Wang return -ENOMEM; 2835e9d7417bSJason Wang } 2836e9d7417bSJason Wang 28373f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 28383f9c10b0SAmit Shah { 2839986a4f4dSJason Wang int ret; 28403f9c10b0SAmit Shah 2841986a4f4dSJason Wang /* Allocate send & receive queues */ 2842986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 2843986a4f4dSJason Wang if (ret) 2844986a4f4dSJason Wang goto err; 28453f9c10b0SAmit Shah 2846986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 2847986a4f4dSJason Wang if (ret) 2848986a4f4dSJason Wang goto err_free; 28493f9c10b0SAmit Shah 285047be2479SWanlong Gao get_online_cpus(); 28518898c21cSWanlong Gao virtnet_set_affinity(vi); 285247be2479SWanlong Gao put_online_cpus(); 285347be2479SWanlong Gao 28543f9c10b0SAmit Shah return 0; 2855986a4f4dSJason Wang 2856986a4f4dSJason Wang err_free: 2857986a4f4dSJason Wang virtnet_free_queues(vi); 2858986a4f4dSJason Wang err: 2859986a4f4dSJason Wang return ret; 28603f9c10b0SAmit Shah } 28613f9c10b0SAmit Shah 2862fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2863fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 2864718ad681Sstephen hemminger char *buf) 2865fbf28d78SMichael Dalton { 2866fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 2867fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 28683cc81a9aSJason Wang unsigned int headroom = virtnet_get_headroom(vi); 28693cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 28705377d758SJohannes Berg struct ewma_pkt_len *avg; 2871fbf28d78SMichael Dalton 2872fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 2873fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 2874d85b758fSMichael S. Tsirkin return sprintf(buf, "%u\n", 28753cc81a9aSJason Wang get_mergeable_buf_len(&vi->rq[queue_index], avg, 28763cc81a9aSJason Wang SKB_DATA_ALIGN(headroom + tailroom))); 2877fbf28d78SMichael Dalton } 2878fbf28d78SMichael Dalton 2879fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 2880fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 2881fbf28d78SMichael Dalton 2882fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 2883fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 2884fbf28d78SMichael Dalton NULL 2885fbf28d78SMichael Dalton }; 2886fbf28d78SMichael Dalton 2887fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 2888fbf28d78SMichael Dalton .name = "virtio_net", 2889fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 2890fbf28d78SMichael Dalton }; 2891fbf28d78SMichael Dalton #endif 2892fbf28d78SMichael Dalton 2893892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 2894892d6eb1SJason Wang unsigned int fbit, 2895892d6eb1SJason Wang const char *fname, const char *dname) 2896892d6eb1SJason Wang { 2897892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 2898892d6eb1SJason Wang return false; 2899892d6eb1SJason Wang 2900892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 2901892d6eb1SJason Wang fname, dname); 2902892d6eb1SJason Wang 2903892d6eb1SJason Wang return true; 2904892d6eb1SJason Wang } 2905892d6eb1SJason Wang 2906892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 2907892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 2908892d6eb1SJason Wang 2909892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 2910892d6eb1SJason Wang { 2911892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 2912892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 2913892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2914892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 2915892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2916892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 2917892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2918892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 2919892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 2920892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 2921892d6eb1SJason Wang return false; 2922892d6eb1SJason Wang } 2923892d6eb1SJason Wang 2924892d6eb1SJason Wang return true; 2925892d6eb1SJason Wang } 2926892d6eb1SJason Wang 2927d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU 2928d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU 2929d0c2c997SJarod Wilson 2930fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev) 2931296f96fcSRusty Russell { 29326ba42248SMichael S. Tsirkin if (!vdev->config->get) { 29336ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 29346ba42248SMichael S. Tsirkin __func__); 29356ba42248SMichael S. Tsirkin return -EINVAL; 29366ba42248SMichael S. Tsirkin } 29376ba42248SMichael S. Tsirkin 2938892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 2939892d6eb1SJason Wang return -EINVAL; 2940892d6eb1SJason Wang 2941fe36cbe0SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 2942fe36cbe0SMichael S. Tsirkin int mtu = virtio_cread16(vdev, 2943fe36cbe0SMichael S. Tsirkin offsetof(struct virtio_net_config, 2944fe36cbe0SMichael S. Tsirkin mtu)); 2945fe36cbe0SMichael S. Tsirkin if (mtu < MIN_MTU) 2946fe36cbe0SMichael S. Tsirkin __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 2947fe36cbe0SMichael S. Tsirkin } 2948fe36cbe0SMichael S. Tsirkin 2949fe36cbe0SMichael S. Tsirkin return 0; 2950fe36cbe0SMichael S. Tsirkin } 2951fe36cbe0SMichael S. Tsirkin 2952fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev) 2953fe36cbe0SMichael S. Tsirkin { 2954d7dfc5cfSToshiaki Makita int i, err = -ENOMEM; 2955fe36cbe0SMichael S. Tsirkin struct net_device *dev; 2956fe36cbe0SMichael S. Tsirkin struct virtnet_info *vi; 2957fe36cbe0SMichael S. Tsirkin u16 max_queue_pairs; 2958fe36cbe0SMichael S. Tsirkin int mtu; 2959fe36cbe0SMichael S. Tsirkin 2960986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 2961855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 2962855e0c52SRusty Russell struct virtio_net_config, 2963855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 2964986a4f4dSJason Wang 2965986a4f4dSJason Wang /* We need at least 2 queue's */ 2966986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 2967986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 2968986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2969986a4f4dSJason Wang max_queue_pairs = 1; 2970296f96fcSRusty Russell 2971296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 2972986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 2973296f96fcSRusty Russell if (!dev) 2974296f96fcSRusty Russell return -ENOMEM; 2975296f96fcSRusty Russell 2976296f96fcSRusty Russell /* Set up network device as normal. */ 2977ab5bd583SXuan Zhuo dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE | 2978ab5bd583SXuan Zhuo IFF_TX_SKB_NO_LINEAR; 297976288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 2980296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 29813fa2a1dfSstephen hemminger 29827ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 2983296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 2984296f96fcSRusty Russell 2985296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 298698e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 2987296f96fcSRusty Russell /* This opens up the world of extra features. */ 298848900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 298998e778c9SMichał Mirosław if (csum) 299048900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 299198e778c9SMichał Mirosław 299298e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 2993e078de03SDavid S. Miller dev->hw_features |= NETIF_F_TSO 299434a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 299534a48579SRusty Russell } 29965539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 299798e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 299898e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 299998e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 300098e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 300198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 300298e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 300398e778c9SMichał Mirosław 300441f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 300541f2f127SJason Wang 300698e778c9SMichał Mirosław if (gso) 3007e078de03SDavid S. Miller dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 300898e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 3009296f96fcSRusty Russell } 30104f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 30114f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 3012a02e8964SWillem de Bruijn if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 3013a02e8964SWillem de Bruijn virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)) 3014a02e8964SWillem de Bruijn dev->features |= NETIF_F_LRO; 3015cf8691cbSMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) 3016a02e8964SWillem de Bruijn dev->hw_features |= NETIF_F_LRO; 3017296f96fcSRusty Russell 30184fda8302SJason Wang dev->vlan_features = dev->features; 30194fda8302SJason Wang 3020d0c2c997SJarod Wilson /* MTU range: 68 - 65535 */ 3021d0c2c997SJarod Wilson dev->min_mtu = MIN_MTU; 3022d0c2c997SJarod Wilson dev->max_mtu = MAX_MTU; 3023d0c2c997SJarod Wilson 3024296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 3025855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 3026855e0c52SRusty Russell virtio_cread_bytes(vdev, 3027a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 3028855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 3029855e0c52SRusty Russell else 3030f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 3031296f96fcSRusty Russell 3032296f96fcSRusty Russell /* Set up our device-specific information */ 3033296f96fcSRusty Russell vi = netdev_priv(dev); 3034296f96fcSRusty Russell vi->dev = dev; 3035296f96fcSRusty Russell vi->vdev = vdev; 3036d9d5dcc8SChristian Borntraeger vdev->priv = vi; 3037827da44cSJohn Stultz 3038586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 3039296f96fcSRusty Russell 304097402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 30418e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 30428e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 3043e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 3044e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 304597402b96SHerbert Xu vi->big_packets = true; 304697402b96SHerbert Xu 30473f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 30483f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 30493f2c31d9SMark McLoughlin 3050d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 3051d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 3052012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 3053012873d0SMichael S. Tsirkin else 3054012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 3055012873d0SMichael S. Tsirkin 305675993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 305775993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 3058e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 3059e7428e95SMichael S. Tsirkin 3060986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 3061986a4f4dSJason Wang vi->has_cvq = true; 3062986a4f4dSJason Wang 306314de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 306414de9d11SAaron Conole mtu = virtio_cread16(vdev, 306514de9d11SAaron Conole offsetof(struct virtio_net_config, 306614de9d11SAaron Conole mtu)); 306793a205eeSAaron Conole if (mtu < dev->min_mtu) { 3068fe36cbe0SMichael S. Tsirkin /* Should never trigger: MTU was previously validated 3069fe36cbe0SMichael S. Tsirkin * in virtnet_validate. 3070fe36cbe0SMichael S. Tsirkin */ 30717934b481SYuval Shaia dev_err(&vdev->dev, 30727934b481SYuval Shaia "device MTU appears to have changed it is now %d < %d", 30737934b481SYuval Shaia mtu, dev->min_mtu); 3074411ea23aSDan Carpenter err = -EINVAL; 3075d7dfc5cfSToshiaki Makita goto free; 3076fe36cbe0SMichael S. Tsirkin } 3077fe36cbe0SMichael S. Tsirkin 3078d0c2c997SJarod Wilson dev->mtu = mtu; 307993a205eeSAaron Conole dev->max_mtu = mtu; 30802e123b44SMichael S. Tsirkin 30812e123b44SMichael S. Tsirkin /* TODO: size buffers correctly in this case. */ 30822e123b44SMichael S. Tsirkin if (dev->mtu > ETH_DATA_LEN) 30832e123b44SMichael S. Tsirkin vi->big_packets = true; 308414de9d11SAaron Conole } 308514de9d11SAaron Conole 3086012873d0SMichael S. Tsirkin if (vi->any_header_sg) 3087012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 30886ebbc1a6SZhangjie \(HZ\) 308944900010SJason Wang /* Enable multiqueue by default */ 309044900010SJason Wang if (num_online_cpus() >= max_queue_pairs) 309144900010SJason Wang vi->curr_queue_pairs = max_queue_pairs; 309244900010SJason Wang else 309344900010SJason Wang vi->curr_queue_pairs = num_online_cpus(); 3094986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 3095986a4f4dSJason Wang 3096986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 30973f9c10b0SAmit Shah err = init_vqs(vi); 3098d2a7dddaSMichael S. Tsirkin if (err) 3099d7dfc5cfSToshiaki Makita goto free; 3100d2a7dddaSMichael S. Tsirkin 3101fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 3102fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 3103fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 3104fbf28d78SMichael Dalton #endif 31050f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 31060f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 3107986a4f4dSJason Wang 310816032be5SNikolay Aleksandrov virtnet_init_settings(dev); 310916032be5SNikolay Aleksandrov 3110ba5e4426SSridhar Samudrala if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3111ba5e4426SSridhar Samudrala vi->failover = net_failover_create(vi->dev); 31124b8e6ac4SWei Yongjun if (IS_ERR(vi->failover)) { 31134b8e6ac4SWei Yongjun err = PTR_ERR(vi->failover); 3114ba5e4426SSridhar Samudrala goto free_vqs; 3115ba5e4426SSridhar Samudrala } 31164b8e6ac4SWei Yongjun } 3117ba5e4426SSridhar Samudrala 3118296f96fcSRusty Russell err = register_netdev(dev); 3119296f96fcSRusty Russell if (err) { 3120296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 3121ba5e4426SSridhar Samudrala goto free_failover; 3122296f96fcSRusty Russell } 3123b3369c1fSRusty Russell 31244baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 31254baf1e33SMichael S. Tsirkin 31268017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 31278de4b2f3SWanlong Gao if (err) { 31288de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 3129f00e35e2Swangyunjian goto free_unregister_netdev; 31308de4b2f3SWanlong Gao } 31318de4b2f3SWanlong Gao 3132a220871bSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 313344900010SJason Wang 3134167c25e4SJason Wang /* Assume link up if device can't report link status, 3135167c25e4SJason Wang otherwise get link status from config. */ 3136167c25e4SJason Wang netif_carrier_off(dev); 3137bda7fab5SJay Vosburgh if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 31383b07e9caSTejun Heo schedule_work(&vi->config_work); 3139167c25e4SJason Wang } else { 3140167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 3141faa9b39fSJason Baron virtnet_update_settings(vi); 31424783256eSPantelis Koukousoulas netif_carrier_on(dev); 3143167c25e4SJason Wang } 31449f4d26d0SMark McLoughlin 31453f93522fSJason Wang for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 31463f93522fSJason Wang if (virtio_has_feature(vi->vdev, guest_offloads[i])) 31473f93522fSJason Wang set_bit(guest_offloads[i], &vi->guest_offloads); 3148a02e8964SWillem de Bruijn vi->guest_offloads_capable = vi->guest_offloads; 31493f93522fSJason Wang 3150986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 3151986a4f4dSJason Wang dev->name, max_queue_pairs); 3152986a4f4dSJason Wang 3153296f96fcSRusty Russell return 0; 3154296f96fcSRusty Russell 3155f00e35e2Swangyunjian free_unregister_netdev: 315602465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 315702465555SMichael S. Tsirkin 3158b3369c1fSRusty Russell unregister_netdev(dev); 3159ba5e4426SSridhar Samudrala free_failover: 3160ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 3161d2a7dddaSMichael S. Tsirkin free_vqs: 3162986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 3163fb51879dSMichael Dalton free_receive_page_frags(vi); 3164e9d7417bSJason Wang virtnet_del_vqs(vi); 3165296f96fcSRusty Russell free: 3166296f96fcSRusty Russell free_netdev(dev); 3167296f96fcSRusty Russell return err; 3168296f96fcSRusty Russell } 3169296f96fcSRusty Russell 317004486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 3171296f96fcSRusty Russell { 317204486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 3173830a8a97SShirley Ma 3174830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 31759ab86bbcSShirley Ma free_unused_bufs(vi); 3176fb6813f4SRusty Russell 3177986a4f4dSJason Wang free_receive_bufs(vi); 3178d2a7dddaSMichael S. Tsirkin 3179fb51879dSMichael Dalton free_receive_page_frags(vi); 3180fb51879dSMichael Dalton 3181986a4f4dSJason Wang virtnet_del_vqs(vi); 318204486ed0SAmit Shah } 318304486ed0SAmit Shah 31848cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 318504486ed0SAmit Shah { 318604486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 318704486ed0SAmit Shah 31888017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 31898de4b2f3SWanlong Gao 3190102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 3191102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 3192586d17c5SJason Wang 319304486ed0SAmit Shah unregister_netdev(vi->dev); 319404486ed0SAmit Shah 3195ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 3196ba5e4426SSridhar Samudrala 319704486ed0SAmit Shah remove_vq_common(vi); 3198fb6813f4SRusty Russell 319974b2553fSRusty Russell free_netdev(vi->dev); 3200296f96fcSRusty Russell } 3201296f96fcSRusty Russell 320267a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 32030741bcb5SAmit Shah { 32040741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 32050741bcb5SAmit Shah 32068017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 32079fe7bfceSJohn Fastabend virtnet_freeze_down(vdev); 32080741bcb5SAmit Shah remove_vq_common(vi); 32090741bcb5SAmit Shah 32100741bcb5SAmit Shah return 0; 32110741bcb5SAmit Shah } 32120741bcb5SAmit Shah 321367a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 32140741bcb5SAmit Shah { 32150741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 32169fe7bfceSJohn Fastabend int err; 32170741bcb5SAmit Shah 32189fe7bfceSJohn Fastabend err = virtnet_restore_up(vdev); 32190741bcb5SAmit Shah if (err) 32200741bcb5SAmit Shah return err; 3221986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 3222986a4f4dSJason Wang 32238017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 3224ec9debbdSJason Wang if (err) 3225ec9debbdSJason Wang return err; 3226ec9debbdSJason Wang 32270741bcb5SAmit Shah return 0; 32280741bcb5SAmit Shah } 32290741bcb5SAmit Shah 3230296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 3231296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 3232296f96fcSRusty Russell { 0 }, 3233296f96fcSRusty Russell }; 3234296f96fcSRusty Russell 3235f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \ 3236f3358507SMichael S. Tsirkin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 3237f3358507SMichael S. Tsirkin VIRTIO_NET_F_MAC, \ 3238f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 3239f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 3240f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 3241f3358507SMichael S. Tsirkin VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 3242f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 3243f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 3244f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_MAC_ADDR, \ 3245faa9b39fSJason Baron VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 32469805069dSSridhar Samudrala VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY 3247f3358507SMichael S. Tsirkin 3248c45a6816SRusty Russell static unsigned int features[] = { 3249f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 3250f3358507SMichael S. Tsirkin }; 3251f3358507SMichael S. Tsirkin 3252f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = { 3253f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 3254f3358507SMichael S. Tsirkin VIRTIO_NET_F_GSO, 3255e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 3256c45a6816SRusty Russell }; 3257c45a6816SRusty Russell 325822402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 3259c45a6816SRusty Russell .feature_table = features, 3260c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 3261f3358507SMichael S. Tsirkin .feature_table_legacy = features_legacy, 3262f3358507SMichael S. Tsirkin .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 3263296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 3264296f96fcSRusty Russell .driver.owner = THIS_MODULE, 3265296f96fcSRusty Russell .id_table = id_table, 3266fe36cbe0SMichael S. Tsirkin .validate = virtnet_validate, 3267296f96fcSRusty Russell .probe = virtnet_probe, 32688cc085d6SBill Pemberton .remove = virtnet_remove, 32699f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 327089107000SAaron Lu #ifdef CONFIG_PM_SLEEP 32710741bcb5SAmit Shah .freeze = virtnet_freeze, 32720741bcb5SAmit Shah .restore = virtnet_restore, 32730741bcb5SAmit Shah #endif 3274296f96fcSRusty Russell }; 3275296f96fcSRusty Russell 32768017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void) 32778017c279SSebastian Andrzej Siewior { 32788017c279SSebastian Andrzej Siewior int ret; 32798017c279SSebastian Andrzej Siewior 328073c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 32818017c279SSebastian Andrzej Siewior virtnet_cpu_online, 32828017c279SSebastian Andrzej Siewior virtnet_cpu_down_prep); 32838017c279SSebastian Andrzej Siewior if (ret < 0) 32848017c279SSebastian Andrzej Siewior goto out; 32858017c279SSebastian Andrzej Siewior virtionet_online = ret; 328673c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 32878017c279SSebastian Andrzej Siewior NULL, virtnet_cpu_dead); 32888017c279SSebastian Andrzej Siewior if (ret) 32898017c279SSebastian Andrzej Siewior goto err_dead; 32908017c279SSebastian Andrzej Siewior 32918017c279SSebastian Andrzej Siewior ret = register_virtio_driver(&virtio_net_driver); 32928017c279SSebastian Andrzej Siewior if (ret) 32938017c279SSebastian Andrzej Siewior goto err_virtio; 32948017c279SSebastian Andrzej Siewior return 0; 32958017c279SSebastian Andrzej Siewior err_virtio: 32968017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 32978017c279SSebastian Andrzej Siewior err_dead: 32988017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 32998017c279SSebastian Andrzej Siewior out: 33008017c279SSebastian Andrzej Siewior return ret; 33018017c279SSebastian Andrzej Siewior } 33028017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init); 33038017c279SSebastian Andrzej Siewior 33048017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void) 33058017c279SSebastian Andrzej Siewior { 3306cfa0ebc9SAndrew Jones unregister_virtio_driver(&virtio_net_driver); 33078017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 33088017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 33098017c279SSebastian Andrzej Siewior } 33108017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit); 3311296f96fcSRusty Russell 3312296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 3313296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 3314296f96fcSRusty Russell MODULE_LICENSE("GPL"); 3315