11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 248925e37SRusty Russell /* A network driver using virtio. 3296f96fcSRusty Russell * 4296f96fcSRusty Russell * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation 5296f96fcSRusty Russell */ 6296f96fcSRusty Russell //#define DEBUG 7296f96fcSRusty Russell #include <linux/netdevice.h> 8296f96fcSRusty Russell #include <linux/etherdevice.h> 9a9ea3fc6SHerbert Xu #include <linux/ethtool.h> 10296f96fcSRusty Russell #include <linux/module.h> 11296f96fcSRusty Russell #include <linux/virtio.h> 12296f96fcSRusty Russell #include <linux/virtio_net.h> 13f600b690SJohn Fastabend #include <linux/bpf.h> 14a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h> 15296f96fcSRusty Russell #include <linux/scatterlist.h> 16e918085aSAlex Williamson #include <linux/if_vlan.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 188de4b2f3SWanlong Gao #include <linux/cpu.h> 19ab7db917SMichael Dalton #include <linux/average.h> 20186b3c99SJason Wang #include <linux/filter.h> 212ca653d6SCaleb Raitto #include <linux/kernel.h> 22d85b758fSMichael S. Tsirkin #include <net/route.h> 23754b8a21SJesper Dangaard Brouer #include <net/xdp.h> 24ba5e4426SSridhar Samudrala #include <net/net_failover.h> 25296f96fcSRusty Russell 26d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT; 276c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 286c0cd7c0SDor Laor 2931c03aefSWillem de Bruijn static bool csum = true, gso = true, napi_tx = true; 3034a48579SRusty Russell module_param(csum, bool, 0444); 3134a48579SRusty Russell module_param(gso, bool, 0444); 32b92f1e67SWillem de Bruijn module_param(napi_tx, bool, 0644); 3334a48579SRusty Russell 34296f96fcSRusty Russell /* FIXME: MTU in config. */ 355061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 363f2c31d9SMark McLoughlin #define GOOD_COPY_LEN 128 37296f96fcSRusty Russell 38f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 39f6b10209SJason Wang 402de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ 412de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256 422de2f7f4SJohn Fastabend 432471c75eSJesper Dangaard Brouer /* Separating two types of XDP xmit */ 442471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_TX BIT(0) 452471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_REDIR BIT(1) 462471c75eSJesper Dangaard Brouer 475050471dSToshiaki Makita #define VIRTIO_XDP_FLAG BIT(0) 485050471dSToshiaki Makita 495377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet 505377d758SJohannes Berg * buffer size when refilling RX rings. As the entire RX ring may be refilled 515377d758SJohannes Berg * at once, the weight is chosen so that the EWMA will be insensitive to short- 525377d758SJohannes Berg * term, transient changes in packet size. 53ab7db917SMichael Dalton */ 54eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64) 55ab7db917SMichael Dalton 5666846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0" 572a41f71dSAlex Williamson 587acd4329SColin Ian King static const unsigned long guest_offloads[] = { 597acd4329SColin Ian King VIRTIO_NET_F_GUEST_TSO4, 603f93522fSJason Wang VIRTIO_NET_F_GUEST_TSO6, 613f93522fSJason Wang VIRTIO_NET_F_GUEST_ECN, 62e59ff2c4SJason Wang VIRTIO_NET_F_GUEST_UFO, 63418044e1SAndrew Melnychenko VIRTIO_NET_F_GUEST_CSUM, 64418044e1SAndrew Melnychenko VIRTIO_NET_F_GUEST_USO4, 65418044e1SAndrew Melnychenko VIRTIO_NET_F_GUEST_USO6 667acd4329SColin Ian King }; 673f93522fSJason Wang 68dbcf24d1SJason Wang #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \ 691a03b8a3STonghao Zhang (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \ 701a03b8a3STonghao Zhang (1ULL << VIRTIO_NET_F_GUEST_ECN) | \ 71418044e1SAndrew Melnychenko (1ULL << VIRTIO_NET_F_GUEST_UFO) | \ 72418044e1SAndrew Melnychenko (1ULL << VIRTIO_NET_F_GUEST_USO4) | \ 73418044e1SAndrew Melnychenko (1ULL << VIRTIO_NET_F_GUEST_USO6)) 741a03b8a3STonghao Zhang 75d7dfc5cfSToshiaki Makita struct virtnet_stat_desc { 76d7dfc5cfSToshiaki Makita char desc[ETH_GSTRING_LEN]; 77d7dfc5cfSToshiaki Makita size_t offset; 783fa2a1dfSstephen hemminger }; 793fa2a1dfSstephen hemminger 80d7dfc5cfSToshiaki Makita struct virtnet_sq_stats { 81d7dfc5cfSToshiaki Makita struct u64_stats_sync syncp; 82d7dfc5cfSToshiaki Makita u64 packets; 83d7dfc5cfSToshiaki Makita u64 bytes; 845b8f3c8dSToshiaki Makita u64 xdp_tx; 855b8f3c8dSToshiaki Makita u64 xdp_tx_drops; 86461f03dcSToshiaki Makita u64 kicks; 87a520794bSTony Lu u64 tx_timeouts; 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) }, 111a520794bSTony Lu { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) }, 112d7dfc5cfSToshiaki Makita }; 113d7dfc5cfSToshiaki Makita 114d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { 115d7dfc5cfSToshiaki Makita { "packets", VIRTNET_RQ_STAT(packets) }, 116d7dfc5cfSToshiaki Makita { "bytes", VIRTNET_RQ_STAT(bytes) }, 1172c4a2f7dSToshiaki Makita { "drops", VIRTNET_RQ_STAT(drops) }, 1185b8f3c8dSToshiaki Makita { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) }, 1195b8f3c8dSToshiaki Makita { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) }, 1205b8f3c8dSToshiaki Makita { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) }, 1215b8f3c8dSToshiaki Makita { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) }, 122461f03dcSToshiaki Makita { "kicks", VIRTNET_RQ_STAT(kicks) }, 123d7dfc5cfSToshiaki Makita }; 124d7dfc5cfSToshiaki Makita 125d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) 126d7dfc5cfSToshiaki Makita #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc) 127d7dfc5cfSToshiaki Makita 128e9d7417bSJason Wang /* Internal representation of a send virtqueue */ 129e9d7417bSJason Wang struct send_queue { 130e9d7417bSJason Wang /* Virtqueue associated with this send _queue */ 131e9d7417bSJason Wang struct virtqueue *vq; 132e9d7417bSJason Wang 133e9d7417bSJason Wang /* TX: fragments + linear part + virtio header */ 134e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 135986a4f4dSJason Wang 136986a4f4dSJason Wang /* Name of the send queue: output.$index */ 137d0671115SParav Pandit char name[16]; 138b92f1e67SWillem de Bruijn 139d7dfc5cfSToshiaki Makita struct virtnet_sq_stats stats; 140d7dfc5cfSToshiaki Makita 141b92f1e67SWillem de Bruijn struct napi_struct napi; 142ebcce492SXuan Zhuo 143ebcce492SXuan Zhuo /* Record whether sq is in reset state. */ 144ebcce492SXuan Zhuo bool reset; 145e9d7417bSJason Wang }; 146e9d7417bSJason Wang 147e9d7417bSJason Wang /* Internal representation of a receive virtqueue */ 148e9d7417bSJason Wang struct receive_queue { 149e9d7417bSJason Wang /* Virtqueue associated with this receive_queue */ 150e9d7417bSJason Wang struct virtqueue *vq; 151e9d7417bSJason Wang 152296f96fcSRusty Russell struct napi_struct napi; 153296f96fcSRusty Russell 154f600b690SJohn Fastabend struct bpf_prog __rcu *xdp_prog; 155f600b690SJohn Fastabend 156d7dfc5cfSToshiaki Makita struct virtnet_rq_stats stats; 157d7dfc5cfSToshiaki Makita 158e9d7417bSJason Wang /* Chain pages by the private ptr. */ 159e9d7417bSJason Wang struct page *pages; 160e9d7417bSJason Wang 161ab7db917SMichael Dalton /* Average packet length for mergeable receive buffers. */ 1625377d758SJohannes Berg struct ewma_pkt_len mrg_avg_pkt_len; 163ab7db917SMichael Dalton 164fb51879dSMichael Dalton /* Page frag for packet buffer allocation. */ 165fb51879dSMichael Dalton struct page_frag alloc_frag; 166fb51879dSMichael Dalton 167e9d7417bSJason Wang /* RX: fragments + linear part + virtio header */ 168e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 169986a4f4dSJason Wang 170d85b758fSMichael S. Tsirkin /* Min single buffer size for mergeable buffers case. */ 171d85b758fSMichael S. Tsirkin unsigned int min_buf_len; 172d85b758fSMichael S. Tsirkin 173986a4f4dSJason Wang /* Name of this receive queue: input.$index */ 174d0671115SParav Pandit char name[16]; 175754b8a21SJesper Dangaard Brouer 176754b8a21SJesper Dangaard Brouer struct xdp_rxq_info xdp_rxq; 177e9d7417bSJason Wang }; 178e9d7417bSJason Wang 179c7114b12SAndrew Melnychenko /* This structure can contain rss message with maximum settings for indirection table and keysize 180c7114b12SAndrew Melnychenko * Note, that default structure that describes RSS configuration virtio_net_rss_config 181c7114b12SAndrew Melnychenko * contains same info but can't handle table values. 182c7114b12SAndrew Melnychenko * In any case, structure would be passed to virtio hw through sg_buf split by parts 183c7114b12SAndrew Melnychenko * because table sizes may be differ according to the device configuration. 184c7114b12SAndrew Melnychenko */ 185c7114b12SAndrew Melnychenko #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40 186c7114b12SAndrew Melnychenko #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128 187c7114b12SAndrew Melnychenko struct virtio_net_ctrl_rss { 188c7114b12SAndrew Melnychenko u32 hash_types; 189c7114b12SAndrew Melnychenko u16 indirection_table_mask; 190c7114b12SAndrew Melnychenko u16 unclassified_queue; 191c7114b12SAndrew Melnychenko u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN]; 192c7114b12SAndrew Melnychenko u16 max_tx_vq; 193c7114b12SAndrew Melnychenko u8 hash_key_length; 194c7114b12SAndrew Melnychenko u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE]; 195c7114b12SAndrew Melnychenko }; 196c7114b12SAndrew Melnychenko 19712e57169SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */ 19812e57169SMichael S. Tsirkin struct control_buf { 19912e57169SMichael S. Tsirkin struct virtio_net_ctrl_hdr hdr; 20012e57169SMichael S. Tsirkin virtio_net_ctrl_ack status; 20112e57169SMichael S. Tsirkin struct virtio_net_ctrl_mq mq; 20212e57169SMichael S. Tsirkin u8 promisc; 20312e57169SMichael S. Tsirkin u8 allmulti; 204d7fad4c8SMichael S. Tsirkin __virtio16 vid; 205f4ee703aSMichael S. Tsirkin __virtio64 offloads; 206c7114b12SAndrew Melnychenko struct virtio_net_ctrl_rss rss; 20712e57169SMichael S. Tsirkin }; 20812e57169SMichael S. Tsirkin 209e9d7417bSJason Wang struct virtnet_info { 210e9d7417bSJason Wang struct virtio_device *vdev; 211e9d7417bSJason Wang struct virtqueue *cvq; 212e9d7417bSJason Wang struct net_device *dev; 213986a4f4dSJason Wang struct send_queue *sq; 214986a4f4dSJason Wang struct receive_queue *rq; 215e9d7417bSJason Wang unsigned int status; 216e9d7417bSJason Wang 217986a4f4dSJason Wang /* Max # of queue pairs supported by the device */ 218986a4f4dSJason Wang u16 max_queue_pairs; 219986a4f4dSJason Wang 220986a4f4dSJason Wang /* # of queue pairs currently used by the driver */ 221986a4f4dSJason Wang u16 curr_queue_pairs; 222986a4f4dSJason Wang 223672aafd5SJohn Fastabend /* # of XDP queue pairs currently used by the driver */ 224672aafd5SJohn Fastabend u16 xdp_queue_pairs; 225672aafd5SJohn Fastabend 22697c2c69eSXuan Zhuo /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */ 22797c2c69eSXuan Zhuo bool xdp_enabled; 22897c2c69eSXuan Zhuo 22997402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 23097402b96SHerbert Xu bool big_packets; 23197402b96SHerbert Xu 2324959aebbSGavin Li /* number of sg entries allocated for big packets */ 2334959aebbSGavin Li unsigned int big_packets_num_skbfrags; 2344959aebbSGavin Li 2353f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 2363f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 2373f2c31d9SMark McLoughlin 238c7114b12SAndrew Melnychenko /* Host supports rss and/or hash report */ 239c7114b12SAndrew Melnychenko bool has_rss; 24091f41f01SAndrew Melnychenko bool has_rss_hash_report; 241c7114b12SAndrew Melnychenko u8 rss_key_size; 242c7114b12SAndrew Melnychenko u16 rss_indir_table_size; 243c7114b12SAndrew Melnychenko u32 rss_hash_types_supported; 244c1170820SAndrew Melnychenko u32 rss_hash_types_saved; 245c7114b12SAndrew Melnychenko 246986a4f4dSJason Wang /* Has control virtqueue */ 247986a4f4dSJason Wang bool has_cvq; 248986a4f4dSJason Wang 249e7428e95SMichael S. Tsirkin /* Host can handle any s/g split between our header and packet data */ 250e7428e95SMichael S. Tsirkin bool any_header_sg; 251e7428e95SMichael S. Tsirkin 252012873d0SMichael S. Tsirkin /* Packet virtio header size */ 253012873d0SMichael S. Tsirkin u8 hdr_len; 254012873d0SMichael S. Tsirkin 2555a159128SJason Wang /* Work struct for delayed refilling if we run low on memory. */ 2563161e453SRusty Russell struct delayed_work refill; 2573161e453SRusty Russell 2585a159128SJason Wang /* Is delayed refill enabled? */ 2595a159128SJason Wang bool refill_enabled; 2605a159128SJason Wang 2615a159128SJason Wang /* The lock to synchronize the access to refill_enabled */ 2625a159128SJason Wang spinlock_t refill_lock; 2635a159128SJason Wang 264586d17c5SJason Wang /* Work struct for config space updates */ 265586d17c5SJason Wang struct work_struct config_work; 266586d17c5SJason Wang 267986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 268986a4f4dSJason Wang bool affinity_hint_set; 26947be2479SWanlong Gao 2708017c279SSebastian Andrzej Siewior /* CPU hotplug instances for online & dead */ 2718017c279SSebastian Andrzej Siewior struct hlist_node node; 2728017c279SSebastian Andrzej Siewior struct hlist_node node_dead; 2732ac46030SMichael S. Tsirkin 27412e57169SMichael S. Tsirkin struct control_buf *ctrl; 27516032be5SNikolay Aleksandrov 27616032be5SNikolay Aleksandrov /* Ethtool settings */ 27716032be5SNikolay Aleksandrov u8 duplex; 27816032be5SNikolay Aleksandrov u32 speed; 2793f93522fSJason Wang 280699b045aSAlvaro Karsz /* Interrupt coalescing settings */ 281699b045aSAlvaro Karsz u32 tx_usecs; 282699b045aSAlvaro Karsz u32 rx_usecs; 283699b045aSAlvaro Karsz u32 tx_max_packets; 284699b045aSAlvaro Karsz u32 rx_max_packets; 285699b045aSAlvaro Karsz 2863f93522fSJason Wang unsigned long guest_offloads; 287a02e8964SWillem de Bruijn unsigned long guest_offloads_capable; 288ba5e4426SSridhar Samudrala 289ba5e4426SSridhar Samudrala /* failover when STANDBY feature enabled */ 290ba5e4426SSridhar Samudrala struct failover *failover; 291296f96fcSRusty Russell }; 292296f96fcSRusty Russell 2939ab86bbcSShirley Ma struct padded_vnet_hdr { 294c1ddc42dSAndrew Melnychenko struct virtio_net_hdr_v1_hash hdr; 2959ab86bbcSShirley Ma /* 296012873d0SMichael S. Tsirkin * hdr is in a separate sg buffer, and data sg buffer shares same page 297012873d0SMichael S. Tsirkin * with this header sg. This padding makes next sg 16 byte aligned 298012873d0SMichael S. Tsirkin * after the header. 2999ab86bbcSShirley Ma */ 300c1ddc42dSAndrew Melnychenko char padding[12]; 3019ab86bbcSShirley Ma }; 3029ab86bbcSShirley Ma 3036a4763e2SXuan Zhuo static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf); 304ebcce492SXuan Zhuo static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf); 3056a4763e2SXuan Zhuo 3065050471dSToshiaki Makita static bool is_xdp_frame(void *ptr) 3075050471dSToshiaki Makita { 3085050471dSToshiaki Makita return (unsigned long)ptr & VIRTIO_XDP_FLAG; 3095050471dSToshiaki Makita } 3105050471dSToshiaki Makita 3115050471dSToshiaki Makita static void *xdp_to_ptr(struct xdp_frame *ptr) 3125050471dSToshiaki Makita { 3135050471dSToshiaki Makita return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG); 3145050471dSToshiaki Makita } 3155050471dSToshiaki Makita 3165050471dSToshiaki Makita static struct xdp_frame *ptr_to_xdp(void *ptr) 3175050471dSToshiaki Makita { 3185050471dSToshiaki Makita return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); 3195050471dSToshiaki Makita } 3205050471dSToshiaki Makita 321986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 322986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 323986a4f4dSJason Wang */ 324986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 325986a4f4dSJason Wang { 3269d0ca6edSRusty Russell return (vq->index - 1) / 2; 327986a4f4dSJason Wang } 328986a4f4dSJason Wang 329986a4f4dSJason Wang static int txq2vq(int txq) 330986a4f4dSJason Wang { 331986a4f4dSJason Wang return txq * 2 + 1; 332986a4f4dSJason Wang } 333986a4f4dSJason Wang 334986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 335986a4f4dSJason Wang { 3369d0ca6edSRusty Russell return vq->index / 2; 337986a4f4dSJason Wang } 338986a4f4dSJason Wang 339986a4f4dSJason Wang static int rxq2vq(int rxq) 340986a4f4dSJason Wang { 341986a4f4dSJason Wang return rxq * 2; 342986a4f4dSJason Wang } 343986a4f4dSJason Wang 344012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 345296f96fcSRusty Russell { 346012873d0SMichael S. Tsirkin return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 347296f96fcSRusty Russell } 348296f96fcSRusty Russell 3499ab86bbcSShirley Ma /* 3509ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 3519ab86bbcSShirley Ma * most recent used list in the beginning for reuse 3529ab86bbcSShirley Ma */ 353e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 354fb6813f4SRusty Russell { 3559ab86bbcSShirley Ma struct page *end; 3569ab86bbcSShirley Ma 357e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 3589ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 359e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 360e9d7417bSJason Wang rq->pages = page; 361fb6813f4SRusty Russell } 362fb6813f4SRusty Russell 363e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 364fb6813f4SRusty Russell { 365e9d7417bSJason Wang struct page *p = rq->pages; 366fb6813f4SRusty Russell 3679ab86bbcSShirley Ma if (p) { 368e9d7417bSJason Wang rq->pages = (struct page *)p->private; 3699ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 3709ab86bbcSShirley Ma p->private = 0; 3719ab86bbcSShirley Ma } else 372fb6813f4SRusty Russell p = alloc_page(gfp_mask); 373fb6813f4SRusty Russell return p; 374fb6813f4SRusty Russell } 375fb6813f4SRusty Russell 3765a159128SJason Wang static void enable_delayed_refill(struct virtnet_info *vi) 3775a159128SJason Wang { 3785a159128SJason Wang spin_lock_bh(&vi->refill_lock); 3795a159128SJason Wang vi->refill_enabled = true; 3805a159128SJason Wang spin_unlock_bh(&vi->refill_lock); 3815a159128SJason Wang } 3825a159128SJason Wang 3835a159128SJason Wang static void disable_delayed_refill(struct virtnet_info *vi) 3845a159128SJason Wang { 3855a159128SJason Wang spin_lock_bh(&vi->refill_lock); 3865a159128SJason Wang vi->refill_enabled = false; 3875a159128SJason Wang spin_unlock_bh(&vi->refill_lock); 3885a159128SJason Wang } 3895a159128SJason Wang 390e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi, 391e4e8452aSWillem de Bruijn struct virtqueue *vq) 392e4e8452aSWillem de Bruijn { 393e4e8452aSWillem de Bruijn if (napi_schedule_prep(napi)) { 394e4e8452aSWillem de Bruijn virtqueue_disable_cb(vq); 395e4e8452aSWillem de Bruijn __napi_schedule(napi); 396e4e8452aSWillem de Bruijn } 397e4e8452aSWillem de Bruijn } 398e4e8452aSWillem de Bruijn 399e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi, 400e4e8452aSWillem de Bruijn struct virtqueue *vq, int processed) 401e4e8452aSWillem de Bruijn { 402e4e8452aSWillem de Bruijn int opaque; 403e4e8452aSWillem de Bruijn 404e4e8452aSWillem de Bruijn opaque = virtqueue_enable_cb_prepare(vq); 405fdaa767aSToshiaki Makita if (napi_complete_done(napi, processed)) { 406fdaa767aSToshiaki Makita if (unlikely(virtqueue_poll(vq, opaque))) 407e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 408fdaa767aSToshiaki Makita } else { 409fdaa767aSToshiaki Makita virtqueue_disable_cb(vq); 410fdaa767aSToshiaki Makita } 411e4e8452aSWillem de Bruijn } 412e4e8452aSWillem de Bruijn 413e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 414296f96fcSRusty Russell { 415e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 416b92f1e67SWillem de Bruijn struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 417296f96fcSRusty Russell 4182cb9c6baSRusty Russell /* Suppress further interrupts. */ 419e9d7417bSJason Wang virtqueue_disable_cb(vq); 42011a3a154SRusty Russell 421b92f1e67SWillem de Bruijn if (napi->weight) 422b92f1e67SWillem de Bruijn virtqueue_napi_schedule(napi, vq); 423b92f1e67SWillem de Bruijn else 424363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 425986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 426296f96fcSRusty Russell } 427296f96fcSRusty Russell 42828b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22 42928b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize, 43028b39bc7SJason Wang unsigned int headroom) 43128b39bc7SJason Wang { 43228b39bc7SJason Wang return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 43328b39bc7SJason Wang } 43428b39bc7SJason Wang 43528b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 43628b39bc7SJason Wang { 43728b39bc7SJason Wang return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 43828b39bc7SJason Wang } 43928b39bc7SJason Wang 44028b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 44128b39bc7SJason Wang { 44228b39bc7SJason Wang return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 44328b39bc7SJason Wang } 44428b39bc7SJason Wang 4453464645aSMike Waychison /* Called from bottom half context */ 446946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 447946fa564SMichael S. Tsirkin struct receive_queue *rq, 4482613af0eSMichael Dalton struct page *page, unsigned int offset, 44918117a84SHeng Qi unsigned int len, unsigned int truesize) 4509ab86bbcSShirley Ma { 4519ab86bbcSShirley Ma struct sk_buff *skb; 452012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 4532613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 454af39c8f7SEric Dumazet struct page *page_to_free = NULL; 455fb32856bSXuan Zhuo int tailroom, shinfo_size; 456f80bd740SXuan Zhuo char *p, *hdr_p, *buf; 4579ab86bbcSShirley Ma 4582613af0eSMichael Dalton p = page_address(page) + offset; 459fb32856bSXuan Zhuo hdr_p = p; 4609ab86bbcSShirley Ma 461012873d0SMichael S. Tsirkin hdr_len = vi->hdr_len; 462012873d0SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 463c1ddc42dSAndrew Melnychenko hdr_padded_len = hdr_len; 464012873d0SMichael S. Tsirkin else 4652613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 4663f2c31d9SMark McLoughlin 46718117a84SHeng Qi buf = p; 4689ab86bbcSShirley Ma len -= hdr_len; 4692613af0eSMichael Dalton offset += hdr_padded_len; 4702613af0eSMichael Dalton p += hdr_padded_len; 47118117a84SHeng Qi tailroom = truesize - hdr_padded_len - len; 4723f2c31d9SMark McLoughlin 473fb32856bSXuan Zhuo shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 474fb32856bSXuan Zhuo 475f80bd740SXuan Zhuo /* copy small packet so we can reuse these pages */ 476f5d7872aSEric Dumazet if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) { 477f80bd740SXuan Zhuo skb = build_skb(buf, truesize); 478fb32856bSXuan Zhuo if (unlikely(!skb)) 479fb32856bSXuan Zhuo return NULL; 480fb32856bSXuan Zhuo 481f80bd740SXuan Zhuo skb_reserve(skb, p - buf); 482fb32856bSXuan Zhuo skb_put(skb, len); 483afd92d82SJason Wang 484afd92d82SJason Wang page = (struct page *)page->private; 485afd92d82SJason Wang if (page) 486afd92d82SJason Wang give_pages(rq, page); 487fb32856bSXuan Zhuo goto ok; 488fb32856bSXuan Zhuo } 489fb32856bSXuan Zhuo 490fb32856bSXuan Zhuo /* copy small packet so we can reuse these pages for small data */ 491fb32856bSXuan Zhuo skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 492fb32856bSXuan Zhuo if (unlikely(!skb)) 493fb32856bSXuan Zhuo return NULL; 494fb32856bSXuan Zhuo 4950f6925b3SEric Dumazet /* Copy all frame if it fits skb->head, otherwise 4960f6925b3SEric Dumazet * we let virtio_net_hdr_to_skb() and GRO pull headers as needed. 4970f6925b3SEric Dumazet */ 4980f6925b3SEric Dumazet if (len <= skb_tailroom(skb)) 4993f2c31d9SMark McLoughlin copy = len; 5000f6925b3SEric Dumazet else 50118117a84SHeng Qi copy = ETH_HLEN; 50259ae1d12SJohannes Berg skb_put_data(skb, p, copy); 5033f2c31d9SMark McLoughlin 5043f2c31d9SMark McLoughlin len -= copy; 5059ab86bbcSShirley Ma offset += copy; 5063f2c31d9SMark McLoughlin 5072613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 5082613af0eSMichael Dalton if (len) 5092613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 5102613af0eSMichael Dalton else 511af39c8f7SEric Dumazet page_to_free = page; 512fb32856bSXuan Zhuo goto ok; 5132613af0eSMichael Dalton } 5142613af0eSMichael Dalton 515e878d78bSSasha Levin /* 516e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 517e878d78bSSasha Levin * This is here to handle cases when the device erroneously 518e878d78bSSasha Levin * tries to receive more than is possible. This is usually 519e878d78bSSasha Levin * the case of a broken device. 520e878d78bSSasha Levin */ 521e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 522be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 523e878d78bSSasha Levin dev_kfree_skb(skb); 524e878d78bSSasha Levin return NULL; 525e878d78bSSasha Levin } 5262613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 5279ab86bbcSShirley Ma while (len) { 5282613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 5292613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 5302613af0eSMichael Dalton frag_size, truesize); 5312613af0eSMichael Dalton len -= frag_size; 5329ab86bbcSShirley Ma page = (struct page *)page->private; 5339ab86bbcSShirley Ma offset = 0; 5343f2c31d9SMark McLoughlin } 5353f2c31d9SMark McLoughlin 5369ab86bbcSShirley Ma if (page) 537e9d7417bSJason Wang give_pages(rq, page); 5383f2c31d9SMark McLoughlin 539fb32856bSXuan Zhuo ok: 540fb32856bSXuan Zhuo hdr = skb_vnet_hdr(skb); 541fb32856bSXuan Zhuo memcpy(hdr, hdr_p, hdr_len); 542af39c8f7SEric Dumazet if (page_to_free) 543af39c8f7SEric Dumazet put_page(page_to_free); 544fb32856bSXuan Zhuo 5459ab86bbcSShirley Ma return skb; 5469ab86bbcSShirley Ma } 5479ab86bbcSShirley Ma 54825074a44SXuan Zhuo static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi) 54925074a44SXuan Zhuo { 55025074a44SXuan Zhuo unsigned int len; 55125074a44SXuan Zhuo unsigned int packets = 0; 55225074a44SXuan Zhuo unsigned int bytes = 0; 55325074a44SXuan Zhuo void *ptr; 55425074a44SXuan Zhuo 55525074a44SXuan Zhuo while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 55625074a44SXuan Zhuo if (likely(!is_xdp_frame(ptr))) { 55725074a44SXuan Zhuo struct sk_buff *skb = ptr; 55825074a44SXuan Zhuo 55925074a44SXuan Zhuo pr_debug("Sent skb %p\n", skb); 56025074a44SXuan Zhuo 56125074a44SXuan Zhuo bytes += skb->len; 56225074a44SXuan Zhuo napi_consume_skb(skb, in_napi); 56325074a44SXuan Zhuo } else { 56425074a44SXuan Zhuo struct xdp_frame *frame = ptr_to_xdp(ptr); 56525074a44SXuan Zhuo 56625074a44SXuan Zhuo bytes += xdp_get_frame_len(frame); 56725074a44SXuan Zhuo xdp_return_frame(frame); 56825074a44SXuan Zhuo } 56925074a44SXuan Zhuo packets++; 57025074a44SXuan Zhuo } 57125074a44SXuan Zhuo 57225074a44SXuan Zhuo /* Avoid overhead when no packets have been processed 57325074a44SXuan Zhuo * happens when called speculatively from start_xmit. 57425074a44SXuan Zhuo */ 57525074a44SXuan Zhuo if (!packets) 57625074a44SXuan Zhuo return; 57725074a44SXuan Zhuo 57825074a44SXuan Zhuo u64_stats_update_begin(&sq->stats.syncp); 57925074a44SXuan Zhuo sq->stats.bytes += bytes; 58025074a44SXuan Zhuo sq->stats.packets += packets; 58125074a44SXuan Zhuo u64_stats_update_end(&sq->stats.syncp); 58225074a44SXuan Zhuo } 58325074a44SXuan Zhuo 58425074a44SXuan Zhuo static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 58525074a44SXuan Zhuo { 58625074a44SXuan Zhuo if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 58725074a44SXuan Zhuo return false; 58825074a44SXuan Zhuo else if (q < vi->curr_queue_pairs) 58925074a44SXuan Zhuo return true; 59025074a44SXuan Zhuo else 59125074a44SXuan Zhuo return false; 59225074a44SXuan Zhuo } 59325074a44SXuan Zhuo 594b8ef4809SXuan Zhuo static void check_sq_full_and_disable(struct virtnet_info *vi, 595b8ef4809SXuan Zhuo struct net_device *dev, 596b8ef4809SXuan Zhuo struct send_queue *sq) 597b8ef4809SXuan Zhuo { 598b8ef4809SXuan Zhuo bool use_napi = sq->napi.weight; 599b8ef4809SXuan Zhuo int qnum; 600b8ef4809SXuan Zhuo 601b8ef4809SXuan Zhuo qnum = sq - vi->sq; 602b8ef4809SXuan Zhuo 603b8ef4809SXuan Zhuo /* If running out of space, stop queue to avoid getting packets that we 604b8ef4809SXuan Zhuo * are then unable to transmit. 605b8ef4809SXuan Zhuo * An alternative would be to force queuing layer to requeue the skb by 606b8ef4809SXuan Zhuo * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 607b8ef4809SXuan Zhuo * returned in a normal path of operation: it means that driver is not 608b8ef4809SXuan Zhuo * maintaining the TX queue stop/start state properly, and causes 609b8ef4809SXuan Zhuo * the stack to do a non-trivial amount of useless work. 610b8ef4809SXuan Zhuo * Since most packets only take 1 or 2 ring slots, stopping the queue 611b8ef4809SXuan Zhuo * early means 16 slots are typically wasted. 612b8ef4809SXuan Zhuo */ 613b8ef4809SXuan Zhuo if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 614b8ef4809SXuan Zhuo netif_stop_subqueue(dev, qnum); 615b8ef4809SXuan Zhuo if (use_napi) { 616b8ef4809SXuan Zhuo if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) 617b8ef4809SXuan Zhuo virtqueue_napi_schedule(&sq->napi, sq->vq); 618b8ef4809SXuan Zhuo } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 619b8ef4809SXuan Zhuo /* More just got used, free them then recheck. */ 620b8ef4809SXuan Zhuo free_old_xmit_skbs(sq, false); 621b8ef4809SXuan Zhuo if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 622b8ef4809SXuan Zhuo netif_start_subqueue(dev, qnum); 623b8ef4809SXuan Zhuo virtqueue_disable_cb(sq->vq); 624b8ef4809SXuan Zhuo } 625b8ef4809SXuan Zhuo } 626b8ef4809SXuan Zhuo } 627b8ef4809SXuan Zhuo } 628b8ef4809SXuan Zhuo 629735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, 630735fc405SJesper Dangaard Brouer struct send_queue *sq, 63144fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf) 63256434a01SJohn Fastabend { 63356434a01SJohn Fastabend struct virtio_net_hdr_mrg_rxbuf *hdr; 63497717e8dSHeng Qi struct skb_shared_info *shinfo; 63597717e8dSHeng Qi u8 nr_frags = 0; 63697717e8dSHeng Qi int err, i; 63756434a01SJohn Fastabend 638cac320c8SJesper Dangaard Brouer if (unlikely(xdpf->headroom < vi->hdr_len)) 639cac320c8SJesper Dangaard Brouer return -EOVERFLOW; 640cac320c8SJesper Dangaard Brouer 64197717e8dSHeng Qi if (unlikely(xdp_frame_has_frags(xdpf))) { 64297717e8dSHeng Qi shinfo = xdp_get_shared_info_from_frame(xdpf); 64397717e8dSHeng Qi nr_frags = shinfo->nr_frags; 64497717e8dSHeng Qi } 64597717e8dSHeng Qi 64697717e8dSHeng Qi /* In wrapping function virtnet_xdp_xmit(), we need to free 64797717e8dSHeng Qi * up the pending old buffers, where we need to calculate the 64897717e8dSHeng Qi * position of skb_shared_info in xdp_get_frame_len() and 64997717e8dSHeng Qi * xdp_return_frame(), which will involve to xdpf->data and 65097717e8dSHeng Qi * xdpf->headroom. Therefore, we need to update the value of 65197717e8dSHeng Qi * headroom synchronously here. 65297717e8dSHeng Qi */ 65397717e8dSHeng Qi xdpf->headroom -= vi->hdr_len; 654cac320c8SJesper Dangaard Brouer xdpf->data -= vi->hdr_len; 65556434a01SJohn Fastabend /* Zero header and leave csum up to XDP layers */ 656cac320c8SJesper Dangaard Brouer hdr = xdpf->data; 65756434a01SJohn Fastabend memset(hdr, 0, vi->hdr_len); 658cac320c8SJesper Dangaard Brouer xdpf->len += vi->hdr_len; 65956434a01SJohn Fastabend 66097717e8dSHeng Qi sg_init_table(sq->sg, nr_frags + 1); 66197717e8dSHeng Qi sg_set_buf(sq->sg, xdpf->data, xdpf->len); 66297717e8dSHeng Qi for (i = 0; i < nr_frags; i++) { 66397717e8dSHeng Qi skb_frag_t *frag = &shinfo->frags[i]; 664bb91accfSJason Wang 66597717e8dSHeng Qi sg_set_page(&sq->sg[i + 1], skb_frag_page(frag), 66697717e8dSHeng Qi skb_frag_size(frag), skb_frag_off(frag)); 66797717e8dSHeng Qi } 66897717e8dSHeng Qi 66997717e8dSHeng Qi err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1, 67097717e8dSHeng Qi xdp_to_ptr(xdpf), GFP_ATOMIC); 67111b7d897SJesper Dangaard Brouer if (unlikely(err)) 672cac320c8SJesper Dangaard Brouer return -ENOSPC; /* Caller handle free/refcnt */ 67356434a01SJohn Fastabend 674cac320c8SJesper Dangaard Brouer return 0; 67556434a01SJohn Fastabend } 67656434a01SJohn Fastabend 67797c2c69eSXuan Zhuo /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on 67897c2c69eSXuan Zhuo * the current cpu, so it does not need to be locked. 67997c2c69eSXuan Zhuo * 68097c2c69eSXuan Zhuo * Here we use marco instead of inline functions because we have to deal with 68197c2c69eSXuan Zhuo * three issues at the same time: 1. the choice of sq. 2. judge and execute the 68297c2c69eSXuan Zhuo * lock/unlock of txq 3. make sparse happy. It is difficult for two inline 68397c2c69eSXuan Zhuo * functions to perfectly solve these three problems at the same time. 68497c2c69eSXuan Zhuo */ 68597c2c69eSXuan Zhuo #define virtnet_xdp_get_sq(vi) ({ \ 6863dcc1edcSLi RongQing int cpu = smp_processor_id(); \ 68797c2c69eSXuan Zhuo struct netdev_queue *txq; \ 68897c2c69eSXuan Zhuo typeof(vi) v = (vi); \ 68997c2c69eSXuan Zhuo unsigned int qp; \ 69097c2c69eSXuan Zhuo \ 69197c2c69eSXuan Zhuo if (v->curr_queue_pairs > nr_cpu_ids) { \ 69297c2c69eSXuan Zhuo qp = v->curr_queue_pairs - v->xdp_queue_pairs; \ 6933dcc1edcSLi RongQing qp += cpu; \ 69497c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, qp); \ 69597c2c69eSXuan Zhuo __netif_tx_acquire(txq); \ 69697c2c69eSXuan Zhuo } else { \ 6973dcc1edcSLi RongQing qp = cpu % v->curr_queue_pairs; \ 69897c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, qp); \ 6993dcc1edcSLi RongQing __netif_tx_lock(txq, cpu); \ 70097c2c69eSXuan Zhuo } \ 70197c2c69eSXuan Zhuo v->sq + qp; \ 70297c2c69eSXuan Zhuo }) 7032a43565cSToshiaki Makita 70497c2c69eSXuan Zhuo #define virtnet_xdp_put_sq(vi, q) { \ 70597c2c69eSXuan Zhuo struct netdev_queue *txq; \ 70697c2c69eSXuan Zhuo typeof(vi) v = (vi); \ 70797c2c69eSXuan Zhuo \ 70897c2c69eSXuan Zhuo txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \ 70997c2c69eSXuan Zhuo if (v->curr_queue_pairs > nr_cpu_ids) \ 71097c2c69eSXuan Zhuo __netif_tx_release(txq); \ 71197c2c69eSXuan Zhuo else \ 71297c2c69eSXuan Zhuo __netif_tx_unlock(txq); \ 7132a43565cSToshiaki Makita } 7142a43565cSToshiaki Makita 715735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev, 71642b33468SJesper Dangaard Brouer int n, struct xdp_frame **frames, u32 flags) 717186b3c99SJason Wang { 718186b3c99SJason Wang struct virtnet_info *vi = netdev_priv(dev); 7198dcc5b0aSJesper Dangaard Brouer struct receive_queue *rq = vi->rq; 7208dcc5b0aSJesper Dangaard Brouer struct bpf_prog *xdp_prog; 721735fc405SJesper Dangaard Brouer struct send_queue *sq; 722735fc405SJesper Dangaard Brouer unsigned int len; 723546f2897SToshiaki Makita int packets = 0; 724546f2897SToshiaki Makita int bytes = 0; 725fdc13979SLorenzo Bianconi int nxmit = 0; 726461f03dcSToshiaki Makita int kicks = 0; 7275050471dSToshiaki Makita void *ptr; 728fdc13979SLorenzo Bianconi int ret; 729735fc405SJesper Dangaard Brouer int i; 730735fc405SJesper Dangaard Brouer 7318dcc5b0aSJesper Dangaard Brouer /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 7328dcc5b0aSJesper Dangaard Brouer * indicate XDP resources have been successfully allocated. 7338dcc5b0aSJesper Dangaard Brouer */ 7349719c6b9SJohn Fastabend xdp_prog = rcu_access_pointer(rq->xdp_prog); 7351667c08aSToshiaki Makita if (!xdp_prog) 7361667c08aSToshiaki Makita return -ENXIO; 7371667c08aSToshiaki Makita 73897c2c69eSXuan Zhuo sq = virtnet_xdp_get_sq(vi); 7399ab86bbcSShirley Ma 7409ab86bbcSShirley Ma if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { 741186b3c99SJason Wang ret = -EINVAL; 742186b3c99SJason Wang goto out; 743186b3c99SJason Wang } 744186b3c99SJason Wang 745735fc405SJesper Dangaard Brouer /* Free up any pending old buffers before queueing new ones. */ 7465050471dSToshiaki Makita while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 747546f2897SToshiaki Makita if (likely(is_xdp_frame(ptr))) { 748546f2897SToshiaki Makita struct xdp_frame *frame = ptr_to_xdp(ptr); 749546f2897SToshiaki Makita 75050bd14bcSHeng Qi bytes += xdp_get_frame_len(frame); 751546f2897SToshiaki Makita xdp_return_frame(frame); 752546f2897SToshiaki Makita } else { 753546f2897SToshiaki Makita struct sk_buff *skb = ptr; 754546f2897SToshiaki Makita 755546f2897SToshiaki Makita bytes += skb->len; 756546f2897SToshiaki Makita napi_consume_skb(skb, false); 757546f2897SToshiaki Makita } 758546f2897SToshiaki Makita packets++; 7595050471dSToshiaki Makita } 760735fc405SJesper Dangaard Brouer 761735fc405SJesper Dangaard Brouer for (i = 0; i < n; i++) { 762735fc405SJesper Dangaard Brouer struct xdp_frame *xdpf = frames[i]; 763735fc405SJesper Dangaard Brouer 764fdc13979SLorenzo Bianconi if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) 765fdc13979SLorenzo Bianconi break; 766fdc13979SLorenzo Bianconi nxmit++; 767735fc405SJesper Dangaard Brouer } 768fdc13979SLorenzo Bianconi ret = nxmit; 7695d274cb4SJesper Dangaard Brouer 770*cd1c604aSXuan Zhuo if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq)) 771*cd1c604aSXuan Zhuo check_sq_full_and_disable(vi, dev, sq); 772*cd1c604aSXuan Zhuo 773461f03dcSToshiaki Makita if (flags & XDP_XMIT_FLUSH) { 774461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) 775461f03dcSToshiaki Makita kicks = 1; 776461f03dcSToshiaki Makita } 7775b8f3c8dSToshiaki Makita out: 7785b8f3c8dSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 779546f2897SToshiaki Makita sq->stats.bytes += bytes; 780546f2897SToshiaki Makita sq->stats.packets += packets; 7815b8f3c8dSToshiaki Makita sq->stats.xdp_tx += n; 782fdc13979SLorenzo Bianconi sq->stats.xdp_tx_drops += n - nxmit; 783461f03dcSToshiaki Makita sq->stats.kicks += kicks; 7845b8f3c8dSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 7855d274cb4SJesper Dangaard Brouer 78697c2c69eSXuan Zhuo virtnet_xdp_put_sq(vi, sq); 7875b8f3c8dSToshiaki Makita return ret; 788186b3c99SJason Wang } 789186b3c99SJason Wang 790f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 791f6b10209SJason Wang { 79297c2c69eSXuan Zhuo return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; 793f6b10209SJason Wang } 794f6b10209SJason Wang 7954941d472SJason Wang /* We copy the packet for XDP in the following cases: 7964941d472SJason Wang * 7974941d472SJason Wang * 1) Packet is scattered across multiple rx buffers. 7984941d472SJason Wang * 2) Headroom space is insufficient. 7994941d472SJason Wang * 8004941d472SJason Wang * This is inefficient but it's a temporary condition that 8014941d472SJason Wang * we hit right after XDP is enabled and until queue is refilled 8024941d472SJason Wang * with large buffers with sufficient headroom - so it should affect 8034941d472SJason Wang * at most queue size packets. 8044941d472SJason Wang * Afterwards, the conditions to enable 8054941d472SJason Wang * XDP should preclude the underlying device from sending packets 8064941d472SJason Wang * across multiple buffers (num_buf > 1), and we make sure buffers 8074941d472SJason Wang * have enough headroom. 80872979a6cSJohn Fastabend */ 80972979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq, 810981f14d4SHeng Qi int *num_buf, 81172979a6cSJohn Fastabend struct page *p, 81272979a6cSJohn Fastabend int offset, 8134941d472SJason Wang int page_off, 81472979a6cSJohn Fastabend unsigned int *len) 81572979a6cSJohn Fastabend { 81672979a6cSJohn Fastabend struct page *page = alloc_page(GFP_ATOMIC); 81772979a6cSJohn Fastabend 81872979a6cSJohn Fastabend if (!page) 81972979a6cSJohn Fastabend return NULL; 82072979a6cSJohn Fastabend 82172979a6cSJohn Fastabend memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 82272979a6cSJohn Fastabend page_off += *len; 82372979a6cSJohn Fastabend 82456a86f84SJason Wang while (--*num_buf) { 8253cc81a9aSJason Wang int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 82672979a6cSJohn Fastabend unsigned int buflen; 82772979a6cSJohn Fastabend void *buf; 82872979a6cSJohn Fastabend int off; 82972979a6cSJohn Fastabend 830680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &buflen); 831680557cfSMichael S. Tsirkin if (unlikely(!buf)) 83272979a6cSJohn Fastabend goto err_buf; 83372979a6cSJohn Fastabend 83472979a6cSJohn Fastabend p = virt_to_head_page(buf); 83572979a6cSJohn Fastabend off = buf - page_address(p); 83672979a6cSJohn Fastabend 83756a86f84SJason Wang /* guard against a misconfigured or uncooperative backend that 83856a86f84SJason Wang * is sending packet larger than the MTU. 83956a86f84SJason Wang */ 8403cc81a9aSJason Wang if ((page_off + buflen + tailroom) > PAGE_SIZE) { 84156a86f84SJason Wang put_page(p); 84256a86f84SJason Wang goto err_buf; 84356a86f84SJason Wang } 84456a86f84SJason Wang 84572979a6cSJohn Fastabend memcpy(page_address(page) + page_off, 84672979a6cSJohn Fastabend page_address(p) + off, buflen); 84772979a6cSJohn Fastabend page_off += buflen; 84856a86f84SJason Wang put_page(p); 84972979a6cSJohn Fastabend } 85072979a6cSJohn Fastabend 8512de2f7f4SJohn Fastabend /* Headroom does not contribute to packet length */ 8522de2f7f4SJohn Fastabend *len = page_off - VIRTIO_XDP_HEADROOM; 85372979a6cSJohn Fastabend return page; 85472979a6cSJohn Fastabend err_buf: 85572979a6cSJohn Fastabend __free_pages(page, 0); 85672979a6cSJohn Fastabend return NULL; 85772979a6cSJohn Fastabend } 85872979a6cSJohn Fastabend 8594941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev, 8604941d472SJason Wang struct virtnet_info *vi, 8614941d472SJason Wang struct receive_queue *rq, 8624941d472SJason Wang void *buf, void *ctx, 863186b3c99SJason Wang unsigned int len, 8647d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 865d46eeeafSJason Wang struct virtnet_rq_stats *stats) 8664941d472SJason Wang { 8674941d472SJason Wang struct sk_buff *skb; 8684941d472SJason Wang struct bpf_prog *xdp_prog; 8694941d472SJason Wang unsigned int xdp_headroom = (unsigned long)ctx; 8704941d472SJason Wang unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 8714941d472SJason Wang unsigned int headroom = vi->hdr_len + header_offset; 8724941d472SJason Wang unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 8734941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 8744941d472SJason Wang struct page *page = virt_to_head_page(buf); 87511b7d897SJesper Dangaard Brouer unsigned int delta = 0; 8764941d472SJason Wang struct page *xdp_page; 87711b7d897SJesper Dangaard Brouer int err; 878503d539aSYuya Kusakabe unsigned int metasize = 0; 87911b7d897SJesper Dangaard Brouer 8804941d472SJason Wang len -= vi->hdr_len; 881d46eeeafSJason Wang stats->bytes += len; 8824941d472SJason Wang 883ad993a95SXie Yongji if (unlikely(len > GOOD_PACKET_LEN)) { 884ad993a95SXie Yongji pr_debug("%s: rx error: len %u exceeds max size %d\n", 885ad993a95SXie Yongji dev->name, len, GOOD_PACKET_LEN); 886ad993a95SXie Yongji dev->stats.rx_length_errors++; 887053c9e18SWenliang Wang goto err; 888ad993a95SXie Yongji } 8896213f07cSLi RongQing 8906213f07cSLi RongQing if (likely(!vi->xdp_enabled)) { 8916213f07cSLi RongQing xdp_prog = NULL; 8926213f07cSLi RongQing goto skip_xdp; 8936213f07cSLi RongQing } 8946213f07cSLi RongQing 8954941d472SJason Wang rcu_read_lock(); 8964941d472SJason Wang xdp_prog = rcu_dereference(rq->xdp_prog); 8974941d472SJason Wang if (xdp_prog) { 8984941d472SJason Wang struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 89944fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 9004941d472SJason Wang struct xdp_buff xdp; 9014941d472SJason Wang void *orig_data; 9024941d472SJason Wang u32 act; 9034941d472SJason Wang 90495dbe9e7SJesper Dangaard Brouer if (unlikely(hdr->hdr.gso_type)) 9054941d472SJason Wang goto err_xdp; 9064941d472SJason Wang 9074941d472SJason Wang if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 9084941d472SJason Wang int offset = buf - page_address(page) + header_offset; 9094941d472SJason Wang unsigned int tlen = len + vi->hdr_len; 910981f14d4SHeng Qi int num_buf = 1; 9114941d472SJason Wang 9124941d472SJason Wang xdp_headroom = virtnet_get_headroom(vi); 9134941d472SJason Wang header_offset = VIRTNET_RX_PAD + xdp_headroom; 9144941d472SJason Wang headroom = vi->hdr_len + header_offset; 9154941d472SJason Wang buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 9164941d472SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 9174941d472SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, page, 9184941d472SJason Wang offset, header_offset, 9194941d472SJason Wang &tlen); 9204941d472SJason Wang if (!xdp_page) 9214941d472SJason Wang goto err_xdp; 9224941d472SJason Wang 9234941d472SJason Wang buf = page_address(xdp_page); 9244941d472SJason Wang put_page(page); 9254941d472SJason Wang page = xdp_page; 9264941d472SJason Wang } 9274941d472SJason Wang 92843b5169dSLorenzo Bianconi xdp_init_buff(&xdp, buflen, &rq->xdp_rxq); 929be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, 930be9df4afSLorenzo Bianconi xdp_headroom, len, true); 9314941d472SJason Wang orig_data = xdp.data; 9324941d472SJason Wang act = bpf_prog_run_xdp(xdp_prog, &xdp); 933d46eeeafSJason Wang stats->xdp_packets++; 9344941d472SJason Wang 9354941d472SJason Wang switch (act) { 9364941d472SJason Wang case XDP_PASS: 9374941d472SJason Wang /* Recalculate length in case bpf program changed it */ 9384941d472SJason Wang delta = orig_data - xdp.data; 9396870de43SNikita V. Shirokov len = xdp.data_end - xdp.data; 940503d539aSYuya Kusakabe metasize = xdp.data - xdp.data_meta; 9414941d472SJason Wang break; 9424941d472SJason Wang case XDP_TX: 943d46eeeafSJason Wang stats->xdp_tx++; 9441b698fa5SLorenzo Bianconi xdpf = xdp_convert_buff_to_frame(&xdp); 94544fa2dbdSJesper Dangaard Brouer if (unlikely(!xdpf)) 94644fa2dbdSJesper Dangaard Brouer goto err_xdp; 947ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 948fdc13979SLorenzo Bianconi if (unlikely(!err)) { 949fdc13979SLorenzo Bianconi xdp_return_frame_rx_napi(xdpf); 950fdc13979SLorenzo Bianconi } else if (unlikely(err < 0)) { 9514941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 95211b7d897SJesper Dangaard Brouer goto err_xdp; 95311b7d897SJesper Dangaard Brouer } 9542471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 955186b3c99SJason Wang rcu_read_unlock(); 956186b3c99SJason Wang goto xdp_xmit; 957186b3c99SJason Wang case XDP_REDIRECT: 958d46eeeafSJason Wang stats->xdp_redirects++; 959186b3c99SJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 96011b7d897SJesper Dangaard Brouer if (err) 96111b7d897SJesper Dangaard Brouer goto err_xdp; 9622471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 9634941d472SJason Wang rcu_read_unlock(); 9644941d472SJason Wang goto xdp_xmit; 9654941d472SJason Wang default: 966c8064e5bSPaolo Abeni bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act); 967df561f66SGustavo A. R. Silva fallthrough; 9684941d472SJason Wang case XDP_ABORTED: 9694941d472SJason Wang trace_xdp_exception(vi->dev, xdp_prog, act); 97095efabf0SGustavo A. R. Silva goto err_xdp; 9714941d472SJason Wang case XDP_DROP: 9724941d472SJason Wang goto err_xdp; 9734941d472SJason Wang } 9744941d472SJason Wang } 9754941d472SJason Wang rcu_read_unlock(); 9764941d472SJason Wang 9776213f07cSLi RongQing skip_xdp: 9784941d472SJason Wang skb = build_skb(buf, buflen); 979053c9e18SWenliang Wang if (!skb) 9804941d472SJason Wang goto err; 9814941d472SJason Wang skb_reserve(skb, headroom - delta); 9826870de43SNikita V. Shirokov skb_put(skb, len); 983f1d4884dSYuya Kusakabe if (!xdp_prog) { 9844941d472SJason Wang buf += header_offset; 9854941d472SJason Wang memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 986f1d4884dSYuya Kusakabe } /* keep zeroed vnet hdr since XDP is loaded */ 9874941d472SJason Wang 988503d539aSYuya Kusakabe if (metasize) 989503d539aSYuya Kusakabe skb_metadata_set(skb, metasize); 990503d539aSYuya Kusakabe 9914941d472SJason Wang return skb; 9924941d472SJason Wang 9934941d472SJason Wang err_xdp: 9944941d472SJason Wang rcu_read_unlock(); 995d46eeeafSJason Wang stats->xdp_drops++; 996053c9e18SWenliang Wang err: 997d46eeeafSJason Wang stats->drops++; 9984941d472SJason Wang put_page(page); 9994941d472SJason Wang xdp_xmit: 10004941d472SJason Wang return NULL; 10014941d472SJason Wang } 10024941d472SJason Wang 10034941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev, 10044941d472SJason Wang struct virtnet_info *vi, 10054941d472SJason Wang struct receive_queue *rq, 10064941d472SJason Wang void *buf, 10077d9d60fdSToshiaki Makita unsigned int len, 1008d46eeeafSJason Wang struct virtnet_rq_stats *stats) 10094941d472SJason Wang { 10104941d472SJason Wang struct page *page = buf; 1011503d539aSYuya Kusakabe struct sk_buff *skb = 101218117a84SHeng Qi page_to_skb(vi, rq, page, 0, len, PAGE_SIZE); 10134941d472SJason Wang 1014d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 10154941d472SJason Wang if (unlikely(!skb)) 10164941d472SJason Wang goto err; 10174941d472SJason Wang 10184941d472SJason Wang return skb; 10194941d472SJason Wang 10204941d472SJason Wang err: 1021d46eeeafSJason Wang stats->drops++; 10224941d472SJason Wang give_pages(rq, page); 10234941d472SJason Wang return NULL; 10244941d472SJason Wang } 10254941d472SJason Wang 1026b26aa481SHeng Qi /* Why not use xdp_build_skb_from_frame() ? 1027b26aa481SHeng Qi * XDP core assumes that xdp frags are PAGE_SIZE in length, while in 1028b26aa481SHeng Qi * virtio-net there are 2 points that do not match its requirements: 1029b26aa481SHeng Qi * 1. The size of the prefilled buffer is not fixed before xdp is set. 1030b26aa481SHeng Qi * 2. xdp_build_skb_from_frame() does more checks that we don't need, 1031b26aa481SHeng Qi * like eth_type_trans() (which virtio-net does in receive_buf()). 1032b26aa481SHeng Qi */ 1033b26aa481SHeng Qi static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev, 1034b26aa481SHeng Qi struct virtnet_info *vi, 1035b26aa481SHeng Qi struct xdp_buff *xdp, 1036b26aa481SHeng Qi unsigned int xdp_frags_truesz) 1037b26aa481SHeng Qi { 1038b26aa481SHeng Qi struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp); 1039b26aa481SHeng Qi unsigned int headroom, data_len; 1040b26aa481SHeng Qi struct sk_buff *skb; 1041b26aa481SHeng Qi int metasize; 1042b26aa481SHeng Qi u8 nr_frags; 1043b26aa481SHeng Qi 1044b26aa481SHeng Qi if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) { 1045b26aa481SHeng Qi pr_debug("Error building skb as missing reserved tailroom for xdp"); 1046b26aa481SHeng Qi return NULL; 1047b26aa481SHeng Qi } 1048b26aa481SHeng Qi 1049b26aa481SHeng Qi if (unlikely(xdp_buff_has_frags(xdp))) 1050b26aa481SHeng Qi nr_frags = sinfo->nr_frags; 1051b26aa481SHeng Qi 1052b26aa481SHeng Qi skb = build_skb(xdp->data_hard_start, xdp->frame_sz); 1053b26aa481SHeng Qi if (unlikely(!skb)) 1054b26aa481SHeng Qi return NULL; 1055b26aa481SHeng Qi 1056b26aa481SHeng Qi headroom = xdp->data - xdp->data_hard_start; 1057b26aa481SHeng Qi data_len = xdp->data_end - xdp->data; 1058b26aa481SHeng Qi skb_reserve(skb, headroom); 1059b26aa481SHeng Qi __skb_put(skb, data_len); 1060b26aa481SHeng Qi 1061b26aa481SHeng Qi metasize = xdp->data - xdp->data_meta; 1062b26aa481SHeng Qi metasize = metasize > 0 ? metasize : 0; 1063b26aa481SHeng Qi if (metasize) 1064b26aa481SHeng Qi skb_metadata_set(skb, metasize); 1065b26aa481SHeng Qi 1066b26aa481SHeng Qi if (unlikely(xdp_buff_has_frags(xdp))) 1067b26aa481SHeng Qi xdp_update_skb_shared_info(skb, nr_frags, 1068b26aa481SHeng Qi sinfo->xdp_frags_size, 1069b26aa481SHeng Qi xdp_frags_truesz, 1070b26aa481SHeng Qi xdp_buff_is_frag_pfmemalloc(xdp)); 1071b26aa481SHeng Qi 1072b26aa481SHeng Qi return skb; 1073b26aa481SHeng Qi } 1074b26aa481SHeng Qi 1075ef75cb51SHeng Qi /* TODO: build xdp in big mode */ 1076ef75cb51SHeng Qi static int virtnet_build_xdp_buff_mrg(struct net_device *dev, 1077ef75cb51SHeng Qi struct virtnet_info *vi, 1078ef75cb51SHeng Qi struct receive_queue *rq, 1079ef75cb51SHeng Qi struct xdp_buff *xdp, 1080ef75cb51SHeng Qi void *buf, 1081ef75cb51SHeng Qi unsigned int len, 1082ef75cb51SHeng Qi unsigned int frame_sz, 1083981f14d4SHeng Qi int *num_buf, 1084ef75cb51SHeng Qi unsigned int *xdp_frags_truesize, 1085ef75cb51SHeng Qi struct virtnet_rq_stats *stats) 1086ef75cb51SHeng Qi { 1087ef75cb51SHeng Qi struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 1088ef75cb51SHeng Qi unsigned int headroom, tailroom, room; 1089ef75cb51SHeng Qi unsigned int truesize, cur_frag_size; 1090ef75cb51SHeng Qi struct skb_shared_info *shinfo; 1091ef75cb51SHeng Qi unsigned int xdp_frags_truesz = 0; 1092ef75cb51SHeng Qi struct page *page; 1093ef75cb51SHeng Qi skb_frag_t *frag; 1094ef75cb51SHeng Qi int offset; 1095ef75cb51SHeng Qi void *ctx; 1096ef75cb51SHeng Qi 1097ef75cb51SHeng Qi xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq); 1098ef75cb51SHeng Qi xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM, 1099ef75cb51SHeng Qi VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true); 1100ef75cb51SHeng Qi 1101981f14d4SHeng Qi if (!*num_buf) 1102981f14d4SHeng Qi return 0; 1103981f14d4SHeng Qi 1104ef75cb51SHeng Qi if (*num_buf > 1) { 1105ef75cb51SHeng Qi /* If we want to build multi-buffer xdp, we need 1106ef75cb51SHeng Qi * to specify that the flags of xdp_buff have the 1107ef75cb51SHeng Qi * XDP_FLAGS_HAS_FRAG bit. 1108ef75cb51SHeng Qi */ 1109ef75cb51SHeng Qi if (!xdp_buff_has_frags(xdp)) 1110ef75cb51SHeng Qi xdp_buff_set_frags_flag(xdp); 1111ef75cb51SHeng Qi 1112ef75cb51SHeng Qi shinfo = xdp_get_shared_info_from_buff(xdp); 1113ef75cb51SHeng Qi shinfo->nr_frags = 0; 1114ef75cb51SHeng Qi shinfo->xdp_frags_size = 0; 1115ef75cb51SHeng Qi } 1116ef75cb51SHeng Qi 1117981f14d4SHeng Qi if (*num_buf > MAX_SKB_FRAGS + 1) 1118ef75cb51SHeng Qi return -EINVAL; 1119ef75cb51SHeng Qi 1120981f14d4SHeng Qi while (--*num_buf > 0) { 1121ef75cb51SHeng Qi buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 1122ef75cb51SHeng Qi if (unlikely(!buf)) { 1123ef75cb51SHeng Qi pr_debug("%s: rx error: %d buffers out of %d missing\n", 1124ef75cb51SHeng Qi dev->name, *num_buf, 1125ef75cb51SHeng Qi virtio16_to_cpu(vi->vdev, hdr->num_buffers)); 1126ef75cb51SHeng Qi dev->stats.rx_length_errors++; 1127ef75cb51SHeng Qi return -EINVAL; 1128ef75cb51SHeng Qi } 1129ef75cb51SHeng Qi 1130ef75cb51SHeng Qi stats->bytes += len; 1131ef75cb51SHeng Qi page = virt_to_head_page(buf); 1132ef75cb51SHeng Qi offset = buf - page_address(page); 1133ef75cb51SHeng Qi 1134ef75cb51SHeng Qi truesize = mergeable_ctx_to_truesize(ctx); 1135ef75cb51SHeng Qi headroom = mergeable_ctx_to_headroom(ctx); 1136ef75cb51SHeng Qi tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1137ef75cb51SHeng Qi room = SKB_DATA_ALIGN(headroom + tailroom); 1138ef75cb51SHeng Qi 1139ef75cb51SHeng Qi cur_frag_size = truesize; 1140ef75cb51SHeng Qi xdp_frags_truesz += cur_frag_size; 1141ef75cb51SHeng Qi if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) { 1142ef75cb51SHeng Qi put_page(page); 1143ef75cb51SHeng Qi pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1144ef75cb51SHeng Qi dev->name, len, (unsigned long)(truesize - room)); 1145ef75cb51SHeng Qi dev->stats.rx_length_errors++; 1146ef75cb51SHeng Qi return -EINVAL; 1147ef75cb51SHeng Qi } 1148ef75cb51SHeng Qi 1149ef75cb51SHeng Qi frag = &shinfo->frags[shinfo->nr_frags++]; 1150ef75cb51SHeng Qi __skb_frag_set_page(frag, page); 1151ef75cb51SHeng Qi skb_frag_off_set(frag, offset); 1152ef75cb51SHeng Qi skb_frag_size_set(frag, len); 1153ef75cb51SHeng Qi if (page_is_pfmemalloc(page)) 1154ef75cb51SHeng Qi xdp_buff_set_frag_pfmemalloc(xdp); 1155ef75cb51SHeng Qi 1156ef75cb51SHeng Qi shinfo->xdp_frags_size += len; 1157ef75cb51SHeng Qi } 1158ef75cb51SHeng Qi 1159ef75cb51SHeng Qi *xdp_frags_truesize = xdp_frags_truesz; 1160ef75cb51SHeng Qi return 0; 1161ef75cb51SHeng Qi } 1162ef75cb51SHeng Qi 11638fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 1164fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 11658fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 1166680557cfSMichael S. Tsirkin void *buf, 1167680557cfSMichael S. Tsirkin void *ctx, 1168186b3c99SJason Wang unsigned int len, 11697d9d60fdSToshiaki Makita unsigned int *xdp_xmit, 1170d46eeeafSJason Wang struct virtnet_rq_stats *stats) 11719ab86bbcSShirley Ma { 1172012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 1173981f14d4SHeng Qi int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 11748fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 11758fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 1176f600b690SJohn Fastabend struct sk_buff *head_skb, *curr_skb; 1177f600b690SJohn Fastabend struct bpf_prog *xdp_prog; 11789ce6146eSJesper Dangaard Brouer unsigned int truesize = mergeable_ctx_to_truesize(ctx); 11794941d472SJason Wang unsigned int headroom = mergeable_ctx_to_headroom(ctx); 1180ef75cb51SHeng Qi unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1181ef75cb51SHeng Qi unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 118222174f79SHeng Qi unsigned int frame_sz, xdp_room; 11839ce6146eSJesper Dangaard Brouer int err; 1184ab7db917SMichael Dalton 118556434a01SJohn Fastabend head_skb = NULL; 1186d46eeeafSJason Wang stats->bytes += len - vi->hdr_len; 118756434a01SJohn Fastabend 1188ef75cb51SHeng Qi if (unlikely(len > truesize - room)) { 1189ad993a95SXie Yongji pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1190ef75cb51SHeng Qi dev->name, len, (unsigned long)(truesize - room)); 1191ad993a95SXie Yongji dev->stats.rx_length_errors++; 1192ad993a95SXie Yongji goto err_skb; 1193ad993a95SXie Yongji } 11946213f07cSLi RongQing 11956213f07cSLi RongQing if (likely(!vi->xdp_enabled)) { 11966213f07cSLi RongQing xdp_prog = NULL; 11976213f07cSLi RongQing goto skip_xdp; 11986213f07cSLi RongQing } 11996213f07cSLi RongQing 1200f600b690SJohn Fastabend rcu_read_lock(); 1201f600b690SJohn Fastabend xdp_prog = rcu_dereference(rq->xdp_prog); 1202f600b690SJohn Fastabend if (xdp_prog) { 120322174f79SHeng Qi unsigned int xdp_frags_truesz = 0; 120422174f79SHeng Qi struct skb_shared_info *shinfo; 120544fa2dbdSJesper Dangaard Brouer struct xdp_frame *xdpf; 120672979a6cSJohn Fastabend struct page *xdp_page; 12070354e4d1SJohn Fastabend struct xdp_buff xdp; 12080354e4d1SJohn Fastabend void *data; 1209f600b690SJohn Fastabend u32 act; 121022174f79SHeng Qi int i; 1211f600b690SJohn Fastabend 12123d62b2a0SJason Wang /* Transient failure which in theory could occur if 12133d62b2a0SJason Wang * in-flight packets from before XDP was enabled reach 12143d62b2a0SJason Wang * the receive path after XDP is loaded. 12153d62b2a0SJason Wang */ 12163d62b2a0SJason Wang if (unlikely(hdr->hdr.gso_type)) 12173d62b2a0SJason Wang goto err_xdp; 12183d62b2a0SJason Wang 1219ef75cb51SHeng Qi /* Now XDP core assumes frag size is PAGE_SIZE, but buffers 1220ef75cb51SHeng Qi * with headroom may add hole in truesize, which 1221ef75cb51SHeng Qi * make their length exceed PAGE_SIZE. So we disabled the 1222ef75cb51SHeng Qi * hole mechanism for xdp. See add_recvbuf_mergeable(). 12239ce6146eSJesper Dangaard Brouer */ 1224ef75cb51SHeng Qi frame_sz = truesize; 12259ce6146eSJesper Dangaard Brouer 122622174f79SHeng Qi /* This happens when headroom is not enough because 122722174f79SHeng Qi * of the buffer was prefilled before XDP is set. 122822174f79SHeng Qi * This should only happen for the first several packets. 122922174f79SHeng Qi * In fact, vq reset can be used here to help us clean up 123022174f79SHeng Qi * the prefilled buffers, but many existing devices do not 123122174f79SHeng Qi * support it, and we don't want to bother users who are 123222174f79SHeng Qi * using xdp normally. 12333cc81a9aSJason Wang */ 123422174f79SHeng Qi if (!xdp_prog->aux->xdp_has_frags && 123522174f79SHeng Qi (num_buf > 1 || headroom < virtnet_get_headroom(vi))) { 123672979a6cSJohn Fastabend /* linearize data for XDP */ 123756a86f84SJason Wang xdp_page = xdp_linearize_page(rq, &num_buf, 12384941d472SJason Wang page, offset, 12394941d472SJason Wang VIRTIO_XDP_HEADROOM, 12404941d472SJason Wang &len); 12419ce6146eSJesper Dangaard Brouer frame_sz = PAGE_SIZE; 12429ce6146eSJesper Dangaard Brouer 124372979a6cSJohn Fastabend if (!xdp_page) 1244f600b690SJohn Fastabend goto err_xdp; 12452de2f7f4SJohn Fastabend offset = VIRTIO_XDP_HEADROOM; 124622174f79SHeng Qi } else if (unlikely(headroom < virtnet_get_headroom(vi))) { 124722174f79SHeng Qi xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM + 124822174f79SHeng Qi sizeof(struct skb_shared_info)); 124922174f79SHeng Qi if (len + xdp_room > PAGE_SIZE) 125022174f79SHeng Qi goto err_xdp; 125122174f79SHeng Qi 125222174f79SHeng Qi xdp_page = alloc_page(GFP_ATOMIC); 125322174f79SHeng Qi if (!xdp_page) 125422174f79SHeng Qi goto err_xdp; 125522174f79SHeng Qi 125622174f79SHeng Qi memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM, 125722174f79SHeng Qi page_address(page) + offset, len); 125822174f79SHeng Qi frame_sz = PAGE_SIZE; 125922174f79SHeng Qi offset = VIRTIO_XDP_HEADROOM; 126072979a6cSJohn Fastabend } else { 126172979a6cSJohn Fastabend xdp_page = page; 1262f600b690SJohn Fastabend } 1263f600b690SJohn Fastabend 12640354e4d1SJohn Fastabend data = page_address(xdp_page) + offset; 126522174f79SHeng Qi err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz, 126622174f79SHeng Qi &num_buf, &xdp_frags_truesz, stats); 126722174f79SHeng Qi if (unlikely(err)) 126822174f79SHeng Qi goto err_xdp_frags; 1269754b8a21SJesper Dangaard Brouer 12700354e4d1SJohn Fastabend act = bpf_prog_run_xdp(xdp_prog, &xdp); 1271d46eeeafSJason Wang stats->xdp_packets++; 12720354e4d1SJohn Fastabend 127356434a01SJohn Fastabend switch (act) { 127456434a01SJohn Fastabend case XDP_PASS: 1275fab89bafSHeng Qi if (unlikely(xdp_page != page)) 12761830f893SJason Wang put_page(page); 1277fab89bafSHeng Qi head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz); 1278fab89bafSHeng Qi rcu_read_unlock(); 12791830f893SJason Wang return head_skb; 128056434a01SJohn Fastabend case XDP_TX: 1281d46eeeafSJason Wang stats->xdp_tx++; 12821b698fa5SLorenzo Bianconi xdpf = xdp_convert_buff_to_frame(&xdp); 12837a542beeSXuan Zhuo if (unlikely(!xdpf)) { 1284fab89bafSHeng Qi netdev_dbg(dev, "convert buff to frame failed for xdp\n"); 1285fab89bafSHeng Qi goto err_xdp_frags; 12867a542beeSXuan Zhuo } 1287ca9e83b4SJason Wang err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 1288fdc13979SLorenzo Bianconi if (unlikely(!err)) { 1289fdc13979SLorenzo Bianconi xdp_return_frame_rx_napi(xdpf); 1290fdc13979SLorenzo Bianconi } else if (unlikely(err < 0)) { 12910354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 1292fab89bafSHeng Qi goto err_xdp_frags; 129311b7d897SJesper Dangaard Brouer } 12942471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_TX; 129572979a6cSJohn Fastabend if (unlikely(xdp_page != page)) 12965d458a13SJason Wang put_page(page); 129756434a01SJohn Fastabend rcu_read_unlock(); 129856434a01SJohn Fastabend goto xdp_xmit; 12993cc81a9aSJason Wang case XDP_REDIRECT: 1300d46eeeafSJason Wang stats->xdp_redirects++; 13013cc81a9aSJason Wang err = xdp_do_redirect(dev, &xdp, xdp_prog); 1302fab89bafSHeng Qi if (err) 1303fab89bafSHeng Qi goto err_xdp_frags; 13042471c75eSJesper Dangaard Brouer *xdp_xmit |= VIRTIO_XDP_REDIR; 13053cc81a9aSJason Wang if (unlikely(xdp_page != page)) 13066890418bSJason Wang put_page(page); 13073cc81a9aSJason Wang rcu_read_unlock(); 13083cc81a9aSJason Wang goto xdp_xmit; 130956434a01SJohn Fastabend default: 1310c8064e5bSPaolo Abeni bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act); 1311df561f66SGustavo A. R. Silva fallthrough; 13120354e4d1SJohn Fastabend case XDP_ABORTED: 13130354e4d1SJohn Fastabend trace_xdp_exception(vi->dev, xdp_prog, act); 1314df561f66SGustavo A. R. Silva fallthrough; 13150354e4d1SJohn Fastabend case XDP_DROP: 1316fab89bafSHeng Qi goto err_xdp_frags; 1317f600b690SJohn Fastabend } 131822174f79SHeng Qi err_xdp_frags: 131922174f79SHeng Qi if (unlikely(xdp_page != page)) 132022174f79SHeng Qi __free_pages(xdp_page, 0); 132122174f79SHeng Qi 132222174f79SHeng Qi if (xdp_buff_has_frags(&xdp)) { 132322174f79SHeng Qi shinfo = xdp_get_shared_info_from_buff(&xdp); 132422174f79SHeng Qi for (i = 0; i < shinfo->nr_frags; i++) { 132522174f79SHeng Qi xdp_page = skb_frag_page(&shinfo->frags[i]); 132622174f79SHeng Qi put_page(xdp_page); 132722174f79SHeng Qi } 132822174f79SHeng Qi } 132922174f79SHeng Qi 133022174f79SHeng Qi goto err_xdp; 133156434a01SJohn Fastabend } 1332f600b690SJohn Fastabend rcu_read_unlock(); 1333f600b690SJohn Fastabend 13346213f07cSLi RongQing skip_xdp: 133518117a84SHeng Qi head_skb = page_to_skb(vi, rq, page, offset, len, truesize); 1336f600b690SJohn Fastabend curr_skb = head_skb; 13379ab86bbcSShirley Ma 13388fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 13398fc3b9e9SMichael S. Tsirkin goto err_skb; 13409ab86bbcSShirley Ma while (--num_buf) { 13418fc3b9e9SMichael S. Tsirkin int num_skb_frags; 13428fc3b9e9SMichael S. Tsirkin 1343680557cfSMichael S. Tsirkin buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 134403e9f8a0SYunjian Wang if (unlikely(!buf)) { 13458fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 1346fdd819b2SMichael S. Tsirkin dev->name, num_buf, 1347012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 1348012873d0SMichael S. Tsirkin hdr->num_buffers)); 13498fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 13508fc3b9e9SMichael S. Tsirkin goto err_buf; 13513f2c31d9SMark McLoughlin } 13528fc3b9e9SMichael S. Tsirkin 1353d46eeeafSJason Wang stats->bytes += len; 13548fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 135528b39bc7SJason Wang 135628b39bc7SJason Wang truesize = mergeable_ctx_to_truesize(ctx); 1357ef75cb51SHeng Qi headroom = mergeable_ctx_to_headroom(ctx); 1358ef75cb51SHeng Qi tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1359ef75cb51SHeng Qi room = SKB_DATA_ALIGN(headroom + tailroom); 1360ef75cb51SHeng Qi if (unlikely(len > truesize - room)) { 136156da5fd0SDan Carpenter pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1362ef75cb51SHeng Qi dev->name, len, (unsigned long)(truesize - room)); 1363680557cfSMichael S. Tsirkin dev->stats.rx_length_errors++; 1364680557cfSMichael S. Tsirkin goto err_skb; 1365680557cfSMichael S. Tsirkin } 13668fc3b9e9SMichael S. Tsirkin 13678fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 13682613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 13692613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 13708fc3b9e9SMichael S. Tsirkin 13718fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 13728fc3b9e9SMichael S. Tsirkin goto err_skb; 13732613af0eSMichael Dalton if (curr_skb == head_skb) 13742613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 13752613af0eSMichael Dalton else 13762613af0eSMichael Dalton curr_skb->next = nskb; 13772613af0eSMichael Dalton curr_skb = nskb; 13782613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 13792613af0eSMichael Dalton num_skb_frags = 0; 13802613af0eSMichael Dalton } 13812613af0eSMichael Dalton if (curr_skb != head_skb) { 13822613af0eSMichael Dalton head_skb->data_len += len; 13832613af0eSMichael Dalton head_skb->len += len; 1384fb51879dSMichael Dalton head_skb->truesize += truesize; 13852613af0eSMichael Dalton } 13868fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 1387ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 1388ba275241SJason Wang put_page(page); 1389ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 1390fb51879dSMichael Dalton len, truesize); 1391ba275241SJason Wang } else { 13922613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 1393fb51879dSMichael Dalton offset, len, truesize); 1394ba275241SJason Wang } 13958fc3b9e9SMichael S. Tsirkin } 13968fc3b9e9SMichael S. Tsirkin 13975377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 13988fc3b9e9SMichael S. Tsirkin return head_skb; 13998fc3b9e9SMichael S. Tsirkin 1400f600b690SJohn Fastabend err_xdp: 1401f600b690SJohn Fastabend rcu_read_unlock(); 1402d46eeeafSJason Wang stats->xdp_drops++; 14038fc3b9e9SMichael S. Tsirkin err_skb: 14048fc3b9e9SMichael S. Tsirkin put_page(page); 1405850e088dSJason Wang while (num_buf-- > 1) { 1406680557cfSMichael S. Tsirkin buf = virtqueue_get_buf(rq->vq, &len); 1407680557cfSMichael S. Tsirkin if (unlikely(!buf)) { 14088fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 14098fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 14108fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 14118fc3b9e9SMichael S. Tsirkin break; 14128fc3b9e9SMichael S. Tsirkin } 1413d46eeeafSJason Wang stats->bytes += len; 1414680557cfSMichael S. Tsirkin page = virt_to_head_page(buf); 14158fc3b9e9SMichael S. Tsirkin put_page(page); 14163f2c31d9SMark McLoughlin } 14178fc3b9e9SMichael S. Tsirkin err_buf: 1418d46eeeafSJason Wang stats->drops++; 14198fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 142056434a01SJohn Fastabend xdp_xmit: 14218fc3b9e9SMichael S. Tsirkin return NULL; 14229ab86bbcSShirley Ma } 14239ab86bbcSShirley Ma 142491f41f01SAndrew Melnychenko static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash, 142591f41f01SAndrew Melnychenko struct sk_buff *skb) 142691f41f01SAndrew Melnychenko { 142791f41f01SAndrew Melnychenko enum pkt_hash_types rss_hash_type; 142891f41f01SAndrew Melnychenko 142991f41f01SAndrew Melnychenko if (!hdr_hash || !skb) 143091f41f01SAndrew Melnychenko return; 143191f41f01SAndrew Melnychenko 143295bb6330SMichael S. Tsirkin switch (__le16_to_cpu(hdr_hash->hash_report)) { 143391f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_TCPv4: 143491f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_UDPv4: 143591f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_TCPv6: 143691f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_UDPv6: 143791f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_TCPv6_EX: 143891f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_UDPv6_EX: 143991f41f01SAndrew Melnychenko rss_hash_type = PKT_HASH_TYPE_L4; 144091f41f01SAndrew Melnychenko break; 144191f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_IPv4: 144291f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_IPv6: 144391f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_IPv6_EX: 144491f41f01SAndrew Melnychenko rss_hash_type = PKT_HASH_TYPE_L3; 144591f41f01SAndrew Melnychenko break; 144691f41f01SAndrew Melnychenko case VIRTIO_NET_HASH_REPORT_NONE: 144791f41f01SAndrew Melnychenko default: 144891f41f01SAndrew Melnychenko rss_hash_type = PKT_HASH_TYPE_NONE; 144991f41f01SAndrew Melnychenko } 145095bb6330SMichael S. Tsirkin skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type); 145191f41f01SAndrew Melnychenko } 145291f41f01SAndrew Melnychenko 14537d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 14542471c75eSJesper Dangaard Brouer void *buf, unsigned int len, void **ctx, 1455a0929a44SToshiaki Makita unsigned int *xdp_xmit, 1456d46eeeafSJason Wang struct virtnet_rq_stats *stats) 14579ab86bbcSShirley Ma { 1458e9d7417bSJason Wang struct net_device *dev = vi->dev; 14599ab86bbcSShirley Ma struct sk_buff *skb; 1460012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 14619ab86bbcSShirley Ma 1462bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 14639ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 14649ab86bbcSShirley Ma dev->stats.rx_length_errors++; 1465eb1d929fSParav Pandit virtnet_rq_free_unused_buf(rq->vq, buf); 14667d9d60fdSToshiaki Makita return; 14679ab86bbcSShirley Ma } 14689ab86bbcSShirley Ma 1469f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 14707d9d60fdSToshiaki Makita skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, 1471a0929a44SToshiaki Makita stats); 1472f121159dSMichael S. Tsirkin else if (vi->big_packets) 1473a0929a44SToshiaki Makita skb = receive_big(dev, vi, rq, buf, len, stats); 1474f121159dSMichael S. Tsirkin else 1475a0929a44SToshiaki Makita skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); 1476f121159dSMichael S. Tsirkin 14778fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 14787d9d60fdSToshiaki Makita return; 14793f2c31d9SMark McLoughlin 14809ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 148191f41f01SAndrew Melnychenko if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report) 148291f41f01SAndrew Melnychenko virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb); 14833fa2a1dfSstephen hemminger 1484e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 148510a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 1486296f96fcSRusty Russell 1487e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 1488e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 1489e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 1490e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 1491fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 1492296f96fcSRusty Russell goto frame_err; 1493296f96fcSRusty Russell } 1494296f96fcSRusty Russell 1495133bbb18SWillem de Bruijn skb_record_rx_queue(skb, vq2rxq(rq->vq)); 1496d1dc06dcSMike Rapoport skb->protocol = eth_type_trans(skb, dev); 1497d1dc06dcSMike Rapoport pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 1498d1dc06dcSMike Rapoport ntohs(skb->protocol), skb->len, skb->pkt_type); 1499d1dc06dcSMike Rapoport 15000fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 15017d9d60fdSToshiaki Makita return; 1502296f96fcSRusty Russell 1503296f96fcSRusty Russell frame_err: 1504296f96fcSRusty Russell dev->stats.rx_frame_errors++; 1505296f96fcSRusty Russell dev_kfree_skb(skb); 1506296f96fcSRusty Russell } 1507296f96fcSRusty Russell 1508192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the 1509192f68cfSJason Wang * same size, except for the headroom. For this reason we do 1510192f68cfSJason Wang * not need to use mergeable_len_to_ctx here - it is enough 1511192f68cfSJason Wang * to store the headroom as the context ignoring the truesize. 1512192f68cfSJason Wang */ 1513946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 1514946fa564SMichael S. Tsirkin gfp_t gfp) 1515296f96fcSRusty Russell { 1516f6b10209SJason Wang struct page_frag *alloc_frag = &rq->alloc_frag; 1517f6b10209SJason Wang char *buf; 15182de2f7f4SJohn Fastabend unsigned int xdp_headroom = virtnet_get_headroom(vi); 1519192f68cfSJason Wang void *ctx = (void *)(unsigned long)xdp_headroom; 1520f6b10209SJason Wang int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 15219ab86bbcSShirley Ma int err; 15223f2c31d9SMark McLoughlin 1523f6b10209SJason Wang len = SKB_DATA_ALIGN(len) + 1524f6b10209SJason Wang SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1525f6b10209SJason Wang if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 15269ab86bbcSShirley Ma return -ENOMEM; 1527296f96fcSRusty Russell 1528f6b10209SJason Wang buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1529f6b10209SJason Wang get_page(alloc_frag->page); 1530f6b10209SJason Wang alloc_frag->offset += len; 1531f6b10209SJason Wang sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 1532f6b10209SJason Wang vi->hdr_len + GOOD_PACKET_LEN); 1533192f68cfSJason Wang err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 15349ab86bbcSShirley Ma if (err < 0) 1535f6b10209SJason Wang put_page(virt_to_head_page(buf)); 15369ab86bbcSShirley Ma return err; 153797402b96SHerbert Xu } 153897402b96SHerbert Xu 1539012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 1540012873d0SMichael S. Tsirkin gfp_t gfp) 15419ab86bbcSShirley Ma { 15429ab86bbcSShirley Ma struct page *first, *list = NULL; 15439ab86bbcSShirley Ma char *p; 15449ab86bbcSShirley Ma int i, err, offset; 1545296f96fcSRusty Russell 15464959aebbSGavin Li sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2); 1547a5835440SRusty Russell 15484959aebbSGavin Li /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */ 15494959aebbSGavin Li for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) { 1550e9d7417bSJason Wang first = get_a_page(rq, gfp); 15519ab86bbcSShirley Ma if (!first) { 15529ab86bbcSShirley Ma if (list) 1553e9d7417bSJason Wang give_pages(rq, list); 15549ab86bbcSShirley Ma return -ENOMEM; 1555296f96fcSRusty Russell } 1556e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 15579ab86bbcSShirley Ma 15589ab86bbcSShirley Ma /* chain new page in list head to match sg */ 15599ab86bbcSShirley Ma first->private = (unsigned long)list; 15609ab86bbcSShirley Ma list = first; 15619ab86bbcSShirley Ma } 15629ab86bbcSShirley Ma 1563e9d7417bSJason Wang first = get_a_page(rq, gfp); 15649ab86bbcSShirley Ma if (!first) { 1565e9d7417bSJason Wang give_pages(rq, list); 15669ab86bbcSShirley Ma return -ENOMEM; 15679ab86bbcSShirley Ma } 15689ab86bbcSShirley Ma p = page_address(first); 15699ab86bbcSShirley Ma 1570e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 1571012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 1572012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 15739ab86bbcSShirley Ma 1574e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 15759ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 1576e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 15779ab86bbcSShirley Ma 15789ab86bbcSShirley Ma /* chain first in list head */ 15799ab86bbcSShirley Ma first->private = (unsigned long)list; 15804959aebbSGavin Li err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2, 1581aa989f5eSMichael S. Tsirkin first, gfp); 15829ab86bbcSShirley Ma if (err < 0) 1583e9d7417bSJason Wang give_pages(rq, first); 15849ab86bbcSShirley Ma 15859ab86bbcSShirley Ma return err; 15869ab86bbcSShirley Ma } 15879ab86bbcSShirley Ma 1588d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 15893cc81a9aSJason Wang struct ewma_pkt_len *avg_pkt_len, 15903cc81a9aSJason Wang unsigned int room) 15919ab86bbcSShirley Ma { 1592c1ddc42dSAndrew Melnychenko struct virtnet_info *vi = rq->vq->vdev->priv; 1593c1ddc42dSAndrew Melnychenko const size_t hdr_len = vi->hdr_len; 1594fbf28d78SMichael Dalton unsigned int len; 1595fbf28d78SMichael Dalton 15963cc81a9aSJason Wang if (room) 15973cc81a9aSJason Wang return PAGE_SIZE - room; 15983cc81a9aSJason Wang 15995377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 1600f0c3192cSMichael S. Tsirkin rq->min_buf_len, PAGE_SIZE - hdr_len); 16013cc81a9aSJason Wang 1602e377fcc8SMichael S. Tsirkin return ALIGN(len, L1_CACHE_BYTES); 1603fbf28d78SMichael Dalton } 1604fbf28d78SMichael Dalton 16052de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi, 16062de2f7f4SJohn Fastabend struct receive_queue *rq, gfp_t gfp) 1607fbf28d78SMichael Dalton { 1608fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 16092de2f7f4SJohn Fastabend unsigned int headroom = virtnet_get_headroom(vi); 16103cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 16113cc81a9aSJason Wang unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1612fb51879dSMichael Dalton char *buf; 1613680557cfSMichael S. Tsirkin void *ctx; 16149ab86bbcSShirley Ma int err; 1615fb51879dSMichael Dalton unsigned int len, hole; 16169ab86bbcSShirley Ma 16173cc81a9aSJason Wang /* Extra tailroom is needed to satisfy XDP's assumption. This 16183cc81a9aSJason Wang * means rx frags coalescing won't work, but consider we've 16193cc81a9aSJason Wang * disabled GSO for XDP, it won't be a big issue. 16203cc81a9aSJason Wang */ 16213cc81a9aSJason Wang len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 16223cc81a9aSJason Wang if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp))) 16239ab86bbcSShirley Ma return -ENOMEM; 1624ab7db917SMichael Dalton 1625fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 16262de2f7f4SJohn Fastabend buf += headroom; /* advance address leaving hole at front of pkt */ 1627fb51879dSMichael Dalton get_page(alloc_frag->page); 16283cc81a9aSJason Wang alloc_frag->offset += len + room; 1629fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 16303cc81a9aSJason Wang if (hole < len + room) { 1631ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 1632ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 16331daa8790SMichael S. Tsirkin * the current buffer. 1634484beac2SHeng Qi * XDP core assumes that frame_size of xdp_buff and the length 1635484beac2SHeng Qi * of the frag are PAGE_SIZE, so we disable the hole mechanism. 1636ab7db917SMichael Dalton */ 1637484beac2SHeng Qi if (!headroom) 1638fb51879dSMichael Dalton len += hole; 1639fb51879dSMichael Dalton alloc_frag->offset += hole; 1640fb51879dSMichael Dalton } 16419ab86bbcSShirley Ma 1642fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 1643ef75cb51SHeng Qi ctx = mergeable_len_to_ctx(len + room, headroom); 1644680557cfSMichael S. Tsirkin err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 16459ab86bbcSShirley Ma if (err < 0) 16462613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 16479ab86bbcSShirley Ma 16489ab86bbcSShirley Ma return err; 1649296f96fcSRusty Russell } 1650296f96fcSRusty Russell 1651b2baed69SRusty Russell /* 1652b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 1653b2baed69SRusty Russell * 1654b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 1655b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 1656b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 1657b2baed69SRusty Russell */ 1658946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 1659946fa564SMichael S. Tsirkin gfp_t gfp) 16603f2c31d9SMark McLoughlin { 16613f2c31d9SMark McLoughlin int err; 16621788f495SMichael S. Tsirkin bool oom; 16633f2c31d9SMark McLoughlin 16640aea51c3SAmit Shah do { 16659ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 16662de2f7f4SJohn Fastabend err = add_recvbuf_mergeable(vi, rq, gfp); 16679ab86bbcSShirley Ma else if (vi->big_packets) 1668012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 16699ab86bbcSShirley Ma else 1670946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 16713f2c31d9SMark McLoughlin 16721788f495SMichael S. Tsirkin oom = err == -ENOMEM; 16739ed4cb07SRusty Russell if (err) 16743f2c31d9SMark McLoughlin break; 1675b7dfde95SLinus Torvalds } while (rq->vq->num_free); 1676461f03dcSToshiaki Makita if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) { 167701c32598SMichael S. Tsirkin unsigned long flags; 167801c32598SMichael S. Tsirkin 167901c32598SMichael S. Tsirkin flags = u64_stats_update_begin_irqsave(&rq->stats.syncp); 1680d46eeeafSJason Wang rq->stats.kicks++; 168101c32598SMichael S. Tsirkin u64_stats_update_end_irqrestore(&rq->stats.syncp, flags); 1682461f03dcSToshiaki Makita } 1683461f03dcSToshiaki Makita 16843161e453SRusty Russell return !oom; 16853f2c31d9SMark McLoughlin } 16863f2c31d9SMark McLoughlin 168718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 1688296f96fcSRusty Russell { 1689296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 1690986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1691e9d7417bSJason Wang 1692e4e8452aSWillem de Bruijn virtqueue_napi_schedule(&rq->napi, rvq); 1693296f96fcSRusty Russell } 1694296f96fcSRusty Russell 1695e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 16963e9d08ecSBruce Rogers { 1697e4e8452aSWillem de Bruijn napi_enable(napi); 16983e9d08ecSBruce Rogers 16993e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 1700e4e8452aSWillem de Bruijn * won't get another interrupt, so process any outstanding packets now. 1701e4e8452aSWillem de Bruijn * Call local_bh_enable after to trigger softIRQ processing. 1702e4e8452aSWillem de Bruijn */ 1703ec13ee80SMichael S. Tsirkin local_bh_disable(); 1704e4e8452aSWillem de Bruijn virtqueue_napi_schedule(napi, vq); 1705ec13ee80SMichael S. Tsirkin local_bh_enable(); 17063e9d08ecSBruce Rogers } 17073e9d08ecSBruce Rogers 1708b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1709b92f1e67SWillem de Bruijn struct virtqueue *vq, 1710b92f1e67SWillem de Bruijn struct napi_struct *napi) 1711b92f1e67SWillem de Bruijn { 1712b92f1e67SWillem de Bruijn if (!napi->weight) 1713b92f1e67SWillem de Bruijn return; 1714b92f1e67SWillem de Bruijn 1715b92f1e67SWillem de Bruijn /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1716b92f1e67SWillem de Bruijn * enable the feature if this is likely affine with the transmit path. 1717b92f1e67SWillem de Bruijn */ 1718b92f1e67SWillem de Bruijn if (!vi->affinity_hint_set) { 1719b92f1e67SWillem de Bruijn napi->weight = 0; 1720b92f1e67SWillem de Bruijn return; 1721b92f1e67SWillem de Bruijn } 1722b92f1e67SWillem de Bruijn 1723b92f1e67SWillem de Bruijn return virtnet_napi_enable(vq, napi); 1724b92f1e67SWillem de Bruijn } 1725b92f1e67SWillem de Bruijn 172678a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi) 172778a57b48SWillem de Bruijn { 172878a57b48SWillem de Bruijn if (napi->weight) 172978a57b48SWillem de Bruijn napi_disable(napi); 173078a57b48SWillem de Bruijn } 173178a57b48SWillem de Bruijn 17323161e453SRusty Russell static void refill_work(struct work_struct *work) 17333161e453SRusty Russell { 1734e9d7417bSJason Wang struct virtnet_info *vi = 1735e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 17363161e453SRusty Russell bool still_empty; 1737986a4f4dSJason Wang int i; 17383161e453SRusty Russell 173955257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1740986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 1741986a4f4dSJason Wang 1742986a4f4dSJason Wang napi_disable(&rq->napi); 1743946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1744e4e8452aSWillem de Bruijn virtnet_napi_enable(rq->vq, &rq->napi); 17453161e453SRusty Russell 17463161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 1747986a4f4dSJason Wang * we will *never* try to fill again. 1748986a4f4dSJason Wang */ 17493161e453SRusty Russell if (still_empty) 17503b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 17513161e453SRusty Russell } 1752986a4f4dSJason Wang } 17533161e453SRusty Russell 17542471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget, 17552471c75eSJesper Dangaard Brouer unsigned int *xdp_xmit) 1756296f96fcSRusty Russell { 1757e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 1758d46eeeafSJason Wang struct virtnet_rq_stats stats = {}; 1759a0929a44SToshiaki Makita unsigned int len; 17609ab86bbcSShirley Ma void *buf; 1761a0929a44SToshiaki Makita int i; 1762296f96fcSRusty Russell 1763192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 1764680557cfSMichael S. Tsirkin void *ctx; 1765680557cfSMichael S. Tsirkin 1766d46eeeafSJason Wang while (stats.packets < budget && 1767680557cfSMichael S. Tsirkin (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1768a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); 1769d46eeeafSJason Wang stats.packets++; 1770680557cfSMichael S. Tsirkin } 1771680557cfSMichael S. Tsirkin } else { 1772d46eeeafSJason Wang while (stats.packets < budget && 1773e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1774a0929a44SToshiaki Makita receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); 1775d46eeeafSJason Wang stats.packets++; 1776296f96fcSRusty Russell } 1777680557cfSMichael S. Tsirkin } 1778296f96fcSRusty Russell 1779718be6baS? jiang if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { 17805a159128SJason Wang if (!try_fill_recv(vi, rq, GFP_ATOMIC)) { 17815a159128SJason Wang spin_lock(&vi->refill_lock); 17825a159128SJason Wang if (vi->refill_enabled) 17833b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 17845a159128SJason Wang spin_unlock(&vi->refill_lock); 17855a159128SJason Wang } 17863161e453SRusty Russell } 1787296f96fcSRusty Russell 1788d7dfc5cfSToshiaki Makita u64_stats_update_begin(&rq->stats.syncp); 1789a0929a44SToshiaki Makita for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) { 1790a0929a44SToshiaki Makita size_t offset = virtnet_rq_stats_desc[i].offset; 1791a0929a44SToshiaki Makita u64 *item; 1792a0929a44SToshiaki Makita 1793d46eeeafSJason Wang item = (u64 *)((u8 *)&rq->stats + offset); 1794d46eeeafSJason Wang *item += *(u64 *)((u8 *)&stats + offset); 1795a0929a44SToshiaki Makita } 1796d7dfc5cfSToshiaki Makita u64_stats_update_end(&rq->stats.syncp); 179761845d20SJason Wang 1798d46eeeafSJason Wang return stats.packets; 17992ffa7598SJason Wang } 18002ffa7598SJason Wang 18017b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq) 18027b0411efSWillem de Bruijn { 18037b0411efSWillem de Bruijn struct virtnet_info *vi = rq->vq->vdev->priv; 18047b0411efSWillem de Bruijn unsigned int index = vq2rxq(rq->vq); 18057b0411efSWillem de Bruijn struct send_queue *sq = &vi->sq[index]; 18067b0411efSWillem de Bruijn struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 18077b0411efSWillem de Bruijn 1808534da5e8SToshiaki Makita if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) 18097b0411efSWillem de Bruijn return; 18107b0411efSWillem de Bruijn 18117b0411efSWillem de Bruijn if (__netif_tx_trylock(txq)) { 1812ebcce492SXuan Zhuo if (sq->reset) { 1813ebcce492SXuan Zhuo __netif_tx_unlock(txq); 1814ebcce492SXuan Zhuo return; 1815ebcce492SXuan Zhuo } 1816ebcce492SXuan Zhuo 1817a7766ef1SMichael S. Tsirkin do { 1818a7766ef1SMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 1819df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, true); 1820a7766ef1SMichael S. Tsirkin } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq))); 18217b0411efSWillem de Bruijn 18227b0411efSWillem de Bruijn if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 18237b0411efSWillem de Bruijn netif_tx_wake_queue(txq); 182422bc63c5SMichael S. Tsirkin 182522bc63c5SMichael S. Tsirkin __netif_tx_unlock(txq); 182622bc63c5SMichael S. Tsirkin } 18277b0411efSWillem de Bruijn } 18287b0411efSWillem de Bruijn 18292ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 18302ffa7598SJason Wang { 18312ffa7598SJason Wang struct receive_queue *rq = 18322ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 18339267c430SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 18349267c430SJason Wang struct send_queue *sq; 18352a43565cSToshiaki Makita unsigned int received; 18362471c75eSJesper Dangaard Brouer unsigned int xdp_xmit = 0; 18372ffa7598SJason Wang 18387b0411efSWillem de Bruijn virtnet_poll_cleantx(rq); 18397b0411efSWillem de Bruijn 1840186b3c99SJason Wang received = virtnet_receive(rq, budget, &xdp_xmit); 18412ffa7598SJason Wang 1842ad7e615fSMagnus Karlsson if (xdp_xmit & VIRTIO_XDP_REDIR) 1843ad7e615fSMagnus Karlsson xdp_do_flush(); 1844ad7e615fSMagnus Karlsson 18458329d98eSRusty Russell /* Out of packets? */ 1846e4e8452aSWillem de Bruijn if (received < budget) 1847e4e8452aSWillem de Bruijn virtqueue_napi_complete(napi, rq->vq, received); 1848296f96fcSRusty Russell 18492471c75eSJesper Dangaard Brouer if (xdp_xmit & VIRTIO_XDP_TX) { 185097c2c69eSXuan Zhuo sq = virtnet_xdp_get_sq(vi); 1851461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1852461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 1853461f03dcSToshiaki Makita sq->stats.kicks++; 1854461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 1855461f03dcSToshiaki Makita } 185697c2c69eSXuan Zhuo virtnet_xdp_put_sq(vi, sq); 18579267c430SJason Wang } 1858186b3c99SJason Wang 1859296f96fcSRusty Russell return received; 1860296f96fcSRusty Russell } 1861296f96fcSRusty Russell 1862986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 1863986a4f4dSJason Wang { 1864986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1865754b8a21SJesper Dangaard Brouer int i, err; 1866986a4f4dSJason Wang 18675a159128SJason Wang enable_delayed_refill(vi); 18685a159128SJason Wang 1869e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1870e4166625SJason Wang if (i < vi->curr_queue_pairs) 1871986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 1872946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1873986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 1874754b8a21SJesper Dangaard Brouer 1875b02e5a0eSBjörn Töpel err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); 1876754b8a21SJesper Dangaard Brouer if (err < 0) 1877754b8a21SJesper Dangaard Brouer return err; 1878754b8a21SJesper Dangaard Brouer 18798d5d8852SJesper Dangaard Brouer err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq, 18808d5d8852SJesper Dangaard Brouer MEM_TYPE_PAGE_SHARED, NULL); 18818d5d8852SJesper Dangaard Brouer if (err < 0) { 18828d5d8852SJesper Dangaard Brouer xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 18838d5d8852SJesper Dangaard Brouer return err; 18848d5d8852SJesper Dangaard Brouer } 18858d5d8852SJesper Dangaard Brouer 1886e4e8452aSWillem de Bruijn virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1887b92f1e67SWillem de Bruijn virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1888986a4f4dSJason Wang } 1889986a4f4dSJason Wang 1890986a4f4dSJason Wang return 0; 1891986a4f4dSJason Wang } 1892986a4f4dSJason Wang 1893b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1894b92f1e67SWillem de Bruijn { 1895b92f1e67SWillem de Bruijn struct send_queue *sq = container_of(napi, struct send_queue, napi); 1896b92f1e67SWillem de Bruijn struct virtnet_info *vi = sq->vq->vdev->priv; 1897534da5e8SToshiaki Makita unsigned int index = vq2txq(sq->vq); 1898534da5e8SToshiaki Makita struct netdev_queue *txq; 18995a2f966dSMichael S. Tsirkin int opaque; 19005a2f966dSMichael S. Tsirkin bool done; 1901b92f1e67SWillem de Bruijn 1902534da5e8SToshiaki Makita if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { 1903534da5e8SToshiaki Makita /* We don't need to enable cb for XDP */ 1904534da5e8SToshiaki Makita napi_complete_done(napi, 0); 1905534da5e8SToshiaki Makita return 0; 1906534da5e8SToshiaki Makita } 1907534da5e8SToshiaki Makita 1908534da5e8SToshiaki Makita txq = netdev_get_tx_queue(vi->dev, index); 1909b92f1e67SWillem de Bruijn __netif_tx_lock(txq, raw_smp_processor_id()); 19105a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 1911df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, true); 19125a2f966dSMichael S. Tsirkin 191322bc63c5SMichael S. Tsirkin if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 191422bc63c5SMichael S. Tsirkin netif_tx_wake_queue(txq); 191522bc63c5SMichael S. Tsirkin 19165a2f966dSMichael S. Tsirkin opaque = virtqueue_enable_cb_prepare(sq->vq); 19175a2f966dSMichael S. Tsirkin 19185a2f966dSMichael S. Tsirkin done = napi_complete_done(napi, 0); 19195a2f966dSMichael S. Tsirkin 19205a2f966dSMichael S. Tsirkin if (!done) 19215a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 19225a2f966dSMichael S. Tsirkin 1923b92f1e67SWillem de Bruijn __netif_tx_unlock(txq); 1924b92f1e67SWillem de Bruijn 19255a2f966dSMichael S. Tsirkin if (done) { 19265a2f966dSMichael S. Tsirkin if (unlikely(virtqueue_poll(sq->vq, opaque))) { 19275a2f966dSMichael S. Tsirkin if (napi_schedule_prep(napi)) { 19285a2f966dSMichael S. Tsirkin __netif_tx_lock(txq, raw_smp_processor_id()); 19295a2f966dSMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 19305a2f966dSMichael S. Tsirkin __netif_tx_unlock(txq); 19315a2f966dSMichael S. Tsirkin __napi_schedule(napi); 19325a2f966dSMichael S. Tsirkin } 19335a2f966dSMichael S. Tsirkin } 19345a2f966dSMichael S. Tsirkin } 1935b92f1e67SWillem de Bruijn 1936b92f1e67SWillem de Bruijn return 0; 1937b92f1e67SWillem de Bruijn } 1938b92f1e67SWillem de Bruijn 1939e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1940296f96fcSRusty Russell { 1941012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 1942296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1943e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 1944e2fcad58SJason A. Donenfeld int num_sg; 1945012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 1946e7428e95SMichael S. Tsirkin bool can_push; 1947296f96fcSRusty Russell 1948e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1949e7428e95SMichael S. Tsirkin 1950e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 1951e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1952e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1953e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 1954e7428e95SMichael S. Tsirkin * csum_start offset below. */ 1955e7428e95SMichael S. Tsirkin if (can_push) 1956012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1957e7428e95SMichael S. Tsirkin else 1958e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 1959296f96fcSRusty Russell 1960e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 1961fd3a8862SWillem de Bruijn virtio_is_little_endian(vi->vdev), false, 1962fd3a8862SWillem de Bruijn 0)) 196385eb1389SXianting Tian return -EPROTO; 1964296f96fcSRusty Russell 1965e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 1966012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 19673f2c31d9SMark McLoughlin 1968547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1969e7428e95SMichael S. Tsirkin if (can_push) { 1970e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 1971e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1972e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1973e2fcad58SJason A. Donenfeld return num_sg; 1974e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 1975e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 1976e7428e95SMichael S. Tsirkin } else { 1977e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 1978e2fcad58SJason A. Donenfeld num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1979e2fcad58SJason A. Donenfeld if (unlikely(num_sg < 0)) 1980e2fcad58SJason A. Donenfeld return num_sg; 1981e2fcad58SJason A. Donenfeld num_sg++; 1982e7428e95SMichael S. Tsirkin } 19839dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 198411a3a154SRusty Russell } 198511a3a154SRusty Russell 1986424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 198799ffc696SRusty Russell { 198899ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1989986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 1990986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 19919ed4cb07SRusty Russell int err; 19924b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 19936b16f9eeSFlorian Westphal bool kick = !netdev_xmit_more(); 1994b92f1e67SWillem de Bruijn bool use_napi = sq->napi.weight; 19952cb9c6baSRusty Russell 19962cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 1997a7766ef1SMichael S. Tsirkin do { 1998a7766ef1SMichael S. Tsirkin if (use_napi) 1999a7766ef1SMichael S. Tsirkin virtqueue_disable_cb(sq->vq); 2000a7766ef1SMichael S. Tsirkin 2001df133f3fSMichael S. Tsirkin free_old_xmit_skbs(sq, false); 200299ffc696SRusty Russell 2003a7766ef1SMichael S. Tsirkin } while (use_napi && kick && 2004a7766ef1SMichael S. Tsirkin unlikely(!virtqueue_enable_cb_delayed(sq->vq))); 2005bdb12e0dSWillem de Bruijn 2006074c3582SJacob Keller /* timestamp packet in software */ 2007074c3582SJacob Keller skb_tx_timestamp(skb); 2008074c3582SJacob Keller 200903f191baSMichael S. Tsirkin /* Try to transmit */ 2010b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 201199ffc696SRusty Russell 20129ed4cb07SRusty Russell /* This should not happen! */ 2013681daee2SJason Wang if (unlikely(err)) { 201458eba97dSRusty Russell dev->stats.tx_fifo_errors++; 20152e57b79cSRick Jones if (net_ratelimit()) 201658eba97dSRusty Russell dev_warn(&dev->dev, 20177934b481SYuval Shaia "Unexpected TXQ (%d) queue failure: %d\n", 20187934b481SYuval Shaia qnum, err); 201958eba97dSRusty Russell dev->stats.tx_dropped++; 202085e94525SEric W. Biederman dev_kfree_skb_any(skb); 202158eba97dSRusty Russell return NETDEV_TX_OK; 2022296f96fcSRusty Russell } 202303f191baSMichael S. Tsirkin 202448925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 2025b92f1e67SWillem de Bruijn if (!use_napi) { 202648925e37SRusty Russell skb_orphan(skb); 2027895b5c9fSFlorian Westphal nf_reset_ct(skb); 2028b92f1e67SWillem de Bruijn } 202948925e37SRusty Russell 2030b8ef4809SXuan Zhuo check_sq_full_and_disable(vi, dev, sq); 203148925e37SRusty Russell 2032461f03dcSToshiaki Makita if (kick || netif_xmit_stopped(txq)) { 2033461f03dcSToshiaki Makita if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 2034461f03dcSToshiaki Makita u64_stats_update_begin(&sq->stats.syncp); 2035461f03dcSToshiaki Makita sq->stats.kicks++; 2036461f03dcSToshiaki Makita u64_stats_update_end(&sq->stats.syncp); 2037461f03dcSToshiaki Makita } 2038461f03dcSToshiaki Makita } 20390b725a2cSDavid S. Miller 20400b725a2cSDavid S. Miller return NETDEV_TX_OK; 2041c223a078SDavid S. Miller } 2042c223a078SDavid S. Miller 20436a4763e2SXuan Zhuo static int virtnet_rx_resize(struct virtnet_info *vi, 20446a4763e2SXuan Zhuo struct receive_queue *rq, u32 ring_num) 20456a4763e2SXuan Zhuo { 20466a4763e2SXuan Zhuo bool running = netif_running(vi->dev); 20476a4763e2SXuan Zhuo int err, qindex; 20486a4763e2SXuan Zhuo 20496a4763e2SXuan Zhuo qindex = rq - vi->rq; 20506a4763e2SXuan Zhuo 20516a4763e2SXuan Zhuo if (running) 20526a4763e2SXuan Zhuo napi_disable(&rq->napi); 20536a4763e2SXuan Zhuo 20546a4763e2SXuan Zhuo err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_free_unused_buf); 20556a4763e2SXuan Zhuo if (err) 20566a4763e2SXuan Zhuo netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err); 20576a4763e2SXuan Zhuo 20586a4763e2SXuan Zhuo if (!try_fill_recv(vi, rq, GFP_KERNEL)) 20596a4763e2SXuan Zhuo schedule_delayed_work(&vi->refill, 0); 20606a4763e2SXuan Zhuo 20616a4763e2SXuan Zhuo if (running) 20626a4763e2SXuan Zhuo virtnet_napi_enable(rq->vq, &rq->napi); 20636a4763e2SXuan Zhuo return err; 20646a4763e2SXuan Zhuo } 20656a4763e2SXuan Zhuo 2066ebcce492SXuan Zhuo static int virtnet_tx_resize(struct virtnet_info *vi, 2067ebcce492SXuan Zhuo struct send_queue *sq, u32 ring_num) 2068ebcce492SXuan Zhuo { 2069ebcce492SXuan Zhuo bool running = netif_running(vi->dev); 2070ebcce492SXuan Zhuo struct netdev_queue *txq; 2071ebcce492SXuan Zhuo int err, qindex; 2072ebcce492SXuan Zhuo 2073ebcce492SXuan Zhuo qindex = sq - vi->sq; 2074ebcce492SXuan Zhuo 2075ebcce492SXuan Zhuo if (running) 2076ebcce492SXuan Zhuo virtnet_napi_tx_disable(&sq->napi); 2077ebcce492SXuan Zhuo 2078ebcce492SXuan Zhuo txq = netdev_get_tx_queue(vi->dev, qindex); 2079ebcce492SXuan Zhuo 2080ebcce492SXuan Zhuo /* 1. wait all ximt complete 2081ebcce492SXuan Zhuo * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue() 2082ebcce492SXuan Zhuo */ 2083ebcce492SXuan Zhuo __netif_tx_lock_bh(txq); 2084ebcce492SXuan Zhuo 2085ebcce492SXuan Zhuo /* Prevent rx poll from accessing sq. */ 2086ebcce492SXuan Zhuo sq->reset = true; 2087ebcce492SXuan Zhuo 2088ebcce492SXuan Zhuo /* Prevent the upper layer from trying to send packets. */ 2089ebcce492SXuan Zhuo netif_stop_subqueue(vi->dev, qindex); 2090ebcce492SXuan Zhuo 2091ebcce492SXuan Zhuo __netif_tx_unlock_bh(txq); 2092ebcce492SXuan Zhuo 2093ebcce492SXuan Zhuo err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf); 2094ebcce492SXuan Zhuo if (err) 2095ebcce492SXuan Zhuo netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err); 2096ebcce492SXuan Zhuo 2097ebcce492SXuan Zhuo __netif_tx_lock_bh(txq); 2098ebcce492SXuan Zhuo sq->reset = false; 2099ebcce492SXuan Zhuo netif_tx_wake_queue(txq); 2100ebcce492SXuan Zhuo __netif_tx_unlock_bh(txq); 2101ebcce492SXuan Zhuo 2102ebcce492SXuan Zhuo if (running) 2103ebcce492SXuan Zhuo virtnet_napi_tx_enable(vi, sq->vq, &sq->napi); 2104ebcce492SXuan Zhuo return err; 2105ebcce492SXuan Zhuo } 2106ebcce492SXuan Zhuo 210740cbfc37SAmos Kong /* 210840cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 210940cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 2110788a8b6dSstephen hemminger * never fail unless improperly formatted. 211140cbfc37SAmos Kong */ 211240cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 2113d24bae32Sstephen hemminger struct scatterlist *out) 211440cbfc37SAmos Kong { 2115f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 2116d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 2117222722bcSYunjian Wang int ret; 211840cbfc37SAmos Kong 211940cbfc37SAmos Kong /* Caller should know better */ 2120f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 212140cbfc37SAmos Kong 212212e57169SMichael S. Tsirkin vi->ctrl->status = ~0; 212312e57169SMichael S. Tsirkin vi->ctrl->hdr.class = class; 212412e57169SMichael S. Tsirkin vi->ctrl->hdr.cmd = cmd; 2125f7bc9594SRusty Russell /* Add header */ 212612e57169SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 2127f7bc9594SRusty Russell sgs[out_num++] = &hdr; 212840cbfc37SAmos Kong 2129f7bc9594SRusty Russell if (out) 2130f7bc9594SRusty Russell sgs[out_num++] = out; 213140cbfc37SAmos Kong 2132f7bc9594SRusty Russell /* Add return status. */ 213312e57169SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 2134d24bae32Sstephen hemminger sgs[out_num] = &stat; 213540cbfc37SAmos Kong 2136d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 2137222722bcSYunjian Wang ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 2138222722bcSYunjian Wang if (ret < 0) { 2139222722bcSYunjian Wang dev_warn(&vi->vdev->dev, 2140222722bcSYunjian Wang "Failed to add sgs for command vq: %d\n.", ret); 2141222722bcSYunjian Wang return false; 2142222722bcSYunjian Wang } 214340cbfc37SAmos Kong 214467975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 214512e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 214640cbfc37SAmos Kong 214740cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 214840cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 214940cbfc37SAmos Kong */ 2150047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 2151047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 215240cbfc37SAmos Kong cpu_relax(); 215340cbfc37SAmos Kong 215412e57169SMichael S. Tsirkin return vi->ctrl->status == VIRTIO_NET_OK; 215540cbfc37SAmos Kong } 215640cbfc37SAmos Kong 21579c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 21589c46f6d4SAlex Williamson { 21599c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 21609c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 2161f2f2c8b4SJiri Pirko int ret; 2162e37e2ff3SAndy Lutomirski struct sockaddr *addr; 21637e58d5aeSAmos Kong struct scatterlist sg; 21649c46f6d4SAlex Williamson 2165ba5e4426SSridhar Samudrala if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 2166ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 2167ba5e4426SSridhar Samudrala 2168801822d1SShyam Saini addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 2169e37e2ff3SAndy Lutomirski if (!addr) 2170e37e2ff3SAndy Lutomirski return -ENOMEM; 2171e37e2ff3SAndy Lutomirski 2172e37e2ff3SAndy Lutomirski ret = eth_prepare_mac_addr_change(dev, addr); 2173f2f2c8b4SJiri Pirko if (ret) 2174e37e2ff3SAndy Lutomirski goto out; 21759c46f6d4SAlex Williamson 21767e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 21777e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 21787e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 2179d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 21807e58d5aeSAmos Kong dev_warn(&vdev->dev, 21817e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 2182e37e2ff3SAndy Lutomirski ret = -EINVAL; 2183e37e2ff3SAndy Lutomirski goto out; 21847e58d5aeSAmos Kong } 21857e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 21867e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 2187855e0c52SRusty Russell unsigned int i; 2188855e0c52SRusty Russell 2189855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 2190855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 2191855e0c52SRusty Russell virtio_cwrite8(vdev, 2192855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 2193855e0c52SRusty Russell i, addr->sa_data[i]); 21947e58d5aeSAmos Kong } 21957e58d5aeSAmos Kong 21967e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 2197e37e2ff3SAndy Lutomirski ret = 0; 21989c46f6d4SAlex Williamson 2199e37e2ff3SAndy Lutomirski out: 2200e37e2ff3SAndy Lutomirski kfree(addr); 2201e37e2ff3SAndy Lutomirski return ret; 22029c46f6d4SAlex Williamson } 22039c46f6d4SAlex Williamson 2204bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev, 22053fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 22063fa2a1dfSstephen hemminger { 22073fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 22083fa2a1dfSstephen hemminger unsigned int start; 2209d7dfc5cfSToshiaki Makita int i; 22103fa2a1dfSstephen hemminger 2211d7dfc5cfSToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 2212a520794bSTony Lu u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops; 2213d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 2214d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 22153fa2a1dfSstephen hemminger 22163fa2a1dfSstephen hemminger do { 2217068c38adSThomas Gleixner start = u64_stats_fetch_begin(&sq->stats.syncp); 2218d7dfc5cfSToshiaki Makita tpackets = sq->stats.packets; 2219d7dfc5cfSToshiaki Makita tbytes = sq->stats.bytes; 2220a520794bSTony Lu terrors = sq->stats.tx_timeouts; 2221068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&sq->stats.syncp, start)); 222283a27052SEric Dumazet 222383a27052SEric Dumazet do { 2224068c38adSThomas Gleixner start = u64_stats_fetch_begin(&rq->stats.syncp); 2225d46eeeafSJason Wang rpackets = rq->stats.packets; 2226d46eeeafSJason Wang rbytes = rq->stats.bytes; 2227d46eeeafSJason Wang rdrops = rq->stats.drops; 2228068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&rq->stats.syncp, start)); 22293fa2a1dfSstephen hemminger 22303fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 22313fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 22323fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 22333fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 22342c4a2f7dSToshiaki Makita tot->rx_dropped += rdrops; 2235a520794bSTony Lu tot->tx_errors += terrors; 22363fa2a1dfSstephen hemminger } 22373fa2a1dfSstephen hemminger 22383fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 2239021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 22403fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 22413fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 22423fa2a1dfSstephen hemminger } 22433fa2a1dfSstephen hemminger 2244586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 2245586d17c5SJason Wang { 2246586d17c5SJason Wang rtnl_lock(); 2247586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 2248d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 2249586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 2250586d17c5SJason Wang rtnl_unlock(); 2251586d17c5SJason Wang } 2252586d17c5SJason Wang 225347315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 2254986a4f4dSJason Wang { 2255986a4f4dSJason Wang struct scatterlist sg; 2256986a4f4dSJason Wang struct net_device *dev = vi->dev; 2257986a4f4dSJason Wang 2258986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 2259986a4f4dSJason Wang return 0; 2260986a4f4dSJason Wang 226112e57169SMichael S. Tsirkin vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 226212e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); 2263986a4f4dSJason Wang 2264986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 2265d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 2266986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 2267986a4f4dSJason Wang queue_pairs); 2268986a4f4dSJason Wang return -EINVAL; 226955257d72SSasha Levin } else { 2270986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 227135ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 227235ed159bSJason Wang if (dev->flags & IFF_UP) 22739b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 227455257d72SSasha Levin } 2275986a4f4dSJason Wang 2276986a4f4dSJason Wang return 0; 2277986a4f4dSJason Wang } 2278986a4f4dSJason Wang 227947315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 228047315329SJohn Fastabend { 228147315329SJohn Fastabend int err; 228247315329SJohn Fastabend 228347315329SJohn Fastabend rtnl_lock(); 228447315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 228547315329SJohn Fastabend rtnl_unlock(); 228647315329SJohn Fastabend return err; 228747315329SJohn Fastabend } 228847315329SJohn Fastabend 2289296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 2290296f96fcSRusty Russell { 2291296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 2292986a4f4dSJason Wang int i; 2293296f96fcSRusty Russell 22945a159128SJason Wang /* Make sure NAPI doesn't schedule refill work */ 22955a159128SJason Wang disable_delayed_refill(vi); 2296b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 2297b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 2298986a4f4dSJason Wang 2299b92f1e67SWillem de Bruijn for (i = 0; i < vi->max_queue_pairs; i++) { 230027369c9cSParav Pandit virtnet_napi_tx_disable(&vi->sq[i].napi); 2301986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 230263b11404SParav Pandit xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 2303b92f1e67SWillem de Bruijn } 2304296f96fcSRusty Russell 2305296f96fcSRusty Russell return 0; 2306296f96fcSRusty Russell } 2307296f96fcSRusty Russell 23082af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 23092af7698eSAlex Williamson { 23102af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 2311f565a7c2SAlex Williamson struct scatterlist sg[2]; 2312f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 2313ccffad25SJiri Pirko struct netdev_hw_addr *ha; 231432e7bfc4SJiri Pirko int uc_count; 23154cd24eafSJiri Pirko int mc_count; 2316f565a7c2SAlex Williamson void *buf; 2317f565a7c2SAlex Williamson int i; 23182af7698eSAlex Williamson 2319788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 23202af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 23212af7698eSAlex Williamson return; 23222af7698eSAlex Williamson 232312e57169SMichael S. Tsirkin vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); 232412e57169SMichael S. Tsirkin vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 23252af7698eSAlex Williamson 232612e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); 23272af7698eSAlex Williamson 23282af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 2329d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 23302af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 233112e57169SMichael S. Tsirkin vi->ctrl->promisc ? "en" : "dis"); 23322af7698eSAlex Williamson 233312e57169SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); 23342af7698eSAlex Williamson 23352af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 2336d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 23372af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 233812e57169SMichael S. Tsirkin vi->ctrl->allmulti ? "en" : "dis"); 2339f565a7c2SAlex Williamson 234032e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 23414cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 2342f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 23434cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 2344f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 23454cd24eafSJiri Pirko mac_data = buf; 2346e68ed8f0SJoe Perches if (!buf) 2347f565a7c2SAlex Williamson return; 2348f565a7c2SAlex Williamson 234923e258e1SAlex Williamson sg_init_table(sg, 2); 235023e258e1SAlex Williamson 2351f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 2352fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 2353ccffad25SJiri Pirko i = 0; 235432e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 2355ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2356f565a7c2SAlex Williamson 2357f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 235832e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 2359f565a7c2SAlex Williamson 2360f565a7c2SAlex Williamson /* multicast list and count fill the end */ 236132e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 2362f565a7c2SAlex Williamson 2363fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 2364567ec874SJiri Pirko i = 0; 236522bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 236622bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2367f565a7c2SAlex Williamson 2368f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 23694cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 2370f565a7c2SAlex Williamson 2371f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 2372d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 237399e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 2374f565a7c2SAlex Williamson 2375f565a7c2SAlex Williamson kfree(buf); 23762af7698eSAlex Williamson } 23772af7698eSAlex Williamson 237880d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 237980d5c368SPatrick McHardy __be16 proto, u16 vid) 23800bde9569SAlex Williamson { 23810bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 23820bde9569SAlex Williamson struct scatterlist sg; 23830bde9569SAlex Williamson 2384d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 238512e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 23860bde9569SAlex Williamson 23870bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 2388d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 23890bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 23908e586137SJiri Pirko return 0; 23910bde9569SAlex Williamson } 23920bde9569SAlex Williamson 239380d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 239480d5c368SPatrick McHardy __be16 proto, u16 vid) 23950bde9569SAlex Williamson { 23960bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 23970bde9569SAlex Williamson struct scatterlist sg; 23980bde9569SAlex Williamson 2399d7fad4c8SMichael S. Tsirkin vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 240012e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 24010bde9569SAlex Williamson 24020bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 2403d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 24040bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 24058e586137SJiri Pirko return 0; 24060bde9569SAlex Williamson } 24070bde9569SAlex Williamson 2408310974faSPeter Xu static void virtnet_clean_affinity(struct virtnet_info *vi) 2409986a4f4dSJason Wang { 2410986a4f4dSJason Wang int i; 24118898c21cSWanlong Gao 24128898c21cSWanlong Gao if (vi->affinity_hint_set) { 24138898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 241419e226e8SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, NULL); 241519e226e8SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, NULL); 24168898c21cSWanlong Gao } 24178898c21cSWanlong Gao 24188898c21cSWanlong Gao vi->affinity_hint_set = false; 24198898c21cSWanlong Gao } 24208898c21cSWanlong Gao } 24218898c21cSWanlong Gao 24228898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 2423986a4f4dSJason Wang { 24242ca653d6SCaleb Raitto cpumask_var_t mask; 24252ca653d6SCaleb Raitto int stragglers; 24262ca653d6SCaleb Raitto int group_size; 24272ca653d6SCaleb Raitto int i, j, cpu; 24282ca653d6SCaleb Raitto int num_cpu; 24292ca653d6SCaleb Raitto int stride; 2430986a4f4dSJason Wang 24312ca653d6SCaleb Raitto if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { 2432310974faSPeter Xu virtnet_clean_affinity(vi); 2433986a4f4dSJason Wang return; 2434986a4f4dSJason Wang } 2435986a4f4dSJason Wang 24362ca653d6SCaleb Raitto num_cpu = num_online_cpus(); 24372ca653d6SCaleb Raitto stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); 24382ca653d6SCaleb Raitto stragglers = num_cpu >= vi->curr_queue_pairs ? 24392ca653d6SCaleb Raitto num_cpu % vi->curr_queue_pairs : 24402ca653d6SCaleb Raitto 0; 24419b51d9d8SYury Norov cpu = cpumask_first(cpu_online_mask); 24424d99f660SAndrei Vagin 24432ca653d6SCaleb Raitto for (i = 0; i < vi->curr_queue_pairs; i++) { 24442ca653d6SCaleb Raitto group_size = stride + (i < stragglers ? 1 : 0); 24452ca653d6SCaleb Raitto 24462ca653d6SCaleb Raitto for (j = 0; j < group_size; j++) { 24472ca653d6SCaleb Raitto cpumask_set_cpu(cpu, mask); 24482ca653d6SCaleb Raitto cpu = cpumask_next_wrap(cpu, cpu_online_mask, 24492ca653d6SCaleb Raitto nr_cpu_ids, false); 24502ca653d6SCaleb Raitto } 24512ca653d6SCaleb Raitto virtqueue_set_affinity(vi->rq[i].vq, mask); 24522ca653d6SCaleb Raitto virtqueue_set_affinity(vi->sq[i].vq, mask); 2453044ab86dSAntoine Tenart __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); 24542ca653d6SCaleb Raitto cpumask_clear(mask); 2455986a4f4dSJason Wang } 2456986a4f4dSJason Wang 2457986a4f4dSJason Wang vi->affinity_hint_set = true; 24582ca653d6SCaleb Raitto free_cpumask_var(mask); 245947be2479SWanlong Gao } 2460986a4f4dSJason Wang 24618017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 24628de4b2f3SWanlong Gao { 24638017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 24648017c279SSebastian Andrzej Siewior node); 24658de4b2f3SWanlong Gao virtnet_set_affinity(vi); 24668017c279SSebastian Andrzej Siewior return 0; 24678de4b2f3SWanlong Gao } 24683ab098dfSJason Wang 24698017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 24708017c279SSebastian Andrzej Siewior { 24718017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 24728017c279SSebastian Andrzej Siewior node_dead); 24738017c279SSebastian Andrzej Siewior virtnet_set_affinity(vi); 24748017c279SSebastian Andrzej Siewior return 0; 24758017c279SSebastian Andrzej Siewior } 24768017c279SSebastian Andrzej Siewior 24778017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 24788017c279SSebastian Andrzej Siewior { 24798017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 24808017c279SSebastian Andrzej Siewior node); 24818017c279SSebastian Andrzej Siewior 2482310974faSPeter Xu virtnet_clean_affinity(vi); 24838017c279SSebastian Andrzej Siewior return 0; 24848017c279SSebastian Andrzej Siewior } 24858017c279SSebastian Andrzej Siewior 24868017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online; 24878017c279SSebastian Andrzej Siewior 24888017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi) 24898017c279SSebastian Andrzej Siewior { 24908017c279SSebastian Andrzej Siewior int ret; 24918017c279SSebastian Andrzej Siewior 24928017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 24938017c279SSebastian Andrzej Siewior if (ret) 24948017c279SSebastian Andrzej Siewior return ret; 24958017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 24968017c279SSebastian Andrzej Siewior &vi->node_dead); 24978017c279SSebastian Andrzej Siewior if (!ret) 24988017c279SSebastian Andrzej Siewior return ret; 24998017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 25008017c279SSebastian Andrzej Siewior return ret; 25018017c279SSebastian Andrzej Siewior } 25028017c279SSebastian Andrzej Siewior 25038017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 25048017c279SSebastian Andrzej Siewior { 25058017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 25068017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 25078017c279SSebastian Andrzej Siewior &vi->node_dead); 2508a9ea3fc6SHerbert Xu } 2509a9ea3fc6SHerbert Xu 25108f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 251174624944SHao Chen struct ethtool_ringparam *ring, 251274624944SHao Chen struct kernel_ethtool_ringparam *kernel_ring, 251374624944SHao Chen struct netlink_ext_ack *extack) 25148f9f4668SRick Jones { 25158f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 25168f9f4668SRick Jones 25178597b5ddSXuan Zhuo ring->rx_max_pending = vi->rq[0].vq->num_max; 25188597b5ddSXuan Zhuo ring->tx_max_pending = vi->sq[0].vq->num_max; 25198597b5ddSXuan Zhuo ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); 25208597b5ddSXuan Zhuo ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); 25218f9f4668SRick Jones } 25228f9f4668SRick Jones 2523a335b33fSXuan Zhuo static int virtnet_set_ringparam(struct net_device *dev, 2524a335b33fSXuan Zhuo struct ethtool_ringparam *ring, 2525a335b33fSXuan Zhuo struct kernel_ethtool_ringparam *kernel_ring, 2526a335b33fSXuan Zhuo struct netlink_ext_ack *extack) 2527a335b33fSXuan Zhuo { 2528a335b33fSXuan Zhuo struct virtnet_info *vi = netdev_priv(dev); 2529a335b33fSXuan Zhuo u32 rx_pending, tx_pending; 2530a335b33fSXuan Zhuo struct receive_queue *rq; 2531a335b33fSXuan Zhuo struct send_queue *sq; 2532a335b33fSXuan Zhuo int i, err; 2533a335b33fSXuan Zhuo 2534a335b33fSXuan Zhuo if (ring->rx_mini_pending || ring->rx_jumbo_pending) 2535a335b33fSXuan Zhuo return -EINVAL; 2536a335b33fSXuan Zhuo 2537a335b33fSXuan Zhuo rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); 2538a335b33fSXuan Zhuo tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); 2539a335b33fSXuan Zhuo 2540a335b33fSXuan Zhuo if (ring->rx_pending == rx_pending && 2541a335b33fSXuan Zhuo ring->tx_pending == tx_pending) 2542a335b33fSXuan Zhuo return 0; 2543a335b33fSXuan Zhuo 2544a335b33fSXuan Zhuo if (ring->rx_pending > vi->rq[0].vq->num_max) 2545a335b33fSXuan Zhuo return -EINVAL; 2546a335b33fSXuan Zhuo 2547a335b33fSXuan Zhuo if (ring->tx_pending > vi->sq[0].vq->num_max) 2548a335b33fSXuan Zhuo return -EINVAL; 2549a335b33fSXuan Zhuo 2550a335b33fSXuan Zhuo for (i = 0; i < vi->max_queue_pairs; i++) { 2551a335b33fSXuan Zhuo rq = vi->rq + i; 2552a335b33fSXuan Zhuo sq = vi->sq + i; 2553a335b33fSXuan Zhuo 2554a335b33fSXuan Zhuo if (ring->tx_pending != tx_pending) { 2555a335b33fSXuan Zhuo err = virtnet_tx_resize(vi, sq, ring->tx_pending); 2556a335b33fSXuan Zhuo if (err) 2557a335b33fSXuan Zhuo return err; 2558a335b33fSXuan Zhuo } 2559a335b33fSXuan Zhuo 2560a335b33fSXuan Zhuo if (ring->rx_pending != rx_pending) { 2561a335b33fSXuan Zhuo err = virtnet_rx_resize(vi, rq, ring->rx_pending); 2562a335b33fSXuan Zhuo if (err) 2563a335b33fSXuan Zhuo return err; 2564a335b33fSXuan Zhuo } 2565a335b33fSXuan Zhuo } 2566a335b33fSXuan Zhuo 2567a335b33fSXuan Zhuo return 0; 2568a9ea3fc6SHerbert Xu } 2569a9ea3fc6SHerbert Xu 2570c7114b12SAndrew Melnychenko static bool virtnet_commit_rss_command(struct virtnet_info *vi) 2571c7114b12SAndrew Melnychenko { 2572c7114b12SAndrew Melnychenko struct net_device *dev = vi->dev; 2573c7114b12SAndrew Melnychenko struct scatterlist sgs[4]; 2574c7114b12SAndrew Melnychenko unsigned int sg_buf_size; 2575c7114b12SAndrew Melnychenko 2576c7114b12SAndrew Melnychenko /* prepare sgs */ 2577c7114b12SAndrew Melnychenko sg_init_table(sgs, 4); 2578c7114b12SAndrew Melnychenko 2579c7114b12SAndrew Melnychenko sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table); 2580c7114b12SAndrew Melnychenko sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size); 2581c7114b12SAndrew Melnychenko 2582c7114b12SAndrew Melnychenko sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1); 2583c7114b12SAndrew Melnychenko sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size); 2584c7114b12SAndrew Melnychenko 2585c7114b12SAndrew Melnychenko sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key) 2586c7114b12SAndrew Melnychenko - offsetof(struct virtio_net_ctrl_rss, max_tx_vq); 2587c7114b12SAndrew Melnychenko sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size); 2588c7114b12SAndrew Melnychenko 2589c7114b12SAndrew Melnychenko sg_buf_size = vi->rss_key_size; 2590c7114b12SAndrew Melnychenko sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size); 2591c7114b12SAndrew Melnychenko 2592c7114b12SAndrew Melnychenko if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 259391f41f01SAndrew Melnychenko vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG 259491f41f01SAndrew Melnychenko : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) { 2595c7114b12SAndrew Melnychenko dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n"); 2596c7114b12SAndrew Melnychenko return false; 2597c7114b12SAndrew Melnychenko } 2598c7114b12SAndrew Melnychenko return true; 2599c7114b12SAndrew Melnychenko } 2600c7114b12SAndrew Melnychenko 2601c7114b12SAndrew Melnychenko static void virtnet_init_default_rss(struct virtnet_info *vi) 2602c7114b12SAndrew Melnychenko { 2603c7114b12SAndrew Melnychenko u32 indir_val = 0; 2604c7114b12SAndrew Melnychenko int i = 0; 2605c7114b12SAndrew Melnychenko 2606c7114b12SAndrew Melnychenko vi->ctrl->rss.hash_types = vi->rss_hash_types_supported; 2607c1170820SAndrew Melnychenko vi->rss_hash_types_saved = vi->rss_hash_types_supported; 2608c7114b12SAndrew Melnychenko vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size 2609c7114b12SAndrew Melnychenko ? vi->rss_indir_table_size - 1 : 0; 2610c7114b12SAndrew Melnychenko vi->ctrl->rss.unclassified_queue = 0; 2611c7114b12SAndrew Melnychenko 2612c7114b12SAndrew Melnychenko for (; i < vi->rss_indir_table_size; ++i) { 2613c7114b12SAndrew Melnychenko indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs); 2614c7114b12SAndrew Melnychenko vi->ctrl->rss.indirection_table[i] = indir_val; 2615c7114b12SAndrew Melnychenko } 2616c7114b12SAndrew Melnychenko 2617c7114b12SAndrew Melnychenko vi->ctrl->rss.max_tx_vq = vi->curr_queue_pairs; 2618c7114b12SAndrew Melnychenko vi->ctrl->rss.hash_key_length = vi->rss_key_size; 2619c7114b12SAndrew Melnychenko 2620c7114b12SAndrew Melnychenko netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size); 2621c7114b12SAndrew Melnychenko } 2622c7114b12SAndrew Melnychenko 2623c1170820SAndrew Melnychenko static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info) 2624c1170820SAndrew Melnychenko { 2625c1170820SAndrew Melnychenko info->data = 0; 2626c1170820SAndrew Melnychenko switch (info->flow_type) { 2627c1170820SAndrew Melnychenko case TCP_V4_FLOW: 2628c1170820SAndrew Melnychenko if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) { 2629c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST | 2630c1170820SAndrew Melnychenko RXH_L4_B_0_1 | RXH_L4_B_2_3; 2631c1170820SAndrew Melnychenko } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 2632c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST; 2633c1170820SAndrew Melnychenko } 2634c1170820SAndrew Melnychenko break; 2635c1170820SAndrew Melnychenko case TCP_V6_FLOW: 2636c1170820SAndrew Melnychenko if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) { 2637c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST | 2638c1170820SAndrew Melnychenko RXH_L4_B_0_1 | RXH_L4_B_2_3; 2639c1170820SAndrew Melnychenko } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 2640c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST; 2641c1170820SAndrew Melnychenko } 2642c1170820SAndrew Melnychenko break; 2643c1170820SAndrew Melnychenko case UDP_V4_FLOW: 2644c1170820SAndrew Melnychenko if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) { 2645c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST | 2646c1170820SAndrew Melnychenko RXH_L4_B_0_1 | RXH_L4_B_2_3; 2647c1170820SAndrew Melnychenko } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 2648c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST; 2649c1170820SAndrew Melnychenko } 2650c1170820SAndrew Melnychenko break; 2651c1170820SAndrew Melnychenko case UDP_V6_FLOW: 2652c1170820SAndrew Melnychenko if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) { 2653c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST | 2654c1170820SAndrew Melnychenko RXH_L4_B_0_1 | RXH_L4_B_2_3; 2655c1170820SAndrew Melnychenko } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 2656c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST; 2657c1170820SAndrew Melnychenko } 2658c1170820SAndrew Melnychenko break; 2659c1170820SAndrew Melnychenko case IPV4_FLOW: 2660c1170820SAndrew Melnychenko if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) 2661c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST; 2662c1170820SAndrew Melnychenko 2663c1170820SAndrew Melnychenko break; 2664c1170820SAndrew Melnychenko case IPV6_FLOW: 2665c1170820SAndrew Melnychenko if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) 2666c1170820SAndrew Melnychenko info->data = RXH_IP_SRC | RXH_IP_DST; 2667c1170820SAndrew Melnychenko 2668c1170820SAndrew Melnychenko break; 2669c1170820SAndrew Melnychenko default: 2670c1170820SAndrew Melnychenko info->data = 0; 2671c1170820SAndrew Melnychenko break; 2672c1170820SAndrew Melnychenko } 2673c1170820SAndrew Melnychenko } 2674c1170820SAndrew Melnychenko 2675c1170820SAndrew Melnychenko static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info) 2676c1170820SAndrew Melnychenko { 2677c1170820SAndrew Melnychenko u32 new_hashtypes = vi->rss_hash_types_saved; 2678c1170820SAndrew Melnychenko bool is_disable = info->data & RXH_DISCARD; 2679c1170820SAndrew Melnychenko bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3); 2680c1170820SAndrew Melnychenko 2681c1170820SAndrew Melnychenko /* supports only 'sd', 'sdfn' and 'r' */ 2682c1170820SAndrew Melnychenko if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable)) 2683c1170820SAndrew Melnychenko return false; 2684c1170820SAndrew Melnychenko 2685c1170820SAndrew Melnychenko switch (info->flow_type) { 2686c1170820SAndrew Melnychenko case TCP_V4_FLOW: 2687c1170820SAndrew Melnychenko new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4); 2688c1170820SAndrew Melnychenko if (!is_disable) 2689c1170820SAndrew Melnychenko new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4 2690c1170820SAndrew Melnychenko | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0); 2691c1170820SAndrew Melnychenko break; 2692c1170820SAndrew Melnychenko case UDP_V4_FLOW: 2693c1170820SAndrew Melnychenko new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4); 2694c1170820SAndrew Melnychenko if (!is_disable) 2695c1170820SAndrew Melnychenko new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4 2696c1170820SAndrew Melnychenko | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0); 2697c1170820SAndrew Melnychenko break; 2698c1170820SAndrew Melnychenko case IPV4_FLOW: 2699c1170820SAndrew Melnychenko new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4; 2700c1170820SAndrew Melnychenko if (!is_disable) 2701c1170820SAndrew Melnychenko new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4; 2702c1170820SAndrew Melnychenko break; 2703c1170820SAndrew Melnychenko case TCP_V6_FLOW: 2704c1170820SAndrew Melnychenko new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6); 2705c1170820SAndrew Melnychenko if (!is_disable) 2706c1170820SAndrew Melnychenko new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6 2707c1170820SAndrew Melnychenko | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0); 2708c1170820SAndrew Melnychenko break; 2709c1170820SAndrew Melnychenko case UDP_V6_FLOW: 2710c1170820SAndrew Melnychenko new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6); 2711c1170820SAndrew Melnychenko if (!is_disable) 2712c1170820SAndrew Melnychenko new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6 2713c1170820SAndrew Melnychenko | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0); 2714c1170820SAndrew Melnychenko break; 2715c1170820SAndrew Melnychenko case IPV6_FLOW: 2716c1170820SAndrew Melnychenko new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6; 2717c1170820SAndrew Melnychenko if (!is_disable) 2718c1170820SAndrew Melnychenko new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6; 2719c1170820SAndrew Melnychenko break; 2720c1170820SAndrew Melnychenko default: 2721c1170820SAndrew Melnychenko /* unsupported flow */ 2722c1170820SAndrew Melnychenko return false; 2723c1170820SAndrew Melnychenko } 2724c1170820SAndrew Melnychenko 2725c1170820SAndrew Melnychenko /* if unsupported hashtype was set */ 2726c1170820SAndrew Melnychenko if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported)) 2727c1170820SAndrew Melnychenko return false; 2728c1170820SAndrew Melnychenko 2729c1170820SAndrew Melnychenko if (new_hashtypes != vi->rss_hash_types_saved) { 2730c1170820SAndrew Melnychenko vi->rss_hash_types_saved = new_hashtypes; 2731c1170820SAndrew Melnychenko vi->ctrl->rss.hash_types = vi->rss_hash_types_saved; 2732c1170820SAndrew Melnychenko if (vi->dev->features & NETIF_F_RXHASH) 2733c1170820SAndrew Melnychenko return virtnet_commit_rss_command(vi); 2734c1170820SAndrew Melnychenko } 2735c1170820SAndrew Melnychenko 2736c1170820SAndrew Melnychenko return true; 2737c1170820SAndrew Melnychenko } 273866846048SRick Jones 273966846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 274066846048SRick Jones struct ethtool_drvinfo *info) 274166846048SRick Jones { 274266846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 274366846048SRick Jones struct virtio_device *vdev = vi->vdev; 274466846048SRick Jones 2745fb3ceec1SWolfram Sang strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 2746fb3ceec1SWolfram Sang strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 2747fb3ceec1SWolfram Sang strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 274866846048SRick Jones 274966846048SRick Jones } 275066846048SRick Jones 2751d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 2752d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 2753d73bcd2cSJason Wang struct ethtool_channels *channels) 2754d73bcd2cSJason Wang { 2755d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2756d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 2757d73bcd2cSJason Wang int err; 2758d73bcd2cSJason Wang 2759d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 2760d73bcd2cSJason Wang * We don't allow setting 'other' channels. 2761d73bcd2cSJason Wang */ 2762d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 2763d73bcd2cSJason Wang return -EINVAL; 2764d73bcd2cSJason Wang 2765c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 2766d73bcd2cSJason Wang return -EINVAL; 2767d73bcd2cSJason Wang 2768f600b690SJohn Fastabend /* For now we don't support modifying channels while XDP is loaded 2769f600b690SJohn Fastabend * also when XDP is loaded all RX queues have XDP programs so we only 2770f600b690SJohn Fastabend * need to check a single RX queue. 2771f600b690SJohn Fastabend */ 2772f600b690SJohn Fastabend if (vi->rq[0].xdp_prog) 2773f600b690SJohn Fastabend return -EINVAL; 2774f600b690SJohn Fastabend 2775a0d1d0f4SSebastian Andrzej Siewior cpus_read_lock(); 277647315329SJohn Fastabend err = _virtnet_set_queues(vi, queue_pairs); 2777de33212fSJeff Dike if (err) { 2778a0d1d0f4SSebastian Andrzej Siewior cpus_read_unlock(); 2779de33212fSJeff Dike goto err; 2780d73bcd2cSJason Wang } 2781de33212fSJeff Dike virtnet_set_affinity(vi); 2782a0d1d0f4SSebastian Andrzej Siewior cpus_read_unlock(); 2783d73bcd2cSJason Wang 2784de33212fSJeff Dike netif_set_real_num_tx_queues(dev, queue_pairs); 2785de33212fSJeff Dike netif_set_real_num_rx_queues(dev, queue_pairs); 2786de33212fSJeff Dike err: 2787d73bcd2cSJason Wang return err; 2788d73bcd2cSJason Wang } 2789d73bcd2cSJason Wang 2790d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 2791d7dfc5cfSToshiaki Makita { 2792d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2793d7dfc5cfSToshiaki Makita unsigned int i, j; 2794d7a9a01bSAlexander Duyck u8 *p = data; 2795d7dfc5cfSToshiaki Makita 2796d7dfc5cfSToshiaki Makita switch (stringset) { 2797d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2798d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2799d7a9a01bSAlexander Duyck for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) 2800d7a9a01bSAlexander Duyck ethtool_sprintf(&p, "rx_queue_%u_%s", i, 2801d7a9a01bSAlexander Duyck virtnet_rq_stats_desc[j].desc); 2802d7dfc5cfSToshiaki Makita } 2803d7dfc5cfSToshiaki Makita 2804d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2805d7a9a01bSAlexander Duyck for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) 2806d7a9a01bSAlexander Duyck ethtool_sprintf(&p, "tx_queue_%u_%s", i, 2807d7a9a01bSAlexander Duyck virtnet_sq_stats_desc[j].desc); 2808d7dfc5cfSToshiaki Makita } 2809d7dfc5cfSToshiaki Makita break; 2810d7dfc5cfSToshiaki Makita } 2811d7dfc5cfSToshiaki Makita } 2812d7dfc5cfSToshiaki Makita 2813d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset) 2814d7dfc5cfSToshiaki Makita { 2815d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2816d7dfc5cfSToshiaki Makita 2817d7dfc5cfSToshiaki Makita switch (sset) { 2818d7dfc5cfSToshiaki Makita case ETH_SS_STATS: 2819d7dfc5cfSToshiaki Makita return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + 2820d7dfc5cfSToshiaki Makita VIRTNET_SQ_STATS_LEN); 2821d7dfc5cfSToshiaki Makita default: 2822d7dfc5cfSToshiaki Makita return -EOPNOTSUPP; 2823d7dfc5cfSToshiaki Makita } 2824d7dfc5cfSToshiaki Makita } 2825d7dfc5cfSToshiaki Makita 2826d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev, 2827d7dfc5cfSToshiaki Makita struct ethtool_stats *stats, u64 *data) 2828d7dfc5cfSToshiaki Makita { 2829d7dfc5cfSToshiaki Makita struct virtnet_info *vi = netdev_priv(dev); 2830d7dfc5cfSToshiaki Makita unsigned int idx = 0, start, i, j; 2831d7dfc5cfSToshiaki Makita const u8 *stats_base; 2832d7dfc5cfSToshiaki Makita size_t offset; 2833d7dfc5cfSToshiaki Makita 2834d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2835d7dfc5cfSToshiaki Makita struct receive_queue *rq = &vi->rq[i]; 2836d7dfc5cfSToshiaki Makita 2837d46eeeafSJason Wang stats_base = (u8 *)&rq->stats; 2838d7dfc5cfSToshiaki Makita do { 2839068c38adSThomas Gleixner start = u64_stats_fetch_begin(&rq->stats.syncp); 2840d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2841d7dfc5cfSToshiaki Makita offset = virtnet_rq_stats_desc[j].offset; 2842d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2843d7dfc5cfSToshiaki Makita } 2844068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&rq->stats.syncp, start)); 2845d7dfc5cfSToshiaki Makita idx += VIRTNET_RQ_STATS_LEN; 2846d7dfc5cfSToshiaki Makita } 2847d7dfc5cfSToshiaki Makita 2848d7dfc5cfSToshiaki Makita for (i = 0; i < vi->curr_queue_pairs; i++) { 2849d7dfc5cfSToshiaki Makita struct send_queue *sq = &vi->sq[i]; 2850d7dfc5cfSToshiaki Makita 2851d7dfc5cfSToshiaki Makita stats_base = (u8 *)&sq->stats; 2852d7dfc5cfSToshiaki Makita do { 2853068c38adSThomas Gleixner start = u64_stats_fetch_begin(&sq->stats.syncp); 2854d7dfc5cfSToshiaki Makita for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 2855d7dfc5cfSToshiaki Makita offset = virtnet_sq_stats_desc[j].offset; 2856d7dfc5cfSToshiaki Makita data[idx + j] = *(u64 *)(stats_base + offset); 2857d7dfc5cfSToshiaki Makita } 2858068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&sq->stats.syncp, start)); 2859d7dfc5cfSToshiaki Makita idx += VIRTNET_SQ_STATS_LEN; 2860d7dfc5cfSToshiaki Makita } 2861d7dfc5cfSToshiaki Makita } 2862d7dfc5cfSToshiaki Makita 2863d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 2864d73bcd2cSJason Wang struct ethtool_channels *channels) 2865d73bcd2cSJason Wang { 2866d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 2867d73bcd2cSJason Wang 2868d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 2869d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 2870d73bcd2cSJason Wang channels->max_other = 0; 2871d73bcd2cSJason Wang channels->rx_count = 0; 2872d73bcd2cSJason Wang channels->tx_count = 0; 2873d73bcd2cSJason Wang channels->other_count = 0; 2874d73bcd2cSJason Wang } 2875d73bcd2cSJason Wang 2876ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev, 2877ebb6b4b1SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 287816032be5SNikolay Aleksandrov { 287916032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 288016032be5SNikolay Aleksandrov 28819aedc6e2SCris Forno return ethtool_virtdev_set_link_ksettings(dev, cmd, 28829aedc6e2SCris Forno &vi->speed, &vi->duplex); 288316032be5SNikolay Aleksandrov } 288416032be5SNikolay Aleksandrov 2885ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev, 2886ebb6b4b1SPhilippe Reynes struct ethtool_link_ksettings *cmd) 288716032be5SNikolay Aleksandrov { 288816032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 288916032be5SNikolay Aleksandrov 2890ebb6b4b1SPhilippe Reynes cmd->base.speed = vi->speed; 2891ebb6b4b1SPhilippe Reynes cmd->base.duplex = vi->duplex; 2892ebb6b4b1SPhilippe Reynes cmd->base.port = PORT_OTHER; 289316032be5SNikolay Aleksandrov 289416032be5SNikolay Aleksandrov return 0; 289516032be5SNikolay Aleksandrov } 289616032be5SNikolay Aleksandrov 2897699b045aSAlvaro Karsz static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi, 2898699b045aSAlvaro Karsz struct ethtool_coalesce *ec) 2899699b045aSAlvaro Karsz { 2900699b045aSAlvaro Karsz struct scatterlist sgs_tx, sgs_rx; 2901699b045aSAlvaro Karsz struct virtio_net_ctrl_coal_tx coal_tx; 2902699b045aSAlvaro Karsz struct virtio_net_ctrl_coal_rx coal_rx; 2903699b045aSAlvaro Karsz 2904699b045aSAlvaro Karsz coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs); 2905699b045aSAlvaro Karsz coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames); 2906699b045aSAlvaro Karsz sg_init_one(&sgs_tx, &coal_tx, sizeof(coal_tx)); 2907699b045aSAlvaro Karsz 2908699b045aSAlvaro Karsz if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 2909699b045aSAlvaro Karsz VIRTIO_NET_CTRL_NOTF_COAL_TX_SET, 2910699b045aSAlvaro Karsz &sgs_tx)) 2911699b045aSAlvaro Karsz return -EINVAL; 2912699b045aSAlvaro Karsz 2913699b045aSAlvaro Karsz /* Save parameters */ 2914699b045aSAlvaro Karsz vi->tx_usecs = ec->tx_coalesce_usecs; 2915699b045aSAlvaro Karsz vi->tx_max_packets = ec->tx_max_coalesced_frames; 2916699b045aSAlvaro Karsz 2917699b045aSAlvaro Karsz coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs); 2918699b045aSAlvaro Karsz coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames); 2919699b045aSAlvaro Karsz sg_init_one(&sgs_rx, &coal_rx, sizeof(coal_rx)); 2920699b045aSAlvaro Karsz 2921699b045aSAlvaro Karsz if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 2922699b045aSAlvaro Karsz VIRTIO_NET_CTRL_NOTF_COAL_RX_SET, 2923699b045aSAlvaro Karsz &sgs_rx)) 2924699b045aSAlvaro Karsz return -EINVAL; 2925699b045aSAlvaro Karsz 2926699b045aSAlvaro Karsz /* Save parameters */ 2927699b045aSAlvaro Karsz vi->rx_usecs = ec->rx_coalesce_usecs; 2928699b045aSAlvaro Karsz vi->rx_max_packets = ec->rx_max_coalesced_frames; 2929699b045aSAlvaro Karsz 2930699b045aSAlvaro Karsz return 0; 2931699b045aSAlvaro Karsz } 2932699b045aSAlvaro Karsz 2933699b045aSAlvaro Karsz static int virtnet_coal_params_supported(struct ethtool_coalesce *ec) 2934699b045aSAlvaro Karsz { 2935699b045aSAlvaro Karsz /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL 2936699b045aSAlvaro Karsz * feature is negotiated. 2937699b045aSAlvaro Karsz */ 2938699b045aSAlvaro Karsz if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs) 2939699b045aSAlvaro Karsz return -EOPNOTSUPP; 2940699b045aSAlvaro Karsz 2941699b045aSAlvaro Karsz if (ec->tx_max_coalesced_frames > 1 || 2942699b045aSAlvaro Karsz ec->rx_max_coalesced_frames != 1) 2943699b045aSAlvaro Karsz return -EINVAL; 2944699b045aSAlvaro Karsz 2945699b045aSAlvaro Karsz return 0; 2946699b045aSAlvaro Karsz } 2947699b045aSAlvaro Karsz 29480c465be1SJason Wang static int virtnet_set_coalesce(struct net_device *dev, 2949f3ccfda1SYufeng Mo struct ethtool_coalesce *ec, 2950f3ccfda1SYufeng Mo struct kernel_ethtool_coalesce *kernel_coal, 2951f3ccfda1SYufeng Mo struct netlink_ext_ack *extack) 29520c465be1SJason Wang { 29530c465be1SJason Wang struct virtnet_info *vi = netdev_priv(dev); 2954699b045aSAlvaro Karsz int ret, i, napi_weight; 2955699b045aSAlvaro Karsz bool update_napi = false; 29560c465be1SJason Wang 2957699b045aSAlvaro Karsz /* Can't change NAPI weight if the link is up */ 29580c465be1SJason Wang napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 29590c465be1SJason Wang if (napi_weight ^ vi->sq[0].napi.weight) { 29600c465be1SJason Wang if (dev->flags & IFF_UP) 29610c465be1SJason Wang return -EBUSY; 2962699b045aSAlvaro Karsz else 2963699b045aSAlvaro Karsz update_napi = true; 2964699b045aSAlvaro Karsz } 2965699b045aSAlvaro Karsz 2966699b045aSAlvaro Karsz if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) 2967699b045aSAlvaro Karsz ret = virtnet_send_notf_coal_cmds(vi, ec); 2968699b045aSAlvaro Karsz else 2969699b045aSAlvaro Karsz ret = virtnet_coal_params_supported(ec); 2970699b045aSAlvaro Karsz 2971699b045aSAlvaro Karsz if (ret) 2972699b045aSAlvaro Karsz return ret; 2973699b045aSAlvaro Karsz 2974699b045aSAlvaro Karsz if (update_napi) { 29750c465be1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 29760c465be1SJason Wang vi->sq[i].napi.weight = napi_weight; 29770c465be1SJason Wang } 29780c465be1SJason Wang 2979699b045aSAlvaro Karsz return ret; 29800c465be1SJason Wang } 29810c465be1SJason Wang 29820c465be1SJason Wang static int virtnet_get_coalesce(struct net_device *dev, 2983f3ccfda1SYufeng Mo struct ethtool_coalesce *ec, 2984f3ccfda1SYufeng Mo struct kernel_ethtool_coalesce *kernel_coal, 2985f3ccfda1SYufeng Mo struct netlink_ext_ack *extack) 29860c465be1SJason Wang { 29870c465be1SJason Wang struct virtnet_info *vi = netdev_priv(dev); 29880c465be1SJason Wang 2989699b045aSAlvaro Karsz if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { 2990699b045aSAlvaro Karsz ec->rx_coalesce_usecs = vi->rx_usecs; 2991699b045aSAlvaro Karsz ec->tx_coalesce_usecs = vi->tx_usecs; 2992699b045aSAlvaro Karsz ec->tx_max_coalesced_frames = vi->tx_max_packets; 2993699b045aSAlvaro Karsz ec->rx_max_coalesced_frames = vi->rx_max_packets; 2994699b045aSAlvaro Karsz } else { 2995699b045aSAlvaro Karsz ec->rx_max_coalesced_frames = 1; 29960c465be1SJason Wang 29970c465be1SJason Wang if (vi->sq[0].napi.weight) 29980c465be1SJason Wang ec->tx_max_coalesced_frames = 1; 2999699b045aSAlvaro Karsz } 30000c465be1SJason Wang 30010c465be1SJason Wang return 0; 30020c465be1SJason Wang } 30030c465be1SJason Wang 300416032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 300516032be5SNikolay Aleksandrov { 300616032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 300716032be5SNikolay Aleksandrov 300816032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 300916032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 301016032be5SNikolay Aleksandrov } 301116032be5SNikolay Aleksandrov 3012faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi) 3013faa9b39fSJason Baron { 3014faa9b39fSJason Baron u32 speed; 3015faa9b39fSJason Baron u8 duplex; 3016faa9b39fSJason Baron 3017faa9b39fSJason Baron if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 3018faa9b39fSJason Baron return; 3019faa9b39fSJason Baron 302064ffa39dSMichael S. Tsirkin virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); 302164ffa39dSMichael S. Tsirkin 3022faa9b39fSJason Baron if (ethtool_validate_speed(speed)) 3023faa9b39fSJason Baron vi->speed = speed; 302464ffa39dSMichael S. Tsirkin 302564ffa39dSMichael S. Tsirkin virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); 302664ffa39dSMichael S. Tsirkin 3027faa9b39fSJason Baron if (ethtool_validate_duplex(duplex)) 3028faa9b39fSJason Baron vi->duplex = duplex; 3029faa9b39fSJason Baron } 3030faa9b39fSJason Baron 3031c7114b12SAndrew Melnychenko static u32 virtnet_get_rxfh_key_size(struct net_device *dev) 3032c7114b12SAndrew Melnychenko { 3033c7114b12SAndrew Melnychenko return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size; 3034c7114b12SAndrew Melnychenko } 3035c7114b12SAndrew Melnychenko 3036c7114b12SAndrew Melnychenko static u32 virtnet_get_rxfh_indir_size(struct net_device *dev) 3037c7114b12SAndrew Melnychenko { 3038c7114b12SAndrew Melnychenko return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size; 3039c7114b12SAndrew Melnychenko } 3040c7114b12SAndrew Melnychenko 3041c7114b12SAndrew Melnychenko static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc) 3042c7114b12SAndrew Melnychenko { 3043c7114b12SAndrew Melnychenko struct virtnet_info *vi = netdev_priv(dev); 3044c7114b12SAndrew Melnychenko int i; 3045c7114b12SAndrew Melnychenko 3046c7114b12SAndrew Melnychenko if (indir) { 3047c7114b12SAndrew Melnychenko for (i = 0; i < vi->rss_indir_table_size; ++i) 3048c7114b12SAndrew Melnychenko indir[i] = vi->ctrl->rss.indirection_table[i]; 3049c7114b12SAndrew Melnychenko } 3050c7114b12SAndrew Melnychenko 3051c7114b12SAndrew Melnychenko if (key) 3052c7114b12SAndrew Melnychenko memcpy(key, vi->ctrl->rss.key, vi->rss_key_size); 3053c7114b12SAndrew Melnychenko 3054c7114b12SAndrew Melnychenko if (hfunc) 3055c7114b12SAndrew Melnychenko *hfunc = ETH_RSS_HASH_TOP; 3056c7114b12SAndrew Melnychenko 3057c7114b12SAndrew Melnychenko return 0; 3058c7114b12SAndrew Melnychenko } 3059c7114b12SAndrew Melnychenko 3060c7114b12SAndrew Melnychenko static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc) 3061c7114b12SAndrew Melnychenko { 3062c7114b12SAndrew Melnychenko struct virtnet_info *vi = netdev_priv(dev); 3063c7114b12SAndrew Melnychenko int i; 3064c7114b12SAndrew Melnychenko 3065c7114b12SAndrew Melnychenko if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) 3066c7114b12SAndrew Melnychenko return -EOPNOTSUPP; 3067c7114b12SAndrew Melnychenko 3068c7114b12SAndrew Melnychenko if (indir) { 3069c7114b12SAndrew Melnychenko for (i = 0; i < vi->rss_indir_table_size; ++i) 3070c7114b12SAndrew Melnychenko vi->ctrl->rss.indirection_table[i] = indir[i]; 3071c7114b12SAndrew Melnychenko } 3072c7114b12SAndrew Melnychenko if (key) 3073c7114b12SAndrew Melnychenko memcpy(vi->ctrl->rss.key, key, vi->rss_key_size); 3074c7114b12SAndrew Melnychenko 3075c7114b12SAndrew Melnychenko virtnet_commit_rss_command(vi); 3076c7114b12SAndrew Melnychenko 3077c7114b12SAndrew Melnychenko return 0; 3078c7114b12SAndrew Melnychenko } 3079c7114b12SAndrew Melnychenko 3080c7114b12SAndrew Melnychenko static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs) 3081c7114b12SAndrew Melnychenko { 3082c7114b12SAndrew Melnychenko struct virtnet_info *vi = netdev_priv(dev); 3083c7114b12SAndrew Melnychenko int rc = 0; 3084c7114b12SAndrew Melnychenko 3085c7114b12SAndrew Melnychenko switch (info->cmd) { 3086c7114b12SAndrew Melnychenko case ETHTOOL_GRXRINGS: 3087c7114b12SAndrew Melnychenko info->data = vi->curr_queue_pairs; 3088c7114b12SAndrew Melnychenko break; 3089c1170820SAndrew Melnychenko case ETHTOOL_GRXFH: 3090c1170820SAndrew Melnychenko virtnet_get_hashflow(vi, info); 3091c1170820SAndrew Melnychenko break; 3092c1170820SAndrew Melnychenko default: 3093c1170820SAndrew Melnychenko rc = -EOPNOTSUPP; 3094c1170820SAndrew Melnychenko } 3095c1170820SAndrew Melnychenko 3096c1170820SAndrew Melnychenko return rc; 3097c1170820SAndrew Melnychenko } 3098c1170820SAndrew Melnychenko 3099c1170820SAndrew Melnychenko static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info) 3100c1170820SAndrew Melnychenko { 3101c1170820SAndrew Melnychenko struct virtnet_info *vi = netdev_priv(dev); 3102c1170820SAndrew Melnychenko int rc = 0; 3103c1170820SAndrew Melnychenko 3104c1170820SAndrew Melnychenko switch (info->cmd) { 3105c1170820SAndrew Melnychenko case ETHTOOL_SRXFH: 3106c1170820SAndrew Melnychenko if (!virtnet_set_hashflow(vi, info)) 3107c1170820SAndrew Melnychenko rc = -EINVAL; 3108c1170820SAndrew Melnychenko 3109c1170820SAndrew Melnychenko break; 3110c7114b12SAndrew Melnychenko default: 3111c7114b12SAndrew Melnychenko rc = -EOPNOTSUPP; 3112c7114b12SAndrew Melnychenko } 3113c7114b12SAndrew Melnychenko 3114c7114b12SAndrew Melnychenko return rc; 3115c7114b12SAndrew Melnychenko } 3116c7114b12SAndrew Melnychenko 31170fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 3118699b045aSAlvaro Karsz .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES | 3119699b045aSAlvaro Karsz ETHTOOL_COALESCE_USECS, 312066846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 31219f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 31228f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 3123a335b33fSXuan Zhuo .set_ringparam = virtnet_set_ringparam, 3124d7dfc5cfSToshiaki Makita .get_strings = virtnet_get_strings, 3125d7dfc5cfSToshiaki Makita .get_sset_count = virtnet_get_sset_count, 3126d7dfc5cfSToshiaki Makita .get_ethtool_stats = virtnet_get_ethtool_stats, 3127d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 3128d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 3129074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 3130ebb6b4b1SPhilippe Reynes .get_link_ksettings = virtnet_get_link_ksettings, 3131ebb6b4b1SPhilippe Reynes .set_link_ksettings = virtnet_set_link_ksettings, 31320c465be1SJason Wang .set_coalesce = virtnet_set_coalesce, 31330c465be1SJason Wang .get_coalesce = virtnet_get_coalesce, 3134c7114b12SAndrew Melnychenko .get_rxfh_key_size = virtnet_get_rxfh_key_size, 3135c7114b12SAndrew Melnychenko .get_rxfh_indir_size = virtnet_get_rxfh_indir_size, 3136c7114b12SAndrew Melnychenko .get_rxfh = virtnet_get_rxfh, 3137c7114b12SAndrew Melnychenko .set_rxfh = virtnet_set_rxfh, 3138c7114b12SAndrew Melnychenko .get_rxnfc = virtnet_get_rxnfc, 3139c1170820SAndrew Melnychenko .set_rxnfc = virtnet_set_rxnfc, 3140a9ea3fc6SHerbert Xu }; 3141a9ea3fc6SHerbert Xu 31429fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev) 31439fe7bfceSJohn Fastabend { 31449fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 31459fe7bfceSJohn Fastabend 31469fe7bfceSJohn Fastabend /* Make sure no work handler is accessing the device */ 31479fe7bfceSJohn Fastabend flush_work(&vi->config_work); 31489fe7bfceSJohn Fastabend 314905c998b7SAke Koomsin netif_tx_lock_bh(vi->dev); 31509fe7bfceSJohn Fastabend netif_device_detach(vi->dev); 315105c998b7SAke Koomsin netif_tx_unlock_bh(vi->dev); 31528af52fe9SStephan Gerhold if (netif_running(vi->dev)) 31538af52fe9SStephan Gerhold virtnet_close(vi->dev); 31549fe7bfceSJohn Fastabend } 31559fe7bfceSJohn Fastabend 31569fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi); 31579fe7bfceSJohn Fastabend 31589fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev) 31599fe7bfceSJohn Fastabend { 31609fe7bfceSJohn Fastabend struct virtnet_info *vi = vdev->priv; 31618af52fe9SStephan Gerhold int err; 31629fe7bfceSJohn Fastabend 31639fe7bfceSJohn Fastabend err = init_vqs(vi); 31649fe7bfceSJohn Fastabend if (err) 31659fe7bfceSJohn Fastabend return err; 31669fe7bfceSJohn Fastabend 31679fe7bfceSJohn Fastabend virtio_device_ready(vdev); 31689fe7bfceSJohn Fastabend 31695a159128SJason Wang enable_delayed_refill(vi); 31705a159128SJason Wang 31719fe7bfceSJohn Fastabend if (netif_running(vi->dev)) { 31728af52fe9SStephan Gerhold err = virtnet_open(vi->dev); 31738af52fe9SStephan Gerhold if (err) 31748af52fe9SStephan Gerhold return err; 31759fe7bfceSJohn Fastabend } 31769fe7bfceSJohn Fastabend 317705c998b7SAke Koomsin netif_tx_lock_bh(vi->dev); 31789fe7bfceSJohn Fastabend netif_device_attach(vi->dev); 317905c998b7SAke Koomsin netif_tx_unlock_bh(vi->dev); 31809fe7bfceSJohn Fastabend return err; 31819fe7bfceSJohn Fastabend } 31829fe7bfceSJohn Fastabend 31833f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 31843f93522fSJason Wang { 31853f93522fSJason Wang struct scatterlist sg; 318612e57169SMichael S. Tsirkin vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); 31873f93522fSJason Wang 318812e57169SMichael S. Tsirkin sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); 31893f93522fSJason Wang 31903f93522fSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 31913f93522fSJason Wang VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 31923f93522fSJason Wang dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); 31933f93522fSJason Wang return -EINVAL; 31943f93522fSJason Wang } 31953f93522fSJason Wang 31963f93522fSJason Wang return 0; 31973f93522fSJason Wang } 31983f93522fSJason Wang 31993f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 32003f93522fSJason Wang { 32013f93522fSJason Wang u64 offloads = 0; 32023f93522fSJason Wang 32033f93522fSJason Wang if (!vi->guest_offloads) 32043f93522fSJason Wang return 0; 32053f93522fSJason Wang 32063f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 32073f93522fSJason Wang } 32083f93522fSJason Wang 32093f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 32103f93522fSJason Wang { 32113f93522fSJason Wang u64 offloads = vi->guest_offloads; 32123f93522fSJason Wang 32133f93522fSJason Wang if (!vi->guest_offloads) 32143f93522fSJason Wang return 0; 32153f93522fSJason Wang 32163f93522fSJason Wang return virtnet_set_guest_offloads(vi, offloads); 32173f93522fSJason Wang } 32183f93522fSJason Wang 32199861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 32209861ce03SJakub Kicinski struct netlink_ext_ack *extack) 3221f600b690SJohn Fastabend { 3222e814b958SHeng Qi unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM + 3223e814b958SHeng Qi sizeof(struct skb_shared_info)); 3224e814b958SHeng Qi unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN; 3225f600b690SJohn Fastabend struct virtnet_info *vi = netdev_priv(dev); 3226f600b690SJohn Fastabend struct bpf_prog *old_prog; 3227017b29c3SJason Wang u16 xdp_qp = 0, curr_qp; 3228672aafd5SJohn Fastabend int i, err; 3229f600b690SJohn Fastabend 32303f93522fSJason Wang if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 32313f93522fSJason Wang && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 323292502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 323392502fe8SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 323418ba58e1SJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 3235418044e1SAndrew Melnychenko virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) || 3236418044e1SAndrew Melnychenko virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) || 3237418044e1SAndrew Melnychenko virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) { 3238dbcf24d1SJason Wang NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first"); 3239f600b690SJohn Fastabend return -EOPNOTSUPP; 3240f600b690SJohn Fastabend } 3241f600b690SJohn Fastabend 3242f600b690SJohn Fastabend if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 32434d463c4dSDaniel Borkmann NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 3244f600b690SJohn Fastabend return -EINVAL; 3245f600b690SJohn Fastabend } 3246f600b690SJohn Fastabend 32478d9bc36dSHeng Qi if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) { 32488d9bc36dSHeng Qi NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags"); 32498d9bc36dSHeng Qi netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz); 3250f600b690SJohn Fastabend return -EINVAL; 3251f600b690SJohn Fastabend } 3252f600b690SJohn Fastabend 3253672aafd5SJohn Fastabend curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 3254672aafd5SJohn Fastabend if (prog) 3255672aafd5SJohn Fastabend xdp_qp = nr_cpu_ids; 3256672aafd5SJohn Fastabend 3257672aafd5SJohn Fastabend /* XDP requires extra queues for XDP_TX */ 3258672aafd5SJohn Fastabend if (curr_qp + xdp_qp > vi->max_queue_pairs) { 32599ce4e3d6SXuan Zhuo netdev_warn_once(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n", 3260672aafd5SJohn Fastabend curr_qp + xdp_qp, vi->max_queue_pairs); 326197c2c69eSXuan Zhuo xdp_qp = 0; 3262672aafd5SJohn Fastabend } 3263672aafd5SJohn Fastabend 326403aa6d34SToshiaki Makita old_prog = rtnl_dereference(vi->rq[0].xdp_prog); 326503aa6d34SToshiaki Makita if (!prog && !old_prog) 326603aa6d34SToshiaki Makita return 0; 326703aa6d34SToshiaki Makita 326885192dbfSAndrii Nakryiko if (prog) 326985192dbfSAndrii Nakryiko bpf_prog_add(prog, vi->max_queue_pairs - 1); 32702de2f7f4SJohn Fastabend 32714941d472SJason Wang /* Make sure NAPI is not using any XDP TX queues for RX. */ 3272534da5e8SToshiaki Makita if (netif_running(dev)) { 3273534da5e8SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 32744941d472SJason Wang napi_disable(&vi->rq[i].napi); 3275534da5e8SToshiaki Makita virtnet_napi_tx_disable(&vi->sq[i].napi); 3276534da5e8SToshiaki Makita } 3277534da5e8SToshiaki Makita } 32782de2f7f4SJohn Fastabend 327903aa6d34SToshiaki Makita if (!prog) { 328003aa6d34SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 328103aa6d34SToshiaki Makita rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 328203aa6d34SToshiaki Makita if (i == 0) 328303aa6d34SToshiaki Makita virtnet_restore_guest_offloads(vi); 328403aa6d34SToshiaki Makita } 328503aa6d34SToshiaki Makita synchronize_net(); 328603aa6d34SToshiaki Makita } 328703aa6d34SToshiaki Makita 32884941d472SJason Wang err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 32894941d472SJason Wang if (err) 32904941d472SJason Wang goto err; 3291188313c1SToshiaki Makita netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 32924941d472SJason Wang vi->xdp_queue_pairs = xdp_qp; 3293f600b690SJohn Fastabend 329403aa6d34SToshiaki Makita if (prog) { 329597c2c69eSXuan Zhuo vi->xdp_enabled = true; 3296f600b690SJohn Fastabend for (i = 0; i < vi->max_queue_pairs; i++) { 3297f600b690SJohn Fastabend rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 329803aa6d34SToshiaki Makita if (i == 0 && !old_prog) 32993f93522fSJason Wang virtnet_clear_guest_offloads(vi); 33003f93522fSJason Wang } 330166c0e13aSMarek Majtyka if (!old_prog) 330230bbf891SLorenzo Bianconi xdp_features_set_redirect_target(dev, true); 330397c2c69eSXuan Zhuo } else { 330466c0e13aSMarek Majtyka xdp_features_clear_redirect_target(dev); 330597c2c69eSXuan Zhuo vi->xdp_enabled = false; 330603aa6d34SToshiaki Makita } 330703aa6d34SToshiaki Makita 330803aa6d34SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 3309f600b690SJohn Fastabend if (old_prog) 3310f600b690SJohn Fastabend bpf_prog_put(old_prog); 3311534da5e8SToshiaki Makita if (netif_running(dev)) { 33124941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 3313534da5e8SToshiaki Makita virtnet_napi_tx_enable(vi, vi->sq[i].vq, 3314534da5e8SToshiaki Makita &vi->sq[i].napi); 3315534da5e8SToshiaki Makita } 3316f600b690SJohn Fastabend } 3317f600b690SJohn Fastabend 3318f600b690SJohn Fastabend return 0; 33192de2f7f4SJohn Fastabend 33204941d472SJason Wang err: 332103aa6d34SToshiaki Makita if (!prog) { 332203aa6d34SToshiaki Makita virtnet_clear_guest_offloads(vi); 33234941d472SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 332403aa6d34SToshiaki Makita rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); 332503aa6d34SToshiaki Makita } 332603aa6d34SToshiaki Makita 33278be4d9a4SToshiaki Makita if (netif_running(dev)) { 3328534da5e8SToshiaki Makita for (i = 0; i < vi->max_queue_pairs; i++) { 33294941d472SJason Wang virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 3330534da5e8SToshiaki Makita virtnet_napi_tx_enable(vi, vi->sq[i].vq, 3331534da5e8SToshiaki Makita &vi->sq[i].napi); 3332534da5e8SToshiaki Makita } 33338be4d9a4SToshiaki Makita } 33342de2f7f4SJohn Fastabend if (prog) 33352de2f7f4SJohn Fastabend bpf_prog_sub(prog, vi->max_queue_pairs - 1); 33362de2f7f4SJohn Fastabend return err; 3337f600b690SJohn Fastabend } 3338f600b690SJohn Fastabend 3339f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 3340f600b690SJohn Fastabend { 3341f600b690SJohn Fastabend switch (xdp->command) { 3342f600b690SJohn Fastabend case XDP_SETUP_PROG: 33439861ce03SJakub Kicinski return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 3344f600b690SJohn Fastabend default: 3345f600b690SJohn Fastabend return -EINVAL; 3346f600b690SJohn Fastabend } 3347f600b690SJohn Fastabend } 3348f600b690SJohn Fastabend 3349ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 3350ba5e4426SSridhar Samudrala size_t len) 3351ba5e4426SSridhar Samudrala { 3352ba5e4426SSridhar Samudrala struct virtnet_info *vi = netdev_priv(dev); 3353ba5e4426SSridhar Samudrala int ret; 3354ba5e4426SSridhar Samudrala 3355ba5e4426SSridhar Samudrala if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 3356ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 3357ba5e4426SSridhar Samudrala 3358ba5e4426SSridhar Samudrala ret = snprintf(buf, len, "sby"); 3359ba5e4426SSridhar Samudrala if (ret >= len) 3360ba5e4426SSridhar Samudrala return -EOPNOTSUPP; 3361ba5e4426SSridhar Samudrala 3362ba5e4426SSridhar Samudrala return 0; 3363ba5e4426SSridhar Samudrala } 3364ba5e4426SSridhar Samudrala 3365a02e8964SWillem de Bruijn static int virtnet_set_features(struct net_device *dev, 3366a02e8964SWillem de Bruijn netdev_features_t features) 3367a02e8964SWillem de Bruijn { 3368a02e8964SWillem de Bruijn struct virtnet_info *vi = netdev_priv(dev); 3369cf8691cbSMichael S. Tsirkin u64 offloads; 3370a02e8964SWillem de Bruijn int err; 3371a02e8964SWillem de Bruijn 3372dbcf24d1SJason Wang if ((dev->features ^ features) & NETIF_F_GRO_HW) { 337397c2c69eSXuan Zhuo if (vi->xdp_enabled) 3374a02e8964SWillem de Bruijn return -EBUSY; 3375a02e8964SWillem de Bruijn 3376dbcf24d1SJason Wang if (features & NETIF_F_GRO_HW) 3377cf8691cbSMichael S. Tsirkin offloads = vi->guest_offloads_capable; 3378a02e8964SWillem de Bruijn else 3379cf8691cbSMichael S. Tsirkin offloads = vi->guest_offloads_capable & 3380dbcf24d1SJason Wang ~GUEST_OFFLOAD_GRO_HW_MASK; 3381a02e8964SWillem de Bruijn 3382a02e8964SWillem de Bruijn err = virtnet_set_guest_offloads(vi, offloads); 3383a02e8964SWillem de Bruijn if (err) 3384a02e8964SWillem de Bruijn return err; 33853618ad2aSTonghao Zhang vi->guest_offloads = offloads; 3386cf8691cbSMichael S. Tsirkin } 3387cf8691cbSMichael S. Tsirkin 3388c7114b12SAndrew Melnychenko if ((dev->features ^ features) & NETIF_F_RXHASH) { 3389c7114b12SAndrew Melnychenko if (features & NETIF_F_RXHASH) 3390c1170820SAndrew Melnychenko vi->ctrl->rss.hash_types = vi->rss_hash_types_saved; 3391c7114b12SAndrew Melnychenko else 3392c7114b12SAndrew Melnychenko vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE; 3393c7114b12SAndrew Melnychenko 3394c7114b12SAndrew Melnychenko if (!virtnet_commit_rss_command(vi)) 3395c7114b12SAndrew Melnychenko return -EINVAL; 3396c7114b12SAndrew Melnychenko } 3397c7114b12SAndrew Melnychenko 3398a02e8964SWillem de Bruijn return 0; 3399a02e8964SWillem de Bruijn } 3400a02e8964SWillem de Bruijn 3401a520794bSTony Lu static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue) 3402a520794bSTony Lu { 3403a520794bSTony Lu struct virtnet_info *priv = netdev_priv(dev); 3404a520794bSTony Lu struct send_queue *sq = &priv->sq[txqueue]; 3405a520794bSTony Lu struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue); 3406a520794bSTony Lu 3407a520794bSTony Lu u64_stats_update_begin(&sq->stats.syncp); 3408a520794bSTony Lu sq->stats.tx_timeouts++; 3409a520794bSTony Lu u64_stats_update_end(&sq->stats.syncp); 3410a520794bSTony Lu 3411a520794bSTony Lu netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n", 3412a520794bSTony Lu txqueue, sq->name, sq->vq->index, sq->vq->name, 34135337824fSEric Dumazet jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start))); 3414a520794bSTony Lu } 3415a520794bSTony Lu 341676288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 341776288b4eSStephen Hemminger .ndo_open = virtnet_open, 341876288b4eSStephen Hemminger .ndo_stop = virtnet_close, 341976288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 342076288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 34219c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 34222af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 34233fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 34241824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 34251824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 3426f4e63525SJakub Kicinski .ndo_bpf = virtnet_xdp, 3427186b3c99SJason Wang .ndo_xdp_xmit = virtnet_xdp_xmit, 34282836b4f2SVlad Yasevich .ndo_features_check = passthru_features_check, 3429ba5e4426SSridhar Samudrala .ndo_get_phys_port_name = virtnet_get_phys_port_name, 3430a02e8964SWillem de Bruijn .ndo_set_features = virtnet_set_features, 3431a520794bSTony Lu .ndo_tx_timeout = virtnet_tx_timeout, 343276288b4eSStephen Hemminger }; 343376288b4eSStephen Hemminger 3434586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 34359f4d26d0SMark McLoughlin { 3436586d17c5SJason Wang struct virtnet_info *vi = 3437586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 34389f4d26d0SMark McLoughlin u16 v; 34399f4d26d0SMark McLoughlin 3440855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 3441855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 3442507613bfSMichael S. Tsirkin return; 3443586d17c5SJason Wang 3444586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 3445ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 3446586d17c5SJason Wang virtnet_ack_link_announce(vi); 3447586d17c5SJason Wang } 34489f4d26d0SMark McLoughlin 34499f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 34509f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 34519f4d26d0SMark McLoughlin 34529f4d26d0SMark McLoughlin if (vi->status == v) 3453507613bfSMichael S. Tsirkin return; 34549f4d26d0SMark McLoughlin 34559f4d26d0SMark McLoughlin vi->status = v; 34569f4d26d0SMark McLoughlin 34579f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 3458faa9b39fSJason Baron virtnet_update_settings(vi); 34599f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 3460986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 34619f4d26d0SMark McLoughlin } else { 34629f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 3463986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 34649f4d26d0SMark McLoughlin } 34659f4d26d0SMark McLoughlin } 34669f4d26d0SMark McLoughlin 34679f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 34689f4d26d0SMark McLoughlin { 34699f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 34709f4d26d0SMark McLoughlin 34713b07e9caSTejun Heo schedule_work(&vi->config_work); 34729f4d26d0SMark McLoughlin } 34739f4d26d0SMark McLoughlin 3474986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 3475986a4f4dSJason Wang { 3476d4fb84eeSAndrey Vagin int i; 3477d4fb84eeSAndrey Vagin 3478ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 34795198d545SJakub Kicinski __netif_napi_del(&vi->rq[i].napi); 34805198d545SJakub Kicinski __netif_napi_del(&vi->sq[i].napi); 3481ab3971b1SJason Wang } 3482d4fb84eeSAndrey Vagin 34835198d545SJakub Kicinski /* We called __netif_napi_del(), 3484963abe5cSEric Dumazet * we need to respect an RCU grace period before freeing vi->rq 3485963abe5cSEric Dumazet */ 3486963abe5cSEric Dumazet synchronize_net(); 3487963abe5cSEric Dumazet 3488986a4f4dSJason Wang kfree(vi->rq); 3489986a4f4dSJason Wang kfree(vi->sq); 349012e57169SMichael S. Tsirkin kfree(vi->ctrl); 3491986a4f4dSJason Wang } 3492986a4f4dSJason Wang 349347315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi) 3494986a4f4dSJason Wang { 3495f600b690SJohn Fastabend struct bpf_prog *old_prog; 3496986a4f4dSJason Wang int i; 3497986a4f4dSJason Wang 3498986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 3499986a4f4dSJason Wang while (vi->rq[i].pages) 3500986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 3501f600b690SJohn Fastabend 3502f600b690SJohn Fastabend old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 3503f600b690SJohn Fastabend RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 3504f600b690SJohn Fastabend if (old_prog) 3505f600b690SJohn Fastabend bpf_prog_put(old_prog); 3506986a4f4dSJason Wang } 350747315329SJohn Fastabend } 350847315329SJohn Fastabend 350947315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi) 351047315329SJohn Fastabend { 351147315329SJohn Fastabend rtnl_lock(); 351247315329SJohn Fastabend _free_receive_bufs(vi); 3513f600b690SJohn Fastabend rtnl_unlock(); 3514986a4f4dSJason Wang } 3515986a4f4dSJason Wang 3516fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 3517fb51879dSMichael Dalton { 3518fb51879dSMichael Dalton int i; 3519fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 3520fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 3521fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 3522fb51879dSMichael Dalton } 3523fb51879dSMichael Dalton 35246e345f8cSXuan Zhuo static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf) 35256e345f8cSXuan Zhuo { 35266e345f8cSXuan Zhuo if (!is_xdp_frame(buf)) 35276e345f8cSXuan Zhuo dev_kfree_skb(buf); 35286e345f8cSXuan Zhuo else 35296e345f8cSXuan Zhuo xdp_return_frame(ptr_to_xdp(buf)); 35306e345f8cSXuan Zhuo } 35316e345f8cSXuan Zhuo 35326e345f8cSXuan Zhuo static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf) 35336e345f8cSXuan Zhuo { 35346e345f8cSXuan Zhuo struct virtnet_info *vi = vq->vdev->priv; 35356e345f8cSXuan Zhuo int i = vq2rxq(vq); 35366e345f8cSXuan Zhuo 35376e345f8cSXuan Zhuo if (vi->mergeable_rx_bufs) 35386e345f8cSXuan Zhuo put_page(virt_to_head_page(buf)); 35396e345f8cSXuan Zhuo else if (vi->big_packets) 35406e345f8cSXuan Zhuo give_pages(&vi->rq[i], buf); 35416e345f8cSXuan Zhuo else 35426e345f8cSXuan Zhuo put_page(virt_to_head_page(buf)); 35436e345f8cSXuan Zhuo } 35446e345f8cSXuan Zhuo 3545986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 3546986a4f4dSJason Wang { 3547986a4f4dSJason Wang void *buf; 3548986a4f4dSJason Wang int i; 3549986a4f4dSJason Wang 3550986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 3551986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 35526e345f8cSXuan Zhuo while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 35536e345f8cSXuan Zhuo virtnet_sq_free_unused_buf(vq, buf); 3554986a4f4dSJason Wang } 3555986a4f4dSJason Wang 3556986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 3557986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 35586e345f8cSXuan Zhuo while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 35596e345f8cSXuan Zhuo virtnet_rq_free_unused_buf(vq, buf); 3560986a4f4dSJason Wang } 3561ab7db917SMichael Dalton } 3562986a4f4dSJason Wang 3563e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 3564e9d7417bSJason Wang { 3565e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 3566e9d7417bSJason Wang 3567310974faSPeter Xu virtnet_clean_affinity(vi); 3568986a4f4dSJason Wang 3569e9d7417bSJason Wang vdev->config->del_vqs(vdev); 3570986a4f4dSJason Wang 3571986a4f4dSJason Wang virtnet_free_queues(vi); 3572986a4f4dSJason Wang } 3573986a4f4dSJason Wang 3574d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at 3575d85b758fSMichael S. Tsirkin * least one full packet? 3576d85b758fSMichael S. Tsirkin * Logic below assumes the mergeable buffer header is used. 3577d85b758fSMichael S. Tsirkin */ 3578d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 3579d85b758fSMichael S. Tsirkin { 3580c1ddc42dSAndrew Melnychenko const unsigned int hdr_len = vi->hdr_len; 3581d85b758fSMichael S. Tsirkin unsigned int rq_size = virtqueue_get_vring_size(vq); 3582d85b758fSMichael S. Tsirkin unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 3583d85b758fSMichael S. Tsirkin unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 3584d85b758fSMichael S. Tsirkin unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 3585d85b758fSMichael S. Tsirkin 3586f0c3192cSMichael S. Tsirkin return max(max(min_buf_len, hdr_len) - hdr_len, 3587f0c3192cSMichael S. Tsirkin (unsigned int)GOOD_PACKET_LEN); 3588d85b758fSMichael S. Tsirkin } 3589d85b758fSMichael S. Tsirkin 3590986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 3591986a4f4dSJason Wang { 3592986a4f4dSJason Wang vq_callback_t **callbacks; 3593986a4f4dSJason Wang struct virtqueue **vqs; 3594986a4f4dSJason Wang int ret = -ENOMEM; 3595986a4f4dSJason Wang int i, total_vqs; 3596986a4f4dSJason Wang const char **names; 3597d45b897bSMichael S. Tsirkin bool *ctx; 3598986a4f4dSJason Wang 3599986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 3600986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 3601986a4f4dSJason Wang * possible control vq. 3602986a4f4dSJason Wang */ 3603986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 3604986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 3605986a4f4dSJason Wang 3606986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 36076396bb22SKees Cook vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 3608986a4f4dSJason Wang if (!vqs) 3609986a4f4dSJason Wang goto err_vq; 36106da2ec56SKees Cook callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL); 3611986a4f4dSJason Wang if (!callbacks) 3612986a4f4dSJason Wang goto err_callback; 36136da2ec56SKees Cook names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL); 3614986a4f4dSJason Wang if (!names) 3615986a4f4dSJason Wang goto err_names; 3616192f68cfSJason Wang if (!vi->big_packets || vi->mergeable_rx_bufs) { 36176396bb22SKees Cook ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 3618d45b897bSMichael S. Tsirkin if (!ctx) 3619d45b897bSMichael S. Tsirkin goto err_ctx; 3620d45b897bSMichael S. Tsirkin } else { 3621d45b897bSMichael S. Tsirkin ctx = NULL; 3622d45b897bSMichael S. Tsirkin } 3623986a4f4dSJason Wang 3624986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 3625986a4f4dSJason Wang if (vi->has_cvq) { 3626986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 3627986a4f4dSJason Wang names[total_vqs - 1] = "control"; 3628986a4f4dSJason Wang } 3629986a4f4dSJason Wang 3630986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 3631986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 3632986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 3633986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 3634986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 3635986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 3636986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 3637986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 3638d45b897bSMichael S. Tsirkin if (ctx) 3639d45b897bSMichael S. Tsirkin ctx[rxq2vq(i)] = true; 3640986a4f4dSJason Wang } 3641986a4f4dSJason Wang 36422e9ca760SMichael S. Tsirkin ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks, 36432e9ca760SMichael S. Tsirkin names, ctx, NULL); 3644986a4f4dSJason Wang if (ret) 3645986a4f4dSJason Wang goto err_find; 3646986a4f4dSJason Wang 3647986a4f4dSJason Wang if (vi->has_cvq) { 3648986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 3649986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 3650f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 3651986a4f4dSJason Wang } 3652986a4f4dSJason Wang 3653986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 3654986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 3655d85b758fSMichael S. Tsirkin vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 3656986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 3657986a4f4dSJason Wang } 3658986a4f4dSJason Wang 36592fa3c8a8STonghao Zhang /* run here: ret == 0. */ 3660986a4f4dSJason Wang 3661986a4f4dSJason Wang 3662986a4f4dSJason Wang err_find: 3663d45b897bSMichael S. Tsirkin kfree(ctx); 3664d45b897bSMichael S. Tsirkin err_ctx: 3665986a4f4dSJason Wang kfree(names); 3666986a4f4dSJason Wang err_names: 3667986a4f4dSJason Wang kfree(callbacks); 3668986a4f4dSJason Wang err_callback: 3669986a4f4dSJason Wang kfree(vqs); 3670986a4f4dSJason Wang err_vq: 3671986a4f4dSJason Wang return ret; 3672986a4f4dSJason Wang } 3673986a4f4dSJason Wang 3674986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 3675986a4f4dSJason Wang { 3676986a4f4dSJason Wang int i; 3677986a4f4dSJason Wang 3678122b84a1SMax Gurtovoy if (vi->has_cvq) { 367912e57169SMichael S. Tsirkin vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 368012e57169SMichael S. Tsirkin if (!vi->ctrl) 368112e57169SMichael S. Tsirkin goto err_ctrl; 3682122b84a1SMax Gurtovoy } else { 3683122b84a1SMax Gurtovoy vi->ctrl = NULL; 3684122b84a1SMax Gurtovoy } 36856396bb22SKees Cook vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 3686986a4f4dSJason Wang if (!vi->sq) 3687986a4f4dSJason Wang goto err_sq; 36886396bb22SKees Cook vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 3689008d4278SAmerigo Wang if (!vi->rq) 3690986a4f4dSJason Wang goto err_rq; 3691986a4f4dSJason Wang 3692986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 3693986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 3694986a4f4dSJason Wang vi->rq[i].pages = NULL; 3695d484735dSJakub Kicinski netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll, 3696986a4f4dSJason Wang napi_weight); 36978d602e1aSJakub Kicinski netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi, 36988d602e1aSJakub Kicinski virtnet_poll_tx, 3699b92f1e67SWillem de Bruijn napi_tx ? napi_weight : 0); 3700986a4f4dSJason Wang 3701986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 37025377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 3703986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 3704d7dfc5cfSToshiaki Makita 3705d7dfc5cfSToshiaki Makita u64_stats_init(&vi->rq[i].stats.syncp); 3706d7dfc5cfSToshiaki Makita u64_stats_init(&vi->sq[i].stats.syncp); 3707986a4f4dSJason Wang } 3708986a4f4dSJason Wang 3709986a4f4dSJason Wang return 0; 3710986a4f4dSJason Wang 3711986a4f4dSJason Wang err_rq: 3712986a4f4dSJason Wang kfree(vi->sq); 3713986a4f4dSJason Wang err_sq: 371412e57169SMichael S. Tsirkin kfree(vi->ctrl); 371512e57169SMichael S. Tsirkin err_ctrl: 3716986a4f4dSJason Wang return -ENOMEM; 3717e9d7417bSJason Wang } 3718e9d7417bSJason Wang 37193f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 37203f9c10b0SAmit Shah { 3721986a4f4dSJason Wang int ret; 37223f9c10b0SAmit Shah 3723986a4f4dSJason Wang /* Allocate send & receive queues */ 3724986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 3725986a4f4dSJason Wang if (ret) 3726986a4f4dSJason Wang goto err; 37273f9c10b0SAmit Shah 3728986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 3729986a4f4dSJason Wang if (ret) 3730986a4f4dSJason Wang goto err_free; 37313f9c10b0SAmit Shah 3732a0d1d0f4SSebastian Andrzej Siewior cpus_read_lock(); 37338898c21cSWanlong Gao virtnet_set_affinity(vi); 3734a0d1d0f4SSebastian Andrzej Siewior cpus_read_unlock(); 373547be2479SWanlong Gao 37363f9c10b0SAmit Shah return 0; 3737986a4f4dSJason Wang 3738986a4f4dSJason Wang err_free: 3739986a4f4dSJason Wang virtnet_free_queues(vi); 3740986a4f4dSJason Wang err: 3741986a4f4dSJason Wang return ret; 37423f9c10b0SAmit Shah } 37433f9c10b0SAmit Shah 3744fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 3745fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 3746718ad681Sstephen hemminger char *buf) 3747fbf28d78SMichael Dalton { 3748fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 3749fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 37503cc81a9aSJason Wang unsigned int headroom = virtnet_get_headroom(vi); 37513cc81a9aSJason Wang unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 37525377d758SJohannes Berg struct ewma_pkt_len *avg; 3753fbf28d78SMichael Dalton 3754fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 3755fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 3756d85b758fSMichael S. Tsirkin return sprintf(buf, "%u\n", 37573cc81a9aSJason Wang get_mergeable_buf_len(&vi->rq[queue_index], avg, 37583cc81a9aSJason Wang SKB_DATA_ALIGN(headroom + tailroom))); 3759fbf28d78SMichael Dalton } 3760fbf28d78SMichael Dalton 3761fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 3762fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 3763fbf28d78SMichael Dalton 3764fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 3765fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 3766fbf28d78SMichael Dalton NULL 3767fbf28d78SMichael Dalton }; 3768fbf28d78SMichael Dalton 3769fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 3770fbf28d78SMichael Dalton .name = "virtio_net", 3771fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 3772fbf28d78SMichael Dalton }; 3773fbf28d78SMichael Dalton #endif 3774fbf28d78SMichael Dalton 3775892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 3776892d6eb1SJason Wang unsigned int fbit, 3777892d6eb1SJason Wang const char *fname, const char *dname) 3778892d6eb1SJason Wang { 3779892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 3780892d6eb1SJason Wang return false; 3781892d6eb1SJason Wang 3782892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 3783892d6eb1SJason Wang fname, dname); 3784892d6eb1SJason Wang 3785892d6eb1SJason Wang return true; 3786892d6eb1SJason Wang } 3787892d6eb1SJason Wang 3788892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 3789892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 3790892d6eb1SJason Wang 3791892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 3792892d6eb1SJason Wang { 3793892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 3794892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 3795892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3796892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 3797892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3798892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 3799892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 3800892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 3801892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 3802c7114b12SAndrew Melnychenko "VIRTIO_NET_F_CTRL_VQ") || 3803c7114b12SAndrew Melnychenko VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS, 380491f41f01SAndrew Melnychenko "VIRTIO_NET_F_CTRL_VQ") || 380591f41f01SAndrew Melnychenko VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT, 3806699b045aSAlvaro Karsz "VIRTIO_NET_F_CTRL_VQ") || 3807699b045aSAlvaro Karsz VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL, 3808892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 3809892d6eb1SJason Wang return false; 3810892d6eb1SJason Wang } 3811892d6eb1SJason Wang 3812892d6eb1SJason Wang return true; 3813892d6eb1SJason Wang } 3814892d6eb1SJason Wang 3815d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU 3816d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU 3817d0c2c997SJarod Wilson 3818fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev) 3819296f96fcSRusty Russell { 38206ba42248SMichael S. Tsirkin if (!vdev->config->get) { 38216ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 38226ba42248SMichael S. Tsirkin __func__); 38236ba42248SMichael S. Tsirkin return -EINVAL; 38246ba42248SMichael S. Tsirkin } 38256ba42248SMichael S. Tsirkin 3826892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 3827892d6eb1SJason Wang return -EINVAL; 3828892d6eb1SJason Wang 3829fe36cbe0SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 3830fe36cbe0SMichael S. Tsirkin int mtu = virtio_cread16(vdev, 3831fe36cbe0SMichael S. Tsirkin offsetof(struct virtio_net_config, 3832fe36cbe0SMichael S. Tsirkin mtu)); 3833fe36cbe0SMichael S. Tsirkin if (mtu < MIN_MTU) 3834fe36cbe0SMichael S. Tsirkin __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 3835fe36cbe0SMichael S. Tsirkin } 3836fe36cbe0SMichael S. Tsirkin 38377c06458cSLaurent Vivier if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) && 38387c06458cSLaurent Vivier !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 38397c06458cSLaurent Vivier dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby"); 38407c06458cSLaurent Vivier __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY); 38417c06458cSLaurent Vivier } 38427c06458cSLaurent Vivier 3843fe36cbe0SMichael S. Tsirkin return 0; 3844fe36cbe0SMichael S. Tsirkin } 3845fe36cbe0SMichael S. Tsirkin 384646cd26f4SGavin Li static bool virtnet_check_guest_gso(const struct virtnet_info *vi) 384746cd26f4SGavin Li { 384846cd26f4SGavin Li return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 384946cd26f4SGavin Li virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 385046cd26f4SGavin Li virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 3851418044e1SAndrew Melnychenko virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 3852418044e1SAndrew Melnychenko (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) && 3853418044e1SAndrew Melnychenko virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6)); 385446cd26f4SGavin Li } 385546cd26f4SGavin Li 38564959aebbSGavin Li static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) 38574959aebbSGavin Li { 38584959aebbSGavin Li bool guest_gso = virtnet_check_guest_gso(vi); 38594959aebbSGavin Li 38604959aebbSGavin Li /* If device can receive ANY guest GSO packets, regardless of mtu, 38614959aebbSGavin Li * allocate packets of maximum size, otherwise limit it to only 38624959aebbSGavin Li * mtu size worth only. 38634959aebbSGavin Li */ 38644959aebbSGavin Li if (mtu > ETH_DATA_LEN || guest_gso) { 38654959aebbSGavin Li vi->big_packets = true; 38664959aebbSGavin Li vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE); 38674959aebbSGavin Li } 38684959aebbSGavin Li } 38694959aebbSGavin Li 3870fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev) 3871fe36cbe0SMichael S. Tsirkin { 3872d7dfc5cfSToshiaki Makita int i, err = -ENOMEM; 3873fe36cbe0SMichael S. Tsirkin struct net_device *dev; 3874fe36cbe0SMichael S. Tsirkin struct virtnet_info *vi; 3875fe36cbe0SMichael S. Tsirkin u16 max_queue_pairs; 38764959aebbSGavin Li int mtu = 0; 3877fe36cbe0SMichael S. Tsirkin 3878c7114b12SAndrew Melnychenko /* Find if host supports multiqueue/rss virtio_net device */ 3879c7114b12SAndrew Melnychenko max_queue_pairs = 1; 3880c7114b12SAndrew Melnychenko if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) 3881c7114b12SAndrew Melnychenko max_queue_pairs = 3882c7114b12SAndrew Melnychenko virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs)); 3883986a4f4dSJason Wang 3884986a4f4dSJason Wang /* We need at least 2 queue's */ 3885c7114b12SAndrew Melnychenko if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 3886986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 3887986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 3888986a4f4dSJason Wang max_queue_pairs = 1; 3889296f96fcSRusty Russell 3890296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 3891986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 3892296f96fcSRusty Russell if (!dev) 3893296f96fcSRusty Russell return -ENOMEM; 3894296f96fcSRusty Russell 3895296f96fcSRusty Russell /* Set up network device as normal. */ 3896ab5bd583SXuan Zhuo dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE | 3897ab5bd583SXuan Zhuo IFF_TX_SKB_NO_LINEAR; 389876288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 3899296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 39003fa2a1dfSstephen hemminger 39017ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 3902296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 3903296f96fcSRusty Russell 3904296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 390598e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 3906296f96fcSRusty Russell /* This opens up the world of extra features. */ 390748900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 390898e778c9SMichał Mirosław if (csum) 390948900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 391098e778c9SMichał Mirosław 391198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 3912e078de03SDavid S. Miller dev->hw_features |= NETIF_F_TSO 391334a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 391434a48579SRusty Russell } 39155539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 391698e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 391798e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 391898e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 391998e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 392098e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 392198e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 3922418044e1SAndrew Melnychenko if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO)) 3923418044e1SAndrew Melnychenko dev->hw_features |= NETIF_F_GSO_UDP_L4; 392498e778c9SMichał Mirosław 392541f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 392641f2f127SJason Wang 392798e778c9SMichał Mirosław if (gso) 3928e078de03SDavid S. Miller dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 392998e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 3930296f96fcSRusty Russell } 39314f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 39324f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 3933a02e8964SWillem de Bruijn if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 3934a02e8964SWillem de Bruijn virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)) 3935dbcf24d1SJason Wang dev->features |= NETIF_F_GRO_HW; 3936cf8691cbSMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) 3937dbcf24d1SJason Wang dev->hw_features |= NETIF_F_GRO_HW; 3938296f96fcSRusty Russell 39394fda8302SJason Wang dev->vlan_features = dev->features; 394066c0e13aSMarek Majtyka dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT; 39414fda8302SJason Wang 3942d0c2c997SJarod Wilson /* MTU range: 68 - 65535 */ 3943d0c2c997SJarod Wilson dev->min_mtu = MIN_MTU; 3944d0c2c997SJarod Wilson dev->max_mtu = MAX_MTU; 3945d0c2c997SJarod Wilson 3946296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 3947f2edaa4aSJakub Kicinski if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 3948f2edaa4aSJakub Kicinski u8 addr[ETH_ALEN]; 3949f2edaa4aSJakub Kicinski 3950855e0c52SRusty Russell virtio_cread_bytes(vdev, 3951a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 3952f2edaa4aSJakub Kicinski addr, ETH_ALEN); 3953f2edaa4aSJakub Kicinski eth_hw_addr_set(dev, addr); 3954f2edaa4aSJakub Kicinski } else { 3955f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 39569f62d221SLaurent Vivier dev_info(&vdev->dev, "Assigned random MAC address %pM\n", 39579f62d221SLaurent Vivier dev->dev_addr); 3958f2edaa4aSJakub Kicinski } 3959296f96fcSRusty Russell 3960296f96fcSRusty Russell /* Set up our device-specific information */ 3961296f96fcSRusty Russell vi = netdev_priv(dev); 3962296f96fcSRusty Russell vi->dev = dev; 3963296f96fcSRusty Russell vi->vdev = vdev; 3964d9d5dcc8SChristian Borntraeger vdev->priv = vi; 3965827da44cSJohn Stultz 3966586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 39675a159128SJason Wang spin_lock_init(&vi->refill_lock); 3968296f96fcSRusty Russell 396930bbf891SLorenzo Bianconi if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) { 39703f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 397130bbf891SLorenzo Bianconi dev->xdp_features |= NETDEV_XDP_ACT_RX_SG; 397230bbf891SLorenzo Bianconi } 39733f2c31d9SMark McLoughlin 3974699b045aSAlvaro Karsz if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { 3975699b045aSAlvaro Karsz vi->rx_usecs = 0; 3976699b045aSAlvaro Karsz vi->tx_usecs = 0; 3977699b045aSAlvaro Karsz vi->tx_max_packets = 0; 3978699b045aSAlvaro Karsz vi->rx_max_packets = 0; 3979699b045aSAlvaro Karsz } 3980699b045aSAlvaro Karsz 398191f41f01SAndrew Melnychenko if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) 398291f41f01SAndrew Melnychenko vi->has_rss_hash_report = true; 398391f41f01SAndrew Melnychenko 398491f41f01SAndrew Melnychenko if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) 3985c7114b12SAndrew Melnychenko vi->has_rss = true; 398691f41f01SAndrew Melnychenko 398791f41f01SAndrew Melnychenko if (vi->has_rss || vi->has_rss_hash_report) { 3988c7114b12SAndrew Melnychenko vi->rss_indir_table_size = 3989c7114b12SAndrew Melnychenko virtio_cread16(vdev, offsetof(struct virtio_net_config, 3990c7114b12SAndrew Melnychenko rss_max_indirection_table_length)); 3991c7114b12SAndrew Melnychenko vi->rss_key_size = 3992c7114b12SAndrew Melnychenko virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size)); 3993c7114b12SAndrew Melnychenko 3994c7114b12SAndrew Melnychenko vi->rss_hash_types_supported = 3995c7114b12SAndrew Melnychenko virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types)); 3996c7114b12SAndrew Melnychenko vi->rss_hash_types_supported &= 3997c7114b12SAndrew Melnychenko ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX | 3998c7114b12SAndrew Melnychenko VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 3999c7114b12SAndrew Melnychenko VIRTIO_NET_RSS_HASH_TYPE_UDP_EX); 4000c7114b12SAndrew Melnychenko 4001c7114b12SAndrew Melnychenko dev->hw_features |= NETIF_F_RXHASH; 4002c7114b12SAndrew Melnychenko } 400391f41f01SAndrew Melnychenko 400491f41f01SAndrew Melnychenko if (vi->has_rss_hash_report) 400591f41f01SAndrew Melnychenko vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); 400691f41f01SAndrew Melnychenko else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 4007d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 4008012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 4009012873d0SMichael S. Tsirkin else 4010012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 4011012873d0SMichael S. Tsirkin 401275993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 401375993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 4014e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 4015e7428e95SMichael S. Tsirkin 4016986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 4017986a4f4dSJason Wang vi->has_cvq = true; 4018986a4f4dSJason Wang 401914de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 402014de9d11SAaron Conole mtu = virtio_cread16(vdev, 402114de9d11SAaron Conole offsetof(struct virtio_net_config, 402214de9d11SAaron Conole mtu)); 402393a205eeSAaron Conole if (mtu < dev->min_mtu) { 4024fe36cbe0SMichael S. Tsirkin /* Should never trigger: MTU was previously validated 4025fe36cbe0SMichael S. Tsirkin * in virtnet_validate. 4026fe36cbe0SMichael S. Tsirkin */ 40277934b481SYuval Shaia dev_err(&vdev->dev, 40287934b481SYuval Shaia "device MTU appears to have changed it is now %d < %d", 40297934b481SYuval Shaia mtu, dev->min_mtu); 4030411ea23aSDan Carpenter err = -EINVAL; 4031d7dfc5cfSToshiaki Makita goto free; 4032fe36cbe0SMichael S. Tsirkin } 4033fe36cbe0SMichael S. Tsirkin 4034d0c2c997SJarod Wilson dev->mtu = mtu; 403593a205eeSAaron Conole dev->max_mtu = mtu; 403614de9d11SAaron Conole } 403714de9d11SAaron Conole 40384959aebbSGavin Li virtnet_set_big_packets(vi, mtu); 40394959aebbSGavin Li 4040012873d0SMichael S. Tsirkin if (vi->any_header_sg) 4041012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 40426ebbc1a6SZhangjie \(HZ\) 404344900010SJason Wang /* Enable multiqueue by default */ 404444900010SJason Wang if (num_online_cpus() >= max_queue_pairs) 404544900010SJason Wang vi->curr_queue_pairs = max_queue_pairs; 404644900010SJason Wang else 404744900010SJason Wang vi->curr_queue_pairs = num_online_cpus(); 4048986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 4049986a4f4dSJason Wang 4050986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 40513f9c10b0SAmit Shah err = init_vqs(vi); 4052d2a7dddaSMichael S. Tsirkin if (err) 4053d7dfc5cfSToshiaki Makita goto free; 4054d2a7dddaSMichael S. Tsirkin 4055fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 4056fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 4057fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 4058fbf28d78SMichael Dalton #endif 40590f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 40600f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 4061986a4f4dSJason Wang 40622e9ca760SMichael S. Tsirkin virtnet_init_settings(dev); 40632e9ca760SMichael S. Tsirkin 4064ba5e4426SSridhar Samudrala if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 4065ba5e4426SSridhar Samudrala vi->failover = net_failover_create(vi->dev); 40664b8e6ac4SWei Yongjun if (IS_ERR(vi->failover)) { 40674b8e6ac4SWei Yongjun err = PTR_ERR(vi->failover); 4068ba5e4426SSridhar Samudrala goto free_vqs; 4069ba5e4426SSridhar Samudrala } 40704b8e6ac4SWei Yongjun } 4071ba5e4426SSridhar Samudrala 407291f41f01SAndrew Melnychenko if (vi->has_rss || vi->has_rss_hash_report) 4073c7114b12SAndrew Melnychenko virtnet_init_default_rss(vi); 4074c7114b12SAndrew Melnychenko 407550c0ada6SJason Wang /* serialize netdev register + virtio_device_ready() with ndo_open() */ 407650c0ada6SJason Wang rtnl_lock(); 407750c0ada6SJason Wang 407850c0ada6SJason Wang err = register_netdevice(dev); 4079296f96fcSRusty Russell if (err) { 4080296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 408150c0ada6SJason Wang rtnl_unlock(); 4082ba5e4426SSridhar Samudrala goto free_failover; 4083296f96fcSRusty Russell } 4084b3369c1fSRusty Russell 40854baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 40864baf1e33SMichael S. Tsirkin 40879f62d221SLaurent Vivier /* a random MAC address has been assigned, notify the device. 40889f62d221SLaurent Vivier * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there 40899f62d221SLaurent Vivier * because many devices work fine without getting MAC explicitly 40909f62d221SLaurent Vivier */ 40919f62d221SLaurent Vivier if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 40929f62d221SLaurent Vivier virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 40939f62d221SLaurent Vivier struct scatterlist sg; 40949f62d221SLaurent Vivier 40959f62d221SLaurent Vivier sg_init_one(&sg, dev->dev_addr, dev->addr_len); 40969f62d221SLaurent Vivier if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 40979f62d221SLaurent Vivier VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 40989f62d221SLaurent Vivier pr_debug("virtio_net: setting MAC address failed\n"); 40999f62d221SLaurent Vivier rtnl_unlock(); 41009f62d221SLaurent Vivier err = -EINVAL; 41019f62d221SLaurent Vivier goto free_unregister_netdev; 41029f62d221SLaurent Vivier } 41039f62d221SLaurent Vivier } 41049f62d221SLaurent Vivier 410550c0ada6SJason Wang rtnl_unlock(); 410650c0ada6SJason Wang 41078017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 41088de4b2f3SWanlong Gao if (err) { 41098de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 4110f00e35e2Swangyunjian goto free_unregister_netdev; 41118de4b2f3SWanlong Gao } 41128de4b2f3SWanlong Gao 4113a220871bSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 411444900010SJason Wang 4115167c25e4SJason Wang /* Assume link up if device can't report link status, 4116167c25e4SJason Wang otherwise get link status from config. */ 4117167c25e4SJason Wang netif_carrier_off(dev); 4118bda7fab5SJay Vosburgh if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 41193b07e9caSTejun Heo schedule_work(&vi->config_work); 4120167c25e4SJason Wang } else { 4121167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 4122faa9b39fSJason Baron virtnet_update_settings(vi); 41234783256eSPantelis Koukousoulas netif_carrier_on(dev); 4124167c25e4SJason Wang } 41259f4d26d0SMark McLoughlin 41263f93522fSJason Wang for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 41273f93522fSJason Wang if (virtio_has_feature(vi->vdev, guest_offloads[i])) 41283f93522fSJason Wang set_bit(guest_offloads[i], &vi->guest_offloads); 4129a02e8964SWillem de Bruijn vi->guest_offloads_capable = vi->guest_offloads; 41303f93522fSJason Wang 4131986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 4132986a4f4dSJason Wang dev->name, max_queue_pairs); 4133986a4f4dSJason Wang 4134296f96fcSRusty Russell return 0; 4135296f96fcSRusty Russell 4136f00e35e2Swangyunjian free_unregister_netdev: 4137b3369c1fSRusty Russell unregister_netdev(dev); 4138ba5e4426SSridhar Samudrala free_failover: 4139ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 4140d2a7dddaSMichael S. Tsirkin free_vqs: 4141b0686565SLi Zetao virtio_reset_device(vdev); 4142986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 4143fb51879dSMichael Dalton free_receive_page_frags(vi); 4144e9d7417bSJason Wang virtnet_del_vqs(vi); 4145296f96fcSRusty Russell free: 4146296f96fcSRusty Russell free_netdev(dev); 4147296f96fcSRusty Russell return err; 4148296f96fcSRusty Russell } 4149296f96fcSRusty Russell 415004486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 4151296f96fcSRusty Russell { 4152d9679d00SMichael S. Tsirkin virtio_reset_device(vi->vdev); 4153830a8a97SShirley Ma 4154830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 41559ab86bbcSShirley Ma free_unused_bufs(vi); 4156fb6813f4SRusty Russell 4157986a4f4dSJason Wang free_receive_bufs(vi); 4158d2a7dddaSMichael S. Tsirkin 4159fb51879dSMichael Dalton free_receive_page_frags(vi); 4160fb51879dSMichael Dalton 4161986a4f4dSJason Wang virtnet_del_vqs(vi); 416204486ed0SAmit Shah } 416304486ed0SAmit Shah 41648cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 416504486ed0SAmit Shah { 416604486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 416704486ed0SAmit Shah 41688017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 41698de4b2f3SWanlong Gao 4170102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 4171102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 4172586d17c5SJason Wang 417304486ed0SAmit Shah unregister_netdev(vi->dev); 417404486ed0SAmit Shah 4175ba5e4426SSridhar Samudrala net_failover_destroy(vi->failover); 4176ba5e4426SSridhar Samudrala 417704486ed0SAmit Shah remove_vq_common(vi); 4178fb6813f4SRusty Russell 417974b2553fSRusty Russell free_netdev(vi->dev); 4180296f96fcSRusty Russell } 4181296f96fcSRusty Russell 418267a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 41830741bcb5SAmit Shah { 41840741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 41850741bcb5SAmit Shah 41868017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 41879fe7bfceSJohn Fastabend virtnet_freeze_down(vdev); 41880741bcb5SAmit Shah remove_vq_common(vi); 41890741bcb5SAmit Shah 41900741bcb5SAmit Shah return 0; 41910741bcb5SAmit Shah } 41920741bcb5SAmit Shah 419367a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 41940741bcb5SAmit Shah { 41950741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 41969fe7bfceSJohn Fastabend int err; 41970741bcb5SAmit Shah 41989fe7bfceSJohn Fastabend err = virtnet_restore_up(vdev); 41990741bcb5SAmit Shah if (err) 42000741bcb5SAmit Shah return err; 4201986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 4202986a4f4dSJason Wang 42038017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 42043f2869caSXie Yongji if (err) { 42053f2869caSXie Yongji virtnet_freeze_down(vdev); 42063f2869caSXie Yongji remove_vq_common(vi); 4207ec9debbdSJason Wang return err; 42083f2869caSXie Yongji } 4209ec9debbdSJason Wang 42100741bcb5SAmit Shah return 0; 42110741bcb5SAmit Shah } 42120741bcb5SAmit Shah 4213296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 4214296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 4215296f96fcSRusty Russell { 0 }, 4216296f96fcSRusty Russell }; 4217296f96fcSRusty Russell 4218f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \ 4219f3358507SMichael S. Tsirkin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 4220f3358507SMichael S. Tsirkin VIRTIO_NET_F_MAC, \ 4221f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 4222f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 4223f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 4224418044e1SAndrew Melnychenko VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \ 4225f3358507SMichael S. Tsirkin VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 4226f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 4227f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 4228f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_MAC_ADDR, \ 4229faa9b39fSJason Baron VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 4230c7114b12SAndrew Melnychenko VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \ 4231699b045aSAlvaro Karsz VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL 4232f3358507SMichael S. Tsirkin 4233c45a6816SRusty Russell static unsigned int features[] = { 4234f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 4235f3358507SMichael S. Tsirkin }; 4236f3358507SMichael S. Tsirkin 4237f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = { 4238f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 4239f3358507SMichael S. Tsirkin VIRTIO_NET_F_GSO, 4240e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 4241c45a6816SRusty Russell }; 4242c45a6816SRusty Russell 424322402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 4244c45a6816SRusty Russell .feature_table = features, 4245c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 4246f3358507SMichael S. Tsirkin .feature_table_legacy = features_legacy, 4247f3358507SMichael S. Tsirkin .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 4248296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 4249296f96fcSRusty Russell .driver.owner = THIS_MODULE, 4250296f96fcSRusty Russell .id_table = id_table, 4251fe36cbe0SMichael S. Tsirkin .validate = virtnet_validate, 4252296f96fcSRusty Russell .probe = virtnet_probe, 42538cc085d6SBill Pemberton .remove = virtnet_remove, 42549f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 425589107000SAaron Lu #ifdef CONFIG_PM_SLEEP 42560741bcb5SAmit Shah .freeze = virtnet_freeze, 42570741bcb5SAmit Shah .restore = virtnet_restore, 42580741bcb5SAmit Shah #endif 4259296f96fcSRusty Russell }; 4260296f96fcSRusty Russell 42618017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void) 42628017c279SSebastian Andrzej Siewior { 42638017c279SSebastian Andrzej Siewior int ret; 42648017c279SSebastian Andrzej Siewior 426573c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 42668017c279SSebastian Andrzej Siewior virtnet_cpu_online, 42678017c279SSebastian Andrzej Siewior virtnet_cpu_down_prep); 42688017c279SSebastian Andrzej Siewior if (ret < 0) 42698017c279SSebastian Andrzej Siewior goto out; 42708017c279SSebastian Andrzej Siewior virtionet_online = ret; 427173c1b41eSThomas Gleixner ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 42728017c279SSebastian Andrzej Siewior NULL, virtnet_cpu_dead); 42738017c279SSebastian Andrzej Siewior if (ret) 42748017c279SSebastian Andrzej Siewior goto err_dead; 42758017c279SSebastian Andrzej Siewior ret = register_virtio_driver(&virtio_net_driver); 42768017c279SSebastian Andrzej Siewior if (ret) 42778017c279SSebastian Andrzej Siewior goto err_virtio; 42788017c279SSebastian Andrzej Siewior return 0; 42798017c279SSebastian Andrzej Siewior err_virtio: 42808017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 42818017c279SSebastian Andrzej Siewior err_dead: 42828017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 42838017c279SSebastian Andrzej Siewior out: 42848017c279SSebastian Andrzej Siewior return ret; 42858017c279SSebastian Andrzej Siewior } 42868017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init); 42878017c279SSebastian Andrzej Siewior 42888017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void) 42898017c279SSebastian Andrzej Siewior { 4290cfa0ebc9SAndrew Jones unregister_virtio_driver(&virtio_net_driver); 42918017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 42928017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 42938017c279SSebastian Andrzej Siewior } 42948017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit); 4295296f96fcSRusty Russell 4296296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 4297296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 4298296f96fcSRusty Russell MODULE_LICENSE("GPL"); 4299