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 27328b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22 27428b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize, 27528b39bc7SJason Wang unsigned int headroom) 27628b39bc7SJason Wang { 27728b39bc7SJason Wang return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 27828b39bc7SJason Wang } 27928b39bc7SJason Wang 28028b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 28128b39bc7SJason Wang { 28228b39bc7SJason Wang return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 28328b39bc7SJason Wang } 28428b39bc7SJason Wang 28528b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 28628b39bc7SJason Wang { 28728b39bc7SJason Wang return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 28828b39bc7SJason Wang } 28928b39bc7SJason Wang 2903464645aSMike Waychison /* Called from bottom half context */ 291946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 292946fa564SMichael S. Tsirkin struct receive_queue *rq, 2932613af0eSMichael Dalton struct page *page, unsigned int offset, 2942613af0eSMichael Dalton unsigned int len, unsigned int truesize) 2959ab86bbcSShirley Ma { 2969ab86bbcSShirley Ma struct sk_buff *skb; 297012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 2982613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 2999ab86bbcSShirley Ma char *p; 3009ab86bbcSShirley Ma 3012613af0eSMichael Dalton p = page_address(page) + offset; 3029ab86bbcSShirley Ma 3039ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 304c67f5db8SPaolo Abeni skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 3059ab86bbcSShirley Ma if (unlikely(!skb)) 3069ab86bbcSShirley Ma return NULL; 3079ab86bbcSShirley Ma 3089ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 3099ab86bbcSShirley Ma 310012873d0SMichael S. Tsirkin hdr_len = vi->hdr_len; 311012873d0SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 312012873d0SMichael S. Tsirkin hdr_padded_len = sizeof *hdr; 313012873d0SMichael S. Tsirkin else 3142613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 3153f2c31d9SMark McLoughlin 3169ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 3173f2c31d9SMark McLoughlin 3189ab86bbcSShirley Ma len -= hdr_len; 3192613af0eSMichael Dalton offset += hdr_padded_len; 3202613af0eSMichael Dalton p += hdr_padded_len; 3213f2c31d9SMark McLoughlin 3223f2c31d9SMark McLoughlin copy = len; 3233f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 3243f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 32559ae1d12SJohannes Berg skb_put_data(skb, p, copy); 3263f2c31d9SMark McLoughlin 3273f2c31d9SMark McLoughlin len -= copy; 3289ab86bbcSShirley Ma offset += copy; 3293f2c31d9SMark McLoughlin 3302613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 3312613af0eSMichael Dalton if (len) 3322613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 3332613af0eSMichael Dalton else 3342613af0eSMichael Dalton put_page(page); 3352613af0eSMichael Dalton return skb; 3362613af0eSMichael Dalton } 3372613af0eSMichael Dalton 338e878d78bSSasha Levin /* 339e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 340e878d78bSSasha Levin * This is here to handle cases when the device erroneously 341e878d78bSSasha Levin * tries to receive more than is possible. This is usually 342e878d78bSSasha Levin * the case of a broken device. 343e878d78bSSasha Levin */ 344e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 345be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 346e878d78bSSasha Levin dev_kfree_skb(skb); 347e878d78bSSasha Levin return NULL; 348e878d78bSSasha Levin } 3492613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 3509ab86bbcSShirley Ma while (len) { 3512613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 3522613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 3532613af0eSMichael Dalton frag_size, truesize); 3542613af0eSMichael Dalton len -= frag_size; 3559ab86bbcSShirley Ma page = (struct page *)page->private; 3569ab86bbcSShirley Ma offset = 0; 3573f2c31d9SMark McLoughlin } 3583f2c31d9SMark McLoughlin 3599ab86bbcSShirley Ma if (page) 360e9d7417bSJason Wang give_pages(rq, page); 3613f2c31d9SMark McLoughlin 3629ab86bbcSShirley Ma return skb; 3639ab86bbcSShirley Ma } 3649ab86bbcSShirley Ma 365a67edbf4SDaniel Borkmann static bool virtnet_xdp_xmit(struct virtnet_info *vi, 36656434a01SJohn Fastabend struct receive_queue *rq, 367f6b10209SJason Wang struct xdp_buff *xdp) 36856434a01SJohn Fastabend { 36956434a01SJohn Fastabend struct virtio_net_hdr_mrg_rxbuf *hdr; 370f6b10209SJason Wang unsigned int len; 371722d8283SJohn Fastabend struct send_queue *sq; 372722d8283SJohn Fastabend unsigned int qp; 37356434a01SJohn Fastabend void *xdp_sent; 37456434a01SJohn Fastabend int err; 37556434a01SJohn Fastabend 376722d8283SJohn Fastabend qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); 377722d8283SJohn Fastabend sq = &vi->sq[qp]; 378722d8283SJohn Fastabend 37956434a01SJohn Fastabend /* Free up any pending old buffers before queueing new ones. */ 38056434a01SJohn Fastabend while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) { 38156434a01SJohn Fastabend struct page *sent_page = virt_to_head_page(xdp_sent); 382bb91accfSJason Wang 38356434a01SJohn Fastabend put_page(sent_page); 38456434a01SJohn Fastabend } 38556434a01SJohn Fastabend 386f6b10209SJason Wang xdp->data -= vi->hdr_len; 38756434a01SJohn Fastabend /* Zero header and leave csum up to XDP layers */ 38856434a01SJohn Fastabend hdr = xdp->data; 38956434a01SJohn Fastabend memset(hdr, 0, vi->hdr_len); 39056434a01SJohn Fastabend 39156434a01SJohn Fastabend sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data); 392bb91accfSJason Wang 393f6b10209SJason Wang err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC); 39456434a01SJohn Fastabend if (unlikely(err)) { 395bb91accfSJason Wang struct page *page = virt_to_head_page(xdp->data); 396bb91accfSJason Wang 39756434a01SJohn Fastabend put_page(page); 398a67edbf4SDaniel Borkmann return false; 39956434a01SJohn Fastabend } 40056434a01SJohn Fastabend 40156434a01SJohn Fastabend virtqueue_kick(sq->vq); 402a67edbf4SDaniel Borkmann return true; 40356434a01SJohn Fastabend } 40456434a01SJohn Fastabend 405f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 406f6b10209SJason Wang { 407f6b10209SJason Wang return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0; 408f6b10209SJason Wang } 409f6b10209SJason Wang 410*4941d472SJason Wang /* We copy the packet for XDP in the following cases: 411*4941d472SJason Wang * 412*4941d472SJason Wang * 1) Packet is scattered across multiple rx buffers. 413*4941d472SJason Wang * 2) Headroom space is insufficient. 414*4941d472SJason Wang * 415*4941d472SJason Wang * This is inefficient but it's a temporary condition that 416*4941d472SJason Wang * we hit right after XDP is enabled and until queue is refilled 417*4941d472SJason Wang * with large buffers with sufficient headroom - so it should affect 418*4941d472SJason Wang * at most queue size packets. 419*4941d472SJason Wang * Afterwards, the conditions to enable 420*4941d472SJason Wang * XDP should preclude the underlying device from sending packets 421*4941d472SJason Wang * across multiple buffers (num_buf > 1), and we make sure buffers 422*4941d472SJason Wang * have enough headroom. 42372979a6cSJohn Fastabend */ 42472979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq, 42556a86f84SJason Wang u16 *num_buf, 42672979a6cSJohn Fastabend struct page *p, 42772979a6cSJohn Fastabend int offset, 428*4941d472SJason Wang int page_off, 42972979a6cSJohn Fastabend unsigned int *len) 43072979a6cSJohn Fastabend { 43172979a6cSJohn Fastabend struct page *page = alloc_page(GFP_ATOMIC); 43272979a6cSJohn Fastabend 43372979a6cSJohn Fastabend if (!page) 43472979a6cSJohn Fastabend return NULL; 43572979a6cSJohn Fastabend 43672979a6cSJohn Fastabend memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 43772979a6cSJohn Fastabend page_off += *len; 43872979a6cSJohn Fastabend 43956a86f84SJason Wang while (--*num_buf) { 44072979a6cSJohn Fastabend unsigned int buflen; 44172979a6cSJohn Fastabend void *buf; 44272979a6cSJohn Fastabend int off; 44372979a6cSJohn Fastabend 444680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &buflen); 445680557cfSMichael S. Tsirkin if (unlikely(!buf)) 44672979a6cSJohn Fastabend goto err_buf; 44772979a6cSJohn Fastabend 44872979a6cSJohn Fastabend p = virt_to_head_page(buf); 44972979a6cSJohn Fastabend off = buf - page_address(p); 45072979a6cSJohn Fastabend 45156a86f84SJason Wang /* guard against a misconfigured or uncooperative backend that 45256a86f84SJason Wang * is sending packet larger than the MTU. 45356a86f84SJason Wang */ 45456a86f84SJason Wang if ((page_off + buflen) > PAGE_SIZE) { 45556a86f84SJason Wang put_page(p); 45656a86f84SJason Wang goto err_buf; 45756a86f84SJason Wang } 45856a86f84SJason Wang 45972979a6cSJohn Fastabend memcpy(page_address(page) + page_off, 46072979a6cSJohn Fastabend page_address(p) + off, buflen); 46172979a6cSJohn Fastabend page_off += buflen; 46256a86f84SJason Wang put_page(p); 46372979a6cSJohn Fastabend } 46472979a6cSJohn Fastabend 4652de2f7f4SJohn Fastabend /* Headroom does not contribute to packet length */ 4662de2f7f4SJohn Fastabend *len = page_off - VIRTIO_XDP_HEADROOM; 46772979a6cSJohn Fastabend return page; 46872979a6cSJohn Fastabend err_buf: 46972979a6cSJohn Fastabend __free_pages(page, 0); 47072979a6cSJohn Fastabend return NULL; 47172979a6cSJohn Fastabend } 47272979a6cSJohn Fastabend 473*4941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev, 474*4941d472SJason Wang struct virtnet_info *vi, 475*4941d472SJason Wang struct receive_queue *rq, 476*4941d472SJason Wang void *buf, void *ctx, 477*4941d472SJason Wang unsigned int len) 478*4941d472SJason Wang { 479*4941d472SJason Wang struct sk_buff *skb; 480*4941d472SJason Wang struct bpf_prog *xdp_prog; 481*4941d472SJason Wang unsigned int xdp_headroom = (unsigned long)ctx; 482*4941d472SJason Wang unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 483*4941d472SJason Wang unsigned int headroom = vi->hdr_len + header_offset; 484*4941d472SJason Wang unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 485*4941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 486*4941d472SJason Wang struct page *page = virt_to_head_page(buf); 487*4941d472SJason Wang unsigned int delta = 0; 488*4941d472SJason Wang struct page *xdp_page; 489*4941d472SJason Wang len -= vi->hdr_len; 490*4941d472SJason Wang 491*4941d472SJason Wang rcu_read_lock(); 492*4941d472SJason Wang xdp_prog = rcu_dereference(rq->xdp_prog); 493*4941d472SJason Wang if (xdp_prog) { 494*4941d472SJason Wang struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 495*4941d472SJason Wang struct xdp_buff xdp; 496*4941d472SJason Wang void *orig_data; 497*4941d472SJason Wang u32 act; 498*4941d472SJason Wang 499*4941d472SJason Wang if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags)) 500*4941d472SJason Wang goto err_xdp; 501*4941d472SJason Wang 502*4941d472SJason Wang if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 503*4941d472SJason Wang int offset = buf - page_address(page) + header_offset; 504*4941d472SJason Wang unsigned int tlen = len + vi->hdr_len; 505*4941d472SJason Wang u16 num_buf = 1; 506*4941d472SJason Wang 507*4941d472SJason Wang xdp_headroom = virtnet_get_headroom(vi); 508*4941d472SJason Wang header_offset = VIRTNET_RX_PAD + xdp_headroom; 509*4941d472SJason Wang headroom = vi->hdr_len + header_offset; 510*4941d472SJason Wang buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 511*4941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 512*4941d472SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, page, 513*4941d472SJason Wang offset, header_offset, 514*4941d472SJason Wang &tlen); 515*4941d472SJason Wang if (!xdp_page) 516*4941d472SJason Wang goto err_xdp; 517*4941d472SJason Wang 518*4941d472SJason Wang buf = page_address(xdp_page); 519*4941d472SJason Wang put_page(page); 520*4941d472SJason Wang page = xdp_page; 521*4941d472SJason Wang } 522*4941d472SJason Wang 523*4941d472SJason Wang xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len; 524*4941d472SJason Wang xdp.data = xdp.data_hard_start + xdp_headroom; 525*4941d472SJason Wang xdp.data_end = xdp.data + len; 526*4941d472SJason Wang orig_data = xdp.data; 527*4941d472SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 528*4941d472SJason Wang 529*4941d472SJason Wang switch (act) { 530*4941d472SJason Wang case XDP_PASS: 531*4941d472SJason Wang /* Recalculate length in case bpf program changed it */ 532*4941d472SJason Wang delta = orig_data - xdp.data; 533*4941d472SJason Wang break; 534*4941d472SJason Wang case XDP_TX: 535*4941d472SJason Wang if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp))) 536*4941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 537*4941d472SJason Wang rcu_read_unlock(); 538*4941d472SJason Wang goto xdp_xmit; 539*4941d472SJason Wang default: 540*4941d472SJason Wang bpf_warn_invalid_xdp_action(act); 541*4941d472SJason Wang case XDP_ABORTED: 542*4941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 543*4941d472SJason Wang case XDP_DROP: 544*4941d472SJason Wang goto err_xdp; 545*4941d472SJason Wang } 546*4941d472SJason Wang } 547*4941d472SJason Wang rcu_read_unlock(); 548*4941d472SJason Wang 549*4941d472SJason Wang skb = build_skb(buf, buflen); 550*4941d472SJason Wang if (!skb) { 551*4941d472SJason Wang put_page(page); 552*4941d472SJason Wang goto err; 553*4941d472SJason Wang } 554*4941d472SJason Wang skb_reserve(skb, headroom - delta); 555*4941d472SJason Wang skb_put(skb, len + delta); 556*4941d472SJason Wang if (!delta) { 557*4941d472SJason Wang buf += header_offset; 558*4941d472SJason Wang memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 559*4941d472SJason Wang } /* keep zeroed vnet hdr since packet was changed by bpf */ 560*4941d472SJason Wang 561*4941d472SJason Wang err: 562*4941d472SJason Wang return skb; 563*4941d472SJason Wang 564*4941d472SJason Wang err_xdp: 565*4941d472SJason Wang rcu_read_unlock(); 566*4941d472SJason Wang dev->stats.rx_dropped++; 567*4941d472SJason Wang put_page(page); 568*4941d472SJason Wang xdp_xmit: 569*4941d472SJason Wang return NULL; 570*4941d472SJason Wang } 571*4941d472SJason Wang 572*4941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev, 573*4941d472SJason Wang struct virtnet_info *vi, 574*4941d472SJason Wang struct receive_queue *rq, 575*4941d472SJason Wang void *buf, 576*4941d472SJason Wang unsigned int len) 577*4941d472SJason Wang { 578*4941d472SJason Wang struct page *page = buf; 579*4941d472SJason Wang struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE); 580*4941d472SJason Wang 581*4941d472SJason Wang if (unlikely(!skb)) 582*4941d472SJason Wang goto err; 583*4941d472SJason Wang 584*4941d472SJason Wang return skb; 585*4941d472SJason Wang 586*4941d472SJason Wang err: 587*4941d472SJason Wang dev->stats.rx_dropped++; 588*4941d472SJason Wang give_pages(rq, page); 589*4941d472SJason Wang return NULL; 590*4941d472SJason Wang } 591*4941d472SJason Wang 5928fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 593fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 5948fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 595680557cfSMichael S. Tsirkin void *buf, 596680557cfSMichael S. Tsirkin void *ctx, 5978fc3b9e9SMichael S. Tsirkin unsigned int len) 5989ab86bbcSShirley Ma { 599012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 600012873d0SMichael S. Tsirkin u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 6018fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 6028fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 603f600b690SJohn Fastabend struct sk_buff *head_skb, *curr_skb; 604f600b690SJohn Fastabend struct bpf_prog *xdp_prog; 605f600b690SJohn Fastabend unsigned int truesize; 606*4941d472SJason Wang unsigned int headroom = mergeable_ctx_to_headroom(ctx); 607ab7db917SMichael Dalton 60856434a01SJohn Fastabend head_skb = NULL; 60956434a01SJohn Fastabend 610f600b690SJohn Fastabend rcu_read_lock(); 611f600b690SJohn Fastabend xdp_prog = rcu_dereference(rq->xdp_prog); 612f600b690SJohn Fastabend if (xdp_prog) { 61372979a6cSJohn Fastabend struct page *xdp_page; 6140354e4d1SJohn Fastabend struct xdp_buff xdp; 6150354e4d1SJohn Fastabend void *data; 616f600b690SJohn Fastabend u32 act; 617f600b690SJohn Fastabend 61873b62bd0SJason Wang /* This happens when rx buffer size is underestimated */ 619*4941d472SJason Wang if (unlikely(num_buf > 1 || 620*4941d472SJason Wang headroom < virtnet_get_headroom(vi))) { 62172979a6cSJohn Fastabend /* linearize data for XDP */ 62256a86f84SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, 623*4941d472SJason Wang page, offset, 624*4941d472SJason Wang VIRTIO_XDP_HEADROOM, 625*4941d472SJason Wang &len); 62672979a6cSJohn Fastabend if (!xdp_page) 627f600b690SJohn Fastabend goto err_xdp; 6282de2f7f4SJohn Fastabend offset = VIRTIO_XDP_HEADROOM; 62972979a6cSJohn Fastabend } else { 63072979a6cSJohn Fastabend xdp_page = page; 631f600b690SJohn Fastabend } 632f600b690SJohn Fastabend 633f600b690SJohn Fastabend /* Transient failure which in theory could occur if 634f600b690SJohn Fastabend * in-flight packets from before XDP was enabled reach 635f600b690SJohn Fastabend * the receive path after XDP is loaded. In practice I 636f600b690SJohn Fastabend * was not able to create this condition. 637f600b690SJohn Fastabend */ 638b00f70b0SJason Wang if (unlikely(hdr->hdr.gso_type)) 639f600b690SJohn Fastabend goto err_xdp; 640f600b690SJohn Fastabend 6412de2f7f4SJohn Fastabend /* Allow consuming headroom but reserve enough space to push 6422de2f7f4SJohn Fastabend * the descriptor on if we get an XDP_TX return code. 6432de2f7f4SJohn Fastabend */ 6440354e4d1SJohn Fastabend data = page_address(xdp_page) + offset; 6452de2f7f4SJohn Fastabend xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len; 6460354e4d1SJohn Fastabend xdp.data = data + vi->hdr_len; 6470354e4d1SJohn Fastabend xdp.data_end = xdp.data + (len - vi->hdr_len); 6480354e4d1SJohn Fastabend act = bpf_prog_run_xdp(xdp_prog, &xdp); 6490354e4d1SJohn Fastabend 65056434a01SJohn Fastabend switch (act) { 65156434a01SJohn Fastabend case XDP_PASS: 6522de2f7f4SJohn Fastabend /* recalculate offset to account for any header 6532de2f7f4SJohn Fastabend * adjustments. Note other cases do not build an 6542de2f7f4SJohn Fastabend * skb and avoid using offset 6552de2f7f4SJohn Fastabend */ 6562de2f7f4SJohn Fastabend offset = xdp.data - 6572de2f7f4SJohn Fastabend page_address(xdp_page) - vi->hdr_len; 6582de2f7f4SJohn Fastabend 6591830f893SJason Wang /* We can only create skb based on xdp_page. */ 6601830f893SJason Wang if (unlikely(xdp_page != page)) { 6611830f893SJason Wang rcu_read_unlock(); 6621830f893SJason Wang put_page(page); 6631830f893SJason Wang head_skb = page_to_skb(vi, rq, xdp_page, 6642de2f7f4SJohn Fastabend offset, len, PAGE_SIZE); 6655c33474dSJason Wang ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); 6661830f893SJason Wang return head_skb; 6671830f893SJason Wang } 66856434a01SJohn Fastabend break; 66956434a01SJohn Fastabend case XDP_TX: 670f6b10209SJason Wang if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp))) 6710354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 6725c33474dSJason Wang ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); 67372979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 67472979a6cSJohn Fastabend goto err_xdp; 67556434a01SJohn Fastabend rcu_read_unlock(); 67656434a01SJohn Fastabend goto xdp_xmit; 67756434a01SJohn Fastabend default: 6780354e4d1SJohn Fastabend bpf_warn_invalid_xdp_action(act); 6790354e4d1SJohn Fastabend case XDP_ABORTED: 6800354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 6810354e4d1SJohn Fastabend case XDP_DROP: 68272979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 68372979a6cSJohn Fastabend __free_pages(xdp_page, 0); 6845c33474dSJason Wang ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); 685f600b690SJohn Fastabend goto err_xdp; 686f600b690SJohn Fastabend } 68756434a01SJohn Fastabend } 688f600b690SJohn Fastabend rcu_read_unlock(); 689f600b690SJohn Fastabend 69028b39bc7SJason Wang truesize = mergeable_ctx_to_truesize(ctx); 69128b39bc7SJason Wang if (unlikely(len > truesize)) { 69256da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 693680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 694680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 695680557cfSMichael S. Tsirkin goto err_skb; 696680557cfSMichael S. Tsirkin } 69728b39bc7SJason Wang 698f600b690SJohn Fastabend head_skb = page_to_skb(vi, rq, page, offset, len, truesize); 699f600b690SJohn Fastabend curr_skb = head_skb; 7009ab86bbcSShirley Ma 7018fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 7028fc3b9e9SMichael S. Tsirkin goto err_skb; 7039ab86bbcSShirley Ma while (--num_buf) { 7048fc3b9e9SMichael S. Tsirkin int num_skb_frags; 7058fc3b9e9SMichael S. Tsirkin 706680557cfSMichael S. Tsirkin buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 707ab7db917SMichael Dalton if (unlikely(!ctx)) { 7088fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 709fdd819b2SMichael S. Tsirkin dev->name, num_buf, 710012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 711012873d0SMichael S. Tsirkin hdr->num_buffers)); 7128fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 7138fc3b9e9SMichael S. Tsirkin goto err_buf; 7143f2c31d9SMark McLoughlin } 7158fc3b9e9SMichael S. Tsirkin 7168fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 71728b39bc7SJason Wang 71828b39bc7SJason Wang truesize = mergeable_ctx_to_truesize(ctx); 71928b39bc7SJason Wang if (unlikely(len > truesize)) { 72056da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 721680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 722680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 723680557cfSMichael S. Tsirkin goto err_skb; 724680557cfSMichael S. Tsirkin } 7258fc3b9e9SMichael S. Tsirkin 7268fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 7272613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 7282613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 7298fc3b9e9SMichael S. Tsirkin 7308fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 7318fc3b9e9SMichael S. Tsirkin goto err_skb; 7322613af0eSMichael Dalton if (curr_skb == head_skb) 7332613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 7342613af0eSMichael Dalton else 7352613af0eSMichael Dalton curr_skb->next = nskb; 7362613af0eSMichael Dalton curr_skb = nskb; 7372613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 7382613af0eSMichael Dalton num_skb_frags = 0; 7392613af0eSMichael Dalton } 7402613af0eSMichael Dalton if (curr_skb != head_skb) { 7412613af0eSMichael Dalton head_skb->data_len += len; 7422613af0eSMichael Dalton head_skb->len += len; 743fb51879dSMichael Dalton head_skb->truesize += truesize; 7442613af0eSMichael Dalton } 7458fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 746ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 747ba275241SJason Wang put_page(page); 748ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 749fb51879dSMichael Dalton len, truesize); 750ba275241SJason Wang } else { 7512613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 752fb51879dSMichael Dalton offset, len, truesize); 753ba275241SJason Wang } 7548fc3b9e9SMichael S. Tsirkin } 7558fc3b9e9SMichael S. Tsirkin 7565377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 7578fc3b9e9SMichael S. Tsirkin return head_skb; 7588fc3b9e9SMichael S. Tsirkin 759f600b690SJohn Fastabend err_xdp: 760f600b690SJohn Fastabend rcu_read_unlock(); 7618fc3b9e9SMichael S. Tsirkin err_skb: 7628fc3b9e9SMichael S. Tsirkin put_page(page); 7638fc3b9e9SMichael S. Tsirkin while (--num_buf) { 764680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &len); 765680557cfSMichael S. Tsirkin if (unlikely(!buf)) { 7668fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 7678fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 7688fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 7698fc3b9e9SMichael S. Tsirkin break; 7708fc3b9e9SMichael S. Tsirkin } 771680557cfSMichael S. Tsirkin page = virt_to_head_page(buf); 7728fc3b9e9SMichael S. Tsirkin put_page(page); 7733f2c31d9SMark McLoughlin } 7748fc3b9e9SMichael S. Tsirkin err_buf: 7758fc3b9e9SMichael S. Tsirkin dev->stats.rx_dropped++; 7768fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 77756434a01SJohn Fastabend xdp_xmit: 7788fc3b9e9SMichael S. Tsirkin return NULL; 7799ab86bbcSShirley Ma } 7809ab86bbcSShirley Ma 78161845d20SJason Wang static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 782680557cfSMichael S. Tsirkin void *buf, unsigned int len, void **ctx) 7839ab86bbcSShirley Ma { 784e9d7417bSJason Wang struct net_device *dev = vi->dev; 7859ab86bbcSShirley Ma struct sk_buff *skb; 786012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 78761845d20SJason Wang int ret; 7889ab86bbcSShirley Ma 789bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 7909ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 7919ab86bbcSShirley Ma dev->stats.rx_length_errors++; 792ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 793680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 794ab7db917SMichael Dalton } else if (vi->big_packets) { 79598bfd23cSMichael Dalton give_pages(rq, buf); 796ab7db917SMichael Dalton } else { 797f6b10209SJason Wang put_page(virt_to_head_page(buf)); 798ab7db917SMichael Dalton } 79961845d20SJason Wang return 0; 8009ab86bbcSShirley Ma } 8019ab86bbcSShirley Ma 802f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 803680557cfSMichael S. Tsirkin skb = receive_mergeable(dev, vi, rq, buf, ctx, len); 804f121159dSMichael S. Tsirkin else if (vi->big_packets) 805946fa564SMichael S. Tsirkin skb = receive_big(dev, vi, rq, buf, len); 806f121159dSMichael S. Tsirkin else 807192f68cfSJason Wang skb = receive_small(dev, vi, rq, buf, ctx, len); 808f121159dSMichael S. Tsirkin 8098fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 81061845d20SJason Wang return 0; 8113f2c31d9SMark McLoughlin 8129ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 8133fa2a1dfSstephen hemminger 81461845d20SJason Wang ret = skb->len; 815296f96fcSRusty Russell 816e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 81710a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 818296f96fcSRusty Russell 819e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 820e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 821e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 822e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 823fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 824296f96fcSRusty Russell goto frame_err; 825296f96fcSRusty Russell } 826296f96fcSRusty Russell 827d1dc06dcSMike Rapoport skb->protocol = eth_type_trans(skb, dev); 828d1dc06dcSMike Rapoport pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 829d1dc06dcSMike Rapoport ntohs(skb->protocol), skb->len, skb->pkt_type); 830d1dc06dcSMike Rapoport 8310fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 83261845d20SJason Wang return ret; 833296f96fcSRusty Russell 834296f96fcSRusty Russell frame_err: 835296f96fcSRusty Russell dev->stats.rx_frame_errors++; 836296f96fcSRusty Russell dev_kfree_skb(skb); 83761845d20SJason Wang return 0; 838296f96fcSRusty Russell } 839296f96fcSRusty Russell 840192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the 841192f68cfSJason Wang * same size, except for the headroom. For this reason we do 842192f68cfSJason Wang * not need to use mergeable_len_to_ctx here - it is enough 843192f68cfSJason Wang * to store the headroom as the context ignoring the truesize. 844192f68cfSJason Wang */ 845946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 846946fa564SMichael S. Tsirkin gfp_t gfp) 847296f96fcSRusty Russell { 848f6b10209SJason Wang struct page_frag *alloc_frag = &rq->alloc_frag; 849f6b10209SJason Wang char *buf; 8502de2f7f4SJohn Fastabend unsigned int xdp_headroom = virtnet_get_headroom(vi); 851192f68cfSJason Wang void *ctx = (void *)(unsigned long)xdp_headroom; 852f6b10209SJason Wang int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 8539ab86bbcSShirley Ma int err; 8543f2c31d9SMark McLoughlin 855f6b10209SJason Wang len = SKB_DATA_ALIGN(len) + 856f6b10209SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 857f6b10209SJason Wang if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 8589ab86bbcSShirley Ma return -ENOMEM; 859296f96fcSRusty Russell 860f6b10209SJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 861f6b10209SJason Wang get_page(alloc_frag->page); 862f6b10209SJason Wang alloc_frag->offset += len; 863f6b10209SJason Wang sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 864f6b10209SJason Wang vi->hdr_len + GOOD_PACKET_LEN); 865192f68cfSJason Wang err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 8669ab86bbcSShirley Ma if (err < 0) 867f6b10209SJason Wang put_page(virt_to_head_page(buf)); 8689ab86bbcSShirley Ma return err; 86997402b96SHerbert Xu } 87097402b96SHerbert Xu 871012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 872012873d0SMichael S. Tsirkin gfp_t gfp) 8739ab86bbcSShirley Ma { 8749ab86bbcSShirley Ma struct page *first, *list = NULL; 8759ab86bbcSShirley Ma char *p; 8769ab86bbcSShirley Ma int i, err, offset; 877296f96fcSRusty Russell 878a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 879a5835440SRusty Russell 880e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 8819ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 882e9d7417bSJason Wang first = get_a_page(rq, gfp); 8839ab86bbcSShirley Ma if (!first) { 8849ab86bbcSShirley Ma if (list) 885e9d7417bSJason Wang give_pages(rq, list); 8869ab86bbcSShirley Ma return -ENOMEM; 887296f96fcSRusty Russell } 888e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 8899ab86bbcSShirley Ma 8909ab86bbcSShirley Ma /* chain new page in list head to match sg */ 8919ab86bbcSShirley Ma first->private = (unsigned long)list; 8929ab86bbcSShirley Ma list = first; 8939ab86bbcSShirley Ma } 8949ab86bbcSShirley Ma 895e9d7417bSJason Wang first = get_a_page(rq, gfp); 8969ab86bbcSShirley Ma if (!first) { 897e9d7417bSJason Wang give_pages(rq, list); 8989ab86bbcSShirley Ma return -ENOMEM; 8999ab86bbcSShirley Ma } 9009ab86bbcSShirley Ma p = page_address(first); 9019ab86bbcSShirley Ma 902e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 903012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 904012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 9059ab86bbcSShirley Ma 906e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 9079ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 908e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 9099ab86bbcSShirley Ma 9109ab86bbcSShirley Ma /* chain first in list head */ 9119ab86bbcSShirley Ma first->private = (unsigned long)list; 9129dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 913aa989f5eSMichael S. Tsirkin first, gfp); 9149ab86bbcSShirley Ma if (err < 0) 915e9d7417bSJason Wang give_pages(rq, first); 9169ab86bbcSShirley Ma 9179ab86bbcSShirley Ma return err; 9189ab86bbcSShirley Ma } 9199ab86bbcSShirley Ma 920d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 921d85b758fSMichael S. Tsirkin struct ewma_pkt_len *avg_pkt_len) 9229ab86bbcSShirley Ma { 923ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 924fbf28d78SMichael Dalton unsigned int len; 925fbf28d78SMichael Dalton 9265377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 927f0c3192cSMichael S. Tsirkin rq->min_buf_len, PAGE_SIZE - hdr_len); 928e377fcc8SMichael S. Tsirkin return ALIGN(len, L1_CACHE_BYTES); 929fbf28d78SMichael Dalton } 930fbf28d78SMichael Dalton 9312de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi, 9322de2f7f4SJohn Fastabend struct receive_queue *rq, gfp_t gfp) 933fbf28d78SMichael Dalton { 934fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 9352de2f7f4SJohn Fastabend unsigned int headroom = virtnet_get_headroom(vi); 936fb51879dSMichael Dalton char *buf; 937680557cfSMichael S. Tsirkin void *ctx; 9389ab86bbcSShirley Ma int err; 939fb51879dSMichael Dalton unsigned int len, hole; 9409ab86bbcSShirley Ma 941d85b758fSMichael S. Tsirkin len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len); 9422de2f7f4SJohn Fastabend if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp))) 9439ab86bbcSShirley Ma return -ENOMEM; 944ab7db917SMichael Dalton 945fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 9462de2f7f4SJohn Fastabend buf += headroom; /* advance address leaving hole at front of pkt */ 94728b39bc7SJason Wang ctx = mergeable_len_to_ctx(len, headroom); 948fb51879dSMichael Dalton get_page(alloc_frag->page); 9492de2f7f4SJohn Fastabend alloc_frag->offset += len + headroom; 950fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 9512de2f7f4SJohn Fastabend if (hole < len + headroom) { 952ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 953ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 954ab7db917SMichael Dalton * the current buffer. This extra space is not included in 955ab7db917SMichael Dalton * the truesize stored in ctx. 956ab7db917SMichael Dalton */ 957fb51879dSMichael Dalton len += hole; 958fb51879dSMichael Dalton alloc_frag->offset += hole; 959fb51879dSMichael Dalton } 9609ab86bbcSShirley Ma 961fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 962680557cfSMichael S. Tsirkin err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 9639ab86bbcSShirley Ma if (err < 0) 9642613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 9659ab86bbcSShirley Ma 9669ab86bbcSShirley Ma return err; 967296f96fcSRusty Russell } 968296f96fcSRusty Russell 969b2baed69SRusty Russell /* 970b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 971b2baed69SRusty Russell * 972b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 973b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 974b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 975b2baed69SRusty Russell */ 976946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 977946fa564SMichael S. Tsirkin gfp_t gfp) 9783f2c31d9SMark McLoughlin { 9793f2c31d9SMark McLoughlin int err; 9801788f495SMichael S. Tsirkin bool oom; 9813f2c31d9SMark McLoughlin 982fb51879dSMichael Dalton gfp |= __GFP_COLD; 9830aea51c3SAmit Shah do { 9849ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 9852de2f7f4SJohn Fastabend err = add_recvbuf_mergeable(vi, rq, gfp); 9869ab86bbcSShirley Ma else if (vi->big_packets) 987012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 9889ab86bbcSShirley Ma else 989946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 9903f2c31d9SMark McLoughlin 9911788f495SMichael S. Tsirkin oom = err == -ENOMEM; 9929ed4cb07SRusty Russell if (err) 9933f2c31d9SMark McLoughlin break; 994b7dfde95SLinus Torvalds } while (rq->vq->num_free); 995681daee2SJason Wang virtqueue_kick(rq->vq); 9963161e453SRusty Russell return !oom; 9973f2c31d9SMark McLoughlin } 9983f2c31d9SMark McLoughlin 99918445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 1000296f96fcSRusty Russell { 1001296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 1002986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1003e9d7417bSJason Wang 1004e4e8452aSWillem de Bruijn virtqueue_napi_schedule(&rq->napi, rvq); 1005296f96fcSRusty Russell } 1006296f96fcSRusty Russell 1007e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 10083e9d08ecSBruce Rogers { 1009e4e8452aSWillem de Bruijn napi_enable(napi); 10103e9d08ecSBruce Rogers 10113e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 1012e4e8452aSWillem de Bruijn * won't get another interrupt, so process any outstanding packets now. 1013e4e8452aSWillem de Bruijn * Call local_bh_enable after to trigger softIRQ processing. 1014e4e8452aSWillem de Bruijn */ 1015ec13ee80SMichael S. Tsirkin local_bh_disable(); 1016e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 1017ec13ee80SMichael S. Tsirkin local_bh_enable(); 10183e9d08ecSBruce Rogers } 10193e9d08ecSBruce Rogers 1020b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1021b92f1e67SWillem de Bruijn struct virtqueue *vq, 1022b92f1e67SWillem de Bruijn struct napi_struct *napi) 1023b92f1e67SWillem de Bruijn { 1024b92f1e67SWillem de Bruijn if (!napi->weight) 1025b92f1e67SWillem de Bruijn return; 1026b92f1e67SWillem de Bruijn 1027b92f1e67SWillem de Bruijn /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1028b92f1e67SWillem de Bruijn * enable the feature if this is likely affine with the transmit path. 1029b92f1e67SWillem de Bruijn */ 1030b92f1e67SWillem de Bruijn if (!vi->affinity_hint_set) { 1031b92f1e67SWillem de Bruijn napi->weight = 0; 1032b92f1e67SWillem de Bruijn return; 1033b92f1e67SWillem de Bruijn } 1034b92f1e67SWillem de Bruijn 1035b92f1e67SWillem de Bruijn return virtnet_napi_enable(vq, napi); 1036b92f1e67SWillem de Bruijn } 1037b92f1e67SWillem de Bruijn 103878a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi) 103978a57b48SWillem de Bruijn { 104078a57b48SWillem de Bruijn if (napi->weight) 104178a57b48SWillem de Bruijn napi_disable(napi); 104278a57b48SWillem de Bruijn } 104378a57b48SWillem de Bruijn 10443161e453SRusty Russell static void refill_work(struct work_struct *work) 10453161e453SRusty Russell { 1046e9d7417bSJason Wang struct virtnet_info *vi = 1047e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 10483161e453SRusty Russell bool still_empty; 1049986a4f4dSJason Wang int i; 10503161e453SRusty Russell 105155257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1052986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 1053986a4f4dSJason Wang 1054986a4f4dSJason Wang napi_disable(&rq->napi); 1055946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1056e4e8452aSWillem de Bruijn virtnet_napi_enable(rq->vq, &rq->napi); 10573161e453SRusty Russell 10583161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 1059986a4f4dSJason Wang * we will *never* try to fill again. 1060986a4f4dSJason Wang */ 10613161e453SRusty Russell if (still_empty) 10623b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 10633161e453SRusty Russell } 1064986a4f4dSJason Wang } 10653161e453SRusty Russell 10662ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget) 1067296f96fcSRusty Russell { 1068e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 106961845d20SJason Wang unsigned int len, received = 0, bytes = 0; 10709ab86bbcSShirley Ma void *buf; 107161845d20SJason Wang struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 1072296f96fcSRusty Russell 1073192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 1074680557cfSMichael S. Tsirkin void *ctx; 1075680557cfSMichael S. Tsirkin 1076680557cfSMichael S. Tsirkin while (received < budget && 1077680557cfSMichael S. Tsirkin (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1078680557cfSMichael S. Tsirkin bytes += receive_buf(vi, rq, buf, len, ctx); 1079680557cfSMichael S. Tsirkin received++; 1080680557cfSMichael S. Tsirkin } 1081680557cfSMichael S. Tsirkin } else { 1082296f96fcSRusty Russell while (received < budget && 1083e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1084680557cfSMichael S. Tsirkin bytes += receive_buf(vi, rq, buf, len, NULL); 1085296f96fcSRusty Russell received++; 1086296f96fcSRusty Russell } 1087680557cfSMichael S. Tsirkin } 1088296f96fcSRusty Russell 1089be121f46SJason Wang if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 1090946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 10913b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 10923161e453SRusty Russell } 1093296f96fcSRusty Russell 109461845d20SJason Wang u64_stats_update_begin(&stats->rx_syncp); 109561845d20SJason Wang stats->rx_bytes += bytes; 109661845d20SJason Wang stats->rx_packets += received; 109761845d20SJason Wang u64_stats_update_end(&stats->rx_syncp); 109861845d20SJason Wang 10992ffa7598SJason Wang return received; 11002ffa7598SJason Wang } 11012ffa7598SJason Wang 1102ea7735d9SWillem de Bruijn static void free_old_xmit_skbs(struct send_queue *sq) 1103ea7735d9SWillem de Bruijn { 1104ea7735d9SWillem de Bruijn struct sk_buff *skb; 1105ea7735d9SWillem de Bruijn unsigned int len; 1106ea7735d9SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1107ea7735d9SWillem de Bruijn struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 1108ea7735d9SWillem de Bruijn unsigned int packets = 0; 1109ea7735d9SWillem de Bruijn unsigned int bytes = 0; 1110ea7735d9SWillem de Bruijn 1111ea7735d9SWillem de Bruijn while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 1112ea7735d9SWillem de Bruijn pr_debug("Sent skb %p\n", skb); 1113ea7735d9SWillem de Bruijn 1114ea7735d9SWillem de Bruijn bytes += skb->len; 1115ea7735d9SWillem de Bruijn packets++; 1116ea7735d9SWillem de Bruijn 1117ea7735d9SWillem de Bruijn dev_kfree_skb_any(skb); 1118ea7735d9SWillem de Bruijn } 1119ea7735d9SWillem de Bruijn 1120ea7735d9SWillem de Bruijn /* Avoid overhead when no packets have been processed 1121ea7735d9SWillem de Bruijn * happens when called speculatively from start_xmit. 1122ea7735d9SWillem de Bruijn */ 1123ea7735d9SWillem de Bruijn if (!packets) 1124ea7735d9SWillem de Bruijn return; 1125ea7735d9SWillem de Bruijn 1126ea7735d9SWillem de Bruijn u64_stats_update_begin(&stats->tx_syncp); 1127ea7735d9SWillem de Bruijn stats->tx_bytes += bytes; 1128ea7735d9SWillem de Bruijn stats->tx_packets += packets; 1129ea7735d9SWillem de Bruijn u64_stats_update_end(&stats->tx_syncp); 1130ea7735d9SWillem de Bruijn } 1131ea7735d9SWillem de Bruijn 11327b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq) 11337b0411efSWillem de Bruijn { 11347b0411efSWillem de Bruijn struct virtnet_info *vi = rq->vq->vdev->priv; 11357b0411efSWillem de Bruijn unsigned int index = vq2rxq(rq->vq); 11367b0411efSWillem de Bruijn struct send_queue *sq = &vi->sq[index]; 11377b0411efSWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 11387b0411efSWillem de Bruijn 11397b0411efSWillem de Bruijn if (!sq->napi.weight) 11407b0411efSWillem de Bruijn return; 11417b0411efSWillem de Bruijn 11427b0411efSWillem de Bruijn if (__netif_tx_trylock(txq)) { 11437b0411efSWillem de Bruijn free_old_xmit_skbs(sq); 11447b0411efSWillem de Bruijn __netif_tx_unlock(txq); 11457b0411efSWillem de Bruijn } 11467b0411efSWillem de Bruijn 11477b0411efSWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 11487b0411efSWillem de Bruijn netif_tx_wake_queue(txq); 11497b0411efSWillem de Bruijn } 11507b0411efSWillem de Bruijn 11512ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 11522ffa7598SJason Wang { 11532ffa7598SJason Wang struct receive_queue *rq = 11542ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 1155e4e8452aSWillem de Bruijn unsigned int received; 11562ffa7598SJason Wang 11577b0411efSWillem de Bruijn virtnet_poll_cleantx(rq); 11587b0411efSWillem de Bruijn 1159faadb05fSLi RongQing received = virtnet_receive(rq, budget); 11602ffa7598SJason Wang 11618329d98eSRusty Russell /* Out of packets? */ 1162e4e8452aSWillem de Bruijn if (received < budget) 1163e4e8452aSWillem de Bruijn virtqueue_napi_complete(napi, rq->vq, received); 1164296f96fcSRusty Russell 1165296f96fcSRusty Russell return received; 1166296f96fcSRusty Russell } 1167296f96fcSRusty Russell 1168986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 1169986a4f4dSJason Wang { 1170986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1171986a4f4dSJason Wang int i; 1172986a4f4dSJason Wang 1173e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1174e4166625SJason Wang if (i < vi->curr_queue_pairs) 1175986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 1176946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1177986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 1178e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1179b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1180986a4f4dSJason Wang } 1181986a4f4dSJason Wang 1182986a4f4dSJason Wang return 0; 1183986a4f4dSJason Wang } 1184986a4f4dSJason Wang 1185b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1186b92f1e67SWillem de Bruijn { 1187b92f1e67SWillem de Bruijn struct send_queue *sq = container_of(napi, struct send_queue, napi); 1188b92f1e67SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1189b92f1e67SWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq)); 1190b92f1e67SWillem de Bruijn 1191b92f1e67SWillem de Bruijn __netif_tx_lock(txq, raw_smp_processor_id()); 1192b92f1e67SWillem de Bruijn free_old_xmit_skbs(sq); 1193b92f1e67SWillem de Bruijn __netif_tx_unlock(txq); 1194b92f1e67SWillem de Bruijn 1195b92f1e67SWillem de Bruijn virtqueue_napi_complete(napi, sq->vq, 0); 1196b92f1e67SWillem de Bruijn 1197b92f1e67SWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1198b92f1e67SWillem de Bruijn netif_tx_wake_queue(txq); 1199b92f1e67SWillem de Bruijn 1200b92f1e67SWillem de Bruijn return 0; 1201b92f1e67SWillem de Bruijn } 1202b92f1e67SWillem de Bruijn 1203e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1204296f96fcSRusty Russell { 1205012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 1206296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1207e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 1208e2fcad58SJason A. Donenfeld int num_sg; 1209012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 1210e7428e95SMichael S. Tsirkin bool can_push; 1211296f96fcSRusty Russell 1212e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1213e7428e95SMichael S. Tsirkin 1214e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 1215e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1216e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1217e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 1218e7428e95SMichael S. Tsirkin * csum_start offset below. */ 1219e7428e95SMichael S. Tsirkin if (can_push) 1220012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1221e7428e95SMichael S. Tsirkin else 1222e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 1223296f96fcSRusty Russell 1224e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 12256391a448SJason Wang virtio_is_little_endian(vi->vdev), false)) 1226296f96fcSRusty Russell BUG(); 1227296f96fcSRusty Russell 1228e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 1229012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 12303f2c31d9SMark McLoughlin 1231547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1232e7428e95SMichael S. Tsirkin if (can_push) { 1233e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 1234e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1235e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1236e2fcad58SJason A. Donenfeld return num_sg; 1237e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 1238e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 1239e7428e95SMichael S. Tsirkin } else { 1240e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 1241e2fcad58SJason A. Donenfeld num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1242e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1243e2fcad58SJason A. Donenfeld return num_sg; 1244e2fcad58SJason A. Donenfeld num_sg++; 1245e7428e95SMichael S. Tsirkin } 12469dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 124711a3a154SRusty Russell } 124811a3a154SRusty Russell 1249424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 125099ffc696SRusty Russell { 125199ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1252986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 1253986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 12549ed4cb07SRusty Russell int err; 12554b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 12564b7fd2e6SMichael S. Tsirkin bool kick = !skb->xmit_more; 1257b92f1e67SWillem de Bruijn bool use_napi = sq->napi.weight; 12582cb9c6baSRusty Russell 12592cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 1260e9d7417bSJason Wang free_old_xmit_skbs(sq); 126199ffc696SRusty Russell 1262bdb12e0dSWillem de Bruijn if (use_napi && kick) 1263bdb12e0dSWillem de Bruijn virtqueue_enable_cb_delayed(sq->vq); 1264bdb12e0dSWillem de Bruijn 1265074c3582SJacob Keller /* timestamp packet in software */ 1266074c3582SJacob Keller skb_tx_timestamp(skb); 1267074c3582SJacob Keller 126803f191baSMichael S. Tsirkin /* Try to transmit */ 1269b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 127099ffc696SRusty Russell 12719ed4cb07SRusty Russell /* This should not happen! */ 1272681daee2SJason Wang if (unlikely(err)) { 127358eba97dSRusty Russell dev->stats.tx_fifo_errors++; 12742e57b79cSRick Jones if (net_ratelimit()) 127558eba97dSRusty Russell dev_warn(&dev->dev, 1276b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 127758eba97dSRusty Russell dev->stats.tx_dropped++; 127885e94525SEric W. Biederman dev_kfree_skb_any(skb); 127958eba97dSRusty Russell return NETDEV_TX_OK; 1280296f96fcSRusty Russell } 128103f191baSMichael S. Tsirkin 128248925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 1283b92f1e67SWillem de Bruijn if (!use_napi) { 128448925e37SRusty Russell skb_orphan(skb); 128548925e37SRusty Russell nf_reset(skb); 1286b92f1e67SWillem de Bruijn } 128748925e37SRusty Russell 128860302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 128960302ff6SMichael S. Tsirkin * are then unable to transmit. 129060302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 129160302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 129260302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 129360302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 129460302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 129560302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 129660302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 1297d631b94eSstephen hemminger */ 1298b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1299986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 1300b92f1e67SWillem de Bruijn if (!use_napi && 1301b92f1e67SWillem de Bruijn unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 130248925e37SRusty Russell /* More just got used, free them then recheck. */ 1303b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 1304b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1305986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 1306e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 130748925e37SRusty Russell } 130848925e37SRusty Russell } 130948925e37SRusty Russell } 131048925e37SRusty Russell 13114b7fd2e6SMichael S. Tsirkin if (kick || netif_xmit_stopped(txq)) 1312c223a078SDavid S. Miller virtqueue_kick(sq->vq); 13130b725a2cSDavid S. Miller 13140b725a2cSDavid S. Miller return NETDEV_TX_OK; 1315c223a078SDavid S. Miller } 1316c223a078SDavid S. Miller 131740cbfc37SAmos Kong /* 131840cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 131940cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 1320788a8b6dSstephen hemminger * never fail unless improperly formatted. 132140cbfc37SAmos Kong */ 132240cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 1323d24bae32Sstephen hemminger struct scatterlist *out) 132440cbfc37SAmos Kong { 1325f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 1326d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 132740cbfc37SAmos Kong 132840cbfc37SAmos Kong /* Caller should know better */ 1329f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 133040cbfc37SAmos Kong 13312ac46030SMichael S. Tsirkin vi->ctrl_status = ~0; 13322ac46030SMichael S. Tsirkin vi->ctrl_hdr.class = class; 13332ac46030SMichael S. Tsirkin vi->ctrl_hdr.cmd = cmd; 1334f7bc9594SRusty Russell /* Add header */ 13352ac46030SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr)); 1336f7bc9594SRusty Russell sgs[out_num++] = &hdr; 133740cbfc37SAmos Kong 1338f7bc9594SRusty Russell if (out) 1339f7bc9594SRusty Russell sgs[out_num++] = out; 134040cbfc37SAmos Kong 1341f7bc9594SRusty Russell /* Add return status. */ 13422ac46030SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status)); 1343d24bae32Sstephen hemminger sgs[out_num] = &stat; 134440cbfc37SAmos Kong 1345d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1346a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 134740cbfc37SAmos Kong 134867975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 13492ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 135040cbfc37SAmos Kong 135140cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 135240cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 135340cbfc37SAmos Kong */ 1354047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 1355047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 135640cbfc37SAmos Kong cpu_relax(); 135740cbfc37SAmos Kong 13582ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 135940cbfc37SAmos Kong } 136040cbfc37SAmos Kong 13619c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 13629c46f6d4SAlex Williamson { 13639c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 13649c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 1365f2f2c8b4SJiri Pirko int ret; 1366e37e2ff3SAndy Lutomirski struct sockaddr *addr; 13677e58d5aeSAmos Kong struct scatterlist sg; 13689c46f6d4SAlex Williamson 1369801822d1SShyam Saini addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 1370e37e2ff3SAndy Lutomirski if (!addr) 1371e37e2ff3SAndy Lutomirski return -ENOMEM; 1372e37e2ff3SAndy Lutomirski 1373e37e2ff3SAndy Lutomirski ret = eth_prepare_mac_addr_change(dev, addr); 1374f2f2c8b4SJiri Pirko if (ret) 1375e37e2ff3SAndy Lutomirski goto out; 13769c46f6d4SAlex Williamson 13777e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 13787e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 13797e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1380d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 13817e58d5aeSAmos Kong dev_warn(&vdev->dev, 13827e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 1383e37e2ff3SAndy Lutomirski ret = -EINVAL; 1384e37e2ff3SAndy Lutomirski goto out; 13857e58d5aeSAmos Kong } 13867e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 13877e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1388855e0c52SRusty Russell unsigned int i; 1389855e0c52SRusty Russell 1390855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 1391855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 1392855e0c52SRusty Russell virtio_cwrite8(vdev, 1393855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1394855e0c52SRusty Russell i, addr->sa_data[i]); 13957e58d5aeSAmos Kong } 13967e58d5aeSAmos Kong 13977e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 1398e37e2ff3SAndy Lutomirski ret = 0; 13999c46f6d4SAlex Williamson 1400e37e2ff3SAndy Lutomirski out: 1401e37e2ff3SAndy Lutomirski kfree(addr); 1402e37e2ff3SAndy Lutomirski return ret; 14039c46f6d4SAlex Williamson } 14049c46f6d4SAlex Williamson 1405bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev, 14063fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 14073fa2a1dfSstephen hemminger { 14083fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 14093fa2a1dfSstephen hemminger int cpu; 14103fa2a1dfSstephen hemminger unsigned int start; 14113fa2a1dfSstephen hemminger 14123fa2a1dfSstephen hemminger for_each_possible_cpu(cpu) { 141358472a76SEric Dumazet struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); 14143fa2a1dfSstephen hemminger u64 tpackets, tbytes, rpackets, rbytes; 14153fa2a1dfSstephen hemminger 14163fa2a1dfSstephen hemminger do { 141757a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->tx_syncp); 14183fa2a1dfSstephen hemminger tpackets = stats->tx_packets; 14193fa2a1dfSstephen hemminger tbytes = stats->tx_bytes; 142057a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start)); 142183a27052SEric Dumazet 142283a27052SEric Dumazet do { 142357a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->rx_syncp); 14243fa2a1dfSstephen hemminger rpackets = stats->rx_packets; 14253fa2a1dfSstephen hemminger rbytes = stats->rx_bytes; 142657a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start)); 14273fa2a1dfSstephen hemminger 14283fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 14293fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 14303fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 14313fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 14323fa2a1dfSstephen hemminger } 14333fa2a1dfSstephen hemminger 14343fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1435021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 14363fa2a1dfSstephen hemminger tot->rx_dropped = dev->stats.rx_dropped; 14373fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 14383fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 14393fa2a1dfSstephen hemminger } 14403fa2a1dfSstephen hemminger 1441da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 1442da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 1443da74e89dSAmit Shah { 1444da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 1445986a4f4dSJason Wang int i; 1446da74e89dSAmit Shah 1447986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 1448986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 1449da74e89dSAmit Shah } 1450da74e89dSAmit Shah #endif 1451da74e89dSAmit Shah 1452586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1453586d17c5SJason Wang { 1454586d17c5SJason Wang rtnl_lock(); 1455586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1456d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1457586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1458586d17c5SJason Wang rtnl_unlock(); 1459586d17c5SJason Wang } 1460586d17c5SJason Wang 146147315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1462986a4f4dSJason Wang { 1463986a4f4dSJason Wang struct scatterlist sg; 1464986a4f4dSJason Wang struct net_device *dev = vi->dev; 1465986a4f4dSJason Wang 1466986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1467986a4f4dSJason Wang return 0; 1468986a4f4dSJason Wang 1469a725ee3eSAndy Lutomirski vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 1470a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq)); 1471986a4f4dSJason Wang 1472986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1473d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1474986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1475986a4f4dSJason Wang queue_pairs); 1476986a4f4dSJason Wang return -EINVAL; 147755257d72SSasha Levin } else { 1478986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 147935ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 148035ed159bSJason Wang if (dev->flags & IFF_UP) 14819b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 148255257d72SSasha Levin } 1483986a4f4dSJason Wang 1484986a4f4dSJason Wang return 0; 1485986a4f4dSJason Wang } 1486986a4f4dSJason Wang 148747315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 148847315329SJohn Fastabend { 148947315329SJohn Fastabend int err; 149047315329SJohn Fastabend 149147315329SJohn Fastabend rtnl_lock(); 149247315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 149347315329SJohn Fastabend rtnl_unlock(); 149447315329SJohn Fastabend return err; 149547315329SJohn Fastabend } 149647315329SJohn Fastabend 1497296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1498296f96fcSRusty Russell { 1499296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1500986a4f4dSJason Wang int i; 1501296f96fcSRusty Russell 1502b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1503b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1504986a4f4dSJason Wang 1505b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 1506986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 150778a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 1508b92f1e67SWillem de Bruijn } 1509296f96fcSRusty Russell 1510296f96fcSRusty Russell return 0; 1511296f96fcSRusty Russell } 1512296f96fcSRusty Russell 15132af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 15142af7698eSAlex Williamson { 15152af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1516f565a7c2SAlex Williamson struct scatterlist sg[2]; 1517f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1518ccffad25SJiri Pirko struct netdev_hw_addr *ha; 151932e7bfc4SJiri Pirko int uc_count; 15204cd24eafSJiri Pirko int mc_count; 1521f565a7c2SAlex Williamson void *buf; 1522f565a7c2SAlex Williamson int i; 15232af7698eSAlex Williamson 1524788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 15252af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 15262af7698eSAlex Williamson return; 15272af7698eSAlex Williamson 15282ac46030SMichael S. Tsirkin vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0); 15292ac46030SMichael S. Tsirkin vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 15302af7698eSAlex Williamson 15312ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc)); 15322af7698eSAlex Williamson 15332af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1534d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 15352af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 15362ac46030SMichael S. Tsirkin vi->ctrl_promisc ? "en" : "dis"); 15372af7698eSAlex Williamson 15382ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti)); 15392af7698eSAlex Williamson 15402af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1541d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 15422af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 15432ac46030SMichael S. Tsirkin vi->ctrl_allmulti ? "en" : "dis"); 1544f565a7c2SAlex Williamson 154532e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 15464cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1547f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 15484cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1549f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 15504cd24eafSJiri Pirko mac_data = buf; 1551e68ed8f0SJoe Perches if (!buf) 1552f565a7c2SAlex Williamson return; 1553f565a7c2SAlex Williamson 155423e258e1SAlex Williamson sg_init_table(sg, 2); 155523e258e1SAlex Williamson 1556f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 1557fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1558ccffad25SJiri Pirko i = 0; 155932e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1560ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1561f565a7c2SAlex Williamson 1562f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 156332e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1564f565a7c2SAlex Williamson 1565f565a7c2SAlex Williamson /* multicast list and count fill the end */ 156632e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1567f565a7c2SAlex Williamson 1568fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1569567ec874SJiri Pirko i = 0; 157022bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 157122bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1572f565a7c2SAlex Williamson 1573f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 15744cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1575f565a7c2SAlex Williamson 1576f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1577d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 157899e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1579f565a7c2SAlex Williamson 1580f565a7c2SAlex Williamson kfree(buf); 15812af7698eSAlex Williamson } 15822af7698eSAlex Williamson 158380d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 158480d5c368SPatrick McHardy __be16 proto, u16 vid) 15850bde9569SAlex Williamson { 15860bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 15870bde9569SAlex Williamson struct scatterlist sg; 15880bde9569SAlex Williamson 1589a725ee3eSAndy Lutomirski vi->ctrl_vid = vid; 1590a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 15910bde9569SAlex Williamson 15920bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1593d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 15940bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 15958e586137SJiri Pirko return 0; 15960bde9569SAlex Williamson } 15970bde9569SAlex Williamson 159880d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 159980d5c368SPatrick McHardy __be16 proto, u16 vid) 16000bde9569SAlex Williamson { 16010bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 16020bde9569SAlex Williamson struct scatterlist sg; 16030bde9569SAlex Williamson 1604a725ee3eSAndy Lutomirski vi->ctrl_vid = vid; 1605a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 16060bde9569SAlex Williamson 16070bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1608d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 16090bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 16108e586137SJiri Pirko return 0; 16110bde9569SAlex Williamson } 16120bde9569SAlex Williamson 16138898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1614986a4f4dSJason Wang { 1615986a4f4dSJason Wang int i; 16168898c21cSWanlong Gao 16178898c21cSWanlong Gao if (vi->affinity_hint_set) { 16188898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 16198898c21cSWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, -1); 16208898c21cSWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, -1); 16218898c21cSWanlong Gao } 16228898c21cSWanlong Gao 16238898c21cSWanlong Gao vi->affinity_hint_set = false; 16248898c21cSWanlong Gao } 16258898c21cSWanlong Gao } 16268898c21cSWanlong Gao 16278898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1628986a4f4dSJason Wang { 1629986a4f4dSJason Wang int i; 163047be2479SWanlong Gao int cpu; 1631986a4f4dSJason Wang 1632986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1633986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1634986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1635986a4f4dSJason Wang */ 16368898c21cSWanlong Gao if (vi->curr_queue_pairs == 1 || 16378898c21cSWanlong Gao vi->max_queue_pairs != num_online_cpus()) { 16388898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1639986a4f4dSJason Wang return; 1640986a4f4dSJason Wang } 1641986a4f4dSJason Wang 164247be2479SWanlong Gao i = 0; 164347be2479SWanlong Gao for_each_online_cpu(cpu) { 1644986a4f4dSJason Wang virtqueue_set_affinity(vi->rq[i].vq, cpu); 1645986a4f4dSJason Wang virtqueue_set_affinity(vi->sq[i].vq, cpu); 16469bb8ca86SJason Wang netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); 164747be2479SWanlong Gao i++; 1648986a4f4dSJason Wang } 1649986a4f4dSJason Wang 1650986a4f4dSJason Wang vi->affinity_hint_set = true; 165147be2479SWanlong Gao } 1652986a4f4dSJason Wang 16538017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 16548de4b2f3SWanlong Gao { 16558017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 16568017c279SSebastian Andrzej Siewior node); 16578de4b2f3SWanlong Gao virtnet_set_affinity(vi); 16588017c279SSebastian Andrzej Siewior return 0; 16598de4b2f3SWanlong Gao } 16603ab098dfSJason Wang 16618017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 16628017c279SSebastian Andrzej Siewior { 16638017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 16648017c279SSebastian Andrzej Siewior node_dead); 16658017c279SSebastian Andrzej Siewior virtnet_set_affinity(vi); 16668017c279SSebastian Andrzej Siewior return 0; 16678017c279SSebastian Andrzej Siewior } 16688017c279SSebastian Andrzej Siewior 16698017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 16708017c279SSebastian Andrzej Siewior { 16718017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 16728017c279SSebastian Andrzej Siewior node); 16738017c279SSebastian Andrzej Siewior 16748017c279SSebastian Andrzej Siewior virtnet_clean_affinity(vi, cpu); 16758017c279SSebastian Andrzej Siewior return 0; 16768017c279SSebastian Andrzej Siewior } 16778017c279SSebastian Andrzej Siewior 16788017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online; 16798017c279SSebastian Andrzej Siewior 16808017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi) 16818017c279SSebastian Andrzej Siewior { 16828017c279SSebastian Andrzej Siewior int ret; 16838017c279SSebastian Andrzej Siewior 16848017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 16858017c279SSebastian Andrzej Siewior if (ret) 16868017c279SSebastian Andrzej Siewior return ret; 16878017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 16888017c279SSebastian Andrzej Siewior &vi->node_dead); 16898017c279SSebastian Andrzej Siewior if (!ret) 16908017c279SSebastian Andrzej Siewior return ret; 16918017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 16928017c279SSebastian Andrzej Siewior return ret; 16938017c279SSebastian Andrzej Siewior } 16948017c279SSebastian Andrzej Siewior 16958017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 16968017c279SSebastian Andrzej Siewior { 16978017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 16988017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 16998017c279SSebastian Andrzej Siewior &vi->node_dead); 1700a9ea3fc6SHerbert Xu } 1701a9ea3fc6SHerbert Xu 17028f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 17038f9f4668SRick Jones struct ethtool_ringparam *ring) 17048f9f4668SRick Jones { 17058f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 17068f9f4668SRick Jones 1707986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1708986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 17098f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 17108f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 17118f9f4668SRick Jones } 17128f9f4668SRick Jones 171366846048SRick Jones 171466846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 171566846048SRick Jones struct ethtool_drvinfo *info) 171666846048SRick Jones { 171766846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 171866846048SRick Jones struct virtio_device *vdev = vi->vdev; 171966846048SRick Jones 172066846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 172166846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 172266846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 172366846048SRick Jones 172466846048SRick Jones } 172566846048SRick Jones 1726d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1727d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1728d73bcd2cSJason Wang struct ethtool_channels *channels) 1729d73bcd2cSJason Wang { 1730d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1731d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1732d73bcd2cSJason Wang int err; 1733d73bcd2cSJason Wang 1734d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1735d73bcd2cSJason Wang * We don't allow setting 'other' channels. 1736d73bcd2cSJason Wang */ 1737d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 1738d73bcd2cSJason Wang return -EINVAL; 1739d73bcd2cSJason Wang 1740c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 1741d73bcd2cSJason Wang return -EINVAL; 1742d73bcd2cSJason Wang 1743f600b690SJohn Fastabend /* For now we don't support modifying channels while XDP is loaded 1744f600b690SJohn Fastabend * also when XDP is loaded all RX queues have XDP programs so we only 1745f600b690SJohn Fastabend * need to check a single RX queue. 1746f600b690SJohn Fastabend */ 1747f600b690SJohn Fastabend if (vi->rq[0].xdp_prog) 1748f600b690SJohn Fastabend return -EINVAL; 1749f600b690SJohn Fastabend 175047be2479SWanlong Gao get_online_cpus(); 175147315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 1752d73bcd2cSJason Wang if (!err) { 1753d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 1754d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 1755d73bcd2cSJason Wang 17568898c21cSWanlong Gao virtnet_set_affinity(vi); 1757d73bcd2cSJason Wang } 175847be2479SWanlong Gao put_online_cpus(); 1759d73bcd2cSJason Wang 1760d73bcd2cSJason Wang return err; 1761d73bcd2cSJason Wang } 1762d73bcd2cSJason Wang 1763d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 1764d73bcd2cSJason Wang struct ethtool_channels *channels) 1765d73bcd2cSJason Wang { 1766d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1767d73bcd2cSJason Wang 1768d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 1769d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 1770d73bcd2cSJason Wang channels->max_other = 0; 1771d73bcd2cSJason Wang channels->rx_count = 0; 1772d73bcd2cSJason Wang channels->tx_count = 0; 1773d73bcd2cSJason Wang channels->other_count = 0; 1774d73bcd2cSJason Wang } 1775d73bcd2cSJason Wang 177616032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */ 1777ebb6b4b1SPhilippe Reynes static bool 1778ebb6b4b1SPhilippe Reynes virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd) 177916032be5SNikolay Aleksandrov { 1780ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings diff1 = *cmd; 1781ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings diff2 = {}; 178216032be5SNikolay Aleksandrov 17830cf3ace9SNikolay Aleksandrov /* cmd is always set so we need to clear it, validate the port type 17840cf3ace9SNikolay Aleksandrov * and also without autonegotiation we can ignore advertising 17850cf3ace9SNikolay Aleksandrov */ 1786ebb6b4b1SPhilippe Reynes diff1.base.speed = 0; 1787ebb6b4b1SPhilippe Reynes diff2.base.port = PORT_OTHER; 1788ebb6b4b1SPhilippe Reynes ethtool_link_ksettings_zero_link_mode(&diff1, advertising); 1789ebb6b4b1SPhilippe Reynes diff1.base.duplex = 0; 1790ebb6b4b1SPhilippe Reynes diff1.base.cmd = 0; 1791ebb6b4b1SPhilippe Reynes diff1.base.link_mode_masks_nwords = 0; 179216032be5SNikolay Aleksandrov 1793ebb6b4b1SPhilippe Reynes return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) && 1794ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.supported, 1795ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS) && 1796ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.advertising, 1797ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS) && 1798ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.lp_advertising, 1799ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS); 180016032be5SNikolay Aleksandrov } 180116032be5SNikolay Aleksandrov 1802ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev, 1803ebb6b4b1SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 180416032be5SNikolay Aleksandrov { 180516032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 180616032be5SNikolay Aleksandrov u32 speed; 180716032be5SNikolay Aleksandrov 1808ebb6b4b1SPhilippe Reynes speed = cmd->base.speed; 180916032be5SNikolay Aleksandrov /* don't allow custom speed and duplex */ 181016032be5SNikolay Aleksandrov if (!ethtool_validate_speed(speed) || 1811ebb6b4b1SPhilippe Reynes !ethtool_validate_duplex(cmd->base.duplex) || 181216032be5SNikolay Aleksandrov !virtnet_validate_ethtool_cmd(cmd)) 181316032be5SNikolay Aleksandrov return -EINVAL; 181416032be5SNikolay Aleksandrov vi->speed = speed; 1815ebb6b4b1SPhilippe Reynes vi->duplex = cmd->base.duplex; 181616032be5SNikolay Aleksandrov 181716032be5SNikolay Aleksandrov return 0; 181816032be5SNikolay Aleksandrov } 181916032be5SNikolay Aleksandrov 1820ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev, 1821ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings *cmd) 182216032be5SNikolay Aleksandrov { 182316032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 182416032be5SNikolay Aleksandrov 1825ebb6b4b1SPhilippe Reynes cmd->base.speed = vi->speed; 1826ebb6b4b1SPhilippe Reynes cmd->base.duplex = vi->duplex; 1827ebb6b4b1SPhilippe Reynes cmd->base.port = PORT_OTHER; 182816032be5SNikolay Aleksandrov 182916032be5SNikolay Aleksandrov return 0; 183016032be5SNikolay Aleksandrov } 183116032be5SNikolay Aleksandrov 183216032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 183316032be5SNikolay Aleksandrov { 183416032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 183516032be5SNikolay Aleksandrov 183616032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 183716032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 183816032be5SNikolay Aleksandrov } 183916032be5SNikolay Aleksandrov 18400fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 184166846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 18429f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 18438f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 1844d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 1845d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 1846074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 1847ebb6b4b1SPhilippe Reynes .get_link_ksettings = virtnet_get_link_ksettings, 1848ebb6b4b1SPhilippe Reynes .set_link_ksettings = virtnet_set_link_ksettings, 1849a9ea3fc6SHerbert Xu }; 1850a9ea3fc6SHerbert Xu 18519fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev) 18529fe7bfceSJohn Fastabend { 18539fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 18549fe7bfceSJohn Fastabend int i; 18559fe7bfceSJohn Fastabend 18569fe7bfceSJohn Fastabend /* Make sure no work handler is accessing the device */ 18579fe7bfceSJohn Fastabend flush_work(&vi->config_work); 18589fe7bfceSJohn Fastabend 18599fe7bfceSJohn Fastabend netif_device_detach(vi->dev); 1860713a98d9SJason Wang netif_tx_disable(vi->dev); 18619fe7bfceSJohn Fastabend cancel_delayed_work_sync(&vi->refill); 18629fe7bfceSJohn Fastabend 18639fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 1864b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 18659fe7bfceSJohn Fastabend napi_disable(&vi->rq[i].napi); 186678a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 1867b92f1e67SWillem de Bruijn } 18689fe7bfceSJohn Fastabend } 18699fe7bfceSJohn Fastabend } 18709fe7bfceSJohn Fastabend 18719fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi); 18729fe7bfceSJohn Fastabend 18739fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev) 18749fe7bfceSJohn Fastabend { 18759fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 18769fe7bfceSJohn Fastabend int err, i; 18779fe7bfceSJohn Fastabend 18789fe7bfceSJohn Fastabend err = init_vqs(vi); 18799fe7bfceSJohn Fastabend if (err) 18809fe7bfceSJohn Fastabend return err; 18819fe7bfceSJohn Fastabend 18829fe7bfceSJohn Fastabend virtio_device_ready(vdev); 18839fe7bfceSJohn Fastabend 18849fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 18859fe7bfceSJohn Fastabend for (i = 0; i < vi->curr_queue_pairs; i++) 18869fe7bfceSJohn Fastabend if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 18879fe7bfceSJohn Fastabend schedule_delayed_work(&vi->refill, 0); 18889fe7bfceSJohn Fastabend 1889b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 1890e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1891b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, 1892b92f1e67SWillem de Bruijn &vi->sq[i].napi); 1893b92f1e67SWillem de Bruijn } 18949fe7bfceSJohn Fastabend } 18959fe7bfceSJohn Fastabend 18969fe7bfceSJohn Fastabend netif_device_attach(vi->dev); 18979fe7bfceSJohn Fastabend return err; 18989fe7bfceSJohn Fastabend } 18999fe7bfceSJohn Fastabend 19009861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 19019861ce03SJakub Kicinski struct netlink_ext_ack *extack) 1902f600b690SJohn Fastabend { 1903f600b690SJohn Fastabend unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); 1904f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 1905f600b690SJohn Fastabend struct bpf_prog *old_prog; 1906017b29c3SJason Wang u16 xdp_qp = 0, curr_qp; 1907672aafd5SJohn Fastabend int i, err; 1908f600b690SJohn Fastabend 1909f600b690SJohn Fastabend if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 191092502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 191192502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 191292502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) { 19134d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first"); 1914f600b690SJohn Fastabend return -EOPNOTSUPP; 1915f600b690SJohn Fastabend } 1916f600b690SJohn Fastabend 1917f600b690SJohn Fastabend if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 19184d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 1919f600b690SJohn Fastabend return -EINVAL; 1920f600b690SJohn Fastabend } 1921f600b690SJohn Fastabend 1922f600b690SJohn Fastabend if (dev->mtu > max_sz) { 19234d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); 1924f600b690SJohn Fastabend netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); 1925f600b690SJohn Fastabend return -EINVAL; 1926f600b690SJohn Fastabend } 1927f600b690SJohn Fastabend 1928672aafd5SJohn Fastabend curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 1929672aafd5SJohn Fastabend if (prog) 1930672aafd5SJohn Fastabend xdp_qp = nr_cpu_ids; 1931672aafd5SJohn Fastabend 1932672aafd5SJohn Fastabend /* XDP requires extra queues for XDP_TX */ 1933672aafd5SJohn Fastabend if (curr_qp + xdp_qp > vi->max_queue_pairs) { 19344d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available"); 1935672aafd5SJohn Fastabend netdev_warn(dev, "request %i queues but max is %i\n", 1936672aafd5SJohn Fastabend curr_qp + xdp_qp, vi->max_queue_pairs); 1937672aafd5SJohn Fastabend return -ENOMEM; 1938672aafd5SJohn Fastabend } 1939672aafd5SJohn Fastabend 19402de2f7f4SJohn Fastabend if (prog) { 19412de2f7f4SJohn Fastabend prog = bpf_prog_add(prog, vi->max_queue_pairs - 1); 19422de2f7f4SJohn Fastabend if (IS_ERR(prog)) 19432de2f7f4SJohn Fastabend return PTR_ERR(prog); 19442de2f7f4SJohn Fastabend } 19452de2f7f4SJohn Fastabend 1946*4941d472SJason Wang /* Make sure NAPI is not using any XDP TX queues for RX. */ 1947*4941d472SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1948*4941d472SJason Wang napi_disable(&vi->rq[i].napi); 19492de2f7f4SJohn Fastabend 1950672aafd5SJohn Fastabend netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 1951*4941d472SJason Wang err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 1952*4941d472SJason Wang if (err) 1953*4941d472SJason Wang goto err; 1954*4941d472SJason Wang vi->xdp_queue_pairs = xdp_qp; 1955f600b690SJohn Fastabend 1956f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 1957f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 1958f600b690SJohn Fastabend rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 1959f600b690SJohn Fastabend if (old_prog) 1960f600b690SJohn Fastabend bpf_prog_put(old_prog); 1961*4941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1962f600b690SJohn Fastabend } 1963f600b690SJohn Fastabend 1964f600b690SJohn Fastabend return 0; 19652de2f7f4SJohn Fastabend 1966*4941d472SJason Wang err: 1967*4941d472SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1968*4941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 19692de2f7f4SJohn Fastabend if (prog) 19702de2f7f4SJohn Fastabend bpf_prog_sub(prog, vi->max_queue_pairs - 1); 19712de2f7f4SJohn Fastabend return err; 1972f600b690SJohn Fastabend } 1973f600b690SJohn Fastabend 19745b0e6629SMartin KaFai Lau static u32 virtnet_xdp_query(struct net_device *dev) 1975f600b690SJohn Fastabend { 1976f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 19775b0e6629SMartin KaFai Lau const struct bpf_prog *xdp_prog; 1978f600b690SJohn Fastabend int i; 1979f600b690SJohn Fastabend 1980f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 19815b0e6629SMartin KaFai Lau xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog); 19825b0e6629SMartin KaFai Lau if (xdp_prog) 19835b0e6629SMartin KaFai Lau return xdp_prog->aux->id; 1984f600b690SJohn Fastabend } 19855b0e6629SMartin KaFai Lau return 0; 1986f600b690SJohn Fastabend } 1987f600b690SJohn Fastabend 1988f600b690SJohn Fastabend static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp) 1989f600b690SJohn Fastabend { 1990f600b690SJohn Fastabend switch (xdp->command) { 1991f600b690SJohn Fastabend case XDP_SETUP_PROG: 19929861ce03SJakub Kicinski return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 1993f600b690SJohn Fastabend case XDP_QUERY_PROG: 19945b0e6629SMartin KaFai Lau xdp->prog_id = virtnet_xdp_query(dev); 19955b0e6629SMartin KaFai Lau xdp->prog_attached = !!xdp->prog_id; 1996f600b690SJohn Fastabend return 0; 1997f600b690SJohn Fastabend default: 1998f600b690SJohn Fastabend return -EINVAL; 1999f600b690SJohn Fastabend } 2000f600b690SJohn Fastabend } 2001f600b690SJohn Fastabend 200276288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 200376288b4eSStephen Hemminger .ndo_open = virtnet_open, 200476288b4eSStephen Hemminger .ndo_stop = virtnet_close, 200576288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 200676288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 20079c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 20082af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 20093fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 20101824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 20111824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 201276288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 201376288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 201476288b4eSStephen Hemminger #endif 2015f600b690SJohn Fastabend .ndo_xdp = virtnet_xdp, 20162836b4f2SVlad Yasevich .ndo_features_check = passthru_features_check, 201776288b4eSStephen Hemminger }; 201876288b4eSStephen Hemminger 2019586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 20209f4d26d0SMark McLoughlin { 2021586d17c5SJason Wang struct virtnet_info *vi = 2022586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 20239f4d26d0SMark McLoughlin u16 v; 20249f4d26d0SMark McLoughlin 2025855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 2026855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 2027507613bfSMichael S. Tsirkin return; 2028586d17c5SJason Wang 2029586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 2030ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 2031586d17c5SJason Wang virtnet_ack_link_announce(vi); 2032586d17c5SJason Wang } 20339f4d26d0SMark McLoughlin 20349f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 20359f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 20369f4d26d0SMark McLoughlin 20379f4d26d0SMark McLoughlin if (vi->status == v) 2038507613bfSMichael S. Tsirkin return; 20399f4d26d0SMark McLoughlin 20409f4d26d0SMark McLoughlin vi->status = v; 20419f4d26d0SMark McLoughlin 20429f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 20439f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 2044986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 20459f4d26d0SMark McLoughlin } else { 20469f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 2047986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 20489f4d26d0SMark McLoughlin } 20499f4d26d0SMark McLoughlin } 20509f4d26d0SMark McLoughlin 20519f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 20529f4d26d0SMark McLoughlin { 20539f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 20549f4d26d0SMark McLoughlin 20553b07e9caSTejun Heo schedule_work(&vi->config_work); 20569f4d26d0SMark McLoughlin } 20579f4d26d0SMark McLoughlin 2058986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 2059986a4f4dSJason Wang { 2060d4fb84eeSAndrey Vagin int i; 2061d4fb84eeSAndrey Vagin 2062ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2063ab3971b1SJason Wang napi_hash_del(&vi->rq[i].napi); 2064d4fb84eeSAndrey Vagin netif_napi_del(&vi->rq[i].napi); 2065b92f1e67SWillem de Bruijn netif_napi_del(&vi->sq[i].napi); 2066ab3971b1SJason Wang } 2067d4fb84eeSAndrey Vagin 2068963abe5cSEric Dumazet /* We called napi_hash_del() before netif_napi_del(), 2069963abe5cSEric Dumazet * we need to respect an RCU grace period before freeing vi->rq 2070963abe5cSEric Dumazet */ 2071963abe5cSEric Dumazet synchronize_net(); 2072963abe5cSEric Dumazet 2073986a4f4dSJason Wang kfree(vi->rq); 2074986a4f4dSJason Wang kfree(vi->sq); 2075986a4f4dSJason Wang } 2076986a4f4dSJason Wang 207747315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi) 2078986a4f4dSJason Wang { 2079f600b690SJohn Fastabend struct bpf_prog *old_prog; 2080986a4f4dSJason Wang int i; 2081986a4f4dSJason Wang 2082986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2083986a4f4dSJason Wang while (vi->rq[i].pages) 2084986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 2085f600b690SJohn Fastabend 2086f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2087f600b690SJohn Fastabend RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 2088f600b690SJohn Fastabend if (old_prog) 2089f600b690SJohn Fastabend bpf_prog_put(old_prog); 2090986a4f4dSJason Wang } 209147315329SJohn Fastabend } 209247315329SJohn Fastabend 209347315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi) 209447315329SJohn Fastabend { 209547315329SJohn Fastabend rtnl_lock(); 209647315329SJohn Fastabend _free_receive_bufs(vi); 2097f600b690SJohn Fastabend rtnl_unlock(); 2098986a4f4dSJason Wang } 2099986a4f4dSJason Wang 2100fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 2101fb51879dSMichael Dalton { 2102fb51879dSMichael Dalton int i; 2103fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 2104fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 2105fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 2106fb51879dSMichael Dalton } 2107fb51879dSMichael Dalton 2108b68df015SJohn Fastabend static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 210956434a01SJohn Fastabend { 211056434a01SJohn Fastabend if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 211156434a01SJohn Fastabend return false; 211256434a01SJohn Fastabend else if (q < vi->curr_queue_pairs) 211356434a01SJohn Fastabend return true; 211456434a01SJohn Fastabend else 211556434a01SJohn Fastabend return false; 211656434a01SJohn Fastabend } 211756434a01SJohn Fastabend 2118986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 2119986a4f4dSJason Wang { 2120986a4f4dSJason Wang void *buf; 2121986a4f4dSJason Wang int i; 2122986a4f4dSJason Wang 2123986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2124986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 212556434a01SJohn Fastabend while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2126b68df015SJohn Fastabend if (!is_xdp_raw_buffer_queue(vi, i)) 2127986a4f4dSJason Wang dev_kfree_skb(buf); 212856434a01SJohn Fastabend else 212956434a01SJohn Fastabend put_page(virt_to_head_page(buf)); 213056434a01SJohn Fastabend } 2131986a4f4dSJason Wang } 2132986a4f4dSJason Wang 2133986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2134986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 2135986a4f4dSJason Wang 2136986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2137ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 2138680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 2139ab7db917SMichael Dalton } else if (vi->big_packets) { 2140fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 2141ab7db917SMichael Dalton } else { 2142f6b10209SJason Wang put_page(virt_to_head_page(buf)); 2143986a4f4dSJason Wang } 2144986a4f4dSJason Wang } 2145986a4f4dSJason Wang } 2146ab7db917SMichael Dalton } 2147986a4f4dSJason Wang 2148e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 2149e9d7417bSJason Wang { 2150e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 2151e9d7417bSJason Wang 21528898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 2153986a4f4dSJason Wang 2154e9d7417bSJason Wang vdev->config->del_vqs(vdev); 2155986a4f4dSJason Wang 2156986a4f4dSJason Wang virtnet_free_queues(vi); 2157986a4f4dSJason Wang } 2158986a4f4dSJason Wang 2159d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at 2160d85b758fSMichael S. Tsirkin * least one full packet? 2161d85b758fSMichael S. Tsirkin * Logic below assumes the mergeable buffer header is used. 2162d85b758fSMichael S. Tsirkin */ 2163d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 2164d85b758fSMichael S. Tsirkin { 2165d85b758fSMichael S. Tsirkin const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2166d85b758fSMichael S. Tsirkin unsigned int rq_size = virtqueue_get_vring_size(vq); 2167d85b758fSMichael S. Tsirkin unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 2168d85b758fSMichael S. Tsirkin unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 2169d85b758fSMichael S. Tsirkin unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 2170d85b758fSMichael S. Tsirkin 2171f0c3192cSMichael S. Tsirkin return max(max(min_buf_len, hdr_len) - hdr_len, 2172f0c3192cSMichael S. Tsirkin (unsigned int)GOOD_PACKET_LEN); 2173d85b758fSMichael S. Tsirkin } 2174d85b758fSMichael S. Tsirkin 2175986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 2176986a4f4dSJason Wang { 2177986a4f4dSJason Wang vq_callback_t **callbacks; 2178986a4f4dSJason Wang struct virtqueue **vqs; 2179986a4f4dSJason Wang int ret = -ENOMEM; 2180986a4f4dSJason Wang int i, total_vqs; 2181986a4f4dSJason Wang const char **names; 2182d45b897bSMichael S. Tsirkin bool *ctx; 2183986a4f4dSJason Wang 2184986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 2185986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 2186986a4f4dSJason Wang * possible control vq. 2187986a4f4dSJason Wang */ 2188986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 2189986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 2190986a4f4dSJason Wang 2191986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 2192986a4f4dSJason Wang vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 2193986a4f4dSJason Wang if (!vqs) 2194986a4f4dSJason Wang goto err_vq; 2195986a4f4dSJason Wang callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 2196986a4f4dSJason Wang if (!callbacks) 2197986a4f4dSJason Wang goto err_callback; 2198986a4f4dSJason Wang names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 2199986a4f4dSJason Wang if (!names) 2200986a4f4dSJason Wang goto err_names; 2201192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 2202d45b897bSMichael S. Tsirkin ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL); 2203d45b897bSMichael S. Tsirkin if (!ctx) 2204d45b897bSMichael S. Tsirkin goto err_ctx; 2205d45b897bSMichael S. Tsirkin } else { 2206d45b897bSMichael S. Tsirkin ctx = NULL; 2207d45b897bSMichael S. Tsirkin } 2208986a4f4dSJason Wang 2209986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 2210986a4f4dSJason Wang if (vi->has_cvq) { 2211986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 2212986a4f4dSJason Wang names[total_vqs - 1] = "control"; 2213986a4f4dSJason Wang } 2214986a4f4dSJason Wang 2215986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 2216986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2217986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 2218986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 2219986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 2220986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 2221986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 2222986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 2223d45b897bSMichael S. Tsirkin if (ctx) 2224d45b897bSMichael S. Tsirkin ctx[rxq2vq(i)] = true; 2225986a4f4dSJason Wang } 2226986a4f4dSJason Wang 2227986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 2228d45b897bSMichael S. Tsirkin names, ctx, NULL); 2229986a4f4dSJason Wang if (ret) 2230986a4f4dSJason Wang goto err_find; 2231986a4f4dSJason Wang 2232986a4f4dSJason Wang if (vi->has_cvq) { 2233986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 2234986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 2235f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2236986a4f4dSJason Wang } 2237986a4f4dSJason Wang 2238986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2239986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 2240d85b758fSMichael S. Tsirkin vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 2241986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 2242986a4f4dSJason Wang } 2243986a4f4dSJason Wang 2244986a4f4dSJason Wang kfree(names); 2245986a4f4dSJason Wang kfree(callbacks); 2246986a4f4dSJason Wang kfree(vqs); 224755281621SJason Wang kfree(ctx); 2248986a4f4dSJason Wang 2249986a4f4dSJason Wang return 0; 2250986a4f4dSJason Wang 2251986a4f4dSJason Wang err_find: 2252d45b897bSMichael S. Tsirkin kfree(ctx); 2253d45b897bSMichael S. Tsirkin err_ctx: 2254986a4f4dSJason Wang kfree(names); 2255986a4f4dSJason Wang err_names: 2256986a4f4dSJason Wang kfree(callbacks); 2257986a4f4dSJason Wang err_callback: 2258986a4f4dSJason Wang kfree(vqs); 2259986a4f4dSJason Wang err_vq: 2260986a4f4dSJason Wang return ret; 2261986a4f4dSJason Wang } 2262986a4f4dSJason Wang 2263986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 2264986a4f4dSJason Wang { 2265986a4f4dSJason Wang int i; 2266986a4f4dSJason Wang 2267986a4f4dSJason Wang vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 2268986a4f4dSJason Wang if (!vi->sq) 2269986a4f4dSJason Wang goto err_sq; 2270986a4f4dSJason Wang vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 2271008d4278SAmerigo Wang if (!vi->rq) 2272986a4f4dSJason Wang goto err_rq; 2273986a4f4dSJason Wang 2274986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 2275986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2276986a4f4dSJason Wang vi->rq[i].pages = NULL; 2277986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2278986a4f4dSJason Wang napi_weight); 22791d11e732SWillem de Bruijn netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2280b92f1e67SWillem de Bruijn napi_tx ? napi_weight : 0); 2281986a4f4dSJason Wang 2282986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 22835377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 2284986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 2285986a4f4dSJason Wang } 2286986a4f4dSJason Wang 2287986a4f4dSJason Wang return 0; 2288986a4f4dSJason Wang 2289986a4f4dSJason Wang err_rq: 2290986a4f4dSJason Wang kfree(vi->sq); 2291986a4f4dSJason Wang err_sq: 2292986a4f4dSJason Wang return -ENOMEM; 2293e9d7417bSJason Wang } 2294e9d7417bSJason Wang 22953f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 22963f9c10b0SAmit Shah { 2297986a4f4dSJason Wang int ret; 22983f9c10b0SAmit Shah 2299986a4f4dSJason Wang /* Allocate send & receive queues */ 2300986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 2301986a4f4dSJason Wang if (ret) 2302986a4f4dSJason Wang goto err; 23033f9c10b0SAmit Shah 2304986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 2305986a4f4dSJason Wang if (ret) 2306986a4f4dSJason Wang goto err_free; 23073f9c10b0SAmit Shah 230847be2479SWanlong Gao get_online_cpus(); 23098898c21cSWanlong Gao virtnet_set_affinity(vi); 231047be2479SWanlong Gao put_online_cpus(); 231147be2479SWanlong Gao 23123f9c10b0SAmit Shah return 0; 2313986a4f4dSJason Wang 2314986a4f4dSJason Wang err_free: 2315986a4f4dSJason Wang virtnet_free_queues(vi); 2316986a4f4dSJason Wang err: 2317986a4f4dSJason Wang return ret; 23183f9c10b0SAmit Shah } 23193f9c10b0SAmit Shah 2320fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2321fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 2322fbf28d78SMichael Dalton struct rx_queue_attribute *attribute, char *buf) 2323fbf28d78SMichael Dalton { 2324fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 2325fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 23265377d758SJohannes Berg struct ewma_pkt_len *avg; 2327fbf28d78SMichael Dalton 2328fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 2329fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 2330d85b758fSMichael S. Tsirkin return sprintf(buf, "%u\n", 2331d85b758fSMichael S. Tsirkin get_mergeable_buf_len(&vi->rq[queue_index], avg)); 2332fbf28d78SMichael Dalton } 2333fbf28d78SMichael Dalton 2334fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 2335fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 2336fbf28d78SMichael Dalton 2337fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 2338fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 2339fbf28d78SMichael Dalton NULL 2340fbf28d78SMichael Dalton }; 2341fbf28d78SMichael Dalton 2342fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 2343fbf28d78SMichael Dalton .name = "virtio_net", 2344fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 2345fbf28d78SMichael Dalton }; 2346fbf28d78SMichael Dalton #endif 2347fbf28d78SMichael Dalton 2348892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 2349892d6eb1SJason Wang unsigned int fbit, 2350892d6eb1SJason Wang const char *fname, const char *dname) 2351892d6eb1SJason Wang { 2352892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 2353892d6eb1SJason Wang return false; 2354892d6eb1SJason Wang 2355892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 2356892d6eb1SJason Wang fname, dname); 2357892d6eb1SJason Wang 2358892d6eb1SJason Wang return true; 2359892d6eb1SJason Wang } 2360892d6eb1SJason Wang 2361892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 2362892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 2363892d6eb1SJason Wang 2364892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 2365892d6eb1SJason Wang { 2366892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 2367892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 2368892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2369892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 2370892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2371892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 2372892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2373892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 2374892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 2375892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 2376892d6eb1SJason Wang return false; 2377892d6eb1SJason Wang } 2378892d6eb1SJason Wang 2379892d6eb1SJason Wang return true; 2380892d6eb1SJason Wang } 2381892d6eb1SJason Wang 2382d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU 2383d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU 2384d0c2c997SJarod Wilson 2385fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev) 2386296f96fcSRusty Russell { 23876ba42248SMichael S. Tsirkin if (!vdev->config->get) { 23886ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 23896ba42248SMichael S. Tsirkin __func__); 23906ba42248SMichael S. Tsirkin return -EINVAL; 23916ba42248SMichael S. Tsirkin } 23926ba42248SMichael S. Tsirkin 2393892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 2394892d6eb1SJason Wang return -EINVAL; 2395892d6eb1SJason Wang 2396fe36cbe0SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 2397fe36cbe0SMichael S. Tsirkin int mtu = virtio_cread16(vdev, 2398fe36cbe0SMichael S. Tsirkin offsetof(struct virtio_net_config, 2399fe36cbe0SMichael S. Tsirkin mtu)); 2400fe36cbe0SMichael S. Tsirkin if (mtu < MIN_MTU) 2401fe36cbe0SMichael S. Tsirkin __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 2402fe36cbe0SMichael S. Tsirkin } 2403fe36cbe0SMichael S. Tsirkin 2404fe36cbe0SMichael S. Tsirkin return 0; 2405fe36cbe0SMichael S. Tsirkin } 2406fe36cbe0SMichael S. Tsirkin 2407fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev) 2408fe36cbe0SMichael S. Tsirkin { 2409fe36cbe0SMichael S. Tsirkin int i, err; 2410fe36cbe0SMichael S. Tsirkin struct net_device *dev; 2411fe36cbe0SMichael S. Tsirkin struct virtnet_info *vi; 2412fe36cbe0SMichael S. Tsirkin u16 max_queue_pairs; 2413fe36cbe0SMichael S. Tsirkin int mtu; 2414fe36cbe0SMichael S. Tsirkin 2415986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 2416855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 2417855e0c52SRusty Russell struct virtio_net_config, 2418855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 2419986a4f4dSJason Wang 2420986a4f4dSJason Wang /* We need at least 2 queue's */ 2421986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 2422986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 2423986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2424986a4f4dSJason Wang max_queue_pairs = 1; 2425296f96fcSRusty Russell 2426296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 2427986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 2428296f96fcSRusty Russell if (!dev) 2429296f96fcSRusty Russell return -ENOMEM; 2430296f96fcSRusty Russell 2431296f96fcSRusty Russell /* Set up network device as normal. */ 2432f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 243376288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 2434296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 24353fa2a1dfSstephen hemminger 24367ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 2437296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 2438296f96fcSRusty Russell 2439296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 244098e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 2441296f96fcSRusty Russell /* This opens up the world of extra features. */ 244248900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 244398e778c9SMichał Mirosław if (csum) 244448900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 244598e778c9SMichał Mirosław 244698e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 2447e078de03SDavid S. Miller dev->hw_features |= NETIF_F_TSO 244834a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 244934a48579SRusty Russell } 24505539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 245198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 245298e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 245398e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 245498e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 245598e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 245698e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 245798e778c9SMichał Mirosław 245841f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 245941f2f127SJason Wang 246098e778c9SMichał Mirosław if (gso) 2461e078de03SDavid S. Miller dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 246298e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 2463296f96fcSRusty Russell } 24644f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 24654f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 2466296f96fcSRusty Russell 24674fda8302SJason Wang dev->vlan_features = dev->features; 24684fda8302SJason Wang 2469d0c2c997SJarod Wilson /* MTU range: 68 - 65535 */ 2470d0c2c997SJarod Wilson dev->min_mtu = MIN_MTU; 2471d0c2c997SJarod Wilson dev->max_mtu = MAX_MTU; 2472d0c2c997SJarod Wilson 2473296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 2474855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 2475855e0c52SRusty Russell virtio_cread_bytes(vdev, 2476a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 2477855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 2478855e0c52SRusty Russell else 2479f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 2480296f96fcSRusty Russell 2481296f96fcSRusty Russell /* Set up our device-specific information */ 2482296f96fcSRusty Russell vi = netdev_priv(dev); 2483296f96fcSRusty Russell vi->dev = dev; 2484296f96fcSRusty Russell vi->vdev = vdev; 2485d9d5dcc8SChristian Borntraeger vdev->priv = vi; 24863fa2a1dfSstephen hemminger vi->stats = alloc_percpu(struct virtnet_stats); 24873fa2a1dfSstephen hemminger err = -ENOMEM; 24883fa2a1dfSstephen hemminger if (vi->stats == NULL) 24893fa2a1dfSstephen hemminger goto free; 24903fa2a1dfSstephen hemminger 2491827da44cSJohn Stultz for_each_possible_cpu(i) { 2492827da44cSJohn Stultz struct virtnet_stats *virtnet_stats; 2493827da44cSJohn Stultz virtnet_stats = per_cpu_ptr(vi->stats, i); 2494827da44cSJohn Stultz u64_stats_init(&virtnet_stats->tx_syncp); 2495827da44cSJohn Stultz u64_stats_init(&virtnet_stats->rx_syncp); 2496827da44cSJohn Stultz } 2497827da44cSJohn Stultz 2498586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 2499296f96fcSRusty Russell 250097402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 25018e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 25028e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 2503e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 2504e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 250597402b96SHerbert Xu vi->big_packets = true; 250697402b96SHerbert Xu 25073f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 25083f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 25093f2c31d9SMark McLoughlin 2510d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 2511d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 2512012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2513012873d0SMichael S. Tsirkin else 2514012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 2515012873d0SMichael S. Tsirkin 251675993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 251775993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 2518e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 2519e7428e95SMichael S. Tsirkin 2520986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2521986a4f4dSJason Wang vi->has_cvq = true; 2522986a4f4dSJason Wang 252314de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 252414de9d11SAaron Conole mtu = virtio_cread16(vdev, 252514de9d11SAaron Conole offsetof(struct virtio_net_config, 252614de9d11SAaron Conole mtu)); 252793a205eeSAaron Conole if (mtu < dev->min_mtu) { 2528fe36cbe0SMichael S. Tsirkin /* Should never trigger: MTU was previously validated 2529fe36cbe0SMichael S. Tsirkin * in virtnet_validate. 2530fe36cbe0SMichael S. Tsirkin */ 2531fe36cbe0SMichael S. Tsirkin dev_err(&vdev->dev, "device MTU appears to have changed " 2532fe36cbe0SMichael S. Tsirkin "it is now %d < %d", mtu, dev->min_mtu); 2533fe36cbe0SMichael S. Tsirkin goto free_stats; 2534fe36cbe0SMichael S. Tsirkin } 2535fe36cbe0SMichael S. Tsirkin 2536d0c2c997SJarod Wilson dev->mtu = mtu; 253793a205eeSAaron Conole dev->max_mtu = mtu; 25382e123b44SMichael S. Tsirkin 25392e123b44SMichael S. Tsirkin /* TODO: size buffers correctly in this case. */ 25402e123b44SMichael S. Tsirkin if (dev->mtu > ETH_DATA_LEN) 25412e123b44SMichael S. Tsirkin vi->big_packets = true; 254214de9d11SAaron Conole } 254314de9d11SAaron Conole 2544012873d0SMichael S. Tsirkin if (vi->any_header_sg) 2545012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 25466ebbc1a6SZhangjie \(HZ\) 254744900010SJason Wang /* Enable multiqueue by default */ 254844900010SJason Wang if (num_online_cpus() >= max_queue_pairs) 254944900010SJason Wang vi->curr_queue_pairs = max_queue_pairs; 255044900010SJason Wang else 255144900010SJason Wang vi->curr_queue_pairs = num_online_cpus(); 2552986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 2553986a4f4dSJason Wang 2554986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 25553f9c10b0SAmit Shah err = init_vqs(vi); 2556d2a7dddaSMichael S. Tsirkin if (err) 25579bb8ca86SJason Wang goto free_stats; 2558d2a7dddaSMichael S. Tsirkin 2559fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2560fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 2561fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 2562fbf28d78SMichael Dalton #endif 25630f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 25640f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 2565986a4f4dSJason Wang 256616032be5SNikolay Aleksandrov virtnet_init_settings(dev); 256716032be5SNikolay Aleksandrov 2568296f96fcSRusty Russell err = register_netdev(dev); 2569296f96fcSRusty Russell if (err) { 2570296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 2571d2a7dddaSMichael S. Tsirkin goto free_vqs; 2572296f96fcSRusty Russell } 2573b3369c1fSRusty Russell 25744baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 25754baf1e33SMichael S. Tsirkin 25768017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 25778de4b2f3SWanlong Gao if (err) { 25788de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 2579f00e35e2Swangyunjian goto free_unregister_netdev; 25808de4b2f3SWanlong Gao } 25818de4b2f3SWanlong Gao 2582a220871bSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 258344900010SJason Wang 2584167c25e4SJason Wang /* Assume link up if device can't report link status, 2585167c25e4SJason Wang otherwise get link status from config. */ 2586167c25e4SJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 2587167c25e4SJason Wang netif_carrier_off(dev); 25883b07e9caSTejun Heo schedule_work(&vi->config_work); 2589167c25e4SJason Wang } else { 2590167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 25914783256eSPantelis Koukousoulas netif_carrier_on(dev); 2592167c25e4SJason Wang } 25939f4d26d0SMark McLoughlin 2594986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 2595986a4f4dSJason Wang dev->name, max_queue_pairs); 2596986a4f4dSJason Wang 2597296f96fcSRusty Russell return 0; 2598296f96fcSRusty Russell 2599f00e35e2Swangyunjian free_unregister_netdev: 260002465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 260102465555SMichael S. Tsirkin 2602b3369c1fSRusty Russell unregister_netdev(dev); 2603d2a7dddaSMichael S. Tsirkin free_vqs: 2604986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 2605fb51879dSMichael Dalton free_receive_page_frags(vi); 2606e9d7417bSJason Wang virtnet_del_vqs(vi); 26073fa2a1dfSstephen hemminger free_stats: 26083fa2a1dfSstephen hemminger free_percpu(vi->stats); 2609296f96fcSRusty Russell free: 2610296f96fcSRusty Russell free_netdev(dev); 2611296f96fcSRusty Russell return err; 2612296f96fcSRusty Russell } 2613296f96fcSRusty Russell 261404486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 2615296f96fcSRusty Russell { 261604486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 2617830a8a97SShirley Ma 2618830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 26199ab86bbcSShirley Ma free_unused_bufs(vi); 2620fb6813f4SRusty Russell 2621986a4f4dSJason Wang free_receive_bufs(vi); 2622d2a7dddaSMichael S. Tsirkin 2623fb51879dSMichael Dalton free_receive_page_frags(vi); 2624fb51879dSMichael Dalton 2625986a4f4dSJason Wang virtnet_del_vqs(vi); 262604486ed0SAmit Shah } 262704486ed0SAmit Shah 26288cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 262904486ed0SAmit Shah { 263004486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 263104486ed0SAmit Shah 26328017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 26338de4b2f3SWanlong Gao 2634102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 2635102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 2636586d17c5SJason Wang 263704486ed0SAmit Shah unregister_netdev(vi->dev); 263804486ed0SAmit Shah 263904486ed0SAmit Shah remove_vq_common(vi); 2640fb6813f4SRusty Russell 26412e66f55bSKrishna Kumar free_percpu(vi->stats); 264274b2553fSRusty Russell free_netdev(vi->dev); 2643296f96fcSRusty Russell } 2644296f96fcSRusty Russell 264589107000SAaron Lu #ifdef CONFIG_PM_SLEEP 26460741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev) 26470741bcb5SAmit Shah { 26480741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 26490741bcb5SAmit Shah 26508017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 26519fe7bfceSJohn Fastabend virtnet_freeze_down(vdev); 26520741bcb5SAmit Shah remove_vq_common(vi); 26530741bcb5SAmit Shah 26540741bcb5SAmit Shah return 0; 26550741bcb5SAmit Shah } 26560741bcb5SAmit Shah 26570741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev) 26580741bcb5SAmit Shah { 26590741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 26609fe7bfceSJohn Fastabend int err; 26610741bcb5SAmit Shah 26629fe7bfceSJohn Fastabend err = virtnet_restore_up(vdev); 26630741bcb5SAmit Shah if (err) 26640741bcb5SAmit Shah return err; 2665986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 2666986a4f4dSJason Wang 26678017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 2668ec9debbdSJason Wang if (err) 2669ec9debbdSJason Wang return err; 2670ec9debbdSJason Wang 26710741bcb5SAmit Shah return 0; 26720741bcb5SAmit Shah } 26730741bcb5SAmit Shah #endif 26740741bcb5SAmit Shah 2675296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 2676296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 2677296f96fcSRusty Russell { 0 }, 2678296f96fcSRusty Russell }; 2679296f96fcSRusty Russell 2680f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \ 2681f3358507SMichael S. Tsirkin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 2682f3358507SMichael S. Tsirkin VIRTIO_NET_F_MAC, \ 2683f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 2684f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 2685f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 2686f3358507SMichael S. Tsirkin VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 2687f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 2688f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 2689f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_MAC_ADDR, \ 2690f3358507SMichael S. Tsirkin VIRTIO_NET_F_MTU 2691f3358507SMichael S. Tsirkin 2692c45a6816SRusty Russell static unsigned int features[] = { 2693f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 2694f3358507SMichael S. Tsirkin }; 2695f3358507SMichael S. Tsirkin 2696f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = { 2697f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 2698f3358507SMichael S. Tsirkin VIRTIO_NET_F_GSO, 2699e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 2700c45a6816SRusty Russell }; 2701c45a6816SRusty Russell 270222402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 2703c45a6816SRusty Russell .feature_table = features, 2704c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 2705f3358507SMichael S. Tsirkin .feature_table_legacy = features_legacy, 2706f3358507SMichael S. Tsirkin .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 2707296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 2708296f96fcSRusty Russell .driver.owner = THIS_MODULE, 2709296f96fcSRusty Russell .id_table = id_table, 2710fe36cbe0SMichael S. Tsirkin .validate = virtnet_validate, 2711296f96fcSRusty Russell .probe = virtnet_probe, 27128cc085d6SBill Pemberton .remove = virtnet_remove, 27139f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 271489107000SAaron Lu #ifdef CONFIG_PM_SLEEP 27150741bcb5SAmit Shah .freeze = virtnet_freeze, 27160741bcb5SAmit Shah .restore = virtnet_restore, 27170741bcb5SAmit Shah #endif 2718296f96fcSRusty Russell }; 2719296f96fcSRusty Russell 27208017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void) 27218017c279SSebastian Andrzej Siewior { 27228017c279SSebastian Andrzej Siewior int ret; 27238017c279SSebastian Andrzej Siewior 272473c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 27258017c279SSebastian Andrzej Siewior virtnet_cpu_online, 27268017c279SSebastian Andrzej Siewior virtnet_cpu_down_prep); 27278017c279SSebastian Andrzej Siewior if (ret < 0) 27288017c279SSebastian Andrzej Siewior goto out; 27298017c279SSebastian Andrzej Siewior virtionet_online = ret; 273073c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 27318017c279SSebastian Andrzej Siewior NULL, virtnet_cpu_dead); 27328017c279SSebastian Andrzej Siewior if (ret) 27338017c279SSebastian Andrzej Siewior goto err_dead; 27348017c279SSebastian Andrzej Siewior 27358017c279SSebastian Andrzej Siewior ret = register_virtio_driver(&virtio_net_driver); 27368017c279SSebastian Andrzej Siewior if (ret) 27378017c279SSebastian Andrzej Siewior goto err_virtio; 27388017c279SSebastian Andrzej Siewior return 0; 27398017c279SSebastian Andrzej Siewior err_virtio: 27408017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 27418017c279SSebastian Andrzej Siewior err_dead: 27428017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 27438017c279SSebastian Andrzej Siewior out: 27448017c279SSebastian Andrzej Siewior return ret; 27458017c279SSebastian Andrzej Siewior } 27468017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init); 27478017c279SSebastian Andrzej Siewior 27488017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void) 27498017c279SSebastian Andrzej Siewior { 27508017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 27518017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 27528017c279SSebastian Andrzej Siewior unregister_virtio_driver(&virtio_net_driver); 27538017c279SSebastian Andrzej Siewior } 27548017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit); 2755296f96fcSRusty Russell 2756296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 2757296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 2758296f96fcSRusty Russell MODULE_LICENSE("GPL"); 2759