148925e37SRusty Russell /* A network driver using virtio. 2296f96fcSRusty Russell * 3296f96fcSRusty Russell * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation 4296f96fcSRusty Russell * 5296f96fcSRusty Russell * This program is free software; you can redistribute it and/or modify 6296f96fcSRusty Russell * it under the terms of the GNU General Public License as published by 7296f96fcSRusty Russell * the Free Software Foundation; either version 2 of the License, or 8296f96fcSRusty Russell * (at your option) any later version. 9296f96fcSRusty Russell * 10296f96fcSRusty Russell * This program is distributed in the hope that it will be useful, 11296f96fcSRusty Russell * but WITHOUT ANY WARRANTY; without even the implied warranty of 12296f96fcSRusty Russell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13296f96fcSRusty Russell * GNU General Public License for more details. 14296f96fcSRusty Russell * 15296f96fcSRusty Russell * You should have received a copy of the GNU General Public License 16adf8d3ffSJeff Kirsher * along with this program; if not, see <http://www.gnu.org/licenses/>. 17296f96fcSRusty Russell */ 18296f96fcSRusty Russell //#define DEBUG 19296f96fcSRusty Russell #include <linux/netdevice.h> 20296f96fcSRusty Russell #include <linux/etherdevice.h> 21a9ea3fc6SHerbert Xu #include <linux/ethtool.h> 22296f96fcSRusty Russell #include <linux/module.h> 23296f96fcSRusty Russell #include <linux/virtio.h> 24296f96fcSRusty Russell #include <linux/virtio_net.h> 25f600b690SJohn Fastabend #include <linux/bpf.h> 26a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h> 27296f96fcSRusty Russell #include <linux/scatterlist.h> 28e918085aSAlex Williamson #include <linux/if_vlan.h> 295a0e3ad6STejun Heo #include <linux/slab.h> 308de4b2f3SWanlong Gao #include <linux/cpu.h> 31ab7db917SMichael Dalton #include <linux/average.h> 32d85b758fSMichael S. Tsirkin #include <net/route.h> 33296f96fcSRusty Russell 34d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT; 356c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 366c0cd7c0SDor Laor 37b92f1e67SWillem de Bruijn static bool csum = true, gso = true, napi_tx; 3834a48579SRusty Russell module_param(csum, bool, 0444); 3934a48579SRusty Russell module_param(gso, bool, 0444); 40b92f1e67SWillem de Bruijn module_param(napi_tx, bool, 0644); 4134a48579SRusty Russell 42296f96fcSRusty Russell /* FIXME: MTU in config. */ 435061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 443f2c31d9SMark McLoughlin #define GOOD_COPY_LEN 128 45296f96fcSRusty Russell 46f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 47f6b10209SJason Wang 482de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ 492de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256 502de2f7f4SJohn Fastabend 515377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet 525377d758SJohannes Berg * buffer size when refilling RX rings. As the entire RX ring may be refilled 535377d758SJohannes Berg * at once, the weight is chosen so that the EWMA will be insensitive to short- 545377d758SJohannes Berg * term, transient changes in packet size. 55ab7db917SMichael Dalton */ 56eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64) 57ab7db917SMichael Dalton 5866846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0" 592a41f71dSAlex Williamson 603fa2a1dfSstephen hemminger struct virtnet_stats { 6183a27052SEric Dumazet struct u64_stats_sync tx_syncp; 6283a27052SEric Dumazet struct u64_stats_sync rx_syncp; 633fa2a1dfSstephen hemminger u64 tx_bytes; 643fa2a1dfSstephen hemminger u64 tx_packets; 653fa2a1dfSstephen hemminger 663fa2a1dfSstephen hemminger u64 rx_bytes; 673fa2a1dfSstephen hemminger u64 rx_packets; 683fa2a1dfSstephen hemminger }; 693fa2a1dfSstephen hemminger 70e9d7417bSJason Wang /* Internal representation of a send virtqueue */ 71e9d7417bSJason Wang struct send_queue { 72e9d7417bSJason Wang /* Virtqueue associated with this send _queue */ 73e9d7417bSJason Wang struct virtqueue *vq; 74e9d7417bSJason Wang 75e9d7417bSJason Wang /* TX: fragments + linear part + virtio header */ 76e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 77986a4f4dSJason Wang 78986a4f4dSJason Wang /* Name of the send queue: output.$index */ 79986a4f4dSJason Wang char name[40]; 80b92f1e67SWillem de Bruijn 81b92f1e67SWillem de Bruijn struct napi_struct napi; 82e9d7417bSJason Wang }; 83e9d7417bSJason Wang 84e9d7417bSJason Wang /* Internal representation of a receive virtqueue */ 85e9d7417bSJason Wang struct receive_queue { 86e9d7417bSJason Wang /* Virtqueue associated with this receive_queue */ 87e9d7417bSJason Wang struct virtqueue *vq; 88e9d7417bSJason Wang 89296f96fcSRusty Russell struct napi_struct napi; 90296f96fcSRusty Russell 91f600b690SJohn Fastabend struct bpf_prog __rcu *xdp_prog; 92f600b690SJohn Fastabend 93e9d7417bSJason Wang /* Chain pages by the private ptr. */ 94e9d7417bSJason Wang struct page *pages; 95e9d7417bSJason Wang 96ab7db917SMichael Dalton /* Average packet length for mergeable receive buffers. */ 975377d758SJohannes Berg struct ewma_pkt_len mrg_avg_pkt_len; 98ab7db917SMichael Dalton 99fb51879dSMichael Dalton /* Page frag for packet buffer allocation. */ 100fb51879dSMichael Dalton struct page_frag alloc_frag; 101fb51879dSMichael Dalton 102e9d7417bSJason Wang /* RX: fragments + linear part + virtio header */ 103e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 104986a4f4dSJason Wang 105d85b758fSMichael S. Tsirkin /* Min single buffer size for mergeable buffers case. */ 106d85b758fSMichael S. Tsirkin unsigned int min_buf_len; 107d85b758fSMichael S. Tsirkin 108986a4f4dSJason Wang /* Name of this receive queue: input.$index */ 109986a4f4dSJason Wang char name[40]; 110e9d7417bSJason Wang }; 111e9d7417bSJason Wang 112e9d7417bSJason Wang struct virtnet_info { 113e9d7417bSJason Wang struct virtio_device *vdev; 114e9d7417bSJason Wang struct virtqueue *cvq; 115e9d7417bSJason Wang struct net_device *dev; 116986a4f4dSJason Wang struct send_queue *sq; 117986a4f4dSJason Wang struct receive_queue *rq; 118e9d7417bSJason Wang unsigned int status; 119e9d7417bSJason Wang 120986a4f4dSJason Wang /* Max # of queue pairs supported by the device */ 121986a4f4dSJason Wang u16 max_queue_pairs; 122986a4f4dSJason Wang 123986a4f4dSJason Wang /* # of queue pairs currently used by the driver */ 124986a4f4dSJason Wang u16 curr_queue_pairs; 125986a4f4dSJason Wang 126672aafd5SJohn Fastabend /* # of XDP queue pairs currently used by the driver */ 127672aafd5SJohn Fastabend u16 xdp_queue_pairs; 128672aafd5SJohn Fastabend 12997402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 13097402b96SHerbert Xu bool big_packets; 13197402b96SHerbert Xu 1323f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 1333f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 1343f2c31d9SMark McLoughlin 135986a4f4dSJason Wang /* Has control virtqueue */ 136986a4f4dSJason Wang bool has_cvq; 137986a4f4dSJason Wang 138e7428e95SMichael S. Tsirkin /* Host can handle any s/g split between our header and packet data */ 139e7428e95SMichael S. Tsirkin bool any_header_sg; 140e7428e95SMichael S. Tsirkin 141012873d0SMichael S. Tsirkin /* Packet virtio header size */ 142012873d0SMichael S. Tsirkin u8 hdr_len; 143012873d0SMichael S. Tsirkin 1443fa2a1dfSstephen hemminger /* Active statistics */ 1453fa2a1dfSstephen hemminger struct virtnet_stats __percpu *stats; 1463fa2a1dfSstephen hemminger 1473161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 1483161e453SRusty Russell struct delayed_work refill; 1493161e453SRusty Russell 150586d17c5SJason Wang /* Work struct for config space updates */ 151586d17c5SJason Wang struct work_struct config_work; 152586d17c5SJason Wang 153986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 154986a4f4dSJason Wang bool affinity_hint_set; 15547be2479SWanlong Gao 1568017c279SSebastian Andrzej Siewior /* CPU hotplug instances for online & dead */ 1578017c279SSebastian Andrzej Siewior struct hlist_node node; 1588017c279SSebastian Andrzej Siewior struct hlist_node node_dead; 1592ac46030SMichael S. Tsirkin 1602ac46030SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */ 1612ac46030SMichael S. Tsirkin struct virtio_net_ctrl_hdr ctrl_hdr; 1622ac46030SMichael S. Tsirkin virtio_net_ctrl_ack ctrl_status; 163a725ee3eSAndy Lutomirski struct virtio_net_ctrl_mq ctrl_mq; 1642ac46030SMichael S. Tsirkin u8 ctrl_promisc; 1652ac46030SMichael S. Tsirkin u8 ctrl_allmulti; 166a725ee3eSAndy Lutomirski u16 ctrl_vid; 16716032be5SNikolay Aleksandrov 16816032be5SNikolay Aleksandrov /* Ethtool settings */ 16916032be5SNikolay Aleksandrov u8 duplex; 17016032be5SNikolay Aleksandrov u32 speed; 171296f96fcSRusty Russell }; 172296f96fcSRusty Russell 1739ab86bbcSShirley Ma struct padded_vnet_hdr { 174012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr; 1759ab86bbcSShirley Ma /* 176012873d0SMichael S. Tsirkin * hdr is in a separate sg buffer, and data sg buffer shares same page 177012873d0SMichael S. Tsirkin * with this header sg. This padding makes next sg 16 byte aligned 178012873d0SMichael S. Tsirkin * after the header. 1799ab86bbcSShirley Ma */ 180012873d0SMichael S. Tsirkin char padding[4]; 1819ab86bbcSShirley Ma }; 1829ab86bbcSShirley Ma 183986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 184986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 185986a4f4dSJason Wang */ 186986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 187986a4f4dSJason Wang { 1889d0ca6edSRusty Russell return (vq->index - 1) / 2; 189986a4f4dSJason Wang } 190986a4f4dSJason Wang 191986a4f4dSJason Wang static int txq2vq(int txq) 192986a4f4dSJason Wang { 193986a4f4dSJason Wang return txq * 2 + 1; 194986a4f4dSJason Wang } 195986a4f4dSJason Wang 196986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 197986a4f4dSJason Wang { 1989d0ca6edSRusty Russell return vq->index / 2; 199986a4f4dSJason Wang } 200986a4f4dSJason Wang 201986a4f4dSJason Wang static int rxq2vq(int rxq) 202986a4f4dSJason Wang { 203986a4f4dSJason Wang return rxq * 2; 204986a4f4dSJason Wang } 205986a4f4dSJason Wang 206012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 207296f96fcSRusty Russell { 208012873d0SMichael S. Tsirkin return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 209296f96fcSRusty Russell } 210296f96fcSRusty Russell 2119ab86bbcSShirley Ma /* 2129ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 2139ab86bbcSShirley Ma * most recent used list in the beginning for reuse 2149ab86bbcSShirley Ma */ 215e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 216fb6813f4SRusty Russell { 2179ab86bbcSShirley Ma struct page *end; 2189ab86bbcSShirley Ma 219e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 2209ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 221e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 222e9d7417bSJason Wang rq->pages = page; 223fb6813f4SRusty Russell } 224fb6813f4SRusty Russell 225e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 226fb6813f4SRusty Russell { 227e9d7417bSJason Wang struct page *p = rq->pages; 228fb6813f4SRusty Russell 2299ab86bbcSShirley Ma if (p) { 230e9d7417bSJason Wang rq->pages = (struct page *)p->private; 2319ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 2329ab86bbcSShirley Ma p->private = 0; 2339ab86bbcSShirley Ma } else 234fb6813f4SRusty Russell p = alloc_page(gfp_mask); 235fb6813f4SRusty Russell return p; 236fb6813f4SRusty Russell } 237fb6813f4SRusty Russell 238e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi, 239e4e8452aSWillem de Bruijn struct virtqueue *vq) 240e4e8452aSWillem de Bruijn { 241e4e8452aSWillem de Bruijn if (napi_schedule_prep(napi)) { 242e4e8452aSWillem de Bruijn virtqueue_disable_cb(vq); 243e4e8452aSWillem de Bruijn __napi_schedule(napi); 244e4e8452aSWillem de Bruijn } 245e4e8452aSWillem de Bruijn } 246e4e8452aSWillem de Bruijn 247e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi, 248e4e8452aSWillem de Bruijn struct virtqueue *vq, int processed) 249e4e8452aSWillem de Bruijn { 250e4e8452aSWillem de Bruijn int opaque; 251e4e8452aSWillem de Bruijn 252e4e8452aSWillem de Bruijn opaque = virtqueue_enable_cb_prepare(vq); 253e4e8452aSWillem de Bruijn if (napi_complete_done(napi, processed) && 254e4e8452aSWillem de Bruijn unlikely(virtqueue_poll(vq, opaque))) 255e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 256e4e8452aSWillem de Bruijn } 257e4e8452aSWillem de Bruijn 258e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 259296f96fcSRusty Russell { 260e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 261b92f1e67SWillem de Bruijn struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 262296f96fcSRusty Russell 2632cb9c6baSRusty Russell /* Suppress further interrupts. */ 264e9d7417bSJason Wang virtqueue_disable_cb(vq); 26511a3a154SRusty Russell 266b92f1e67SWillem de Bruijn if (napi->weight) 267b92f1e67SWillem de Bruijn virtqueue_napi_schedule(napi, vq); 268b92f1e67SWillem de Bruijn else 269363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 270986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 271296f96fcSRusty Russell } 272296f96fcSRusty Russell 2733464645aSMike Waychison /* Called from bottom half context */ 274946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 275946fa564SMichael S. Tsirkin struct receive_queue *rq, 2762613af0eSMichael Dalton struct page *page, unsigned int offset, 2772613af0eSMichael Dalton unsigned int len, unsigned int truesize) 2789ab86bbcSShirley Ma { 2799ab86bbcSShirley Ma struct sk_buff *skb; 280012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 2812613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 2829ab86bbcSShirley Ma char *p; 2839ab86bbcSShirley Ma 2842613af0eSMichael Dalton p = page_address(page) + offset; 2859ab86bbcSShirley Ma 2869ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 287c67f5db8SPaolo Abeni skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 2889ab86bbcSShirley Ma if (unlikely(!skb)) 2899ab86bbcSShirley Ma return NULL; 2909ab86bbcSShirley Ma 2919ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 2929ab86bbcSShirley Ma 293012873d0SMichael S. Tsirkin hdr_len = vi->hdr_len; 294012873d0SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 295012873d0SMichael S. Tsirkin hdr_padded_len = sizeof *hdr; 296012873d0SMichael S. Tsirkin else 2972613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 2983f2c31d9SMark McLoughlin 2999ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 3003f2c31d9SMark McLoughlin 3019ab86bbcSShirley Ma len -= hdr_len; 3022613af0eSMichael Dalton offset += hdr_padded_len; 3032613af0eSMichael Dalton p += hdr_padded_len; 3043f2c31d9SMark McLoughlin 3053f2c31d9SMark McLoughlin copy = len; 3063f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 3073f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 30859ae1d12SJohannes Berg skb_put_data(skb, p, copy); 3093f2c31d9SMark McLoughlin 3103f2c31d9SMark McLoughlin len -= copy; 3119ab86bbcSShirley Ma offset += copy; 3123f2c31d9SMark McLoughlin 3132613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 3142613af0eSMichael Dalton if (len) 3152613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 3162613af0eSMichael Dalton else 3172613af0eSMichael Dalton put_page(page); 3182613af0eSMichael Dalton return skb; 3192613af0eSMichael Dalton } 3202613af0eSMichael Dalton 321e878d78bSSasha Levin /* 322e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 323e878d78bSSasha Levin * This is here to handle cases when the device erroneously 324e878d78bSSasha Levin * tries to receive more than is possible. This is usually 325e878d78bSSasha Levin * the case of a broken device. 326e878d78bSSasha Levin */ 327e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 328be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 329e878d78bSSasha Levin dev_kfree_skb(skb); 330e878d78bSSasha Levin return NULL; 331e878d78bSSasha Levin } 3322613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 3339ab86bbcSShirley Ma while (len) { 3342613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 3352613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 3362613af0eSMichael Dalton frag_size, truesize); 3372613af0eSMichael Dalton len -= frag_size; 3389ab86bbcSShirley Ma page = (struct page *)page->private; 3399ab86bbcSShirley Ma offset = 0; 3403f2c31d9SMark McLoughlin } 3413f2c31d9SMark McLoughlin 3429ab86bbcSShirley Ma if (page) 343e9d7417bSJason Wang give_pages(rq, page); 3443f2c31d9SMark McLoughlin 3459ab86bbcSShirley Ma return skb; 3469ab86bbcSShirley Ma } 3479ab86bbcSShirley Ma 348a67edbf4SDaniel Borkmann static bool virtnet_xdp_xmit(struct virtnet_info *vi, 34956434a01SJohn Fastabend struct receive_queue *rq, 350f6b10209SJason Wang struct xdp_buff *xdp) 35156434a01SJohn Fastabend { 35256434a01SJohn Fastabend struct virtio_net_hdr_mrg_rxbuf *hdr; 353f6b10209SJason Wang unsigned int len; 354722d8283SJohn Fastabend struct send_queue *sq; 355722d8283SJohn Fastabend unsigned int qp; 35656434a01SJohn Fastabend void *xdp_sent; 35756434a01SJohn Fastabend int err; 35856434a01SJohn Fastabend 359722d8283SJohn Fastabend qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); 360722d8283SJohn Fastabend sq = &vi->sq[qp]; 361722d8283SJohn Fastabend 36256434a01SJohn Fastabend /* Free up any pending old buffers before queueing new ones. */ 36356434a01SJohn Fastabend while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) { 36456434a01SJohn Fastabend struct page *sent_page = virt_to_head_page(xdp_sent); 365bb91accfSJason Wang 36656434a01SJohn Fastabend put_page(sent_page); 36756434a01SJohn Fastabend } 36856434a01SJohn Fastabend 369f6b10209SJason Wang xdp->data -= vi->hdr_len; 37056434a01SJohn Fastabend /* Zero header and leave csum up to XDP layers */ 37156434a01SJohn Fastabend hdr = xdp->data; 37256434a01SJohn Fastabend memset(hdr, 0, vi->hdr_len); 37356434a01SJohn Fastabend 37456434a01SJohn Fastabend sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data); 375bb91accfSJason Wang 376f6b10209SJason Wang err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC); 37756434a01SJohn Fastabend if (unlikely(err)) { 378bb91accfSJason Wang struct page *page = virt_to_head_page(xdp->data); 379bb91accfSJason Wang 38056434a01SJohn Fastabend put_page(page); 381a67edbf4SDaniel Borkmann return false; 38256434a01SJohn Fastabend } 38356434a01SJohn Fastabend 38456434a01SJohn Fastabend virtqueue_kick(sq->vq); 385a67edbf4SDaniel Borkmann return true; 38656434a01SJohn Fastabend } 38756434a01SJohn Fastabend 388f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 389f6b10209SJason Wang { 390f6b10209SJason Wang return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0; 391f6b10209SJason Wang } 392f6b10209SJason Wang 393bb91accfSJason Wang static struct sk_buff *receive_small(struct net_device *dev, 394bb91accfSJason Wang struct virtnet_info *vi, 395bb91accfSJason Wang struct receive_queue *rq, 396bb91accfSJason Wang void *buf, unsigned int len) 397f121159dSMichael S. Tsirkin { 398f6b10209SJason Wang struct sk_buff *skb; 399bb91accfSJason Wang struct bpf_prog *xdp_prog; 400f6b10209SJason Wang unsigned int xdp_headroom = virtnet_get_headroom(vi); 401f6b10209SJason Wang unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 402f6b10209SJason Wang unsigned int headroom = vi->hdr_len + header_offset; 403f6b10209SJason Wang unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 404f6b10209SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 405f6b10209SJason Wang unsigned int delta = 0; 406012873d0SMichael S. Tsirkin len -= vi->hdr_len; 407f121159dSMichael S. Tsirkin 408bb91accfSJason Wang rcu_read_lock(); 409bb91accfSJason Wang xdp_prog = rcu_dereference(rq->xdp_prog); 410bb91accfSJason Wang if (xdp_prog) { 411f6b10209SJason Wang struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 4120354e4d1SJohn Fastabend struct xdp_buff xdp; 413f6b10209SJason Wang void *orig_data; 414bb91accfSJason Wang u32 act; 415bb91accfSJason Wang 416bb91accfSJason Wang if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags)) 417bb91accfSJason Wang goto err_xdp; 4180354e4d1SJohn Fastabend 419f6b10209SJason Wang xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len; 420f6b10209SJason Wang xdp.data = xdp.data_hard_start + xdp_headroom; 4210354e4d1SJohn Fastabend xdp.data_end = xdp.data + len; 422f6b10209SJason Wang orig_data = xdp.data; 4230354e4d1SJohn Fastabend act = bpf_prog_run_xdp(xdp_prog, &xdp); 4240354e4d1SJohn Fastabend 425bb91accfSJason Wang switch (act) { 426bb91accfSJason Wang case XDP_PASS: 4272de2f7f4SJohn Fastabend /* Recalculate length in case bpf program changed it */ 428f6b10209SJason Wang delta = orig_data - xdp.data; 429bb91accfSJason Wang break; 430bb91accfSJason Wang case XDP_TX: 431f6b10209SJason Wang if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp))) 4320354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 433bb91accfSJason Wang rcu_read_unlock(); 434bb91accfSJason Wang goto xdp_xmit; 435bb91accfSJason Wang default: 4360354e4d1SJohn Fastabend bpf_warn_invalid_xdp_action(act); 4370354e4d1SJohn Fastabend case XDP_ABORTED: 4380354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 4390354e4d1SJohn Fastabend case XDP_DROP: 440bb91accfSJason Wang goto err_xdp; 441bb91accfSJason Wang } 442bb91accfSJason Wang } 443bb91accfSJason Wang rcu_read_unlock(); 444bb91accfSJason Wang 445f6b10209SJason Wang skb = build_skb(buf, buflen); 446f6b10209SJason Wang if (!skb) { 447f6b10209SJason Wang put_page(virt_to_head_page(buf)); 448f6b10209SJason Wang goto err; 449f6b10209SJason Wang } 450f6b10209SJason Wang skb_reserve(skb, headroom - delta); 451f6b10209SJason Wang skb_put(skb, len + delta); 452f6b10209SJason Wang if (!delta) { 453f6b10209SJason Wang buf += header_offset; 454f6b10209SJason Wang memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 455f6b10209SJason Wang } /* keep zeroed vnet hdr since packet was changed by bpf */ 456f6b10209SJason Wang 457f6b10209SJason Wang err: 458f121159dSMichael S. Tsirkin return skb; 459bb91accfSJason Wang 460bb91accfSJason Wang err_xdp: 461bb91accfSJason Wang rcu_read_unlock(); 462bb91accfSJason Wang dev->stats.rx_dropped++; 463f6b10209SJason Wang put_page(virt_to_head_page(buf)); 464bb91accfSJason Wang xdp_xmit: 465bb91accfSJason Wang return NULL; 466f121159dSMichael S. Tsirkin } 467f121159dSMichael S. Tsirkin 468f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev, 469946fa564SMichael S. Tsirkin struct virtnet_info *vi, 470f121159dSMichael S. Tsirkin struct receive_queue *rq, 471f121159dSMichael S. Tsirkin void *buf, 472f121159dSMichael S. Tsirkin unsigned int len) 473f121159dSMichael S. Tsirkin { 474f121159dSMichael S. Tsirkin struct page *page = buf; 475c47a43d3SJason Wang struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE); 476f121159dSMichael S. Tsirkin 477f121159dSMichael S. Tsirkin if (unlikely(!skb)) 478f121159dSMichael S. Tsirkin goto err; 479f121159dSMichael S. Tsirkin 480f121159dSMichael S. Tsirkin return skb; 481f121159dSMichael S. Tsirkin 482f121159dSMichael S. Tsirkin err: 483f121159dSMichael S. Tsirkin dev->stats.rx_dropped++; 484f121159dSMichael S. Tsirkin give_pages(rq, page); 485f121159dSMichael S. Tsirkin return NULL; 486f121159dSMichael S. Tsirkin } 487f121159dSMichael S. Tsirkin 48872979a6cSJohn Fastabend /* The conditions to enable XDP should preclude the underlying device from 48972979a6cSJohn Fastabend * sending packets across multiple buffers (num_buf > 1). However per spec 49072979a6cSJohn Fastabend * it does not appear to be illegal to do so but rather just against convention. 49172979a6cSJohn Fastabend * So in order to avoid making a system unresponsive the packets are pushed 49272979a6cSJohn Fastabend * into a page and the XDP program is run. This will be extremely slow and we 49372979a6cSJohn Fastabend * push a warning to the user to fix this as soon as possible. Fixing this may 49472979a6cSJohn Fastabend * require resolving the underlying hardware to determine why multiple buffers 49572979a6cSJohn Fastabend * are being received or simply loading the XDP program in the ingress stack 49672979a6cSJohn Fastabend * after the skb is built because there is no advantage to running it here 49772979a6cSJohn Fastabend * anymore. 49872979a6cSJohn Fastabend */ 49972979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq, 50056a86f84SJason Wang u16 *num_buf, 50172979a6cSJohn Fastabend struct page *p, 50272979a6cSJohn Fastabend int offset, 50372979a6cSJohn Fastabend unsigned int *len) 50472979a6cSJohn Fastabend { 50572979a6cSJohn Fastabend struct page *page = alloc_page(GFP_ATOMIC); 5062de2f7f4SJohn Fastabend unsigned int page_off = VIRTIO_XDP_HEADROOM; 50772979a6cSJohn Fastabend 50872979a6cSJohn Fastabend if (!page) 50972979a6cSJohn Fastabend return NULL; 51072979a6cSJohn Fastabend 51172979a6cSJohn Fastabend memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 51272979a6cSJohn Fastabend page_off += *len; 51372979a6cSJohn Fastabend 51456a86f84SJason Wang while (--*num_buf) { 51572979a6cSJohn Fastabend unsigned int buflen; 51672979a6cSJohn Fastabend void *buf; 51772979a6cSJohn Fastabend int off; 51872979a6cSJohn Fastabend 519680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &buflen); 520680557cfSMichael S. Tsirkin if (unlikely(!buf)) 52172979a6cSJohn Fastabend goto err_buf; 52272979a6cSJohn Fastabend 52372979a6cSJohn Fastabend p = virt_to_head_page(buf); 52472979a6cSJohn Fastabend off = buf - page_address(p); 52572979a6cSJohn Fastabend 52656a86f84SJason Wang /* guard against a misconfigured or uncooperative backend that 52756a86f84SJason Wang * is sending packet larger than the MTU. 52856a86f84SJason Wang */ 52956a86f84SJason Wang if ((page_off + buflen) > PAGE_SIZE) { 53056a86f84SJason Wang put_page(p); 53156a86f84SJason Wang goto err_buf; 53256a86f84SJason Wang } 53356a86f84SJason Wang 53472979a6cSJohn Fastabend memcpy(page_address(page) + page_off, 53572979a6cSJohn Fastabend page_address(p) + off, buflen); 53672979a6cSJohn Fastabend page_off += buflen; 53756a86f84SJason Wang put_page(p); 53872979a6cSJohn Fastabend } 53972979a6cSJohn Fastabend 5402de2f7f4SJohn Fastabend /* Headroom does not contribute to packet length */ 5412de2f7f4SJohn Fastabend *len = page_off - VIRTIO_XDP_HEADROOM; 54272979a6cSJohn Fastabend return page; 54372979a6cSJohn Fastabend err_buf: 54472979a6cSJohn Fastabend __free_pages(page, 0); 54572979a6cSJohn Fastabend return NULL; 54672979a6cSJohn Fastabend } 54772979a6cSJohn Fastabend 5488fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 549fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 5508fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 551680557cfSMichael S. Tsirkin void *buf, 552680557cfSMichael S. Tsirkin void *ctx, 5538fc3b9e9SMichael S. Tsirkin unsigned int len) 5549ab86bbcSShirley Ma { 555012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 556012873d0SMichael S. Tsirkin u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 5578fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 5588fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 559f600b690SJohn Fastabend struct sk_buff *head_skb, *curr_skb; 560f600b690SJohn Fastabend struct bpf_prog *xdp_prog; 561f600b690SJohn Fastabend unsigned int truesize; 562ab7db917SMichael Dalton 56356434a01SJohn Fastabend head_skb = NULL; 56456434a01SJohn Fastabend 565f600b690SJohn Fastabend rcu_read_lock(); 566f600b690SJohn Fastabend xdp_prog = rcu_dereference(rq->xdp_prog); 567f600b690SJohn Fastabend if (xdp_prog) { 56872979a6cSJohn Fastabend struct page *xdp_page; 5690354e4d1SJohn Fastabend struct xdp_buff xdp; 5700354e4d1SJohn Fastabend void *data; 571f600b690SJohn Fastabend u32 act; 572f600b690SJohn Fastabend 57373b62bd0SJason Wang /* This happens when rx buffer size is underestimated */ 574f600b690SJohn Fastabend if (unlikely(num_buf > 1)) { 57572979a6cSJohn Fastabend /* linearize data for XDP */ 57656a86f84SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, 57772979a6cSJohn Fastabend page, offset, &len); 57872979a6cSJohn Fastabend if (!xdp_page) 579f600b690SJohn Fastabend goto err_xdp; 5802de2f7f4SJohn Fastabend offset = VIRTIO_XDP_HEADROOM; 58172979a6cSJohn Fastabend } else { 58272979a6cSJohn Fastabend xdp_page = page; 583f600b690SJohn Fastabend } 584f600b690SJohn Fastabend 585f600b690SJohn Fastabend /* Transient failure which in theory could occur if 586f600b690SJohn Fastabend * in-flight packets from before XDP was enabled reach 587f600b690SJohn Fastabend * the receive path after XDP is loaded. In practice I 588f600b690SJohn Fastabend * was not able to create this condition. 589f600b690SJohn Fastabend */ 590b00f70b0SJason Wang if (unlikely(hdr->hdr.gso_type)) 591f600b690SJohn Fastabend goto err_xdp; 592f600b690SJohn Fastabend 5932de2f7f4SJohn Fastabend /* Allow consuming headroom but reserve enough space to push 5942de2f7f4SJohn Fastabend * the descriptor on if we get an XDP_TX return code. 5952de2f7f4SJohn Fastabend */ 5960354e4d1SJohn Fastabend data = page_address(xdp_page) + offset; 5972de2f7f4SJohn Fastabend xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len; 5980354e4d1SJohn Fastabend xdp.data = data + vi->hdr_len; 5990354e4d1SJohn Fastabend xdp.data_end = xdp.data + (len - vi->hdr_len); 6000354e4d1SJohn Fastabend act = bpf_prog_run_xdp(xdp_prog, &xdp); 6010354e4d1SJohn Fastabend 60256434a01SJohn Fastabend switch (act) { 60356434a01SJohn Fastabend case XDP_PASS: 6042de2f7f4SJohn Fastabend /* recalculate offset to account for any header 6052de2f7f4SJohn Fastabend * adjustments. Note other cases do not build an 6062de2f7f4SJohn Fastabend * skb and avoid using offset 6072de2f7f4SJohn Fastabend */ 6082de2f7f4SJohn Fastabend offset = xdp.data - 6092de2f7f4SJohn Fastabend page_address(xdp_page) - vi->hdr_len; 6102de2f7f4SJohn Fastabend 6111830f893SJason Wang /* We can only create skb based on xdp_page. */ 6121830f893SJason Wang if (unlikely(xdp_page != page)) { 6131830f893SJason Wang rcu_read_unlock(); 6141830f893SJason Wang put_page(page); 6151830f893SJason Wang head_skb = page_to_skb(vi, rq, xdp_page, 6162de2f7f4SJohn Fastabend offset, len, PAGE_SIZE); 6175c33474dSJason Wang ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); 6181830f893SJason Wang return head_skb; 6191830f893SJason Wang } 62056434a01SJohn Fastabend break; 62156434a01SJohn Fastabend case XDP_TX: 622f6b10209SJason Wang if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp))) 6230354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 6245c33474dSJason Wang ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); 62572979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 62672979a6cSJohn Fastabend goto err_xdp; 62756434a01SJohn Fastabend rcu_read_unlock(); 62856434a01SJohn Fastabend goto xdp_xmit; 62956434a01SJohn Fastabend default: 6300354e4d1SJohn Fastabend bpf_warn_invalid_xdp_action(act); 6310354e4d1SJohn Fastabend case XDP_ABORTED: 6320354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 6330354e4d1SJohn Fastabend case XDP_DROP: 63472979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 63572979a6cSJohn Fastabend __free_pages(xdp_page, 0); 6365c33474dSJason Wang ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); 637f600b690SJohn Fastabend goto err_xdp; 638f600b690SJohn Fastabend } 63956434a01SJohn Fastabend } 640f600b690SJohn Fastabend rcu_read_unlock(); 641f600b690SJohn Fastabend 642680557cfSMichael S. Tsirkin if (unlikely(len > (unsigned long)ctx)) { 64356da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 644680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 645680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 646680557cfSMichael S. Tsirkin goto err_skb; 647680557cfSMichael S. Tsirkin } 648680557cfSMichael S. Tsirkin truesize = (unsigned long)ctx; 649f600b690SJohn Fastabend head_skb = page_to_skb(vi, rq, page, offset, len, truesize); 650f600b690SJohn Fastabend curr_skb = head_skb; 6519ab86bbcSShirley Ma 6528fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 6538fc3b9e9SMichael S. Tsirkin goto err_skb; 6549ab86bbcSShirley Ma while (--num_buf) { 6558fc3b9e9SMichael S. Tsirkin int num_skb_frags; 6568fc3b9e9SMichael S. Tsirkin 657680557cfSMichael S. Tsirkin buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 658ab7db917SMichael Dalton if (unlikely(!ctx)) { 6598fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 660fdd819b2SMichael S. Tsirkin dev->name, num_buf, 661012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 662012873d0SMichael S. Tsirkin hdr->num_buffers)); 6638fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 6648fc3b9e9SMichael S. Tsirkin goto err_buf; 6653f2c31d9SMark McLoughlin } 6668fc3b9e9SMichael S. Tsirkin 6678fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 668680557cfSMichael S. Tsirkin if (unlikely(len > (unsigned long)ctx)) { 66956da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 670680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 671680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 672680557cfSMichael S. Tsirkin goto err_skb; 673680557cfSMichael S. Tsirkin } 674680557cfSMichael S. Tsirkin truesize = (unsigned long)ctx; 6758fc3b9e9SMichael S. Tsirkin 6768fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 6772613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 6782613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 6798fc3b9e9SMichael S. Tsirkin 6808fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 6818fc3b9e9SMichael S. Tsirkin goto err_skb; 6822613af0eSMichael Dalton if (curr_skb == head_skb) 6832613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 6842613af0eSMichael Dalton else 6852613af0eSMichael Dalton curr_skb->next = nskb; 6862613af0eSMichael Dalton curr_skb = nskb; 6872613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 6882613af0eSMichael Dalton num_skb_frags = 0; 6892613af0eSMichael Dalton } 6902613af0eSMichael Dalton if (curr_skb != head_skb) { 6912613af0eSMichael Dalton head_skb->data_len += len; 6922613af0eSMichael Dalton head_skb->len += len; 693fb51879dSMichael Dalton head_skb->truesize += truesize; 6942613af0eSMichael Dalton } 6958fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 696ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 697ba275241SJason Wang put_page(page); 698ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 699fb51879dSMichael Dalton len, truesize); 700ba275241SJason Wang } else { 7012613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 702fb51879dSMichael Dalton offset, len, truesize); 703ba275241SJason Wang } 7048fc3b9e9SMichael S. Tsirkin } 7058fc3b9e9SMichael S. Tsirkin 7065377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 7078fc3b9e9SMichael S. Tsirkin return head_skb; 7088fc3b9e9SMichael S. Tsirkin 709f600b690SJohn Fastabend err_xdp: 710f600b690SJohn Fastabend rcu_read_unlock(); 7118fc3b9e9SMichael S. Tsirkin err_skb: 7128fc3b9e9SMichael S. Tsirkin put_page(page); 7138fc3b9e9SMichael S. Tsirkin while (--num_buf) { 714680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &len); 715680557cfSMichael S. Tsirkin if (unlikely(!buf)) { 7168fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 7178fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 7188fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 7198fc3b9e9SMichael S. Tsirkin break; 7208fc3b9e9SMichael S. Tsirkin } 721680557cfSMichael S. Tsirkin page = virt_to_head_page(buf); 7228fc3b9e9SMichael S. Tsirkin put_page(page); 7233f2c31d9SMark McLoughlin } 7248fc3b9e9SMichael S. Tsirkin err_buf: 7258fc3b9e9SMichael S. Tsirkin dev->stats.rx_dropped++; 7268fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 72756434a01SJohn Fastabend xdp_xmit: 7288fc3b9e9SMichael S. Tsirkin return NULL; 7299ab86bbcSShirley Ma } 7309ab86bbcSShirley Ma 73161845d20SJason Wang static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 732680557cfSMichael S. Tsirkin void *buf, unsigned int len, void **ctx) 7339ab86bbcSShirley Ma { 734e9d7417bSJason Wang struct net_device *dev = vi->dev; 7359ab86bbcSShirley Ma struct sk_buff *skb; 736012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 73761845d20SJason Wang int ret; 7389ab86bbcSShirley Ma 739bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 7409ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 7419ab86bbcSShirley Ma dev->stats.rx_length_errors++; 742ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 743680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 744ab7db917SMichael Dalton } else if (vi->big_packets) { 74598bfd23cSMichael Dalton give_pages(rq, buf); 746ab7db917SMichael Dalton } else { 747f6b10209SJason Wang put_page(virt_to_head_page(buf)); 748ab7db917SMichael Dalton } 74961845d20SJason Wang return 0; 7509ab86bbcSShirley Ma } 7519ab86bbcSShirley Ma 752f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 753680557cfSMichael S. Tsirkin skb = receive_mergeable(dev, vi, rq, buf, ctx, len); 754f121159dSMichael S. Tsirkin else if (vi->big_packets) 755946fa564SMichael S. Tsirkin skb = receive_big(dev, vi, rq, buf, len); 756f121159dSMichael S. Tsirkin else 757bb91accfSJason Wang skb = receive_small(dev, vi, rq, buf, len); 758f121159dSMichael S. Tsirkin 7598fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 76061845d20SJason Wang return 0; 7613f2c31d9SMark McLoughlin 7629ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 7633fa2a1dfSstephen hemminger 76461845d20SJason Wang ret = skb->len; 765296f96fcSRusty Russell 766e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 76710a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 768296f96fcSRusty Russell 769e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 770e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 771e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 772e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 773fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 774296f96fcSRusty Russell goto frame_err; 775296f96fcSRusty Russell } 776296f96fcSRusty Russell 777d1dc06dcSMike Rapoport skb->protocol = eth_type_trans(skb, dev); 778d1dc06dcSMike Rapoport pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 779d1dc06dcSMike Rapoport ntohs(skb->protocol), skb->len, skb->pkt_type); 780d1dc06dcSMike Rapoport 7810fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 78261845d20SJason Wang return ret; 783296f96fcSRusty Russell 784296f96fcSRusty Russell frame_err: 785296f96fcSRusty Russell dev->stats.rx_frame_errors++; 786296f96fcSRusty Russell dev_kfree_skb(skb); 78761845d20SJason Wang return 0; 788296f96fcSRusty Russell } 789296f96fcSRusty Russell 790946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 791946fa564SMichael S. Tsirkin gfp_t gfp) 792296f96fcSRusty Russell { 793f6b10209SJason Wang struct page_frag *alloc_frag = &rq->alloc_frag; 794f6b10209SJason Wang char *buf; 7952de2f7f4SJohn Fastabend unsigned int xdp_headroom = virtnet_get_headroom(vi); 796f6b10209SJason Wang int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 7979ab86bbcSShirley Ma int err; 7983f2c31d9SMark McLoughlin 799f6b10209SJason Wang len = SKB_DATA_ALIGN(len) + 800f6b10209SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 801f6b10209SJason Wang if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 8029ab86bbcSShirley Ma return -ENOMEM; 803296f96fcSRusty Russell 804f6b10209SJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 805f6b10209SJason Wang get_page(alloc_frag->page); 806f6b10209SJason Wang alloc_frag->offset += len; 807f6b10209SJason Wang sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 808f6b10209SJason Wang vi->hdr_len + GOOD_PACKET_LEN); 809f6b10209SJason Wang err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp); 8109ab86bbcSShirley Ma if (err < 0) 811f6b10209SJason Wang put_page(virt_to_head_page(buf)); 81297402b96SHerbert Xu 8139ab86bbcSShirley Ma return err; 81497402b96SHerbert Xu } 81597402b96SHerbert Xu 816012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 817012873d0SMichael S. Tsirkin gfp_t gfp) 8189ab86bbcSShirley Ma { 8199ab86bbcSShirley Ma struct page *first, *list = NULL; 8209ab86bbcSShirley Ma char *p; 8219ab86bbcSShirley Ma int i, err, offset; 822296f96fcSRusty Russell 823a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 824a5835440SRusty Russell 825e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 8269ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 827e9d7417bSJason Wang first = get_a_page(rq, gfp); 8289ab86bbcSShirley Ma if (!first) { 8299ab86bbcSShirley Ma if (list) 830e9d7417bSJason Wang give_pages(rq, list); 8319ab86bbcSShirley Ma return -ENOMEM; 832296f96fcSRusty Russell } 833e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 8349ab86bbcSShirley Ma 8359ab86bbcSShirley Ma /* chain new page in list head to match sg */ 8369ab86bbcSShirley Ma first->private = (unsigned long)list; 8379ab86bbcSShirley Ma list = first; 8389ab86bbcSShirley Ma } 8399ab86bbcSShirley Ma 840e9d7417bSJason Wang first = get_a_page(rq, gfp); 8419ab86bbcSShirley Ma if (!first) { 842e9d7417bSJason Wang give_pages(rq, list); 8439ab86bbcSShirley Ma return -ENOMEM; 8449ab86bbcSShirley Ma } 8459ab86bbcSShirley Ma p = page_address(first); 8469ab86bbcSShirley Ma 847e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 848012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 849012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 8509ab86bbcSShirley Ma 851e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 8529ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 853e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 8549ab86bbcSShirley Ma 8559ab86bbcSShirley Ma /* chain first in list head */ 8569ab86bbcSShirley Ma first->private = (unsigned long)list; 8579dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 858aa989f5eSMichael S. Tsirkin first, gfp); 8599ab86bbcSShirley Ma if (err < 0) 860e9d7417bSJason Wang give_pages(rq, first); 8619ab86bbcSShirley Ma 8629ab86bbcSShirley Ma return err; 8639ab86bbcSShirley Ma } 8649ab86bbcSShirley Ma 865d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 866d85b758fSMichael S. Tsirkin struct ewma_pkt_len *avg_pkt_len) 8679ab86bbcSShirley Ma { 868ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 869fbf28d78SMichael Dalton unsigned int len; 870fbf28d78SMichael Dalton 8715377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 872f0c3192cSMichael S. Tsirkin rq->min_buf_len, PAGE_SIZE - hdr_len); 873e377fcc8SMichael S. Tsirkin return ALIGN(len, L1_CACHE_BYTES); 874fbf28d78SMichael Dalton } 875fbf28d78SMichael Dalton 8762de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi, 8772de2f7f4SJohn Fastabend struct receive_queue *rq, gfp_t gfp) 878fbf28d78SMichael Dalton { 879fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 8802de2f7f4SJohn Fastabend unsigned int headroom = virtnet_get_headroom(vi); 881fb51879dSMichael Dalton char *buf; 882680557cfSMichael S. Tsirkin void *ctx; 8839ab86bbcSShirley Ma int err; 884fb51879dSMichael Dalton unsigned int len, hole; 8859ab86bbcSShirley Ma 886d85b758fSMichael S. Tsirkin len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len); 8872de2f7f4SJohn Fastabend if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp))) 8889ab86bbcSShirley Ma return -ENOMEM; 889ab7db917SMichael Dalton 890fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 8912de2f7f4SJohn Fastabend buf += headroom; /* advance address leaving hole at front of pkt */ 892680557cfSMichael S. Tsirkin ctx = (void *)(unsigned long)len; 893fb51879dSMichael Dalton get_page(alloc_frag->page); 8942de2f7f4SJohn Fastabend alloc_frag->offset += len + headroom; 895fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 8962de2f7f4SJohn Fastabend if (hole < len + headroom) { 897ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 898ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 899ab7db917SMichael Dalton * the current buffer. This extra space is not included in 900ab7db917SMichael Dalton * the truesize stored in ctx. 901ab7db917SMichael Dalton */ 902fb51879dSMichael Dalton len += hole; 903fb51879dSMichael Dalton alloc_frag->offset += hole; 904fb51879dSMichael Dalton } 9059ab86bbcSShirley Ma 906fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 907680557cfSMichael S. Tsirkin err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 9089ab86bbcSShirley Ma if (err < 0) 9092613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 9109ab86bbcSShirley Ma 9119ab86bbcSShirley Ma return err; 912296f96fcSRusty Russell } 913296f96fcSRusty Russell 914b2baed69SRusty Russell /* 915b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 916b2baed69SRusty Russell * 917b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 918b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 919b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 920b2baed69SRusty Russell */ 921946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 922946fa564SMichael S. Tsirkin gfp_t gfp) 9233f2c31d9SMark McLoughlin { 9243f2c31d9SMark McLoughlin int err; 9251788f495SMichael S. Tsirkin bool oom; 9263f2c31d9SMark McLoughlin 927fb51879dSMichael Dalton gfp |= __GFP_COLD; 9280aea51c3SAmit Shah do { 9299ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 9302de2f7f4SJohn Fastabend err = add_recvbuf_mergeable(vi, rq, gfp); 9319ab86bbcSShirley Ma else if (vi->big_packets) 932012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 9339ab86bbcSShirley Ma else 934946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 9353f2c31d9SMark McLoughlin 9361788f495SMichael S. Tsirkin oom = err == -ENOMEM; 9379ed4cb07SRusty Russell if (err) 9383f2c31d9SMark McLoughlin break; 939b7dfde95SLinus Torvalds } while (rq->vq->num_free); 940681daee2SJason Wang virtqueue_kick(rq->vq); 9413161e453SRusty Russell return !oom; 9423f2c31d9SMark McLoughlin } 9433f2c31d9SMark McLoughlin 94418445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 945296f96fcSRusty Russell { 946296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 947986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 948e9d7417bSJason Wang 949e4e8452aSWillem de Bruijn virtqueue_napi_schedule(&rq->napi, rvq); 950296f96fcSRusty Russell } 951296f96fcSRusty Russell 952e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 9533e9d08ecSBruce Rogers { 954e4e8452aSWillem de Bruijn napi_enable(napi); 9553e9d08ecSBruce Rogers 9563e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 957e4e8452aSWillem de Bruijn * won't get another interrupt, so process any outstanding packets now. 958e4e8452aSWillem de Bruijn * Call local_bh_enable after to trigger softIRQ processing. 959e4e8452aSWillem de Bruijn */ 960ec13ee80SMichael S. Tsirkin local_bh_disable(); 961e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 962ec13ee80SMichael S. Tsirkin local_bh_enable(); 9633e9d08ecSBruce Rogers } 9643e9d08ecSBruce Rogers 965b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi, 966b92f1e67SWillem de Bruijn struct virtqueue *vq, 967b92f1e67SWillem de Bruijn struct napi_struct *napi) 968b92f1e67SWillem de Bruijn { 969b92f1e67SWillem de Bruijn if (!napi->weight) 970b92f1e67SWillem de Bruijn return; 971b92f1e67SWillem de Bruijn 972b92f1e67SWillem de Bruijn /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 973b92f1e67SWillem de Bruijn * enable the feature if this is likely affine with the transmit path. 974b92f1e67SWillem de Bruijn */ 975b92f1e67SWillem de Bruijn if (!vi->affinity_hint_set) { 976b92f1e67SWillem de Bruijn napi->weight = 0; 977b92f1e67SWillem de Bruijn return; 978b92f1e67SWillem de Bruijn } 979b92f1e67SWillem de Bruijn 980b92f1e67SWillem de Bruijn return virtnet_napi_enable(vq, napi); 981b92f1e67SWillem de Bruijn } 982b92f1e67SWillem de Bruijn 98378a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi) 98478a57b48SWillem de Bruijn { 98578a57b48SWillem de Bruijn if (napi->weight) 98678a57b48SWillem de Bruijn napi_disable(napi); 98778a57b48SWillem de Bruijn } 98878a57b48SWillem de Bruijn 9893161e453SRusty Russell static void refill_work(struct work_struct *work) 9903161e453SRusty Russell { 991e9d7417bSJason Wang struct virtnet_info *vi = 992e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 9933161e453SRusty Russell bool still_empty; 994986a4f4dSJason Wang int i; 9953161e453SRusty Russell 99655257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 997986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 998986a4f4dSJason Wang 999986a4f4dSJason Wang napi_disable(&rq->napi); 1000946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1001e4e8452aSWillem de Bruijn virtnet_napi_enable(rq->vq, &rq->napi); 10023161e453SRusty Russell 10033161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 1004986a4f4dSJason Wang * we will *never* try to fill again. 1005986a4f4dSJason Wang */ 10063161e453SRusty Russell if (still_empty) 10073b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 10083161e453SRusty Russell } 1009986a4f4dSJason Wang } 10103161e453SRusty Russell 10112ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget) 1012296f96fcSRusty Russell { 1013e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 101461845d20SJason Wang unsigned int len, received = 0, bytes = 0; 10159ab86bbcSShirley Ma void *buf; 101661845d20SJason Wang struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 1017296f96fcSRusty Russell 1018680557cfSMichael S. Tsirkin if (vi->mergeable_rx_bufs) { 1019680557cfSMichael S. Tsirkin void *ctx; 1020680557cfSMichael S. Tsirkin 1021680557cfSMichael S. Tsirkin while (received < budget && 1022680557cfSMichael S. Tsirkin (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1023680557cfSMichael S. Tsirkin bytes += receive_buf(vi, rq, buf, len, ctx); 1024680557cfSMichael S. Tsirkin received++; 1025680557cfSMichael S. Tsirkin } 1026680557cfSMichael S. Tsirkin } else { 1027296f96fcSRusty Russell while (received < budget && 1028e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1029680557cfSMichael S. Tsirkin bytes += receive_buf(vi, rq, buf, len, NULL); 1030296f96fcSRusty Russell received++; 1031296f96fcSRusty Russell } 1032680557cfSMichael S. Tsirkin } 1033296f96fcSRusty Russell 1034be121f46SJason Wang if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 1035946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 10363b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 10373161e453SRusty Russell } 1038296f96fcSRusty Russell 103961845d20SJason Wang u64_stats_update_begin(&stats->rx_syncp); 104061845d20SJason Wang stats->rx_bytes += bytes; 104161845d20SJason Wang stats->rx_packets += received; 104261845d20SJason Wang u64_stats_update_end(&stats->rx_syncp); 104361845d20SJason Wang 10442ffa7598SJason Wang return received; 10452ffa7598SJason Wang } 10462ffa7598SJason Wang 1047ea7735d9SWillem de Bruijn static void free_old_xmit_skbs(struct send_queue *sq) 1048ea7735d9SWillem de Bruijn { 1049ea7735d9SWillem de Bruijn struct sk_buff *skb; 1050ea7735d9SWillem de Bruijn unsigned int len; 1051ea7735d9SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1052ea7735d9SWillem de Bruijn struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 1053ea7735d9SWillem de Bruijn unsigned int packets = 0; 1054ea7735d9SWillem de Bruijn unsigned int bytes = 0; 1055ea7735d9SWillem de Bruijn 1056ea7735d9SWillem de Bruijn while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 1057ea7735d9SWillem de Bruijn pr_debug("Sent skb %p\n", skb); 1058ea7735d9SWillem de Bruijn 1059ea7735d9SWillem de Bruijn bytes += skb->len; 1060ea7735d9SWillem de Bruijn packets++; 1061ea7735d9SWillem de Bruijn 1062ea7735d9SWillem de Bruijn dev_kfree_skb_any(skb); 1063ea7735d9SWillem de Bruijn } 1064ea7735d9SWillem de Bruijn 1065ea7735d9SWillem de Bruijn /* Avoid overhead when no packets have been processed 1066ea7735d9SWillem de Bruijn * happens when called speculatively from start_xmit. 1067ea7735d9SWillem de Bruijn */ 1068ea7735d9SWillem de Bruijn if (!packets) 1069ea7735d9SWillem de Bruijn return; 1070ea7735d9SWillem de Bruijn 1071ea7735d9SWillem de Bruijn u64_stats_update_begin(&stats->tx_syncp); 1072ea7735d9SWillem de Bruijn stats->tx_bytes += bytes; 1073ea7735d9SWillem de Bruijn stats->tx_packets += packets; 1074ea7735d9SWillem de Bruijn u64_stats_update_end(&stats->tx_syncp); 1075ea7735d9SWillem de Bruijn } 1076ea7735d9SWillem de Bruijn 10777b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq) 10787b0411efSWillem de Bruijn { 10797b0411efSWillem de Bruijn struct virtnet_info *vi = rq->vq->vdev->priv; 10807b0411efSWillem de Bruijn unsigned int index = vq2rxq(rq->vq); 10817b0411efSWillem de Bruijn struct send_queue *sq = &vi->sq[index]; 10827b0411efSWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 10837b0411efSWillem de Bruijn 10847b0411efSWillem de Bruijn if (!sq->napi.weight) 10857b0411efSWillem de Bruijn return; 10867b0411efSWillem de Bruijn 10877b0411efSWillem de Bruijn if (__netif_tx_trylock(txq)) { 10887b0411efSWillem de Bruijn free_old_xmit_skbs(sq); 10897b0411efSWillem de Bruijn __netif_tx_unlock(txq); 10907b0411efSWillem de Bruijn } 10917b0411efSWillem de Bruijn 10927b0411efSWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 10937b0411efSWillem de Bruijn netif_tx_wake_queue(txq); 10947b0411efSWillem de Bruijn } 10957b0411efSWillem de Bruijn 10962ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 10972ffa7598SJason Wang { 10982ffa7598SJason Wang struct receive_queue *rq = 10992ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 1100e4e8452aSWillem de Bruijn unsigned int received; 11012ffa7598SJason Wang 11027b0411efSWillem de Bruijn virtnet_poll_cleantx(rq); 11037b0411efSWillem de Bruijn 1104faadb05fSLi RongQing received = virtnet_receive(rq, budget); 11052ffa7598SJason Wang 11068329d98eSRusty Russell /* Out of packets? */ 1107e4e8452aSWillem de Bruijn if (received < budget) 1108e4e8452aSWillem de Bruijn virtqueue_napi_complete(napi, rq->vq, received); 1109296f96fcSRusty Russell 1110296f96fcSRusty Russell return received; 1111296f96fcSRusty Russell } 1112296f96fcSRusty Russell 1113986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 1114986a4f4dSJason Wang { 1115986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1116986a4f4dSJason Wang int i; 1117986a4f4dSJason Wang 1118e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1119e4166625SJason Wang if (i < vi->curr_queue_pairs) 1120986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 1121946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1122986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 1123e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1124b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1125986a4f4dSJason Wang } 1126986a4f4dSJason Wang 1127986a4f4dSJason Wang return 0; 1128986a4f4dSJason Wang } 1129986a4f4dSJason Wang 1130b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1131b92f1e67SWillem de Bruijn { 1132b92f1e67SWillem de Bruijn struct send_queue *sq = container_of(napi, struct send_queue, napi); 1133b92f1e67SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1134b92f1e67SWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq)); 1135b92f1e67SWillem de Bruijn 1136b92f1e67SWillem de Bruijn __netif_tx_lock(txq, raw_smp_processor_id()); 1137b92f1e67SWillem de Bruijn free_old_xmit_skbs(sq); 1138b92f1e67SWillem de Bruijn __netif_tx_unlock(txq); 1139b92f1e67SWillem de Bruijn 1140b92f1e67SWillem de Bruijn virtqueue_napi_complete(napi, sq->vq, 0); 1141b92f1e67SWillem de Bruijn 1142b92f1e67SWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1143b92f1e67SWillem de Bruijn netif_tx_wake_queue(txq); 1144b92f1e67SWillem de Bruijn 1145b92f1e67SWillem de Bruijn return 0; 1146b92f1e67SWillem de Bruijn } 1147b92f1e67SWillem de Bruijn 1148e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1149296f96fcSRusty Russell { 1150012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 1151296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1152e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 1153e2fcad58SJason A. Donenfeld int num_sg; 1154012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 1155e7428e95SMichael S. Tsirkin bool can_push; 1156296f96fcSRusty Russell 1157e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1158e7428e95SMichael S. Tsirkin 1159e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 1160e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1161e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1162e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 1163e7428e95SMichael S. Tsirkin * csum_start offset below. */ 1164e7428e95SMichael S. Tsirkin if (can_push) 1165012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1166e7428e95SMichael S. Tsirkin else 1167e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 1168296f96fcSRusty Russell 1169e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 11706391a448SJason Wang virtio_is_little_endian(vi->vdev), false)) 1171296f96fcSRusty Russell BUG(); 1172296f96fcSRusty Russell 1173e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 1174012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 11753f2c31d9SMark McLoughlin 1176547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1177e7428e95SMichael S. Tsirkin if (can_push) { 1178e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 1179e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1180e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1181e2fcad58SJason A. Donenfeld return num_sg; 1182e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 1183e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 1184e7428e95SMichael S. Tsirkin } else { 1185e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 1186e2fcad58SJason A. Donenfeld num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1187e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1188e2fcad58SJason A. Donenfeld return num_sg; 1189e2fcad58SJason A. Donenfeld num_sg++; 1190e7428e95SMichael S. Tsirkin } 11919dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 119211a3a154SRusty Russell } 119311a3a154SRusty Russell 1194424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 119599ffc696SRusty Russell { 119699ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1197986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 1198986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 11999ed4cb07SRusty Russell int err; 12004b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 12014b7fd2e6SMichael S. Tsirkin bool kick = !skb->xmit_more; 1202b92f1e67SWillem de Bruijn bool use_napi = sq->napi.weight; 12032cb9c6baSRusty Russell 12042cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 1205e9d7417bSJason Wang free_old_xmit_skbs(sq); 120699ffc696SRusty Russell 1207bdb12e0dSWillem de Bruijn if (use_napi && kick) 1208bdb12e0dSWillem de Bruijn virtqueue_enable_cb_delayed(sq->vq); 1209bdb12e0dSWillem de Bruijn 1210074c3582SJacob Keller /* timestamp packet in software */ 1211074c3582SJacob Keller skb_tx_timestamp(skb); 1212074c3582SJacob Keller 121303f191baSMichael S. Tsirkin /* Try to transmit */ 1214b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 121599ffc696SRusty Russell 12169ed4cb07SRusty Russell /* This should not happen! */ 1217681daee2SJason Wang if (unlikely(err)) { 121858eba97dSRusty Russell dev->stats.tx_fifo_errors++; 12192e57b79cSRick Jones if (net_ratelimit()) 122058eba97dSRusty Russell dev_warn(&dev->dev, 1221b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 122258eba97dSRusty Russell dev->stats.tx_dropped++; 122385e94525SEric W. Biederman dev_kfree_skb_any(skb); 122458eba97dSRusty Russell return NETDEV_TX_OK; 1225296f96fcSRusty Russell } 122603f191baSMichael S. Tsirkin 122748925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 1228b92f1e67SWillem de Bruijn if (!use_napi) { 122948925e37SRusty Russell skb_orphan(skb); 123048925e37SRusty Russell nf_reset(skb); 1231b92f1e67SWillem de Bruijn } 123248925e37SRusty Russell 123360302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 123460302ff6SMichael S. Tsirkin * are then unable to transmit. 123560302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 123660302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 123760302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 123860302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 123960302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 124060302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 124160302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 1242d631b94eSstephen hemminger */ 1243b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1244986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 1245b92f1e67SWillem de Bruijn if (!use_napi && 1246b92f1e67SWillem de Bruijn unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 124748925e37SRusty Russell /* More just got used, free them then recheck. */ 1248b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 1249b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1250986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 1251e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 125248925e37SRusty Russell } 125348925e37SRusty Russell } 125448925e37SRusty Russell } 125548925e37SRusty Russell 12564b7fd2e6SMichael S. Tsirkin if (kick || netif_xmit_stopped(txq)) 1257c223a078SDavid S. Miller virtqueue_kick(sq->vq); 12580b725a2cSDavid S. Miller 12590b725a2cSDavid S. Miller return NETDEV_TX_OK; 1260c223a078SDavid S. Miller } 1261c223a078SDavid S. Miller 126240cbfc37SAmos Kong /* 126340cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 126440cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 1265788a8b6dSstephen hemminger * never fail unless improperly formatted. 126640cbfc37SAmos Kong */ 126740cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 1268d24bae32Sstephen hemminger struct scatterlist *out) 126940cbfc37SAmos Kong { 1270f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 1271d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 127240cbfc37SAmos Kong 127340cbfc37SAmos Kong /* Caller should know better */ 1274f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 127540cbfc37SAmos Kong 12762ac46030SMichael S. Tsirkin vi->ctrl_status = ~0; 12772ac46030SMichael S. Tsirkin vi->ctrl_hdr.class = class; 12782ac46030SMichael S. Tsirkin vi->ctrl_hdr.cmd = cmd; 1279f7bc9594SRusty Russell /* Add header */ 12802ac46030SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr)); 1281f7bc9594SRusty Russell sgs[out_num++] = &hdr; 128240cbfc37SAmos Kong 1283f7bc9594SRusty Russell if (out) 1284f7bc9594SRusty Russell sgs[out_num++] = out; 128540cbfc37SAmos Kong 1286f7bc9594SRusty Russell /* Add return status. */ 12872ac46030SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status)); 1288d24bae32Sstephen hemminger sgs[out_num] = &stat; 128940cbfc37SAmos Kong 1290d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1291a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 129240cbfc37SAmos Kong 129367975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 12942ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 129540cbfc37SAmos Kong 129640cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 129740cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 129840cbfc37SAmos Kong */ 1299047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 1300047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 130140cbfc37SAmos Kong cpu_relax(); 130240cbfc37SAmos Kong 13032ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 130440cbfc37SAmos Kong } 130540cbfc37SAmos Kong 13069c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 13079c46f6d4SAlex Williamson { 13089c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 13099c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 1310f2f2c8b4SJiri Pirko int ret; 1311e37e2ff3SAndy Lutomirski struct sockaddr *addr; 13127e58d5aeSAmos Kong struct scatterlist sg; 13139c46f6d4SAlex Williamson 1314801822d1SShyam Saini addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 1315e37e2ff3SAndy Lutomirski if (!addr) 1316e37e2ff3SAndy Lutomirski return -ENOMEM; 1317e37e2ff3SAndy Lutomirski 1318e37e2ff3SAndy Lutomirski ret = eth_prepare_mac_addr_change(dev, addr); 1319f2f2c8b4SJiri Pirko if (ret) 1320e37e2ff3SAndy Lutomirski goto out; 13219c46f6d4SAlex Williamson 13227e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 13237e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 13247e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1325d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 13267e58d5aeSAmos Kong dev_warn(&vdev->dev, 13277e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 1328e37e2ff3SAndy Lutomirski ret = -EINVAL; 1329e37e2ff3SAndy Lutomirski goto out; 13307e58d5aeSAmos Kong } 13317e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 13327e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1333855e0c52SRusty Russell unsigned int i; 1334855e0c52SRusty Russell 1335855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 1336855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 1337855e0c52SRusty Russell virtio_cwrite8(vdev, 1338855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1339855e0c52SRusty Russell i, addr->sa_data[i]); 13407e58d5aeSAmos Kong } 13417e58d5aeSAmos Kong 13427e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 1343e37e2ff3SAndy Lutomirski ret = 0; 13449c46f6d4SAlex Williamson 1345e37e2ff3SAndy Lutomirski out: 1346e37e2ff3SAndy Lutomirski kfree(addr); 1347e37e2ff3SAndy Lutomirski return ret; 13489c46f6d4SAlex Williamson } 13499c46f6d4SAlex Williamson 1350bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev, 13513fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 13523fa2a1dfSstephen hemminger { 13533fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 13543fa2a1dfSstephen hemminger int cpu; 13553fa2a1dfSstephen hemminger unsigned int start; 13563fa2a1dfSstephen hemminger 13573fa2a1dfSstephen hemminger for_each_possible_cpu(cpu) { 135858472a76SEric Dumazet struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); 13593fa2a1dfSstephen hemminger u64 tpackets, tbytes, rpackets, rbytes; 13603fa2a1dfSstephen hemminger 13613fa2a1dfSstephen hemminger do { 136257a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->tx_syncp); 13633fa2a1dfSstephen hemminger tpackets = stats->tx_packets; 13643fa2a1dfSstephen hemminger tbytes = stats->tx_bytes; 136557a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start)); 136683a27052SEric Dumazet 136783a27052SEric Dumazet do { 136857a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->rx_syncp); 13693fa2a1dfSstephen hemminger rpackets = stats->rx_packets; 13703fa2a1dfSstephen hemminger rbytes = stats->rx_bytes; 137157a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start)); 13723fa2a1dfSstephen hemminger 13733fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 13743fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 13753fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 13763fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 13773fa2a1dfSstephen hemminger } 13783fa2a1dfSstephen hemminger 13793fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1380021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 13813fa2a1dfSstephen hemminger tot->rx_dropped = dev->stats.rx_dropped; 13823fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 13833fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 13843fa2a1dfSstephen hemminger } 13853fa2a1dfSstephen hemminger 1386da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 1387da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 1388da74e89dSAmit Shah { 1389da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 1390986a4f4dSJason Wang int i; 1391da74e89dSAmit Shah 1392986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 1393986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 1394da74e89dSAmit Shah } 1395da74e89dSAmit Shah #endif 1396da74e89dSAmit Shah 1397586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1398586d17c5SJason Wang { 1399586d17c5SJason Wang rtnl_lock(); 1400586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1401d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1402586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1403586d17c5SJason Wang rtnl_unlock(); 1404586d17c5SJason Wang } 1405586d17c5SJason Wang 140647315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1407986a4f4dSJason Wang { 1408986a4f4dSJason Wang struct scatterlist sg; 1409986a4f4dSJason Wang struct net_device *dev = vi->dev; 1410986a4f4dSJason Wang 1411986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1412986a4f4dSJason Wang return 0; 1413986a4f4dSJason Wang 1414a725ee3eSAndy Lutomirski vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 1415a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq)); 1416986a4f4dSJason Wang 1417986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1418d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1419986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1420986a4f4dSJason Wang queue_pairs); 1421986a4f4dSJason Wang return -EINVAL; 142255257d72SSasha Levin } else { 1423986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 142435ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 142535ed159bSJason Wang if (dev->flags & IFF_UP) 14269b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 142755257d72SSasha Levin } 1428986a4f4dSJason Wang 1429986a4f4dSJason Wang return 0; 1430986a4f4dSJason Wang } 1431986a4f4dSJason Wang 143247315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 143347315329SJohn Fastabend { 143447315329SJohn Fastabend int err; 143547315329SJohn Fastabend 143647315329SJohn Fastabend rtnl_lock(); 143747315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 143847315329SJohn Fastabend rtnl_unlock(); 143947315329SJohn Fastabend return err; 144047315329SJohn Fastabend } 144147315329SJohn Fastabend 1442296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1443296f96fcSRusty Russell { 1444296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1445986a4f4dSJason Wang int i; 1446296f96fcSRusty Russell 1447b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1448b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1449986a4f4dSJason Wang 1450b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 1451986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 145278a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 1453b92f1e67SWillem de Bruijn } 1454296f96fcSRusty Russell 1455296f96fcSRusty Russell return 0; 1456296f96fcSRusty Russell } 1457296f96fcSRusty Russell 14582af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 14592af7698eSAlex Williamson { 14602af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1461f565a7c2SAlex Williamson struct scatterlist sg[2]; 1462f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1463ccffad25SJiri Pirko struct netdev_hw_addr *ha; 146432e7bfc4SJiri Pirko int uc_count; 14654cd24eafSJiri Pirko int mc_count; 1466f565a7c2SAlex Williamson void *buf; 1467f565a7c2SAlex Williamson int i; 14682af7698eSAlex Williamson 1469788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 14702af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 14712af7698eSAlex Williamson return; 14722af7698eSAlex Williamson 14732ac46030SMichael S. Tsirkin vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0); 14742ac46030SMichael S. Tsirkin vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 14752af7698eSAlex Williamson 14762ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc)); 14772af7698eSAlex Williamson 14782af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1479d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 14802af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 14812ac46030SMichael S. Tsirkin vi->ctrl_promisc ? "en" : "dis"); 14822af7698eSAlex Williamson 14832ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti)); 14842af7698eSAlex Williamson 14852af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1486d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 14872af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 14882ac46030SMichael S. Tsirkin vi->ctrl_allmulti ? "en" : "dis"); 1489f565a7c2SAlex Williamson 149032e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 14914cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1492f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 14934cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1494f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 14954cd24eafSJiri Pirko mac_data = buf; 1496e68ed8f0SJoe Perches if (!buf) 1497f565a7c2SAlex Williamson return; 1498f565a7c2SAlex Williamson 149923e258e1SAlex Williamson sg_init_table(sg, 2); 150023e258e1SAlex Williamson 1501f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 1502fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1503ccffad25SJiri Pirko i = 0; 150432e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1505ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1506f565a7c2SAlex Williamson 1507f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 150832e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1509f565a7c2SAlex Williamson 1510f565a7c2SAlex Williamson /* multicast list and count fill the end */ 151132e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1512f565a7c2SAlex Williamson 1513fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1514567ec874SJiri Pirko i = 0; 151522bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 151622bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1517f565a7c2SAlex Williamson 1518f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 15194cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1520f565a7c2SAlex Williamson 1521f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1522d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 152399e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1524f565a7c2SAlex Williamson 1525f565a7c2SAlex Williamson kfree(buf); 15262af7698eSAlex Williamson } 15272af7698eSAlex Williamson 152880d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 152980d5c368SPatrick McHardy __be16 proto, u16 vid) 15300bde9569SAlex Williamson { 15310bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 15320bde9569SAlex Williamson struct scatterlist sg; 15330bde9569SAlex Williamson 1534a725ee3eSAndy Lutomirski vi->ctrl_vid = vid; 1535a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 15360bde9569SAlex Williamson 15370bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1538d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 15390bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 15408e586137SJiri Pirko return 0; 15410bde9569SAlex Williamson } 15420bde9569SAlex Williamson 154380d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 154480d5c368SPatrick McHardy __be16 proto, u16 vid) 15450bde9569SAlex Williamson { 15460bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 15470bde9569SAlex Williamson struct scatterlist sg; 15480bde9569SAlex Williamson 1549a725ee3eSAndy Lutomirski vi->ctrl_vid = vid; 1550a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 15510bde9569SAlex Williamson 15520bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1553d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 15540bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 15558e586137SJiri Pirko return 0; 15560bde9569SAlex Williamson } 15570bde9569SAlex Williamson 15588898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1559986a4f4dSJason Wang { 1560986a4f4dSJason Wang int i; 15618898c21cSWanlong Gao 15628898c21cSWanlong Gao if (vi->affinity_hint_set) { 15638898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 15648898c21cSWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, -1); 15658898c21cSWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, -1); 15668898c21cSWanlong Gao } 15678898c21cSWanlong Gao 15688898c21cSWanlong Gao vi->affinity_hint_set = false; 15698898c21cSWanlong Gao } 15708898c21cSWanlong Gao } 15718898c21cSWanlong Gao 15728898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1573986a4f4dSJason Wang { 1574986a4f4dSJason Wang int i; 157547be2479SWanlong Gao int cpu; 1576986a4f4dSJason Wang 1577986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1578986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1579986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1580986a4f4dSJason Wang */ 15818898c21cSWanlong Gao if (vi->curr_queue_pairs == 1 || 15828898c21cSWanlong Gao vi->max_queue_pairs != num_online_cpus()) { 15838898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1584986a4f4dSJason Wang return; 1585986a4f4dSJason Wang } 1586986a4f4dSJason Wang 158747be2479SWanlong Gao i = 0; 158847be2479SWanlong Gao for_each_online_cpu(cpu) { 1589986a4f4dSJason Wang virtqueue_set_affinity(vi->rq[i].vq, cpu); 1590986a4f4dSJason Wang virtqueue_set_affinity(vi->sq[i].vq, cpu); 15919bb8ca86SJason Wang netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); 159247be2479SWanlong Gao i++; 1593986a4f4dSJason Wang } 1594986a4f4dSJason Wang 1595986a4f4dSJason Wang vi->affinity_hint_set = true; 159647be2479SWanlong Gao } 1597986a4f4dSJason Wang 15988017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 15998de4b2f3SWanlong Gao { 16008017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 16018017c279SSebastian Andrzej Siewior node); 16028de4b2f3SWanlong Gao virtnet_set_affinity(vi); 16038017c279SSebastian Andrzej Siewior return 0; 16048de4b2f3SWanlong Gao } 16053ab098dfSJason Wang 16068017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 16078017c279SSebastian Andrzej Siewior { 16088017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 16098017c279SSebastian Andrzej Siewior node_dead); 16108017c279SSebastian Andrzej Siewior virtnet_set_affinity(vi); 16118017c279SSebastian Andrzej Siewior return 0; 16128017c279SSebastian Andrzej Siewior } 16138017c279SSebastian Andrzej Siewior 16148017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 16158017c279SSebastian Andrzej Siewior { 16168017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 16178017c279SSebastian Andrzej Siewior node); 16188017c279SSebastian Andrzej Siewior 16198017c279SSebastian Andrzej Siewior virtnet_clean_affinity(vi, cpu); 16208017c279SSebastian Andrzej Siewior return 0; 16218017c279SSebastian Andrzej Siewior } 16228017c279SSebastian Andrzej Siewior 16238017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online; 16248017c279SSebastian Andrzej Siewior 16258017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi) 16268017c279SSebastian Andrzej Siewior { 16278017c279SSebastian Andrzej Siewior int ret; 16288017c279SSebastian Andrzej Siewior 16298017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 16308017c279SSebastian Andrzej Siewior if (ret) 16318017c279SSebastian Andrzej Siewior return ret; 16328017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 16338017c279SSebastian Andrzej Siewior &vi->node_dead); 16348017c279SSebastian Andrzej Siewior if (!ret) 16358017c279SSebastian Andrzej Siewior return ret; 16368017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 16378017c279SSebastian Andrzej Siewior return ret; 16388017c279SSebastian Andrzej Siewior } 16398017c279SSebastian Andrzej Siewior 16408017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 16418017c279SSebastian Andrzej Siewior { 16428017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 16438017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 16448017c279SSebastian Andrzej Siewior &vi->node_dead); 1645a9ea3fc6SHerbert Xu } 1646a9ea3fc6SHerbert Xu 16478f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 16488f9f4668SRick Jones struct ethtool_ringparam *ring) 16498f9f4668SRick Jones { 16508f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 16518f9f4668SRick Jones 1652986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1653986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 16548f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 16558f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 16568f9f4668SRick Jones } 16578f9f4668SRick Jones 165866846048SRick Jones 165966846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 166066846048SRick Jones struct ethtool_drvinfo *info) 166166846048SRick Jones { 166266846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 166366846048SRick Jones struct virtio_device *vdev = vi->vdev; 166466846048SRick Jones 166566846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 166666846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 166766846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 166866846048SRick Jones 166966846048SRick Jones } 167066846048SRick Jones 1671d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1672d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1673d73bcd2cSJason Wang struct ethtool_channels *channels) 1674d73bcd2cSJason Wang { 1675d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1676d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1677d73bcd2cSJason Wang int err; 1678d73bcd2cSJason Wang 1679d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1680d73bcd2cSJason Wang * We don't allow setting 'other' channels. 1681d73bcd2cSJason Wang */ 1682d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 1683d73bcd2cSJason Wang return -EINVAL; 1684d73bcd2cSJason Wang 1685c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 1686d73bcd2cSJason Wang return -EINVAL; 1687d73bcd2cSJason Wang 1688f600b690SJohn Fastabend /* For now we don't support modifying channels while XDP is loaded 1689f600b690SJohn Fastabend * also when XDP is loaded all RX queues have XDP programs so we only 1690f600b690SJohn Fastabend * need to check a single RX queue. 1691f600b690SJohn Fastabend */ 1692f600b690SJohn Fastabend if (vi->rq[0].xdp_prog) 1693f600b690SJohn Fastabend return -EINVAL; 1694f600b690SJohn Fastabend 169547be2479SWanlong Gao get_online_cpus(); 169647315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 1697d73bcd2cSJason Wang if (!err) { 1698d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 1699d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 1700d73bcd2cSJason Wang 17018898c21cSWanlong Gao virtnet_set_affinity(vi); 1702d73bcd2cSJason Wang } 170347be2479SWanlong Gao put_online_cpus(); 1704d73bcd2cSJason Wang 1705d73bcd2cSJason Wang return err; 1706d73bcd2cSJason Wang } 1707d73bcd2cSJason Wang 1708d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 1709d73bcd2cSJason Wang struct ethtool_channels *channels) 1710d73bcd2cSJason Wang { 1711d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1712d73bcd2cSJason Wang 1713d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 1714d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 1715d73bcd2cSJason Wang channels->max_other = 0; 1716d73bcd2cSJason Wang channels->rx_count = 0; 1717d73bcd2cSJason Wang channels->tx_count = 0; 1718d73bcd2cSJason Wang channels->other_count = 0; 1719d73bcd2cSJason Wang } 1720d73bcd2cSJason Wang 172116032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */ 1722ebb6b4b1SPhilippe Reynes static bool 1723ebb6b4b1SPhilippe Reynes virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd) 172416032be5SNikolay Aleksandrov { 1725ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings diff1 = *cmd; 1726ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings diff2 = {}; 172716032be5SNikolay Aleksandrov 17280cf3ace9SNikolay Aleksandrov /* cmd is always set so we need to clear it, validate the port type 17290cf3ace9SNikolay Aleksandrov * and also without autonegotiation we can ignore advertising 17300cf3ace9SNikolay Aleksandrov */ 1731ebb6b4b1SPhilippe Reynes diff1.base.speed = 0; 1732ebb6b4b1SPhilippe Reynes diff2.base.port = PORT_OTHER; 1733ebb6b4b1SPhilippe Reynes ethtool_link_ksettings_zero_link_mode(&diff1, advertising); 1734ebb6b4b1SPhilippe Reynes diff1.base.duplex = 0; 1735ebb6b4b1SPhilippe Reynes diff1.base.cmd = 0; 1736ebb6b4b1SPhilippe Reynes diff1.base.link_mode_masks_nwords = 0; 173716032be5SNikolay Aleksandrov 1738ebb6b4b1SPhilippe Reynes return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) && 1739ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.supported, 1740ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS) && 1741ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.advertising, 1742ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS) && 1743ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.lp_advertising, 1744ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS); 174516032be5SNikolay Aleksandrov } 174616032be5SNikolay Aleksandrov 1747ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev, 1748ebb6b4b1SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 174916032be5SNikolay Aleksandrov { 175016032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 175116032be5SNikolay Aleksandrov u32 speed; 175216032be5SNikolay Aleksandrov 1753ebb6b4b1SPhilippe Reynes speed = cmd->base.speed; 175416032be5SNikolay Aleksandrov /* don't allow custom speed and duplex */ 175516032be5SNikolay Aleksandrov if (!ethtool_validate_speed(speed) || 1756ebb6b4b1SPhilippe Reynes !ethtool_validate_duplex(cmd->base.duplex) || 175716032be5SNikolay Aleksandrov !virtnet_validate_ethtool_cmd(cmd)) 175816032be5SNikolay Aleksandrov return -EINVAL; 175916032be5SNikolay Aleksandrov vi->speed = speed; 1760ebb6b4b1SPhilippe Reynes vi->duplex = cmd->base.duplex; 176116032be5SNikolay Aleksandrov 176216032be5SNikolay Aleksandrov return 0; 176316032be5SNikolay Aleksandrov } 176416032be5SNikolay Aleksandrov 1765ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev, 1766ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings *cmd) 176716032be5SNikolay Aleksandrov { 176816032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 176916032be5SNikolay Aleksandrov 1770ebb6b4b1SPhilippe Reynes cmd->base.speed = vi->speed; 1771ebb6b4b1SPhilippe Reynes cmd->base.duplex = vi->duplex; 1772ebb6b4b1SPhilippe Reynes cmd->base.port = PORT_OTHER; 177316032be5SNikolay Aleksandrov 177416032be5SNikolay Aleksandrov return 0; 177516032be5SNikolay Aleksandrov } 177616032be5SNikolay Aleksandrov 177716032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 177816032be5SNikolay Aleksandrov { 177916032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 178016032be5SNikolay Aleksandrov 178116032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 178216032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 178316032be5SNikolay Aleksandrov } 178416032be5SNikolay Aleksandrov 17850fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 178666846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 17879f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 17888f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 1789d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 1790d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 1791074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 1792ebb6b4b1SPhilippe Reynes .get_link_ksettings = virtnet_get_link_ksettings, 1793ebb6b4b1SPhilippe Reynes .set_link_ksettings = virtnet_set_link_ksettings, 1794a9ea3fc6SHerbert Xu }; 1795a9ea3fc6SHerbert Xu 17969fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev) 17979fe7bfceSJohn Fastabend { 17989fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 17999fe7bfceSJohn Fastabend int i; 18009fe7bfceSJohn Fastabend 18019fe7bfceSJohn Fastabend /* Make sure no work handler is accessing the device */ 18029fe7bfceSJohn Fastabend flush_work(&vi->config_work); 18039fe7bfceSJohn Fastabend 18049fe7bfceSJohn Fastabend netif_device_detach(vi->dev); 1805713a98d9SJason Wang netif_tx_disable(vi->dev); 18069fe7bfceSJohn Fastabend cancel_delayed_work_sync(&vi->refill); 18079fe7bfceSJohn Fastabend 18089fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 1809b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 18109fe7bfceSJohn Fastabend napi_disable(&vi->rq[i].napi); 181178a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 1812b92f1e67SWillem de Bruijn } 18139fe7bfceSJohn Fastabend } 18149fe7bfceSJohn Fastabend } 18159fe7bfceSJohn Fastabend 18169fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi); 18172de2f7f4SJohn Fastabend static void _remove_vq_common(struct virtnet_info *vi); 18189fe7bfceSJohn Fastabend 18199fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev) 18209fe7bfceSJohn Fastabend { 18219fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 18229fe7bfceSJohn Fastabend int err, i; 18239fe7bfceSJohn Fastabend 18249fe7bfceSJohn Fastabend err = init_vqs(vi); 18259fe7bfceSJohn Fastabend if (err) 18269fe7bfceSJohn Fastabend return err; 18279fe7bfceSJohn Fastabend 18289fe7bfceSJohn Fastabend virtio_device_ready(vdev); 18299fe7bfceSJohn Fastabend 18309fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 18319fe7bfceSJohn Fastabend for (i = 0; i < vi->curr_queue_pairs; i++) 18329fe7bfceSJohn Fastabend if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 18339fe7bfceSJohn Fastabend schedule_delayed_work(&vi->refill, 0); 18349fe7bfceSJohn Fastabend 1835b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 1836e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1837b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, 1838b92f1e67SWillem de Bruijn &vi->sq[i].napi); 1839b92f1e67SWillem de Bruijn } 18409fe7bfceSJohn Fastabend } 18419fe7bfceSJohn Fastabend 18429fe7bfceSJohn Fastabend netif_device_attach(vi->dev); 18439fe7bfceSJohn Fastabend return err; 18449fe7bfceSJohn Fastabend } 18459fe7bfceSJohn Fastabend 1846017b29c3SJason Wang static int virtnet_reset(struct virtnet_info *vi, int curr_qp, int xdp_qp) 18472de2f7f4SJohn Fastabend { 18482de2f7f4SJohn Fastabend struct virtio_device *dev = vi->vdev; 18492de2f7f4SJohn Fastabend int ret; 18502de2f7f4SJohn Fastabend 18512de2f7f4SJohn Fastabend virtio_config_disable(dev); 18522de2f7f4SJohn Fastabend dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED; 18532de2f7f4SJohn Fastabend virtnet_freeze_down(dev); 18542de2f7f4SJohn Fastabend _remove_vq_common(vi); 18552de2f7f4SJohn Fastabend 18562de2f7f4SJohn Fastabend virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); 18572de2f7f4SJohn Fastabend virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER); 18582de2f7f4SJohn Fastabend 18592de2f7f4SJohn Fastabend ret = virtio_finalize_features(dev); 18602de2f7f4SJohn Fastabend if (ret) 18612de2f7f4SJohn Fastabend goto err; 18622de2f7f4SJohn Fastabend 1863017b29c3SJason Wang vi->xdp_queue_pairs = xdp_qp; 18642de2f7f4SJohn Fastabend ret = virtnet_restore_up(dev); 18652de2f7f4SJohn Fastabend if (ret) 18662de2f7f4SJohn Fastabend goto err; 1867017b29c3SJason Wang ret = _virtnet_set_queues(vi, curr_qp); 18682de2f7f4SJohn Fastabend if (ret) 18692de2f7f4SJohn Fastabend goto err; 18702de2f7f4SJohn Fastabend 18712de2f7f4SJohn Fastabend virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK); 18722de2f7f4SJohn Fastabend virtio_config_enable(dev); 18732de2f7f4SJohn Fastabend return 0; 18742de2f7f4SJohn Fastabend err: 18752de2f7f4SJohn Fastabend virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); 18762de2f7f4SJohn Fastabend return ret; 18772de2f7f4SJohn Fastabend } 18782de2f7f4SJohn Fastabend 18799861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 18809861ce03SJakub Kicinski struct netlink_ext_ack *extack) 1881f600b690SJohn Fastabend { 1882f600b690SJohn Fastabend unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); 1883f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 1884f600b690SJohn Fastabend struct bpf_prog *old_prog; 1885017b29c3SJason Wang u16 xdp_qp = 0, curr_qp; 1886672aafd5SJohn Fastabend int i, err; 1887f600b690SJohn Fastabend 1888f600b690SJohn Fastabend if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 188992502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 189092502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 189192502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) { 18924d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first"); 1893f600b690SJohn Fastabend return -EOPNOTSUPP; 1894f600b690SJohn Fastabend } 1895f600b690SJohn Fastabend 1896f600b690SJohn Fastabend if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 18974d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 1898f600b690SJohn Fastabend return -EINVAL; 1899f600b690SJohn Fastabend } 1900f600b690SJohn Fastabend 1901f600b690SJohn Fastabend if (dev->mtu > max_sz) { 19024d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); 1903f600b690SJohn Fastabend netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); 1904f600b690SJohn Fastabend return -EINVAL; 1905f600b690SJohn Fastabend } 1906f600b690SJohn Fastabend 1907672aafd5SJohn Fastabend curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 1908672aafd5SJohn Fastabend if (prog) 1909672aafd5SJohn Fastabend xdp_qp = nr_cpu_ids; 1910672aafd5SJohn Fastabend 1911672aafd5SJohn Fastabend /* XDP requires extra queues for XDP_TX */ 1912672aafd5SJohn Fastabend if (curr_qp + xdp_qp > vi->max_queue_pairs) { 19134d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available"); 1914672aafd5SJohn Fastabend netdev_warn(dev, "request %i queues but max is %i\n", 1915672aafd5SJohn Fastabend curr_qp + xdp_qp, vi->max_queue_pairs); 1916672aafd5SJohn Fastabend return -ENOMEM; 1917672aafd5SJohn Fastabend } 1918672aafd5SJohn Fastabend 19192de2f7f4SJohn Fastabend if (prog) { 19202de2f7f4SJohn Fastabend prog = bpf_prog_add(prog, vi->max_queue_pairs - 1); 19212de2f7f4SJohn Fastabend if (IS_ERR(prog)) 19222de2f7f4SJohn Fastabend return PTR_ERR(prog); 19232de2f7f4SJohn Fastabend } 19242de2f7f4SJohn Fastabend 19252de2f7f4SJohn Fastabend /* Changing the headroom in buffers is a disruptive operation because 19262de2f7f4SJohn Fastabend * existing buffers must be flushed and reallocated. This will happen 19272de2f7f4SJohn Fastabend * when a xdp program is initially added or xdp is disabled by removing 19282de2f7f4SJohn Fastabend * the xdp program resulting in number of XDP queues changing. 19292de2f7f4SJohn Fastabend */ 19302de2f7f4SJohn Fastabend if (vi->xdp_queue_pairs != xdp_qp) { 1931017b29c3SJason Wang err = virtnet_reset(vi, curr_qp + xdp_qp, xdp_qp); 1932017b29c3SJason Wang if (err) { 1933017b29c3SJason Wang dev_warn(&dev->dev, "XDP reset failure.\n"); 19342de2f7f4SJohn Fastabend goto virtio_reset_err; 19352de2f7f4SJohn Fastabend } 1936017b29c3SJason Wang } 19372de2f7f4SJohn Fastabend 1938672aafd5SJohn Fastabend netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 1939f600b690SJohn Fastabend 1940f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 1941f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 1942f600b690SJohn Fastabend rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 1943f600b690SJohn Fastabend if (old_prog) 1944f600b690SJohn Fastabend bpf_prog_put(old_prog); 1945f600b690SJohn Fastabend } 1946f600b690SJohn Fastabend 1947f600b690SJohn Fastabend return 0; 19482de2f7f4SJohn Fastabend 19492de2f7f4SJohn Fastabend virtio_reset_err: 19502de2f7f4SJohn Fastabend /* On reset error do our best to unwind XDP changes inflight and return 19512de2f7f4SJohn Fastabend * error up to user space for resolution. The underlying reset hung on 19522de2f7f4SJohn Fastabend * us so not much we can do here. 19532de2f7f4SJohn Fastabend */ 19542de2f7f4SJohn Fastabend if (prog) 19552de2f7f4SJohn Fastabend bpf_prog_sub(prog, vi->max_queue_pairs - 1); 19562de2f7f4SJohn Fastabend return err; 1957f600b690SJohn Fastabend } 1958f600b690SJohn Fastabend 19595b0e6629SMartin KaFai Lau static u32 virtnet_xdp_query(struct net_device *dev) 1960f600b690SJohn Fastabend { 1961f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 19625b0e6629SMartin KaFai Lau const struct bpf_prog *xdp_prog; 1963f600b690SJohn Fastabend int i; 1964f600b690SJohn Fastabend 1965f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 19665b0e6629SMartin KaFai Lau xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog); 19675b0e6629SMartin KaFai Lau if (xdp_prog) 19685b0e6629SMartin KaFai Lau return xdp_prog->aux->id; 1969f600b690SJohn Fastabend } 19705b0e6629SMartin KaFai Lau return 0; 1971f600b690SJohn Fastabend } 1972f600b690SJohn Fastabend 1973f600b690SJohn Fastabend static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp) 1974f600b690SJohn Fastabend { 1975f600b690SJohn Fastabend switch (xdp->command) { 1976f600b690SJohn Fastabend case XDP_SETUP_PROG: 19779861ce03SJakub Kicinski return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 1978f600b690SJohn Fastabend case XDP_QUERY_PROG: 19795b0e6629SMartin KaFai Lau xdp->prog_id = virtnet_xdp_query(dev); 19805b0e6629SMartin KaFai Lau xdp->prog_attached = !!xdp->prog_id; 1981f600b690SJohn Fastabend return 0; 1982f600b690SJohn Fastabend default: 1983f600b690SJohn Fastabend return -EINVAL; 1984f600b690SJohn Fastabend } 1985f600b690SJohn Fastabend } 1986f600b690SJohn Fastabend 198776288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 198876288b4eSStephen Hemminger .ndo_open = virtnet_open, 198976288b4eSStephen Hemminger .ndo_stop = virtnet_close, 199076288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 199176288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 19929c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 19932af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 19943fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 19951824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 19961824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 199776288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 199876288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 199976288b4eSStephen Hemminger #endif 2000f600b690SJohn Fastabend .ndo_xdp = virtnet_xdp, 20012836b4f2SVlad Yasevich .ndo_features_check = passthru_features_check, 200276288b4eSStephen Hemminger }; 200376288b4eSStephen Hemminger 2004586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 20059f4d26d0SMark McLoughlin { 2006586d17c5SJason Wang struct virtnet_info *vi = 2007586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 20089f4d26d0SMark McLoughlin u16 v; 20099f4d26d0SMark McLoughlin 2010855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 2011855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 2012507613bfSMichael S. Tsirkin return; 2013586d17c5SJason Wang 2014586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 2015ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 2016586d17c5SJason Wang virtnet_ack_link_announce(vi); 2017586d17c5SJason Wang } 20189f4d26d0SMark McLoughlin 20199f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 20209f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 20219f4d26d0SMark McLoughlin 20229f4d26d0SMark McLoughlin if (vi->status == v) 2023507613bfSMichael S. Tsirkin return; 20249f4d26d0SMark McLoughlin 20259f4d26d0SMark McLoughlin vi->status = v; 20269f4d26d0SMark McLoughlin 20279f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 20289f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 2029986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 20309f4d26d0SMark McLoughlin } else { 20319f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 2032986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 20339f4d26d0SMark McLoughlin } 20349f4d26d0SMark McLoughlin } 20359f4d26d0SMark McLoughlin 20369f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 20379f4d26d0SMark McLoughlin { 20389f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 20399f4d26d0SMark McLoughlin 20403b07e9caSTejun Heo schedule_work(&vi->config_work); 20419f4d26d0SMark McLoughlin } 20429f4d26d0SMark McLoughlin 2043986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 2044986a4f4dSJason Wang { 2045d4fb84eeSAndrey Vagin int i; 2046d4fb84eeSAndrey Vagin 2047ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2048ab3971b1SJason Wang napi_hash_del(&vi->rq[i].napi); 2049d4fb84eeSAndrey Vagin netif_napi_del(&vi->rq[i].napi); 2050b92f1e67SWillem de Bruijn netif_napi_del(&vi->sq[i].napi); 2051ab3971b1SJason Wang } 2052d4fb84eeSAndrey Vagin 2053963abe5cSEric Dumazet /* We called napi_hash_del() before netif_napi_del(), 2054963abe5cSEric Dumazet * we need to respect an RCU grace period before freeing vi->rq 2055963abe5cSEric Dumazet */ 2056963abe5cSEric Dumazet synchronize_net(); 2057963abe5cSEric Dumazet 2058986a4f4dSJason Wang kfree(vi->rq); 2059986a4f4dSJason Wang kfree(vi->sq); 2060986a4f4dSJason Wang } 2061986a4f4dSJason Wang 206247315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi) 2063986a4f4dSJason Wang { 2064f600b690SJohn Fastabend struct bpf_prog *old_prog; 2065986a4f4dSJason Wang int i; 2066986a4f4dSJason Wang 2067986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2068986a4f4dSJason Wang while (vi->rq[i].pages) 2069986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 2070f600b690SJohn Fastabend 2071f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2072f600b690SJohn Fastabend RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 2073f600b690SJohn Fastabend if (old_prog) 2074f600b690SJohn Fastabend bpf_prog_put(old_prog); 2075986a4f4dSJason Wang } 207647315329SJohn Fastabend } 207747315329SJohn Fastabend 207847315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi) 207947315329SJohn Fastabend { 208047315329SJohn Fastabend rtnl_lock(); 208147315329SJohn Fastabend _free_receive_bufs(vi); 2082f600b690SJohn Fastabend rtnl_unlock(); 2083986a4f4dSJason Wang } 2084986a4f4dSJason Wang 2085fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 2086fb51879dSMichael Dalton { 2087fb51879dSMichael Dalton int i; 2088fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 2089fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 2090fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 2091fb51879dSMichael Dalton } 2092fb51879dSMichael Dalton 2093b68df015SJohn Fastabend static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 209456434a01SJohn Fastabend { 209556434a01SJohn Fastabend if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 209656434a01SJohn Fastabend return false; 209756434a01SJohn Fastabend else if (q < vi->curr_queue_pairs) 209856434a01SJohn Fastabend return true; 209956434a01SJohn Fastabend else 210056434a01SJohn Fastabend return false; 210156434a01SJohn Fastabend } 210256434a01SJohn Fastabend 2103986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 2104986a4f4dSJason Wang { 2105986a4f4dSJason Wang void *buf; 2106986a4f4dSJason Wang int i; 2107986a4f4dSJason Wang 2108986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2109986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 211056434a01SJohn Fastabend while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2111b68df015SJohn Fastabend if (!is_xdp_raw_buffer_queue(vi, i)) 2112986a4f4dSJason Wang dev_kfree_skb(buf); 211356434a01SJohn Fastabend else 211456434a01SJohn Fastabend put_page(virt_to_head_page(buf)); 211556434a01SJohn Fastabend } 2116986a4f4dSJason Wang } 2117986a4f4dSJason Wang 2118986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2119986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 2120986a4f4dSJason Wang 2121986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2122ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 2123680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 2124ab7db917SMichael Dalton } else if (vi->big_packets) { 2125fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 2126ab7db917SMichael Dalton } else { 2127f6b10209SJason Wang put_page(virt_to_head_page(buf)); 2128986a4f4dSJason Wang } 2129986a4f4dSJason Wang } 2130986a4f4dSJason Wang } 2131ab7db917SMichael Dalton } 2132986a4f4dSJason Wang 2133e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 2134e9d7417bSJason Wang { 2135e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 2136e9d7417bSJason Wang 21378898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 2138986a4f4dSJason Wang 2139e9d7417bSJason Wang vdev->config->del_vqs(vdev); 2140986a4f4dSJason Wang 2141986a4f4dSJason Wang virtnet_free_queues(vi); 2142986a4f4dSJason Wang } 2143986a4f4dSJason Wang 2144d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at 2145d85b758fSMichael S. Tsirkin * least one full packet? 2146d85b758fSMichael S. Tsirkin * Logic below assumes the mergeable buffer header is used. 2147d85b758fSMichael S. Tsirkin */ 2148d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 2149d85b758fSMichael S. Tsirkin { 2150d85b758fSMichael S. Tsirkin const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2151d85b758fSMichael S. Tsirkin unsigned int rq_size = virtqueue_get_vring_size(vq); 2152d85b758fSMichael S. Tsirkin unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 2153d85b758fSMichael S. Tsirkin unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 2154d85b758fSMichael S. Tsirkin unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 2155d85b758fSMichael S. Tsirkin 2156f0c3192cSMichael S. Tsirkin return max(max(min_buf_len, hdr_len) - hdr_len, 2157f0c3192cSMichael S. Tsirkin (unsigned int)GOOD_PACKET_LEN); 2158d85b758fSMichael S. Tsirkin } 2159d85b758fSMichael S. Tsirkin 2160986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 2161986a4f4dSJason Wang { 2162986a4f4dSJason Wang vq_callback_t **callbacks; 2163986a4f4dSJason Wang struct virtqueue **vqs; 2164986a4f4dSJason Wang int ret = -ENOMEM; 2165986a4f4dSJason Wang int i, total_vqs; 2166986a4f4dSJason Wang const char **names; 2167d45b897bSMichael S. Tsirkin bool *ctx; 2168986a4f4dSJason Wang 2169986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 2170986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 2171986a4f4dSJason Wang * possible control vq. 2172986a4f4dSJason Wang */ 2173986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 2174986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 2175986a4f4dSJason Wang 2176986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 2177986a4f4dSJason Wang vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 2178986a4f4dSJason Wang if (!vqs) 2179986a4f4dSJason Wang goto err_vq; 2180986a4f4dSJason Wang callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 2181986a4f4dSJason Wang if (!callbacks) 2182986a4f4dSJason Wang goto err_callback; 2183986a4f4dSJason Wang names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 2184986a4f4dSJason Wang if (!names) 2185986a4f4dSJason Wang goto err_names; 2186d45b897bSMichael S. Tsirkin if (vi->mergeable_rx_bufs) { 2187d45b897bSMichael S. Tsirkin ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL); 2188d45b897bSMichael S. Tsirkin if (!ctx) 2189d45b897bSMichael S. Tsirkin goto err_ctx; 2190d45b897bSMichael S. Tsirkin } else { 2191d45b897bSMichael S. Tsirkin ctx = NULL; 2192d45b897bSMichael S. Tsirkin } 2193986a4f4dSJason Wang 2194986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 2195986a4f4dSJason Wang if (vi->has_cvq) { 2196986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 2197986a4f4dSJason Wang names[total_vqs - 1] = "control"; 2198986a4f4dSJason Wang } 2199986a4f4dSJason Wang 2200986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 2201986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2202986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 2203986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 2204986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 2205986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 2206986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 2207986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 2208d45b897bSMichael S. Tsirkin if (ctx) 2209d45b897bSMichael S. Tsirkin ctx[rxq2vq(i)] = true; 2210986a4f4dSJason Wang } 2211986a4f4dSJason Wang 2212986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 2213d45b897bSMichael S. Tsirkin names, ctx, NULL); 2214986a4f4dSJason Wang if (ret) 2215986a4f4dSJason Wang goto err_find; 2216986a4f4dSJason Wang 2217986a4f4dSJason Wang if (vi->has_cvq) { 2218986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 2219986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 2220f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2221986a4f4dSJason Wang } 2222986a4f4dSJason Wang 2223986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2224986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 2225d85b758fSMichael S. Tsirkin vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 2226986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 2227986a4f4dSJason Wang } 2228986a4f4dSJason Wang 2229986a4f4dSJason Wang kfree(names); 2230986a4f4dSJason Wang kfree(callbacks); 2231986a4f4dSJason Wang kfree(vqs); 223255281621SJason Wang kfree(ctx); 2233986a4f4dSJason Wang 2234986a4f4dSJason Wang return 0; 2235986a4f4dSJason Wang 2236986a4f4dSJason Wang err_find: 2237d45b897bSMichael S. Tsirkin kfree(ctx); 2238d45b897bSMichael S. Tsirkin err_ctx: 2239986a4f4dSJason Wang kfree(names); 2240986a4f4dSJason Wang err_names: 2241986a4f4dSJason Wang kfree(callbacks); 2242986a4f4dSJason Wang err_callback: 2243986a4f4dSJason Wang kfree(vqs); 2244986a4f4dSJason Wang err_vq: 2245986a4f4dSJason Wang return ret; 2246986a4f4dSJason Wang } 2247986a4f4dSJason Wang 2248986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 2249986a4f4dSJason Wang { 2250986a4f4dSJason Wang int i; 2251986a4f4dSJason Wang 2252986a4f4dSJason Wang vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 2253986a4f4dSJason Wang if (!vi->sq) 2254986a4f4dSJason Wang goto err_sq; 2255986a4f4dSJason Wang vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 2256008d4278SAmerigo Wang if (!vi->rq) 2257986a4f4dSJason Wang goto err_rq; 2258986a4f4dSJason Wang 2259986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 2260986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2261986a4f4dSJason Wang vi->rq[i].pages = NULL; 2262986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2263986a4f4dSJason Wang napi_weight); 22641d11e732SWillem de Bruijn netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2265b92f1e67SWillem de Bruijn napi_tx ? napi_weight : 0); 2266986a4f4dSJason Wang 2267986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 22685377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 2269986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 2270986a4f4dSJason Wang } 2271986a4f4dSJason Wang 2272986a4f4dSJason Wang return 0; 2273986a4f4dSJason Wang 2274986a4f4dSJason Wang err_rq: 2275986a4f4dSJason Wang kfree(vi->sq); 2276986a4f4dSJason Wang err_sq: 2277986a4f4dSJason Wang return -ENOMEM; 2278e9d7417bSJason Wang } 2279e9d7417bSJason Wang 22803f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 22813f9c10b0SAmit Shah { 2282986a4f4dSJason Wang int ret; 22833f9c10b0SAmit Shah 2284986a4f4dSJason Wang /* Allocate send & receive queues */ 2285986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 2286986a4f4dSJason Wang if (ret) 2287986a4f4dSJason Wang goto err; 22883f9c10b0SAmit Shah 2289986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 2290986a4f4dSJason Wang if (ret) 2291986a4f4dSJason Wang goto err_free; 22923f9c10b0SAmit Shah 229347be2479SWanlong Gao get_online_cpus(); 22948898c21cSWanlong Gao virtnet_set_affinity(vi); 229547be2479SWanlong Gao put_online_cpus(); 229647be2479SWanlong Gao 22973f9c10b0SAmit Shah return 0; 2298986a4f4dSJason Wang 2299986a4f4dSJason Wang err_free: 2300986a4f4dSJason Wang virtnet_free_queues(vi); 2301986a4f4dSJason Wang err: 2302986a4f4dSJason Wang return ret; 23033f9c10b0SAmit Shah } 23043f9c10b0SAmit Shah 2305fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2306fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 2307fbf28d78SMichael Dalton struct rx_queue_attribute *attribute, char *buf) 2308fbf28d78SMichael Dalton { 2309fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 2310fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 23115377d758SJohannes Berg struct ewma_pkt_len *avg; 2312fbf28d78SMichael Dalton 2313fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 2314fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 2315d85b758fSMichael S. Tsirkin return sprintf(buf, "%u\n", 2316d85b758fSMichael S. Tsirkin get_mergeable_buf_len(&vi->rq[queue_index], avg)); 2317fbf28d78SMichael Dalton } 2318fbf28d78SMichael Dalton 2319fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 2320fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 2321fbf28d78SMichael Dalton 2322fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 2323fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 2324fbf28d78SMichael Dalton NULL 2325fbf28d78SMichael Dalton }; 2326fbf28d78SMichael Dalton 2327fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 2328fbf28d78SMichael Dalton .name = "virtio_net", 2329fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 2330fbf28d78SMichael Dalton }; 2331fbf28d78SMichael Dalton #endif 2332fbf28d78SMichael Dalton 2333892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 2334892d6eb1SJason Wang unsigned int fbit, 2335892d6eb1SJason Wang const char *fname, const char *dname) 2336892d6eb1SJason Wang { 2337892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 2338892d6eb1SJason Wang return false; 2339892d6eb1SJason Wang 2340892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 2341892d6eb1SJason Wang fname, dname); 2342892d6eb1SJason Wang 2343892d6eb1SJason Wang return true; 2344892d6eb1SJason Wang } 2345892d6eb1SJason Wang 2346892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 2347892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 2348892d6eb1SJason Wang 2349892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 2350892d6eb1SJason Wang { 2351892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 2352892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 2353892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2354892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 2355892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2356892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 2357892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2358892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 2359892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 2360892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 2361892d6eb1SJason Wang return false; 2362892d6eb1SJason Wang } 2363892d6eb1SJason Wang 2364892d6eb1SJason Wang return true; 2365892d6eb1SJason Wang } 2366892d6eb1SJason Wang 2367d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU 2368d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU 2369d0c2c997SJarod Wilson 2370fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev) 2371296f96fcSRusty Russell { 23726ba42248SMichael S. Tsirkin if (!vdev->config->get) { 23736ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 23746ba42248SMichael S. Tsirkin __func__); 23756ba42248SMichael S. Tsirkin return -EINVAL; 23766ba42248SMichael S. Tsirkin } 23776ba42248SMichael S. Tsirkin 2378892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 2379892d6eb1SJason Wang return -EINVAL; 2380892d6eb1SJason Wang 2381fe36cbe0SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 2382fe36cbe0SMichael S. Tsirkin int mtu = virtio_cread16(vdev, 2383fe36cbe0SMichael S. Tsirkin offsetof(struct virtio_net_config, 2384fe36cbe0SMichael S. Tsirkin mtu)); 2385fe36cbe0SMichael S. Tsirkin if (mtu < MIN_MTU) 2386fe36cbe0SMichael S. Tsirkin __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 2387fe36cbe0SMichael S. Tsirkin } 2388fe36cbe0SMichael S. Tsirkin 2389fe36cbe0SMichael S. Tsirkin return 0; 2390fe36cbe0SMichael S. Tsirkin } 2391fe36cbe0SMichael S. Tsirkin 2392fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev) 2393fe36cbe0SMichael S. Tsirkin { 2394fe36cbe0SMichael S. Tsirkin int i, err; 2395fe36cbe0SMichael S. Tsirkin struct net_device *dev; 2396fe36cbe0SMichael S. Tsirkin struct virtnet_info *vi; 2397fe36cbe0SMichael S. Tsirkin u16 max_queue_pairs; 2398fe36cbe0SMichael S. Tsirkin int mtu; 2399fe36cbe0SMichael S. Tsirkin 2400986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 2401855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 2402855e0c52SRusty Russell struct virtio_net_config, 2403855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 2404986a4f4dSJason Wang 2405986a4f4dSJason Wang /* We need at least 2 queue's */ 2406986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 2407986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 2408986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2409986a4f4dSJason Wang max_queue_pairs = 1; 2410296f96fcSRusty Russell 2411296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 2412986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 2413296f96fcSRusty Russell if (!dev) 2414296f96fcSRusty Russell return -ENOMEM; 2415296f96fcSRusty Russell 2416296f96fcSRusty Russell /* Set up network device as normal. */ 2417f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 241876288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 2419296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 24203fa2a1dfSstephen hemminger 24217ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 2422296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 2423296f96fcSRusty Russell 2424296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 242598e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 2426296f96fcSRusty Russell /* This opens up the world of extra features. */ 242748900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 242898e778c9SMichał Mirosław if (csum) 242948900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 243098e778c9SMichał Mirosław 243198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 2432e3e3c423SVlad Yasevich dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO 243334a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 243434a48579SRusty Russell } 24355539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 243698e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 243798e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 243898e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 243998e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 244098e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 244198e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 2442e3e3c423SVlad Yasevich if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 2443e3e3c423SVlad Yasevich dev->hw_features |= NETIF_F_UFO; 244498e778c9SMichał Mirosław 244541f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 244641f2f127SJason Wang 244798e778c9SMichał Mirosław if (gso) 2448e3e3c423SVlad Yasevich dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO); 244998e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 2450296f96fcSRusty Russell } 24514f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 24524f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 2453296f96fcSRusty Russell 24544fda8302SJason Wang dev->vlan_features = dev->features; 24554fda8302SJason Wang 2456d0c2c997SJarod Wilson /* MTU range: 68 - 65535 */ 2457d0c2c997SJarod Wilson dev->min_mtu = MIN_MTU; 2458d0c2c997SJarod Wilson dev->max_mtu = MAX_MTU; 2459d0c2c997SJarod Wilson 2460296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 2461855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 2462855e0c52SRusty Russell virtio_cread_bytes(vdev, 2463a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 2464855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 2465855e0c52SRusty Russell else 2466f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 2467296f96fcSRusty Russell 2468296f96fcSRusty Russell /* Set up our device-specific information */ 2469296f96fcSRusty Russell vi = netdev_priv(dev); 2470296f96fcSRusty Russell vi->dev = dev; 2471296f96fcSRusty Russell vi->vdev = vdev; 2472d9d5dcc8SChristian Borntraeger vdev->priv = vi; 24733fa2a1dfSstephen hemminger vi->stats = alloc_percpu(struct virtnet_stats); 24743fa2a1dfSstephen hemminger err = -ENOMEM; 24753fa2a1dfSstephen hemminger if (vi->stats == NULL) 24763fa2a1dfSstephen hemminger goto free; 24773fa2a1dfSstephen hemminger 2478827da44cSJohn Stultz for_each_possible_cpu(i) { 2479827da44cSJohn Stultz struct virtnet_stats *virtnet_stats; 2480827da44cSJohn Stultz virtnet_stats = per_cpu_ptr(vi->stats, i); 2481827da44cSJohn Stultz u64_stats_init(&virtnet_stats->tx_syncp); 2482827da44cSJohn Stultz u64_stats_init(&virtnet_stats->rx_syncp); 2483827da44cSJohn Stultz } 2484827da44cSJohn Stultz 2485586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 2486296f96fcSRusty Russell 248797402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 24888e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 24898e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 2490e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 2491e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 249297402b96SHerbert Xu vi->big_packets = true; 249397402b96SHerbert Xu 24943f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 24953f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 24963f2c31d9SMark McLoughlin 2497d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 2498d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 2499012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2500012873d0SMichael S. Tsirkin else 2501012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 2502012873d0SMichael S. Tsirkin 250375993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 250475993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 2505e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 2506e7428e95SMichael S. Tsirkin 2507986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2508986a4f4dSJason Wang vi->has_cvq = true; 2509986a4f4dSJason Wang 251014de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 251114de9d11SAaron Conole mtu = virtio_cread16(vdev, 251214de9d11SAaron Conole offsetof(struct virtio_net_config, 251314de9d11SAaron Conole mtu)); 251493a205eeSAaron Conole if (mtu < dev->min_mtu) { 2515fe36cbe0SMichael S. Tsirkin /* Should never trigger: MTU was previously validated 2516fe36cbe0SMichael S. Tsirkin * in virtnet_validate. 2517fe36cbe0SMichael S. Tsirkin */ 2518fe36cbe0SMichael S. Tsirkin dev_err(&vdev->dev, "device MTU appears to have changed " 2519fe36cbe0SMichael S. Tsirkin "it is now %d < %d", mtu, dev->min_mtu); 2520fe36cbe0SMichael S. Tsirkin goto free_stats; 2521fe36cbe0SMichael S. Tsirkin } 2522fe36cbe0SMichael S. Tsirkin 2523d0c2c997SJarod Wilson dev->mtu = mtu; 252493a205eeSAaron Conole dev->max_mtu = mtu; 25252e123b44SMichael S. Tsirkin 25262e123b44SMichael S. Tsirkin /* TODO: size buffers correctly in this case. */ 25272e123b44SMichael S. Tsirkin if (dev->mtu > ETH_DATA_LEN) 25282e123b44SMichael S. Tsirkin vi->big_packets = true; 252914de9d11SAaron Conole } 253014de9d11SAaron Conole 2531012873d0SMichael S. Tsirkin if (vi->any_header_sg) 2532012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 25336ebbc1a6SZhangjie \(HZ\) 253444900010SJason Wang /* Enable multiqueue by default */ 253544900010SJason Wang if (num_online_cpus() >= max_queue_pairs) 253644900010SJason Wang vi->curr_queue_pairs = max_queue_pairs; 253744900010SJason Wang else 253844900010SJason Wang vi->curr_queue_pairs = num_online_cpus(); 2539986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 2540986a4f4dSJason Wang 2541986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 25423f9c10b0SAmit Shah err = init_vqs(vi); 2543d2a7dddaSMichael S. Tsirkin if (err) 25449bb8ca86SJason Wang goto free_stats; 2545d2a7dddaSMichael S. Tsirkin 2546fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2547fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 2548fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 2549fbf28d78SMichael Dalton #endif 25500f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 25510f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 2552986a4f4dSJason Wang 255316032be5SNikolay Aleksandrov virtnet_init_settings(dev); 255416032be5SNikolay Aleksandrov 2555296f96fcSRusty Russell err = register_netdev(dev); 2556296f96fcSRusty Russell if (err) { 2557296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 2558d2a7dddaSMichael S. Tsirkin goto free_vqs; 2559296f96fcSRusty Russell } 2560b3369c1fSRusty Russell 25614baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 25624baf1e33SMichael S. Tsirkin 25638017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 25648de4b2f3SWanlong Gao if (err) { 25658de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 2566f00e35e2Swangyunjian goto free_unregister_netdev; 25678de4b2f3SWanlong Gao } 25688de4b2f3SWanlong Gao 2569a220871bSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 257044900010SJason Wang 2571167c25e4SJason Wang /* Assume link up if device can't report link status, 2572167c25e4SJason Wang otherwise get link status from config. */ 2573167c25e4SJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 2574167c25e4SJason Wang netif_carrier_off(dev); 25753b07e9caSTejun Heo schedule_work(&vi->config_work); 2576167c25e4SJason Wang } else { 2577167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 25784783256eSPantelis Koukousoulas netif_carrier_on(dev); 2579167c25e4SJason Wang } 25809f4d26d0SMark McLoughlin 2581986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 2582986a4f4dSJason Wang dev->name, max_queue_pairs); 2583986a4f4dSJason Wang 2584296f96fcSRusty Russell return 0; 2585296f96fcSRusty Russell 2586f00e35e2Swangyunjian free_unregister_netdev: 258702465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 258802465555SMichael S. Tsirkin 2589b3369c1fSRusty Russell unregister_netdev(dev); 2590d2a7dddaSMichael S. Tsirkin free_vqs: 2591986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 2592fb51879dSMichael Dalton free_receive_page_frags(vi); 2593e9d7417bSJason Wang virtnet_del_vqs(vi); 25943fa2a1dfSstephen hemminger free_stats: 25953fa2a1dfSstephen hemminger free_percpu(vi->stats); 2596296f96fcSRusty Russell free: 2597296f96fcSRusty Russell free_netdev(dev); 2598296f96fcSRusty Russell return err; 2599296f96fcSRusty Russell } 2600296f96fcSRusty Russell 26012de2f7f4SJohn Fastabend static void _remove_vq_common(struct virtnet_info *vi) 26022de2f7f4SJohn Fastabend { 26032de2f7f4SJohn Fastabend vi->vdev->config->reset(vi->vdev); 26042de2f7f4SJohn Fastabend free_unused_bufs(vi); 26052de2f7f4SJohn Fastabend _free_receive_bufs(vi); 26062de2f7f4SJohn Fastabend free_receive_page_frags(vi); 26072de2f7f4SJohn Fastabend virtnet_del_vqs(vi); 26082de2f7f4SJohn Fastabend } 26092de2f7f4SJohn Fastabend 261004486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 2611296f96fcSRusty Russell { 261204486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 2613830a8a97SShirley Ma 2614830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 26159ab86bbcSShirley Ma free_unused_bufs(vi); 2616fb6813f4SRusty Russell 2617986a4f4dSJason Wang free_receive_bufs(vi); 2618d2a7dddaSMichael S. Tsirkin 2619fb51879dSMichael Dalton free_receive_page_frags(vi); 2620fb51879dSMichael Dalton 2621986a4f4dSJason Wang virtnet_del_vqs(vi); 262204486ed0SAmit Shah } 262304486ed0SAmit Shah 26248cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 262504486ed0SAmit Shah { 262604486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 262704486ed0SAmit Shah 26288017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 26298de4b2f3SWanlong Gao 2630102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 2631102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 2632586d17c5SJason Wang 263304486ed0SAmit Shah unregister_netdev(vi->dev); 263404486ed0SAmit Shah 263504486ed0SAmit Shah remove_vq_common(vi); 2636fb6813f4SRusty Russell 26372e66f55bSKrishna Kumar free_percpu(vi->stats); 263874b2553fSRusty Russell free_netdev(vi->dev); 2639296f96fcSRusty Russell } 2640296f96fcSRusty Russell 264189107000SAaron Lu #ifdef CONFIG_PM_SLEEP 26420741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev) 26430741bcb5SAmit Shah { 26440741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 26450741bcb5SAmit Shah 26468017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 26479fe7bfceSJohn Fastabend virtnet_freeze_down(vdev); 26480741bcb5SAmit Shah remove_vq_common(vi); 26490741bcb5SAmit Shah 26500741bcb5SAmit Shah return 0; 26510741bcb5SAmit Shah } 26520741bcb5SAmit Shah 26530741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev) 26540741bcb5SAmit Shah { 26550741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 26569fe7bfceSJohn Fastabend int err; 26570741bcb5SAmit Shah 26589fe7bfceSJohn Fastabend err = virtnet_restore_up(vdev); 26590741bcb5SAmit Shah if (err) 26600741bcb5SAmit Shah return err; 2661986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 2662986a4f4dSJason Wang 26638017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 2664ec9debbdSJason Wang if (err) 2665ec9debbdSJason Wang return err; 2666ec9debbdSJason Wang 26670741bcb5SAmit Shah return 0; 26680741bcb5SAmit Shah } 26690741bcb5SAmit Shah #endif 26700741bcb5SAmit Shah 2671296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 2672296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 2673296f96fcSRusty Russell { 0 }, 2674296f96fcSRusty Russell }; 2675296f96fcSRusty Russell 2676f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \ 2677f3358507SMichael S. Tsirkin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 2678f3358507SMichael S. Tsirkin VIRTIO_NET_F_MAC, \ 2679f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 2680f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 2681f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 2682f3358507SMichael S. Tsirkin VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 2683f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 2684f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 2685f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_MAC_ADDR, \ 2686f3358507SMichael S. Tsirkin VIRTIO_NET_F_MTU 2687f3358507SMichael S. Tsirkin 2688c45a6816SRusty Russell static unsigned int features[] = { 2689f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 2690f3358507SMichael S. Tsirkin }; 2691f3358507SMichael S. Tsirkin 2692f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = { 2693f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 2694f3358507SMichael S. Tsirkin VIRTIO_NET_F_GSO, 2695e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 2696c45a6816SRusty Russell }; 2697c45a6816SRusty Russell 269822402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 2699c45a6816SRusty Russell .feature_table = features, 2700c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 2701f3358507SMichael S. Tsirkin .feature_table_legacy = features_legacy, 2702f3358507SMichael S. Tsirkin .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 2703296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 2704296f96fcSRusty Russell .driver.owner = THIS_MODULE, 2705296f96fcSRusty Russell .id_table = id_table, 2706fe36cbe0SMichael S. Tsirkin .validate = virtnet_validate, 2707296f96fcSRusty Russell .probe = virtnet_probe, 27088cc085d6SBill Pemberton .remove = virtnet_remove, 27099f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 271089107000SAaron Lu #ifdef CONFIG_PM_SLEEP 27110741bcb5SAmit Shah .freeze = virtnet_freeze, 27120741bcb5SAmit Shah .restore = virtnet_restore, 27130741bcb5SAmit Shah #endif 2714296f96fcSRusty Russell }; 2715296f96fcSRusty Russell 27168017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void) 27178017c279SSebastian Andrzej Siewior { 27188017c279SSebastian Andrzej Siewior int ret; 27198017c279SSebastian Andrzej Siewior 272073c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 27218017c279SSebastian Andrzej Siewior virtnet_cpu_online, 27228017c279SSebastian Andrzej Siewior virtnet_cpu_down_prep); 27238017c279SSebastian Andrzej Siewior if (ret < 0) 27248017c279SSebastian Andrzej Siewior goto out; 27258017c279SSebastian Andrzej Siewior virtionet_online = ret; 272673c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 27278017c279SSebastian Andrzej Siewior NULL, virtnet_cpu_dead); 27288017c279SSebastian Andrzej Siewior if (ret) 27298017c279SSebastian Andrzej Siewior goto err_dead; 27308017c279SSebastian Andrzej Siewior 27318017c279SSebastian Andrzej Siewior ret = register_virtio_driver(&virtio_net_driver); 27328017c279SSebastian Andrzej Siewior if (ret) 27338017c279SSebastian Andrzej Siewior goto err_virtio; 27348017c279SSebastian Andrzej Siewior return 0; 27358017c279SSebastian Andrzej Siewior err_virtio: 27368017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 27378017c279SSebastian Andrzej Siewior err_dead: 27388017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 27398017c279SSebastian Andrzej Siewior out: 27408017c279SSebastian Andrzej Siewior return ret; 27418017c279SSebastian Andrzej Siewior } 27428017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init); 27438017c279SSebastian Andrzej Siewior 27448017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void) 27458017c279SSebastian Andrzej Siewior { 2746*cfa0ebc9SAndrew Jones unregister_virtio_driver(&virtio_net_driver); 27478017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 27488017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 27498017c279SSebastian Andrzej Siewior } 27508017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit); 2751296f96fcSRusty Russell 2752296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 2753296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 2754296f96fcSRusty Russell MODULE_LICENSE("GPL"); 2755