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 19897c2c69eSXuan Zhuo /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */ 19997c2c69eSXuan Zhuo bool xdp_enabled; 20097c2c69eSXuan Zhuo 20197402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 20297402b96SHerbert Xu bool big_packets; 20397402b96SHerbert Xu 2043f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 2053f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 2063f2c31d9SMark McLoughlin 207986a4f4dSJason Wang /* Has control virtqueue */ 208986a4f4dSJason Wang bool has_cvq; 209986a4f4dSJason Wang 210e7428e95SMichael S. Tsirkin /* Host can handle any s/g split between our header and packet data */ 211e7428e95SMichael S. Tsirkin bool any_header_sg; 212e7428e95SMichael S. Tsirkin 213012873d0SMichael S. Tsirkin /* Packet virtio header size */ 214012873d0SMichael S. Tsirkin u8 hdr_len; 215012873d0SMichael S. Tsirkin 2163161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 2173161e453SRusty Russell struct delayed_work refill; 2183161e453SRusty Russell 219586d17c5SJason Wang /* Work struct for config space updates */ 220586d17c5SJason Wang struct work_struct config_work; 221586d17c5SJason Wang 222986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 223986a4f4dSJason Wang bool affinity_hint_set; 22447be2479SWanlong Gao 2258017c279SSebastian Andrzej Siewior /* CPU hotplug instances for online & dead */ 2268017c279SSebastian Andrzej Siewior struct hlist_node node; 2278017c279SSebastian Andrzej Siewior struct hlist_node node_dead; 2282ac46030SMichael S. Tsirkin 22912e57169SMichael S. Tsirkin struct control_buf *ctrl; 23016032be5SNikolay Aleksandrov 23116032be5SNikolay Aleksandrov /* Ethtool settings */ 23216032be5SNikolay Aleksandrov u8 duplex; 23316032be5SNikolay Aleksandrov u32 speed; 2343f93522fSJason Wang 2353f93522fSJason Wang unsigned long guest_offloads; 236a02e8964SWillem de Bruijn unsigned long guest_offloads_capable; 237ba5e4426SSridhar Samudrala 238ba5e4426SSridhar Samudrala /* failover when STANDBY feature enabled */ 239ba5e4426SSridhar Samudrala struct failover *failover; 240296f96fcSRusty Russell }; 241296f96fcSRusty Russell 2429ab86bbcSShirley Ma struct padded_vnet_hdr { 243012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr; 2449ab86bbcSShirley Ma /* 245012873d0SMichael S. Tsirkin * hdr is in a separate sg buffer, and data sg buffer shares same page 246012873d0SMichael S. Tsirkin * with this header sg. This padding makes next sg 16 byte aligned 247012873d0SMichael S. Tsirkin * after the header. 2489ab86bbcSShirley Ma */ 249012873d0SMichael S. Tsirkin char padding[4]; 2509ab86bbcSShirley Ma }; 2519ab86bbcSShirley Ma 2525050471dSToshiaki Makita static bool is_xdp_frame(void *ptr) 2535050471dSToshiaki Makita { 2545050471dSToshiaki Makita return (unsigned long)ptr & VIRTIO_XDP_FLAG; 2555050471dSToshiaki Makita } 2565050471dSToshiaki Makita 2575050471dSToshiaki Makita static void *xdp_to_ptr(struct xdp_frame *ptr) 2585050471dSToshiaki Makita { 2595050471dSToshiaki Makita return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG); 2605050471dSToshiaki Makita } 2615050471dSToshiaki Makita 2625050471dSToshiaki Makita static struct xdp_frame *ptr_to_xdp(void *ptr) 2635050471dSToshiaki Makita { 2645050471dSToshiaki Makita return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); 2655050471dSToshiaki Makita } 2665050471dSToshiaki Makita 267986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 268986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 269986a4f4dSJason Wang */ 270986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 271986a4f4dSJason Wang { 2729d0ca6edSRusty Russell return (vq->index - 1) / 2; 273986a4f4dSJason Wang } 274986a4f4dSJason Wang 275986a4f4dSJason Wang static int txq2vq(int txq) 276986a4f4dSJason Wang { 277986a4f4dSJason Wang return txq * 2 + 1; 278986a4f4dSJason Wang } 279986a4f4dSJason Wang 280986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 281986a4f4dSJason Wang { 2829d0ca6edSRusty Russell return vq->index / 2; 283986a4f4dSJason Wang } 284986a4f4dSJason Wang 285986a4f4dSJason Wang static int rxq2vq(int rxq) 286986a4f4dSJason Wang { 287986a4f4dSJason Wang return rxq * 2; 288986a4f4dSJason Wang } 289986a4f4dSJason Wang 290012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 291296f96fcSRusty Russell { 292012873d0SMichael S. Tsirkin return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 293296f96fcSRusty Russell } 294296f96fcSRusty Russell 2959ab86bbcSShirley Ma /* 2969ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 2979ab86bbcSShirley Ma * most recent used list in the beginning for reuse 2989ab86bbcSShirley Ma */ 299e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 300fb6813f4SRusty Russell { 3019ab86bbcSShirley Ma struct page *end; 3029ab86bbcSShirley Ma 303e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 3049ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 305e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 306e9d7417bSJason Wang rq->pages = page; 307fb6813f4SRusty Russell } 308fb6813f4SRusty Russell 309e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 310fb6813f4SRusty Russell { 311e9d7417bSJason Wang struct page *p = rq->pages; 312fb6813f4SRusty Russell 3139ab86bbcSShirley Ma if (p) { 314e9d7417bSJason Wang rq->pages = (struct page *)p->private; 3159ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 3169ab86bbcSShirley Ma p->private = 0; 3179ab86bbcSShirley Ma } else 318fb6813f4SRusty Russell p = alloc_page(gfp_mask); 319fb6813f4SRusty Russell return p; 320fb6813f4SRusty Russell } 321fb6813f4SRusty Russell 322e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi, 323e4e8452aSWillem de Bruijn struct virtqueue *vq) 324e4e8452aSWillem de Bruijn { 325e4e8452aSWillem de Bruijn if (napi_schedule_prep(napi)) { 326e4e8452aSWillem de Bruijn virtqueue_disable_cb(vq); 327e4e8452aSWillem de Bruijn __napi_schedule(napi); 328e4e8452aSWillem de Bruijn } 329e4e8452aSWillem de Bruijn } 330e4e8452aSWillem de Bruijn 331e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi, 332e4e8452aSWillem de Bruijn struct virtqueue *vq, int processed) 333e4e8452aSWillem de Bruijn { 334e4e8452aSWillem de Bruijn int opaque; 335e4e8452aSWillem de Bruijn 336e4e8452aSWillem de Bruijn opaque = virtqueue_enable_cb_prepare(vq); 337fdaa767aSToshiaki Makita if (napi_complete_done(napi, processed)) { 338fdaa767aSToshiaki Makita if (unlikely(virtqueue_poll(vq, opaque))) 339e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 340fdaa767aSToshiaki Makita } else { 341fdaa767aSToshiaki Makita virtqueue_disable_cb(vq); 342fdaa767aSToshiaki Makita } 343e4e8452aSWillem de Bruijn } 344e4e8452aSWillem de Bruijn 345e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 346296f96fcSRusty Russell { 347e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 348b92f1e67SWillem de Bruijn struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 349296f96fcSRusty Russell 3502cb9c6baSRusty Russell /* Suppress further interrupts. */ 351e9d7417bSJason Wang virtqueue_disable_cb(vq); 35211a3a154SRusty Russell 353b92f1e67SWillem de Bruijn if (napi->weight) 354b92f1e67SWillem de Bruijn virtqueue_napi_schedule(napi, vq); 355b92f1e67SWillem de Bruijn else 356363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 357986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 358296f96fcSRusty Russell } 359296f96fcSRusty Russell 36028b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22 36128b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize, 36228b39bc7SJason Wang unsigned int headroom) 36328b39bc7SJason Wang { 36428b39bc7SJason Wang return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 36528b39bc7SJason Wang } 36628b39bc7SJason Wang 36728b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 36828b39bc7SJason Wang { 36928b39bc7SJason Wang return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 37028b39bc7SJason Wang } 37128b39bc7SJason Wang 37228b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 37328b39bc7SJason Wang { 37428b39bc7SJason Wang return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 37528b39bc7SJason Wang } 37628b39bc7SJason Wang 3773464645aSMike Waychison /* Called from bottom half context */ 378946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 379946fa564SMichael S. Tsirkin struct receive_queue *rq, 3802613af0eSMichael Dalton struct page *page, unsigned int offset, 381436c9453SJason Wang unsigned int len, unsigned int truesize, 382fb32856bSXuan Zhuo bool hdr_valid, unsigned int metasize, 3836c66c147SXuan Zhuo bool whole_page) 3849ab86bbcSShirley Ma { 3859ab86bbcSShirley Ma struct sk_buff *skb; 386012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 3872613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 388af39c8f7SEric Dumazet struct page *page_to_free = NULL; 389fb32856bSXuan Zhuo int tailroom, shinfo_size; 390f80bd740SXuan Zhuo char *p, *hdr_p, *buf; 3919ab86bbcSShirley Ma 3922613af0eSMichael Dalton p = page_address(page) + offset; 393fb32856bSXuan Zhuo hdr_p = p; 3949ab86bbcSShirley Ma 395012873d0SMichael S. Tsirkin hdr_len = vi->hdr_len; 396012873d0SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 397a4a76503Sstephen hemminger hdr_padded_len = sizeof(*hdr); 398012873d0SMichael S. Tsirkin else 3992613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 4003f2c31d9SMark McLoughlin 4016c66c147SXuan Zhuo /* If whole_page, there is an offset between the beginning of the 402fb32856bSXuan Zhuo * data and the allocated space, otherwise the data and the allocated 403fb32856bSXuan Zhuo * space are aligned. 4048fb7da9eSXuan Zhuo * 4058fb7da9eSXuan Zhuo * Buffers with headroom use PAGE_SIZE as alloc size, see 4068fb7da9eSXuan Zhuo * add_recvbuf_mergeable() + get_mergeable_buf_len() 407fb32856bSXuan Zhuo */ 4086c66c147SXuan Zhuo if (whole_page) { 4096c66c147SXuan Zhuo /* Buffers with whole_page use PAGE_SIZE as alloc size, 410f80bd740SXuan Zhuo * see add_recvbuf_mergeable() + get_mergeable_buf_len() 411f80bd740SXuan Zhuo */ 412fb32856bSXuan Zhuo truesize = PAGE_SIZE; 4137bf64460SXuan Zhuo 4147bf64460SXuan Zhuo /* page maybe head page, so we should get the buf by p, not the 4157bf64460SXuan Zhuo * page 4167bf64460SXuan Zhuo */ 4177bf64460SXuan Zhuo tailroom = truesize - len - offset_in_page(p); 4187bf64460SXuan Zhuo buf = (char *)((unsigned long)p & PAGE_MASK); 419fb32856bSXuan Zhuo } else { 420fb32856bSXuan Zhuo tailroom = truesize - len; 421f80bd740SXuan Zhuo buf = p; 422fb32856bSXuan Zhuo } 4233f2c31d9SMark McLoughlin 4249ab86bbcSShirley Ma len -= hdr_len; 4252613af0eSMichael Dalton offset += hdr_padded_len; 4262613af0eSMichael Dalton p += hdr_padded_len; 4273f2c31d9SMark McLoughlin 428fb32856bSXuan Zhuo shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 429fb32856bSXuan Zhuo 430f80bd740SXuan Zhuo /* copy small packet so we can reuse these pages */ 431f5d7872aSEric Dumazet if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) { 432f80bd740SXuan Zhuo skb = build_skb(buf, truesize); 433fb32856bSXuan Zhuo if (unlikely(!skb)) 434fb32856bSXuan Zhuo return NULL; 435fb32856bSXuan Zhuo 436f80bd740SXuan Zhuo skb_reserve(skb, p - buf); 437fb32856bSXuan Zhuo skb_put(skb, len); 438fb32856bSXuan Zhuo goto ok; 439fb32856bSXuan Zhuo } 440fb32856bSXuan Zhuo 441fb32856bSXuan Zhuo /* copy small packet so we can reuse these pages for small data */ 442fb32856bSXuan Zhuo skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 443fb32856bSXuan Zhuo if (unlikely(!skb)) 444fb32856bSXuan Zhuo return NULL; 445fb32856bSXuan Zhuo 4460f6925b3SEric Dumazet /* Copy all frame if it fits skb->head, otherwise 4470f6925b3SEric Dumazet * we let virtio_net_hdr_to_skb() and GRO pull headers as needed. 4480f6925b3SEric Dumazet */ 4490f6925b3SEric Dumazet if (len <= skb_tailroom(skb)) 4503f2c31d9SMark McLoughlin copy = len; 4510f6925b3SEric Dumazet else 4520f6925b3SEric Dumazet copy = ETH_HLEN + metasize; 45359ae1d12SJohannes Berg skb_put_data(skb, p, copy); 4543f2c31d9SMark McLoughlin 4553f2c31d9SMark McLoughlin len -= copy; 4569ab86bbcSShirley Ma offset += copy; 4573f2c31d9SMark McLoughlin 4582613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 4592613af0eSMichael Dalton if (len) 4602613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 4612613af0eSMichael Dalton else 462af39c8f7SEric Dumazet page_to_free = page; 463fb32856bSXuan Zhuo goto ok; 4642613af0eSMichael Dalton } 4652613af0eSMichael Dalton 466e878d78bSSasha Levin /* 467e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 468e878d78bSSasha Levin * This is here to handle cases when the device erroneously 469e878d78bSSasha Levin * tries to receive more than is possible. This is usually 470e878d78bSSasha Levin * the case of a broken device. 471e878d78bSSasha Levin */ 472e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 473be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 474e878d78bSSasha Levin dev_kfree_skb(skb); 475e878d78bSSasha Levin return NULL; 476e878d78bSSasha Levin } 4772613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 4789ab86bbcSShirley Ma while (len) { 4792613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 4802613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 4812613af0eSMichael Dalton frag_size, truesize); 4822613af0eSMichael Dalton len -= frag_size; 4839ab86bbcSShirley Ma page = (struct page *)page->private; 4849ab86bbcSShirley Ma offset = 0; 4853f2c31d9SMark McLoughlin } 4863f2c31d9SMark McLoughlin 4879ab86bbcSShirley Ma if (page) 488e9d7417bSJason Wang give_pages(rq, page); 4893f2c31d9SMark McLoughlin 490fb32856bSXuan Zhuo ok: 491fb32856bSXuan Zhuo /* hdr_valid means no XDP, so we can copy the vnet header */ 492fb32856bSXuan Zhuo if (hdr_valid) { 493fb32856bSXuan Zhuo hdr = skb_vnet_hdr(skb); 494fb32856bSXuan Zhuo memcpy(hdr, hdr_p, hdr_len); 495fb32856bSXuan Zhuo } 496af39c8f7SEric Dumazet if (page_to_free) 497af39c8f7SEric Dumazet put_page(page_to_free); 498fb32856bSXuan Zhuo 499fb32856bSXuan Zhuo if (metasize) { 500fb32856bSXuan Zhuo __skb_pull(skb, metasize); 501fb32856bSXuan Zhuo skb_metadata_set(skb, metasize); 502fb32856bSXuan Zhuo } 503fb32856bSXuan Zhuo 5049ab86bbcSShirley Ma return skb; 5059ab86bbcSShirley Ma } 5069ab86bbcSShirley Ma 507735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, 508735fc405SJesper Dangaard Brouer struct send_queue *sq, 50944fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf) 51056434a01SJohn Fastabend { 51156434a01SJohn Fastabend struct virtio_net_hdr_mrg_rxbuf *hdr; 51256434a01SJohn Fastabend int err; 51356434a01SJohn Fastabend 514cac320c8SJesper Dangaard Brouer if (unlikely(xdpf->headroom < vi->hdr_len)) 515cac320c8SJesper Dangaard Brouer return -EOVERFLOW; 516cac320c8SJesper Dangaard Brouer 517cac320c8SJesper Dangaard Brouer /* Make room for virtqueue hdr (also change xdpf->headroom?) */ 518cac320c8SJesper Dangaard Brouer xdpf->data -= vi->hdr_len; 51956434a01SJohn Fastabend /* Zero header and leave csum up to XDP layers */ 520cac320c8SJesper Dangaard Brouer hdr = xdpf->data; 52156434a01SJohn Fastabend memset(hdr, 0, vi->hdr_len); 522cac320c8SJesper Dangaard Brouer xdpf->len += vi->hdr_len; 52356434a01SJohn Fastabend 524cac320c8SJesper Dangaard Brouer sg_init_one(sq->sg, xdpf->data, xdpf->len); 525bb91accfSJason Wang 5265050471dSToshiaki Makita err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf), 5275050471dSToshiaki Makita GFP_ATOMIC); 52811b7d897SJesper Dangaard Brouer if (unlikely(err)) 529cac320c8SJesper Dangaard Brouer return -ENOSPC; /* Caller handle free/refcnt */ 53056434a01SJohn Fastabend 531cac320c8SJesper Dangaard Brouer return 0; 53256434a01SJohn Fastabend } 53356434a01SJohn Fastabend 53497c2c69eSXuan Zhuo /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on 53597c2c69eSXuan Zhuo * the current cpu, so it does not need to be locked. 53697c2c69eSXuan Zhuo * 53797c2c69eSXuan Zhuo * Here we use marco instead of inline functions because we have to deal with 53897c2c69eSXuan Zhuo * three issues at the same time: 1. the choice of sq. 2. judge and execute the 53997c2c69eSXuan Zhuo * lock/unlock of txq 3. make sparse happy. It is difficult for two inline 54097c2c69eSXuan Zhuo * functions to perfectly solve these three problems at the same time. 54197c2c69eSXuan Zhuo */ 54297c2c69eSXuan Zhuo #define virtnet_xdp_get_sq(vi) ({ \ 54397c2c69eSXuan Zhuo struct netdev_queue *txq; \ 54497c2c69eSXuan Zhuo typeof(vi) v = (vi); \ 54597c2c69eSXuan Zhuo unsigned int qp; \ 54697c2c69eSXuan Zhuo \ 54797c2c69eSXuan Zhuo if (v->curr_queue_pairs > nr_cpu_ids) { \ 54897c2c69eSXuan Zhuo qp = v->curr_queue_pairs - v->xdp_queue_pairs; \ 54997c2c69eSXuan Zhuo qp += smp_processor_id(); \ 55097c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, qp); \ 55197c2c69eSXuan Zhuo __netif_tx_acquire(txq); \ 55297c2c69eSXuan Zhuo } else { \ 55397c2c69eSXuan Zhuo qp = smp_processor_id() % v->curr_queue_pairs; \ 55497c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, qp); \ 55597c2c69eSXuan Zhuo __netif_tx_lock(txq, raw_smp_processor_id()); \ 55697c2c69eSXuan Zhuo } \ 55797c2c69eSXuan Zhuo v->sq + qp; \ 55897c2c69eSXuan Zhuo }) 5592a43565cSToshiaki Makita 56097c2c69eSXuan Zhuo #define virtnet_xdp_put_sq(vi, q) { \ 56197c2c69eSXuan Zhuo struct netdev_queue *txq; \ 56297c2c69eSXuan Zhuo typeof(vi) v = (vi); \ 56397c2c69eSXuan Zhuo \ 56497c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \ 56597c2c69eSXuan Zhuo if (v->curr_queue_pairs > nr_cpu_ids) \ 56697c2c69eSXuan Zhuo __netif_tx_release(txq); \ 56797c2c69eSXuan Zhuo else \ 56897c2c69eSXuan Zhuo __netif_tx_unlock(txq); \ 5692a43565cSToshiaki Makita } 5702a43565cSToshiaki Makita 571735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev, 57242b33468SJesper Dangaard Brouer int n, struct xdp_frame **frames, u32 flags) 573186b3c99SJason Wang { 574186b3c99SJason Wang struct virtnet_info *vi = netdev_priv(dev); 5758dcc5b0aSJesper Dangaard Brouer struct receive_queue *rq = vi->rq; 5768dcc5b0aSJesper Dangaard Brouer struct bpf_prog *xdp_prog; 577735fc405SJesper Dangaard Brouer struct send_queue *sq; 578735fc405SJesper Dangaard Brouer unsigned int len; 579546f2897SToshiaki Makita int packets = 0; 580546f2897SToshiaki Makita int bytes = 0; 581fdc13979SLorenzo Bianconi int nxmit = 0; 582461f03dcSToshiaki Makita int kicks = 0; 5835050471dSToshiaki Makita void *ptr; 584fdc13979SLorenzo Bianconi int ret; 585735fc405SJesper Dangaard Brouer int i; 586735fc405SJesper Dangaard Brouer 5878dcc5b0aSJesper Dangaard Brouer /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 5888dcc5b0aSJesper Dangaard Brouer * indicate XDP resources have been successfully allocated. 5898dcc5b0aSJesper Dangaard Brouer */ 5909719c6b9SJohn Fastabend xdp_prog = rcu_access_pointer(rq->xdp_prog); 5911667c08aSToshiaki Makita if (!xdp_prog) 5921667c08aSToshiaki Makita return -ENXIO; 5931667c08aSToshiaki Makita 59497c2c69eSXuan Zhuo sq = virtnet_xdp_get_sq(vi); 5959ab86bbcSShirley Ma 5969ab86bbcSShirley Ma if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { 597186b3c99SJason Wang ret = -EINVAL; 598186b3c99SJason Wang goto out; 599186b3c99SJason Wang } 600186b3c99SJason Wang 601735fc405SJesper Dangaard Brouer /* Free up any pending old buffers before queueing new ones. */ 6025050471dSToshiaki Makita while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 603546f2897SToshiaki Makita if (likely(is_xdp_frame(ptr))) { 604546f2897SToshiaki Makita struct xdp_frame *frame = ptr_to_xdp(ptr); 605546f2897SToshiaki Makita 606546f2897SToshiaki Makita bytes += frame->len; 607546f2897SToshiaki Makita xdp_return_frame(frame); 608546f2897SToshiaki Makita } else { 609546f2897SToshiaki Makita struct sk_buff *skb = ptr; 610546f2897SToshiaki Makita 611546f2897SToshiaki Makita bytes += skb->len; 612546f2897SToshiaki Makita napi_consume_skb(skb, false); 613546f2897SToshiaki Makita } 614546f2897SToshiaki Makita packets++; 6155050471dSToshiaki Makita } 616735fc405SJesper Dangaard Brouer 617735fc405SJesper Dangaard Brouer for (i = 0; i < n; i++) { 618735fc405SJesper Dangaard Brouer struct xdp_frame *xdpf = frames[i]; 619735fc405SJesper Dangaard Brouer 620fdc13979SLorenzo Bianconi if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) 621fdc13979SLorenzo Bianconi break; 622fdc13979SLorenzo Bianconi nxmit++; 623735fc405SJesper Dangaard Brouer } 624fdc13979SLorenzo Bianconi ret = nxmit; 6255d274cb4SJesper Dangaard Brouer 626461f03dcSToshiaki Makita if (flags & XDP_XMIT_FLUSH) { 627461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) 628461f03dcSToshiaki Makita kicks = 1; 629461f03dcSToshiaki Makita } 6305b8f3c8dSToshiaki Makita out: 6315b8f3c8dSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 632546f2897SToshiaki Makita sq->stats.bytes += bytes; 633546f2897SToshiaki Makita sq->stats.packets += packets; 6345b8f3c8dSToshiaki Makita sq->stats.xdp_tx += n; 635fdc13979SLorenzo Bianconi sq->stats.xdp_tx_drops += n - nxmit; 636461f03dcSToshiaki Makita sq->stats.kicks += kicks; 6375b8f3c8dSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 6385d274cb4SJesper Dangaard Brouer 63997c2c69eSXuan Zhuo virtnet_xdp_put_sq(vi, sq); 6405b8f3c8dSToshiaki Makita return ret; 641186b3c99SJason Wang } 642186b3c99SJason Wang 643f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 644f6b10209SJason Wang { 64597c2c69eSXuan Zhuo return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; 646f6b10209SJason Wang } 647f6b10209SJason Wang 6484941d472SJason Wang /* We copy the packet for XDP in the following cases: 6494941d472SJason Wang * 6504941d472SJason Wang * 1) Packet is scattered across multiple rx buffers. 6514941d472SJason Wang * 2) Headroom space is insufficient. 6524941d472SJason Wang * 6534941d472SJason Wang * This is inefficient but it's a temporary condition that 6544941d472SJason Wang * we hit right after XDP is enabled and until queue is refilled 6554941d472SJason Wang * with large buffers with sufficient headroom - so it should affect 6564941d472SJason Wang * at most queue size packets. 6574941d472SJason Wang * Afterwards, the conditions to enable 6584941d472SJason Wang * XDP should preclude the underlying device from sending packets 6594941d472SJason Wang * across multiple buffers (num_buf > 1), and we make sure buffers 6604941d472SJason Wang * have enough headroom. 66172979a6cSJohn Fastabend */ 66272979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq, 66356a86f84SJason Wang u16 *num_buf, 66472979a6cSJohn Fastabend struct page *p, 66572979a6cSJohn Fastabend int offset, 6664941d472SJason Wang int page_off, 66772979a6cSJohn Fastabend unsigned int *len) 66872979a6cSJohn Fastabend { 66972979a6cSJohn Fastabend struct page *page = alloc_page(GFP_ATOMIC); 67072979a6cSJohn Fastabend 67172979a6cSJohn Fastabend if (!page) 67272979a6cSJohn Fastabend return NULL; 67372979a6cSJohn Fastabend 67472979a6cSJohn Fastabend memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 67572979a6cSJohn Fastabend page_off += *len; 67672979a6cSJohn Fastabend 67756a86f84SJason Wang while (--*num_buf) { 6783cc81a9aSJason Wang int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 67972979a6cSJohn Fastabend unsigned int buflen; 68072979a6cSJohn Fastabend void *buf; 68172979a6cSJohn Fastabend int off; 68272979a6cSJohn Fastabend 683680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &buflen); 684680557cfSMichael S. Tsirkin if (unlikely(!buf)) 68572979a6cSJohn Fastabend goto err_buf; 68672979a6cSJohn Fastabend 68772979a6cSJohn Fastabend p = virt_to_head_page(buf); 68872979a6cSJohn Fastabend off = buf - page_address(p); 68972979a6cSJohn Fastabend 69056a86f84SJason Wang /* guard against a misconfigured or uncooperative backend that 69156a86f84SJason Wang * is sending packet larger than the MTU. 69256a86f84SJason Wang */ 6933cc81a9aSJason Wang if ((page_off + buflen + tailroom) > PAGE_SIZE) { 69456a86f84SJason Wang put_page(p); 69556a86f84SJason Wang goto err_buf; 69656a86f84SJason Wang } 69756a86f84SJason Wang 69872979a6cSJohn Fastabend memcpy(page_address(page) + page_off, 69972979a6cSJohn Fastabend page_address(p) + off, buflen); 70072979a6cSJohn Fastabend page_off += buflen; 70156a86f84SJason Wang put_page(p); 70272979a6cSJohn Fastabend } 70372979a6cSJohn Fastabend 7042de2f7f4SJohn Fastabend /* Headroom does not contribute to packet length */ 7052de2f7f4SJohn Fastabend *len = page_off - VIRTIO_XDP_HEADROOM; 70672979a6cSJohn Fastabend return page; 70772979a6cSJohn Fastabend err_buf: 70872979a6cSJohn Fastabend __free_pages(page, 0); 70972979a6cSJohn Fastabend return NULL; 71072979a6cSJohn Fastabend } 71172979a6cSJohn Fastabend 7124941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev, 7134941d472SJason Wang struct virtnet_info *vi, 7144941d472SJason Wang struct receive_queue *rq, 7154941d472SJason Wang void *buf, void *ctx, 716186b3c99SJason Wang unsigned int len, 7177d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 718d46eeeafSJason Wang struct virtnet_rq_stats *stats) 7194941d472SJason Wang { 7204941d472SJason Wang struct sk_buff *skb; 7214941d472SJason Wang struct bpf_prog *xdp_prog; 7224941d472SJason Wang unsigned int xdp_headroom = (unsigned long)ctx; 7234941d472SJason Wang unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 7244941d472SJason Wang unsigned int headroom = vi->hdr_len + header_offset; 7254941d472SJason Wang unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 7264941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 7274941d472SJason Wang struct page *page = virt_to_head_page(buf); 72811b7d897SJesper Dangaard Brouer unsigned int delta = 0; 7294941d472SJason Wang struct page *xdp_page; 73011b7d897SJesper Dangaard Brouer int err; 731503d539aSYuya Kusakabe unsigned int metasize = 0; 73211b7d897SJesper Dangaard Brouer 7334941d472SJason Wang len -= vi->hdr_len; 734d46eeeafSJason Wang stats->bytes += len; 7354941d472SJason Wang 736ad993a95SXie Yongji if (unlikely(len > GOOD_PACKET_LEN)) { 737ad993a95SXie Yongji pr_debug("%s: rx error: len %u exceeds max size %d\n", 738ad993a95SXie Yongji dev->name, len, GOOD_PACKET_LEN); 739ad993a95SXie Yongji dev->stats.rx_length_errors++; 740ad993a95SXie Yongji goto err_len; 741ad993a95SXie Yongji } 7424941d472SJason Wang rcu_read_lock(); 7434941d472SJason Wang xdp_prog = rcu_dereference(rq->xdp_prog); 7444941d472SJason Wang if (xdp_prog) { 7454941d472SJason Wang struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 74644fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 7474941d472SJason Wang struct xdp_buff xdp; 7484941d472SJason Wang void *orig_data; 7494941d472SJason Wang u32 act; 7504941d472SJason Wang 75195dbe9e7SJesper Dangaard Brouer if (unlikely(hdr->hdr.gso_type)) 7524941d472SJason Wang goto err_xdp; 7534941d472SJason Wang 7544941d472SJason Wang if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 7554941d472SJason Wang int offset = buf - page_address(page) + header_offset; 7564941d472SJason Wang unsigned int tlen = len + vi->hdr_len; 7574941d472SJason Wang u16 num_buf = 1; 7584941d472SJason Wang 7594941d472SJason Wang xdp_headroom = virtnet_get_headroom(vi); 7604941d472SJason Wang header_offset = VIRTNET_RX_PAD + xdp_headroom; 7614941d472SJason Wang headroom = vi->hdr_len + header_offset; 7624941d472SJason Wang buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 7634941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 7644941d472SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, page, 7654941d472SJason Wang offset, header_offset, 7664941d472SJason Wang &tlen); 7674941d472SJason Wang if (!xdp_page) 7684941d472SJason Wang goto err_xdp; 7694941d472SJason Wang 7704941d472SJason Wang buf = page_address(xdp_page); 7714941d472SJason Wang put_page(page); 7724941d472SJason Wang page = xdp_page; 7734941d472SJason Wang } 7744941d472SJason Wang 77543b5169dSLorenzo Bianconi xdp_init_buff(&xdp, buflen, &rq->xdp_rxq); 776be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, 777be9df4afSLorenzo Bianconi xdp_headroom, len, true); 7784941d472SJason Wang orig_data = xdp.data; 7794941d472SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 780d46eeeafSJason Wang stats->xdp_packets++; 7814941d472SJason Wang 7824941d472SJason Wang switch (act) { 7834941d472SJason Wang case XDP_PASS: 7844941d472SJason Wang /* Recalculate length in case bpf program changed it */ 7854941d472SJason Wang delta = orig_data - xdp.data; 7866870de43SNikita V. Shirokov len = xdp.data_end - xdp.data; 787503d539aSYuya Kusakabe metasize = xdp.data - xdp.data_meta; 7884941d472SJason Wang break; 7894941d472SJason Wang case XDP_TX: 790d46eeeafSJason Wang stats->xdp_tx++; 7911b698fa5SLorenzo Bianconi xdpf = xdp_convert_buff_to_frame(&xdp); 79244fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 79344fa2dbdSJesper Dangaard Brouer goto err_xdp; 794ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 795fdc13979SLorenzo Bianconi if (unlikely(!err)) { 796fdc13979SLorenzo Bianconi xdp_return_frame_rx_napi(xdpf); 797fdc13979SLorenzo Bianconi } else if (unlikely(err < 0)) { 7984941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 79911b7d897SJesper Dangaard Brouer goto err_xdp; 80011b7d897SJesper Dangaard Brouer } 8012471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 802186b3c99SJason Wang rcu_read_unlock(); 803186b3c99SJason Wang goto xdp_xmit; 804186b3c99SJason Wang case XDP_REDIRECT: 805d46eeeafSJason Wang stats->xdp_redirects++; 806186b3c99SJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 80711b7d897SJesper Dangaard Brouer if (err) 80811b7d897SJesper Dangaard Brouer goto err_xdp; 8092471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 8104941d472SJason Wang rcu_read_unlock(); 8114941d472SJason Wang goto xdp_xmit; 8124941d472SJason Wang default: 8134941d472SJason Wang bpf_warn_invalid_xdp_action(act); 814df561f66SGustavo A. R. Silva fallthrough; 8154941d472SJason Wang case XDP_ABORTED: 8164941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 81795efabf0SGustavo A. R. Silva goto err_xdp; 8184941d472SJason Wang case XDP_DROP: 8194941d472SJason Wang goto err_xdp; 8204941d472SJason Wang } 8214941d472SJason Wang } 8224941d472SJason Wang rcu_read_unlock(); 8234941d472SJason Wang 8244941d472SJason Wang skb = build_skb(buf, buflen); 8254941d472SJason Wang if (!skb) { 8264941d472SJason Wang put_page(page); 8274941d472SJason Wang goto err; 8284941d472SJason Wang } 8294941d472SJason Wang skb_reserve(skb, headroom - delta); 8306870de43SNikita V. Shirokov skb_put(skb, len); 831f1d4884dSYuya Kusakabe if (!xdp_prog) { 8324941d472SJason Wang buf += header_offset; 8334941d472SJason Wang memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 834f1d4884dSYuya Kusakabe } /* keep zeroed vnet hdr since XDP is loaded */ 8354941d472SJason Wang 836503d539aSYuya Kusakabe if (metasize) 837503d539aSYuya Kusakabe skb_metadata_set(skb, metasize); 838503d539aSYuya Kusakabe 8394941d472SJason Wang err: 8404941d472SJason Wang return skb; 8414941d472SJason Wang 8424941d472SJason Wang err_xdp: 8434941d472SJason Wang rcu_read_unlock(); 844d46eeeafSJason Wang stats->xdp_drops++; 845ad993a95SXie Yongji err_len: 846d46eeeafSJason Wang stats->drops++; 8474941d472SJason Wang put_page(page); 8484941d472SJason Wang xdp_xmit: 8494941d472SJason Wang return NULL; 8504941d472SJason Wang } 8514941d472SJason Wang 8524941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev, 8534941d472SJason Wang struct virtnet_info *vi, 8544941d472SJason Wang struct receive_queue *rq, 8554941d472SJason Wang void *buf, 8567d9d60fdSToshiaki Makita unsigned int len, 857d46eeeafSJason Wang struct virtnet_rq_stats *stats) 8584941d472SJason Wang { 8594941d472SJason Wang struct page *page = buf; 860503d539aSYuya Kusakabe struct sk_buff *skb = 861fb32856bSXuan Zhuo page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0); 8624941d472SJason Wang 863d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 8644941d472SJason Wang if (unlikely(!skb)) 8654941d472SJason Wang goto err; 8664941d472SJason Wang 8674941d472SJason Wang return skb; 8684941d472SJason Wang 8694941d472SJason Wang err: 870d46eeeafSJason Wang stats->drops++; 8714941d472SJason Wang give_pages(rq, page); 8724941d472SJason Wang return NULL; 8734941d472SJason Wang } 8744941d472SJason Wang 8758fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 876fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 8778fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 878680557cfSMichael S. Tsirkin void *buf, 879680557cfSMichael S. Tsirkin void *ctx, 880186b3c99SJason Wang unsigned int len, 8817d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 882d46eeeafSJason Wang struct virtnet_rq_stats *stats) 8839ab86bbcSShirley Ma { 884012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 885012873d0SMichael S. Tsirkin u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 8868fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 8878fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 888f600b690SJohn Fastabend struct sk_buff *head_skb, *curr_skb; 889f600b690SJohn Fastabend struct bpf_prog *xdp_prog; 8909ce6146eSJesper Dangaard Brouer unsigned int truesize = mergeable_ctx_to_truesize(ctx); 8914941d472SJason Wang unsigned int headroom = mergeable_ctx_to_headroom(ctx); 892503d539aSYuya Kusakabe unsigned int metasize = 0; 8939ce6146eSJesper Dangaard Brouer unsigned int frame_sz; 8949ce6146eSJesper Dangaard Brouer int err; 895ab7db917SMichael Dalton 89656434a01SJohn Fastabend head_skb = NULL; 897d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 89856434a01SJohn Fastabend 899ad993a95SXie Yongji if (unlikely(len > truesize)) { 900ad993a95SXie Yongji pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 901ad993a95SXie Yongji dev->name, len, (unsigned long)ctx); 902ad993a95SXie Yongji dev->stats.rx_length_errors++; 903ad993a95SXie Yongji goto err_skb; 904ad993a95SXie Yongji } 905f600b690SJohn Fastabend rcu_read_lock(); 906f600b690SJohn Fastabend xdp_prog = rcu_dereference(rq->xdp_prog); 907f600b690SJohn Fastabend if (xdp_prog) { 90844fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 90972979a6cSJohn Fastabend struct page *xdp_page; 9100354e4d1SJohn Fastabend struct xdp_buff xdp; 9110354e4d1SJohn Fastabend void *data; 912f600b690SJohn Fastabend u32 act; 913f600b690SJohn Fastabend 9143d62b2a0SJason Wang /* Transient failure which in theory could occur if 9153d62b2a0SJason Wang * in-flight packets from before XDP was enabled reach 9163d62b2a0SJason Wang * the receive path after XDP is loaded. 9173d62b2a0SJason Wang */ 9183d62b2a0SJason Wang if (unlikely(hdr->hdr.gso_type)) 9193d62b2a0SJason Wang goto err_xdp; 9203d62b2a0SJason Wang 9219ce6146eSJesper Dangaard Brouer /* Buffers with headroom use PAGE_SIZE as alloc size, 9229ce6146eSJesper Dangaard Brouer * see add_recvbuf_mergeable() + get_mergeable_buf_len() 9239ce6146eSJesper Dangaard Brouer */ 9249ce6146eSJesper Dangaard Brouer frame_sz = headroom ? PAGE_SIZE : truesize; 9259ce6146eSJesper Dangaard Brouer 9263cc81a9aSJason Wang /* This happens when rx buffer size is underestimated 9273cc81a9aSJason Wang * or headroom is not enough because of the buffer 9283cc81a9aSJason Wang * was refilled before XDP is set. This should only 9293cc81a9aSJason Wang * happen for the first several packets, so we don't 9303cc81a9aSJason Wang * care much about its performance. 9313cc81a9aSJason Wang */ 9324941d472SJason Wang if (unlikely(num_buf > 1 || 9334941d472SJason Wang headroom < virtnet_get_headroom(vi))) { 93472979a6cSJohn Fastabend /* linearize data for XDP */ 93556a86f84SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, 9364941d472SJason Wang page, offset, 9374941d472SJason Wang VIRTIO_XDP_HEADROOM, 9384941d472SJason Wang &len); 9399ce6146eSJesper Dangaard Brouer frame_sz = PAGE_SIZE; 9409ce6146eSJesper Dangaard Brouer 94172979a6cSJohn Fastabend if (!xdp_page) 942f600b690SJohn Fastabend goto err_xdp; 9432de2f7f4SJohn Fastabend offset = VIRTIO_XDP_HEADROOM; 94472979a6cSJohn Fastabend } else { 94572979a6cSJohn Fastabend xdp_page = page; 946f600b690SJohn Fastabend } 947f600b690SJohn Fastabend 9482de2f7f4SJohn Fastabend /* Allow consuming headroom but reserve enough space to push 9492de2f7f4SJohn Fastabend * the descriptor on if we get an XDP_TX return code. 9502de2f7f4SJohn Fastabend */ 9510354e4d1SJohn Fastabend data = page_address(xdp_page) + offset; 95243b5169dSLorenzo Bianconi xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq); 953be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len, 954be9df4afSLorenzo Bianconi VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true); 955754b8a21SJesper Dangaard Brouer 9560354e4d1SJohn Fastabend act = bpf_prog_run_xdp(xdp_prog, &xdp); 957d46eeeafSJason Wang stats->xdp_packets++; 9580354e4d1SJohn Fastabend 95956434a01SJohn Fastabend switch (act) { 96056434a01SJohn Fastabend case XDP_PASS: 961503d539aSYuya Kusakabe metasize = xdp.data - xdp.data_meta; 9622de2f7f4SJohn Fastabend 963503d539aSYuya Kusakabe /* recalculate offset to account for any header 964503d539aSYuya Kusakabe * adjustments and minus the metasize to copy the 965503d539aSYuya Kusakabe * metadata in page_to_skb(). Note other cases do not 966503d539aSYuya Kusakabe * build an skb and avoid using offset 9676870de43SNikita V. Shirokov */ 968503d539aSYuya Kusakabe offset = xdp.data - page_address(xdp_page) - 969503d539aSYuya Kusakabe vi->hdr_len - metasize; 970503d539aSYuya Kusakabe 971503d539aSYuya Kusakabe /* recalculate len if xdp.data, xdp.data_end or 972503d539aSYuya Kusakabe * xdp.data_meta were adjusted 973503d539aSYuya Kusakabe */ 974503d539aSYuya Kusakabe len = xdp.data_end - xdp.data + vi->hdr_len + metasize; 9751830f893SJason Wang /* We can only create skb based on xdp_page. */ 9761830f893SJason Wang if (unlikely(xdp_page != page)) { 9771830f893SJason Wang rcu_read_unlock(); 9781830f893SJason Wang put_page(page); 979503d539aSYuya Kusakabe head_skb = page_to_skb(vi, rq, xdp_page, offset, 980503d539aSYuya Kusakabe len, PAGE_SIZE, false, 9816c66c147SXuan Zhuo metasize, true); 9821830f893SJason Wang return head_skb; 9831830f893SJason Wang } 98456434a01SJohn Fastabend break; 98556434a01SJohn Fastabend case XDP_TX: 986d46eeeafSJason Wang stats->xdp_tx++; 9871b698fa5SLorenzo Bianconi xdpf = xdp_convert_buff_to_frame(&xdp); 98844fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 98944fa2dbdSJesper Dangaard Brouer goto err_xdp; 990ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 991fdc13979SLorenzo Bianconi if (unlikely(!err)) { 992fdc13979SLorenzo Bianconi xdp_return_frame_rx_napi(xdpf); 993fdc13979SLorenzo Bianconi } else if (unlikely(err < 0)) { 9940354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 99511b7d897SJesper Dangaard Brouer if (unlikely(xdp_page != page)) 99611b7d897SJesper Dangaard Brouer put_page(xdp_page); 99711b7d897SJesper Dangaard Brouer goto err_xdp; 99811b7d897SJesper Dangaard Brouer } 9992471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 100072979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 10015d458a13SJason Wang put_page(page); 100256434a01SJohn Fastabend rcu_read_unlock(); 100356434a01SJohn Fastabend goto xdp_xmit; 10043cc81a9aSJason Wang case XDP_REDIRECT: 1005d46eeeafSJason Wang stats->xdp_redirects++; 10063cc81a9aSJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 10073cc81a9aSJason Wang if (err) { 10083cc81a9aSJason Wang if (unlikely(xdp_page != page)) 10093cc81a9aSJason Wang put_page(xdp_page); 10103cc81a9aSJason Wang goto err_xdp; 10113cc81a9aSJason Wang } 10122471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 10133cc81a9aSJason Wang if (unlikely(xdp_page != page)) 10146890418bSJason Wang put_page(page); 10153cc81a9aSJason Wang rcu_read_unlock(); 10163cc81a9aSJason Wang goto xdp_xmit; 101756434a01SJohn Fastabend default: 10180354e4d1SJohn Fastabend bpf_warn_invalid_xdp_action(act); 1019df561f66SGustavo A. R. Silva fallthrough; 10200354e4d1SJohn Fastabend case XDP_ABORTED: 10210354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 1022df561f66SGustavo A. R. Silva fallthrough; 10230354e4d1SJohn Fastabend case XDP_DROP: 102472979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 102572979a6cSJohn Fastabend __free_pages(xdp_page, 0); 1026f600b690SJohn Fastabend goto err_xdp; 1027f600b690SJohn Fastabend } 102856434a01SJohn Fastabend } 1029f600b690SJohn Fastabend rcu_read_unlock(); 1030f600b690SJohn Fastabend 1031503d539aSYuya Kusakabe head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog, 10326c66c147SXuan Zhuo metasize, !!headroom); 1033f600b690SJohn Fastabend curr_skb = head_skb; 10349ab86bbcSShirley Ma 10358fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 10368fc3b9e9SMichael S. Tsirkin goto err_skb; 10379ab86bbcSShirley Ma while (--num_buf) { 10388fc3b9e9SMichael S. Tsirkin int num_skb_frags; 10398fc3b9e9SMichael S. Tsirkin 1040680557cfSMichael S. Tsirkin buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 104103e9f8a0SYunjian Wang if (unlikely(!buf)) { 10428fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 1043fdd819b2SMichael S. Tsirkin dev->name, num_buf, 1044012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 1045012873d0SMichael S. Tsirkin hdr->num_buffers)); 10468fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 10478fc3b9e9SMichael S. Tsirkin goto err_buf; 10483f2c31d9SMark McLoughlin } 10498fc3b9e9SMichael S. Tsirkin 1050d46eeeafSJason Wang stats->bytes += len; 10518fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 105228b39bc7SJason Wang 105328b39bc7SJason Wang truesize = mergeable_ctx_to_truesize(ctx); 105428b39bc7SJason Wang if (unlikely(len > truesize)) { 105556da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1056680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 1057680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 1058680557cfSMichael S. Tsirkin goto err_skb; 1059680557cfSMichael S. Tsirkin } 10608fc3b9e9SMichael S. Tsirkin 10618fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 10622613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 10632613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 10648fc3b9e9SMichael S. Tsirkin 10658fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 10668fc3b9e9SMichael S. Tsirkin goto err_skb; 10672613af0eSMichael Dalton if (curr_skb == head_skb) 10682613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 10692613af0eSMichael Dalton else 10702613af0eSMichael Dalton curr_skb->next = nskb; 10712613af0eSMichael Dalton curr_skb = nskb; 10722613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 10732613af0eSMichael Dalton num_skb_frags = 0; 10742613af0eSMichael Dalton } 10752613af0eSMichael Dalton if (curr_skb != head_skb) { 10762613af0eSMichael Dalton head_skb->data_len += len; 10772613af0eSMichael Dalton head_skb->len += len; 1078fb51879dSMichael Dalton head_skb->truesize += truesize; 10792613af0eSMichael Dalton } 10808fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 1081ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 1082ba275241SJason Wang put_page(page); 1083ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 1084fb51879dSMichael Dalton len, truesize); 1085ba275241SJason Wang } else { 10862613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 1087fb51879dSMichael Dalton offset, len, truesize); 1088ba275241SJason Wang } 10898fc3b9e9SMichael S. Tsirkin } 10908fc3b9e9SMichael S. Tsirkin 10915377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 10928fc3b9e9SMichael S. Tsirkin return head_skb; 10938fc3b9e9SMichael S. Tsirkin 1094f600b690SJohn Fastabend err_xdp: 1095f600b690SJohn Fastabend rcu_read_unlock(); 1096d46eeeafSJason Wang stats->xdp_drops++; 10978fc3b9e9SMichael S. Tsirkin err_skb: 10988fc3b9e9SMichael S. Tsirkin put_page(page); 1099850e088dSJason Wang while (num_buf-- > 1) { 1100680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &len); 1101680557cfSMichael S. Tsirkin if (unlikely(!buf)) { 11028fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 11038fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 11048fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 11058fc3b9e9SMichael S. Tsirkin break; 11068fc3b9e9SMichael S. Tsirkin } 1107d46eeeafSJason Wang stats->bytes += len; 1108680557cfSMichael S. Tsirkin page = virt_to_head_page(buf); 11098fc3b9e9SMichael S. Tsirkin put_page(page); 11103f2c31d9SMark McLoughlin } 11118fc3b9e9SMichael S. Tsirkin err_buf: 1112d46eeeafSJason Wang stats->drops++; 11138fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 111456434a01SJohn Fastabend xdp_xmit: 11158fc3b9e9SMichael S. Tsirkin return NULL; 11169ab86bbcSShirley Ma } 11179ab86bbcSShirley Ma 11187d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 11192471c75eSJesper Dangaard Brouer void *buf, unsigned int len, void **ctx, 1120a0929a44SToshiaki Makita unsigned int *xdp_xmit, 1121d46eeeafSJason Wang struct virtnet_rq_stats *stats) 11229ab86bbcSShirley Ma { 1123e9d7417bSJason Wang struct net_device *dev = vi->dev; 11249ab86bbcSShirley Ma struct sk_buff *skb; 1125012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 11269ab86bbcSShirley Ma 1127bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 11289ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 11299ab86bbcSShirley Ma dev->stats.rx_length_errors++; 1130ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 1131680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 1132ab7db917SMichael Dalton } else if (vi->big_packets) { 113398bfd23cSMichael Dalton give_pages(rq, buf); 1134ab7db917SMichael Dalton } else { 1135f6b10209SJason Wang put_page(virt_to_head_page(buf)); 1136ab7db917SMichael Dalton } 11377d9d60fdSToshiaki Makita return; 11389ab86bbcSShirley Ma } 11399ab86bbcSShirley Ma 1140f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 11417d9d60fdSToshiaki Makita skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, 1142a0929a44SToshiaki Makita stats); 1143f121159dSMichael S. Tsirkin else if (vi->big_packets) 1144a0929a44SToshiaki Makita skb = receive_big(dev, vi, rq, buf, len, stats); 1145f121159dSMichael S. Tsirkin else 1146a0929a44SToshiaki Makita skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); 1147f121159dSMichael S. Tsirkin 11488fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 11497d9d60fdSToshiaki Makita return; 11503f2c31d9SMark McLoughlin 11519ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 11523fa2a1dfSstephen hemminger 1153e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 115410a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 1155296f96fcSRusty Russell 1156e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 1157e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 1158e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 1159e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 1160fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 1161296f96fcSRusty Russell goto frame_err; 1162296f96fcSRusty Russell } 1163296f96fcSRusty Russell 1164133bbb18SWillem de Bruijn skb_record_rx_queue(skb, vq2rxq(rq->vq)); 1165d1dc06dcSMike Rapoport skb->protocol = eth_type_trans(skb, dev); 1166d1dc06dcSMike Rapoport pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 1167d1dc06dcSMike Rapoport ntohs(skb->protocol), skb->len, skb->pkt_type); 1168d1dc06dcSMike Rapoport 11690fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 11707d9d60fdSToshiaki Makita return; 1171296f96fcSRusty Russell 1172296f96fcSRusty Russell frame_err: 1173296f96fcSRusty Russell dev->stats.rx_frame_errors++; 1174296f96fcSRusty Russell dev_kfree_skb(skb); 1175296f96fcSRusty Russell } 1176296f96fcSRusty Russell 1177192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the 1178192f68cfSJason Wang * same size, except for the headroom. For this reason we do 1179192f68cfSJason Wang * not need to use mergeable_len_to_ctx here - it is enough 1180192f68cfSJason Wang * to store the headroom as the context ignoring the truesize. 1181192f68cfSJason Wang */ 1182946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 1183946fa564SMichael S. Tsirkin gfp_t gfp) 1184296f96fcSRusty Russell { 1185f6b10209SJason Wang struct page_frag *alloc_frag = &rq->alloc_frag; 1186f6b10209SJason Wang char *buf; 11872de2f7f4SJohn Fastabend unsigned int xdp_headroom = virtnet_get_headroom(vi); 1188192f68cfSJason Wang void *ctx = (void *)(unsigned long)xdp_headroom; 1189f6b10209SJason Wang int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 11909ab86bbcSShirley Ma int err; 11913f2c31d9SMark McLoughlin 1192f6b10209SJason Wang len = SKB_DATA_ALIGN(len) + 1193f6b10209SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1194f6b10209SJason Wang if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 11959ab86bbcSShirley Ma return -ENOMEM; 1196296f96fcSRusty Russell 1197f6b10209SJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1198f6b10209SJason Wang get_page(alloc_frag->page); 1199f6b10209SJason Wang alloc_frag->offset += len; 1200f6b10209SJason Wang sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 1201f6b10209SJason Wang vi->hdr_len + GOOD_PACKET_LEN); 1202192f68cfSJason Wang err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 12039ab86bbcSShirley Ma if (err < 0) 1204f6b10209SJason Wang put_page(virt_to_head_page(buf)); 12059ab86bbcSShirley Ma return err; 120697402b96SHerbert Xu } 120797402b96SHerbert Xu 1208012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 1209012873d0SMichael S. Tsirkin gfp_t gfp) 12109ab86bbcSShirley Ma { 12119ab86bbcSShirley Ma struct page *first, *list = NULL; 12129ab86bbcSShirley Ma char *p; 12139ab86bbcSShirley Ma int i, err, offset; 1214296f96fcSRusty Russell 1215a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 1216a5835440SRusty Russell 1217e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 12189ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 1219e9d7417bSJason Wang first = get_a_page(rq, gfp); 12209ab86bbcSShirley Ma if (!first) { 12219ab86bbcSShirley Ma if (list) 1222e9d7417bSJason Wang give_pages(rq, list); 12239ab86bbcSShirley Ma return -ENOMEM; 1224296f96fcSRusty Russell } 1225e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 12269ab86bbcSShirley Ma 12279ab86bbcSShirley Ma /* chain new page in list head to match sg */ 12289ab86bbcSShirley Ma first->private = (unsigned long)list; 12299ab86bbcSShirley Ma list = first; 12309ab86bbcSShirley Ma } 12319ab86bbcSShirley Ma 1232e9d7417bSJason Wang first = get_a_page(rq, gfp); 12339ab86bbcSShirley Ma if (!first) { 1234e9d7417bSJason Wang give_pages(rq, list); 12359ab86bbcSShirley Ma return -ENOMEM; 12369ab86bbcSShirley Ma } 12379ab86bbcSShirley Ma p = page_address(first); 12389ab86bbcSShirley Ma 1239e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 1240012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 1241012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 12429ab86bbcSShirley Ma 1243e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 12449ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 1245e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 12469ab86bbcSShirley Ma 12479ab86bbcSShirley Ma /* chain first in list head */ 12489ab86bbcSShirley Ma first->private = (unsigned long)list; 12499dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 1250aa989f5eSMichael S. Tsirkin first, gfp); 12519ab86bbcSShirley Ma if (err < 0) 1252e9d7417bSJason Wang give_pages(rq, first); 12539ab86bbcSShirley Ma 12549ab86bbcSShirley Ma return err; 12559ab86bbcSShirley Ma } 12569ab86bbcSShirley Ma 1257d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 12583cc81a9aSJason Wang struct ewma_pkt_len *avg_pkt_len, 12593cc81a9aSJason Wang unsigned int room) 12609ab86bbcSShirley Ma { 1261ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1262fbf28d78SMichael Dalton unsigned int len; 1263fbf28d78SMichael Dalton 12643cc81a9aSJason Wang if (room) 12653cc81a9aSJason Wang return PAGE_SIZE - room; 12663cc81a9aSJason Wang 12675377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 1268f0c3192cSMichael S. Tsirkin rq->min_buf_len, PAGE_SIZE - hdr_len); 12693cc81a9aSJason Wang 1270e377fcc8SMichael S. Tsirkin return ALIGN(len, L1_CACHE_BYTES); 1271fbf28d78SMichael Dalton } 1272fbf28d78SMichael Dalton 12732de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi, 12742de2f7f4SJohn Fastabend struct receive_queue *rq, gfp_t gfp) 1275fbf28d78SMichael Dalton { 1276fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 12772de2f7f4SJohn Fastabend unsigned int headroom = virtnet_get_headroom(vi); 12783cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 12793cc81a9aSJason Wang unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1280fb51879dSMichael Dalton char *buf; 1281680557cfSMichael S. Tsirkin void *ctx; 12829ab86bbcSShirley Ma int err; 1283fb51879dSMichael Dalton unsigned int len, hole; 12849ab86bbcSShirley Ma 12853cc81a9aSJason Wang /* Extra tailroom is needed to satisfy XDP's assumption. This 12863cc81a9aSJason Wang * means rx frags coalescing won't work, but consider we've 12873cc81a9aSJason Wang * disabled GSO for XDP, it won't be a big issue. 12883cc81a9aSJason Wang */ 12893cc81a9aSJason Wang len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 12903cc81a9aSJason Wang if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp))) 12919ab86bbcSShirley Ma return -ENOMEM; 1292ab7db917SMichael Dalton 1293fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 12942de2f7f4SJohn Fastabend buf += headroom; /* advance address leaving hole at front of pkt */ 1295fb51879dSMichael Dalton get_page(alloc_frag->page); 12963cc81a9aSJason Wang alloc_frag->offset += len + room; 1297fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 12983cc81a9aSJason Wang if (hole < len + room) { 1299ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 1300ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 13011daa8790SMichael S. Tsirkin * the current buffer. 1302ab7db917SMichael Dalton */ 1303fb51879dSMichael Dalton len += hole; 1304fb51879dSMichael Dalton alloc_frag->offset += hole; 1305fb51879dSMichael Dalton } 13069ab86bbcSShirley Ma 1307fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 130829fda25aSDavid S. Miller ctx = mergeable_len_to_ctx(len, headroom); 1309680557cfSMichael S. Tsirkin err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 13109ab86bbcSShirley Ma if (err < 0) 13112613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 13129ab86bbcSShirley Ma 13139ab86bbcSShirley Ma return err; 1314296f96fcSRusty Russell } 1315296f96fcSRusty Russell 1316b2baed69SRusty Russell /* 1317b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 1318b2baed69SRusty Russell * 1319b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 1320b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 1321b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 1322b2baed69SRusty Russell */ 1323946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 1324946fa564SMichael S. Tsirkin gfp_t gfp) 13253f2c31d9SMark McLoughlin { 13263f2c31d9SMark McLoughlin int err; 13271788f495SMichael S. Tsirkin bool oom; 13283f2c31d9SMark McLoughlin 13290aea51c3SAmit Shah do { 13309ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 13312de2f7f4SJohn Fastabend err = add_recvbuf_mergeable(vi, rq, gfp); 13329ab86bbcSShirley Ma else if (vi->big_packets) 1333012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 13349ab86bbcSShirley Ma else 1335946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 13363f2c31d9SMark McLoughlin 13371788f495SMichael S. Tsirkin oom = err == -ENOMEM; 13389ed4cb07SRusty Russell if (err) 13393f2c31d9SMark McLoughlin break; 1340b7dfde95SLinus Torvalds } while (rq->vq->num_free); 1341461f03dcSToshiaki Makita if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) { 134201c32598SMichael S. Tsirkin unsigned long flags; 134301c32598SMichael S. Tsirkin 134401c32598SMichael S. Tsirkin flags = u64_stats_update_begin_irqsave(&rq->stats.syncp); 1345d46eeeafSJason Wang rq->stats.kicks++; 134601c32598SMichael S. Tsirkin u64_stats_update_end_irqrestore(&rq->stats.syncp, flags); 1347461f03dcSToshiaki Makita } 1348461f03dcSToshiaki Makita 13493161e453SRusty Russell return !oom; 13503f2c31d9SMark McLoughlin } 13513f2c31d9SMark McLoughlin 135218445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 1353296f96fcSRusty Russell { 1354296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 1355986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1356e9d7417bSJason Wang 1357e4e8452aSWillem de Bruijn virtqueue_napi_schedule(&rq->napi, rvq); 1358296f96fcSRusty Russell } 1359296f96fcSRusty Russell 1360e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 13613e9d08ecSBruce Rogers { 1362e4e8452aSWillem de Bruijn napi_enable(napi); 13633e9d08ecSBruce Rogers 13643e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 1365e4e8452aSWillem de Bruijn * won't get another interrupt, so process any outstanding packets now. 1366e4e8452aSWillem de Bruijn * Call local_bh_enable after to trigger softIRQ processing. 1367e4e8452aSWillem de Bruijn */ 1368ec13ee80SMichael S. Tsirkin local_bh_disable(); 1369e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 1370ec13ee80SMichael S. Tsirkin local_bh_enable(); 13713e9d08ecSBruce Rogers } 13723e9d08ecSBruce Rogers 1373b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1374b92f1e67SWillem de Bruijn struct virtqueue *vq, 1375b92f1e67SWillem de Bruijn struct napi_struct *napi) 1376b92f1e67SWillem de Bruijn { 1377b92f1e67SWillem de Bruijn if (!napi->weight) 1378b92f1e67SWillem de Bruijn return; 1379b92f1e67SWillem de Bruijn 1380b92f1e67SWillem de Bruijn /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1381b92f1e67SWillem de Bruijn * enable the feature if this is likely affine with the transmit path. 1382b92f1e67SWillem de Bruijn */ 1383b92f1e67SWillem de Bruijn if (!vi->affinity_hint_set) { 1384b92f1e67SWillem de Bruijn napi->weight = 0; 1385b92f1e67SWillem de Bruijn return; 1386b92f1e67SWillem de Bruijn } 1387b92f1e67SWillem de Bruijn 1388b92f1e67SWillem de Bruijn return virtnet_napi_enable(vq, napi); 1389b92f1e67SWillem de Bruijn } 1390b92f1e67SWillem de Bruijn 139178a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi) 139278a57b48SWillem de Bruijn { 139378a57b48SWillem de Bruijn if (napi->weight) 139478a57b48SWillem de Bruijn napi_disable(napi); 139578a57b48SWillem de Bruijn } 139678a57b48SWillem de Bruijn 13973161e453SRusty Russell static void refill_work(struct work_struct *work) 13983161e453SRusty Russell { 1399e9d7417bSJason Wang struct virtnet_info *vi = 1400e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 14013161e453SRusty Russell bool still_empty; 1402986a4f4dSJason Wang int i; 14033161e453SRusty Russell 140455257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1405986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 1406986a4f4dSJason Wang 1407986a4f4dSJason Wang napi_disable(&rq->napi); 1408946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1409e4e8452aSWillem de Bruijn virtnet_napi_enable(rq->vq, &rq->napi); 14103161e453SRusty Russell 14113161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 1412986a4f4dSJason Wang * we will *never* try to fill again. 1413986a4f4dSJason Wang */ 14143161e453SRusty Russell if (still_empty) 14153b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 14163161e453SRusty Russell } 1417986a4f4dSJason Wang } 14183161e453SRusty Russell 14192471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget, 14202471c75eSJesper Dangaard Brouer unsigned int *xdp_xmit) 1421296f96fcSRusty Russell { 1422e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 1423d46eeeafSJason Wang struct virtnet_rq_stats stats = {}; 1424a0929a44SToshiaki Makita unsigned int len; 14259ab86bbcSShirley Ma void *buf; 1426a0929a44SToshiaki Makita int i; 1427296f96fcSRusty Russell 1428192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 1429680557cfSMichael S. Tsirkin void *ctx; 1430680557cfSMichael S. Tsirkin 1431d46eeeafSJason Wang while (stats.packets < budget && 1432680557cfSMichael S. Tsirkin (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1433a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); 1434d46eeeafSJason Wang stats.packets++; 1435680557cfSMichael S. Tsirkin } 1436680557cfSMichael S. Tsirkin } else { 1437d46eeeafSJason Wang while (stats.packets < budget && 1438e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1439a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); 1440d46eeeafSJason Wang stats.packets++; 1441296f96fcSRusty Russell } 1442680557cfSMichael S. Tsirkin } 1443296f96fcSRusty Russell 1444718be6baS? jiang if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { 1445946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 14463b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 14473161e453SRusty Russell } 1448296f96fcSRusty Russell 1449d7dfc5cfSToshiaki Makita u64_stats_update_begin(&rq->stats.syncp); 1450a0929a44SToshiaki Makita for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) { 1451a0929a44SToshiaki Makita size_t offset = virtnet_rq_stats_desc[i].offset; 1452a0929a44SToshiaki Makita u64 *item; 1453a0929a44SToshiaki Makita 1454d46eeeafSJason Wang item = (u64 *)((u8 *)&rq->stats + offset); 1455d46eeeafSJason Wang *item += *(u64 *)((u8 *)&stats + offset); 1456a0929a44SToshiaki Makita } 1457d7dfc5cfSToshiaki Makita u64_stats_update_end(&rq->stats.syncp); 145861845d20SJason Wang 1459d46eeeafSJason Wang return stats.packets; 14602ffa7598SJason Wang } 14612ffa7598SJason Wang 1462df133f3fSMichael S. Tsirkin static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi) 1463ea7735d9SWillem de Bruijn { 1464ea7735d9SWillem de Bruijn unsigned int len; 1465ea7735d9SWillem de Bruijn unsigned int packets = 0; 1466ea7735d9SWillem de Bruijn unsigned int bytes = 0; 14675050471dSToshiaki Makita void *ptr; 1468ea7735d9SWillem de Bruijn 14695050471dSToshiaki Makita while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 14705050471dSToshiaki Makita if (likely(!is_xdp_frame(ptr))) { 14715050471dSToshiaki Makita struct sk_buff *skb = ptr; 14725050471dSToshiaki Makita 1473ea7735d9SWillem de Bruijn pr_debug("Sent skb %p\n", skb); 1474ea7735d9SWillem de Bruijn 1475ea7735d9SWillem de Bruijn bytes += skb->len; 1476df133f3fSMichael S. Tsirkin napi_consume_skb(skb, in_napi); 14775050471dSToshiaki Makita } else { 14785050471dSToshiaki Makita struct xdp_frame *frame = ptr_to_xdp(ptr); 14795050471dSToshiaki Makita 14805050471dSToshiaki Makita bytes += frame->len; 14815050471dSToshiaki Makita xdp_return_frame(frame); 14825050471dSToshiaki Makita } 14835050471dSToshiaki Makita packets++; 1484ea7735d9SWillem de Bruijn } 1485ea7735d9SWillem de Bruijn 1486ea7735d9SWillem de Bruijn /* Avoid overhead when no packets have been processed 1487ea7735d9SWillem de Bruijn * happens when called speculatively from start_xmit. 1488ea7735d9SWillem de Bruijn */ 1489ea7735d9SWillem de Bruijn if (!packets) 1490ea7735d9SWillem de Bruijn return; 1491ea7735d9SWillem de Bruijn 1492d7dfc5cfSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1493d7dfc5cfSToshiaki Makita sq->stats.bytes += bytes; 1494d7dfc5cfSToshiaki Makita sq->stats.packets += packets; 1495d7dfc5cfSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1496ea7735d9SWillem de Bruijn } 1497ea7735d9SWillem de Bruijn 1498534da5e8SToshiaki Makita static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 1499534da5e8SToshiaki Makita { 1500534da5e8SToshiaki Makita if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 1501534da5e8SToshiaki Makita return false; 1502534da5e8SToshiaki Makita else if (q < vi->curr_queue_pairs) 1503534da5e8SToshiaki Makita return true; 1504534da5e8SToshiaki Makita else 1505534da5e8SToshiaki Makita return false; 1506534da5e8SToshiaki Makita } 1507534da5e8SToshiaki Makita 15087b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq) 15097b0411efSWillem de Bruijn { 15107b0411efSWillem de Bruijn struct virtnet_info *vi = rq->vq->vdev->priv; 15117b0411efSWillem de Bruijn unsigned int index = vq2rxq(rq->vq); 15127b0411efSWillem de Bruijn struct send_queue *sq = &vi->sq[index]; 15137b0411efSWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 15147b0411efSWillem de Bruijn 1515534da5e8SToshiaki Makita if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) 15167b0411efSWillem de Bruijn return; 15177b0411efSWillem de Bruijn 15187b0411efSWillem de Bruijn if (__netif_tx_trylock(txq)) { 1519df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, true); 15207b0411efSWillem de Bruijn __netif_tx_unlock(txq); 15217b0411efSWillem de Bruijn } 15227b0411efSWillem de Bruijn 15237b0411efSWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 15247b0411efSWillem de Bruijn netif_tx_wake_queue(txq); 15257b0411efSWillem de Bruijn } 15267b0411efSWillem de Bruijn 15272ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 15282ffa7598SJason Wang { 15292ffa7598SJason Wang struct receive_queue *rq = 15302ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 15319267c430SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 15329267c430SJason Wang struct send_queue *sq; 15332a43565cSToshiaki Makita unsigned int received; 15342471c75eSJesper Dangaard Brouer unsigned int xdp_xmit = 0; 15352ffa7598SJason Wang 15367b0411efSWillem de Bruijn virtnet_poll_cleantx(rq); 15377b0411efSWillem de Bruijn 1538186b3c99SJason Wang received = virtnet_receive(rq, budget, &xdp_xmit); 15392ffa7598SJason Wang 15408329d98eSRusty Russell /* Out of packets? */ 1541e4e8452aSWillem de Bruijn if (received < budget) 1542e4e8452aSWillem de Bruijn virtqueue_napi_complete(napi, rq->vq, received); 1543296f96fcSRusty Russell 15442471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_REDIR) 15451d233886SToke Høiland-Jørgensen xdp_do_flush(); 15462471c75eSJesper Dangaard Brouer 15472471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_TX) { 154897c2c69eSXuan Zhuo sq = virtnet_xdp_get_sq(vi); 1549461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1550461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1551461f03dcSToshiaki Makita sq->stats.kicks++; 1552461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1553461f03dcSToshiaki Makita } 155497c2c69eSXuan Zhuo virtnet_xdp_put_sq(vi, sq); 15559267c430SJason Wang } 1556186b3c99SJason Wang 1557296f96fcSRusty Russell return received; 1558296f96fcSRusty Russell } 1559296f96fcSRusty Russell 1560986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 1561986a4f4dSJason Wang { 1562986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1563754b8a21SJesper Dangaard Brouer int i, err; 1564986a4f4dSJason Wang 1565e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1566e4166625SJason Wang if (i < vi->curr_queue_pairs) 1567986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 1568946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1569986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 1570754b8a21SJesper Dangaard Brouer 1571b02e5a0eSBjörn Töpel err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); 1572754b8a21SJesper Dangaard Brouer if (err < 0) 1573754b8a21SJesper Dangaard Brouer return err; 1574754b8a21SJesper Dangaard Brouer 15758d5d8852SJesper Dangaard Brouer err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq, 15768d5d8852SJesper Dangaard Brouer MEM_TYPE_PAGE_SHARED, NULL); 15778d5d8852SJesper Dangaard Brouer if (err < 0) { 15788d5d8852SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 15798d5d8852SJesper Dangaard Brouer return err; 15808d5d8852SJesper Dangaard Brouer } 15818d5d8852SJesper Dangaard Brouer 1582e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1583b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1584986a4f4dSJason Wang } 1585986a4f4dSJason Wang 1586986a4f4dSJason Wang return 0; 1587986a4f4dSJason Wang } 1588986a4f4dSJason Wang 1589b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1590b92f1e67SWillem de Bruijn { 1591b92f1e67SWillem de Bruijn struct send_queue *sq = container_of(napi, struct send_queue, napi); 1592b92f1e67SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1593534da5e8SToshiaki Makita unsigned int index = vq2txq(sq->vq); 1594534da5e8SToshiaki Makita struct netdev_queue *txq; 1595*5a2f966dSMichael S. Tsirkin int opaque; 1596*5a2f966dSMichael S. Tsirkin bool done; 1597b92f1e67SWillem de Bruijn 1598534da5e8SToshiaki Makita if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { 1599534da5e8SToshiaki Makita /* We don't need to enable cb for XDP */ 1600534da5e8SToshiaki Makita napi_complete_done(napi, 0); 1601534da5e8SToshiaki Makita return 0; 1602534da5e8SToshiaki Makita } 1603534da5e8SToshiaki Makita 1604534da5e8SToshiaki Makita txq = netdev_get_tx_queue(vi->dev, index); 1605b92f1e67SWillem de Bruijn __netif_tx_lock(txq, raw_smp_processor_id()); 1606*5a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 1607df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, true); 1608*5a2f966dSMichael S. Tsirkin 1609*5a2f966dSMichael S. Tsirkin opaque = virtqueue_enable_cb_prepare(sq->vq); 1610*5a2f966dSMichael S. Tsirkin 1611*5a2f966dSMichael S. Tsirkin done = napi_complete_done(napi, 0); 1612*5a2f966dSMichael S. Tsirkin 1613*5a2f966dSMichael S. Tsirkin if (!done) 1614*5a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 1615*5a2f966dSMichael S. Tsirkin 1616b92f1e67SWillem de Bruijn __netif_tx_unlock(txq); 1617b92f1e67SWillem de Bruijn 1618*5a2f966dSMichael S. Tsirkin if (done) { 1619*5a2f966dSMichael S. Tsirkin if (unlikely(virtqueue_poll(sq->vq, opaque))) { 1620*5a2f966dSMichael S. Tsirkin if (napi_schedule_prep(napi)) { 1621*5a2f966dSMichael S. Tsirkin __netif_tx_lock(txq, raw_smp_processor_id()); 1622*5a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 1623*5a2f966dSMichael S. Tsirkin __netif_tx_unlock(txq); 1624*5a2f966dSMichael S. Tsirkin __napi_schedule(napi); 1625*5a2f966dSMichael S. Tsirkin } 1626*5a2f966dSMichael S. Tsirkin } 1627*5a2f966dSMichael S. Tsirkin } 1628b92f1e67SWillem de Bruijn 1629b92f1e67SWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1630b92f1e67SWillem de Bruijn netif_tx_wake_queue(txq); 1631b92f1e67SWillem de Bruijn 1632b92f1e67SWillem de Bruijn return 0; 1633b92f1e67SWillem de Bruijn } 1634b92f1e67SWillem de Bruijn 1635e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1636296f96fcSRusty Russell { 1637012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 1638296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1639e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 1640e2fcad58SJason A. Donenfeld int num_sg; 1641012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 1642e7428e95SMichael S. Tsirkin bool can_push; 1643296f96fcSRusty Russell 1644e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1645e7428e95SMichael S. Tsirkin 1646e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 1647e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1648e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1649e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 1650e7428e95SMichael S. Tsirkin * csum_start offset below. */ 1651e7428e95SMichael S. Tsirkin if (can_push) 1652012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1653e7428e95SMichael S. Tsirkin else 1654e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 1655296f96fcSRusty Russell 1656e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 1657fd3a8862SWillem de Bruijn virtio_is_little_endian(vi->vdev), false, 1658fd3a8862SWillem de Bruijn 0)) 165985eb1389SXianting Tian return -EPROTO; 1660296f96fcSRusty Russell 1661e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 1662012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 16633f2c31d9SMark McLoughlin 1664547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1665e7428e95SMichael S. Tsirkin if (can_push) { 1666e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 1667e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1668e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1669e2fcad58SJason A. Donenfeld return num_sg; 1670e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 1671e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 1672e7428e95SMichael S. Tsirkin } else { 1673e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 1674e2fcad58SJason A. Donenfeld num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1675e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1676e2fcad58SJason A. Donenfeld return num_sg; 1677e2fcad58SJason A. Donenfeld num_sg++; 1678e7428e95SMichael S. Tsirkin } 16799dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 168011a3a154SRusty Russell } 168111a3a154SRusty Russell 1682424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 168399ffc696SRusty Russell { 168499ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1685986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 1686986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 16879ed4cb07SRusty Russell int err; 16884b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 16896b16f9eeSFlorian Westphal bool kick = !netdev_xmit_more(); 1690b92f1e67SWillem de Bruijn bool use_napi = sq->napi.weight; 16912cb9c6baSRusty Russell 16922cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 1693df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, false); 169499ffc696SRusty Russell 1695bdb12e0dSWillem de Bruijn if (use_napi && kick) 1696bdb12e0dSWillem de Bruijn virtqueue_enable_cb_delayed(sq->vq); 1697bdb12e0dSWillem de Bruijn 1698074c3582SJacob Keller /* timestamp packet in software */ 1699074c3582SJacob Keller skb_tx_timestamp(skb); 1700074c3582SJacob Keller 170103f191baSMichael S. Tsirkin /* Try to transmit */ 1702b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 170399ffc696SRusty Russell 17049ed4cb07SRusty Russell /* This should not happen! */ 1705681daee2SJason Wang if (unlikely(err)) { 170658eba97dSRusty Russell dev->stats.tx_fifo_errors++; 17072e57b79cSRick Jones if (net_ratelimit()) 170858eba97dSRusty Russell dev_warn(&dev->dev, 17097934b481SYuval Shaia "Unexpected TXQ (%d) queue failure: %d\n", 17107934b481SYuval Shaia qnum, err); 171158eba97dSRusty Russell dev->stats.tx_dropped++; 171285e94525SEric W. Biederman dev_kfree_skb_any(skb); 171358eba97dSRusty Russell return NETDEV_TX_OK; 1714296f96fcSRusty Russell } 171503f191baSMichael S. Tsirkin 171648925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 1717b92f1e67SWillem de Bruijn if (!use_napi) { 171848925e37SRusty Russell skb_orphan(skb); 1719895b5c9fSFlorian Westphal nf_reset_ct(skb); 1720b92f1e67SWillem de Bruijn } 172148925e37SRusty Russell 172260302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 172360302ff6SMichael S. Tsirkin * are then unable to transmit. 172460302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 172560302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 172660302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 172760302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 172860302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 172960302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 173060302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 1731d631b94eSstephen hemminger */ 1732b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1733986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 1734b92f1e67SWillem de Bruijn if (!use_napi && 1735b92f1e67SWillem de Bruijn unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 173648925e37SRusty Russell /* More just got used, free them then recheck. */ 1737df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, false); 1738b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1739986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 1740e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 174148925e37SRusty Russell } 174248925e37SRusty Russell } 174348925e37SRusty Russell } 174448925e37SRusty Russell 1745461f03dcSToshiaki Makita if (kick || netif_xmit_stopped(txq)) { 1746461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1747461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1748461f03dcSToshiaki Makita sq->stats.kicks++; 1749461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1750461f03dcSToshiaki Makita } 1751461f03dcSToshiaki Makita } 17520b725a2cSDavid S. Miller 17530b725a2cSDavid S. Miller return NETDEV_TX_OK; 1754c223a078SDavid S. Miller } 1755c223a078SDavid S. Miller 175640cbfc37SAmos Kong /* 175740cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 175840cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 1759788a8b6dSstephen hemminger * never fail unless improperly formatted. 176040cbfc37SAmos Kong */ 176140cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 1762d24bae32Sstephen hemminger struct scatterlist *out) 176340cbfc37SAmos Kong { 1764f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 1765d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 176640cbfc37SAmos Kong 176740cbfc37SAmos Kong /* Caller should know better */ 1768f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 176940cbfc37SAmos Kong 177012e57169SMichael S. Tsirkin vi->ctrl->status = ~0; 177112e57169SMichael S. Tsirkin vi->ctrl->hdr.class = class; 177212e57169SMichael S. Tsirkin vi->ctrl->hdr.cmd = cmd; 1773f7bc9594SRusty Russell /* Add header */ 177412e57169SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 1775f7bc9594SRusty Russell sgs[out_num++] = &hdr; 177640cbfc37SAmos Kong 1777f7bc9594SRusty Russell if (out) 1778f7bc9594SRusty Russell sgs[out_num++] = out; 177940cbfc37SAmos Kong 1780f7bc9594SRusty Russell /* Add return status. */ 178112e57169SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 1782d24bae32Sstephen hemminger sgs[out_num] = &stat; 178340cbfc37SAmos Kong 1784d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1785a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 178640cbfc37SAmos Kong 178767975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 178812e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 178940cbfc37SAmos Kong 179040cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 179140cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 179240cbfc37SAmos Kong */ 1793047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 1794047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 179540cbfc37SAmos Kong cpu_relax(); 179640cbfc37SAmos Kong 179712e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 179840cbfc37SAmos Kong } 179940cbfc37SAmos Kong 18009c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 18019c46f6d4SAlex Williamson { 18029c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 18039c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 1804f2f2c8b4SJiri Pirko int ret; 1805e37e2ff3SAndy Lutomirski struct sockaddr *addr; 18067e58d5aeSAmos Kong struct scatterlist sg; 18079c46f6d4SAlex Williamson 1808ba5e4426SSridhar Samudrala if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 1809ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 1810ba5e4426SSridhar Samudrala 1811801822d1SShyam Saini addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 1812e37e2ff3SAndy Lutomirski if (!addr) 1813e37e2ff3SAndy Lutomirski return -ENOMEM; 1814e37e2ff3SAndy Lutomirski 1815e37e2ff3SAndy Lutomirski ret = eth_prepare_mac_addr_change(dev, addr); 1816f2f2c8b4SJiri Pirko if (ret) 1817e37e2ff3SAndy Lutomirski goto out; 18189c46f6d4SAlex Williamson 18197e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 18207e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 18217e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1822d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 18237e58d5aeSAmos Kong dev_warn(&vdev->dev, 18247e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 1825e37e2ff3SAndy Lutomirski ret = -EINVAL; 1826e37e2ff3SAndy Lutomirski goto out; 18277e58d5aeSAmos Kong } 18287e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 18297e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1830855e0c52SRusty Russell unsigned int i; 1831855e0c52SRusty Russell 1832855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 1833855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 1834855e0c52SRusty Russell virtio_cwrite8(vdev, 1835855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1836855e0c52SRusty Russell i, addr->sa_data[i]); 18377e58d5aeSAmos Kong } 18387e58d5aeSAmos Kong 18397e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 1840e37e2ff3SAndy Lutomirski ret = 0; 18419c46f6d4SAlex Williamson 1842e37e2ff3SAndy Lutomirski out: 1843e37e2ff3SAndy Lutomirski kfree(addr); 1844e37e2ff3SAndy Lutomirski return ret; 18459c46f6d4SAlex Williamson } 18469c46f6d4SAlex Williamson 1847bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev, 18483fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 18493fa2a1dfSstephen hemminger { 18503fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 18513fa2a1dfSstephen hemminger unsigned int start; 1852d7dfc5cfSToshiaki Makita int i; 18533fa2a1dfSstephen hemminger 1854d7dfc5cfSToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 18552c4a2f7dSToshiaki Makita u64 tpackets, tbytes, rpackets, rbytes, rdrops; 1856d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 1857d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 18583fa2a1dfSstephen hemminger 18593fa2a1dfSstephen hemminger do { 1860d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 1861d7dfc5cfSToshiaki Makita tpackets = sq->stats.packets; 1862d7dfc5cfSToshiaki Makita tbytes = sq->stats.bytes; 1863d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 186483a27052SEric Dumazet 186583a27052SEric Dumazet do { 1866d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 1867d46eeeafSJason Wang rpackets = rq->stats.packets; 1868d46eeeafSJason Wang rbytes = rq->stats.bytes; 1869d46eeeafSJason Wang rdrops = rq->stats.drops; 1870d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 18713fa2a1dfSstephen hemminger 18723fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 18733fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 18743fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 18753fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 18762c4a2f7dSToshiaki Makita tot->rx_dropped += rdrops; 18773fa2a1dfSstephen hemminger } 18783fa2a1dfSstephen hemminger 18793fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1880021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 18813fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 18823fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 18833fa2a1dfSstephen hemminger } 18843fa2a1dfSstephen hemminger 1885586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1886586d17c5SJason Wang { 1887586d17c5SJason Wang rtnl_lock(); 1888586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1889d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1890586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1891586d17c5SJason Wang rtnl_unlock(); 1892586d17c5SJason Wang } 1893586d17c5SJason Wang 189447315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1895986a4f4dSJason Wang { 1896986a4f4dSJason Wang struct scatterlist sg; 1897986a4f4dSJason Wang struct net_device *dev = vi->dev; 1898986a4f4dSJason Wang 1899986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1900986a4f4dSJason Wang return 0; 1901986a4f4dSJason Wang 190212e57169SMichael S. Tsirkin vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 190312e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); 1904986a4f4dSJason Wang 1905986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1906d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1907986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1908986a4f4dSJason Wang queue_pairs); 1909986a4f4dSJason Wang return -EINVAL; 191055257d72SSasha Levin } else { 1911986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 191235ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 191335ed159bSJason Wang if (dev->flags & IFF_UP) 19149b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 191555257d72SSasha Levin } 1916986a4f4dSJason Wang 1917986a4f4dSJason Wang return 0; 1918986a4f4dSJason Wang } 1919986a4f4dSJason Wang 192047315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 192147315329SJohn Fastabend { 192247315329SJohn Fastabend int err; 192347315329SJohn Fastabend 192447315329SJohn Fastabend rtnl_lock(); 192547315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 192647315329SJohn Fastabend rtnl_unlock(); 192747315329SJohn Fastabend return err; 192847315329SJohn Fastabend } 192947315329SJohn Fastabend 1930296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1931296f96fcSRusty Russell { 1932296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1933986a4f4dSJason Wang int i; 1934296f96fcSRusty Russell 1935b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1936b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1937986a4f4dSJason Wang 1938b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 1939754b8a21SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 1940986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 194178a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 1942b92f1e67SWillem de Bruijn } 1943296f96fcSRusty Russell 1944296f96fcSRusty Russell return 0; 1945296f96fcSRusty Russell } 1946296f96fcSRusty Russell 19472af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 19482af7698eSAlex Williamson { 19492af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1950f565a7c2SAlex Williamson struct scatterlist sg[2]; 1951f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1952ccffad25SJiri Pirko struct netdev_hw_addr *ha; 195332e7bfc4SJiri Pirko int uc_count; 19544cd24eafSJiri Pirko int mc_count; 1955f565a7c2SAlex Williamson void *buf; 1956f565a7c2SAlex Williamson int i; 19572af7698eSAlex Williamson 1958788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 19592af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 19602af7698eSAlex Williamson return; 19612af7698eSAlex Williamson 196212e57169SMichael S. Tsirkin vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); 196312e57169SMichael S. Tsirkin vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 19642af7698eSAlex Williamson 196512e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); 19662af7698eSAlex Williamson 19672af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1968d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 19692af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 197012e57169SMichael S. Tsirkin vi->ctrl->promisc ? "en" : "dis"); 19712af7698eSAlex Williamson 197212e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); 19732af7698eSAlex Williamson 19742af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1975d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 19762af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 197712e57169SMichael S. Tsirkin vi->ctrl->allmulti ? "en" : "dis"); 1978f565a7c2SAlex Williamson 197932e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 19804cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1981f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 19824cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1983f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 19844cd24eafSJiri Pirko mac_data = buf; 1985e68ed8f0SJoe Perches if (!buf) 1986f565a7c2SAlex Williamson return; 1987f565a7c2SAlex Williamson 198823e258e1SAlex Williamson sg_init_table(sg, 2); 198923e258e1SAlex Williamson 1990f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 1991fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1992ccffad25SJiri Pirko i = 0; 199332e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1994ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1995f565a7c2SAlex Williamson 1996f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 199732e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1998f565a7c2SAlex Williamson 1999f565a7c2SAlex Williamson /* multicast list and count fill the end */ 200032e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 2001f565a7c2SAlex Williamson 2002fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 2003567ec874SJiri Pirko i = 0; 200422bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 200522bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2006f565a7c2SAlex Williamson 2007f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 20084cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 2009f565a7c2SAlex Williamson 2010f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 2011d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 201299e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 2013f565a7c2SAlex Williamson 2014f565a7c2SAlex Williamson kfree(buf); 20152af7698eSAlex Williamson } 20162af7698eSAlex Williamson 201780d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 201880d5c368SPatrick McHardy __be16 proto, u16 vid) 20190bde9569SAlex Williamson { 20200bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 20210bde9569SAlex Williamson struct scatterlist sg; 20220bde9569SAlex Williamson 2023d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 202412e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 20250bde9569SAlex Williamson 20260bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 2027d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 20280bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 20298e586137SJiri Pirko return 0; 20300bde9569SAlex Williamson } 20310bde9569SAlex Williamson 203280d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 203380d5c368SPatrick McHardy __be16 proto, u16 vid) 20340bde9569SAlex Williamson { 20350bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 20360bde9569SAlex Williamson struct scatterlist sg; 20370bde9569SAlex Williamson 2038d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 203912e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 20400bde9569SAlex Williamson 20410bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 2042d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 20430bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 20448e586137SJiri Pirko return 0; 20450bde9569SAlex Williamson } 20460bde9569SAlex Williamson 2047310974faSPeter Xu static void virtnet_clean_affinity(struct virtnet_info *vi) 2048986a4f4dSJason Wang { 2049986a4f4dSJason Wang int i; 20508898c21cSWanlong Gao 20518898c21cSWanlong Gao if (vi->affinity_hint_set) { 20528898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 205319e226e8SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, NULL); 205419e226e8SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, NULL); 20558898c21cSWanlong Gao } 20568898c21cSWanlong Gao 20578898c21cSWanlong Gao vi->affinity_hint_set = false; 20588898c21cSWanlong Gao } 20598898c21cSWanlong Gao } 20608898c21cSWanlong Gao 20618898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 2062986a4f4dSJason Wang { 20632ca653d6SCaleb Raitto cpumask_var_t mask; 20642ca653d6SCaleb Raitto int stragglers; 20652ca653d6SCaleb Raitto int group_size; 20662ca653d6SCaleb Raitto int i, j, cpu; 20672ca653d6SCaleb Raitto int num_cpu; 20682ca653d6SCaleb Raitto int stride; 2069986a4f4dSJason Wang 20702ca653d6SCaleb Raitto if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { 2071310974faSPeter Xu virtnet_clean_affinity(vi); 2072986a4f4dSJason Wang return; 2073986a4f4dSJason Wang } 2074986a4f4dSJason Wang 20752ca653d6SCaleb Raitto num_cpu = num_online_cpus(); 20762ca653d6SCaleb Raitto stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); 20772ca653d6SCaleb Raitto stragglers = num_cpu >= vi->curr_queue_pairs ? 20782ca653d6SCaleb Raitto num_cpu % vi->curr_queue_pairs : 20792ca653d6SCaleb Raitto 0; 20802ca653d6SCaleb Raitto cpu = cpumask_next(-1, cpu_online_mask); 20814d99f660SAndrei Vagin 20822ca653d6SCaleb Raitto for (i = 0; i < vi->curr_queue_pairs; i++) { 20832ca653d6SCaleb Raitto group_size = stride + (i < stragglers ? 1 : 0); 20842ca653d6SCaleb Raitto 20852ca653d6SCaleb Raitto for (j = 0; j < group_size; j++) { 20862ca653d6SCaleb Raitto cpumask_set_cpu(cpu, mask); 20872ca653d6SCaleb Raitto cpu = cpumask_next_wrap(cpu, cpu_online_mask, 20882ca653d6SCaleb Raitto nr_cpu_ids, false); 20892ca653d6SCaleb Raitto } 20902ca653d6SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, mask); 20912ca653d6SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, mask); 2092044ab86dSAntoine Tenart __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); 20932ca653d6SCaleb Raitto cpumask_clear(mask); 2094986a4f4dSJason Wang } 2095986a4f4dSJason Wang 2096986a4f4dSJason Wang vi->affinity_hint_set = true; 20972ca653d6SCaleb Raitto free_cpumask_var(mask); 209847be2479SWanlong Gao } 2099986a4f4dSJason Wang 21008017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 21018de4b2f3SWanlong Gao { 21028017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 21038017c279SSebastian Andrzej Siewior node); 21048de4b2f3SWanlong Gao virtnet_set_affinity(vi); 21058017c279SSebastian Andrzej Siewior return 0; 21068de4b2f3SWanlong Gao } 21073ab098dfSJason Wang 21088017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 21098017c279SSebastian Andrzej Siewior { 21108017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 21118017c279SSebastian Andrzej Siewior node_dead); 21128017c279SSebastian Andrzej Siewior virtnet_set_affinity(vi); 21138017c279SSebastian Andrzej Siewior return 0; 21148017c279SSebastian Andrzej Siewior } 21158017c279SSebastian Andrzej Siewior 21168017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 21178017c279SSebastian Andrzej Siewior { 21188017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 21198017c279SSebastian Andrzej Siewior node); 21208017c279SSebastian Andrzej Siewior 2121310974faSPeter Xu virtnet_clean_affinity(vi); 21228017c279SSebastian Andrzej Siewior return 0; 21238017c279SSebastian Andrzej Siewior } 21248017c279SSebastian Andrzej Siewior 21258017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online; 21268017c279SSebastian Andrzej Siewior 21278017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi) 21288017c279SSebastian Andrzej Siewior { 21298017c279SSebastian Andrzej Siewior int ret; 21308017c279SSebastian Andrzej Siewior 21318017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 21328017c279SSebastian Andrzej Siewior if (ret) 21338017c279SSebastian Andrzej Siewior return ret; 21348017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 21358017c279SSebastian Andrzej Siewior &vi->node_dead); 21368017c279SSebastian Andrzej Siewior if (!ret) 21378017c279SSebastian Andrzej Siewior return ret; 21388017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 21398017c279SSebastian Andrzej Siewior return ret; 21408017c279SSebastian Andrzej Siewior } 21418017c279SSebastian Andrzej Siewior 21428017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 21438017c279SSebastian Andrzej Siewior { 21448017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 21458017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 21468017c279SSebastian Andrzej Siewior &vi->node_dead); 2147a9ea3fc6SHerbert Xu } 2148a9ea3fc6SHerbert Xu 21498f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 21508f9f4668SRick Jones struct ethtool_ringparam *ring) 21518f9f4668SRick Jones { 21528f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 21538f9f4668SRick Jones 2154986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 2155986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 21568f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 21578f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 21588f9f4668SRick Jones } 21598f9f4668SRick Jones 216066846048SRick Jones 216166846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 216266846048SRick Jones struct ethtool_drvinfo *info) 216366846048SRick Jones { 216466846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 216566846048SRick Jones struct virtio_device *vdev = vi->vdev; 216666846048SRick Jones 216766846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 216866846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 216966846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 217066846048SRick Jones 217166846048SRick Jones } 217266846048SRick Jones 2173d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 2174d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 2175d73bcd2cSJason Wang struct ethtool_channels *channels) 2176d73bcd2cSJason Wang { 2177d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2178d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 2179d73bcd2cSJason Wang int err; 2180d73bcd2cSJason Wang 2181d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 2182d73bcd2cSJason Wang * We don't allow setting 'other' channels. 2183d73bcd2cSJason Wang */ 2184d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 2185d73bcd2cSJason Wang return -EINVAL; 2186d73bcd2cSJason Wang 2187c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 2188d73bcd2cSJason Wang return -EINVAL; 2189d73bcd2cSJason Wang 2190f600b690SJohn Fastabend /* For now we don't support modifying channels while XDP is loaded 2191f600b690SJohn Fastabend * also when XDP is loaded all RX queues have XDP programs so we only 2192f600b690SJohn Fastabend * need to check a single RX queue. 2193f600b690SJohn Fastabend */ 2194f600b690SJohn Fastabend if (vi->rq[0].xdp_prog) 2195f600b690SJohn Fastabend return -EINVAL; 2196f600b690SJohn Fastabend 219747be2479SWanlong Gao get_online_cpus(); 219847315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 2199de33212fSJeff Dike if (err) { 2200de33212fSJeff Dike put_online_cpus(); 2201de33212fSJeff Dike goto err; 2202d73bcd2cSJason Wang } 2203de33212fSJeff Dike virtnet_set_affinity(vi); 220447be2479SWanlong Gao put_online_cpus(); 2205d73bcd2cSJason Wang 2206de33212fSJeff Dike netif_set_real_num_tx_queues(dev, queue_pairs); 2207de33212fSJeff Dike netif_set_real_num_rx_queues(dev, queue_pairs); 2208de33212fSJeff Dike err: 2209d73bcd2cSJason Wang return err; 2210d73bcd2cSJason Wang } 2211d73bcd2cSJason Wang 2212d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 2213d7dfc5cfSToshiaki Makita { 2214d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2215d7dfc5cfSToshiaki Makita unsigned int i, j; 2216d7a9a01bSAlexander Duyck u8 *p = data; 2217d7dfc5cfSToshiaki Makita 2218d7dfc5cfSToshiaki Makita switch (stringset) { 2219d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2220d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2221d7a9a01bSAlexander Duyck for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) 2222d7a9a01bSAlexander Duyck ethtool_sprintf(&p, "rx_queue_%u_%s", i, 2223d7a9a01bSAlexander Duyck virtnet_rq_stats_desc[j].desc); 2224d7dfc5cfSToshiaki Makita } 2225d7dfc5cfSToshiaki Makita 2226d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2227d7a9a01bSAlexander Duyck for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) 2228d7a9a01bSAlexander Duyck ethtool_sprintf(&p, "tx_queue_%u_%s", i, 2229d7a9a01bSAlexander Duyck virtnet_sq_stats_desc[j].desc); 2230d7dfc5cfSToshiaki Makita } 2231d7dfc5cfSToshiaki Makita break; 2232d7dfc5cfSToshiaki Makita } 2233d7dfc5cfSToshiaki Makita } 2234d7dfc5cfSToshiaki Makita 2235d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset) 2236d7dfc5cfSToshiaki Makita { 2237d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2238d7dfc5cfSToshiaki Makita 2239d7dfc5cfSToshiaki Makita switch (sset) { 2240d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2241d7dfc5cfSToshiaki Makita return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + 2242d7dfc5cfSToshiaki Makita VIRTNET_SQ_STATS_LEN); 2243d7dfc5cfSToshiaki Makita default: 2244d7dfc5cfSToshiaki Makita return -EOPNOTSUPP; 2245d7dfc5cfSToshiaki Makita } 2246d7dfc5cfSToshiaki Makita } 2247d7dfc5cfSToshiaki Makita 2248d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev, 2249d7dfc5cfSToshiaki Makita struct ethtool_stats *stats, u64 *data) 2250d7dfc5cfSToshiaki Makita { 2251d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2252d7dfc5cfSToshiaki Makita unsigned int idx = 0, start, i, j; 2253d7dfc5cfSToshiaki Makita const u8 *stats_base; 2254d7dfc5cfSToshiaki Makita size_t offset; 2255d7dfc5cfSToshiaki Makita 2256d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2257d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 2258d7dfc5cfSToshiaki Makita 2259d46eeeafSJason Wang stats_base = (u8 *)&rq->stats; 2260d7dfc5cfSToshiaki Makita do { 2261d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 2262d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2263d7dfc5cfSToshiaki Makita offset = virtnet_rq_stats_desc[j].offset; 2264d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2265d7dfc5cfSToshiaki Makita } 2266d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 2267d7dfc5cfSToshiaki Makita idx += VIRTNET_RQ_STATS_LEN; 2268d7dfc5cfSToshiaki Makita } 2269d7dfc5cfSToshiaki Makita 2270d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2271d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 2272d7dfc5cfSToshiaki Makita 2273d7dfc5cfSToshiaki Makita stats_base = (u8 *)&sq->stats; 2274d7dfc5cfSToshiaki Makita do { 2275d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 2276d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 2277d7dfc5cfSToshiaki Makita offset = virtnet_sq_stats_desc[j].offset; 2278d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2279d7dfc5cfSToshiaki Makita } 2280d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 2281d7dfc5cfSToshiaki Makita idx += VIRTNET_SQ_STATS_LEN; 2282d7dfc5cfSToshiaki Makita } 2283d7dfc5cfSToshiaki Makita } 2284d7dfc5cfSToshiaki Makita 2285d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 2286d73bcd2cSJason Wang struct ethtool_channels *channels) 2287d73bcd2cSJason Wang { 2288d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2289d73bcd2cSJason Wang 2290d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 2291d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 2292d73bcd2cSJason Wang channels->max_other = 0; 2293d73bcd2cSJason Wang channels->rx_count = 0; 2294d73bcd2cSJason Wang channels->tx_count = 0; 2295d73bcd2cSJason Wang channels->other_count = 0; 2296d73bcd2cSJason Wang } 2297d73bcd2cSJason Wang 2298ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev, 2299ebb6b4b1SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 230016032be5SNikolay Aleksandrov { 230116032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 230216032be5SNikolay Aleksandrov 23039aedc6e2SCris Forno return ethtool_virtdev_set_link_ksettings(dev, cmd, 23049aedc6e2SCris Forno &vi->speed, &vi->duplex); 230516032be5SNikolay Aleksandrov } 230616032be5SNikolay Aleksandrov 2307ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev, 2308ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings *cmd) 230916032be5SNikolay Aleksandrov { 231016032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 231116032be5SNikolay Aleksandrov 2312ebb6b4b1SPhilippe Reynes cmd->base.speed = vi->speed; 2313ebb6b4b1SPhilippe Reynes cmd->base.duplex = vi->duplex; 2314ebb6b4b1SPhilippe Reynes cmd->base.port = PORT_OTHER; 231516032be5SNikolay Aleksandrov 231616032be5SNikolay Aleksandrov return 0; 231716032be5SNikolay Aleksandrov } 231816032be5SNikolay Aleksandrov 23190c465be1SJason Wang static int virtnet_set_coalesce(struct net_device *dev, 23200c465be1SJason Wang struct ethtool_coalesce *ec) 23210c465be1SJason Wang { 23220c465be1SJason Wang struct virtnet_info *vi = netdev_priv(dev); 23230c465be1SJason Wang int i, napi_weight; 23240c465be1SJason Wang 2325a51e5206SJakub Kicinski if (ec->tx_max_coalesced_frames > 1 || 2326a51e5206SJakub Kicinski ec->rx_max_coalesced_frames != 1) 23270c465be1SJason Wang return -EINVAL; 23280c465be1SJason Wang 23290c465be1SJason Wang napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 23300c465be1SJason Wang if (napi_weight ^ vi->sq[0].napi.weight) { 23310c465be1SJason Wang if (dev->flags & IFF_UP) 23320c465be1SJason Wang return -EBUSY; 23330c465be1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 23340c465be1SJason Wang vi->sq[i].napi.weight = napi_weight; 23350c465be1SJason Wang } 23360c465be1SJason Wang 23370c465be1SJason Wang return 0; 23380c465be1SJason Wang } 23390c465be1SJason Wang 23400c465be1SJason Wang static int virtnet_get_coalesce(struct net_device *dev, 23410c465be1SJason Wang struct ethtool_coalesce *ec) 23420c465be1SJason Wang { 23430c465be1SJason Wang struct ethtool_coalesce ec_default = { 23440c465be1SJason Wang .cmd = ETHTOOL_GCOALESCE, 23450c465be1SJason Wang .rx_max_coalesced_frames = 1, 23460c465be1SJason Wang }; 23470c465be1SJason Wang struct virtnet_info *vi = netdev_priv(dev); 23480c465be1SJason Wang 23490c465be1SJason Wang memcpy(ec, &ec_default, sizeof(ec_default)); 23500c465be1SJason Wang 23510c465be1SJason Wang if (vi->sq[0].napi.weight) 23520c465be1SJason Wang ec->tx_max_coalesced_frames = 1; 23530c465be1SJason Wang 23540c465be1SJason Wang return 0; 23550c465be1SJason Wang } 23560c465be1SJason Wang 235716032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 235816032be5SNikolay Aleksandrov { 235916032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 236016032be5SNikolay Aleksandrov 236116032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 236216032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 236316032be5SNikolay Aleksandrov } 236416032be5SNikolay Aleksandrov 2365faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi) 2366faa9b39fSJason Baron { 2367faa9b39fSJason Baron u32 speed; 2368faa9b39fSJason Baron u8 duplex; 2369faa9b39fSJason Baron 2370faa9b39fSJason Baron if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 2371faa9b39fSJason Baron return; 2372faa9b39fSJason Baron 237364ffa39dSMichael S. Tsirkin virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); 237464ffa39dSMichael S. Tsirkin 2375faa9b39fSJason Baron if (ethtool_validate_speed(speed)) 2376faa9b39fSJason Baron vi->speed = speed; 237764ffa39dSMichael S. Tsirkin 237864ffa39dSMichael S. Tsirkin virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); 237964ffa39dSMichael S. Tsirkin 2380faa9b39fSJason Baron if (ethtool_validate_duplex(duplex)) 2381faa9b39fSJason Baron vi->duplex = duplex; 2382faa9b39fSJason Baron } 2383faa9b39fSJason Baron 23840fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 2385a51e5206SJakub Kicinski .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES, 238666846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 23879f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 23888f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 2389d7dfc5cfSToshiaki Makita .get_strings = virtnet_get_strings, 2390d7dfc5cfSToshiaki Makita .get_sset_count = virtnet_get_sset_count, 2391d7dfc5cfSToshiaki Makita .get_ethtool_stats = virtnet_get_ethtool_stats, 2392d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 2393d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 2394074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 2395ebb6b4b1SPhilippe Reynes .get_link_ksettings = virtnet_get_link_ksettings, 2396ebb6b4b1SPhilippe Reynes .set_link_ksettings = virtnet_set_link_ksettings, 23970c465be1SJason Wang .set_coalesce = virtnet_set_coalesce, 23980c465be1SJason Wang .get_coalesce = virtnet_get_coalesce, 2399a9ea3fc6SHerbert Xu }; 2400a9ea3fc6SHerbert Xu 24019fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev) 24029fe7bfceSJohn Fastabend { 24039fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 24049fe7bfceSJohn Fastabend int i; 24059fe7bfceSJohn Fastabend 24069fe7bfceSJohn Fastabend /* Make sure no work handler is accessing the device */ 24079fe7bfceSJohn Fastabend flush_work(&vi->config_work); 24089fe7bfceSJohn Fastabend 240905c998b7SAke Koomsin netif_tx_lock_bh(vi->dev); 24109fe7bfceSJohn Fastabend netif_device_detach(vi->dev); 241105c998b7SAke Koomsin netif_tx_unlock_bh(vi->dev); 24129fe7bfceSJohn Fastabend cancel_delayed_work_sync(&vi->refill); 24139fe7bfceSJohn Fastabend 24149fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 2415b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 24169fe7bfceSJohn Fastabend napi_disable(&vi->rq[i].napi); 241778a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 2418b92f1e67SWillem de Bruijn } 24199fe7bfceSJohn Fastabend } 24209fe7bfceSJohn Fastabend } 24219fe7bfceSJohn Fastabend 24229fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi); 24239fe7bfceSJohn Fastabend 24249fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev) 24259fe7bfceSJohn Fastabend { 24269fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 24279fe7bfceSJohn Fastabend int err, i; 24289fe7bfceSJohn Fastabend 24299fe7bfceSJohn Fastabend err = init_vqs(vi); 24309fe7bfceSJohn Fastabend if (err) 24319fe7bfceSJohn Fastabend return err; 24329fe7bfceSJohn Fastabend 24339fe7bfceSJohn Fastabend virtio_device_ready(vdev); 24349fe7bfceSJohn Fastabend 24359fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 24369fe7bfceSJohn Fastabend for (i = 0; i < vi->curr_queue_pairs; i++) 24379fe7bfceSJohn Fastabend if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 24389fe7bfceSJohn Fastabend schedule_delayed_work(&vi->refill, 0); 24399fe7bfceSJohn Fastabend 2440b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 2441e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2442b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2443b92f1e67SWillem de Bruijn &vi->sq[i].napi); 2444b92f1e67SWillem de Bruijn } 24459fe7bfceSJohn Fastabend } 24469fe7bfceSJohn Fastabend 244705c998b7SAke Koomsin netif_tx_lock_bh(vi->dev); 24489fe7bfceSJohn Fastabend netif_device_attach(vi->dev); 244905c998b7SAke Koomsin netif_tx_unlock_bh(vi->dev); 24509fe7bfceSJohn Fastabend return err; 24519fe7bfceSJohn Fastabend } 24529fe7bfceSJohn Fastabend 24533f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 24543f93522fSJason Wang { 24553f93522fSJason Wang struct scatterlist sg; 245612e57169SMichael S. Tsirkin vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); 24573f93522fSJason Wang 245812e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); 24593f93522fSJason Wang 24603f93522fSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 24613f93522fSJason Wang VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 24623f93522fSJason Wang dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); 24633f93522fSJason Wang return -EINVAL; 24643f93522fSJason Wang } 24653f93522fSJason Wang 24663f93522fSJason Wang return 0; 24673f93522fSJason Wang } 24683f93522fSJason Wang 24693f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 24703f93522fSJason Wang { 24713f93522fSJason Wang u64 offloads = 0; 24723f93522fSJason Wang 24733f93522fSJason Wang if (!vi->guest_offloads) 24743f93522fSJason Wang return 0; 24753f93522fSJason Wang 24763f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 24773f93522fSJason Wang } 24783f93522fSJason Wang 24793f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 24803f93522fSJason Wang { 24813f93522fSJason Wang u64 offloads = vi->guest_offloads; 24823f93522fSJason Wang 24833f93522fSJason Wang if (!vi->guest_offloads) 24843f93522fSJason Wang return 0; 24853f93522fSJason Wang 24863f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 24873f93522fSJason Wang } 24883f93522fSJason Wang 24899861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 24909861ce03SJakub Kicinski struct netlink_ext_ack *extack) 2491f600b690SJohn Fastabend { 2492f600b690SJohn Fastabend unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); 2493f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 2494f600b690SJohn Fastabend struct bpf_prog *old_prog; 2495017b29c3SJason Wang u16 xdp_qp = 0, curr_qp; 2496672aafd5SJohn Fastabend int i, err; 2497f600b690SJohn Fastabend 24983f93522fSJason Wang if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 24993f93522fSJason Wang && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 250092502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 250192502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 250218ba58e1SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 250318ba58e1SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) { 250418ba58e1SJason Wang NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first"); 2505f600b690SJohn Fastabend return -EOPNOTSUPP; 2506f600b690SJohn Fastabend } 2507f600b690SJohn Fastabend 2508f600b690SJohn Fastabend if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 25094d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 2510f600b690SJohn Fastabend return -EINVAL; 2511f600b690SJohn Fastabend } 2512f600b690SJohn Fastabend 2513f600b690SJohn Fastabend if (dev->mtu > max_sz) { 25144d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); 2515f600b690SJohn Fastabend netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); 2516f600b690SJohn Fastabend return -EINVAL; 2517f600b690SJohn Fastabend } 2518f600b690SJohn Fastabend 2519672aafd5SJohn Fastabend curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 2520672aafd5SJohn Fastabend if (prog) 2521672aafd5SJohn Fastabend xdp_qp = nr_cpu_ids; 2522672aafd5SJohn Fastabend 2523672aafd5SJohn Fastabend /* XDP requires extra queues for XDP_TX */ 2524672aafd5SJohn Fastabend if (curr_qp + xdp_qp > vi->max_queue_pairs) { 252597c2c69eSXuan Zhuo netdev_warn(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n", 2526672aafd5SJohn Fastabend curr_qp + xdp_qp, vi->max_queue_pairs); 252797c2c69eSXuan Zhuo xdp_qp = 0; 2528672aafd5SJohn Fastabend } 2529672aafd5SJohn Fastabend 253003aa6d34SToshiaki Makita old_prog = rtnl_dereference(vi->rq[0].xdp_prog); 253103aa6d34SToshiaki Makita if (!prog && !old_prog) 253203aa6d34SToshiaki Makita return 0; 253303aa6d34SToshiaki Makita 253485192dbfSAndrii Nakryiko if (prog) 253585192dbfSAndrii Nakryiko bpf_prog_add(prog, vi->max_queue_pairs - 1); 25362de2f7f4SJohn Fastabend 25374941d472SJason Wang /* Make sure NAPI is not using any XDP TX queues for RX. */ 2538534da5e8SToshiaki Makita if (netif_running(dev)) { 2539534da5e8SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 25404941d472SJason Wang napi_disable(&vi->rq[i].napi); 2541534da5e8SToshiaki Makita virtnet_napi_tx_disable(&vi->sq[i].napi); 2542534da5e8SToshiaki Makita } 2543534da5e8SToshiaki Makita } 25442de2f7f4SJohn Fastabend 254503aa6d34SToshiaki Makita if (!prog) { 254603aa6d34SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 254703aa6d34SToshiaki Makita rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 254803aa6d34SToshiaki Makita if (i == 0) 254903aa6d34SToshiaki Makita virtnet_restore_guest_offloads(vi); 255003aa6d34SToshiaki Makita } 255103aa6d34SToshiaki Makita synchronize_net(); 255203aa6d34SToshiaki Makita } 255303aa6d34SToshiaki Makita 25544941d472SJason Wang err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 25554941d472SJason Wang if (err) 25564941d472SJason Wang goto err; 2557188313c1SToshiaki Makita netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 25584941d472SJason Wang vi->xdp_queue_pairs = xdp_qp; 2559f600b690SJohn Fastabend 256003aa6d34SToshiaki Makita if (prog) { 256197c2c69eSXuan Zhuo vi->xdp_enabled = true; 2562f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 2563f600b690SJohn Fastabend rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 256403aa6d34SToshiaki Makita if (i == 0 && !old_prog) 25653f93522fSJason Wang virtnet_clear_guest_offloads(vi); 25663f93522fSJason Wang } 256797c2c69eSXuan Zhuo } else { 256897c2c69eSXuan Zhuo vi->xdp_enabled = false; 256903aa6d34SToshiaki Makita } 257003aa6d34SToshiaki Makita 257103aa6d34SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 2572f600b690SJohn Fastabend if (old_prog) 2573f600b690SJohn Fastabend bpf_prog_put(old_prog); 2574534da5e8SToshiaki Makita if (netif_running(dev)) { 25754941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2576534da5e8SToshiaki Makita virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2577534da5e8SToshiaki Makita &vi->sq[i].napi); 2578534da5e8SToshiaki Makita } 2579f600b690SJohn Fastabend } 2580f600b690SJohn Fastabend 2581f600b690SJohn Fastabend return 0; 25822de2f7f4SJohn Fastabend 25834941d472SJason Wang err: 258403aa6d34SToshiaki Makita if (!prog) { 258503aa6d34SToshiaki Makita virtnet_clear_guest_offloads(vi); 25864941d472SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 258703aa6d34SToshiaki Makita rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); 258803aa6d34SToshiaki Makita } 258903aa6d34SToshiaki Makita 25908be4d9a4SToshiaki Makita if (netif_running(dev)) { 2591534da5e8SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 25924941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2593534da5e8SToshiaki Makita virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2594534da5e8SToshiaki Makita &vi->sq[i].napi); 2595534da5e8SToshiaki Makita } 25968be4d9a4SToshiaki Makita } 25972de2f7f4SJohn Fastabend if (prog) 25982de2f7f4SJohn Fastabend bpf_prog_sub(prog, vi->max_queue_pairs - 1); 25992de2f7f4SJohn Fastabend return err; 2600f600b690SJohn Fastabend } 2601f600b690SJohn Fastabend 2602f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 2603f600b690SJohn Fastabend { 2604f600b690SJohn Fastabend switch (xdp->command) { 2605f600b690SJohn Fastabend case XDP_SETUP_PROG: 26069861ce03SJakub Kicinski return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 2607f600b690SJohn Fastabend default: 2608f600b690SJohn Fastabend return -EINVAL; 2609f600b690SJohn Fastabend } 2610f600b690SJohn Fastabend } 2611f600b690SJohn Fastabend 2612ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 2613ba5e4426SSridhar Samudrala size_t len) 2614ba5e4426SSridhar Samudrala { 2615ba5e4426SSridhar Samudrala struct virtnet_info *vi = netdev_priv(dev); 2616ba5e4426SSridhar Samudrala int ret; 2617ba5e4426SSridhar Samudrala 2618ba5e4426SSridhar Samudrala if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 2619ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2620ba5e4426SSridhar Samudrala 2621ba5e4426SSridhar Samudrala ret = snprintf(buf, len, "sby"); 2622ba5e4426SSridhar Samudrala if (ret >= len) 2623ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2624ba5e4426SSridhar Samudrala 2625ba5e4426SSridhar Samudrala return 0; 2626ba5e4426SSridhar Samudrala } 2627ba5e4426SSridhar Samudrala 2628a02e8964SWillem de Bruijn static int virtnet_set_features(struct net_device *dev, 2629a02e8964SWillem de Bruijn netdev_features_t features) 2630a02e8964SWillem de Bruijn { 2631a02e8964SWillem de Bruijn struct virtnet_info *vi = netdev_priv(dev); 2632cf8691cbSMichael S. Tsirkin u64 offloads; 2633a02e8964SWillem de Bruijn int err; 2634a02e8964SWillem de Bruijn 2635cf8691cbSMichael S. Tsirkin if ((dev->features ^ features) & NETIF_F_LRO) { 263697c2c69eSXuan Zhuo if (vi->xdp_enabled) 2637a02e8964SWillem de Bruijn return -EBUSY; 2638a02e8964SWillem de Bruijn 2639a02e8964SWillem de Bruijn if (features & NETIF_F_LRO) 2640cf8691cbSMichael S. Tsirkin offloads = vi->guest_offloads_capable; 2641a02e8964SWillem de Bruijn else 2642cf8691cbSMichael S. Tsirkin offloads = vi->guest_offloads_capable & 2643cf8691cbSMichael S. Tsirkin ~GUEST_OFFLOAD_LRO_MASK; 2644a02e8964SWillem de Bruijn 2645a02e8964SWillem de Bruijn err = virtnet_set_guest_offloads(vi, offloads); 2646a02e8964SWillem de Bruijn if (err) 2647a02e8964SWillem de Bruijn return err; 26483618ad2aSTonghao Zhang vi->guest_offloads = offloads; 2649cf8691cbSMichael S. Tsirkin } 2650cf8691cbSMichael S. Tsirkin 2651a02e8964SWillem de Bruijn return 0; 2652a02e8964SWillem de Bruijn } 2653a02e8964SWillem de Bruijn 265476288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 265576288b4eSStephen Hemminger .ndo_open = virtnet_open, 265676288b4eSStephen Hemminger .ndo_stop = virtnet_close, 265776288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 265876288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 26599c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 26602af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 26613fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 26621824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 26631824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 2664f4e63525SJakub Kicinski .ndo_bpf = virtnet_xdp, 2665186b3c99SJason Wang .ndo_xdp_xmit = virtnet_xdp_xmit, 26662836b4f2SVlad Yasevich .ndo_features_check = passthru_features_check, 2667ba5e4426SSridhar Samudrala .ndo_get_phys_port_name = virtnet_get_phys_port_name, 2668a02e8964SWillem de Bruijn .ndo_set_features = virtnet_set_features, 266976288b4eSStephen Hemminger }; 267076288b4eSStephen Hemminger 2671586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 26729f4d26d0SMark McLoughlin { 2673586d17c5SJason Wang struct virtnet_info *vi = 2674586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 26759f4d26d0SMark McLoughlin u16 v; 26769f4d26d0SMark McLoughlin 2677855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 2678855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 2679507613bfSMichael S. Tsirkin return; 2680586d17c5SJason Wang 2681586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 2682ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 2683586d17c5SJason Wang virtnet_ack_link_announce(vi); 2684586d17c5SJason Wang } 26859f4d26d0SMark McLoughlin 26869f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 26879f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 26889f4d26d0SMark McLoughlin 26899f4d26d0SMark McLoughlin if (vi->status == v) 2690507613bfSMichael S. Tsirkin return; 26919f4d26d0SMark McLoughlin 26929f4d26d0SMark McLoughlin vi->status = v; 26939f4d26d0SMark McLoughlin 26949f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 2695faa9b39fSJason Baron virtnet_update_settings(vi); 26969f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 2697986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 26989f4d26d0SMark McLoughlin } else { 26999f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 2700986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 27019f4d26d0SMark McLoughlin } 27029f4d26d0SMark McLoughlin } 27039f4d26d0SMark McLoughlin 27049f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 27059f4d26d0SMark McLoughlin { 27069f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 27079f4d26d0SMark McLoughlin 27083b07e9caSTejun Heo schedule_work(&vi->config_work); 27099f4d26d0SMark McLoughlin } 27109f4d26d0SMark McLoughlin 2711986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 2712986a4f4dSJason Wang { 2713d4fb84eeSAndrey Vagin int i; 2714d4fb84eeSAndrey Vagin 2715ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 27165198d545SJakub Kicinski __netif_napi_del(&vi->rq[i].napi); 27175198d545SJakub Kicinski __netif_napi_del(&vi->sq[i].napi); 2718ab3971b1SJason Wang } 2719d4fb84eeSAndrey Vagin 27205198d545SJakub Kicinski /* We called __netif_napi_del(), 2721963abe5cSEric Dumazet * we need to respect an RCU grace period before freeing vi->rq 2722963abe5cSEric Dumazet */ 2723963abe5cSEric Dumazet synchronize_net(); 2724963abe5cSEric Dumazet 2725986a4f4dSJason Wang kfree(vi->rq); 2726986a4f4dSJason Wang kfree(vi->sq); 272712e57169SMichael S. Tsirkin kfree(vi->ctrl); 2728986a4f4dSJason Wang } 2729986a4f4dSJason Wang 273047315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi) 2731986a4f4dSJason Wang { 2732f600b690SJohn Fastabend struct bpf_prog *old_prog; 2733986a4f4dSJason Wang int i; 2734986a4f4dSJason Wang 2735986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2736986a4f4dSJason Wang while (vi->rq[i].pages) 2737986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 2738f600b690SJohn Fastabend 2739f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2740f600b690SJohn Fastabend RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 2741f600b690SJohn Fastabend if (old_prog) 2742f600b690SJohn Fastabend bpf_prog_put(old_prog); 2743986a4f4dSJason Wang } 274447315329SJohn Fastabend } 274547315329SJohn Fastabend 274647315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi) 274747315329SJohn Fastabend { 274847315329SJohn Fastabend rtnl_lock(); 274947315329SJohn Fastabend _free_receive_bufs(vi); 2750f600b690SJohn Fastabend rtnl_unlock(); 2751986a4f4dSJason Wang } 2752986a4f4dSJason Wang 2753fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 2754fb51879dSMichael Dalton { 2755fb51879dSMichael Dalton int i; 2756fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 2757fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 2758fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 2759fb51879dSMichael Dalton } 2760fb51879dSMichael Dalton 2761986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 2762986a4f4dSJason Wang { 2763986a4f4dSJason Wang void *buf; 2764986a4f4dSJason Wang int i; 2765986a4f4dSJason Wang 2766986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2767986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 276856434a01SJohn Fastabend while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 27695050471dSToshiaki Makita if (!is_xdp_frame(buf)) 2770986a4f4dSJason Wang dev_kfree_skb(buf); 277156434a01SJohn Fastabend else 27725050471dSToshiaki Makita xdp_return_frame(ptr_to_xdp(buf)); 277356434a01SJohn Fastabend } 2774986a4f4dSJason Wang } 2775986a4f4dSJason Wang 2776986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2777986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 2778986a4f4dSJason Wang 2779986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2780ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 2781680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 2782ab7db917SMichael Dalton } else if (vi->big_packets) { 2783fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 2784ab7db917SMichael Dalton } else { 2785f6b10209SJason Wang put_page(virt_to_head_page(buf)); 2786986a4f4dSJason Wang } 2787986a4f4dSJason Wang } 2788986a4f4dSJason Wang } 2789ab7db917SMichael Dalton } 2790986a4f4dSJason Wang 2791e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 2792e9d7417bSJason Wang { 2793e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 2794e9d7417bSJason Wang 2795310974faSPeter Xu virtnet_clean_affinity(vi); 2796986a4f4dSJason Wang 2797e9d7417bSJason Wang vdev->config->del_vqs(vdev); 2798986a4f4dSJason Wang 2799986a4f4dSJason Wang virtnet_free_queues(vi); 2800986a4f4dSJason Wang } 2801986a4f4dSJason Wang 2802d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at 2803d85b758fSMichael S. Tsirkin * least one full packet? 2804d85b758fSMichael S. Tsirkin * Logic below assumes the mergeable buffer header is used. 2805d85b758fSMichael S. Tsirkin */ 2806d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 2807d85b758fSMichael S. Tsirkin { 2808d85b758fSMichael S. Tsirkin const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2809d85b758fSMichael S. Tsirkin unsigned int rq_size = virtqueue_get_vring_size(vq); 2810d85b758fSMichael S. Tsirkin unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 2811d85b758fSMichael S. Tsirkin unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 2812d85b758fSMichael S. Tsirkin unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 2813d85b758fSMichael S. Tsirkin 2814f0c3192cSMichael S. Tsirkin return max(max(min_buf_len, hdr_len) - hdr_len, 2815f0c3192cSMichael S. Tsirkin (unsigned int)GOOD_PACKET_LEN); 2816d85b758fSMichael S. Tsirkin } 2817d85b758fSMichael S. Tsirkin 2818986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 2819986a4f4dSJason Wang { 2820986a4f4dSJason Wang vq_callback_t **callbacks; 2821986a4f4dSJason Wang struct virtqueue **vqs; 2822986a4f4dSJason Wang int ret = -ENOMEM; 2823986a4f4dSJason Wang int i, total_vqs; 2824986a4f4dSJason Wang const char **names; 2825d45b897bSMichael S. Tsirkin bool *ctx; 2826986a4f4dSJason Wang 2827986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 2828986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 2829986a4f4dSJason Wang * possible control vq. 2830986a4f4dSJason Wang */ 2831986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 2832986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 2833986a4f4dSJason Wang 2834986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 28356396bb22SKees Cook vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 2836986a4f4dSJason Wang if (!vqs) 2837986a4f4dSJason Wang goto err_vq; 28386da2ec56SKees Cook callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL); 2839986a4f4dSJason Wang if (!callbacks) 2840986a4f4dSJason Wang goto err_callback; 28416da2ec56SKees Cook names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL); 2842986a4f4dSJason Wang if (!names) 2843986a4f4dSJason Wang goto err_names; 2844192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 28456396bb22SKees Cook ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 2846d45b897bSMichael S. Tsirkin if (!ctx) 2847d45b897bSMichael S. Tsirkin goto err_ctx; 2848d45b897bSMichael S. Tsirkin } else { 2849d45b897bSMichael S. Tsirkin ctx = NULL; 2850d45b897bSMichael S. Tsirkin } 2851986a4f4dSJason Wang 2852986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 2853986a4f4dSJason Wang if (vi->has_cvq) { 2854986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 2855986a4f4dSJason Wang names[total_vqs - 1] = "control"; 2856986a4f4dSJason Wang } 2857986a4f4dSJason Wang 2858986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 2859986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2860986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 2861986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 2862986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 2863986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 2864986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 2865986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 2866d45b897bSMichael S. Tsirkin if (ctx) 2867d45b897bSMichael S. Tsirkin ctx[rxq2vq(i)] = true; 2868986a4f4dSJason Wang } 2869986a4f4dSJason Wang 2870a2f7dc00SXianting Tian ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks, 2871d45b897bSMichael S. Tsirkin names, ctx, NULL); 2872986a4f4dSJason Wang if (ret) 2873986a4f4dSJason Wang goto err_find; 2874986a4f4dSJason Wang 2875986a4f4dSJason Wang if (vi->has_cvq) { 2876986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 2877986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 2878f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2879986a4f4dSJason Wang } 2880986a4f4dSJason Wang 2881986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2882986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 2883d85b758fSMichael S. Tsirkin vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 2884986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 2885986a4f4dSJason Wang } 2886986a4f4dSJason Wang 28872fa3c8a8STonghao Zhang /* run here: ret == 0. */ 2888986a4f4dSJason Wang 2889986a4f4dSJason Wang 2890986a4f4dSJason Wang err_find: 2891d45b897bSMichael S. Tsirkin kfree(ctx); 2892d45b897bSMichael S. Tsirkin err_ctx: 2893986a4f4dSJason Wang kfree(names); 2894986a4f4dSJason Wang err_names: 2895986a4f4dSJason Wang kfree(callbacks); 2896986a4f4dSJason Wang err_callback: 2897986a4f4dSJason Wang kfree(vqs); 2898986a4f4dSJason Wang err_vq: 2899986a4f4dSJason Wang return ret; 2900986a4f4dSJason Wang } 2901986a4f4dSJason Wang 2902986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 2903986a4f4dSJason Wang { 2904986a4f4dSJason Wang int i; 2905986a4f4dSJason Wang 2906122b84a1SMax Gurtovoy if (vi->has_cvq) { 290712e57169SMichael S. Tsirkin vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 290812e57169SMichael S. Tsirkin if (!vi->ctrl) 290912e57169SMichael S. Tsirkin goto err_ctrl; 2910122b84a1SMax Gurtovoy } else { 2911122b84a1SMax Gurtovoy vi->ctrl = NULL; 2912122b84a1SMax Gurtovoy } 29136396bb22SKees Cook vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 2914986a4f4dSJason Wang if (!vi->sq) 2915986a4f4dSJason Wang goto err_sq; 29166396bb22SKees Cook vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 2917008d4278SAmerigo Wang if (!vi->rq) 2918986a4f4dSJason Wang goto err_rq; 2919986a4f4dSJason Wang 2920986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 2921986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2922986a4f4dSJason Wang vi->rq[i].pages = NULL; 2923986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2924986a4f4dSJason Wang napi_weight); 29251d11e732SWillem de Bruijn netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2926b92f1e67SWillem de Bruijn napi_tx ? napi_weight : 0); 2927986a4f4dSJason Wang 2928986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 29295377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 2930986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 2931d7dfc5cfSToshiaki Makita 2932d7dfc5cfSToshiaki Makita u64_stats_init(&vi->rq[i].stats.syncp); 2933d7dfc5cfSToshiaki Makita u64_stats_init(&vi->sq[i].stats.syncp); 2934986a4f4dSJason Wang } 2935986a4f4dSJason Wang 2936986a4f4dSJason Wang return 0; 2937986a4f4dSJason Wang 2938986a4f4dSJason Wang err_rq: 2939986a4f4dSJason Wang kfree(vi->sq); 2940986a4f4dSJason Wang err_sq: 294112e57169SMichael S. Tsirkin kfree(vi->ctrl); 294212e57169SMichael S. Tsirkin err_ctrl: 2943986a4f4dSJason Wang return -ENOMEM; 2944e9d7417bSJason Wang } 2945e9d7417bSJason Wang 29463f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 29473f9c10b0SAmit Shah { 2948986a4f4dSJason Wang int ret; 29493f9c10b0SAmit Shah 2950986a4f4dSJason Wang /* Allocate send & receive queues */ 2951986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 2952986a4f4dSJason Wang if (ret) 2953986a4f4dSJason Wang goto err; 29543f9c10b0SAmit Shah 2955986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 2956986a4f4dSJason Wang if (ret) 2957986a4f4dSJason Wang goto err_free; 29583f9c10b0SAmit Shah 295947be2479SWanlong Gao get_online_cpus(); 29608898c21cSWanlong Gao virtnet_set_affinity(vi); 296147be2479SWanlong Gao put_online_cpus(); 296247be2479SWanlong Gao 29633f9c10b0SAmit Shah return 0; 2964986a4f4dSJason Wang 2965986a4f4dSJason Wang err_free: 2966986a4f4dSJason Wang virtnet_free_queues(vi); 2967986a4f4dSJason Wang err: 2968986a4f4dSJason Wang return ret; 29693f9c10b0SAmit Shah } 29703f9c10b0SAmit Shah 2971fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2972fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 2973718ad681Sstephen hemminger char *buf) 2974fbf28d78SMichael Dalton { 2975fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 2976fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 29773cc81a9aSJason Wang unsigned int headroom = virtnet_get_headroom(vi); 29783cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 29795377d758SJohannes Berg struct ewma_pkt_len *avg; 2980fbf28d78SMichael Dalton 2981fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 2982fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 2983d85b758fSMichael S. Tsirkin return sprintf(buf, "%u\n", 29843cc81a9aSJason Wang get_mergeable_buf_len(&vi->rq[queue_index], avg, 29853cc81a9aSJason Wang SKB_DATA_ALIGN(headroom + tailroom))); 2986fbf28d78SMichael Dalton } 2987fbf28d78SMichael Dalton 2988fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 2989fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 2990fbf28d78SMichael Dalton 2991fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 2992fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 2993fbf28d78SMichael Dalton NULL 2994fbf28d78SMichael Dalton }; 2995fbf28d78SMichael Dalton 2996fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 2997fbf28d78SMichael Dalton .name = "virtio_net", 2998fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 2999fbf28d78SMichael Dalton }; 3000fbf28d78SMichael Dalton #endif 3001fbf28d78SMichael Dalton 3002892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 3003892d6eb1SJason Wang unsigned int fbit, 3004892d6eb1SJason Wang const char *fname, const char *dname) 3005892d6eb1SJason Wang { 3006892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 3007892d6eb1SJason Wang return false; 3008892d6eb1SJason Wang 3009892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 3010892d6eb1SJason Wang fname, dname); 3011892d6eb1SJason Wang 3012892d6eb1SJason Wang return true; 3013892d6eb1SJason Wang } 3014892d6eb1SJason Wang 3015892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 3016892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 3017892d6eb1SJason Wang 3018892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 3019892d6eb1SJason Wang { 3020892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 3021892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 3022892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3023892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 3024892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3025892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 3026892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3027892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 3028892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 3029892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 3030892d6eb1SJason Wang return false; 3031892d6eb1SJason Wang } 3032892d6eb1SJason Wang 3033892d6eb1SJason Wang return true; 3034892d6eb1SJason Wang } 3035892d6eb1SJason Wang 3036d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU 3037d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU 3038d0c2c997SJarod Wilson 3039fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev) 3040296f96fcSRusty Russell { 30416ba42248SMichael S. Tsirkin if (!vdev->config->get) { 30426ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 30436ba42248SMichael S. Tsirkin __func__); 30446ba42248SMichael S. Tsirkin return -EINVAL; 30456ba42248SMichael S. Tsirkin } 30466ba42248SMichael S. Tsirkin 3047892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 3048892d6eb1SJason Wang return -EINVAL; 3049892d6eb1SJason Wang 3050fe36cbe0SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 3051fe36cbe0SMichael S. Tsirkin int mtu = virtio_cread16(vdev, 3052fe36cbe0SMichael S. Tsirkin offsetof(struct virtio_net_config, 3053fe36cbe0SMichael S. Tsirkin mtu)); 3054fe36cbe0SMichael S. Tsirkin if (mtu < MIN_MTU) 3055fe36cbe0SMichael S. Tsirkin __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 3056fe36cbe0SMichael S. Tsirkin } 3057fe36cbe0SMichael S. Tsirkin 3058fe36cbe0SMichael S. Tsirkin return 0; 3059fe36cbe0SMichael S. Tsirkin } 3060fe36cbe0SMichael S. Tsirkin 3061fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev) 3062fe36cbe0SMichael S. Tsirkin { 3063d7dfc5cfSToshiaki Makita int i, err = -ENOMEM; 3064fe36cbe0SMichael S. Tsirkin struct net_device *dev; 3065fe36cbe0SMichael S. Tsirkin struct virtnet_info *vi; 3066fe36cbe0SMichael S. Tsirkin u16 max_queue_pairs; 3067fe36cbe0SMichael S. Tsirkin int mtu; 3068fe36cbe0SMichael S. Tsirkin 3069986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 3070855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 3071855e0c52SRusty Russell struct virtio_net_config, 3072855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 3073986a4f4dSJason Wang 3074986a4f4dSJason Wang /* We need at least 2 queue's */ 3075986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 3076986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 3077986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 3078986a4f4dSJason Wang max_queue_pairs = 1; 3079296f96fcSRusty Russell 3080296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 3081986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 3082296f96fcSRusty Russell if (!dev) 3083296f96fcSRusty Russell return -ENOMEM; 3084296f96fcSRusty Russell 3085296f96fcSRusty Russell /* Set up network device as normal. */ 3086ab5bd583SXuan Zhuo dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE | 3087ab5bd583SXuan Zhuo IFF_TX_SKB_NO_LINEAR; 308876288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 3089296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 30903fa2a1dfSstephen hemminger 30917ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 3092296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 3093296f96fcSRusty Russell 3094296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 309598e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 3096296f96fcSRusty Russell /* This opens up the world of extra features. */ 309748900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 309898e778c9SMichał Mirosław if (csum) 309948900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 310098e778c9SMichał Mirosław 310198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 3102e078de03SDavid S. Miller dev->hw_features |= NETIF_F_TSO 310334a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 310434a48579SRusty Russell } 31055539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 310698e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 310798e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 310898e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 310998e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 311098e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 311198e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 311298e778c9SMichał Mirosław 311341f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 311441f2f127SJason Wang 311598e778c9SMichał Mirosław if (gso) 3116e078de03SDavid S. Miller dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 311798e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 3118296f96fcSRusty Russell } 31194f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 31204f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 3121a02e8964SWillem de Bruijn if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 3122a02e8964SWillem de Bruijn virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)) 3123a02e8964SWillem de Bruijn dev->features |= NETIF_F_LRO; 3124cf8691cbSMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) 3125a02e8964SWillem de Bruijn dev->hw_features |= NETIF_F_LRO; 3126296f96fcSRusty Russell 31274fda8302SJason Wang dev->vlan_features = dev->features; 31284fda8302SJason Wang 3129d0c2c997SJarod Wilson /* MTU range: 68 - 65535 */ 3130d0c2c997SJarod Wilson dev->min_mtu = MIN_MTU; 3131d0c2c997SJarod Wilson dev->max_mtu = MAX_MTU; 3132d0c2c997SJarod Wilson 3133296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 3134855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 3135855e0c52SRusty Russell virtio_cread_bytes(vdev, 3136a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 3137855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 3138855e0c52SRusty Russell else 3139f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 3140296f96fcSRusty Russell 3141296f96fcSRusty Russell /* Set up our device-specific information */ 3142296f96fcSRusty Russell vi = netdev_priv(dev); 3143296f96fcSRusty Russell vi->dev = dev; 3144296f96fcSRusty Russell vi->vdev = vdev; 3145d9d5dcc8SChristian Borntraeger vdev->priv = vi; 3146827da44cSJohn Stultz 3147586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 3148296f96fcSRusty Russell 314997402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 31508e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 31518e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 3152e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 3153e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 315497402b96SHerbert Xu vi->big_packets = true; 315597402b96SHerbert Xu 31563f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 31573f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 31583f2c31d9SMark McLoughlin 3159d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 3160d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 3161012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 3162012873d0SMichael S. Tsirkin else 3163012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 3164012873d0SMichael S. Tsirkin 316575993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 316675993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 3167e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 3168e7428e95SMichael S. Tsirkin 3169986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 3170986a4f4dSJason Wang vi->has_cvq = true; 3171986a4f4dSJason Wang 317214de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 317314de9d11SAaron Conole mtu = virtio_cread16(vdev, 317414de9d11SAaron Conole offsetof(struct virtio_net_config, 317514de9d11SAaron Conole mtu)); 317693a205eeSAaron Conole if (mtu < dev->min_mtu) { 3177fe36cbe0SMichael S. Tsirkin /* Should never trigger: MTU was previously validated 3178fe36cbe0SMichael S. Tsirkin * in virtnet_validate. 3179fe36cbe0SMichael S. Tsirkin */ 31807934b481SYuval Shaia dev_err(&vdev->dev, 31817934b481SYuval Shaia "device MTU appears to have changed it is now %d < %d", 31827934b481SYuval Shaia mtu, dev->min_mtu); 3183411ea23aSDan Carpenter err = -EINVAL; 3184d7dfc5cfSToshiaki Makita goto free; 3185fe36cbe0SMichael S. Tsirkin } 3186fe36cbe0SMichael S. Tsirkin 3187d0c2c997SJarod Wilson dev->mtu = mtu; 318893a205eeSAaron Conole dev->max_mtu = mtu; 31892e123b44SMichael S. Tsirkin 31902e123b44SMichael S. Tsirkin /* TODO: size buffers correctly in this case. */ 31912e123b44SMichael S. Tsirkin if (dev->mtu > ETH_DATA_LEN) 31922e123b44SMichael S. Tsirkin vi->big_packets = true; 319314de9d11SAaron Conole } 319414de9d11SAaron Conole 3195012873d0SMichael S. Tsirkin if (vi->any_header_sg) 3196012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 31976ebbc1a6SZhangjie \(HZ\) 319844900010SJason Wang /* Enable multiqueue by default */ 319944900010SJason Wang if (num_online_cpus() >= max_queue_pairs) 320044900010SJason Wang vi->curr_queue_pairs = max_queue_pairs; 320144900010SJason Wang else 320244900010SJason Wang vi->curr_queue_pairs = num_online_cpus(); 3203986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 3204986a4f4dSJason Wang 3205986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 32063f9c10b0SAmit Shah err = init_vqs(vi); 3207d2a7dddaSMichael S. Tsirkin if (err) 3208d7dfc5cfSToshiaki Makita goto free; 3209d2a7dddaSMichael S. Tsirkin 3210fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 3211fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 3212fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 3213fbf28d78SMichael Dalton #endif 32140f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 32150f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 3216986a4f4dSJason Wang 321716032be5SNikolay Aleksandrov virtnet_init_settings(dev); 321816032be5SNikolay Aleksandrov 3219ba5e4426SSridhar Samudrala if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3220ba5e4426SSridhar Samudrala vi->failover = net_failover_create(vi->dev); 32214b8e6ac4SWei Yongjun if (IS_ERR(vi->failover)) { 32224b8e6ac4SWei Yongjun err = PTR_ERR(vi->failover); 3223ba5e4426SSridhar Samudrala goto free_vqs; 3224ba5e4426SSridhar Samudrala } 32254b8e6ac4SWei Yongjun } 3226ba5e4426SSridhar Samudrala 3227296f96fcSRusty Russell err = register_netdev(dev); 3228296f96fcSRusty Russell if (err) { 3229296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 3230ba5e4426SSridhar Samudrala goto free_failover; 3231296f96fcSRusty Russell } 3232b3369c1fSRusty Russell 32334baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 32344baf1e33SMichael S. Tsirkin 32358017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 32368de4b2f3SWanlong Gao if (err) { 32378de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 3238f00e35e2Swangyunjian goto free_unregister_netdev; 32398de4b2f3SWanlong Gao } 32408de4b2f3SWanlong Gao 3241a220871bSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 324244900010SJason Wang 3243167c25e4SJason Wang /* Assume link up if device can't report link status, 3244167c25e4SJason Wang otherwise get link status from config. */ 3245167c25e4SJason Wang netif_carrier_off(dev); 3246bda7fab5SJay Vosburgh if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 32473b07e9caSTejun Heo schedule_work(&vi->config_work); 3248167c25e4SJason Wang } else { 3249167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 3250faa9b39fSJason Baron virtnet_update_settings(vi); 32514783256eSPantelis Koukousoulas netif_carrier_on(dev); 3252167c25e4SJason Wang } 32539f4d26d0SMark McLoughlin 32543f93522fSJason Wang for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 32553f93522fSJason Wang if (virtio_has_feature(vi->vdev, guest_offloads[i])) 32563f93522fSJason Wang set_bit(guest_offloads[i], &vi->guest_offloads); 3257a02e8964SWillem de Bruijn vi->guest_offloads_capable = vi->guest_offloads; 32583f93522fSJason Wang 3259986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 3260986a4f4dSJason Wang dev->name, max_queue_pairs); 3261986a4f4dSJason Wang 3262296f96fcSRusty Russell return 0; 3263296f96fcSRusty Russell 3264f00e35e2Swangyunjian free_unregister_netdev: 326502465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 326602465555SMichael S. Tsirkin 3267b3369c1fSRusty Russell unregister_netdev(dev); 3268ba5e4426SSridhar Samudrala free_failover: 3269ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 3270d2a7dddaSMichael S. Tsirkin free_vqs: 3271986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 3272fb51879dSMichael Dalton free_receive_page_frags(vi); 3273e9d7417bSJason Wang virtnet_del_vqs(vi); 3274296f96fcSRusty Russell free: 3275296f96fcSRusty Russell free_netdev(dev); 3276296f96fcSRusty Russell return err; 3277296f96fcSRusty Russell } 3278296f96fcSRusty Russell 327904486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 3280296f96fcSRusty Russell { 328104486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 3282830a8a97SShirley Ma 3283830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 32849ab86bbcSShirley Ma free_unused_bufs(vi); 3285fb6813f4SRusty Russell 3286986a4f4dSJason Wang free_receive_bufs(vi); 3287d2a7dddaSMichael S. Tsirkin 3288fb51879dSMichael Dalton free_receive_page_frags(vi); 3289fb51879dSMichael Dalton 3290986a4f4dSJason Wang virtnet_del_vqs(vi); 329104486ed0SAmit Shah } 329204486ed0SAmit Shah 32938cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 329404486ed0SAmit Shah { 329504486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 329604486ed0SAmit Shah 32978017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 32988de4b2f3SWanlong Gao 3299102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 3300102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 3301586d17c5SJason Wang 330204486ed0SAmit Shah unregister_netdev(vi->dev); 330304486ed0SAmit Shah 3304ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 3305ba5e4426SSridhar Samudrala 330604486ed0SAmit Shah remove_vq_common(vi); 3307fb6813f4SRusty Russell 330874b2553fSRusty Russell free_netdev(vi->dev); 3309296f96fcSRusty Russell } 3310296f96fcSRusty Russell 331167a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 33120741bcb5SAmit Shah { 33130741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 33140741bcb5SAmit Shah 33158017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 33169fe7bfceSJohn Fastabend virtnet_freeze_down(vdev); 33170741bcb5SAmit Shah remove_vq_common(vi); 33180741bcb5SAmit Shah 33190741bcb5SAmit Shah return 0; 33200741bcb5SAmit Shah } 33210741bcb5SAmit Shah 332267a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 33230741bcb5SAmit Shah { 33240741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 33259fe7bfceSJohn Fastabend int err; 33260741bcb5SAmit Shah 33279fe7bfceSJohn Fastabend err = virtnet_restore_up(vdev); 33280741bcb5SAmit Shah if (err) 33290741bcb5SAmit Shah return err; 3330986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 3331986a4f4dSJason Wang 33328017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 33333f2869caSXie Yongji if (err) { 33343f2869caSXie Yongji virtnet_freeze_down(vdev); 33353f2869caSXie Yongji remove_vq_common(vi); 3336ec9debbdSJason Wang return err; 33373f2869caSXie Yongji } 3338ec9debbdSJason Wang 33390741bcb5SAmit Shah return 0; 33400741bcb5SAmit Shah } 33410741bcb5SAmit Shah 3342296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 3343296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 3344296f96fcSRusty Russell { 0 }, 3345296f96fcSRusty Russell }; 3346296f96fcSRusty Russell 3347f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \ 3348f3358507SMichael S. Tsirkin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 3349f3358507SMichael S. Tsirkin VIRTIO_NET_F_MAC, \ 3350f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 3351f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 3352f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 3353f3358507SMichael S. Tsirkin VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 3354f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 3355f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 3356f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_MAC_ADDR, \ 3357faa9b39fSJason Baron VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 33589805069dSSridhar Samudrala VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY 3359f3358507SMichael S. Tsirkin 3360c45a6816SRusty Russell static unsigned int features[] = { 3361f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 3362f3358507SMichael S. Tsirkin }; 3363f3358507SMichael S. Tsirkin 3364f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = { 3365f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 3366f3358507SMichael S. Tsirkin VIRTIO_NET_F_GSO, 3367e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 3368c45a6816SRusty Russell }; 3369c45a6816SRusty Russell 337022402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 3371c45a6816SRusty Russell .feature_table = features, 3372c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 3373f3358507SMichael S. Tsirkin .feature_table_legacy = features_legacy, 3374f3358507SMichael S. Tsirkin .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 3375296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 3376296f96fcSRusty Russell .driver.owner = THIS_MODULE, 3377296f96fcSRusty Russell .id_table = id_table, 3378fe36cbe0SMichael S. Tsirkin .validate = virtnet_validate, 3379296f96fcSRusty Russell .probe = virtnet_probe, 33808cc085d6SBill Pemberton .remove = virtnet_remove, 33819f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 338289107000SAaron Lu #ifdef CONFIG_PM_SLEEP 33830741bcb5SAmit Shah .freeze = virtnet_freeze, 33840741bcb5SAmit Shah .restore = virtnet_restore, 33850741bcb5SAmit Shah #endif 3386296f96fcSRusty Russell }; 3387296f96fcSRusty Russell 33888017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void) 33898017c279SSebastian Andrzej Siewior { 33908017c279SSebastian Andrzej Siewior int ret; 33918017c279SSebastian Andrzej Siewior 339273c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 33938017c279SSebastian Andrzej Siewior virtnet_cpu_online, 33948017c279SSebastian Andrzej Siewior virtnet_cpu_down_prep); 33958017c279SSebastian Andrzej Siewior if (ret < 0) 33968017c279SSebastian Andrzej Siewior goto out; 33978017c279SSebastian Andrzej Siewior virtionet_online = ret; 339873c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 33998017c279SSebastian Andrzej Siewior NULL, virtnet_cpu_dead); 34008017c279SSebastian Andrzej Siewior if (ret) 34018017c279SSebastian Andrzej Siewior goto err_dead; 34028017c279SSebastian Andrzej Siewior 34038017c279SSebastian Andrzej Siewior ret = register_virtio_driver(&virtio_net_driver); 34048017c279SSebastian Andrzej Siewior if (ret) 34058017c279SSebastian Andrzej Siewior goto err_virtio; 34068017c279SSebastian Andrzej Siewior return 0; 34078017c279SSebastian Andrzej Siewior err_virtio: 34088017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 34098017c279SSebastian Andrzej Siewior err_dead: 34108017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 34118017c279SSebastian Andrzej Siewior out: 34128017c279SSebastian Andrzej Siewior return ret; 34138017c279SSebastian Andrzej Siewior } 34148017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init); 34158017c279SSebastian Andrzej Siewior 34168017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void) 34178017c279SSebastian Andrzej Siewior { 3418cfa0ebc9SAndrew Jones unregister_virtio_driver(&virtio_net_driver); 34198017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 34208017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 34218017c279SSebastian Andrzej Siewior } 34228017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit); 3423296f96fcSRusty Russell 3424296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 3425296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 3426296f96fcSRusty Russell MODULE_LICENSE("GPL"); 3427