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> 32186b3c99SJason Wang #include <linux/filter.h> 33ba5e4426SSridhar Samudrala #include <linux/netdevice.h> 34ba5e4426SSridhar Samudrala #include <linux/pci.h> 35d85b758fSMichael S. Tsirkin #include <net/route.h> 36754b8a21SJesper Dangaard Brouer #include <net/xdp.h> 37ba5e4426SSridhar Samudrala #include <net/net_failover.h> 38296f96fcSRusty Russell 39d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT; 406c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 416c0cd7c0SDor Laor 42b92f1e67SWillem de Bruijn static bool csum = true, gso = true, napi_tx; 4334a48579SRusty Russell module_param(csum, bool, 0444); 4434a48579SRusty Russell module_param(gso, bool, 0444); 45b92f1e67SWillem de Bruijn module_param(napi_tx, bool, 0644); 4634a48579SRusty Russell 47296f96fcSRusty Russell /* FIXME: MTU in config. */ 485061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 493f2c31d9SMark McLoughlin #define GOOD_COPY_LEN 128 50296f96fcSRusty Russell 51f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 52f6b10209SJason Wang 532de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ 542de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256 552de2f7f4SJohn Fastabend 562471c75eSJesper Dangaard Brouer /* Separating two types of XDP xmit */ 572471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_TX BIT(0) 582471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_REDIR BIT(1) 592471c75eSJesper Dangaard Brouer 605377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet 615377d758SJohannes Berg * buffer size when refilling RX rings. As the entire RX ring may be refilled 625377d758SJohannes Berg * at once, the weight is chosen so that the EWMA will be insensitive to short- 635377d758SJohannes Berg * term, transient changes in packet size. 64ab7db917SMichael Dalton */ 65eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64) 66ab7db917SMichael Dalton 6766846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0" 682a41f71dSAlex Williamson 697acd4329SColin Ian King static const unsigned long guest_offloads[] = { 707acd4329SColin Ian King VIRTIO_NET_F_GUEST_TSO4, 713f93522fSJason Wang VIRTIO_NET_F_GUEST_TSO6, 723f93522fSJason Wang VIRTIO_NET_F_GUEST_ECN, 737acd4329SColin Ian King VIRTIO_NET_F_GUEST_UFO 747acd4329SColin Ian King }; 753f93522fSJason Wang 76d7dfc5cfSToshiaki Makita struct virtnet_stat_desc { 77d7dfc5cfSToshiaki Makita char desc[ETH_GSTRING_LEN]; 78d7dfc5cfSToshiaki Makita size_t offset; 793fa2a1dfSstephen hemminger }; 803fa2a1dfSstephen hemminger 81d7dfc5cfSToshiaki Makita struct virtnet_sq_stats { 82d7dfc5cfSToshiaki Makita struct u64_stats_sync syncp; 83d7dfc5cfSToshiaki Makita u64 packets; 84d7dfc5cfSToshiaki Makita u64 bytes; 855b8f3c8dSToshiaki Makita u64 xdp_tx; 865b8f3c8dSToshiaki Makita u64 xdp_tx_drops; 87461f03dcSToshiaki Makita u64 kicks; 88d7dfc5cfSToshiaki Makita }; 89d7dfc5cfSToshiaki Makita 90d46eeeafSJason Wang struct virtnet_rq_stats { 91d46eeeafSJason Wang struct u64_stats_sync syncp; 92d7dfc5cfSToshiaki Makita u64 packets; 93d7dfc5cfSToshiaki Makita u64 bytes; 942c4a2f7dSToshiaki Makita u64 drops; 955b8f3c8dSToshiaki Makita u64 xdp_packets; 965b8f3c8dSToshiaki Makita u64 xdp_tx; 975b8f3c8dSToshiaki Makita u64 xdp_redirects; 985b8f3c8dSToshiaki Makita u64 xdp_drops; 99461f03dcSToshiaki Makita u64 kicks; 100d7dfc5cfSToshiaki Makita }; 101d7dfc5cfSToshiaki Makita 102d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m) 103d46eeeafSJason Wang #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m) 104d7dfc5cfSToshiaki Makita 105d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = { 106d7dfc5cfSToshiaki Makita { "packets", VIRTNET_SQ_STAT(packets) }, 107d7dfc5cfSToshiaki Makita { "bytes", VIRTNET_SQ_STAT(bytes) }, 1085b8f3c8dSToshiaki Makita { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) }, 1095b8f3c8dSToshiaki Makita { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) }, 110461f03dcSToshiaki Makita { "kicks", VIRTNET_SQ_STAT(kicks) }, 111d7dfc5cfSToshiaki Makita }; 112d7dfc5cfSToshiaki Makita 113d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { 114d7dfc5cfSToshiaki Makita { "packets", VIRTNET_RQ_STAT(packets) }, 115d7dfc5cfSToshiaki Makita { "bytes", VIRTNET_RQ_STAT(bytes) }, 1162c4a2f7dSToshiaki Makita { "drops", VIRTNET_RQ_STAT(drops) }, 1175b8f3c8dSToshiaki Makita { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) }, 1185b8f3c8dSToshiaki Makita { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) }, 1195b8f3c8dSToshiaki Makita { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) }, 1205b8f3c8dSToshiaki Makita { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) }, 121461f03dcSToshiaki Makita { "kicks", VIRTNET_RQ_STAT(kicks) }, 122d7dfc5cfSToshiaki Makita }; 123d7dfc5cfSToshiaki Makita 124d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) 125d7dfc5cfSToshiaki Makita #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc) 126d7dfc5cfSToshiaki Makita 127e9d7417bSJason Wang /* Internal representation of a send virtqueue */ 128e9d7417bSJason Wang struct send_queue { 129e9d7417bSJason Wang /* Virtqueue associated with this send _queue */ 130e9d7417bSJason Wang struct virtqueue *vq; 131e9d7417bSJason Wang 132e9d7417bSJason Wang /* TX: fragments + linear part + virtio header */ 133e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 134986a4f4dSJason Wang 135986a4f4dSJason Wang /* Name of the send queue: output.$index */ 136986a4f4dSJason Wang char name[40]; 137b92f1e67SWillem de Bruijn 138d7dfc5cfSToshiaki Makita struct virtnet_sq_stats stats; 139d7dfc5cfSToshiaki Makita 140b92f1e67SWillem de Bruijn struct napi_struct napi; 141e9d7417bSJason Wang }; 142e9d7417bSJason Wang 143e9d7417bSJason Wang /* Internal representation of a receive virtqueue */ 144e9d7417bSJason Wang struct receive_queue { 145e9d7417bSJason Wang /* Virtqueue associated with this receive_queue */ 146e9d7417bSJason Wang struct virtqueue *vq; 147e9d7417bSJason Wang 148296f96fcSRusty Russell struct napi_struct napi; 149296f96fcSRusty Russell 150f600b690SJohn Fastabend struct bpf_prog __rcu *xdp_prog; 151f600b690SJohn Fastabend 152d7dfc5cfSToshiaki Makita struct virtnet_rq_stats stats; 153d7dfc5cfSToshiaki Makita 154e9d7417bSJason Wang /* Chain pages by the private ptr. */ 155e9d7417bSJason Wang struct page *pages; 156e9d7417bSJason Wang 157ab7db917SMichael Dalton /* Average packet length for mergeable receive buffers. */ 1585377d758SJohannes Berg struct ewma_pkt_len mrg_avg_pkt_len; 159ab7db917SMichael Dalton 160fb51879dSMichael Dalton /* Page frag for packet buffer allocation. */ 161fb51879dSMichael Dalton struct page_frag alloc_frag; 162fb51879dSMichael Dalton 163e9d7417bSJason Wang /* RX: fragments + linear part + virtio header */ 164e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 165986a4f4dSJason Wang 166d85b758fSMichael S. Tsirkin /* Min single buffer size for mergeable buffers case. */ 167d85b758fSMichael S. Tsirkin unsigned int min_buf_len; 168d85b758fSMichael S. Tsirkin 169986a4f4dSJason Wang /* Name of this receive queue: input.$index */ 170986a4f4dSJason Wang char name[40]; 171754b8a21SJesper Dangaard Brouer 172754b8a21SJesper Dangaard Brouer struct xdp_rxq_info xdp_rxq; 173e9d7417bSJason Wang }; 174e9d7417bSJason Wang 17512e57169SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */ 17612e57169SMichael S. Tsirkin struct control_buf { 17712e57169SMichael S. Tsirkin struct virtio_net_ctrl_hdr hdr; 17812e57169SMichael S. Tsirkin virtio_net_ctrl_ack status; 17912e57169SMichael S. Tsirkin struct virtio_net_ctrl_mq mq; 18012e57169SMichael S. Tsirkin u8 promisc; 18112e57169SMichael S. Tsirkin u8 allmulti; 182d7fad4c8SMichael S. Tsirkin __virtio16 vid; 183f4ee703aSMichael S. Tsirkin __virtio64 offloads; 18412e57169SMichael S. Tsirkin }; 18512e57169SMichael S. Tsirkin 186e9d7417bSJason Wang struct virtnet_info { 187e9d7417bSJason Wang struct virtio_device *vdev; 188e9d7417bSJason Wang struct virtqueue *cvq; 189e9d7417bSJason Wang struct net_device *dev; 190986a4f4dSJason Wang struct send_queue *sq; 191986a4f4dSJason Wang struct receive_queue *rq; 192e9d7417bSJason Wang unsigned int status; 193e9d7417bSJason Wang 194986a4f4dSJason Wang /* Max # of queue pairs supported by the device */ 195986a4f4dSJason Wang u16 max_queue_pairs; 196986a4f4dSJason Wang 197986a4f4dSJason Wang /* # of queue pairs currently used by the driver */ 198986a4f4dSJason Wang u16 curr_queue_pairs; 199986a4f4dSJason Wang 200672aafd5SJohn Fastabend /* # of XDP queue pairs currently used by the driver */ 201672aafd5SJohn Fastabend u16 xdp_queue_pairs; 202672aafd5SJohn Fastabend 20397402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 20497402b96SHerbert Xu bool big_packets; 20597402b96SHerbert Xu 2063f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 2073f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 2083f2c31d9SMark McLoughlin 209986a4f4dSJason Wang /* Has control virtqueue */ 210986a4f4dSJason Wang bool has_cvq; 211986a4f4dSJason Wang 212e7428e95SMichael S. Tsirkin /* Host can handle any s/g split between our header and packet data */ 213e7428e95SMichael S. Tsirkin bool any_header_sg; 214e7428e95SMichael S. Tsirkin 215012873d0SMichael S. Tsirkin /* Packet virtio header size */ 216012873d0SMichael S. Tsirkin u8 hdr_len; 217012873d0SMichael S. Tsirkin 2183161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 2193161e453SRusty Russell struct delayed_work refill; 2203161e453SRusty Russell 221586d17c5SJason Wang /* Work struct for config space updates */ 222586d17c5SJason Wang struct work_struct config_work; 223586d17c5SJason Wang 224986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 225986a4f4dSJason Wang bool affinity_hint_set; 22647be2479SWanlong Gao 2278017c279SSebastian Andrzej Siewior /* CPU hotplug instances for online & dead */ 2288017c279SSebastian Andrzej Siewior struct hlist_node node; 2298017c279SSebastian Andrzej Siewior struct hlist_node node_dead; 2302ac46030SMichael S. Tsirkin 23112e57169SMichael S. Tsirkin struct control_buf *ctrl; 23216032be5SNikolay Aleksandrov 23316032be5SNikolay Aleksandrov /* Ethtool settings */ 23416032be5SNikolay Aleksandrov u8 duplex; 23516032be5SNikolay Aleksandrov u32 speed; 2363f93522fSJason Wang 2373f93522fSJason Wang unsigned long guest_offloads; 238ba5e4426SSridhar Samudrala 239ba5e4426SSridhar Samudrala /* failover when STANDBY feature enabled */ 240ba5e4426SSridhar Samudrala struct failover *failover; 241296f96fcSRusty Russell }; 242296f96fcSRusty Russell 2439ab86bbcSShirley Ma struct padded_vnet_hdr { 244012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr; 2459ab86bbcSShirley Ma /* 246012873d0SMichael S. Tsirkin * hdr is in a separate sg buffer, and data sg buffer shares same page 247012873d0SMichael S. Tsirkin * with this header sg. This padding makes next sg 16 byte aligned 248012873d0SMichael S. Tsirkin * after the header. 2499ab86bbcSShirley Ma */ 250012873d0SMichael S. Tsirkin char padding[4]; 2519ab86bbcSShirley Ma }; 2529ab86bbcSShirley Ma 253986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 254986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 255986a4f4dSJason Wang */ 256986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 257986a4f4dSJason Wang { 2589d0ca6edSRusty Russell return (vq->index - 1) / 2; 259986a4f4dSJason Wang } 260986a4f4dSJason Wang 261986a4f4dSJason Wang static int txq2vq(int txq) 262986a4f4dSJason Wang { 263986a4f4dSJason Wang return txq * 2 + 1; 264986a4f4dSJason Wang } 265986a4f4dSJason Wang 266986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 267986a4f4dSJason Wang { 2689d0ca6edSRusty Russell return vq->index / 2; 269986a4f4dSJason Wang } 270986a4f4dSJason Wang 271986a4f4dSJason Wang static int rxq2vq(int rxq) 272986a4f4dSJason Wang { 273986a4f4dSJason Wang return rxq * 2; 274986a4f4dSJason Wang } 275986a4f4dSJason Wang 276012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 277296f96fcSRusty Russell { 278012873d0SMichael S. Tsirkin return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 279296f96fcSRusty Russell } 280296f96fcSRusty Russell 2819ab86bbcSShirley Ma /* 2829ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 2839ab86bbcSShirley Ma * most recent used list in the beginning for reuse 2849ab86bbcSShirley Ma */ 285e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 286fb6813f4SRusty Russell { 2879ab86bbcSShirley Ma struct page *end; 2889ab86bbcSShirley Ma 289e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 2909ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 291e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 292e9d7417bSJason Wang rq->pages = page; 293fb6813f4SRusty Russell } 294fb6813f4SRusty Russell 295e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 296fb6813f4SRusty Russell { 297e9d7417bSJason Wang struct page *p = rq->pages; 298fb6813f4SRusty Russell 2999ab86bbcSShirley Ma if (p) { 300e9d7417bSJason Wang rq->pages = (struct page *)p->private; 3019ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 3029ab86bbcSShirley Ma p->private = 0; 3039ab86bbcSShirley Ma } else 304fb6813f4SRusty Russell p = alloc_page(gfp_mask); 305fb6813f4SRusty Russell return p; 306fb6813f4SRusty Russell } 307fb6813f4SRusty Russell 308e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi, 309e4e8452aSWillem de Bruijn struct virtqueue *vq) 310e4e8452aSWillem de Bruijn { 311e4e8452aSWillem de Bruijn if (napi_schedule_prep(napi)) { 312e4e8452aSWillem de Bruijn virtqueue_disable_cb(vq); 313e4e8452aSWillem de Bruijn __napi_schedule(napi); 314e4e8452aSWillem de Bruijn } 315e4e8452aSWillem de Bruijn } 316e4e8452aSWillem de Bruijn 317e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi, 318e4e8452aSWillem de Bruijn struct virtqueue *vq, int processed) 319e4e8452aSWillem de Bruijn { 320e4e8452aSWillem de Bruijn int opaque; 321e4e8452aSWillem de Bruijn 322e4e8452aSWillem de Bruijn opaque = virtqueue_enable_cb_prepare(vq); 323fdaa767aSToshiaki Makita if (napi_complete_done(napi, processed)) { 324fdaa767aSToshiaki Makita if (unlikely(virtqueue_poll(vq, opaque))) 325e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 326fdaa767aSToshiaki Makita } else { 327fdaa767aSToshiaki Makita virtqueue_disable_cb(vq); 328fdaa767aSToshiaki Makita } 329e4e8452aSWillem de Bruijn } 330e4e8452aSWillem de Bruijn 331e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 332296f96fcSRusty Russell { 333e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 334b92f1e67SWillem de Bruijn struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 335296f96fcSRusty Russell 3362cb9c6baSRusty Russell /* Suppress further interrupts. */ 337e9d7417bSJason Wang virtqueue_disable_cb(vq); 33811a3a154SRusty Russell 339b92f1e67SWillem de Bruijn if (napi->weight) 340b92f1e67SWillem de Bruijn virtqueue_napi_schedule(napi, vq); 341b92f1e67SWillem de Bruijn else 342363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 343986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 344296f96fcSRusty Russell } 345296f96fcSRusty Russell 34628b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22 34728b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize, 34828b39bc7SJason Wang unsigned int headroom) 34928b39bc7SJason Wang { 35028b39bc7SJason Wang return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 35128b39bc7SJason Wang } 35228b39bc7SJason Wang 35328b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 35428b39bc7SJason Wang { 35528b39bc7SJason Wang return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 35628b39bc7SJason Wang } 35728b39bc7SJason Wang 35828b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 35928b39bc7SJason Wang { 36028b39bc7SJason Wang return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 36128b39bc7SJason Wang } 36228b39bc7SJason Wang 3633464645aSMike Waychison /* Called from bottom half context */ 364946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 365946fa564SMichael S. Tsirkin struct receive_queue *rq, 3662613af0eSMichael Dalton struct page *page, unsigned int offset, 3672613af0eSMichael Dalton unsigned int len, unsigned int truesize) 3689ab86bbcSShirley Ma { 3699ab86bbcSShirley Ma struct sk_buff *skb; 370012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 3712613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 3729ab86bbcSShirley Ma char *p; 3739ab86bbcSShirley Ma 3742613af0eSMichael Dalton p = page_address(page) + offset; 3759ab86bbcSShirley Ma 3769ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 377c67f5db8SPaolo Abeni skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 3789ab86bbcSShirley Ma if (unlikely(!skb)) 3799ab86bbcSShirley Ma return NULL; 3809ab86bbcSShirley Ma 3819ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 3829ab86bbcSShirley Ma 383012873d0SMichael S. Tsirkin hdr_len = vi->hdr_len; 384012873d0SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 385a4a76503Sstephen hemminger hdr_padded_len = sizeof(*hdr); 386012873d0SMichael S. Tsirkin else 3872613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 3883f2c31d9SMark McLoughlin 3899ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 3903f2c31d9SMark McLoughlin 3919ab86bbcSShirley Ma len -= hdr_len; 3922613af0eSMichael Dalton offset += hdr_padded_len; 3932613af0eSMichael Dalton p += hdr_padded_len; 3943f2c31d9SMark McLoughlin 3953f2c31d9SMark McLoughlin copy = len; 3963f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 3973f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 39859ae1d12SJohannes Berg skb_put_data(skb, p, copy); 3993f2c31d9SMark McLoughlin 4003f2c31d9SMark McLoughlin len -= copy; 4019ab86bbcSShirley Ma offset += copy; 4023f2c31d9SMark McLoughlin 4032613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 4042613af0eSMichael Dalton if (len) 4052613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 4062613af0eSMichael Dalton else 4072613af0eSMichael Dalton put_page(page); 4082613af0eSMichael Dalton return skb; 4092613af0eSMichael Dalton } 4102613af0eSMichael Dalton 411e878d78bSSasha Levin /* 412e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 413e878d78bSSasha Levin * This is here to handle cases when the device erroneously 414e878d78bSSasha Levin * tries to receive more than is possible. This is usually 415e878d78bSSasha Levin * the case of a broken device. 416e878d78bSSasha Levin */ 417e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 418be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 419e878d78bSSasha Levin dev_kfree_skb(skb); 420e878d78bSSasha Levin return NULL; 421e878d78bSSasha Levin } 4222613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 4239ab86bbcSShirley Ma while (len) { 4242613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 4252613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 4262613af0eSMichael Dalton frag_size, truesize); 4272613af0eSMichael Dalton len -= frag_size; 4289ab86bbcSShirley Ma page = (struct page *)page->private; 4299ab86bbcSShirley Ma offset = 0; 4303f2c31d9SMark McLoughlin } 4313f2c31d9SMark McLoughlin 4329ab86bbcSShirley Ma if (page) 433e9d7417bSJason Wang give_pages(rq, page); 4343f2c31d9SMark McLoughlin 4359ab86bbcSShirley Ma return skb; 4369ab86bbcSShirley Ma } 4379ab86bbcSShirley Ma 438735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, 439735fc405SJesper Dangaard Brouer struct send_queue *sq, 44044fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf) 44156434a01SJohn Fastabend { 44256434a01SJohn Fastabend struct virtio_net_hdr_mrg_rxbuf *hdr; 44356434a01SJohn Fastabend int err; 44456434a01SJohn Fastabend 445cac320c8SJesper Dangaard Brouer /* virtqueue want to use data area in-front of packet */ 446cac320c8SJesper Dangaard Brouer if (unlikely(xdpf->metasize > 0)) 447cac320c8SJesper Dangaard Brouer return -EOPNOTSUPP; 448cac320c8SJesper Dangaard Brouer 449cac320c8SJesper Dangaard Brouer if (unlikely(xdpf->headroom < vi->hdr_len)) 450cac320c8SJesper Dangaard Brouer return -EOVERFLOW; 451cac320c8SJesper Dangaard Brouer 452cac320c8SJesper Dangaard Brouer /* Make room for virtqueue hdr (also change xdpf->headroom?) */ 453cac320c8SJesper Dangaard Brouer xdpf->data -= vi->hdr_len; 45456434a01SJohn Fastabend /* Zero header and leave csum up to XDP layers */ 455cac320c8SJesper Dangaard Brouer hdr = xdpf->data; 45656434a01SJohn Fastabend memset(hdr, 0, vi->hdr_len); 457cac320c8SJesper Dangaard Brouer xdpf->len += vi->hdr_len; 45856434a01SJohn Fastabend 459cac320c8SJesper Dangaard Brouer sg_init_one(sq->sg, xdpf->data, xdpf->len); 460bb91accfSJason Wang 461cac320c8SJesper Dangaard Brouer err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdpf, GFP_ATOMIC); 46211b7d897SJesper Dangaard Brouer if (unlikely(err)) 463cac320c8SJesper Dangaard Brouer return -ENOSPC; /* Caller handle free/refcnt */ 46456434a01SJohn Fastabend 465cac320c8SJesper Dangaard Brouer return 0; 46656434a01SJohn Fastabend } 46756434a01SJohn Fastabend 4682a43565cSToshiaki Makita static struct send_queue *virtnet_xdp_sq(struct virtnet_info *vi) 4692a43565cSToshiaki Makita { 4702a43565cSToshiaki Makita unsigned int qp; 4712a43565cSToshiaki Makita 4722a43565cSToshiaki Makita qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); 4732a43565cSToshiaki Makita return &vi->sq[qp]; 4742a43565cSToshiaki Makita } 4752a43565cSToshiaki Makita 476735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev, 47742b33468SJesper Dangaard Brouer int n, struct xdp_frame **frames, u32 flags) 478186b3c99SJason Wang { 479186b3c99SJason Wang struct virtnet_info *vi = netdev_priv(dev); 4808dcc5b0aSJesper Dangaard Brouer struct receive_queue *rq = vi->rq; 481735fc405SJesper Dangaard Brouer struct xdp_frame *xdpf_sent; 4828dcc5b0aSJesper Dangaard Brouer struct bpf_prog *xdp_prog; 483735fc405SJesper Dangaard Brouer struct send_queue *sq; 484735fc405SJesper Dangaard Brouer unsigned int len; 485735fc405SJesper Dangaard Brouer int drops = 0; 486461f03dcSToshiaki Makita int kicks = 0; 4875b8f3c8dSToshiaki Makita int ret, err; 488735fc405SJesper Dangaard Brouer int i; 489735fc405SJesper Dangaard Brouer 4902a43565cSToshiaki Makita sq = virtnet_xdp_sq(vi); 491186b3c99SJason Wang 4925b8f3c8dSToshiaki Makita if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { 4935b8f3c8dSToshiaki Makita ret = -EINVAL; 4945b8f3c8dSToshiaki Makita drops = n; 4955b8f3c8dSToshiaki Makita goto out; 4965b8f3c8dSToshiaki Makita } 4975b8f3c8dSToshiaki Makita 4988dcc5b0aSJesper Dangaard Brouer /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 4998dcc5b0aSJesper Dangaard Brouer * indicate XDP resources have been successfully allocated. 5008dcc5b0aSJesper Dangaard Brouer */ 5018dcc5b0aSJesper Dangaard Brouer xdp_prog = rcu_dereference(rq->xdp_prog); 5025b8f3c8dSToshiaki Makita if (!xdp_prog) { 5035b8f3c8dSToshiaki Makita ret = -ENXIO; 5045b8f3c8dSToshiaki Makita drops = n; 5055b8f3c8dSToshiaki Makita goto out; 5065b8f3c8dSToshiaki Makita } 5078dcc5b0aSJesper Dangaard Brouer 508735fc405SJesper Dangaard Brouer /* Free up any pending old buffers before queueing new ones. */ 509735fc405SJesper Dangaard Brouer while ((xdpf_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) 510735fc405SJesper Dangaard Brouer xdp_return_frame(xdpf_sent); 511735fc405SJesper Dangaard Brouer 512735fc405SJesper Dangaard Brouer for (i = 0; i < n; i++) { 513735fc405SJesper Dangaard Brouer struct xdp_frame *xdpf = frames[i]; 514735fc405SJesper Dangaard Brouer 515735fc405SJesper Dangaard Brouer err = __virtnet_xdp_xmit_one(vi, sq, xdpf); 516735fc405SJesper Dangaard Brouer if (err) { 517735fc405SJesper Dangaard Brouer xdp_return_frame_rx_napi(xdpf); 518735fc405SJesper Dangaard Brouer drops++; 519735fc405SJesper Dangaard Brouer } 520735fc405SJesper Dangaard Brouer } 5215b8f3c8dSToshiaki Makita ret = n - drops; 5225d274cb4SJesper Dangaard Brouer 523461f03dcSToshiaki Makita if (flags & XDP_XMIT_FLUSH) { 524461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) 525461f03dcSToshiaki Makita kicks = 1; 526461f03dcSToshiaki Makita } 5275b8f3c8dSToshiaki Makita out: 5285b8f3c8dSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 5295b8f3c8dSToshiaki Makita sq->stats.xdp_tx += n; 5305b8f3c8dSToshiaki Makita sq->stats.xdp_tx_drops += drops; 531461f03dcSToshiaki Makita sq->stats.kicks += kicks; 5325b8f3c8dSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 5335d274cb4SJesper Dangaard Brouer 5345b8f3c8dSToshiaki Makita return ret; 535186b3c99SJason Wang } 536186b3c99SJason Wang 537f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 538f6b10209SJason Wang { 539f6b10209SJason Wang return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0; 540f6b10209SJason Wang } 541f6b10209SJason Wang 5424941d472SJason Wang /* We copy the packet for XDP in the following cases: 5434941d472SJason Wang * 5444941d472SJason Wang * 1) Packet is scattered across multiple rx buffers. 5454941d472SJason Wang * 2) Headroom space is insufficient. 5464941d472SJason Wang * 5474941d472SJason Wang * This is inefficient but it's a temporary condition that 5484941d472SJason Wang * we hit right after XDP is enabled and until queue is refilled 5494941d472SJason Wang * with large buffers with sufficient headroom - so it should affect 5504941d472SJason Wang * at most queue size packets. 5514941d472SJason Wang * Afterwards, the conditions to enable 5524941d472SJason Wang * XDP should preclude the underlying device from sending packets 5534941d472SJason Wang * across multiple buffers (num_buf > 1), and we make sure buffers 5544941d472SJason Wang * have enough headroom. 55572979a6cSJohn Fastabend */ 55672979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq, 55756a86f84SJason Wang u16 *num_buf, 55872979a6cSJohn Fastabend struct page *p, 55972979a6cSJohn Fastabend int offset, 5604941d472SJason Wang int page_off, 56172979a6cSJohn Fastabend unsigned int *len) 56272979a6cSJohn Fastabend { 56372979a6cSJohn Fastabend struct page *page = alloc_page(GFP_ATOMIC); 56472979a6cSJohn Fastabend 56572979a6cSJohn Fastabend if (!page) 56672979a6cSJohn Fastabend return NULL; 56772979a6cSJohn Fastabend 56872979a6cSJohn Fastabend memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 56972979a6cSJohn Fastabend page_off += *len; 57072979a6cSJohn Fastabend 57156a86f84SJason Wang while (--*num_buf) { 5723cc81a9aSJason Wang int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 57372979a6cSJohn Fastabend unsigned int buflen; 57472979a6cSJohn Fastabend void *buf; 57572979a6cSJohn Fastabend int off; 57672979a6cSJohn Fastabend 577680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &buflen); 578680557cfSMichael S. Tsirkin if (unlikely(!buf)) 57972979a6cSJohn Fastabend goto err_buf; 58072979a6cSJohn Fastabend 58172979a6cSJohn Fastabend p = virt_to_head_page(buf); 58272979a6cSJohn Fastabend off = buf - page_address(p); 58372979a6cSJohn Fastabend 58456a86f84SJason Wang /* guard against a misconfigured or uncooperative backend that 58556a86f84SJason Wang * is sending packet larger than the MTU. 58656a86f84SJason Wang */ 5873cc81a9aSJason Wang if ((page_off + buflen + tailroom) > PAGE_SIZE) { 58856a86f84SJason Wang put_page(p); 58956a86f84SJason Wang goto err_buf; 59056a86f84SJason Wang } 59156a86f84SJason Wang 59272979a6cSJohn Fastabend memcpy(page_address(page) + page_off, 59372979a6cSJohn Fastabend page_address(p) + off, buflen); 59472979a6cSJohn Fastabend page_off += buflen; 59556a86f84SJason Wang put_page(p); 59672979a6cSJohn Fastabend } 59772979a6cSJohn Fastabend 5982de2f7f4SJohn Fastabend /* Headroom does not contribute to packet length */ 5992de2f7f4SJohn Fastabend *len = page_off - VIRTIO_XDP_HEADROOM; 60072979a6cSJohn Fastabend return page; 60172979a6cSJohn Fastabend err_buf: 60272979a6cSJohn Fastabend __free_pages(page, 0); 60372979a6cSJohn Fastabend return NULL; 60472979a6cSJohn Fastabend } 60572979a6cSJohn Fastabend 6064941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev, 6074941d472SJason Wang struct virtnet_info *vi, 6084941d472SJason Wang struct receive_queue *rq, 6094941d472SJason Wang void *buf, void *ctx, 610186b3c99SJason Wang unsigned int len, 6117d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 612d46eeeafSJason Wang struct virtnet_rq_stats *stats) 6134941d472SJason Wang { 6144941d472SJason Wang struct sk_buff *skb; 6154941d472SJason Wang struct bpf_prog *xdp_prog; 6164941d472SJason Wang unsigned int xdp_headroom = (unsigned long)ctx; 6174941d472SJason Wang unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 6184941d472SJason Wang unsigned int headroom = vi->hdr_len + header_offset; 6194941d472SJason Wang unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 6204941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 6214941d472SJason Wang struct page *page = virt_to_head_page(buf); 62211b7d897SJesper Dangaard Brouer unsigned int delta = 0; 6234941d472SJason Wang struct page *xdp_page; 62411b7d897SJesper Dangaard Brouer int err; 62511b7d897SJesper Dangaard Brouer 6264941d472SJason Wang len -= vi->hdr_len; 627d46eeeafSJason Wang stats->bytes += len; 6284941d472SJason Wang 6294941d472SJason Wang rcu_read_lock(); 6304941d472SJason Wang xdp_prog = rcu_dereference(rq->xdp_prog); 6314941d472SJason Wang if (xdp_prog) { 6324941d472SJason Wang struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 63344fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 6344941d472SJason Wang struct xdp_buff xdp; 6354941d472SJason Wang void *orig_data; 6364941d472SJason Wang u32 act; 6374941d472SJason Wang 63895dbe9e7SJesper Dangaard Brouer if (unlikely(hdr->hdr.gso_type)) 6394941d472SJason Wang goto err_xdp; 6404941d472SJason Wang 6414941d472SJason Wang if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 6424941d472SJason Wang int offset = buf - page_address(page) + header_offset; 6434941d472SJason Wang unsigned int tlen = len + vi->hdr_len; 6444941d472SJason Wang u16 num_buf = 1; 6454941d472SJason Wang 6464941d472SJason Wang xdp_headroom = virtnet_get_headroom(vi); 6474941d472SJason Wang header_offset = VIRTNET_RX_PAD + xdp_headroom; 6484941d472SJason Wang headroom = vi->hdr_len + header_offset; 6494941d472SJason Wang buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 6504941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 6514941d472SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, page, 6524941d472SJason Wang offset, header_offset, 6534941d472SJason Wang &tlen); 6544941d472SJason Wang if (!xdp_page) 6554941d472SJason Wang goto err_xdp; 6564941d472SJason Wang 6574941d472SJason Wang buf = page_address(xdp_page); 6584941d472SJason Wang put_page(page); 6594941d472SJason Wang page = xdp_page; 6604941d472SJason Wang } 6614941d472SJason Wang 6624941d472SJason Wang xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len; 6634941d472SJason Wang xdp.data = xdp.data_hard_start + xdp_headroom; 664de8f3a83SDaniel Borkmann xdp_set_data_meta_invalid(&xdp); 6654941d472SJason Wang xdp.data_end = xdp.data + len; 666754b8a21SJesper Dangaard Brouer xdp.rxq = &rq->xdp_rxq; 6674941d472SJason Wang orig_data = xdp.data; 6684941d472SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 669d46eeeafSJason Wang stats->xdp_packets++; 6704941d472SJason Wang 6714941d472SJason Wang switch (act) { 6724941d472SJason Wang case XDP_PASS: 6734941d472SJason Wang /* Recalculate length in case bpf program changed it */ 6744941d472SJason Wang delta = orig_data - xdp.data; 6756870de43SNikita V. Shirokov len = xdp.data_end - xdp.data; 6764941d472SJason Wang break; 6774941d472SJason Wang case XDP_TX: 678d46eeeafSJason Wang stats->xdp_tx++; 67944fa2dbdSJesper Dangaard Brouer xdpf = convert_to_xdp_frame(&xdp); 68044fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 68144fa2dbdSJesper Dangaard Brouer goto err_xdp; 682ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 683ca9e83b4SJason Wang if (unlikely(err < 0)) { 6844941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 68511b7d897SJesper Dangaard Brouer goto err_xdp; 68611b7d897SJesper Dangaard Brouer } 6872471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 688186b3c99SJason Wang rcu_read_unlock(); 689186b3c99SJason Wang goto xdp_xmit; 690186b3c99SJason Wang case XDP_REDIRECT: 691d46eeeafSJason Wang stats->xdp_redirects++; 692186b3c99SJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 69311b7d897SJesper Dangaard Brouer if (err) 69411b7d897SJesper Dangaard Brouer goto err_xdp; 6952471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 6964941d472SJason Wang rcu_read_unlock(); 6974941d472SJason Wang goto xdp_xmit; 6984941d472SJason Wang default: 6994941d472SJason Wang bpf_warn_invalid_xdp_action(act); 700b633d440SGustavo A. R. Silva /* fall through */ 7014941d472SJason Wang case XDP_ABORTED: 7024941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 7034941d472SJason Wang case XDP_DROP: 7044941d472SJason Wang goto err_xdp; 7054941d472SJason Wang } 7064941d472SJason Wang } 7074941d472SJason Wang rcu_read_unlock(); 7084941d472SJason Wang 7094941d472SJason Wang skb = build_skb(buf, buflen); 7104941d472SJason Wang if (!skb) { 7114941d472SJason Wang put_page(page); 7124941d472SJason Wang goto err; 7134941d472SJason Wang } 7144941d472SJason Wang skb_reserve(skb, headroom - delta); 7156870de43SNikita V. Shirokov skb_put(skb, len); 7164941d472SJason Wang if (!delta) { 7174941d472SJason Wang buf += header_offset; 7184941d472SJason Wang memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 7194941d472SJason Wang } /* keep zeroed vnet hdr since packet was changed by bpf */ 7204941d472SJason Wang 7214941d472SJason Wang err: 7224941d472SJason Wang return skb; 7234941d472SJason Wang 7244941d472SJason Wang err_xdp: 7254941d472SJason Wang rcu_read_unlock(); 726d46eeeafSJason Wang stats->xdp_drops++; 727d46eeeafSJason Wang stats->drops++; 7284941d472SJason Wang put_page(page); 7294941d472SJason Wang xdp_xmit: 7304941d472SJason Wang return NULL; 7314941d472SJason Wang } 7324941d472SJason Wang 7334941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev, 7344941d472SJason Wang struct virtnet_info *vi, 7354941d472SJason Wang struct receive_queue *rq, 7364941d472SJason Wang void *buf, 7377d9d60fdSToshiaki Makita unsigned int len, 738d46eeeafSJason Wang struct virtnet_rq_stats *stats) 7394941d472SJason Wang { 7404941d472SJason Wang struct page *page = buf; 7414941d472SJason Wang struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE); 7424941d472SJason Wang 743d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 7444941d472SJason Wang if (unlikely(!skb)) 7454941d472SJason Wang goto err; 7464941d472SJason Wang 7474941d472SJason Wang return skb; 7484941d472SJason Wang 7494941d472SJason Wang err: 750d46eeeafSJason Wang stats->drops++; 7514941d472SJason Wang give_pages(rq, page); 7524941d472SJason Wang return NULL; 7534941d472SJason Wang } 7544941d472SJason Wang 7558fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 756fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 7578fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 758680557cfSMichael S. Tsirkin void *buf, 759680557cfSMichael S. Tsirkin void *ctx, 760186b3c99SJason Wang unsigned int len, 7617d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 762d46eeeafSJason Wang struct virtnet_rq_stats *stats) 7639ab86bbcSShirley Ma { 764012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 765012873d0SMichael S. Tsirkin u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 7668fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 7678fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 768f600b690SJohn Fastabend struct sk_buff *head_skb, *curr_skb; 769f600b690SJohn Fastabend struct bpf_prog *xdp_prog; 770f600b690SJohn Fastabend unsigned int truesize; 7714941d472SJason Wang unsigned int headroom = mergeable_ctx_to_headroom(ctx); 7723cc81a9aSJason Wang int err; 773ab7db917SMichael Dalton 77456434a01SJohn Fastabend head_skb = NULL; 775d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 77656434a01SJohn Fastabend 777f600b690SJohn Fastabend rcu_read_lock(); 778f600b690SJohn Fastabend xdp_prog = rcu_dereference(rq->xdp_prog); 779f600b690SJohn Fastabend if (xdp_prog) { 78044fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 78172979a6cSJohn Fastabend struct page *xdp_page; 7820354e4d1SJohn Fastabend struct xdp_buff xdp; 7830354e4d1SJohn Fastabend void *data; 784f600b690SJohn Fastabend u32 act; 785f600b690SJohn Fastabend 7863d62b2a0SJason Wang /* Transient failure which in theory could occur if 7873d62b2a0SJason Wang * in-flight packets from before XDP was enabled reach 7883d62b2a0SJason Wang * the receive path after XDP is loaded. 7893d62b2a0SJason Wang */ 7903d62b2a0SJason Wang if (unlikely(hdr->hdr.gso_type)) 7913d62b2a0SJason Wang goto err_xdp; 7923d62b2a0SJason Wang 7933cc81a9aSJason Wang /* This happens when rx buffer size is underestimated 7943cc81a9aSJason Wang * or headroom is not enough because of the buffer 7953cc81a9aSJason Wang * was refilled before XDP is set. This should only 7963cc81a9aSJason Wang * happen for the first several packets, so we don't 7973cc81a9aSJason Wang * care much about its performance. 7983cc81a9aSJason Wang */ 7994941d472SJason Wang if (unlikely(num_buf > 1 || 8004941d472SJason Wang headroom < virtnet_get_headroom(vi))) { 80172979a6cSJohn Fastabend /* linearize data for XDP */ 80256a86f84SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, 8034941d472SJason Wang page, offset, 8044941d472SJason Wang VIRTIO_XDP_HEADROOM, 8054941d472SJason Wang &len); 80672979a6cSJohn Fastabend if (!xdp_page) 807f600b690SJohn Fastabend goto err_xdp; 8082de2f7f4SJohn Fastabend offset = VIRTIO_XDP_HEADROOM; 80972979a6cSJohn Fastabend } else { 81072979a6cSJohn Fastabend xdp_page = page; 811f600b690SJohn Fastabend } 812f600b690SJohn Fastabend 8132de2f7f4SJohn Fastabend /* Allow consuming headroom but reserve enough space to push 8142de2f7f4SJohn Fastabend * the descriptor on if we get an XDP_TX return code. 8152de2f7f4SJohn Fastabend */ 8160354e4d1SJohn Fastabend data = page_address(xdp_page) + offset; 8172de2f7f4SJohn Fastabend xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len; 8180354e4d1SJohn Fastabend xdp.data = data + vi->hdr_len; 819de8f3a83SDaniel Borkmann xdp_set_data_meta_invalid(&xdp); 8200354e4d1SJohn Fastabend xdp.data_end = xdp.data + (len - vi->hdr_len); 821754b8a21SJesper Dangaard Brouer xdp.rxq = &rq->xdp_rxq; 822754b8a21SJesper Dangaard Brouer 8230354e4d1SJohn Fastabend act = bpf_prog_run_xdp(xdp_prog, &xdp); 824d46eeeafSJason Wang stats->xdp_packets++; 8250354e4d1SJohn Fastabend 82656434a01SJohn Fastabend switch (act) { 82756434a01SJohn Fastabend case XDP_PASS: 8282de2f7f4SJohn Fastabend /* recalculate offset to account for any header 8292de2f7f4SJohn Fastabend * adjustments. Note other cases do not build an 8302de2f7f4SJohn Fastabend * skb and avoid using offset 8312de2f7f4SJohn Fastabend */ 8322de2f7f4SJohn Fastabend offset = xdp.data - 8332de2f7f4SJohn Fastabend page_address(xdp_page) - vi->hdr_len; 8342de2f7f4SJohn Fastabend 8356870de43SNikita V. Shirokov /* recalculate len if xdp.data or xdp.data_end were 8366870de43SNikita V. Shirokov * adjusted 8376870de43SNikita V. Shirokov */ 838aaa64527SNikita V. Shirokov len = xdp.data_end - xdp.data + vi->hdr_len; 8391830f893SJason Wang /* We can only create skb based on xdp_page. */ 8401830f893SJason Wang if (unlikely(xdp_page != page)) { 8411830f893SJason Wang rcu_read_unlock(); 8421830f893SJason Wang put_page(page); 8431830f893SJason Wang head_skb = page_to_skb(vi, rq, xdp_page, 8442de2f7f4SJohn Fastabend offset, len, PAGE_SIZE); 8451830f893SJason Wang return head_skb; 8461830f893SJason Wang } 84756434a01SJohn Fastabend break; 84856434a01SJohn Fastabend case XDP_TX: 849d46eeeafSJason Wang stats->xdp_tx++; 85044fa2dbdSJesper Dangaard Brouer xdpf = convert_to_xdp_frame(&xdp); 85144fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 85244fa2dbdSJesper Dangaard Brouer goto err_xdp; 853ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 854ca9e83b4SJason Wang if (unlikely(err < 0)) { 8550354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 85611b7d897SJesper Dangaard Brouer if (unlikely(xdp_page != page)) 85711b7d897SJesper Dangaard Brouer put_page(xdp_page); 85811b7d897SJesper Dangaard Brouer goto err_xdp; 85911b7d897SJesper Dangaard Brouer } 8602471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 86172979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 8625d458a13SJason Wang put_page(page); 86356434a01SJohn Fastabend rcu_read_unlock(); 86456434a01SJohn Fastabend goto xdp_xmit; 8653cc81a9aSJason Wang case XDP_REDIRECT: 866d46eeeafSJason Wang stats->xdp_redirects++; 8673cc81a9aSJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 8683cc81a9aSJason Wang if (err) { 8693cc81a9aSJason Wang if (unlikely(xdp_page != page)) 8703cc81a9aSJason Wang put_page(xdp_page); 8713cc81a9aSJason Wang goto err_xdp; 8723cc81a9aSJason Wang } 8732471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 8743cc81a9aSJason Wang if (unlikely(xdp_page != page)) 8756890418bSJason Wang put_page(page); 8763cc81a9aSJason Wang rcu_read_unlock(); 8773cc81a9aSJason Wang goto xdp_xmit; 87856434a01SJohn Fastabend default: 8790354e4d1SJohn Fastabend bpf_warn_invalid_xdp_action(act); 880b633d440SGustavo A. R. Silva /* fall through */ 8810354e4d1SJohn Fastabend case XDP_ABORTED: 8820354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 883b633d440SGustavo A. R. Silva /* fall through */ 8840354e4d1SJohn Fastabend case XDP_DROP: 88572979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 88672979a6cSJohn Fastabend __free_pages(xdp_page, 0); 887f600b690SJohn Fastabend goto err_xdp; 888f600b690SJohn Fastabend } 88956434a01SJohn Fastabend } 890f600b690SJohn Fastabend rcu_read_unlock(); 891f600b690SJohn Fastabend 89228b39bc7SJason Wang truesize = mergeable_ctx_to_truesize(ctx); 89328b39bc7SJason Wang if (unlikely(len > truesize)) { 89456da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 895680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 896680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 897680557cfSMichael S. Tsirkin goto err_skb; 898680557cfSMichael S. Tsirkin } 89928b39bc7SJason Wang 900f600b690SJohn Fastabend head_skb = page_to_skb(vi, rq, page, offset, len, truesize); 901f600b690SJohn Fastabend curr_skb = head_skb; 9029ab86bbcSShirley Ma 9038fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 9048fc3b9e9SMichael S. Tsirkin goto err_skb; 9059ab86bbcSShirley Ma while (--num_buf) { 9068fc3b9e9SMichael S. Tsirkin int num_skb_frags; 9078fc3b9e9SMichael S. Tsirkin 908680557cfSMichael S. Tsirkin buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 90903e9f8a0SYunjian Wang if (unlikely(!buf)) { 9108fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 911fdd819b2SMichael S. Tsirkin dev->name, num_buf, 912012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 913012873d0SMichael S. Tsirkin hdr->num_buffers)); 9148fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 9158fc3b9e9SMichael S. Tsirkin goto err_buf; 9163f2c31d9SMark McLoughlin } 9178fc3b9e9SMichael S. Tsirkin 918d46eeeafSJason Wang stats->bytes += len; 9198fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 92028b39bc7SJason Wang 92128b39bc7SJason Wang truesize = mergeable_ctx_to_truesize(ctx); 92228b39bc7SJason Wang if (unlikely(len > truesize)) { 92356da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 924680557cfSMichael S. Tsirkin dev->name, len, (unsigned long)ctx); 925680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 926680557cfSMichael S. Tsirkin goto err_skb; 927680557cfSMichael S. Tsirkin } 9288fc3b9e9SMichael S. Tsirkin 9298fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 9302613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 9312613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 9328fc3b9e9SMichael S. Tsirkin 9338fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 9348fc3b9e9SMichael S. Tsirkin goto err_skb; 9352613af0eSMichael Dalton if (curr_skb == head_skb) 9362613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 9372613af0eSMichael Dalton else 9382613af0eSMichael Dalton curr_skb->next = nskb; 9392613af0eSMichael Dalton curr_skb = nskb; 9402613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 9412613af0eSMichael Dalton num_skb_frags = 0; 9422613af0eSMichael Dalton } 9432613af0eSMichael Dalton if (curr_skb != head_skb) { 9442613af0eSMichael Dalton head_skb->data_len += len; 9452613af0eSMichael Dalton head_skb->len += len; 946fb51879dSMichael Dalton head_skb->truesize += truesize; 9472613af0eSMichael Dalton } 9488fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 949ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 950ba275241SJason Wang put_page(page); 951ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 952fb51879dSMichael Dalton len, truesize); 953ba275241SJason Wang } else { 9542613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 955fb51879dSMichael Dalton offset, len, truesize); 956ba275241SJason Wang } 9578fc3b9e9SMichael S. Tsirkin } 9588fc3b9e9SMichael S. Tsirkin 9595377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 9608fc3b9e9SMichael S. Tsirkin return head_skb; 9618fc3b9e9SMichael S. Tsirkin 962f600b690SJohn Fastabend err_xdp: 963f600b690SJohn Fastabend rcu_read_unlock(); 964d46eeeafSJason Wang stats->xdp_drops++; 9658fc3b9e9SMichael S. Tsirkin err_skb: 9668fc3b9e9SMichael S. Tsirkin put_page(page); 967850e088dSJason Wang while (num_buf-- > 1) { 968680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &len); 969680557cfSMichael S. Tsirkin if (unlikely(!buf)) { 9708fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 9718fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 9728fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 9738fc3b9e9SMichael S. Tsirkin break; 9748fc3b9e9SMichael S. Tsirkin } 975d46eeeafSJason Wang stats->bytes += len; 976680557cfSMichael S. Tsirkin page = virt_to_head_page(buf); 9778fc3b9e9SMichael S. Tsirkin put_page(page); 9783f2c31d9SMark McLoughlin } 9798fc3b9e9SMichael S. Tsirkin err_buf: 980d46eeeafSJason Wang stats->drops++; 9818fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 98256434a01SJohn Fastabend xdp_xmit: 9838fc3b9e9SMichael S. Tsirkin return NULL; 9849ab86bbcSShirley Ma } 9859ab86bbcSShirley Ma 9867d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 9872471c75eSJesper Dangaard Brouer void *buf, unsigned int len, void **ctx, 988a0929a44SToshiaki Makita unsigned int *xdp_xmit, 989d46eeeafSJason Wang struct virtnet_rq_stats *stats) 9909ab86bbcSShirley Ma { 991e9d7417bSJason Wang struct net_device *dev = vi->dev; 9929ab86bbcSShirley Ma struct sk_buff *skb; 993012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 9949ab86bbcSShirley Ma 995bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 9969ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 9979ab86bbcSShirley Ma dev->stats.rx_length_errors++; 998ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 999680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 1000ab7db917SMichael Dalton } else if (vi->big_packets) { 100198bfd23cSMichael Dalton give_pages(rq, buf); 1002ab7db917SMichael Dalton } else { 1003f6b10209SJason Wang put_page(virt_to_head_page(buf)); 1004ab7db917SMichael Dalton } 10057d9d60fdSToshiaki Makita return; 10069ab86bbcSShirley Ma } 10079ab86bbcSShirley Ma 1008f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 10097d9d60fdSToshiaki Makita skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, 1010a0929a44SToshiaki Makita stats); 1011f121159dSMichael S. Tsirkin else if (vi->big_packets) 1012a0929a44SToshiaki Makita skb = receive_big(dev, vi, rq, buf, len, stats); 1013f121159dSMichael S. Tsirkin else 1014a0929a44SToshiaki Makita skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); 1015f121159dSMichael S. Tsirkin 10168fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 10177d9d60fdSToshiaki Makita return; 10183f2c31d9SMark McLoughlin 10199ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 10203fa2a1dfSstephen hemminger 1021e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 102210a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 1023296f96fcSRusty Russell 1024e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 1025e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 1026e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 1027e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 1028fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 1029296f96fcSRusty Russell goto frame_err; 1030296f96fcSRusty Russell } 1031296f96fcSRusty Russell 1032d1dc06dcSMike Rapoport skb->protocol = eth_type_trans(skb, dev); 1033d1dc06dcSMike Rapoport pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 1034d1dc06dcSMike Rapoport ntohs(skb->protocol), skb->len, skb->pkt_type); 1035d1dc06dcSMike Rapoport 10360fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 10377d9d60fdSToshiaki Makita return; 1038296f96fcSRusty Russell 1039296f96fcSRusty Russell frame_err: 1040296f96fcSRusty Russell dev->stats.rx_frame_errors++; 1041296f96fcSRusty Russell dev_kfree_skb(skb); 1042296f96fcSRusty Russell } 1043296f96fcSRusty Russell 1044192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the 1045192f68cfSJason Wang * same size, except for the headroom. For this reason we do 1046192f68cfSJason Wang * not need to use mergeable_len_to_ctx here - it is enough 1047192f68cfSJason Wang * to store the headroom as the context ignoring the truesize. 1048192f68cfSJason Wang */ 1049946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 1050946fa564SMichael S. Tsirkin gfp_t gfp) 1051296f96fcSRusty Russell { 1052f6b10209SJason Wang struct page_frag *alloc_frag = &rq->alloc_frag; 1053f6b10209SJason Wang char *buf; 10542de2f7f4SJohn Fastabend unsigned int xdp_headroom = virtnet_get_headroom(vi); 1055192f68cfSJason Wang void *ctx = (void *)(unsigned long)xdp_headroom; 1056f6b10209SJason Wang int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 10579ab86bbcSShirley Ma int err; 10583f2c31d9SMark McLoughlin 1059f6b10209SJason Wang len = SKB_DATA_ALIGN(len) + 1060f6b10209SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1061f6b10209SJason Wang if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 10629ab86bbcSShirley Ma return -ENOMEM; 1063296f96fcSRusty Russell 1064f6b10209SJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1065f6b10209SJason Wang get_page(alloc_frag->page); 1066f6b10209SJason Wang alloc_frag->offset += len; 1067f6b10209SJason Wang sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 1068f6b10209SJason Wang vi->hdr_len + GOOD_PACKET_LEN); 1069192f68cfSJason Wang err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 10709ab86bbcSShirley Ma if (err < 0) 1071f6b10209SJason Wang put_page(virt_to_head_page(buf)); 10729ab86bbcSShirley Ma return err; 107397402b96SHerbert Xu } 107497402b96SHerbert Xu 1075012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 1076012873d0SMichael S. Tsirkin gfp_t gfp) 10779ab86bbcSShirley Ma { 10789ab86bbcSShirley Ma struct page *first, *list = NULL; 10799ab86bbcSShirley Ma char *p; 10809ab86bbcSShirley Ma int i, err, offset; 1081296f96fcSRusty Russell 1082a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 1083a5835440SRusty Russell 1084e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 10859ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 1086e9d7417bSJason Wang first = get_a_page(rq, gfp); 10879ab86bbcSShirley Ma if (!first) { 10889ab86bbcSShirley Ma if (list) 1089e9d7417bSJason Wang give_pages(rq, list); 10909ab86bbcSShirley Ma return -ENOMEM; 1091296f96fcSRusty Russell } 1092e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 10939ab86bbcSShirley Ma 10949ab86bbcSShirley Ma /* chain new page in list head to match sg */ 10959ab86bbcSShirley Ma first->private = (unsigned long)list; 10969ab86bbcSShirley Ma list = first; 10979ab86bbcSShirley Ma } 10989ab86bbcSShirley Ma 1099e9d7417bSJason Wang first = get_a_page(rq, gfp); 11009ab86bbcSShirley Ma if (!first) { 1101e9d7417bSJason Wang give_pages(rq, list); 11029ab86bbcSShirley Ma return -ENOMEM; 11039ab86bbcSShirley Ma } 11049ab86bbcSShirley Ma p = page_address(first); 11059ab86bbcSShirley Ma 1106e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 1107012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 1108012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 11099ab86bbcSShirley Ma 1110e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 11119ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 1112e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 11139ab86bbcSShirley Ma 11149ab86bbcSShirley Ma /* chain first in list head */ 11159ab86bbcSShirley Ma first->private = (unsigned long)list; 11169dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 1117aa989f5eSMichael S. Tsirkin first, gfp); 11189ab86bbcSShirley Ma if (err < 0) 1119e9d7417bSJason Wang give_pages(rq, first); 11209ab86bbcSShirley Ma 11219ab86bbcSShirley Ma return err; 11229ab86bbcSShirley Ma } 11239ab86bbcSShirley Ma 1124d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 11253cc81a9aSJason Wang struct ewma_pkt_len *avg_pkt_len, 11263cc81a9aSJason Wang unsigned int room) 11279ab86bbcSShirley Ma { 1128ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1129fbf28d78SMichael Dalton unsigned int len; 1130fbf28d78SMichael Dalton 11313cc81a9aSJason Wang if (room) 11323cc81a9aSJason Wang return PAGE_SIZE - room; 11333cc81a9aSJason Wang 11345377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 1135f0c3192cSMichael S. Tsirkin rq->min_buf_len, PAGE_SIZE - hdr_len); 11363cc81a9aSJason Wang 1137e377fcc8SMichael S. Tsirkin return ALIGN(len, L1_CACHE_BYTES); 1138fbf28d78SMichael Dalton } 1139fbf28d78SMichael Dalton 11402de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi, 11412de2f7f4SJohn Fastabend struct receive_queue *rq, gfp_t gfp) 1142fbf28d78SMichael Dalton { 1143fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 11442de2f7f4SJohn Fastabend unsigned int headroom = virtnet_get_headroom(vi); 11453cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 11463cc81a9aSJason Wang unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1147fb51879dSMichael Dalton char *buf; 1148680557cfSMichael S. Tsirkin void *ctx; 11499ab86bbcSShirley Ma int err; 1150fb51879dSMichael Dalton unsigned int len, hole; 11519ab86bbcSShirley Ma 11523cc81a9aSJason Wang /* Extra tailroom is needed to satisfy XDP's assumption. This 11533cc81a9aSJason Wang * means rx frags coalescing won't work, but consider we've 11543cc81a9aSJason Wang * disabled GSO for XDP, it won't be a big issue. 11553cc81a9aSJason Wang */ 11563cc81a9aSJason Wang len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 11573cc81a9aSJason Wang if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp))) 11589ab86bbcSShirley Ma return -ENOMEM; 1159ab7db917SMichael Dalton 1160fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 11612de2f7f4SJohn Fastabend buf += headroom; /* advance address leaving hole at front of pkt */ 1162fb51879dSMichael Dalton get_page(alloc_frag->page); 11633cc81a9aSJason Wang alloc_frag->offset += len + room; 1164fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 11653cc81a9aSJason Wang if (hole < len + room) { 1166ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 1167ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 11681daa8790SMichael S. Tsirkin * the current buffer. 1169ab7db917SMichael Dalton */ 1170fb51879dSMichael Dalton len += hole; 1171fb51879dSMichael Dalton alloc_frag->offset += hole; 1172fb51879dSMichael Dalton } 11739ab86bbcSShirley Ma 1174fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 117529fda25aSDavid S. Miller ctx = mergeable_len_to_ctx(len, headroom); 1176680557cfSMichael S. Tsirkin err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 11779ab86bbcSShirley Ma if (err < 0) 11782613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 11799ab86bbcSShirley Ma 11809ab86bbcSShirley Ma return err; 1181296f96fcSRusty Russell } 1182296f96fcSRusty Russell 1183b2baed69SRusty Russell /* 1184b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 1185b2baed69SRusty Russell * 1186b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 1187b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 1188b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 1189b2baed69SRusty Russell */ 1190946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 1191946fa564SMichael S. Tsirkin gfp_t gfp) 11923f2c31d9SMark McLoughlin { 11933f2c31d9SMark McLoughlin int err; 11941788f495SMichael S. Tsirkin bool oom; 11953f2c31d9SMark McLoughlin 11960aea51c3SAmit Shah do { 11979ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 11982de2f7f4SJohn Fastabend err = add_recvbuf_mergeable(vi, rq, gfp); 11999ab86bbcSShirley Ma else if (vi->big_packets) 1200012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 12019ab86bbcSShirley Ma else 1202946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 12033f2c31d9SMark McLoughlin 12041788f495SMichael S. Tsirkin oom = err == -ENOMEM; 12059ed4cb07SRusty Russell if (err) 12063f2c31d9SMark McLoughlin break; 1207b7dfde95SLinus Torvalds } while (rq->vq->num_free); 1208461f03dcSToshiaki Makita if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) { 1209461f03dcSToshiaki Makita u64_stats_update_begin(&rq->stats.syncp); 1210d46eeeafSJason Wang rq->stats.kicks++; 1211461f03dcSToshiaki Makita u64_stats_update_end(&rq->stats.syncp); 1212461f03dcSToshiaki Makita } 1213461f03dcSToshiaki Makita 12143161e453SRusty Russell return !oom; 12153f2c31d9SMark McLoughlin } 12163f2c31d9SMark McLoughlin 121718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 1218296f96fcSRusty Russell { 1219296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 1220986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1221e9d7417bSJason Wang 1222e4e8452aSWillem de Bruijn virtqueue_napi_schedule(&rq->napi, rvq); 1223296f96fcSRusty Russell } 1224296f96fcSRusty Russell 1225e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 12263e9d08ecSBruce Rogers { 1227e4e8452aSWillem de Bruijn napi_enable(napi); 12283e9d08ecSBruce Rogers 12293e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 1230e4e8452aSWillem de Bruijn * won't get another interrupt, so process any outstanding packets now. 1231e4e8452aSWillem de Bruijn * Call local_bh_enable after to trigger softIRQ processing. 1232e4e8452aSWillem de Bruijn */ 1233ec13ee80SMichael S. Tsirkin local_bh_disable(); 1234e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 1235ec13ee80SMichael S. Tsirkin local_bh_enable(); 12363e9d08ecSBruce Rogers } 12373e9d08ecSBruce Rogers 1238b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1239b92f1e67SWillem de Bruijn struct virtqueue *vq, 1240b92f1e67SWillem de Bruijn struct napi_struct *napi) 1241b92f1e67SWillem de Bruijn { 1242b92f1e67SWillem de Bruijn if (!napi->weight) 1243b92f1e67SWillem de Bruijn return; 1244b92f1e67SWillem de Bruijn 1245b92f1e67SWillem de Bruijn /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1246b92f1e67SWillem de Bruijn * enable the feature if this is likely affine with the transmit path. 1247b92f1e67SWillem de Bruijn */ 1248b92f1e67SWillem de Bruijn if (!vi->affinity_hint_set) { 1249b92f1e67SWillem de Bruijn napi->weight = 0; 1250b92f1e67SWillem de Bruijn return; 1251b92f1e67SWillem de Bruijn } 1252b92f1e67SWillem de Bruijn 1253b92f1e67SWillem de Bruijn return virtnet_napi_enable(vq, napi); 1254b92f1e67SWillem de Bruijn } 1255b92f1e67SWillem de Bruijn 125678a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi) 125778a57b48SWillem de Bruijn { 125878a57b48SWillem de Bruijn if (napi->weight) 125978a57b48SWillem de Bruijn napi_disable(napi); 126078a57b48SWillem de Bruijn } 126178a57b48SWillem de Bruijn 12623161e453SRusty Russell static void refill_work(struct work_struct *work) 12633161e453SRusty Russell { 1264e9d7417bSJason Wang struct virtnet_info *vi = 1265e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 12663161e453SRusty Russell bool still_empty; 1267986a4f4dSJason Wang int i; 12683161e453SRusty Russell 126955257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1270986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 1271986a4f4dSJason Wang 1272986a4f4dSJason Wang napi_disable(&rq->napi); 1273946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1274e4e8452aSWillem de Bruijn virtnet_napi_enable(rq->vq, &rq->napi); 12753161e453SRusty Russell 12763161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 1277986a4f4dSJason Wang * we will *never* try to fill again. 1278986a4f4dSJason Wang */ 12793161e453SRusty Russell if (still_empty) 12803b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 12813161e453SRusty Russell } 1282986a4f4dSJason Wang } 12833161e453SRusty Russell 12842471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget, 12852471c75eSJesper Dangaard Brouer unsigned int *xdp_xmit) 1286296f96fcSRusty Russell { 1287e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 1288d46eeeafSJason Wang struct virtnet_rq_stats stats = {}; 1289a0929a44SToshiaki Makita unsigned int len; 12909ab86bbcSShirley Ma void *buf; 1291a0929a44SToshiaki Makita int i; 1292296f96fcSRusty Russell 1293192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 1294680557cfSMichael S. Tsirkin void *ctx; 1295680557cfSMichael S. Tsirkin 1296d46eeeafSJason Wang while (stats.packets < budget && 1297680557cfSMichael S. Tsirkin (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1298a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); 1299d46eeeafSJason Wang stats.packets++; 1300680557cfSMichael S. Tsirkin } 1301680557cfSMichael S. Tsirkin } else { 1302d46eeeafSJason Wang while (stats.packets < budget && 1303e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1304a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); 1305d46eeeafSJason Wang stats.packets++; 1306296f96fcSRusty Russell } 1307680557cfSMichael S. Tsirkin } 1308296f96fcSRusty Russell 1309be121f46SJason Wang if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 1310946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 13113b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 13123161e453SRusty Russell } 1313296f96fcSRusty Russell 1314d7dfc5cfSToshiaki Makita u64_stats_update_begin(&rq->stats.syncp); 1315a0929a44SToshiaki Makita for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) { 1316a0929a44SToshiaki Makita size_t offset = virtnet_rq_stats_desc[i].offset; 1317a0929a44SToshiaki Makita u64 *item; 1318a0929a44SToshiaki Makita 1319d46eeeafSJason Wang item = (u64 *)((u8 *)&rq->stats + offset); 1320d46eeeafSJason Wang *item += *(u64 *)((u8 *)&stats + offset); 1321a0929a44SToshiaki Makita } 1322d7dfc5cfSToshiaki Makita u64_stats_update_end(&rq->stats.syncp); 132361845d20SJason Wang 1324d46eeeafSJason Wang return stats.packets; 13252ffa7598SJason Wang } 13262ffa7598SJason Wang 1327ea7735d9SWillem de Bruijn static void free_old_xmit_skbs(struct send_queue *sq) 1328ea7735d9SWillem de Bruijn { 1329ea7735d9SWillem de Bruijn struct sk_buff *skb; 1330ea7735d9SWillem de Bruijn unsigned int len; 1331ea7735d9SWillem de Bruijn unsigned int packets = 0; 1332ea7735d9SWillem de Bruijn unsigned int bytes = 0; 1333ea7735d9SWillem de Bruijn 1334ea7735d9SWillem de Bruijn while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 1335ea7735d9SWillem de Bruijn pr_debug("Sent skb %p\n", skb); 1336ea7735d9SWillem de Bruijn 1337ea7735d9SWillem de Bruijn bytes += skb->len; 1338ea7735d9SWillem de Bruijn packets++; 1339ea7735d9SWillem de Bruijn 1340dadc0736SEric Dumazet dev_consume_skb_any(skb); 1341ea7735d9SWillem de Bruijn } 1342ea7735d9SWillem de Bruijn 1343ea7735d9SWillem de Bruijn /* Avoid overhead when no packets have been processed 1344ea7735d9SWillem de Bruijn * happens when called speculatively from start_xmit. 1345ea7735d9SWillem de Bruijn */ 1346ea7735d9SWillem de Bruijn if (!packets) 1347ea7735d9SWillem de Bruijn return; 1348ea7735d9SWillem de Bruijn 1349d7dfc5cfSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1350d7dfc5cfSToshiaki Makita sq->stats.bytes += bytes; 1351d7dfc5cfSToshiaki Makita sq->stats.packets += packets; 1352d7dfc5cfSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1353ea7735d9SWillem de Bruijn } 1354ea7735d9SWillem de Bruijn 13557b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq) 13567b0411efSWillem de Bruijn { 13577b0411efSWillem de Bruijn struct virtnet_info *vi = rq->vq->vdev->priv; 13587b0411efSWillem de Bruijn unsigned int index = vq2rxq(rq->vq); 13597b0411efSWillem de Bruijn struct send_queue *sq = &vi->sq[index]; 13607b0411efSWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 13617b0411efSWillem de Bruijn 13627b0411efSWillem de Bruijn if (!sq->napi.weight) 13637b0411efSWillem de Bruijn return; 13647b0411efSWillem de Bruijn 13657b0411efSWillem de Bruijn if (__netif_tx_trylock(txq)) { 13667b0411efSWillem de Bruijn free_old_xmit_skbs(sq); 13677b0411efSWillem de Bruijn __netif_tx_unlock(txq); 13687b0411efSWillem de Bruijn } 13697b0411efSWillem de Bruijn 13707b0411efSWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 13717b0411efSWillem de Bruijn netif_tx_wake_queue(txq); 13727b0411efSWillem de Bruijn } 13737b0411efSWillem de Bruijn 13742ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 13752ffa7598SJason Wang { 13762ffa7598SJason Wang struct receive_queue *rq = 13772ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 13789267c430SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 13799267c430SJason Wang struct send_queue *sq; 13802a43565cSToshiaki Makita unsigned int received; 13812471c75eSJesper Dangaard Brouer unsigned int xdp_xmit = 0; 13822ffa7598SJason Wang 13837b0411efSWillem de Bruijn virtnet_poll_cleantx(rq); 13847b0411efSWillem de Bruijn 1385186b3c99SJason Wang received = virtnet_receive(rq, budget, &xdp_xmit); 13862ffa7598SJason Wang 13878329d98eSRusty Russell /* Out of packets? */ 1388e4e8452aSWillem de Bruijn if (received < budget) 1389e4e8452aSWillem de Bruijn virtqueue_napi_complete(napi, rq->vq, received); 1390296f96fcSRusty Russell 13912471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_REDIR) 13922471c75eSJesper Dangaard Brouer xdp_do_flush_map(); 13932471c75eSJesper Dangaard Brouer 13942471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_TX) { 13952a43565cSToshiaki Makita sq = virtnet_xdp_sq(vi); 1396461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1397461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1398461f03dcSToshiaki Makita sq->stats.kicks++; 1399461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1400461f03dcSToshiaki Makita } 14019267c430SJason Wang } 1402186b3c99SJason Wang 1403296f96fcSRusty Russell return received; 1404296f96fcSRusty Russell } 1405296f96fcSRusty Russell 1406986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 1407986a4f4dSJason Wang { 1408986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1409754b8a21SJesper Dangaard Brouer int i, err; 1410986a4f4dSJason Wang 1411e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1412e4166625SJason Wang if (i < vi->curr_queue_pairs) 1413986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 1414946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1415986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 1416754b8a21SJesper Dangaard Brouer 1417754b8a21SJesper Dangaard Brouer err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i); 1418754b8a21SJesper Dangaard Brouer if (err < 0) 1419754b8a21SJesper Dangaard Brouer return err; 1420754b8a21SJesper Dangaard Brouer 14218d5d8852SJesper Dangaard Brouer err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq, 14228d5d8852SJesper Dangaard Brouer MEM_TYPE_PAGE_SHARED, NULL); 14238d5d8852SJesper Dangaard Brouer if (err < 0) { 14248d5d8852SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 14258d5d8852SJesper Dangaard Brouer return err; 14268d5d8852SJesper Dangaard Brouer } 14278d5d8852SJesper Dangaard Brouer 1428e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1429b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1430986a4f4dSJason Wang } 1431986a4f4dSJason Wang 1432986a4f4dSJason Wang return 0; 1433986a4f4dSJason Wang } 1434986a4f4dSJason Wang 1435b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1436b92f1e67SWillem de Bruijn { 1437b92f1e67SWillem de Bruijn struct send_queue *sq = container_of(napi, struct send_queue, napi); 1438b92f1e67SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1439b92f1e67SWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq)); 1440b92f1e67SWillem de Bruijn 1441b92f1e67SWillem de Bruijn __netif_tx_lock(txq, raw_smp_processor_id()); 1442b92f1e67SWillem de Bruijn free_old_xmit_skbs(sq); 1443b92f1e67SWillem de Bruijn __netif_tx_unlock(txq); 1444b92f1e67SWillem de Bruijn 1445b92f1e67SWillem de Bruijn virtqueue_napi_complete(napi, sq->vq, 0); 1446b92f1e67SWillem de Bruijn 1447b92f1e67SWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1448b92f1e67SWillem de Bruijn netif_tx_wake_queue(txq); 1449b92f1e67SWillem de Bruijn 1450b92f1e67SWillem de Bruijn return 0; 1451b92f1e67SWillem de Bruijn } 1452b92f1e67SWillem de Bruijn 1453e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1454296f96fcSRusty Russell { 1455012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 1456296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1457e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 1458e2fcad58SJason A. Donenfeld int num_sg; 1459012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 1460e7428e95SMichael S. Tsirkin bool can_push; 1461296f96fcSRusty Russell 1462e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1463e7428e95SMichael S. Tsirkin 1464e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 1465e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1466e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1467e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 1468e7428e95SMichael S. Tsirkin * csum_start offset below. */ 1469e7428e95SMichael S. Tsirkin if (can_push) 1470012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1471e7428e95SMichael S. Tsirkin else 1472e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 1473296f96fcSRusty Russell 1474e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 1475fd3a8862SWillem de Bruijn virtio_is_little_endian(vi->vdev), false, 1476fd3a8862SWillem de Bruijn 0)) 1477296f96fcSRusty Russell BUG(); 1478296f96fcSRusty Russell 1479e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 1480012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 14813f2c31d9SMark McLoughlin 1482547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1483e7428e95SMichael S. Tsirkin if (can_push) { 1484e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 1485e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1486e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1487e2fcad58SJason A. Donenfeld return num_sg; 1488e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 1489e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 1490e7428e95SMichael S. Tsirkin } else { 1491e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 1492e2fcad58SJason A. Donenfeld num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1493e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1494e2fcad58SJason A. Donenfeld return num_sg; 1495e2fcad58SJason A. Donenfeld num_sg++; 1496e7428e95SMichael S. Tsirkin } 14979dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 149811a3a154SRusty Russell } 149911a3a154SRusty Russell 1500424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 150199ffc696SRusty Russell { 150299ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1503986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 1504986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 15059ed4cb07SRusty Russell int err; 15064b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 15074b7fd2e6SMichael S. Tsirkin bool kick = !skb->xmit_more; 1508b92f1e67SWillem de Bruijn bool use_napi = sq->napi.weight; 15092cb9c6baSRusty Russell 15102cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 1511e9d7417bSJason Wang free_old_xmit_skbs(sq); 151299ffc696SRusty Russell 1513bdb12e0dSWillem de Bruijn if (use_napi && kick) 1514bdb12e0dSWillem de Bruijn virtqueue_enable_cb_delayed(sq->vq); 1515bdb12e0dSWillem de Bruijn 1516074c3582SJacob Keller /* timestamp packet in software */ 1517074c3582SJacob Keller skb_tx_timestamp(skb); 1518074c3582SJacob Keller 151903f191baSMichael S. Tsirkin /* Try to transmit */ 1520b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 152199ffc696SRusty Russell 15229ed4cb07SRusty Russell /* This should not happen! */ 1523681daee2SJason Wang if (unlikely(err)) { 152458eba97dSRusty Russell dev->stats.tx_fifo_errors++; 15252e57b79cSRick Jones if (net_ratelimit()) 152658eba97dSRusty Russell dev_warn(&dev->dev, 1527b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 152858eba97dSRusty Russell dev->stats.tx_dropped++; 152985e94525SEric W. Biederman dev_kfree_skb_any(skb); 153058eba97dSRusty Russell return NETDEV_TX_OK; 1531296f96fcSRusty Russell } 153203f191baSMichael S. Tsirkin 153348925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 1534b92f1e67SWillem de Bruijn if (!use_napi) { 153548925e37SRusty Russell skb_orphan(skb); 153648925e37SRusty Russell nf_reset(skb); 1537b92f1e67SWillem de Bruijn } 153848925e37SRusty Russell 153960302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 154060302ff6SMichael S. Tsirkin * are then unable to transmit. 154160302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 154260302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 154360302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 154460302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 154560302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 154660302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 154760302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 1548d631b94eSstephen hemminger */ 1549b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1550986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 1551b92f1e67SWillem de Bruijn if (!use_napi && 1552b92f1e67SWillem de Bruijn unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 155348925e37SRusty Russell /* More just got used, free them then recheck. */ 1554b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 1555b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1556986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 1557e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 155848925e37SRusty Russell } 155948925e37SRusty Russell } 156048925e37SRusty Russell } 156148925e37SRusty Russell 1562461f03dcSToshiaki Makita if (kick || netif_xmit_stopped(txq)) { 1563461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1564461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1565461f03dcSToshiaki Makita sq->stats.kicks++; 1566461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1567461f03dcSToshiaki Makita } 1568461f03dcSToshiaki Makita } 15690b725a2cSDavid S. Miller 15700b725a2cSDavid S. Miller return NETDEV_TX_OK; 1571c223a078SDavid S. Miller } 1572c223a078SDavid S. Miller 157340cbfc37SAmos Kong /* 157440cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 157540cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 1576788a8b6dSstephen hemminger * never fail unless improperly formatted. 157740cbfc37SAmos Kong */ 157840cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 1579d24bae32Sstephen hemminger struct scatterlist *out) 158040cbfc37SAmos Kong { 1581f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 1582d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 158340cbfc37SAmos Kong 158440cbfc37SAmos Kong /* Caller should know better */ 1585f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 158640cbfc37SAmos Kong 158712e57169SMichael S. Tsirkin vi->ctrl->status = ~0; 158812e57169SMichael S. Tsirkin vi->ctrl->hdr.class = class; 158912e57169SMichael S. Tsirkin vi->ctrl->hdr.cmd = cmd; 1590f7bc9594SRusty Russell /* Add header */ 159112e57169SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 1592f7bc9594SRusty Russell sgs[out_num++] = &hdr; 159340cbfc37SAmos Kong 1594f7bc9594SRusty Russell if (out) 1595f7bc9594SRusty Russell sgs[out_num++] = out; 159640cbfc37SAmos Kong 1597f7bc9594SRusty Russell /* Add return status. */ 159812e57169SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 1599d24bae32Sstephen hemminger sgs[out_num] = &stat; 160040cbfc37SAmos Kong 1601d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1602a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 160340cbfc37SAmos Kong 160467975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 160512e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 160640cbfc37SAmos Kong 160740cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 160840cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 160940cbfc37SAmos Kong */ 1610047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 1611047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 161240cbfc37SAmos Kong cpu_relax(); 161340cbfc37SAmos Kong 161412e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 161540cbfc37SAmos Kong } 161640cbfc37SAmos Kong 16179c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 16189c46f6d4SAlex Williamson { 16199c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 16209c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 1621f2f2c8b4SJiri Pirko int ret; 1622e37e2ff3SAndy Lutomirski struct sockaddr *addr; 16237e58d5aeSAmos Kong struct scatterlist sg; 16249c46f6d4SAlex Williamson 1625ba5e4426SSridhar Samudrala if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 1626ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 1627ba5e4426SSridhar Samudrala 1628801822d1SShyam Saini addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 1629e37e2ff3SAndy Lutomirski if (!addr) 1630e37e2ff3SAndy Lutomirski return -ENOMEM; 1631e37e2ff3SAndy Lutomirski 1632e37e2ff3SAndy Lutomirski ret = eth_prepare_mac_addr_change(dev, addr); 1633f2f2c8b4SJiri Pirko if (ret) 1634e37e2ff3SAndy Lutomirski goto out; 16359c46f6d4SAlex Williamson 16367e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 16377e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 16387e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1639d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 16407e58d5aeSAmos Kong dev_warn(&vdev->dev, 16417e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 1642e37e2ff3SAndy Lutomirski ret = -EINVAL; 1643e37e2ff3SAndy Lutomirski goto out; 16447e58d5aeSAmos Kong } 16457e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 16467e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1647855e0c52SRusty Russell unsigned int i; 1648855e0c52SRusty Russell 1649855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 1650855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 1651855e0c52SRusty Russell virtio_cwrite8(vdev, 1652855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1653855e0c52SRusty Russell i, addr->sa_data[i]); 16547e58d5aeSAmos Kong } 16557e58d5aeSAmos Kong 16567e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 1657e37e2ff3SAndy Lutomirski ret = 0; 16589c46f6d4SAlex Williamson 1659e37e2ff3SAndy Lutomirski out: 1660e37e2ff3SAndy Lutomirski kfree(addr); 1661e37e2ff3SAndy Lutomirski return ret; 16629c46f6d4SAlex Williamson } 16639c46f6d4SAlex Williamson 1664bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev, 16653fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 16663fa2a1dfSstephen hemminger { 16673fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 16683fa2a1dfSstephen hemminger unsigned int start; 1669d7dfc5cfSToshiaki Makita int i; 16703fa2a1dfSstephen hemminger 1671d7dfc5cfSToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 16722c4a2f7dSToshiaki Makita u64 tpackets, tbytes, rpackets, rbytes, rdrops; 1673d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 1674d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 16753fa2a1dfSstephen hemminger 16763fa2a1dfSstephen hemminger do { 1677d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 1678d7dfc5cfSToshiaki Makita tpackets = sq->stats.packets; 1679d7dfc5cfSToshiaki Makita tbytes = sq->stats.bytes; 1680d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 168183a27052SEric Dumazet 168283a27052SEric Dumazet do { 1683d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 1684d46eeeafSJason Wang rpackets = rq->stats.packets; 1685d46eeeafSJason Wang rbytes = rq->stats.bytes; 1686d46eeeafSJason Wang rdrops = rq->stats.drops; 1687d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 16883fa2a1dfSstephen hemminger 16893fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 16903fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 16913fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 16923fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 16932c4a2f7dSToshiaki Makita tot->rx_dropped += rdrops; 16943fa2a1dfSstephen hemminger } 16953fa2a1dfSstephen hemminger 16963fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1697021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 16983fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 16993fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 17003fa2a1dfSstephen hemminger } 17013fa2a1dfSstephen hemminger 1702da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 1703da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 1704da74e89dSAmit Shah { 1705da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 1706986a4f4dSJason Wang int i; 1707da74e89dSAmit Shah 1708986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 1709986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 1710da74e89dSAmit Shah } 1711da74e89dSAmit Shah #endif 1712da74e89dSAmit Shah 1713586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1714586d17c5SJason Wang { 1715586d17c5SJason Wang rtnl_lock(); 1716586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1717d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1718586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1719586d17c5SJason Wang rtnl_unlock(); 1720586d17c5SJason Wang } 1721586d17c5SJason Wang 172247315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1723986a4f4dSJason Wang { 1724986a4f4dSJason Wang struct scatterlist sg; 1725986a4f4dSJason Wang struct net_device *dev = vi->dev; 1726986a4f4dSJason Wang 1727986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1728986a4f4dSJason Wang return 0; 1729986a4f4dSJason Wang 173012e57169SMichael S. Tsirkin vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 173112e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); 1732986a4f4dSJason Wang 1733986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1734d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1735986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1736986a4f4dSJason Wang queue_pairs); 1737986a4f4dSJason Wang return -EINVAL; 173855257d72SSasha Levin } else { 1739986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 174035ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 174135ed159bSJason Wang if (dev->flags & IFF_UP) 17429b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 174355257d72SSasha Levin } 1744986a4f4dSJason Wang 1745986a4f4dSJason Wang return 0; 1746986a4f4dSJason Wang } 1747986a4f4dSJason Wang 174847315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 174947315329SJohn Fastabend { 175047315329SJohn Fastabend int err; 175147315329SJohn Fastabend 175247315329SJohn Fastabend rtnl_lock(); 175347315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 175447315329SJohn Fastabend rtnl_unlock(); 175547315329SJohn Fastabend return err; 175647315329SJohn Fastabend } 175747315329SJohn Fastabend 1758296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1759296f96fcSRusty Russell { 1760296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1761986a4f4dSJason Wang int i; 1762296f96fcSRusty Russell 1763b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1764b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1765986a4f4dSJason Wang 1766b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 1767754b8a21SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 1768986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 176978a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 1770b92f1e67SWillem de Bruijn } 1771296f96fcSRusty Russell 1772296f96fcSRusty Russell return 0; 1773296f96fcSRusty Russell } 1774296f96fcSRusty Russell 17752af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 17762af7698eSAlex Williamson { 17772af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1778f565a7c2SAlex Williamson struct scatterlist sg[2]; 1779f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1780ccffad25SJiri Pirko struct netdev_hw_addr *ha; 178132e7bfc4SJiri Pirko int uc_count; 17824cd24eafSJiri Pirko int mc_count; 1783f565a7c2SAlex Williamson void *buf; 1784f565a7c2SAlex Williamson int i; 17852af7698eSAlex Williamson 1786788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 17872af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 17882af7698eSAlex Williamson return; 17892af7698eSAlex Williamson 179012e57169SMichael S. Tsirkin vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); 179112e57169SMichael S. Tsirkin vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 17922af7698eSAlex Williamson 179312e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); 17942af7698eSAlex Williamson 17952af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1796d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 17972af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 179812e57169SMichael S. Tsirkin vi->ctrl->promisc ? "en" : "dis"); 17992af7698eSAlex Williamson 180012e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); 18012af7698eSAlex Williamson 18022af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1803d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 18042af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 180512e57169SMichael S. Tsirkin vi->ctrl->allmulti ? "en" : "dis"); 1806f565a7c2SAlex Williamson 180732e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 18084cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1809f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 18104cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1811f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 18124cd24eafSJiri Pirko mac_data = buf; 1813e68ed8f0SJoe Perches if (!buf) 1814f565a7c2SAlex Williamson return; 1815f565a7c2SAlex Williamson 181623e258e1SAlex Williamson sg_init_table(sg, 2); 181723e258e1SAlex Williamson 1818f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 1819fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1820ccffad25SJiri Pirko i = 0; 182132e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1822ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1823f565a7c2SAlex Williamson 1824f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 182532e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1826f565a7c2SAlex Williamson 1827f565a7c2SAlex Williamson /* multicast list and count fill the end */ 182832e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1829f565a7c2SAlex Williamson 1830fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1831567ec874SJiri Pirko i = 0; 183222bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 183322bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1834f565a7c2SAlex Williamson 1835f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 18364cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1837f565a7c2SAlex Williamson 1838f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1839d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 184099e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1841f565a7c2SAlex Williamson 1842f565a7c2SAlex Williamson kfree(buf); 18432af7698eSAlex Williamson } 18442af7698eSAlex Williamson 184580d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 184680d5c368SPatrick McHardy __be16 proto, u16 vid) 18470bde9569SAlex Williamson { 18480bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 18490bde9569SAlex Williamson struct scatterlist sg; 18500bde9569SAlex Williamson 1851d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 185212e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 18530bde9569SAlex Williamson 18540bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1855d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 18560bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 18578e586137SJiri Pirko return 0; 18580bde9569SAlex Williamson } 18590bde9569SAlex Williamson 186080d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 186180d5c368SPatrick McHardy __be16 proto, u16 vid) 18620bde9569SAlex Williamson { 18630bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 18640bde9569SAlex Williamson struct scatterlist sg; 18650bde9569SAlex Williamson 1866d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 186712e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 18680bde9569SAlex Williamson 18690bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1870d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 18710bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 18728e586137SJiri Pirko return 0; 18730bde9569SAlex Williamson } 18740bde9569SAlex Williamson 18758898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1876986a4f4dSJason Wang { 1877986a4f4dSJason Wang int i; 18788898c21cSWanlong Gao 18798898c21cSWanlong Gao if (vi->affinity_hint_set) { 18808898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 1881*19e226e8SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, NULL); 1882*19e226e8SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, NULL); 18838898c21cSWanlong Gao } 18848898c21cSWanlong Gao 18858898c21cSWanlong Gao vi->affinity_hint_set = false; 18868898c21cSWanlong Gao } 18878898c21cSWanlong Gao } 18888898c21cSWanlong Gao 18898898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1890986a4f4dSJason Wang { 1891986a4f4dSJason Wang int i; 189247be2479SWanlong Gao int cpu; 1893986a4f4dSJason Wang 1894986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1895986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1896986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1897986a4f4dSJason Wang */ 18988898c21cSWanlong Gao if (vi->curr_queue_pairs == 1 || 18998898c21cSWanlong Gao vi->max_queue_pairs != num_online_cpus()) { 19008898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1901986a4f4dSJason Wang return; 1902986a4f4dSJason Wang } 1903986a4f4dSJason Wang 190447be2479SWanlong Gao i = 0; 190547be2479SWanlong Gao for_each_online_cpu(cpu) { 19064d99f660SAndrei Vagin const unsigned long *mask = cpumask_bits(cpumask_of(cpu)); 19074d99f660SAndrei Vagin 1908*19e226e8SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, cpumask_of(cpu)); 1909*19e226e8SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, cpumask_of(cpu)); 19104d99f660SAndrei Vagin __netif_set_xps_queue(vi->dev, mask, i, false); 191147be2479SWanlong Gao i++; 1912986a4f4dSJason Wang } 1913986a4f4dSJason Wang 1914986a4f4dSJason Wang vi->affinity_hint_set = true; 191547be2479SWanlong Gao } 1916986a4f4dSJason Wang 19178017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 19188de4b2f3SWanlong Gao { 19198017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 19208017c279SSebastian Andrzej Siewior node); 19218de4b2f3SWanlong Gao virtnet_set_affinity(vi); 19228017c279SSebastian Andrzej Siewior return 0; 19238de4b2f3SWanlong Gao } 19243ab098dfSJason Wang 19258017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 19268017c279SSebastian Andrzej Siewior { 19278017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 19288017c279SSebastian Andrzej Siewior node_dead); 19298017c279SSebastian Andrzej Siewior virtnet_set_affinity(vi); 19308017c279SSebastian Andrzej Siewior return 0; 19318017c279SSebastian Andrzej Siewior } 19328017c279SSebastian Andrzej Siewior 19338017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 19348017c279SSebastian Andrzej Siewior { 19358017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 19368017c279SSebastian Andrzej Siewior node); 19378017c279SSebastian Andrzej Siewior 19388017c279SSebastian Andrzej Siewior virtnet_clean_affinity(vi, cpu); 19398017c279SSebastian Andrzej Siewior return 0; 19408017c279SSebastian Andrzej Siewior } 19418017c279SSebastian Andrzej Siewior 19428017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online; 19438017c279SSebastian Andrzej Siewior 19448017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi) 19458017c279SSebastian Andrzej Siewior { 19468017c279SSebastian Andrzej Siewior int ret; 19478017c279SSebastian Andrzej Siewior 19488017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 19498017c279SSebastian Andrzej Siewior if (ret) 19508017c279SSebastian Andrzej Siewior return ret; 19518017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 19528017c279SSebastian Andrzej Siewior &vi->node_dead); 19538017c279SSebastian Andrzej Siewior if (!ret) 19548017c279SSebastian Andrzej Siewior return ret; 19558017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 19568017c279SSebastian Andrzej Siewior return ret; 19578017c279SSebastian Andrzej Siewior } 19588017c279SSebastian Andrzej Siewior 19598017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 19608017c279SSebastian Andrzej Siewior { 19618017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 19628017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 19638017c279SSebastian Andrzej Siewior &vi->node_dead); 1964a9ea3fc6SHerbert Xu } 1965a9ea3fc6SHerbert Xu 19668f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 19678f9f4668SRick Jones struct ethtool_ringparam *ring) 19688f9f4668SRick Jones { 19698f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 19708f9f4668SRick Jones 1971986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1972986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 19738f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 19748f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 19758f9f4668SRick Jones } 19768f9f4668SRick Jones 197766846048SRick Jones 197866846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 197966846048SRick Jones struct ethtool_drvinfo *info) 198066846048SRick Jones { 198166846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 198266846048SRick Jones struct virtio_device *vdev = vi->vdev; 198366846048SRick Jones 198466846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 198566846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 198666846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 198766846048SRick Jones 198866846048SRick Jones } 198966846048SRick Jones 1990d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1991d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1992d73bcd2cSJason Wang struct ethtool_channels *channels) 1993d73bcd2cSJason Wang { 1994d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1995d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1996d73bcd2cSJason Wang int err; 1997d73bcd2cSJason Wang 1998d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1999d73bcd2cSJason Wang * We don't allow setting 'other' channels. 2000d73bcd2cSJason Wang */ 2001d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 2002d73bcd2cSJason Wang return -EINVAL; 2003d73bcd2cSJason Wang 2004c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 2005d73bcd2cSJason Wang return -EINVAL; 2006d73bcd2cSJason Wang 2007f600b690SJohn Fastabend /* For now we don't support modifying channels while XDP is loaded 2008f600b690SJohn Fastabend * also when XDP is loaded all RX queues have XDP programs so we only 2009f600b690SJohn Fastabend * need to check a single RX queue. 2010f600b690SJohn Fastabend */ 2011f600b690SJohn Fastabend if (vi->rq[0].xdp_prog) 2012f600b690SJohn Fastabend return -EINVAL; 2013f600b690SJohn Fastabend 201447be2479SWanlong Gao get_online_cpus(); 201547315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 2016d73bcd2cSJason Wang if (!err) { 2017d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 2018d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 2019d73bcd2cSJason Wang 20208898c21cSWanlong Gao virtnet_set_affinity(vi); 2021d73bcd2cSJason Wang } 202247be2479SWanlong Gao put_online_cpus(); 2023d73bcd2cSJason Wang 2024d73bcd2cSJason Wang return err; 2025d73bcd2cSJason Wang } 2026d73bcd2cSJason Wang 2027d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 2028d7dfc5cfSToshiaki Makita { 2029d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2030d7dfc5cfSToshiaki Makita char *p = (char *)data; 2031d7dfc5cfSToshiaki Makita unsigned int i, j; 2032d7dfc5cfSToshiaki Makita 2033d7dfc5cfSToshiaki Makita switch (stringset) { 2034d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2035d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2036d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2037d7dfc5cfSToshiaki Makita snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s", 2038d7dfc5cfSToshiaki Makita i, virtnet_rq_stats_desc[j].desc); 2039d7dfc5cfSToshiaki Makita p += ETH_GSTRING_LEN; 2040d7dfc5cfSToshiaki Makita } 2041d7dfc5cfSToshiaki Makita } 2042d7dfc5cfSToshiaki Makita 2043d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2044d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 2045d7dfc5cfSToshiaki Makita snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s", 2046d7dfc5cfSToshiaki Makita i, virtnet_sq_stats_desc[j].desc); 2047d7dfc5cfSToshiaki Makita p += ETH_GSTRING_LEN; 2048d7dfc5cfSToshiaki Makita } 2049d7dfc5cfSToshiaki Makita } 2050d7dfc5cfSToshiaki Makita break; 2051d7dfc5cfSToshiaki Makita } 2052d7dfc5cfSToshiaki Makita } 2053d7dfc5cfSToshiaki Makita 2054d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset) 2055d7dfc5cfSToshiaki Makita { 2056d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2057d7dfc5cfSToshiaki Makita 2058d7dfc5cfSToshiaki Makita switch (sset) { 2059d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2060d7dfc5cfSToshiaki Makita return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + 2061d7dfc5cfSToshiaki Makita VIRTNET_SQ_STATS_LEN); 2062d7dfc5cfSToshiaki Makita default: 2063d7dfc5cfSToshiaki Makita return -EOPNOTSUPP; 2064d7dfc5cfSToshiaki Makita } 2065d7dfc5cfSToshiaki Makita } 2066d7dfc5cfSToshiaki Makita 2067d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev, 2068d7dfc5cfSToshiaki Makita struct ethtool_stats *stats, u64 *data) 2069d7dfc5cfSToshiaki Makita { 2070d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2071d7dfc5cfSToshiaki Makita unsigned int idx = 0, start, i, j; 2072d7dfc5cfSToshiaki Makita const u8 *stats_base; 2073d7dfc5cfSToshiaki Makita size_t offset; 2074d7dfc5cfSToshiaki Makita 2075d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2076d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 2077d7dfc5cfSToshiaki Makita 2078d46eeeafSJason Wang stats_base = (u8 *)&rq->stats; 2079d7dfc5cfSToshiaki Makita do { 2080d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 2081d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2082d7dfc5cfSToshiaki Makita offset = virtnet_rq_stats_desc[j].offset; 2083d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2084d7dfc5cfSToshiaki Makita } 2085d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 2086d7dfc5cfSToshiaki Makita idx += VIRTNET_RQ_STATS_LEN; 2087d7dfc5cfSToshiaki Makita } 2088d7dfc5cfSToshiaki Makita 2089d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2090d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 2091d7dfc5cfSToshiaki Makita 2092d7dfc5cfSToshiaki Makita stats_base = (u8 *)&sq->stats; 2093d7dfc5cfSToshiaki Makita do { 2094d7dfc5cfSToshiaki Makita start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 2095d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 2096d7dfc5cfSToshiaki Makita offset = virtnet_sq_stats_desc[j].offset; 2097d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2098d7dfc5cfSToshiaki Makita } 2099d7dfc5cfSToshiaki Makita } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 2100d7dfc5cfSToshiaki Makita idx += VIRTNET_SQ_STATS_LEN; 2101d7dfc5cfSToshiaki Makita } 2102d7dfc5cfSToshiaki Makita } 2103d7dfc5cfSToshiaki Makita 2104d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 2105d73bcd2cSJason Wang struct ethtool_channels *channels) 2106d73bcd2cSJason Wang { 2107d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2108d73bcd2cSJason Wang 2109d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 2110d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 2111d73bcd2cSJason Wang channels->max_other = 0; 2112d73bcd2cSJason Wang channels->rx_count = 0; 2113d73bcd2cSJason Wang channels->tx_count = 0; 2114d73bcd2cSJason Wang channels->other_count = 0; 2115d73bcd2cSJason Wang } 2116d73bcd2cSJason Wang 211716032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */ 2118ebb6b4b1SPhilippe Reynes static bool 2119ebb6b4b1SPhilippe Reynes virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd) 212016032be5SNikolay Aleksandrov { 2121ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings diff1 = *cmd; 2122ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings diff2 = {}; 212316032be5SNikolay Aleksandrov 21240cf3ace9SNikolay Aleksandrov /* cmd is always set so we need to clear it, validate the port type 21250cf3ace9SNikolay Aleksandrov * and also without autonegotiation we can ignore advertising 21260cf3ace9SNikolay Aleksandrov */ 2127ebb6b4b1SPhilippe Reynes diff1.base.speed = 0; 2128ebb6b4b1SPhilippe Reynes diff2.base.port = PORT_OTHER; 2129ebb6b4b1SPhilippe Reynes ethtool_link_ksettings_zero_link_mode(&diff1, advertising); 2130ebb6b4b1SPhilippe Reynes diff1.base.duplex = 0; 2131ebb6b4b1SPhilippe Reynes diff1.base.cmd = 0; 2132ebb6b4b1SPhilippe Reynes diff1.base.link_mode_masks_nwords = 0; 213316032be5SNikolay Aleksandrov 2134ebb6b4b1SPhilippe Reynes return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) && 2135ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.supported, 2136ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS) && 2137ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.advertising, 2138ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS) && 2139ebb6b4b1SPhilippe Reynes bitmap_empty(diff1.link_modes.lp_advertising, 2140ebb6b4b1SPhilippe Reynes __ETHTOOL_LINK_MODE_MASK_NBITS); 214116032be5SNikolay Aleksandrov } 214216032be5SNikolay Aleksandrov 2143ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev, 2144ebb6b4b1SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 214516032be5SNikolay Aleksandrov { 214616032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 214716032be5SNikolay Aleksandrov u32 speed; 214816032be5SNikolay Aleksandrov 2149ebb6b4b1SPhilippe Reynes speed = cmd->base.speed; 215016032be5SNikolay Aleksandrov /* don't allow custom speed and duplex */ 215116032be5SNikolay Aleksandrov if (!ethtool_validate_speed(speed) || 2152ebb6b4b1SPhilippe Reynes !ethtool_validate_duplex(cmd->base.duplex) || 215316032be5SNikolay Aleksandrov !virtnet_validate_ethtool_cmd(cmd)) 215416032be5SNikolay Aleksandrov return -EINVAL; 215516032be5SNikolay Aleksandrov vi->speed = speed; 2156ebb6b4b1SPhilippe Reynes vi->duplex = cmd->base.duplex; 215716032be5SNikolay Aleksandrov 215816032be5SNikolay Aleksandrov return 0; 215916032be5SNikolay Aleksandrov } 216016032be5SNikolay Aleksandrov 2161ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev, 2162ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings *cmd) 216316032be5SNikolay Aleksandrov { 216416032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 216516032be5SNikolay Aleksandrov 2166ebb6b4b1SPhilippe Reynes cmd->base.speed = vi->speed; 2167ebb6b4b1SPhilippe Reynes cmd->base.duplex = vi->duplex; 2168ebb6b4b1SPhilippe Reynes cmd->base.port = PORT_OTHER; 216916032be5SNikolay Aleksandrov 217016032be5SNikolay Aleksandrov return 0; 217116032be5SNikolay Aleksandrov } 217216032be5SNikolay Aleksandrov 217316032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 217416032be5SNikolay Aleksandrov { 217516032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 217616032be5SNikolay Aleksandrov 217716032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 217816032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 217916032be5SNikolay Aleksandrov } 218016032be5SNikolay Aleksandrov 2181faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi) 2182faa9b39fSJason Baron { 2183faa9b39fSJason Baron u32 speed; 2184faa9b39fSJason Baron u8 duplex; 2185faa9b39fSJason Baron 2186faa9b39fSJason Baron if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 2187faa9b39fSJason Baron return; 2188faa9b39fSJason Baron 2189faa9b39fSJason Baron speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config, 2190faa9b39fSJason Baron speed)); 2191faa9b39fSJason Baron if (ethtool_validate_speed(speed)) 2192faa9b39fSJason Baron vi->speed = speed; 2193faa9b39fSJason Baron duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config, 2194faa9b39fSJason Baron duplex)); 2195faa9b39fSJason Baron if (ethtool_validate_duplex(duplex)) 2196faa9b39fSJason Baron vi->duplex = duplex; 2197faa9b39fSJason Baron } 2198faa9b39fSJason Baron 21990fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 220066846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 22019f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 22028f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 2203d7dfc5cfSToshiaki Makita .get_strings = virtnet_get_strings, 2204d7dfc5cfSToshiaki Makita .get_sset_count = virtnet_get_sset_count, 2205d7dfc5cfSToshiaki Makita .get_ethtool_stats = virtnet_get_ethtool_stats, 2206d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 2207d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 2208074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 2209ebb6b4b1SPhilippe Reynes .get_link_ksettings = virtnet_get_link_ksettings, 2210ebb6b4b1SPhilippe Reynes .set_link_ksettings = virtnet_set_link_ksettings, 2211a9ea3fc6SHerbert Xu }; 2212a9ea3fc6SHerbert Xu 22139fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev) 22149fe7bfceSJohn Fastabend { 22159fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 22169fe7bfceSJohn Fastabend int i; 22179fe7bfceSJohn Fastabend 22189fe7bfceSJohn Fastabend /* Make sure no work handler is accessing the device */ 22199fe7bfceSJohn Fastabend flush_work(&vi->config_work); 22209fe7bfceSJohn Fastabend 22219fe7bfceSJohn Fastabend netif_device_detach(vi->dev); 2222713a98d9SJason Wang netif_tx_disable(vi->dev); 22239fe7bfceSJohn Fastabend cancel_delayed_work_sync(&vi->refill); 22249fe7bfceSJohn Fastabend 22259fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 2226b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 22279fe7bfceSJohn Fastabend napi_disable(&vi->rq[i].napi); 222878a57b48SWillem de Bruijn virtnet_napi_tx_disable(&vi->sq[i].napi); 2229b92f1e67SWillem de Bruijn } 22309fe7bfceSJohn Fastabend } 22319fe7bfceSJohn Fastabend } 22329fe7bfceSJohn Fastabend 22339fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi); 22349fe7bfceSJohn Fastabend 22359fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev) 22369fe7bfceSJohn Fastabend { 22379fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 22389fe7bfceSJohn Fastabend int err, i; 22399fe7bfceSJohn Fastabend 22409fe7bfceSJohn Fastabend err = init_vqs(vi); 22419fe7bfceSJohn Fastabend if (err) 22429fe7bfceSJohn Fastabend return err; 22439fe7bfceSJohn Fastabend 22449fe7bfceSJohn Fastabend virtio_device_ready(vdev); 22459fe7bfceSJohn Fastabend 22469fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 22479fe7bfceSJohn Fastabend for (i = 0; i < vi->curr_queue_pairs; i++) 22489fe7bfceSJohn Fastabend if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 22499fe7bfceSJohn Fastabend schedule_delayed_work(&vi->refill, 0); 22509fe7bfceSJohn Fastabend 2251b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 2252e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2253b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2254b92f1e67SWillem de Bruijn &vi->sq[i].napi); 2255b92f1e67SWillem de Bruijn } 22569fe7bfceSJohn Fastabend } 22579fe7bfceSJohn Fastabend 22589fe7bfceSJohn Fastabend netif_device_attach(vi->dev); 22599fe7bfceSJohn Fastabend return err; 22609fe7bfceSJohn Fastabend } 22619fe7bfceSJohn Fastabend 22623f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 22633f93522fSJason Wang { 22643f93522fSJason Wang struct scatterlist sg; 226512e57169SMichael S. Tsirkin vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); 22663f93522fSJason Wang 226712e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); 22683f93522fSJason Wang 22693f93522fSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 22703f93522fSJason Wang VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 22713f93522fSJason Wang dev_warn(&vi->dev->dev, "Fail to set guest offload. \n"); 22723f93522fSJason Wang return -EINVAL; 22733f93522fSJason Wang } 22743f93522fSJason Wang 22753f93522fSJason Wang return 0; 22763f93522fSJason Wang } 22773f93522fSJason Wang 22783f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 22793f93522fSJason Wang { 22803f93522fSJason Wang u64 offloads = 0; 22813f93522fSJason Wang 22823f93522fSJason Wang if (!vi->guest_offloads) 22833f93522fSJason Wang return 0; 22843f93522fSJason Wang 22853f93522fSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM)) 22863f93522fSJason Wang offloads = 1ULL << VIRTIO_NET_F_GUEST_CSUM; 22873f93522fSJason Wang 22883f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 22893f93522fSJason Wang } 22903f93522fSJason Wang 22913f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 22923f93522fSJason Wang { 22933f93522fSJason Wang u64 offloads = vi->guest_offloads; 22943f93522fSJason Wang 22953f93522fSJason Wang if (!vi->guest_offloads) 22963f93522fSJason Wang return 0; 22973f93522fSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM)) 22983f93522fSJason Wang offloads |= 1ULL << VIRTIO_NET_F_GUEST_CSUM; 22993f93522fSJason Wang 23003f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 23013f93522fSJason Wang } 23023f93522fSJason Wang 23039861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 23049861ce03SJakub Kicinski struct netlink_ext_ack *extack) 2305f600b690SJohn Fastabend { 2306f600b690SJohn Fastabend unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); 2307f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 2308f600b690SJohn Fastabend struct bpf_prog *old_prog; 2309017b29c3SJason Wang u16 xdp_qp = 0, curr_qp; 2310672aafd5SJohn Fastabend int i, err; 2311f600b690SJohn Fastabend 23123f93522fSJason Wang if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 23133f93522fSJason Wang && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 231492502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 231592502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 23163f93522fSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO))) { 23174d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first"); 2318f600b690SJohn Fastabend return -EOPNOTSUPP; 2319f600b690SJohn Fastabend } 2320f600b690SJohn Fastabend 2321f600b690SJohn Fastabend if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 23224d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 2323f600b690SJohn Fastabend return -EINVAL; 2324f600b690SJohn Fastabend } 2325f600b690SJohn Fastabend 2326f600b690SJohn Fastabend if (dev->mtu > max_sz) { 23274d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); 2328f600b690SJohn Fastabend netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); 2329f600b690SJohn Fastabend return -EINVAL; 2330f600b690SJohn Fastabend } 2331f600b690SJohn Fastabend 2332672aafd5SJohn Fastabend curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 2333672aafd5SJohn Fastabend if (prog) 2334672aafd5SJohn Fastabend xdp_qp = nr_cpu_ids; 2335672aafd5SJohn Fastabend 2336672aafd5SJohn Fastabend /* XDP requires extra queues for XDP_TX */ 2337672aafd5SJohn Fastabend if (curr_qp + xdp_qp > vi->max_queue_pairs) { 23384d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available"); 2339672aafd5SJohn Fastabend netdev_warn(dev, "request %i queues but max is %i\n", 2340672aafd5SJohn Fastabend curr_qp + xdp_qp, vi->max_queue_pairs); 2341672aafd5SJohn Fastabend return -ENOMEM; 2342672aafd5SJohn Fastabend } 2343672aafd5SJohn Fastabend 23442de2f7f4SJohn Fastabend if (prog) { 23452de2f7f4SJohn Fastabend prog = bpf_prog_add(prog, vi->max_queue_pairs - 1); 23462de2f7f4SJohn Fastabend if (IS_ERR(prog)) 23472de2f7f4SJohn Fastabend return PTR_ERR(prog); 23482de2f7f4SJohn Fastabend } 23492de2f7f4SJohn Fastabend 23504941d472SJason Wang /* Make sure NAPI is not using any XDP TX queues for RX. */ 23514e09ff53SJason Wang if (netif_running(dev)) 23524941d472SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 23534941d472SJason Wang napi_disable(&vi->rq[i].napi); 23542de2f7f4SJohn Fastabend 2355672aafd5SJohn Fastabend netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 23564941d472SJason Wang err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 23574941d472SJason Wang if (err) 23584941d472SJason Wang goto err; 23594941d472SJason Wang vi->xdp_queue_pairs = xdp_qp; 2360f600b690SJohn Fastabend 2361f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 2362f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2363f600b690SJohn Fastabend rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 23643f93522fSJason Wang if (i == 0) { 23653f93522fSJason Wang if (!old_prog) 23663f93522fSJason Wang virtnet_clear_guest_offloads(vi); 23673f93522fSJason Wang if (!prog) 23683f93522fSJason Wang virtnet_restore_guest_offloads(vi); 23693f93522fSJason Wang } 2370f600b690SJohn Fastabend if (old_prog) 2371f600b690SJohn Fastabend bpf_prog_put(old_prog); 23724e09ff53SJason Wang if (netif_running(dev)) 23734941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2374f600b690SJohn Fastabend } 2375f600b690SJohn Fastabend 2376f600b690SJohn Fastabend return 0; 23772de2f7f4SJohn Fastabend 23784941d472SJason Wang err: 23794941d472SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 23804941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 23812de2f7f4SJohn Fastabend if (prog) 23822de2f7f4SJohn Fastabend bpf_prog_sub(prog, vi->max_queue_pairs - 1); 23832de2f7f4SJohn Fastabend return err; 2384f600b690SJohn Fastabend } 2385f600b690SJohn Fastabend 23865b0e6629SMartin KaFai Lau static u32 virtnet_xdp_query(struct net_device *dev) 2387f600b690SJohn Fastabend { 2388f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 23895b0e6629SMartin KaFai Lau const struct bpf_prog *xdp_prog; 2390f600b690SJohn Fastabend int i; 2391f600b690SJohn Fastabend 2392f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 23935b0e6629SMartin KaFai Lau xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog); 23945b0e6629SMartin KaFai Lau if (xdp_prog) 23955b0e6629SMartin KaFai Lau return xdp_prog->aux->id; 2396f600b690SJohn Fastabend } 23975b0e6629SMartin KaFai Lau return 0; 2398f600b690SJohn Fastabend } 2399f600b690SJohn Fastabend 2400f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 2401f600b690SJohn Fastabend { 2402f600b690SJohn Fastabend switch (xdp->command) { 2403f600b690SJohn Fastabend case XDP_SETUP_PROG: 24049861ce03SJakub Kicinski return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 2405f600b690SJohn Fastabend case XDP_QUERY_PROG: 24065b0e6629SMartin KaFai Lau xdp->prog_id = virtnet_xdp_query(dev); 2407f600b690SJohn Fastabend return 0; 2408f600b690SJohn Fastabend default: 2409f600b690SJohn Fastabend return -EINVAL; 2410f600b690SJohn Fastabend } 2411f600b690SJohn Fastabend } 2412f600b690SJohn Fastabend 2413ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 2414ba5e4426SSridhar Samudrala size_t len) 2415ba5e4426SSridhar Samudrala { 2416ba5e4426SSridhar Samudrala struct virtnet_info *vi = netdev_priv(dev); 2417ba5e4426SSridhar Samudrala int ret; 2418ba5e4426SSridhar Samudrala 2419ba5e4426SSridhar Samudrala if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 2420ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2421ba5e4426SSridhar Samudrala 2422ba5e4426SSridhar Samudrala ret = snprintf(buf, len, "sby"); 2423ba5e4426SSridhar Samudrala if (ret >= len) 2424ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2425ba5e4426SSridhar Samudrala 2426ba5e4426SSridhar Samudrala return 0; 2427ba5e4426SSridhar Samudrala } 2428ba5e4426SSridhar Samudrala 242976288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 243076288b4eSStephen Hemminger .ndo_open = virtnet_open, 243176288b4eSStephen Hemminger .ndo_stop = virtnet_close, 243276288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 243376288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 24349c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 24352af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 24363fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 24371824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 24381824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 243976288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 244076288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 244176288b4eSStephen Hemminger #endif 2442f4e63525SJakub Kicinski .ndo_bpf = virtnet_xdp, 2443186b3c99SJason Wang .ndo_xdp_xmit = virtnet_xdp_xmit, 24442836b4f2SVlad Yasevich .ndo_features_check = passthru_features_check, 2445ba5e4426SSridhar Samudrala .ndo_get_phys_port_name = virtnet_get_phys_port_name, 244676288b4eSStephen Hemminger }; 244776288b4eSStephen Hemminger 2448586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 24499f4d26d0SMark McLoughlin { 2450586d17c5SJason Wang struct virtnet_info *vi = 2451586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 24529f4d26d0SMark McLoughlin u16 v; 24539f4d26d0SMark McLoughlin 2454855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 2455855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 2456507613bfSMichael S. Tsirkin return; 2457586d17c5SJason Wang 2458586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 2459ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 2460586d17c5SJason Wang virtnet_ack_link_announce(vi); 2461586d17c5SJason Wang } 24629f4d26d0SMark McLoughlin 24639f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 24649f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 24659f4d26d0SMark McLoughlin 24669f4d26d0SMark McLoughlin if (vi->status == v) 2467507613bfSMichael S. Tsirkin return; 24689f4d26d0SMark McLoughlin 24699f4d26d0SMark McLoughlin vi->status = v; 24709f4d26d0SMark McLoughlin 24719f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 2472faa9b39fSJason Baron virtnet_update_settings(vi); 24739f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 2474986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 24759f4d26d0SMark McLoughlin } else { 24769f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 2477986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 24789f4d26d0SMark McLoughlin } 24799f4d26d0SMark McLoughlin } 24809f4d26d0SMark McLoughlin 24819f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 24829f4d26d0SMark McLoughlin { 24839f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 24849f4d26d0SMark McLoughlin 24853b07e9caSTejun Heo schedule_work(&vi->config_work); 24869f4d26d0SMark McLoughlin } 24879f4d26d0SMark McLoughlin 2488986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 2489986a4f4dSJason Wang { 2490d4fb84eeSAndrey Vagin int i; 2491d4fb84eeSAndrey Vagin 2492ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2493ab3971b1SJason Wang napi_hash_del(&vi->rq[i].napi); 2494d4fb84eeSAndrey Vagin netif_napi_del(&vi->rq[i].napi); 2495b92f1e67SWillem de Bruijn netif_napi_del(&vi->sq[i].napi); 2496ab3971b1SJason Wang } 2497d4fb84eeSAndrey Vagin 2498963abe5cSEric Dumazet /* We called napi_hash_del() before netif_napi_del(), 2499963abe5cSEric Dumazet * we need to respect an RCU grace period before freeing vi->rq 2500963abe5cSEric Dumazet */ 2501963abe5cSEric Dumazet synchronize_net(); 2502963abe5cSEric Dumazet 2503986a4f4dSJason Wang kfree(vi->rq); 2504986a4f4dSJason Wang kfree(vi->sq); 250512e57169SMichael S. Tsirkin kfree(vi->ctrl); 2506986a4f4dSJason Wang } 2507986a4f4dSJason Wang 250847315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi) 2509986a4f4dSJason Wang { 2510f600b690SJohn Fastabend struct bpf_prog *old_prog; 2511986a4f4dSJason Wang int i; 2512986a4f4dSJason Wang 2513986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2514986a4f4dSJason Wang while (vi->rq[i].pages) 2515986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 2516f600b690SJohn Fastabend 2517f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2518f600b690SJohn Fastabend RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 2519f600b690SJohn Fastabend if (old_prog) 2520f600b690SJohn Fastabend bpf_prog_put(old_prog); 2521986a4f4dSJason Wang } 252247315329SJohn Fastabend } 252347315329SJohn Fastabend 252447315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi) 252547315329SJohn Fastabend { 252647315329SJohn Fastabend rtnl_lock(); 252747315329SJohn Fastabend _free_receive_bufs(vi); 2528f600b690SJohn Fastabend rtnl_unlock(); 2529986a4f4dSJason Wang } 2530986a4f4dSJason Wang 2531fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 2532fb51879dSMichael Dalton { 2533fb51879dSMichael Dalton int i; 2534fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 2535fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 2536fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 2537fb51879dSMichael Dalton } 2538fb51879dSMichael Dalton 2539b68df015SJohn Fastabend static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 254056434a01SJohn Fastabend { 254156434a01SJohn Fastabend if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 254256434a01SJohn Fastabend return false; 254356434a01SJohn Fastabend else if (q < vi->curr_queue_pairs) 254456434a01SJohn Fastabend return true; 254556434a01SJohn Fastabend else 254656434a01SJohn Fastabend return false; 254756434a01SJohn Fastabend } 254856434a01SJohn Fastabend 2549986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 2550986a4f4dSJason Wang { 2551986a4f4dSJason Wang void *buf; 2552986a4f4dSJason Wang int i; 2553986a4f4dSJason Wang 2554986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2555986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 255656434a01SJohn Fastabend while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2557b68df015SJohn Fastabend if (!is_xdp_raw_buffer_queue(vi, i)) 2558986a4f4dSJason Wang dev_kfree_skb(buf); 255956434a01SJohn Fastabend else 256056434a01SJohn Fastabend put_page(virt_to_head_page(buf)); 256156434a01SJohn Fastabend } 2562986a4f4dSJason Wang } 2563986a4f4dSJason Wang 2564986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2565986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 2566986a4f4dSJason Wang 2567986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2568ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 2569680557cfSMichael S. Tsirkin put_page(virt_to_head_page(buf)); 2570ab7db917SMichael Dalton } else if (vi->big_packets) { 2571fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 2572ab7db917SMichael Dalton } else { 2573f6b10209SJason Wang put_page(virt_to_head_page(buf)); 2574986a4f4dSJason Wang } 2575986a4f4dSJason Wang } 2576986a4f4dSJason Wang } 2577ab7db917SMichael Dalton } 2578986a4f4dSJason Wang 2579e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 2580e9d7417bSJason Wang { 2581e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 2582e9d7417bSJason Wang 25838898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 2584986a4f4dSJason Wang 2585e9d7417bSJason Wang vdev->config->del_vqs(vdev); 2586986a4f4dSJason Wang 2587986a4f4dSJason Wang virtnet_free_queues(vi); 2588986a4f4dSJason Wang } 2589986a4f4dSJason Wang 2590d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at 2591d85b758fSMichael S. Tsirkin * least one full packet? 2592d85b758fSMichael S. Tsirkin * Logic below assumes the mergeable buffer header is used. 2593d85b758fSMichael S. Tsirkin */ 2594d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 2595d85b758fSMichael S. Tsirkin { 2596d85b758fSMichael S. Tsirkin const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2597d85b758fSMichael S. Tsirkin unsigned int rq_size = virtqueue_get_vring_size(vq); 2598d85b758fSMichael S. Tsirkin unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 2599d85b758fSMichael S. Tsirkin unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 2600d85b758fSMichael S. Tsirkin unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 2601d85b758fSMichael S. Tsirkin 2602f0c3192cSMichael S. Tsirkin return max(max(min_buf_len, hdr_len) - hdr_len, 2603f0c3192cSMichael S. Tsirkin (unsigned int)GOOD_PACKET_LEN); 2604d85b758fSMichael S. Tsirkin } 2605d85b758fSMichael S. Tsirkin 2606986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 2607986a4f4dSJason Wang { 2608986a4f4dSJason Wang vq_callback_t **callbacks; 2609986a4f4dSJason Wang struct virtqueue **vqs; 2610986a4f4dSJason Wang int ret = -ENOMEM; 2611986a4f4dSJason Wang int i, total_vqs; 2612986a4f4dSJason Wang const char **names; 2613d45b897bSMichael S. Tsirkin bool *ctx; 2614986a4f4dSJason Wang 2615986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 2616986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 2617986a4f4dSJason Wang * possible control vq. 2618986a4f4dSJason Wang */ 2619986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 2620986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 2621986a4f4dSJason Wang 2622986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 26236396bb22SKees Cook vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 2624986a4f4dSJason Wang if (!vqs) 2625986a4f4dSJason Wang goto err_vq; 26266da2ec56SKees Cook callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL); 2627986a4f4dSJason Wang if (!callbacks) 2628986a4f4dSJason Wang goto err_callback; 26296da2ec56SKees Cook names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL); 2630986a4f4dSJason Wang if (!names) 2631986a4f4dSJason Wang goto err_names; 2632192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 26336396bb22SKees Cook ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 2634d45b897bSMichael S. Tsirkin if (!ctx) 2635d45b897bSMichael S. Tsirkin goto err_ctx; 2636d45b897bSMichael S. Tsirkin } else { 2637d45b897bSMichael S. Tsirkin ctx = NULL; 2638d45b897bSMichael S. Tsirkin } 2639986a4f4dSJason Wang 2640986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 2641986a4f4dSJason Wang if (vi->has_cvq) { 2642986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 2643986a4f4dSJason Wang names[total_vqs - 1] = "control"; 2644986a4f4dSJason Wang } 2645986a4f4dSJason Wang 2646986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 2647986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2648986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 2649986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 2650986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 2651986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 2652986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 2653986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 2654d45b897bSMichael S. Tsirkin if (ctx) 2655d45b897bSMichael S. Tsirkin ctx[rxq2vq(i)] = true; 2656986a4f4dSJason Wang } 2657986a4f4dSJason Wang 2658986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 2659d45b897bSMichael S. Tsirkin names, ctx, NULL); 2660986a4f4dSJason Wang if (ret) 2661986a4f4dSJason Wang goto err_find; 2662986a4f4dSJason Wang 2663986a4f4dSJason Wang if (vi->has_cvq) { 2664986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 2665986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 2666f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2667986a4f4dSJason Wang } 2668986a4f4dSJason Wang 2669986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2670986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 2671d85b758fSMichael S. Tsirkin vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 2672986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 2673986a4f4dSJason Wang } 2674986a4f4dSJason Wang 26752fa3c8a8STonghao Zhang /* run here: ret == 0. */ 2676986a4f4dSJason Wang 2677986a4f4dSJason Wang 2678986a4f4dSJason Wang err_find: 2679d45b897bSMichael S. Tsirkin kfree(ctx); 2680d45b897bSMichael S. Tsirkin err_ctx: 2681986a4f4dSJason Wang kfree(names); 2682986a4f4dSJason Wang err_names: 2683986a4f4dSJason Wang kfree(callbacks); 2684986a4f4dSJason Wang err_callback: 2685986a4f4dSJason Wang kfree(vqs); 2686986a4f4dSJason Wang err_vq: 2687986a4f4dSJason Wang return ret; 2688986a4f4dSJason Wang } 2689986a4f4dSJason Wang 2690986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 2691986a4f4dSJason Wang { 2692986a4f4dSJason Wang int i; 2693986a4f4dSJason Wang 269412e57169SMichael S. Tsirkin vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 269512e57169SMichael S. Tsirkin if (!vi->ctrl) 269612e57169SMichael S. Tsirkin goto err_ctrl; 26976396bb22SKees Cook vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 2698986a4f4dSJason Wang if (!vi->sq) 2699986a4f4dSJason Wang goto err_sq; 27006396bb22SKees Cook vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 2701008d4278SAmerigo Wang if (!vi->rq) 2702986a4f4dSJason Wang goto err_rq; 2703986a4f4dSJason Wang 2704986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 2705986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 2706986a4f4dSJason Wang vi->rq[i].pages = NULL; 2707986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2708986a4f4dSJason Wang napi_weight); 27091d11e732SWillem de Bruijn netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2710b92f1e67SWillem de Bruijn napi_tx ? napi_weight : 0); 2711986a4f4dSJason Wang 2712986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 27135377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 2714986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 2715d7dfc5cfSToshiaki Makita 2716d7dfc5cfSToshiaki Makita u64_stats_init(&vi->rq[i].stats.syncp); 2717d7dfc5cfSToshiaki Makita u64_stats_init(&vi->sq[i].stats.syncp); 2718986a4f4dSJason Wang } 2719986a4f4dSJason Wang 2720986a4f4dSJason Wang return 0; 2721986a4f4dSJason Wang 2722986a4f4dSJason Wang err_rq: 2723986a4f4dSJason Wang kfree(vi->sq); 2724986a4f4dSJason Wang err_sq: 272512e57169SMichael S. Tsirkin kfree(vi->ctrl); 272612e57169SMichael S. Tsirkin err_ctrl: 2727986a4f4dSJason Wang return -ENOMEM; 2728e9d7417bSJason Wang } 2729e9d7417bSJason Wang 27303f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 27313f9c10b0SAmit Shah { 2732986a4f4dSJason Wang int ret; 27333f9c10b0SAmit Shah 2734986a4f4dSJason Wang /* Allocate send & receive queues */ 2735986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 2736986a4f4dSJason Wang if (ret) 2737986a4f4dSJason Wang goto err; 27383f9c10b0SAmit Shah 2739986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 2740986a4f4dSJason Wang if (ret) 2741986a4f4dSJason Wang goto err_free; 27423f9c10b0SAmit Shah 274347be2479SWanlong Gao get_online_cpus(); 27448898c21cSWanlong Gao virtnet_set_affinity(vi); 274547be2479SWanlong Gao put_online_cpus(); 274647be2479SWanlong Gao 27473f9c10b0SAmit Shah return 0; 2748986a4f4dSJason Wang 2749986a4f4dSJason Wang err_free: 2750986a4f4dSJason Wang virtnet_free_queues(vi); 2751986a4f4dSJason Wang err: 2752986a4f4dSJason Wang return ret; 27533f9c10b0SAmit Shah } 27543f9c10b0SAmit Shah 2755fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2756fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 2757718ad681Sstephen hemminger char *buf) 2758fbf28d78SMichael Dalton { 2759fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 2760fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 27613cc81a9aSJason Wang unsigned int headroom = virtnet_get_headroom(vi); 27623cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 27635377d758SJohannes Berg struct ewma_pkt_len *avg; 2764fbf28d78SMichael Dalton 2765fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 2766fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 2767d85b758fSMichael S. Tsirkin return sprintf(buf, "%u\n", 27683cc81a9aSJason Wang get_mergeable_buf_len(&vi->rq[queue_index], avg, 27693cc81a9aSJason Wang SKB_DATA_ALIGN(headroom + tailroom))); 2770fbf28d78SMichael Dalton } 2771fbf28d78SMichael Dalton 2772fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 2773fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 2774fbf28d78SMichael Dalton 2775fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 2776fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 2777fbf28d78SMichael Dalton NULL 2778fbf28d78SMichael Dalton }; 2779fbf28d78SMichael Dalton 2780fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 2781fbf28d78SMichael Dalton .name = "virtio_net", 2782fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 2783fbf28d78SMichael Dalton }; 2784fbf28d78SMichael Dalton #endif 2785fbf28d78SMichael Dalton 2786892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 2787892d6eb1SJason Wang unsigned int fbit, 2788892d6eb1SJason Wang const char *fname, const char *dname) 2789892d6eb1SJason Wang { 2790892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 2791892d6eb1SJason Wang return false; 2792892d6eb1SJason Wang 2793892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 2794892d6eb1SJason Wang fname, dname); 2795892d6eb1SJason Wang 2796892d6eb1SJason Wang return true; 2797892d6eb1SJason Wang } 2798892d6eb1SJason Wang 2799892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 2800892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 2801892d6eb1SJason Wang 2802892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 2803892d6eb1SJason Wang { 2804892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 2805892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 2806892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2807892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 2808892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2809892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 2810892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 2811892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 2812892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 2813892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 2814892d6eb1SJason Wang return false; 2815892d6eb1SJason Wang } 2816892d6eb1SJason Wang 2817892d6eb1SJason Wang return true; 2818892d6eb1SJason Wang } 2819892d6eb1SJason Wang 2820d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU 2821d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU 2822d0c2c997SJarod Wilson 2823fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev) 2824296f96fcSRusty Russell { 28256ba42248SMichael S. Tsirkin if (!vdev->config->get) { 28266ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 28276ba42248SMichael S. Tsirkin __func__); 28286ba42248SMichael S. Tsirkin return -EINVAL; 28296ba42248SMichael S. Tsirkin } 28306ba42248SMichael S. Tsirkin 2831892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 2832892d6eb1SJason Wang return -EINVAL; 2833892d6eb1SJason Wang 2834fe36cbe0SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 2835fe36cbe0SMichael S. Tsirkin int mtu = virtio_cread16(vdev, 2836fe36cbe0SMichael S. Tsirkin offsetof(struct virtio_net_config, 2837fe36cbe0SMichael S. Tsirkin mtu)); 2838fe36cbe0SMichael S. Tsirkin if (mtu < MIN_MTU) 2839fe36cbe0SMichael S. Tsirkin __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 2840fe36cbe0SMichael S. Tsirkin } 2841fe36cbe0SMichael S. Tsirkin 2842fe36cbe0SMichael S. Tsirkin return 0; 2843fe36cbe0SMichael S. Tsirkin } 2844fe36cbe0SMichael S. Tsirkin 2845fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev) 2846fe36cbe0SMichael S. Tsirkin { 2847d7dfc5cfSToshiaki Makita int i, err = -ENOMEM; 2848fe36cbe0SMichael S. Tsirkin struct net_device *dev; 2849fe36cbe0SMichael S. Tsirkin struct virtnet_info *vi; 2850fe36cbe0SMichael S. Tsirkin u16 max_queue_pairs; 2851fe36cbe0SMichael S. Tsirkin int mtu; 2852fe36cbe0SMichael S. Tsirkin 2853986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 2854855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 2855855e0c52SRusty Russell struct virtio_net_config, 2856855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 2857986a4f4dSJason Wang 2858986a4f4dSJason Wang /* We need at least 2 queue's */ 2859986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 2860986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 2861986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2862986a4f4dSJason Wang max_queue_pairs = 1; 2863296f96fcSRusty Russell 2864296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 2865986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 2866296f96fcSRusty Russell if (!dev) 2867296f96fcSRusty Russell return -ENOMEM; 2868296f96fcSRusty Russell 2869296f96fcSRusty Russell /* Set up network device as normal. */ 2870f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 287176288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 2872296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 28733fa2a1dfSstephen hemminger 28747ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 2875296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 2876296f96fcSRusty Russell 2877296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 287898e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 2879296f96fcSRusty Russell /* This opens up the world of extra features. */ 288048900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 288198e778c9SMichał Mirosław if (csum) 288248900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 288398e778c9SMichał Mirosław 288498e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 2885e078de03SDavid S. Miller dev->hw_features |= NETIF_F_TSO 288634a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 288734a48579SRusty Russell } 28885539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 288998e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 289098e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 289198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 289298e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 289398e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 289498e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 289598e778c9SMichał Mirosław 289641f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 289741f2f127SJason Wang 289898e778c9SMichał Mirosław if (gso) 2899e078de03SDavid S. Miller dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 290098e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 2901296f96fcSRusty Russell } 29024f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 29034f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 2904296f96fcSRusty Russell 29054fda8302SJason Wang dev->vlan_features = dev->features; 29064fda8302SJason Wang 2907d0c2c997SJarod Wilson /* MTU range: 68 - 65535 */ 2908d0c2c997SJarod Wilson dev->min_mtu = MIN_MTU; 2909d0c2c997SJarod Wilson dev->max_mtu = MAX_MTU; 2910d0c2c997SJarod Wilson 2911296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 2912855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 2913855e0c52SRusty Russell virtio_cread_bytes(vdev, 2914a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 2915855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 2916855e0c52SRusty Russell else 2917f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 2918296f96fcSRusty Russell 2919296f96fcSRusty Russell /* Set up our device-specific information */ 2920296f96fcSRusty Russell vi = netdev_priv(dev); 2921296f96fcSRusty Russell vi->dev = dev; 2922296f96fcSRusty Russell vi->vdev = vdev; 2923d9d5dcc8SChristian Borntraeger vdev->priv = vi; 2924827da44cSJohn Stultz 2925586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 2926296f96fcSRusty Russell 292797402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 29288e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 29298e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 2930e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 2931e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 293297402b96SHerbert Xu vi->big_packets = true; 293397402b96SHerbert Xu 29343f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 29353f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 29363f2c31d9SMark McLoughlin 2937d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 2938d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 2939012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2940012873d0SMichael S. Tsirkin else 2941012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 2942012873d0SMichael S. Tsirkin 294375993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 294475993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 2945e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 2946e7428e95SMichael S. Tsirkin 2947986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2948986a4f4dSJason Wang vi->has_cvq = true; 2949986a4f4dSJason Wang 295014de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 295114de9d11SAaron Conole mtu = virtio_cread16(vdev, 295214de9d11SAaron Conole offsetof(struct virtio_net_config, 295314de9d11SAaron Conole mtu)); 295493a205eeSAaron Conole if (mtu < dev->min_mtu) { 2955fe36cbe0SMichael S. Tsirkin /* Should never trigger: MTU was previously validated 2956fe36cbe0SMichael S. Tsirkin * in virtnet_validate. 2957fe36cbe0SMichael S. Tsirkin */ 2958fe36cbe0SMichael S. Tsirkin dev_err(&vdev->dev, "device MTU appears to have changed " 2959fe36cbe0SMichael S. Tsirkin "it is now %d < %d", mtu, dev->min_mtu); 2960d7dfc5cfSToshiaki Makita goto free; 2961fe36cbe0SMichael S. Tsirkin } 2962fe36cbe0SMichael S. Tsirkin 2963d0c2c997SJarod Wilson dev->mtu = mtu; 296493a205eeSAaron Conole dev->max_mtu = mtu; 29652e123b44SMichael S. Tsirkin 29662e123b44SMichael S. Tsirkin /* TODO: size buffers correctly in this case. */ 29672e123b44SMichael S. Tsirkin if (dev->mtu > ETH_DATA_LEN) 29682e123b44SMichael S. Tsirkin vi->big_packets = true; 296914de9d11SAaron Conole } 297014de9d11SAaron Conole 2971012873d0SMichael S. Tsirkin if (vi->any_header_sg) 2972012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 29736ebbc1a6SZhangjie \(HZ\) 297444900010SJason Wang /* Enable multiqueue by default */ 297544900010SJason Wang if (num_online_cpus() >= max_queue_pairs) 297644900010SJason Wang vi->curr_queue_pairs = max_queue_pairs; 297744900010SJason Wang else 297844900010SJason Wang vi->curr_queue_pairs = num_online_cpus(); 2979986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 2980986a4f4dSJason Wang 2981986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 29823f9c10b0SAmit Shah err = init_vqs(vi); 2983d2a7dddaSMichael S. Tsirkin if (err) 2984d7dfc5cfSToshiaki Makita goto free; 2985d2a7dddaSMichael S. Tsirkin 2986fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 2987fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 2988fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 2989fbf28d78SMichael Dalton #endif 29900f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 29910f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 2992986a4f4dSJason Wang 299316032be5SNikolay Aleksandrov virtnet_init_settings(dev); 299416032be5SNikolay Aleksandrov 2995ba5e4426SSridhar Samudrala if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 2996ba5e4426SSridhar Samudrala vi->failover = net_failover_create(vi->dev); 29974b8e6ac4SWei Yongjun if (IS_ERR(vi->failover)) { 29984b8e6ac4SWei Yongjun err = PTR_ERR(vi->failover); 2999ba5e4426SSridhar Samudrala goto free_vqs; 3000ba5e4426SSridhar Samudrala } 30014b8e6ac4SWei Yongjun } 3002ba5e4426SSridhar Samudrala 3003296f96fcSRusty Russell err = register_netdev(dev); 3004296f96fcSRusty Russell if (err) { 3005296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 3006ba5e4426SSridhar Samudrala goto free_failover; 3007296f96fcSRusty Russell } 3008b3369c1fSRusty Russell 30094baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 30104baf1e33SMichael S. Tsirkin 30118017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 30128de4b2f3SWanlong Gao if (err) { 30138de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 3014f00e35e2Swangyunjian goto free_unregister_netdev; 30158de4b2f3SWanlong Gao } 30168de4b2f3SWanlong Gao 3017a220871bSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 301844900010SJason Wang 3019167c25e4SJason Wang /* Assume link up if device can't report link status, 3020167c25e4SJason Wang otherwise get link status from config. */ 3021167c25e4SJason Wang netif_carrier_off(dev); 3022bda7fab5SJay Vosburgh if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 30233b07e9caSTejun Heo schedule_work(&vi->config_work); 3024167c25e4SJason Wang } else { 3025167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 3026faa9b39fSJason Baron virtnet_update_settings(vi); 30274783256eSPantelis Koukousoulas netif_carrier_on(dev); 3028167c25e4SJason Wang } 30299f4d26d0SMark McLoughlin 30303f93522fSJason Wang for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 30313f93522fSJason Wang if (virtio_has_feature(vi->vdev, guest_offloads[i])) 30323f93522fSJason Wang set_bit(guest_offloads[i], &vi->guest_offloads); 30333f93522fSJason Wang 3034986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 3035986a4f4dSJason Wang dev->name, max_queue_pairs); 3036986a4f4dSJason Wang 3037296f96fcSRusty Russell return 0; 3038296f96fcSRusty Russell 3039f00e35e2Swangyunjian free_unregister_netdev: 304002465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 304102465555SMichael S. Tsirkin 3042b3369c1fSRusty Russell unregister_netdev(dev); 3043ba5e4426SSridhar Samudrala free_failover: 3044ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 3045d2a7dddaSMichael S. Tsirkin free_vqs: 3046986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 3047fb51879dSMichael Dalton free_receive_page_frags(vi); 3048e9d7417bSJason Wang virtnet_del_vqs(vi); 3049296f96fcSRusty Russell free: 3050296f96fcSRusty Russell free_netdev(dev); 3051296f96fcSRusty Russell return err; 3052296f96fcSRusty Russell } 3053296f96fcSRusty Russell 305404486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 3055296f96fcSRusty Russell { 305604486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 3057830a8a97SShirley Ma 3058830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 30599ab86bbcSShirley Ma free_unused_bufs(vi); 3060fb6813f4SRusty Russell 3061986a4f4dSJason Wang free_receive_bufs(vi); 3062d2a7dddaSMichael S. Tsirkin 3063fb51879dSMichael Dalton free_receive_page_frags(vi); 3064fb51879dSMichael Dalton 3065986a4f4dSJason Wang virtnet_del_vqs(vi); 306604486ed0SAmit Shah } 306704486ed0SAmit Shah 30688cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 306904486ed0SAmit Shah { 307004486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 307104486ed0SAmit Shah 30728017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 30738de4b2f3SWanlong Gao 3074102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 3075102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 3076586d17c5SJason Wang 307704486ed0SAmit Shah unregister_netdev(vi->dev); 307804486ed0SAmit Shah 3079ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 3080ba5e4426SSridhar Samudrala 308104486ed0SAmit Shah remove_vq_common(vi); 3082fb6813f4SRusty Russell 308374b2553fSRusty Russell free_netdev(vi->dev); 3084296f96fcSRusty Russell } 3085296f96fcSRusty Russell 308667a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 30870741bcb5SAmit Shah { 30880741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 30890741bcb5SAmit Shah 30908017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 30919fe7bfceSJohn Fastabend virtnet_freeze_down(vdev); 30920741bcb5SAmit Shah remove_vq_common(vi); 30930741bcb5SAmit Shah 30940741bcb5SAmit Shah return 0; 30950741bcb5SAmit Shah } 30960741bcb5SAmit Shah 309767a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 30980741bcb5SAmit Shah { 30990741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 31009fe7bfceSJohn Fastabend int err; 31010741bcb5SAmit Shah 31029fe7bfceSJohn Fastabend err = virtnet_restore_up(vdev); 31030741bcb5SAmit Shah if (err) 31040741bcb5SAmit Shah return err; 3105986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 3106986a4f4dSJason Wang 31078017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 3108ec9debbdSJason Wang if (err) 3109ec9debbdSJason Wang return err; 3110ec9debbdSJason Wang 31110741bcb5SAmit Shah return 0; 31120741bcb5SAmit Shah } 31130741bcb5SAmit Shah 3114296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 3115296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 3116296f96fcSRusty Russell { 0 }, 3117296f96fcSRusty Russell }; 3118296f96fcSRusty Russell 3119f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \ 3120f3358507SMichael S. Tsirkin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 3121f3358507SMichael S. Tsirkin VIRTIO_NET_F_MAC, \ 3122f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 3123f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 3124f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 3125f3358507SMichael S. Tsirkin VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 3126f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 3127f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 3128f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_MAC_ADDR, \ 3129faa9b39fSJason Baron VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 31309805069dSSridhar Samudrala VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY 3131f3358507SMichael S. Tsirkin 3132c45a6816SRusty Russell static unsigned int features[] = { 3133f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 3134f3358507SMichael S. Tsirkin }; 3135f3358507SMichael S. Tsirkin 3136f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = { 3137f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 3138f3358507SMichael S. Tsirkin VIRTIO_NET_F_GSO, 3139e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 3140c45a6816SRusty Russell }; 3141c45a6816SRusty Russell 314222402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 3143c45a6816SRusty Russell .feature_table = features, 3144c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 3145f3358507SMichael S. Tsirkin .feature_table_legacy = features_legacy, 3146f3358507SMichael S. Tsirkin .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 3147296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 3148296f96fcSRusty Russell .driver.owner = THIS_MODULE, 3149296f96fcSRusty Russell .id_table = id_table, 3150fe36cbe0SMichael S. Tsirkin .validate = virtnet_validate, 3151296f96fcSRusty Russell .probe = virtnet_probe, 31528cc085d6SBill Pemberton .remove = virtnet_remove, 31539f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 315489107000SAaron Lu #ifdef CONFIG_PM_SLEEP 31550741bcb5SAmit Shah .freeze = virtnet_freeze, 31560741bcb5SAmit Shah .restore = virtnet_restore, 31570741bcb5SAmit Shah #endif 3158296f96fcSRusty Russell }; 3159296f96fcSRusty Russell 31608017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void) 31618017c279SSebastian Andrzej Siewior { 31628017c279SSebastian Andrzej Siewior int ret; 31638017c279SSebastian Andrzej Siewior 316473c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 31658017c279SSebastian Andrzej Siewior virtnet_cpu_online, 31668017c279SSebastian Andrzej Siewior virtnet_cpu_down_prep); 31678017c279SSebastian Andrzej Siewior if (ret < 0) 31688017c279SSebastian Andrzej Siewior goto out; 31698017c279SSebastian Andrzej Siewior virtionet_online = ret; 317073c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 31718017c279SSebastian Andrzej Siewior NULL, virtnet_cpu_dead); 31728017c279SSebastian Andrzej Siewior if (ret) 31738017c279SSebastian Andrzej Siewior goto err_dead; 31748017c279SSebastian Andrzej Siewior 31758017c279SSebastian Andrzej Siewior ret = register_virtio_driver(&virtio_net_driver); 31768017c279SSebastian Andrzej Siewior if (ret) 31778017c279SSebastian Andrzej Siewior goto err_virtio; 31788017c279SSebastian Andrzej Siewior return 0; 31798017c279SSebastian Andrzej Siewior err_virtio: 31808017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 31818017c279SSebastian Andrzej Siewior err_dead: 31828017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 31838017c279SSebastian Andrzej Siewior out: 31848017c279SSebastian Andrzej Siewior return ret; 31858017c279SSebastian Andrzej Siewior } 31868017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init); 31878017c279SSebastian Andrzej Siewior 31888017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void) 31898017c279SSebastian Andrzej Siewior { 3190cfa0ebc9SAndrew Jones unregister_virtio_driver(&virtio_net_driver); 31918017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 31928017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 31938017c279SSebastian Andrzej Siewior } 31948017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit); 3195296f96fcSRusty Russell 3196296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 3197296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 3198296f96fcSRusty Russell MODULE_LICENSE("GPL"); 3199