blob: 56c3f85190938dedc657af691191d34baabc3671 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rusty Russell48925e32009-09-24 09:59:20 -06002/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10003 *
4 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
Rusty Russell296f96f2007-10-22 11:03:37 +10005 */
6//#define DEBUG
7#include <linux/netdevice.h>
8#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +08009#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100010#include <linux/module.h>
11#include <linux/virtio.h>
12#include <linux/virtio_net.h>
John Fastabendf600b692016-12-15 12:13:24 -080013#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010014#include <linux/bpf_trace.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100015#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080016#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000018#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080019#include <linux/average.h>
Jason Wang186b3c92017-09-19 17:42:43 +080020#include <linux/filter.h>
Caleb Raitto2ca653d2018-08-09 17:28:40 -070021#include <linux/kernel.h>
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +020022#include <net/route.h>
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +010023#include <net/xdp.h>
Sridhar Samudralaba5e4422018-05-24 09:55:17 -070024#include <net/net_failover.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100025
Amerigo Wangd34710e2013-05-09 19:50:51 +000026static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020027module_param(napi_weight, int, 0444);
28
Willem de Bruijn31c03ae2019-06-13 12:24:57 -040029static bool csum = true, gso = true, napi_tx = true;
Rusty Russell34a48572008-02-04 23:50:02 -050030module_param(csum, bool, 0444);
31module_param(gso, bool, 0444);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040032module_param(napi_tx, bool, 0644);
Rusty Russell34a48572008-02-04 23:50:02 -050033
Rusty Russell296f96f2007-10-22 11:03:37 +100034/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080035#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080036#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100037
Jason Wangf6b10202017-02-21 16:46:28 +080038#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
39
John Fastabend2de2f7f2017-02-02 19:16:29 -080040/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
41#define VIRTIO_XDP_HEADROOM 256
42
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +020043/* Separating two types of XDP xmit */
44#define VIRTIO_XDP_TX BIT(0)
45#define VIRTIO_XDP_REDIR BIT(1)
46
Toshiaki Makita50504712019-01-29 09:45:59 +090047#define VIRTIO_XDP_FLAG BIT(0)
48
Johannes Berg5377d7582015-08-19 09:48:40 +020049/* RX packet size EWMA. The average packet size is used to determine the packet
50 * buffer size when refilling RX rings. As the entire RX ring may be refilled
51 * at once, the weight is chosen so that the EWMA will be insensitive to short-
52 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080053 */
Johannes Bergeb1e0112017-02-15 09:49:26 +010054DECLARE_EWMA(pkt_len, 0, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080055
Rick Jones66846042011-11-14 14:17:08 +000056#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000057
Colin Ian King7acd43292017-08-12 22:45:53 +010058static const unsigned long guest_offloads[] = {
59 VIRTIO_NET_F_GUEST_TSO4,
60 VIRTIO_NET_F_GUEST_TSO6,
61 VIRTIO_NET_F_GUEST_ECN,
Jason Wange59ff2c2018-11-22 14:36:30 +080062 VIRTIO_NET_F_GUEST_UFO,
63 VIRTIO_NET_F_GUEST_CSUM
Colin Ian King7acd43292017-08-12 22:45:53 +010064};
Jason Wang3f935222017-07-19 16:54:49 +080065
Tonghao Zhang1a03b8a2020-09-29 09:58:06 +080066#define GUEST_OFFLOAD_LRO_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
67 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
68 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
69 (1ULL << VIRTIO_NET_F_GUEST_UFO))
70
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090071struct virtnet_stat_desc {
72 char desc[ETH_GSTRING_LEN];
73 size_t offset;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000074};
75
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090076struct virtnet_sq_stats {
77 struct u64_stats_sync syncp;
78 u64 packets;
79 u64 bytes;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090080 u64 xdp_tx;
81 u64 xdp_tx_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +090082 u64 kicks;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090083};
84
Jason Wangd46eeea2018-07-31 17:43:39 +080085struct virtnet_rq_stats {
86 struct u64_stats_sync syncp;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090087 u64 packets;
88 u64 bytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +090089 u64 drops;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090090 u64 xdp_packets;
91 u64 xdp_tx;
92 u64 xdp_redirects;
93 u64 xdp_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +090094 u64 kicks;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090095};
96
97#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
Jason Wangd46eeea2018-07-31 17:43:39 +080098#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090099
100static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900101 { "packets", VIRTNET_SQ_STAT(packets) },
102 { "bytes", VIRTNET_SQ_STAT(bytes) },
103 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
104 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900105 { "kicks", VIRTNET_SQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900106};
107
108static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900109 { "packets", VIRTNET_RQ_STAT(packets) },
110 { "bytes", VIRTNET_RQ_STAT(bytes) },
111 { "drops", VIRTNET_RQ_STAT(drops) },
112 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
113 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
114 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
115 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900116 { "kicks", VIRTNET_RQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900117};
118
119#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
120#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
121
Jason Wange9d74172012-12-07 07:04:55 +0000122/* Internal representation of a send virtqueue */
123struct send_queue {
124 /* Virtqueue associated with this send _queue */
125 struct virtqueue *vq;
126
127 /* TX: fragments + linear part + virtio header */
128 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000129
130 /* Name of the send queue: output.$index */
131 char name[40];
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400132
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900133 struct virtnet_sq_stats stats;
134
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400135 struct napi_struct napi;
Jason Wange9d74172012-12-07 07:04:55 +0000136};
137
138/* Internal representation of a receive virtqueue */
139struct receive_queue {
140 /* Virtqueue associated with this receive_queue */
141 struct virtqueue *vq;
142
Rusty Russell296f96f2007-10-22 11:03:37 +1000143 struct napi_struct napi;
144
John Fastabendf600b692016-12-15 12:13:24 -0800145 struct bpf_prog __rcu *xdp_prog;
146
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900147 struct virtnet_rq_stats stats;
148
Jason Wange9d74172012-12-07 07:04:55 +0000149 /* Chain pages by the private ptr. */
150 struct page *pages;
151
Michael Daltonab7db912014-01-16 22:23:27 -0800152 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200153 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800154
Michael Daltonfb518792014-01-16 22:23:26 -0800155 /* Page frag for packet buffer allocation. */
156 struct page_frag alloc_frag;
157
Jason Wange9d74172012-12-07 07:04:55 +0000158 /* RX: fragments + linear part + virtio header */
159 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000160
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +0200161 /* Min single buffer size for mergeable buffers case. */
162 unsigned int min_buf_len;
163
Jason Wang986a4f42012-12-07 07:04:56 +0000164 /* Name of this receive queue: input.$index */
165 char name[40];
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100166
167 struct xdp_rxq_info xdp_rxq;
Jason Wange9d74172012-12-07 07:04:55 +0000168};
169
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300170/* Control VQ buffers: protected by the rtnl lock */
171struct control_buf {
172 struct virtio_net_ctrl_hdr hdr;
173 virtio_net_ctrl_ack status;
174 struct virtio_net_ctrl_mq mq;
175 u8 promisc;
176 u8 allmulti;
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +0300177 __virtio16 vid;
Michael S. Tsirkinf4ee7032018-04-19 08:30:50 +0300178 __virtio64 offloads;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300179};
180
Jason Wange9d74172012-12-07 07:04:55 +0000181struct virtnet_info {
182 struct virtio_device *vdev;
183 struct virtqueue *cvq;
184 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000185 struct send_queue *sq;
186 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000187 unsigned int status;
188
Jason Wang986a4f42012-12-07 07:04:56 +0000189 /* Max # of queue pairs supported by the device */
190 u16 max_queue_pairs;
191
192 /* # of queue pairs currently used by the driver */
193 u16 curr_queue_pairs;
194
John Fastabend672aafd2016-12-15 12:13:49 -0800195 /* # of XDP queue pairs currently used by the driver */
196 u16 xdp_queue_pairs;
197
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +0800198 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
199 bool xdp_enabled;
200
Herbert Xu97402b92008-04-18 11:24:27 +0800201 /* I like... big packets and I cannot lie! */
202 bool big_packets;
203
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800204 /* Host will merge rx buffers for big packets (shake it! shake it!) */
205 bool mergeable_rx_bufs;
206
Jason Wang986a4f42012-12-07 07:04:56 +0000207 /* Has control virtqueue */
208 bool has_cvq;
209
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930210 /* Host can handle any s/g split between our header and packet data */
211 bool any_header_sg;
212
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300213 /* Packet virtio header size */
214 u8 hdr_len;
215
Rusty Russell3161e452009-08-26 12:22:32 -0700216 /* Work struct for refilling if we run low on memory. */
217 struct delayed_work refill;
218
Jason Wang586d17c2012-04-11 20:43:52 +0000219 /* Work struct for config space updates */
220 struct work_struct config_work;
221
Jason Wang986a4f42012-12-07 07:04:56 +0000222 /* Does the affinity hint is set for virtqueues? */
223 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000224
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200225 /* CPU hotplug instances for online & dead */
226 struct hlist_node node;
227 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200228
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300229 struct control_buf *ctrl;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100230
231 /* Ethtool settings */
232 u8 duplex;
233 u32 speed;
Jason Wang3f935222017-07-19 16:54:49 +0800234
235 unsigned long guest_offloads;
Willem de Bruijna02e8962018-12-20 17:14:54 -0500236 unsigned long guest_offloads_capable;
Sridhar Samudralaba5e4422018-05-24 09:55:17 -0700237
238 /* failover when STANDBY feature enabled */
239 struct failover *failover;
Rusty Russell296f96f2007-10-22 11:03:37 +1000240};
241
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000242struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300243 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000244 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300245 * hdr is in a separate sg buffer, and data sg buffer shares same page
246 * with this header sg. This padding makes next sg 16 byte aligned
247 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000248 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300249 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000250};
251
Toshiaki Makita50504712019-01-29 09:45:59 +0900252static bool is_xdp_frame(void *ptr)
253{
254 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
255}
256
257static void *xdp_to_ptr(struct xdp_frame *ptr)
258{
259 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
260}
261
262static struct xdp_frame *ptr_to_xdp(void *ptr)
263{
264 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
265}
266
Jason Wang986a4f42012-12-07 07:04:56 +0000267/* Converting between virtqueue no. and kernel tx/rx queue no.
268 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
269 */
270static int vq2txq(struct virtqueue *vq)
271{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000272 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000273}
274
275static int txq2vq(int txq)
276{
277 return txq * 2 + 1;
278}
279
280static int vq2rxq(struct virtqueue *vq)
281{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000282 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000283}
284
285static int rxq2vq(int rxq)
286{
287 return rxq * 2;
288}
289
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300290static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000291{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300292 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000293}
294
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000295/*
296 * private is used to chain pages for big packets, put the whole
297 * most recent used list in the beginning for reuse
298 */
Jason Wange9d74172012-12-07 07:04:55 +0000299static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500300{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000301 struct page *end;
302
Jason Wange9d74172012-12-07 07:04:55 +0000303 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000304 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000305 end->private = (unsigned long)rq->pages;
306 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500307}
308
Jason Wange9d74172012-12-07 07:04:55 +0000309static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500310{
Jason Wange9d74172012-12-07 07:04:55 +0000311 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500312
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000313 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000314 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000315 /* clear private here, it is used to chain pages */
316 p->private = 0;
317 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500318 p = alloc_page(gfp_mask);
319 return p;
320}
321
Willem de Bruijne4e84522017-04-24 13:49:26 -0400322static void virtqueue_napi_schedule(struct napi_struct *napi,
323 struct virtqueue *vq)
324{
325 if (napi_schedule_prep(napi)) {
326 virtqueue_disable_cb(vq);
327 __napi_schedule(napi);
328 }
329}
330
331static void virtqueue_napi_complete(struct napi_struct *napi,
332 struct virtqueue *vq, int processed)
333{
334 int opaque;
335
336 opaque = virtqueue_enable_cb_prepare(vq);
Toshiaki Makitafdaa7672017-12-07 13:15:15 +0900337 if (napi_complete_done(napi, processed)) {
338 if (unlikely(virtqueue_poll(vq, opaque)))
339 virtqueue_napi_schedule(napi, vq);
340 } else {
341 virtqueue_disable_cb(vq);
342 }
Willem de Bruijne4e84522017-04-24 13:49:26 -0400343}
344
Jason Wange9d74172012-12-07 07:04:55 +0000345static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000346{
Jason Wange9d74172012-12-07 07:04:55 +0000347 struct virtnet_info *vi = vq->vdev->priv;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400348 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000349
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500350 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000351 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000352
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400353 if (napi->weight)
354 virtqueue_napi_schedule(napi, vq);
355 else
356 /* We were probably waiting for more output buffers. */
357 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000358}
359
Jason Wang28b39bc2017-07-19 16:54:46 +0800360#define MRG_CTX_HEADER_SHIFT 22
361static void *mergeable_len_to_ctx(unsigned int truesize,
362 unsigned int headroom)
363{
364 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
365}
366
367static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
368{
369 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
370}
371
372static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
373{
374 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
375}
376
Mike Waychison34646452012-01-04 12:52:32 +0000377/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300378static struct sk_buff *page_to_skb(struct virtnet_info *vi,
379 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700380 struct page *page, unsigned int offset,
Jason Wang436c9452018-11-29 13:53:16 +0800381 unsigned int len, unsigned int truesize,
Xuan Zhuofb328562021-04-16 17:16:15 +0800382 bool hdr_valid, unsigned int metasize,
Xuan Zhuo6c66c142021-05-13 19:48:07 +0800383 bool whole_page)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000384{
385 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300386 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700387 unsigned int copy, hdr_len, hdr_padded_len;
Eric Dumazetaf39c8f2021-04-20 02:43:41 -0700388 struct page *page_to_free = NULL;
Xuan Zhuofb328562021-04-16 17:16:15 +0800389 int tailroom, shinfo_size;
Xuan Zhuof80bd742021-04-22 23:16:20 +0800390 char *p, *hdr_p, *buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000391
Michael Dalton2613af02013-10-28 15:44:18 -0700392 p = page_address(page) + offset;
Xuan Zhuofb328562021-04-16 17:16:15 +0800393 hdr_p = p;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000394
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300395 hdr_len = vi->hdr_len;
396 if (vi->mergeable_rx_bufs)
stephen hemmingera4a76502017-08-15 10:29:17 -0700397 hdr_padded_len = sizeof(*hdr);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300398 else
Michael Dalton2613af02013-10-28 15:44:18 -0700399 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000400
Xuan Zhuo6c66c142021-05-13 19:48:07 +0800401 /* If whole_page, there is an offset between the beginning of the
Xuan Zhuofb328562021-04-16 17:16:15 +0800402 * data and the allocated space, otherwise the data and the allocated
403 * space are aligned.
Xuan Zhuo8fb7da92021-06-01 14:40:00 +0800404 *
405 * Buffers with headroom use PAGE_SIZE as alloc size, see
406 * add_recvbuf_mergeable() + get_mergeable_buf_len()
Xuan Zhuofb328562021-04-16 17:16:15 +0800407 */
Xuan Zhuo6c66c142021-05-13 19:48:07 +0800408 if (whole_page) {
409 /* Buffers with whole_page use PAGE_SIZE as alloc size,
Xuan Zhuof80bd742021-04-22 23:16:20 +0800410 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
411 */
Xuan Zhuofb328562021-04-16 17:16:15 +0800412 truesize = PAGE_SIZE;
Xuan Zhuo7bf64462021-05-13 19:48:08 +0800413
414 /* page maybe head page, so we should get the buf by p, not the
415 * page
416 */
417 tailroom = truesize - len - offset_in_page(p);
418 buf = (char *)((unsigned long)p & PAGE_MASK);
Xuan Zhuofb328562021-04-16 17:16:15 +0800419 } else {
420 tailroom = truesize - len;
Xuan Zhuof80bd742021-04-22 23:16:20 +0800421 buf = p;
Xuan Zhuofb328562021-04-16 17:16:15 +0800422 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000423
424 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700425 offset += hdr_padded_len;
426 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000427
Xuan Zhuofb328562021-04-16 17:16:15 +0800428 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
429
Xuan Zhuof80bd742021-04-22 23:16:20 +0800430 /* copy small packet so we can reuse these pages */
Eric Dumazetf5d78722021-04-20 13:01:44 -0700431 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
Xuan Zhuof80bd742021-04-22 23:16:20 +0800432 skb = build_skb(buf, truesize);
Xuan Zhuofb328562021-04-16 17:16:15 +0800433 if (unlikely(!skb))
434 return NULL;
435
Xuan Zhuof80bd742021-04-22 23:16:20 +0800436 skb_reserve(skb, p - buf);
Xuan Zhuofb328562021-04-16 17:16:15 +0800437 skb_put(skb, len);
438 goto ok;
439 }
440
441 /* copy small packet so we can reuse these pages for small data */
442 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
443 if (unlikely(!skb))
444 return NULL;
445
Eric Dumazet0f6925b2021-04-02 06:26:02 -0700446 /* Copy all frame if it fits skb->head, otherwise
447 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
448 */
449 if (len <= skb_tailroom(skb))
450 copy = len;
451 else
452 copy = ETH_HLEN + metasize;
Johannes Berg59ae1d12017-06-16 14:29:20 +0200453 skb_put_data(skb, p, copy);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000454
455 len -= copy;
456 offset += copy;
457
Michael Dalton2613af02013-10-28 15:44:18 -0700458 if (vi->mergeable_rx_bufs) {
459 if (len)
460 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
461 else
Eric Dumazetaf39c8f2021-04-20 02:43:41 -0700462 page_to_free = page;
Xuan Zhuofb328562021-04-16 17:16:15 +0800463 goto ok;
Michael Dalton2613af02013-10-28 15:44:18 -0700464 }
465
Sasha Levine878d782011-09-28 04:40:54 +0000466 /*
467 * Verify that we can indeed put this data into a skb.
468 * This is here to handle cases when the device erroneously
469 * tries to receive more than is possible. This is usually
470 * the case of a broken device.
471 */
472 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000473 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000474 dev_kfree_skb(skb);
475 return NULL;
476 }
Michael Dalton2613af02013-10-28 15:44:18 -0700477 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000478 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700479 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
480 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
481 frag_size, truesize);
482 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000483 page = (struct page *)page->private;
484 offset = 0;
485 }
486
487 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000488 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000489
Xuan Zhuofb328562021-04-16 17:16:15 +0800490ok:
491 /* hdr_valid means no XDP, so we can copy the vnet header */
492 if (hdr_valid) {
493 hdr = skb_vnet_hdr(skb);
494 memcpy(hdr, hdr_p, hdr_len);
495 }
Eric Dumazetaf39c8f2021-04-20 02:43:41 -0700496 if (page_to_free)
497 put_page(page_to_free);
Xuan Zhuofb328562021-04-16 17:16:15 +0800498
499 if (metasize) {
500 __skb_pull(skb, metasize);
501 skb_metadata_set(skb, metasize);
502 }
503
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000504 return skb;
505}
506
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200507static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
508 struct send_queue *sq,
509 struct xdp_frame *xdpf)
John Fastabend56434a02016-12-15 12:14:13 -0800510{
John Fastabend56434a02016-12-15 12:14:13 -0800511 struct virtio_net_hdr_mrg_rxbuf *hdr;
John Fastabend56434a02016-12-15 12:14:13 -0800512 int err;
513
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200514 if (unlikely(xdpf->headroom < vi->hdr_len))
515 return -EOVERFLOW;
516
517 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
518 xdpf->data -= vi->hdr_len;
Jason Wangf6b10202017-02-21 16:46:28 +0800519 /* Zero header and leave csum up to XDP layers */
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200520 hdr = xdpf->data;
Jason Wangf6b10202017-02-21 16:46:28 +0800521 memset(hdr, 0, vi->hdr_len);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200522 xdpf->len += vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800523
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200524 sg_init_one(sq->sg, xdpf->data, xdpf->len);
Jason Wangbb91acc2016-12-23 22:37:32 +0800525
Toshiaki Makita50504712019-01-29 09:45:59 +0900526 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
527 GFP_ATOMIC);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100528 if (unlikely(err))
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200529 return -ENOSPC; /* Caller handle free/refcnt */
John Fastabend56434a02016-12-15 12:14:13 -0800530
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200531 return 0;
John Fastabend56434a02016-12-15 12:14:13 -0800532}
533
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +0800534/* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
535 * the current cpu, so it does not need to be locked.
536 *
537 * Here we use marco instead of inline functions because we have to deal with
538 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
539 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
540 * functions to perfectly solve these three problems at the same time.
541 */
542#define virtnet_xdp_get_sq(vi) ({ \
543 struct netdev_queue *txq; \
544 typeof(vi) v = (vi); \
545 unsigned int qp; \
546 \
547 if (v->curr_queue_pairs > nr_cpu_ids) { \
548 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
549 qp += smp_processor_id(); \
550 txq = netdev_get_tx_queue(v->dev, qp); \
551 __netif_tx_acquire(txq); \
552 } else { \
553 qp = smp_processor_id() % v->curr_queue_pairs; \
554 txq = netdev_get_tx_queue(v->dev, qp); \
555 __netif_tx_lock(txq, raw_smp_processor_id()); \
556 } \
557 v->sq + qp; \
558})
Toshiaki Makita2a435652018-07-23 23:36:07 +0900559
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +0800560#define virtnet_xdp_put_sq(vi, q) { \
561 struct netdev_queue *txq; \
562 typeof(vi) v = (vi); \
563 \
564 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
565 if (v->curr_queue_pairs > nr_cpu_ids) \
566 __netif_tx_release(txq); \
567 else \
568 __netif_tx_unlock(txq); \
Toshiaki Makita2a435652018-07-23 23:36:07 +0900569}
570
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200571static int virtnet_xdp_xmit(struct net_device *dev,
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +0200572 int n, struct xdp_frame **frames, u32 flags)
Jason Wang186b3c92017-09-19 17:42:43 +0800573{
574 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100575 struct receive_queue *rq = vi->rq;
576 struct bpf_prog *xdp_prog;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200577 struct send_queue *sq;
578 unsigned int len;
Toshiaki Makita546f28972019-01-31 20:40:30 +0900579 int packets = 0;
580 int bytes = 0;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100581 int nxmit = 0;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900582 int kicks = 0;
Toshiaki Makita50504712019-01-29 09:45:59 +0900583 void *ptr;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100584 int ret;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200585 int i;
586
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100587 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
588 * indicate XDP resources have been successfully allocated.
589 */
John Fastabend9719c6b2020-01-26 16:14:01 -0800590 xdp_prog = rcu_access_pointer(rq->xdp_prog);
Toshiaki Makita1667c082019-01-29 09:45:56 +0900591 if (!xdp_prog)
592 return -ENXIO;
593
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +0800594 sq = virtnet_xdp_get_sq(vi);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000595
596 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
Jason Wang186b3c92017-09-19 17:42:43 +0800597 ret = -EINVAL;
Jason Wang186b3c92017-09-19 17:42:43 +0800598 goto out;
599 }
600
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200601 /* Free up any pending old buffers before queueing new ones. */
Toshiaki Makita50504712019-01-29 09:45:59 +0900602 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Toshiaki Makita546f28972019-01-31 20:40:30 +0900603 if (likely(is_xdp_frame(ptr))) {
604 struct xdp_frame *frame = ptr_to_xdp(ptr);
605
606 bytes += frame->len;
607 xdp_return_frame(frame);
608 } else {
609 struct sk_buff *skb = ptr;
610
611 bytes += skb->len;
612 napi_consume_skb(skb, false);
613 }
614 packets++;
Toshiaki Makita50504712019-01-29 09:45:59 +0900615 }
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200616
617 for (i = 0; i < n; i++) {
618 struct xdp_frame *xdpf = frames[i];
619
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100620 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
621 break;
622 nxmit++;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200623 }
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100624 ret = nxmit;
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200625
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900626 if (flags & XDP_XMIT_FLUSH) {
627 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
628 kicks = 1;
629 }
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900630out:
631 u64_stats_update_begin(&sq->stats.syncp);
Toshiaki Makita546f28972019-01-31 20:40:30 +0900632 sq->stats.bytes += bytes;
633 sq->stats.packets += packets;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900634 sq->stats.xdp_tx += n;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100635 sq->stats.xdp_tx_drops += n - nxmit;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900636 sq->stats.kicks += kicks;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900637 u64_stats_update_end(&sq->stats.syncp);
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200638
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +0800639 virtnet_xdp_put_sq(vi, sq);
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900640 return ret;
Jason Wang186b3c92017-09-19 17:42:43 +0800641}
642
Jason Wangf6b10202017-02-21 16:46:28 +0800643static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
644{
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +0800645 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
Jason Wangf6b10202017-02-21 16:46:28 +0800646}
647
Jason Wang4941d472017-07-19 16:54:48 +0800648/* We copy the packet for XDP in the following cases:
649 *
650 * 1) Packet is scattered across multiple rx buffers.
651 * 2) Headroom space is insufficient.
652 *
653 * This is inefficient but it's a temporary condition that
654 * we hit right after XDP is enabled and until queue is refilled
655 * with large buffers with sufficient headroom - so it should affect
656 * at most queue size packets.
657 * Afterwards, the conditions to enable
658 * XDP should preclude the underlying device from sending packets
659 * across multiple buffers (num_buf > 1), and we make sure buffers
660 * have enough headroom.
John Fastabend72979a62016-12-15 12:14:36 -0800661 */
662static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800663 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800664 struct page *p,
665 int offset,
Jason Wang4941d472017-07-19 16:54:48 +0800666 int page_off,
John Fastabend72979a62016-12-15 12:14:36 -0800667 unsigned int *len)
668{
669 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend72979a62016-12-15 12:14:36 -0800670
671 if (!page)
672 return NULL;
673
674 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
675 page_off += *len;
676
Jason Wang56a86f82016-12-23 22:37:26 +0800677 while (--*num_buf) {
Jason Wang3cc81a92018-03-02 17:29:14 +0800678 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
John Fastabend72979a62016-12-15 12:14:36 -0800679 unsigned int buflen;
John Fastabend72979a62016-12-15 12:14:36 -0800680 void *buf;
681 int off;
682
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200683 buf = virtqueue_get_buf(rq->vq, &buflen);
684 if (unlikely(!buf))
John Fastabend72979a62016-12-15 12:14:36 -0800685 goto err_buf;
686
John Fastabend72979a62016-12-15 12:14:36 -0800687 p = virt_to_head_page(buf);
688 off = buf - page_address(p);
689
Jason Wang56a86f82016-12-23 22:37:26 +0800690 /* guard against a misconfigured or uncooperative backend that
691 * is sending packet larger than the MTU.
692 */
Jason Wang3cc81a92018-03-02 17:29:14 +0800693 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
Jason Wang56a86f82016-12-23 22:37:26 +0800694 put_page(p);
695 goto err_buf;
696 }
697
John Fastabend72979a62016-12-15 12:14:36 -0800698 memcpy(page_address(page) + page_off,
699 page_address(p) + off, buflen);
700 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800701 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800702 }
703
John Fastabend2de2f7f2017-02-02 19:16:29 -0800704 /* Headroom does not contribute to packet length */
705 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800706 return page;
707err_buf:
708 __free_pages(page, 0);
709 return NULL;
710}
711
Jason Wang4941d472017-07-19 16:54:48 +0800712static struct sk_buff *receive_small(struct net_device *dev,
713 struct virtnet_info *vi,
714 struct receive_queue *rq,
715 void *buf, void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800716 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900717 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800718 struct virtnet_rq_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800719{
720 struct sk_buff *skb;
721 struct bpf_prog *xdp_prog;
722 unsigned int xdp_headroom = (unsigned long)ctx;
723 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
724 unsigned int headroom = vi->hdr_len + header_offset;
725 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
726 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
727 struct page *page = virt_to_head_page(buf);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100728 unsigned int delta = 0;
Jason Wang4941d472017-07-19 16:54:48 +0800729 struct page *xdp_page;
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100730 int err;
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900731 unsigned int metasize = 0;
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100732
Jason Wang4941d472017-07-19 16:54:48 +0800733 len -= vi->hdr_len;
Jason Wangd46eeea2018-07-31 17:43:39 +0800734 stats->bytes += len;
Jason Wang4941d472017-07-19 16:54:48 +0800735
Xie Yongjiad993a92021-05-31 21:58:52 +0800736 if (unlikely(len > GOOD_PACKET_LEN)) {
737 pr_debug("%s: rx error: len %u exceeds max size %d\n",
738 dev->name, len, GOOD_PACKET_LEN);
739 dev->stats.rx_length_errors++;
740 goto err_len;
741 }
Jason Wang4941d472017-07-19 16:54:48 +0800742 rcu_read_lock();
743 xdp_prog = rcu_dereference(rq->xdp_prog);
744 if (xdp_prog) {
745 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200746 struct xdp_frame *xdpf;
Jason Wang4941d472017-07-19 16:54:48 +0800747 struct xdp_buff xdp;
748 void *orig_data;
749 u32 act;
750
Jesper Dangaard Brouer95dbe9e2018-02-20 14:32:10 +0100751 if (unlikely(hdr->hdr.gso_type))
Jason Wang4941d472017-07-19 16:54:48 +0800752 goto err_xdp;
753
754 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
755 int offset = buf - page_address(page) + header_offset;
756 unsigned int tlen = len + vi->hdr_len;
757 u16 num_buf = 1;
758
759 xdp_headroom = virtnet_get_headroom(vi);
760 header_offset = VIRTNET_RX_PAD + xdp_headroom;
761 headroom = vi->hdr_len + header_offset;
762 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
763 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
764 xdp_page = xdp_linearize_page(rq, &num_buf, page,
765 offset, header_offset,
766 &tlen);
767 if (!xdp_page)
768 goto err_xdp;
769
770 buf = page_address(xdp_page);
771 put_page(page);
772 page = xdp_page;
773 }
774
Lorenzo Bianconi43b51692020-12-22 22:09:28 +0100775 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100776 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
777 xdp_headroom, len, true);
Jason Wang4941d472017-07-19 16:54:48 +0800778 orig_data = xdp.data;
779 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Jason Wangd46eeea2018-07-31 17:43:39 +0800780 stats->xdp_packets++;
Jason Wang4941d472017-07-19 16:54:48 +0800781
782 switch (act) {
783 case XDP_PASS:
784 /* Recalculate length in case bpf program changed it */
785 delta = orig_data - xdp.data;
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700786 len = xdp.data_end - xdp.data;
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900787 metasize = xdp.data - xdp.data_meta;
Jason Wang4941d472017-07-19 16:54:48 +0800788 break;
789 case XDP_TX:
Jason Wangd46eeea2018-07-31 17:43:39 +0800790 stats->xdp_tx++;
Lorenzo Bianconi1b698fa2020-05-28 22:47:29 +0200791 xdpf = xdp_convert_buff_to_frame(&xdp);
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200792 if (unlikely(!xdpf))
793 goto err_xdp;
Jason Wangca9e83b2018-07-31 17:43:38 +0800794 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100795 if (unlikely(!err)) {
796 xdp_return_frame_rx_napi(xdpf);
797 } else if (unlikely(err < 0)) {
Jason Wang4941d472017-07-19 16:54:48 +0800798 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100799 goto err_xdp;
800 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200801 *xdp_xmit |= VIRTIO_XDP_TX;
Jason Wang186b3c92017-09-19 17:42:43 +0800802 rcu_read_unlock();
803 goto xdp_xmit;
804 case XDP_REDIRECT:
Jason Wangd46eeea2018-07-31 17:43:39 +0800805 stats->xdp_redirects++;
Jason Wang186b3c92017-09-19 17:42:43 +0800806 err = xdp_do_redirect(dev, &xdp, xdp_prog);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100807 if (err)
808 goto err_xdp;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200809 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang4941d472017-07-19 16:54:48 +0800810 rcu_read_unlock();
811 goto xdp_xmit;
812 default:
813 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500814 fallthrough;
Jason Wang4941d472017-07-19 16:54:48 +0800815 case XDP_ABORTED:
816 trace_xdp_exception(vi->dev, xdp_prog, act);
Gustavo A. R. Silva95efabf2020-11-20 12:40:44 -0600817 goto err_xdp;
Jason Wang4941d472017-07-19 16:54:48 +0800818 case XDP_DROP:
819 goto err_xdp;
820 }
821 }
822 rcu_read_unlock();
823
824 skb = build_skb(buf, buflen);
825 if (!skb) {
826 put_page(page);
827 goto err;
828 }
829 skb_reserve(skb, headroom - delta);
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700830 skb_put(skb, len);
Yuya Kusakabef1d48842020-02-25 12:32:11 +0900831 if (!xdp_prog) {
Jason Wang4941d472017-07-19 16:54:48 +0800832 buf += header_offset;
833 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
Yuya Kusakabef1d48842020-02-25 12:32:11 +0900834 } /* keep zeroed vnet hdr since XDP is loaded */
Jason Wang4941d472017-07-19 16:54:48 +0800835
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900836 if (metasize)
837 skb_metadata_set(skb, metasize);
838
Jason Wang4941d472017-07-19 16:54:48 +0800839err:
840 return skb;
841
842err_xdp:
843 rcu_read_unlock();
Jason Wangd46eeea2018-07-31 17:43:39 +0800844 stats->xdp_drops++;
Xie Yongjiad993a92021-05-31 21:58:52 +0800845err_len:
Jason Wangd46eeea2018-07-31 17:43:39 +0800846 stats->drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800847 put_page(page);
848xdp_xmit:
849 return NULL;
850}
851
852static struct sk_buff *receive_big(struct net_device *dev,
853 struct virtnet_info *vi,
854 struct receive_queue *rq,
855 void *buf,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900856 unsigned int len,
Jason Wangd46eeea2018-07-31 17:43:39 +0800857 struct virtnet_rq_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800858{
859 struct page *page = buf;
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900860 struct sk_buff *skb =
Xuan Zhuofb328562021-04-16 17:16:15 +0800861 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
Jason Wang4941d472017-07-19 16:54:48 +0800862
Jason Wangd46eeea2018-07-31 17:43:39 +0800863 stats->bytes += len - vi->hdr_len;
Jason Wang4941d472017-07-19 16:54:48 +0800864 if (unlikely(!skb))
865 goto err;
866
867 return skb;
868
869err:
Jason Wangd46eeea2018-07-31 17:43:39 +0800870 stats->drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800871 give_pages(rq, page);
872 return NULL;
873}
874
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200875static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200876 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200877 struct receive_queue *rq,
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200878 void *buf,
879 void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800880 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900881 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800882 struct virtnet_rq_stats *stats)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000883{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300884 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
885 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200886 struct page *page = virt_to_head_page(buf);
887 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800888 struct sk_buff *head_skb, *curr_skb;
889 struct bpf_prog *xdp_prog;
Jesper Dangaard Brouer9ce61462020-05-14 12:50:44 +0200890 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
Jason Wang4941d472017-07-19 16:54:48 +0800891 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900892 unsigned int metasize = 0;
Jesper Dangaard Brouer9ce61462020-05-14 12:50:44 +0200893 unsigned int frame_sz;
894 int err;
Michael Daltonab7db912014-01-16 22:23:27 -0800895
John Fastabend56434a02016-12-15 12:14:13 -0800896 head_skb = NULL;
Jason Wangd46eeea2018-07-31 17:43:39 +0800897 stats->bytes += len - vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800898
Xie Yongjiad993a92021-05-31 21:58:52 +0800899 if (unlikely(len > truesize)) {
900 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
901 dev->name, len, (unsigned long)ctx);
902 dev->stats.rx_length_errors++;
903 goto err_skb;
904 }
John Fastabendf600b692016-12-15 12:13:24 -0800905 rcu_read_lock();
906 xdp_prog = rcu_dereference(rq->xdp_prog);
907 if (xdp_prog) {
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200908 struct xdp_frame *xdpf;
John Fastabend72979a62016-12-15 12:14:36 -0800909 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800910 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800911 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800912 u32 act;
913
Jason Wang3d62b2a2018-05-22 11:44:31 +0800914 /* Transient failure which in theory could occur if
915 * in-flight packets from before XDP was enabled reach
916 * the receive path after XDP is loaded.
917 */
918 if (unlikely(hdr->hdr.gso_type))
919 goto err_xdp;
920
Jesper Dangaard Brouer9ce61462020-05-14 12:50:44 +0200921 /* Buffers with headroom use PAGE_SIZE as alloc size,
922 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
923 */
924 frame_sz = headroom ? PAGE_SIZE : truesize;
925
Jason Wang3cc81a92018-03-02 17:29:14 +0800926 /* This happens when rx buffer size is underestimated
927 * or headroom is not enough because of the buffer
928 * was refilled before XDP is set. This should only
929 * happen for the first several packets, so we don't
930 * care much about its performance.
931 */
Jason Wang4941d472017-07-19 16:54:48 +0800932 if (unlikely(num_buf > 1 ||
933 headroom < virtnet_get_headroom(vi))) {
John Fastabend72979a62016-12-15 12:14:36 -0800934 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800935 xdp_page = xdp_linearize_page(rq, &num_buf,
Jason Wang4941d472017-07-19 16:54:48 +0800936 page, offset,
937 VIRTIO_XDP_HEADROOM,
938 &len);
Jesper Dangaard Brouer9ce61462020-05-14 12:50:44 +0200939 frame_sz = PAGE_SIZE;
940
John Fastabend72979a62016-12-15 12:14:36 -0800941 if (!xdp_page)
942 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800943 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800944 } else {
945 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800946 }
947
John Fastabend2de2f7f2017-02-02 19:16:29 -0800948 /* Allow consuming headroom but reserve enough space to push
949 * the descriptor on if we get an XDP_TX return code.
950 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800951 data = page_address(xdp_page) + offset;
Lorenzo Bianconi43b51692020-12-22 22:09:28 +0100952 xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100953 xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
954 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100955
John Fastabend0354e4d2017-02-02 19:15:01 -0800956 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Jason Wangd46eeea2018-07-31 17:43:39 +0800957 stats->xdp_packets++;
John Fastabend0354e4d2017-02-02 19:15:01 -0800958
John Fastabend56434a02016-12-15 12:14:13 -0800959 switch (act) {
960 case XDP_PASS:
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900961 metasize = xdp.data - xdp.data_meta;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800962
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900963 /* recalculate offset to account for any header
964 * adjustments and minus the metasize to copy the
965 * metadata in page_to_skb(). Note other cases do not
966 * build an skb and avoid using offset
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700967 */
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900968 offset = xdp.data - page_address(xdp_page) -
969 vi->hdr_len - metasize;
970
971 /* recalculate len if xdp.data, xdp.data_end or
972 * xdp.data_meta were adjusted
973 */
974 len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
Jason Wang1830f892016-12-23 22:37:27 +0800975 /* We can only create skb based on xdp_page. */
976 if (unlikely(xdp_page != page)) {
977 rcu_read_unlock();
978 put_page(page);
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900979 head_skb = page_to_skb(vi, rq, xdp_page, offset,
980 len, PAGE_SIZE, false,
Xuan Zhuo6c66c142021-05-13 19:48:07 +0800981 metasize, true);
Jason Wang1830f892016-12-23 22:37:27 +0800982 return head_skb;
983 }
John Fastabend56434a02016-12-15 12:14:13 -0800984 break;
985 case XDP_TX:
Jason Wangd46eeea2018-07-31 17:43:39 +0800986 stats->xdp_tx++;
Lorenzo Bianconi1b698fa2020-05-28 22:47:29 +0200987 xdpf = xdp_convert_buff_to_frame(&xdp);
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200988 if (unlikely(!xdpf))
989 goto err_xdp;
Jason Wangca9e83b2018-07-31 17:43:38 +0800990 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100991 if (unlikely(!err)) {
992 xdp_return_frame_rx_napi(xdpf);
993 } else if (unlikely(err < 0)) {
John Fastabend0354e4d2017-02-02 19:15:01 -0800994 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100995 if (unlikely(xdp_page != page))
996 put_page(xdp_page);
997 goto err_xdp;
998 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200999 *xdp_xmit |= VIRTIO_XDP_TX;
John Fastabend72979a62016-12-15 12:14:36 -08001000 if (unlikely(xdp_page != page))
Jason Wang5d458a12018-05-22 11:44:29 +08001001 put_page(page);
John Fastabend56434a02016-12-15 12:14:13 -08001002 rcu_read_unlock();
1003 goto xdp_xmit;
Jason Wang3cc81a92018-03-02 17:29:14 +08001004 case XDP_REDIRECT:
Jason Wangd46eeea2018-07-31 17:43:39 +08001005 stats->xdp_redirects++;
Jason Wang3cc81a92018-03-02 17:29:14 +08001006 err = xdp_do_redirect(dev, &xdp, xdp_prog);
1007 if (err) {
1008 if (unlikely(xdp_page != page))
1009 put_page(xdp_page);
1010 goto err_xdp;
1011 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001012 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang3cc81a92018-03-02 17:29:14 +08001013 if (unlikely(xdp_page != page))
Jason Wang6890418b2018-05-22 11:44:28 +08001014 put_page(page);
Jason Wang3cc81a92018-03-02 17:29:14 +08001015 rcu_read_unlock();
1016 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -08001017 default:
John Fastabend0354e4d2017-02-02 19:15:01 -08001018 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001019 fallthrough;
John Fastabend0354e4d2017-02-02 19:15:01 -08001020 case XDP_ABORTED:
1021 trace_xdp_exception(vi->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001022 fallthrough;
John Fastabend0354e4d2017-02-02 19:15:01 -08001023 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -08001024 if (unlikely(xdp_page != page))
1025 __free_pages(xdp_page, 0);
John Fastabendf600b692016-12-15 12:13:24 -08001026 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -08001027 }
John Fastabendf600b692016-12-15 12:13:24 -08001028 }
1029 rcu_read_unlock();
1030
Yuya Kusakabe503d5392020-02-25 12:32:12 +09001031 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
Xuan Zhuo6c66c142021-05-13 19:48:07 +08001032 metasize, !!headroom);
John Fastabendf600b692016-12-15 12:13:24 -08001033 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001034
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001035 if (unlikely(!curr_skb))
1036 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001037 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001038 int num_skb_frags;
1039
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001040 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
Yunjian Wang03e9f8a2017-12-04 14:02:19 +08001041 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001042 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001043 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001044 virtio16_to_cpu(vi->vdev,
1045 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001046 dev->stats.rx_length_errors++;
1047 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001048 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001049
Jason Wangd46eeea2018-07-31 17:43:39 +08001050 stats->bytes += len;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001051 page = virt_to_head_page(buf);
Jason Wang28b39bc2017-07-19 16:54:46 +08001052
1053 truesize = mergeable_ctx_to_truesize(ctx);
1054 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +03001055 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001056 dev->name, len, (unsigned long)ctx);
1057 dev->stats.rx_length_errors++;
1058 goto err_skb;
1059 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001060
1061 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -07001062 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1063 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001064
1065 if (unlikely(!nskb))
1066 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -07001067 if (curr_skb == head_skb)
1068 skb_shinfo(curr_skb)->frag_list = nskb;
1069 else
1070 curr_skb->next = nskb;
1071 curr_skb = nskb;
1072 head_skb->truesize += nskb->truesize;
1073 num_skb_frags = 0;
1074 }
1075 if (curr_skb != head_skb) {
1076 head_skb->data_len += len;
1077 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -08001078 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -07001079 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001080 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +08001081 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1082 put_page(page);
1083 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -08001084 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +08001085 } else {
1086 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -08001087 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +08001088 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001089 }
1090
Johannes Berg5377d7582015-08-19 09:48:40 +02001091 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001092 return head_skb;
1093
John Fastabendf600b692016-12-15 12:13:24 -08001094err_xdp:
1095 rcu_read_unlock();
Jason Wangd46eeea2018-07-31 17:43:39 +08001096 stats->xdp_drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001097err_skb:
1098 put_page(page);
Jason Wang850e0882018-05-22 11:44:30 +08001099 while (num_buf-- > 1) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001100 buf = virtqueue_get_buf(rq->vq, &len);
1101 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001102 pr_debug("%s: rx error: %d buffers missing\n",
1103 dev->name, num_buf);
1104 dev->stats.rx_length_errors++;
1105 break;
1106 }
Jason Wangd46eeea2018-07-31 17:43:39 +08001107 stats->bytes += len;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001108 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001109 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001110 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001111err_buf:
Jason Wangd46eeea2018-07-31 17:43:39 +08001112 stats->drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001113 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -08001114xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001115 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001116}
1117
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001118static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1119 void *buf, unsigned int len, void **ctx,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001120 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +08001121 struct virtnet_rq_stats *stats)
Rusty Russell296f96f2007-10-22 11:03:37 +10001122{
Jason Wange9d74172012-12-07 07:04:55 +00001123 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001124 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001125 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001126
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +03001127 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001128 pr_debug("%s: short packet %i\n", dev->name, len);
1129 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -08001130 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001131 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001132 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -08001133 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001134 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08001135 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001136 }
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001137 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001138 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001139
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001140 if (vi->mergeable_rx_bufs)
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001141 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001142 stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001143 else if (vi->big_packets)
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001144 skb = receive_big(dev, vi, rq, buf, len, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001145 else
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001146 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001147
1148 if (unlikely(!skb))
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001149 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001150
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001151 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001152
Mike Rapoporte858fae2016-06-08 16:09:21 +03001153 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +00001154 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +10001155
Mike Rapoporte858fae2016-06-08 16:09:21 +03001156 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1157 virtio_is_little_endian(vi->vdev))) {
1158 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1159 dev->name, hdr->hdr.gso_type,
1160 hdr->hdr.gso_size);
1161 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001162 }
1163
Willem de Bruijn133bbb12019-01-17 20:08:53 -05001164 skb_record_rx_queue(skb, vq2rxq(rq->vq));
Mike Rapoportd1dc06d2016-06-14 08:29:38 +03001165 skb->protocol = eth_type_trans(skb, dev);
1166 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1167 ntohs(skb->protocol), skb->len, skb->pkt_type);
1168
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001169 napi_gro_receive(&rq->napi, skb);
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001170 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001171
1172frame_err:
1173 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +10001174 dev_kfree_skb(skb);
1175}
1176
Jason Wang192f68c2017-07-19 16:54:47 +08001177/* Unlike mergeable buffers, all buffers are allocated to the
1178 * same size, except for the headroom. For this reason we do
1179 * not need to use mergeable_len_to_ctx here - it is enough
1180 * to store the headroom as the context ignoring the truesize.
1181 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001182static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1183 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +10001184{
Jason Wangf6b10202017-02-21 16:46:28 +08001185 struct page_frag *alloc_frag = &rq->alloc_frag;
1186 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001187 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wang192f68c2017-07-19 16:54:47 +08001188 void *ctx = (void *)(unsigned long)xdp_headroom;
Jason Wangf6b10202017-02-21 16:46:28 +08001189 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001190 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001191
Jason Wangf6b10202017-02-21 16:46:28 +08001192 len = SKB_DATA_ALIGN(len) +
1193 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1194 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001195 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001196
Jason Wangf6b10202017-02-21 16:46:28 +08001197 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1198 get_page(alloc_frag->page);
1199 alloc_frag->offset += len;
1200 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1201 vi->hdr_len + GOOD_PACKET_LEN);
Jason Wang192f68c2017-07-19 16:54:47 +08001202 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001203 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +08001204 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001205 return err;
1206}
1207
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001208static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1209 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001210{
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001211 struct page *first, *list = NULL;
1212 char *p;
1213 int i, err, offset;
1214
Rusty Russella5835442014-09-11 10:17:36 +09301215 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1216
Jason Wange9d74172012-12-07 07:04:55 +00001217 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001218 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +00001219 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001220 if (!first) {
1221 if (list)
Jason Wange9d74172012-12-07 07:04:55 +00001222 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001223 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -07001224 }
Jason Wange9d74172012-12-07 07:04:55 +00001225 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +10001226
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001227 /* chain new page in list head to match sg */
1228 first->private = (unsigned long)list;
1229 list = first;
1230 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001231
Jason Wange9d74172012-12-07 07:04:55 +00001232 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001233 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +00001234 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001235 return -ENOMEM;
1236 }
1237 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +08001238
Jason Wange9d74172012-12-07 07:04:55 +00001239 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001240 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1241 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +08001242
Jason Wange9d74172012-12-07 07:04:55 +00001243 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001244 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +00001245 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +08001246
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001247 /* chain first in list head */
1248 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301249 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1250 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001251 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +00001252 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +08001253
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001254 return err;
1255}
Herbert Xu97402b92008-04-18 11:24:27 +08001256
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02001257static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
Jason Wang3cc81a92018-03-02 17:29:14 +08001258 struct ewma_pkt_len *avg_pkt_len,
1259 unsigned int room)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001260{
Michael Daltonab7db912014-01-16 22:23:27 -08001261 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001262 unsigned int len;
1263
Jason Wang3cc81a92018-03-02 17:29:14 +08001264 if (room)
1265 return PAGE_SIZE - room;
1266
1267 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03001268 rq->min_buf_len, PAGE_SIZE - hdr_len);
Jason Wang3cc81a92018-03-02 17:29:14 +08001269
Michael S. Tsirkine377fcc2017-03-06 22:21:35 +02001270 return ALIGN(len, L1_CACHE_BYTES);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001271}
1272
John Fastabend2de2f7f2017-02-02 19:16:29 -08001273static int add_recvbuf_mergeable(struct virtnet_info *vi,
1274 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -08001275{
Michael Daltonfb518792014-01-16 22:23:26 -08001276 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001277 unsigned int headroom = virtnet_get_headroom(vi);
Jason Wang3cc81a92018-03-02 17:29:14 +08001278 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1279 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
Michael Daltonfb518792014-01-16 22:23:26 -08001280 char *buf;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001281 void *ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001282 int err;
Michael Daltonfb518792014-01-16 22:23:26 -08001283 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +10001284
Jason Wang3cc81a92018-03-02 17:29:14 +08001285 /* Extra tailroom is needed to satisfy XDP's assumption. This
1286 * means rx frags coalescing won't work, but consider we've
1287 * disabled GSO for XDP, it won't be a big issue.
1288 */
1289 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1290 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001291 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -08001292
Michael Daltonfb518792014-01-16 22:23:26 -08001293 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001294 buf += headroom; /* advance address leaving hole at front of pkt */
Michael Daltonfb518792014-01-16 22:23:26 -08001295 get_page(alloc_frag->page);
Jason Wang3cc81a92018-03-02 17:29:14 +08001296 alloc_frag->offset += len + room;
Michael Daltonfb518792014-01-16 22:23:26 -08001297 hole = alloc_frag->size - alloc_frag->offset;
Jason Wang3cc81a92018-03-02 17:29:14 +08001298 if (hole < len + room) {
Michael Daltonab7db912014-01-16 22:23:27 -08001299 /* To avoid internal fragmentation, if there is very likely not
1300 * enough space for another buffer, add the remaining space to
Michael S. Tsirkin1daa8792017-07-31 21:49:49 +03001301 * the current buffer.
Michael Daltonab7db912014-01-16 22:23:27 -08001302 */
Michael Daltonfb518792014-01-16 22:23:26 -08001303 len += hole;
1304 alloc_frag->offset += hole;
1305 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001306
Michael Daltonfb518792014-01-16 22:23:26 -08001307 sg_init_one(rq->sg, buf, len);
David S. Miller29fda252017-08-01 10:07:50 -07001308 ctx = mergeable_len_to_ctx(len, headroom);
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001309 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001310 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -07001311 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001312
1313 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001314}
1315
Rusty Russellb2baed62011-12-29 00:42:38 +00001316/*
1317 * Returns false if we couldn't fill entirely (OOM).
1318 *
1319 * Normally run in the receive path, but can also be run from ndo_open
1320 * before we're receiving packets, or from refill_work which is
1321 * careful to disable receiving (using napi_disable).
1322 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001323static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1324 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001325{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001326 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001327 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001328
Amit Shah0aea51c2009-08-26 14:58:28 +05301329 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001330 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001331 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001332 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001333 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001334 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001335 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001336
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001337 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301338 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001339 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001340 } while (rq->vq->num_free);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001341 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
Michael S. Tsirkin01c32592020-05-07 03:25:56 -04001342 unsigned long flags;
1343
1344 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
Jason Wangd46eeea2018-07-31 17:43:39 +08001345 rq->stats.kicks++;
Michael S. Tsirkin01c32592020-05-07 03:25:56 -04001346 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001347 }
1348
Rusty Russell3161e452009-08-26 12:22:32 -07001349 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001350}
1351
Rusty Russell18445c42008-02-04 23:49:57 -05001352static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001353{
1354 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001355 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +00001356
Willem de Bruijne4e84522017-04-24 13:49:26 -04001357 virtqueue_napi_schedule(&rq->napi, rvq);
Rusty Russell296f96f2007-10-22 11:03:37 +10001358}
1359
Willem de Bruijne4e84522017-04-24 13:49:26 -04001360static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001361{
Willem de Bruijne4e84522017-04-24 13:49:26 -04001362 napi_enable(napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001363
1364 /* If all buffers were filled by other side before we napi_enabled, we
Willem de Bruijne4e84522017-04-24 13:49:26 -04001365 * won't get another interrupt, so process any outstanding packets now.
1366 * Call local_bh_enable after to trigger softIRQ processing.
1367 */
1368 local_bh_disable();
1369 virtqueue_napi_schedule(napi, vq);
1370 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001371}
1372
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001373static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1374 struct virtqueue *vq,
1375 struct napi_struct *napi)
1376{
1377 if (!napi->weight)
1378 return;
1379
1380 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1381 * enable the feature if this is likely affine with the transmit path.
1382 */
1383 if (!vi->affinity_hint_set) {
1384 napi->weight = 0;
1385 return;
1386 }
1387
1388 return virtnet_napi_enable(vq, napi);
1389}
1390
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001391static void virtnet_napi_tx_disable(struct napi_struct *napi)
1392{
1393 if (napi->weight)
1394 napi_disable(napi);
1395}
1396
Rusty Russell3161e452009-08-26 12:22:32 -07001397static void refill_work(struct work_struct *work)
1398{
Jason Wange9d74172012-12-07 07:04:55 +00001399 struct virtnet_info *vi =
1400 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -07001401 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +00001402 int i;
Rusty Russell3161e452009-08-26 12:22:32 -07001403
Sasha Levin55257d72013-04-29 12:00:08 +09301404 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001405 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -07001406
Jason Wang986a4f42012-12-07 07:04:56 +00001407 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001408 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001409 virtnet_napi_enable(rq->vq, &rq->napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001410
1411 /* In theory, this can happen: if we don't get any buffers in
1412 * we will *never* try to fill again.
1413 */
1414 if (still_empty)
1415 schedule_delayed_work(&vi->refill, HZ/2);
1416 }
Rusty Russell3161e452009-08-26 12:22:32 -07001417}
1418
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001419static int virtnet_receive(struct receive_queue *rq, int budget,
1420 unsigned int *xdp_xmit)
Rusty Russell296f96f2007-10-22 11:03:37 +10001421{
Jason Wange9d74172012-12-07 07:04:55 +00001422 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wangd46eeea2018-07-31 17:43:39 +08001423 struct virtnet_rq_stats stats = {};
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001424 unsigned int len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001425 void *buf;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001426 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001427
Jason Wang192f68c2017-07-19 16:54:47 +08001428 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001429 void *ctx;
1430
Jason Wangd46eeea2018-07-31 17:43:39 +08001431 while (stats.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001432 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001433 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
Jason Wangd46eeea2018-07-31 17:43:39 +08001434 stats.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001435 }
1436 } else {
Jason Wangd46eeea2018-07-31 17:43:39 +08001437 while (stats.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001438 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001439 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
Jason Wangd46eeea2018-07-31 17:43:39 +08001440 stats.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001441 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001442 }
1443
? jiang718be6b2019-08-20 02:51:23 +00001444 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001445 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001446 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -07001447 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001448
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001449 u64_stats_update_begin(&rq->stats.syncp);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001450 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1451 size_t offset = virtnet_rq_stats_desc[i].offset;
1452 u64 *item;
1453
Jason Wangd46eeea2018-07-31 17:43:39 +08001454 item = (u64 *)((u8 *)&rq->stats + offset);
1455 *item += *(u64 *)((u8 *)&stats + offset);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001456 }
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001457 u64_stats_update_end(&rq->stats.syncp);
Jason Wang61845d22017-02-17 11:33:09 +08001458
Jason Wangd46eeea2018-07-31 17:43:39 +08001459 return stats.packets;
Jason Wang2ffa7592014-07-23 16:33:54 +08001460}
1461
Michael S. Tsirkindf133f32019-01-17 23:20:07 -05001462static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001463{
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001464 unsigned int len;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001465 unsigned int packets = 0;
1466 unsigned int bytes = 0;
Toshiaki Makita50504712019-01-29 09:45:59 +09001467 void *ptr;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001468
Toshiaki Makita50504712019-01-29 09:45:59 +09001469 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1470 if (likely(!is_xdp_frame(ptr))) {
1471 struct sk_buff *skb = ptr;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001472
Toshiaki Makita50504712019-01-29 09:45:59 +09001473 pr_debug("Sent skb %p\n", skb);
1474
1475 bytes += skb->len;
1476 napi_consume_skb(skb, in_napi);
1477 } else {
1478 struct xdp_frame *frame = ptr_to_xdp(ptr);
1479
1480 bytes += frame->len;
1481 xdp_return_frame(frame);
1482 }
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001483 packets++;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001484 }
1485
1486 /* Avoid overhead when no packets have been processed
1487 * happens when called speculatively from start_xmit.
1488 */
1489 if (!packets)
1490 return;
1491
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001492 u64_stats_update_begin(&sq->stats.syncp);
1493 sq->stats.bytes += bytes;
1494 sq->stats.packets += packets;
1495 u64_stats_update_end(&sq->stats.syncp);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001496}
1497
Toshiaki Makita534da5e2019-01-29 09:45:54 +09001498static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1499{
1500 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1501 return false;
1502 else if (q < vi->curr_queue_pairs)
1503 return true;
1504 else
1505 return false;
1506}
1507
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001508static void virtnet_poll_cleantx(struct receive_queue *rq)
1509{
1510 struct virtnet_info *vi = rq->vq->vdev->priv;
1511 unsigned int index = vq2rxq(rq->vq);
1512 struct send_queue *sq = &vi->sq[index];
1513 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1514
Toshiaki Makita534da5e2019-01-29 09:45:54 +09001515 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001516 return;
1517
1518 if (__netif_tx_trylock(txq)) {
Michael S. Tsirkina7766ef2021-04-13 01:30:45 -04001519 do {
1520 virtqueue_disable_cb(sq->vq);
1521 free_old_xmit_skbs(sq, true);
1522 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
Michael S. Tsirkin22bc63c2021-04-13 01:38:08 -04001523
1524 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1525 netif_tx_wake_queue(txq);
1526
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001527 __netif_tx_unlock(txq);
1528 }
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001529}
1530
Jason Wang2ffa7592014-07-23 16:33:54 +08001531static int virtnet_poll(struct napi_struct *napi, int budget)
1532{
1533 struct receive_queue *rq =
1534 container_of(napi, struct receive_queue, napi);
Jason Wang9267c432018-04-13 14:58:25 +08001535 struct virtnet_info *vi = rq->vq->vdev->priv;
1536 struct send_queue *sq;
Toshiaki Makita2a435652018-07-23 23:36:07 +09001537 unsigned int received;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001538 unsigned int xdp_xmit = 0;
Jason Wang2ffa7592014-07-23 16:33:54 +08001539
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001540 virtnet_poll_cleantx(rq);
1541
Jason Wang186b3c92017-09-19 17:42:43 +08001542 received = virtnet_receive(rq, budget, &xdp_xmit);
Jason Wang2ffa7592014-07-23 16:33:54 +08001543
Rusty Russell8329d982007-11-19 11:20:43 -05001544 /* Out of packets? */
Willem de Bruijne4e84522017-04-24 13:49:26 -04001545 if (received < budget)
1546 virtqueue_napi_complete(napi, rq->vq, received);
Rusty Russell296f96f2007-10-22 11:03:37 +10001547
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001548 if (xdp_xmit & VIRTIO_XDP_REDIR)
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001549 xdp_do_flush();
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001550
1551 if (xdp_xmit & VIRTIO_XDP_TX) {
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +08001552 sq = virtnet_xdp_get_sq(vi);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001553 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1554 u64_stats_update_begin(&sq->stats.syncp);
1555 sq->stats.kicks++;
1556 u64_stats_update_end(&sq->stats.syncp);
1557 }
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +08001558 virtnet_xdp_put_sq(vi, sq);
Jason Wang9267c432018-04-13 14:58:25 +08001559 }
Jason Wang186b3c92017-09-19 17:42:43 +08001560
Rusty Russell296f96f2007-10-22 11:03:37 +10001561 return received;
1562}
1563
Jason Wang986a4f42012-12-07 07:04:56 +00001564static int virtnet_open(struct net_device *dev)
1565{
1566 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001567 int i, err;
Jason Wang986a4f42012-12-07 07:04:56 +00001568
Jason Wange4166622013-05-21 20:03:58 +00001569 for (i = 0; i < vi->max_queue_pairs; i++) {
1570 if (i < vi->curr_queue_pairs)
1571 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001572 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001573 schedule_delayed_work(&vi->refill, 0);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001574
Björn Töpelb02e5a02020-11-30 19:52:01 +01001575 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001576 if (err < 0)
1577 return err;
1578
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +02001579 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1580 MEM_TYPE_PAGE_SHARED, NULL);
1581 if (err < 0) {
1582 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1583 return err;
1584 }
1585
Willem de Bruijne4e84522017-04-24 13:49:26 -04001586 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001587 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001588 }
1589
1590 return 0;
1591}
1592
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001593static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1594{
1595 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1596 struct virtnet_info *vi = sq->vq->vdev->priv;
Toshiaki Makita534da5e2019-01-29 09:45:54 +09001597 unsigned int index = vq2txq(sq->vq);
1598 struct netdev_queue *txq;
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001599 int opaque;
1600 bool done;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001601
Toshiaki Makita534da5e2019-01-29 09:45:54 +09001602 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1603 /* We don't need to enable cb for XDP */
1604 napi_complete_done(napi, 0);
1605 return 0;
1606 }
1607
1608 txq = netdev_get_tx_queue(vi->dev, index);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001609 __netif_tx_lock(txq, raw_smp_processor_id());
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001610 virtqueue_disable_cb(sq->vq);
Michael S. Tsirkindf133f32019-01-17 23:20:07 -05001611 free_old_xmit_skbs(sq, true);
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001612
Michael S. Tsirkin22bc63c2021-04-13 01:38:08 -04001613 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1614 netif_tx_wake_queue(txq);
1615
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001616 opaque = virtqueue_enable_cb_prepare(sq->vq);
1617
1618 done = napi_complete_done(napi, 0);
1619
1620 if (!done)
1621 virtqueue_disable_cb(sq->vq);
1622
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001623 __netif_tx_unlock(txq);
1624
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001625 if (done) {
1626 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
1627 if (napi_schedule_prep(napi)) {
1628 __netif_tx_lock(txq, raw_smp_processor_id());
1629 virtqueue_disable_cb(sq->vq);
1630 __netif_tx_unlock(txq);
1631 __napi_schedule(napi);
1632 }
1633 }
1634 }
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001635
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001636 return 0;
1637}
1638
Jason Wange9d74172012-12-07 07:04:55 +00001639static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001640{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001641 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001642 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001643 struct virtnet_info *vi = sq->vq->vdev->priv;
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001644 int num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001645 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301646 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001647
Johannes Berge1749612008-10-27 15:59:26 -07001648 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301649
1650 can_push = vi->any_header_sg &&
1651 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1652 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1653 /* Even if we can, don't push here yet as this would skew
1654 * csum_start offset below. */
1655 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001656 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301657 else
1658 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001659
Mike Rapoporte858fae2016-06-08 16:09:21 +03001660 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04001661 virtio_is_little_endian(vi->vdev), false,
1662 0))
Xianting Tian85eb1382021-06-05 11:31:00 -04001663 return -EPROTO;
Rusty Russell296f96f2007-10-22 11:03:37 +10001664
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001665 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001666 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001667
Jason Wang547c8902015-08-27 14:53:06 +08001668 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301669 if (can_push) {
1670 __skb_push(skb, hdr_len);
1671 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001672 if (unlikely(num_sg < 0))
1673 return num_sg;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301674 /* Pull header back to avoid skew in tx bytes calculations. */
1675 __skb_pull(skb, hdr_len);
1676 } else {
1677 sg_set_buf(sq->sg, hdr, hdr_len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001678 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1679 if (unlikely(num_sg < 0))
1680 return num_sg;
1681 num_sg++;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301682 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301683 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001684}
1685
Stephen Hemminger424efe92009-08-31 19:50:51 +00001686static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001687{
1688 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001689 int qnum = skb_get_queue_mapping(skb);
1690 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301691 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001692 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
Florian Westphal6b16f9e2019-04-01 16:42:14 +02001693 bool kick = !netdev_xmit_more();
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001694 bool use_napi = sq->napi.weight;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001695
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001696 /* Free up any pending old buffers before queueing new ones. */
Michael S. Tsirkina7766ef2021-04-13 01:30:45 -04001697 do {
1698 if (use_napi)
1699 virtqueue_disable_cb(sq->vq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001700
Michael S. Tsirkina7766ef2021-04-13 01:30:45 -04001701 free_old_xmit_skbs(sq, false);
1702
1703 } while (use_napi && kick &&
1704 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
Willem de Bruijnbdb12e02017-04-24 13:49:30 -04001705
Jacob Keller074c3582014-06-25 02:37:13 +00001706 /* timestamp packet in software */
1707 skb_tx_timestamp(skb);
1708
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001709 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001710 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001711
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301712 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001713 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301714 dev->stats.tx_fifo_errors++;
1715 if (net_ratelimit())
1716 dev_warn(&dev->dev,
Yuval Shaia7934b482019-04-03 12:10:13 +03001717 "Unexpected TXQ (%d) queue failure: %d\n",
1718 qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001719 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001720 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001721 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001722 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001723
Rusty Russell48925e32009-09-24 09:59:20 -06001724 /* Don't wait up for transmitted skbs to be freed. */
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001725 if (!use_napi) {
1726 skb_orphan(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +02001727 nf_reset_ct(skb);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001728 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001729
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001730 /* If running out of space, stop queue to avoid getting packets that we
1731 * are then unable to transmit.
1732 * An alternative would be to force queuing layer to requeue the skb by
1733 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1734 * returned in a normal path of operation: it means that driver is not
1735 * maintaining the TX queue stop/start state properly, and causes
1736 * the stack to do a non-trivial amount of useless work.
1737 * Since most packets only take 1 or 2 ring slots, stopping the queue
1738 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001739 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001740 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001741 netif_stop_subqueue(dev, qnum);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001742 if (!use_napi &&
1743 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001744 /* More just got used, free them then recheck. */
Michael S. Tsirkindf133f32019-01-17 23:20:07 -05001745 free_old_xmit_skbs(sq, false);
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001746 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001747 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001748 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001749 }
1750 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001751 }
Rusty Russell48925e32009-09-24 09:59:20 -06001752
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001753 if (kick || netif_xmit_stopped(txq)) {
1754 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1755 u64_stats_update_begin(&sq->stats.syncp);
1756 sq->stats.kicks++;
1757 u64_stats_update_end(&sq->stats.syncp);
1758 }
1759 }
David S. Miller0b725a22014-08-25 15:51:53 -07001760
Rusty Russell48925e32009-09-24 09:59:20 -06001761 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001762}
1763
Amos Kong40cbfc32013-01-21 01:17:21 +00001764/*
1765 * Send command via the control virtqueue and check status. Commands
1766 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001767 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001768 */
1769static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001770 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001771{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301772 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001773 unsigned out_num = 0, tmp;
Yunjian Wang222722b2021-07-10 11:32:49 +08001774 int ret;
Amos Kong40cbfc32013-01-21 01:17:21 +00001775
1776 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301777 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001778
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001779 vi->ctrl->status = ~0;
1780 vi->ctrl->hdr.class = class;
1781 vi->ctrl->hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301782 /* Add header */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001783 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301784 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001785
Rusty Russellf7bc9592013-03-20 15:44:28 +10301786 if (out)
1787 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001788
Rusty Russellf7bc9592013-03-20 15:44:28 +10301789 /* Add return status. */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001790 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001791 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001792
stephen hemmingerd24bae32013-12-09 16:17:40 -08001793 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Yunjian Wang222722b2021-07-10 11:32:49 +08001794 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1795 if (ret < 0) {
1796 dev_warn(&vi->vdev->dev,
1797 "Failed to add sgs for command vq: %d\n.", ret);
1798 return false;
1799 }
Amos Kong40cbfc32013-01-21 01:17:21 +00001800
Heinz Graalfs67975902013-10-29 09:40:02 +10301801 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001802 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001803
1804 /* Spin for a response, the kick causes an ioport write, trapping
1805 * into the hypervisor, so the request should be handled immediately.
1806 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301807 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1808 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001809 cpu_relax();
1810
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001811 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001812}
1813
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001814static int virtnet_set_mac_address(struct net_device *dev, void *p)
1815{
1816 struct virtnet_info *vi = netdev_priv(dev);
1817 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001818 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001819 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001820 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001821
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07001822 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1823 return -EOPNOTSUPP;
1824
Shyam Saini801822d2016-12-24 00:44:58 +05301825 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001826 if (!addr)
1827 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001828
1829 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001830 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001831 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001832
Amos Kong7e58d5a2013-01-21 01:17:23 +00001833 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1834 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1835 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001836 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001837 dev_warn(&vdev->dev,
1838 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001839 ret = -EINVAL;
1840 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001841 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001842 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1843 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301844 unsigned int i;
1845
1846 /* Naturally, this has an atomicity problem. */
1847 for (i = 0; i < dev->addr_len; i++)
1848 virtio_cwrite8(vdev,
1849 offsetof(struct virtio_net_config, mac) +
1850 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001851 }
1852
1853 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001854 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001855
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001856out:
1857 kfree(addr);
1858 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001859}
1860
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001861static void virtnet_stats(struct net_device *dev,
1862 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001863{
1864 struct virtnet_info *vi = netdev_priv(dev);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001865 unsigned int start;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001866 int i;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001867
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001868 for (i = 0; i < vi->max_queue_pairs; i++) {
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001869 u64 tpackets, tbytes, rpackets, rbytes, rdrops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001870 struct receive_queue *rq = &vi->rq[i];
1871 struct send_queue *sq = &vi->sq[i];
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001872
1873 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001874 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1875 tpackets = sq->stats.packets;
1876 tbytes = sq->stats.bytes;
1877 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001878
1879 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001880 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
Jason Wangd46eeea2018-07-31 17:43:39 +08001881 rpackets = rq->stats.packets;
1882 rbytes = rq->stats.bytes;
1883 rdrops = rq->stats.drops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001884 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001885
1886 tot->rx_packets += rpackets;
1887 tot->tx_packets += tpackets;
1888 tot->rx_bytes += rbytes;
1889 tot->tx_bytes += tbytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001890 tot->rx_dropped += rdrops;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001891 }
1892
1893 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001894 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001895 tot->rx_length_errors = dev->stats.rx_length_errors;
1896 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001897}
1898
Jason Wang586d17c2012-04-11 20:43:52 +00001899static void virtnet_ack_link_announce(struct virtnet_info *vi)
1900{
1901 rtnl_lock();
1902 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001903 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001904 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1905 rtnl_unlock();
1906}
1907
John Fastabend473153292017-02-02 19:14:32 -08001908static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001909{
1910 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001911 struct net_device *dev = vi->dev;
1912
1913 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1914 return 0;
1915
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001916 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1917 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001918
1919 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001920 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001921 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1922 queue_pairs);
1923 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301924 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001925 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001926 /* virtnet_open() will refill when device is going to up. */
1927 if (dev->flags & IFF_UP)
1928 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301929 }
Jason Wang986a4f42012-12-07 07:04:56 +00001930
1931 return 0;
1932}
1933
John Fastabend473153292017-02-02 19:14:32 -08001934static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1935{
1936 int err;
1937
1938 rtnl_lock();
1939 err = _virtnet_set_queues(vi, queue_pairs);
1940 rtnl_unlock();
1941 return err;
1942}
1943
Rusty Russell296f96f2007-10-22 11:03:37 +10001944static int virtnet_close(struct net_device *dev)
1945{
1946 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001947 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001948
Rusty Russellb2baed62011-12-29 00:42:38 +00001949 /* Make sure refill_work doesn't re-enable napi! */
1950 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001951
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001952 for (i = 0; i < vi->max_queue_pairs; i++) {
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001953 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
Jason Wang986a4f42012-12-07 07:04:56 +00001954 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001955 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001956 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001957
Rusty Russell296f96f2007-10-22 11:03:37 +10001958 return 0;
1959}
1960
Alex Williamson2af76982009-02-04 09:02:40 +00001961static void virtnet_set_rx_mode(struct net_device *dev)
1962{
1963 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001964 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001965 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001966 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001967 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001968 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001969 void *buf;
1970 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001971
stephen hemminger788a8b62013-12-09 16:18:45 -08001972 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001973 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1974 return;
1975
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001976 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1977 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001978
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001979 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001980
1981 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001982 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001983 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001984 vi->ctrl->promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001985
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001986 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001987
1988 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001989 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001990 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001991 vi->ctrl->allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001992
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001993 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001994 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001995 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001996 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1997 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1998 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001999 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002000 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002001
Alex Williamson23e258e2009-05-01 17:27:56 +00002002 sg_init_table(sg, 2);
2003
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002004 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02002005 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00002006 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002007 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00002008 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002009
2010 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002011 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002012
2013 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002014 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002015
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02002016 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00002017 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002018 netdev_for_each_mc_addr(ha, dev)
2019 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002020
2021 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002022 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002023
2024 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08002025 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01002026 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002027
2028 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00002029}
2030
Patrick McHardy80d5c362013-04-19 02:04:28 +00002031static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2032 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00002033{
2034 struct virtnet_info *vi = netdev_priv(dev);
2035 struct scatterlist sg;
2036
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03002037 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002038 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00002039
2040 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08002041 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00002042 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05002043 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00002044}
2045
Patrick McHardy80d5c362013-04-19 02:04:28 +00002046static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2047 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00002048{
2049 struct virtnet_info *vi = netdev_priv(dev);
2050 struct scatterlist sg;
2051
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03002052 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002053 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00002054
2055 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08002056 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00002057 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05002058 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00002059}
2060
Peter Xu310974f2019-03-18 14:56:06 +08002061static void virtnet_clean_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002062{
2063 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00002064
2065 if (vi->affinity_hint_set) {
2066 for (i = 0; i < vi->max_queue_pairs; i++) {
Caleb Raitto19e226e2018-08-09 18:18:28 -07002067 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2068 virtqueue_set_affinity(vi->sq[i].vq, NULL);
Wanlong Gao8898c212013-01-24 23:51:30 +00002069 }
2070
2071 vi->affinity_hint_set = false;
2072 }
Wanlong Gao8898c212013-01-24 23:51:30 +00002073}
2074
2075static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002076{
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002077 cpumask_var_t mask;
2078 int stragglers;
2079 int group_size;
2080 int i, j, cpu;
2081 int num_cpu;
2082 int stride;
Jason Wang986a4f42012-12-07 07:04:56 +00002083
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002084 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
Peter Xu310974f2019-03-18 14:56:06 +08002085 virtnet_clean_affinity(vi);
Wanlong Gao8898c212013-01-24 23:51:30 +00002086 return;
Jason Wang986a4f42012-12-07 07:04:56 +00002087 }
2088
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002089 num_cpu = num_online_cpus();
2090 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2091 stragglers = num_cpu >= vi->curr_queue_pairs ?
2092 num_cpu % vi->curr_queue_pairs :
2093 0;
2094 cpu = cpumask_next(-1, cpu_online_mask);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002095
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002096 for (i = 0; i < vi->curr_queue_pairs; i++) {
2097 group_size = stride + (i < stragglers ? 1 : 0);
2098
2099 for (j = 0; j < group_size; j++) {
2100 cpumask_set_cpu(cpu, mask);
2101 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2102 nr_cpu_ids, false);
2103 }
2104 virtqueue_set_affinity(vi->rq[i].vq, mask);
2105 virtqueue_set_affinity(vi->sq[i].vq, mask);
Antoine Tenart044ab862021-03-18 19:37:46 +01002106 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002107 cpumask_clear(mask);
Jason Wang986a4f42012-12-07 07:04:56 +00002108 }
2109
Wanlong Gao8898c212013-01-24 23:51:30 +00002110 vi->affinity_hint_set = true;
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002111 free_cpumask_var(mask);
Jason Wang986a4f42012-12-07 07:04:56 +00002112}
2113
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002114static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002115{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002116 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2117 node);
2118 virtnet_set_affinity(vi);
2119 return 0;
2120}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002121
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002122static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2123{
2124 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2125 node_dead);
2126 virtnet_set_affinity(vi);
2127 return 0;
2128}
Jason Wang3ab098d2013-10-15 11:18:58 +08002129
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002130static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2131{
2132 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2133 node);
2134
Peter Xu310974f2019-03-18 14:56:06 +08002135 virtnet_clean_affinity(vi);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002136 return 0;
2137}
2138
2139static enum cpuhp_state virtionet_online;
2140
2141static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2142{
2143 int ret;
2144
2145 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2146 if (ret)
2147 return ret;
2148 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2149 &vi->node_dead);
2150 if (!ret)
2151 return ret;
2152 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2153 return ret;
2154}
2155
2156static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2157{
2158 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2159 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2160 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08002161}
2162
Rick Jones8f9f4662011-10-19 08:10:59 +00002163static void virtnet_get_ringparam(struct net_device *dev,
2164 struct ethtool_ringparam *ring)
2165{
2166 struct virtnet_info *vi = netdev_priv(dev);
2167
Jason Wang986a4f42012-12-07 07:04:56 +00002168 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2169 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00002170 ring->rx_pending = ring->rx_max_pending;
2171 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00002172}
2173
Rick Jones66846042011-11-14 14:17:08 +00002174
2175static void virtnet_get_drvinfo(struct net_device *dev,
2176 struct ethtool_drvinfo *info)
2177{
2178 struct virtnet_info *vi = netdev_priv(dev);
2179 struct virtio_device *vdev = vi->vdev;
2180
2181 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2182 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2183 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2184
2185}
2186
Jason Wangd73bcd22012-12-07 07:04:57 +00002187/* TODO: Eliminate OOO packets during switching */
2188static int virtnet_set_channels(struct net_device *dev,
2189 struct ethtool_channels *channels)
2190{
2191 struct virtnet_info *vi = netdev_priv(dev);
2192 u16 queue_pairs = channels->combined_count;
2193 int err;
2194
2195 /* We don't support separate rx/tx channels.
2196 * We don't allow setting 'other' channels.
2197 */
2198 if (channels->rx_count || channels->tx_count || channels->other_count)
2199 return -EINVAL;
2200
Amos Kongc18e9cd2014-04-18 13:45:41 +08002201 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00002202 return -EINVAL;
2203
John Fastabendf600b692016-12-15 12:13:24 -08002204 /* For now we don't support modifying channels while XDP is loaded
2205 * also when XDP is loaded all RX queues have XDP programs so we only
2206 * need to check a single RX queue.
2207 */
2208 if (vi->rq[0].xdp_prog)
2209 return -EINVAL;
2210
Wanlong Gao47be2472013-01-24 23:51:29 +00002211 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08002212 err = _virtnet_set_queues(vi, queue_pairs);
Jeff Dikede332122020-12-22 21:54:21 -05002213 if (err) {
2214 put_online_cpus();
2215 goto err;
Jason Wangd73bcd22012-12-07 07:04:57 +00002216 }
Jeff Dikede332122020-12-22 21:54:21 -05002217 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002218 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00002219
Jeff Dikede332122020-12-22 21:54:21 -05002220 netif_set_real_num_tx_queues(dev, queue_pairs);
2221 netif_set_real_num_rx_queues(dev, queue_pairs);
2222 err:
Jason Wangd73bcd22012-12-07 07:04:57 +00002223 return err;
2224}
2225
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002226static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2227{
2228 struct virtnet_info *vi = netdev_priv(dev);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002229 unsigned int i, j;
Alexander Duyckd7a9a012021-03-16 17:31:29 -07002230 u8 *p = data;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002231
2232 switch (stringset) {
2233 case ETH_SS_STATS:
2234 for (i = 0; i < vi->curr_queue_pairs; i++) {
Alexander Duyckd7a9a012021-03-16 17:31:29 -07002235 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2236 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2237 virtnet_rq_stats_desc[j].desc);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002238 }
2239
2240 for (i = 0; i < vi->curr_queue_pairs; i++) {
Alexander Duyckd7a9a012021-03-16 17:31:29 -07002241 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2242 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2243 virtnet_sq_stats_desc[j].desc);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002244 }
2245 break;
2246 }
2247}
2248
2249static int virtnet_get_sset_count(struct net_device *dev, int sset)
2250{
2251 struct virtnet_info *vi = netdev_priv(dev);
2252
2253 switch (sset) {
2254 case ETH_SS_STATS:
2255 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2256 VIRTNET_SQ_STATS_LEN);
2257 default:
2258 return -EOPNOTSUPP;
2259 }
2260}
2261
2262static void virtnet_get_ethtool_stats(struct net_device *dev,
2263 struct ethtool_stats *stats, u64 *data)
2264{
2265 struct virtnet_info *vi = netdev_priv(dev);
2266 unsigned int idx = 0, start, i, j;
2267 const u8 *stats_base;
2268 size_t offset;
2269
2270 for (i = 0; i < vi->curr_queue_pairs; i++) {
2271 struct receive_queue *rq = &vi->rq[i];
2272
Jason Wangd46eeea2018-07-31 17:43:39 +08002273 stats_base = (u8 *)&rq->stats;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002274 do {
2275 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2276 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2277 offset = virtnet_rq_stats_desc[j].offset;
2278 data[idx + j] = *(u64 *)(stats_base + offset);
2279 }
2280 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2281 idx += VIRTNET_RQ_STATS_LEN;
2282 }
2283
2284 for (i = 0; i < vi->curr_queue_pairs; i++) {
2285 struct send_queue *sq = &vi->sq[i];
2286
2287 stats_base = (u8 *)&sq->stats;
2288 do {
2289 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2290 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2291 offset = virtnet_sq_stats_desc[j].offset;
2292 data[idx + j] = *(u64 *)(stats_base + offset);
2293 }
2294 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2295 idx += VIRTNET_SQ_STATS_LEN;
2296 }
2297}
2298
Jason Wangd73bcd22012-12-07 07:04:57 +00002299static void virtnet_get_channels(struct net_device *dev,
2300 struct ethtool_channels *channels)
2301{
2302 struct virtnet_info *vi = netdev_priv(dev);
2303
2304 channels->combined_count = vi->curr_queue_pairs;
2305 channels->max_combined = vi->max_queue_pairs;
2306 channels->max_other = 0;
2307 channels->rx_count = 0;
2308 channels->tx_count = 0;
2309 channels->other_count = 0;
2310}
2311
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002312static int virtnet_set_link_ksettings(struct net_device *dev,
2313 const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002314{
2315 struct virtnet_info *vi = netdev_priv(dev);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002316
Cris Forno9aedc6e22020-02-28 14:12:05 -06002317 return ethtool_virtdev_set_link_ksettings(dev, cmd,
2318 &vi->speed, &vi->duplex);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002319}
2320
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002321static int virtnet_get_link_ksettings(struct net_device *dev,
2322 struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002323{
2324 struct virtnet_info *vi = netdev_priv(dev);
2325
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002326 cmd->base.speed = vi->speed;
2327 cmd->base.duplex = vi->duplex;
2328 cmd->base.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002329
2330 return 0;
2331}
2332
Jason Wang0c465be2018-10-09 10:06:26 +08002333static int virtnet_set_coalesce(struct net_device *dev,
2334 struct ethtool_coalesce *ec)
2335{
Jason Wang0c465be2018-10-09 10:06:26 +08002336 struct virtnet_info *vi = netdev_priv(dev);
2337 int i, napi_weight;
2338
Jakub Kicinskia51e5202020-03-04 21:15:42 -08002339 if (ec->tx_max_coalesced_frames > 1 ||
2340 ec->rx_max_coalesced_frames != 1)
Jason Wang0c465be2018-10-09 10:06:26 +08002341 return -EINVAL;
2342
Jason Wang0c465be2018-10-09 10:06:26 +08002343 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
Jason Wang0c465be2018-10-09 10:06:26 +08002344 if (napi_weight ^ vi->sq[0].napi.weight) {
2345 if (dev->flags & IFF_UP)
2346 return -EBUSY;
2347 for (i = 0; i < vi->max_queue_pairs; i++)
2348 vi->sq[i].napi.weight = napi_weight;
2349 }
2350
2351 return 0;
2352}
2353
2354static int virtnet_get_coalesce(struct net_device *dev,
2355 struct ethtool_coalesce *ec)
2356{
2357 struct ethtool_coalesce ec_default = {
2358 .cmd = ETHTOOL_GCOALESCE,
2359 .rx_max_coalesced_frames = 1,
2360 };
2361 struct virtnet_info *vi = netdev_priv(dev);
2362
2363 memcpy(ec, &ec_default, sizeof(ec_default));
2364
2365 if (vi->sq[0].napi.weight)
2366 ec->tx_max_coalesced_frames = 1;
2367
2368 return 0;
2369}
2370
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002371static void virtnet_init_settings(struct net_device *dev)
2372{
2373 struct virtnet_info *vi = netdev_priv(dev);
2374
2375 vi->speed = SPEED_UNKNOWN;
2376 vi->duplex = DUPLEX_UNKNOWN;
2377}
2378
Jason Baronfaa9b392018-01-05 17:44:54 -05002379static void virtnet_update_settings(struct virtnet_info *vi)
2380{
2381 u32 speed;
2382 u8 duplex;
2383
2384 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2385 return;
2386
Michael S. Tsirkin64ffa392020-08-05 05:39:36 -04002387 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
2388
Jason Baronfaa9b392018-01-05 17:44:54 -05002389 if (ethtool_validate_speed(speed))
2390 vi->speed = speed;
Michael S. Tsirkin64ffa392020-08-05 05:39:36 -04002391
2392 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
2393
Jason Baronfaa9b392018-01-05 17:44:54 -05002394 if (ethtool_validate_duplex(duplex))
2395 vi->duplex = duplex;
2396}
2397
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002398static const struct ethtool_ops virtnet_ethtool_ops = {
Jakub Kicinskia51e5202020-03-04 21:15:42 -08002399 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
Rick Jones66846042011-11-14 14:17:08 +00002400 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002401 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00002402 .get_ringparam = virtnet_get_ringparam,
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002403 .get_strings = virtnet_get_strings,
2404 .get_sset_count = virtnet_get_sset_count,
2405 .get_ethtool_stats = virtnet_get_ethtool_stats,
Jason Wangd73bcd22012-12-07 07:04:57 +00002406 .set_channels = virtnet_set_channels,
2407 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00002408 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002409 .get_link_ksettings = virtnet_get_link_ksettings,
2410 .set_link_ksettings = virtnet_set_link_ksettings,
Jason Wang0c465be2018-10-09 10:06:26 +08002411 .set_coalesce = virtnet_set_coalesce,
2412 .get_coalesce = virtnet_get_coalesce,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08002413};
2414
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002415static void virtnet_freeze_down(struct virtio_device *vdev)
2416{
2417 struct virtnet_info *vi = vdev->priv;
2418 int i;
2419
2420 /* Make sure no work handler is accessing the device */
2421 flush_work(&vi->config_work);
2422
Ake Koomsin05c998b2018-10-17 19:44:12 +09002423 netif_tx_lock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002424 netif_device_detach(vi->dev);
Ake Koomsin05c998b2018-10-17 19:44:12 +09002425 netif_tx_unlock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002426 cancel_delayed_work_sync(&vi->refill);
2427
2428 if (netif_running(vi->dev)) {
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002429 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002430 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04002431 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002432 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002433 }
2434}
2435
2436static int init_vqs(struct virtnet_info *vi);
2437
2438static int virtnet_restore_up(struct virtio_device *vdev)
2439{
2440 struct virtnet_info *vi = vdev->priv;
2441 int err, i;
2442
2443 err = init_vqs(vi);
2444 if (err)
2445 return err;
2446
2447 virtio_device_ready(vdev);
2448
2449 if (netif_running(vi->dev)) {
2450 for (i = 0; i < vi->curr_queue_pairs; i++)
2451 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2452 schedule_delayed_work(&vi->refill, 0);
2453
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002454 for (i = 0; i < vi->max_queue_pairs; i++) {
Willem de Bruijne4e84522017-04-24 13:49:26 -04002455 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002456 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2457 &vi->sq[i].napi);
2458 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002459 }
2460
Ake Koomsin05c998b2018-10-17 19:44:12 +09002461 netif_tx_lock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002462 netif_device_attach(vi->dev);
Ake Koomsin05c998b2018-10-17 19:44:12 +09002463 netif_tx_unlock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002464 return err;
2465}
2466
Jason Wang3f935222017-07-19 16:54:49 +08002467static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2468{
2469 struct scatterlist sg;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002470 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
Jason Wang3f935222017-07-19 16:54:49 +08002471
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002472 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
Jason Wang3f935222017-07-19 16:54:49 +08002473
2474 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2475 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
Yuval Shaia7934b482019-04-03 12:10:13 +03002476 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
Jason Wang3f935222017-07-19 16:54:49 +08002477 return -EINVAL;
2478 }
2479
2480 return 0;
2481}
2482
2483static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2484{
2485 u64 offloads = 0;
2486
2487 if (!vi->guest_offloads)
2488 return 0;
2489
Jason Wang3f935222017-07-19 16:54:49 +08002490 return virtnet_set_guest_offloads(vi, offloads);
2491}
2492
2493static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2494{
2495 u64 offloads = vi->guest_offloads;
2496
2497 if (!vi->guest_offloads)
2498 return 0;
Jason Wang3f935222017-07-19 16:54:49 +08002499
2500 return virtnet_set_guest_offloads(vi, offloads);
2501}
2502
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002503static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2504 struct netlink_ext_ack *extack)
John Fastabendf600b692016-12-15 12:13:24 -08002505{
2506 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2507 struct virtnet_info *vi = netdev_priv(dev);
2508 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08002509 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002510 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08002511
Jason Wang3f935222017-07-19 16:54:49 +08002512 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2513 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2514 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2515 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
Jason Wang18ba58e2018-11-22 14:36:31 +08002516 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2517 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
2518 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first");
John Fastabendf600b692016-12-15 12:13:24 -08002519 return -EOPNOTSUPP;
2520 }
2521
2522 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002523 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
John Fastabendf600b692016-12-15 12:13:24 -08002524 return -EINVAL;
2525 }
2526
2527 if (dev->mtu > max_sz) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002528 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
John Fastabendf600b692016-12-15 12:13:24 -08002529 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2530 return -EINVAL;
2531 }
2532
John Fastabend672aafd2016-12-15 12:13:49 -08002533 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2534 if (prog)
2535 xdp_qp = nr_cpu_ids;
2536
2537 /* XDP requires extra queues for XDP_TX */
2538 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +08002539 netdev_warn(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
John Fastabend672aafd2016-12-15 12:13:49 -08002540 curr_qp + xdp_qp, vi->max_queue_pairs);
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +08002541 xdp_qp = 0;
John Fastabend672aafd2016-12-15 12:13:49 -08002542 }
2543
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002544 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
2545 if (!prog && !old_prog)
2546 return 0;
2547
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002548 if (prog)
2549 bpf_prog_add(prog, vi->max_queue_pairs - 1);
John Fastabend2de2f7f2017-02-02 19:16:29 -08002550
Jason Wang4941d472017-07-19 16:54:48 +08002551 /* Make sure NAPI is not using any XDP TX queues for RX. */
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002552 if (netif_running(dev)) {
2553 for (i = 0; i < vi->max_queue_pairs; i++) {
Jason Wang4e09ff52018-02-28 18:20:04 +08002554 napi_disable(&vi->rq[i].napi);
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002555 virtnet_napi_tx_disable(&vi->sq[i].napi);
2556 }
2557 }
John Fastabendf600b692016-12-15 12:13:24 -08002558
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002559 if (!prog) {
2560 for (i = 0; i < vi->max_queue_pairs; i++) {
2561 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2562 if (i == 0)
2563 virtnet_restore_guest_offloads(vi);
2564 }
2565 synchronize_net();
2566 }
2567
Jason Wang4941d472017-07-19 16:54:48 +08002568 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2569 if (err)
2570 goto err;
Toshiaki Makita188313c2019-01-29 09:45:55 +09002571 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
Jason Wang4941d472017-07-19 16:54:48 +08002572 vi->xdp_queue_pairs = xdp_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002573
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002574 if (prog) {
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +08002575 vi->xdp_enabled = true;
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002576 for (i = 0; i < vi->max_queue_pairs; i++) {
2577 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2578 if (i == 0 && !old_prog)
Jason Wang3f935222017-07-19 16:54:49 +08002579 virtnet_clear_guest_offloads(vi);
Jason Wang3f935222017-07-19 16:54:49 +08002580 }
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +08002581 } else {
2582 vi->xdp_enabled = false;
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002583 }
2584
2585 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabendf600b692016-12-15 12:13:24 -08002586 if (old_prog)
2587 bpf_prog_put(old_prog);
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002588 if (netif_running(dev)) {
Jason Wang4e09ff52018-02-28 18:20:04 +08002589 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002590 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2591 &vi->sq[i].napi);
2592 }
John Fastabendf600b692016-12-15 12:13:24 -08002593 }
2594
2595 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08002596
Jason Wang4941d472017-07-19 16:54:48 +08002597err:
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002598 if (!prog) {
2599 virtnet_clear_guest_offloads(vi);
2600 for (i = 0; i < vi->max_queue_pairs; i++)
2601 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
2602 }
2603
Toshiaki Makita8be4d9a2019-01-29 09:45:53 +09002604 if (netif_running(dev)) {
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002605 for (i = 0; i < vi->max_queue_pairs; i++) {
Toshiaki Makita8be4d9a2019-01-29 09:45:53 +09002606 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002607 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2608 &vi->sq[i].napi);
2609 }
Toshiaki Makita8be4d9a2019-01-29 09:45:53 +09002610 }
John Fastabend2de2f7f2017-02-02 19:16:29 -08002611 if (prog)
2612 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2613 return err;
John Fastabendf600b692016-12-15 12:13:24 -08002614}
2615
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002616static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
John Fastabendf600b692016-12-15 12:13:24 -08002617{
2618 switch (xdp->command) {
2619 case XDP_SETUP_PROG:
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002620 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
John Fastabendf600b692016-12-15 12:13:24 -08002621 default:
2622 return -EINVAL;
2623 }
2624}
2625
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002626static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2627 size_t len)
2628{
2629 struct virtnet_info *vi = netdev_priv(dev);
2630 int ret;
2631
2632 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2633 return -EOPNOTSUPP;
2634
2635 ret = snprintf(buf, len, "sby");
2636 if (ret >= len)
2637 return -EOPNOTSUPP;
2638
2639 return 0;
2640}
2641
Willem de Bruijna02e8962018-12-20 17:14:54 -05002642static int virtnet_set_features(struct net_device *dev,
2643 netdev_features_t features)
2644{
2645 struct virtnet_info *vi = netdev_priv(dev);
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002646 u64 offloads;
Willem de Bruijna02e8962018-12-20 17:14:54 -05002647 int err;
2648
2649 if ((dev->features ^ features) & NETIF_F_LRO) {
Xuan Zhuo97c2c69e2021-03-10 10:24:45 +08002650 if (vi->xdp_enabled)
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002651 return -EBUSY;
2652
Willem de Bruijna02e8962018-12-20 17:14:54 -05002653 if (features & NETIF_F_LRO)
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002654 offloads = vi->guest_offloads_capable;
Willem de Bruijna02e8962018-12-20 17:14:54 -05002655 else
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002656 offloads = vi->guest_offloads_capable &
2657 ~GUEST_OFFLOAD_LRO_MASK;
2658
2659 err = virtnet_set_guest_offloads(vi, offloads);
2660 if (err)
2661 return err;
2662 vi->guest_offloads = offloads;
Willem de Bruijna02e8962018-12-20 17:14:54 -05002663 }
2664
2665 return 0;
2666}
2667
Stephen Hemminger76288b42009-01-06 10:44:22 -08002668static const struct net_device_ops virtnet_netdev = {
2669 .ndo_open = virtnet_open,
2670 .ndo_stop = virtnet_close,
2671 .ndo_start_xmit = start_xmit,
2672 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08002673 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00002674 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002675 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00002676 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2677 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002678 .ndo_bpf = virtnet_xdp,
Jason Wang186b3c92017-09-19 17:42:43 +08002679 .ndo_xdp_xmit = virtnet_xdp_xmit,
Vlad Yasevich2836b4f2017-05-23 13:38:43 -04002680 .ndo_features_check = passthru_features_check,
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002681 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
Willem de Bruijna02e8962018-12-20 17:14:54 -05002682 .ndo_set_features = virtnet_set_features,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002683};
2684
Jason Wang586d17c2012-04-11 20:43:52 +00002685static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002686{
Jason Wang586d17c2012-04-11 20:43:52 +00002687 struct virtnet_info *vi =
2688 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002689 u16 v;
2690
Rusty Russell855e0c52013-10-14 18:11:51 +10302691 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2692 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302693 return;
Jason Wang586d17c2012-04-11 20:43:52 +00002694
2695 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00002696 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00002697 virtnet_ack_link_announce(vi);
2698 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002699
2700 /* Ignore unknown (future) status bits */
2701 v &= VIRTIO_NET_S_LINK_UP;
2702
2703 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302704 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002705
2706 vi->status = v;
2707
2708 if (vi->status & VIRTIO_NET_S_LINK_UP) {
Jason Baronfaa9b392018-01-05 17:44:54 -05002709 virtnet_update_settings(vi);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002710 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002711 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002712 } else {
2713 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002714 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002715 }
2716}
2717
2718static void virtnet_config_changed(struct virtio_device *vdev)
2719{
2720 struct virtnet_info *vi = vdev->priv;
2721
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002722 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002723}
2724
Jason Wang986a4f42012-12-07 07:04:56 +00002725static void virtnet_free_queues(struct virtnet_info *vi)
2726{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002727 int i;
2728
Jason Wangab3971b2015-03-12 13:57:44 +08002729 for (i = 0; i < vi->max_queue_pairs; i++) {
Jakub Kicinski5198d5452020-09-09 10:37:51 -07002730 __netif_napi_del(&vi->rq[i].napi);
2731 __netif_napi_del(&vi->sq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08002732 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002733
Jakub Kicinski5198d5452020-09-09 10:37:51 -07002734 /* We called __netif_napi_del(),
Eric Dumazet963abe52016-11-15 22:24:12 -08002735 * we need to respect an RCU grace period before freeing vi->rq
2736 */
2737 synchronize_net();
2738
Jason Wang986a4f42012-12-07 07:04:56 +00002739 kfree(vi->rq);
2740 kfree(vi->sq);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002741 kfree(vi->ctrl);
Jason Wang986a4f42012-12-07 07:04:56 +00002742}
2743
John Fastabend473153292017-02-02 19:14:32 -08002744static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002745{
John Fastabendf600b692016-12-15 12:13:24 -08002746 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00002747 int i;
2748
2749 for (i = 0; i < vi->max_queue_pairs; i++) {
2750 while (vi->rq[i].pages)
2751 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08002752
2753 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2754 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2755 if (old_prog)
2756 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00002757 }
John Fastabend473153292017-02-02 19:14:32 -08002758}
2759
2760static void free_receive_bufs(struct virtnet_info *vi)
2761{
2762 rtnl_lock();
2763 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08002764 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002765}
2766
Michael Daltonfb518792014-01-16 22:23:26 -08002767static void free_receive_page_frags(struct virtnet_info *vi)
2768{
2769 int i;
2770 for (i = 0; i < vi->max_queue_pairs; i++)
2771 if (vi->rq[i].alloc_frag.page)
2772 put_page(vi->rq[i].alloc_frag.page);
2773}
2774
Jason Wang986a4f42012-12-07 07:04:56 +00002775static void free_unused_bufs(struct virtnet_info *vi)
2776{
2777 void *buf;
2778 int i;
2779
2780 for (i = 0; i < vi->max_queue_pairs; i++) {
2781 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002782 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Toshiaki Makita50504712019-01-29 09:45:59 +09002783 if (!is_xdp_frame(buf))
John Fastabend56434a02016-12-15 12:14:13 -08002784 dev_kfree_skb(buf);
2785 else
Toshiaki Makita50504712019-01-29 09:45:59 +09002786 xdp_return_frame(ptr_to_xdp(buf));
John Fastabend56434a02016-12-15 12:14:13 -08002787 }
Jason Wang986a4f42012-12-07 07:04:56 +00002788 }
2789
2790 for (i = 0; i < vi->max_queue_pairs; i++) {
2791 struct virtqueue *vq = vi->rq[i].vq;
2792
2793 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002794 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02002795 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002796 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002797 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002798 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002799 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002800 }
Jason Wang986a4f42012-12-07 07:04:56 +00002801 }
Jason Wang986a4f42012-12-07 07:04:56 +00002802 }
2803}
2804
Jason Wange9d74172012-12-07 07:04:55 +00002805static void virtnet_del_vqs(struct virtnet_info *vi)
2806{
2807 struct virtio_device *vdev = vi->vdev;
2808
Peter Xu310974f2019-03-18 14:56:06 +08002809 virtnet_clean_affinity(vi);
Jason Wang986a4f42012-12-07 07:04:56 +00002810
Jason Wange9d74172012-12-07 07:04:55 +00002811 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002812
2813 virtnet_free_queues(vi);
2814}
2815
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002816/* How large should a single buffer be so a queue full of these can fit at
2817 * least one full packet?
2818 * Logic below assumes the mergeable buffer header is used.
2819 */
2820static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2821{
2822 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2823 unsigned int rq_size = virtqueue_get_vring_size(vq);
2824 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2825 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2826 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2827
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03002828 return max(max(min_buf_len, hdr_len) - hdr_len,
2829 (unsigned int)GOOD_PACKET_LEN);
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002830}
2831
Jason Wang986a4f42012-12-07 07:04:56 +00002832static int virtnet_find_vqs(struct virtnet_info *vi)
2833{
2834 vq_callback_t **callbacks;
2835 struct virtqueue **vqs;
2836 int ret = -ENOMEM;
2837 int i, total_vqs;
2838 const char **names;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002839 bool *ctx;
Jason Wang986a4f42012-12-07 07:04:56 +00002840
2841 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2842 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2843 * possible control vq.
2844 */
2845 total_vqs = vi->max_queue_pairs * 2 +
2846 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2847
2848 /* Allocate space for find_vqs parameters */
Kees Cook6396bb22018-06-12 14:03:40 -07002849 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002850 if (!vqs)
2851 goto err_vq;
Kees Cook6da2ec52018-06-12 13:55:00 -07002852 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002853 if (!callbacks)
2854 goto err_callback;
Kees Cook6da2ec52018-06-12 13:55:00 -07002855 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002856 if (!names)
2857 goto err_names;
Jason Wang192f68c2017-07-19 16:54:47 +08002858 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Kees Cook6396bb22018-06-12 14:03:40 -07002859 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002860 if (!ctx)
2861 goto err_ctx;
2862 } else {
2863 ctx = NULL;
2864 }
Jason Wang986a4f42012-12-07 07:04:56 +00002865
2866 /* Parameters for control virtqueue, if any */
2867 if (vi->has_cvq) {
2868 callbacks[total_vqs - 1] = NULL;
2869 names[total_vqs - 1] = "control";
2870 }
2871
2872 /* Allocate/initialize parameters for send/receive virtqueues */
2873 for (i = 0; i < vi->max_queue_pairs; i++) {
2874 callbacks[rxq2vq(i)] = skb_recv_done;
2875 callbacks[txq2vq(i)] = skb_xmit_done;
2876 sprintf(vi->rq[i].name, "input.%d", i);
2877 sprintf(vi->sq[i].name, "output.%d", i);
2878 names[rxq2vq(i)] = vi->rq[i].name;
2879 names[txq2vq(i)] = vi->sq[i].name;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002880 if (ctx)
2881 ctx[rxq2vq(i)] = true;
Jason Wang986a4f42012-12-07 07:04:56 +00002882 }
2883
Xianting Tiana2f7dc02021-06-23 11:16:22 -04002884 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
2885 names, ctx, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002886 if (ret)
2887 goto err_find;
2888
2889 if (vi->has_cvq) {
2890 vi->cvq = vqs[total_vqs - 1];
2891 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002892 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002893 }
2894
2895 for (i = 0; i < vi->max_queue_pairs; i++) {
2896 vi->rq[i].vq = vqs[rxq2vq(i)];
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002897 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
Jason Wang986a4f42012-12-07 07:04:56 +00002898 vi->sq[i].vq = vqs[txq2vq(i)];
2899 }
2900
Tonghao Zhang2fa3c8a2018-05-31 07:16:32 -07002901 /* run here: ret == 0. */
Jason Wang986a4f42012-12-07 07:04:56 +00002902
Jason Wang986a4f42012-12-07 07:04:56 +00002903
2904err_find:
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002905 kfree(ctx);
2906err_ctx:
Jason Wang986a4f42012-12-07 07:04:56 +00002907 kfree(names);
2908err_names:
2909 kfree(callbacks);
2910err_callback:
2911 kfree(vqs);
2912err_vq:
2913 return ret;
2914}
2915
2916static int virtnet_alloc_queues(struct virtnet_info *vi)
2917{
2918 int i;
2919
Max Gurtovoy122b84a2021-05-02 12:33:19 +03002920 if (vi->has_cvq) {
2921 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2922 if (!vi->ctrl)
2923 goto err_ctrl;
2924 } else {
2925 vi->ctrl = NULL;
2926 }
Kees Cook6396bb22018-06-12 14:03:40 -07002927 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002928 if (!vi->sq)
2929 goto err_sq;
Kees Cook6396bb22018-06-12 14:03:40 -07002930 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002931 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002932 goto err_rq;
2933
2934 INIT_DELAYED_WORK(&vi->refill, refill_work);
2935 for (i = 0; i < vi->max_queue_pairs; i++) {
2936 vi->rq[i].pages = NULL;
2937 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2938 napi_weight);
Willem de Bruijn1d11e732017-04-27 20:37:58 -04002939 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2940 napi_tx ? napi_weight : 0);
Jason Wang986a4f42012-12-07 07:04:56 +00002941
2942 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002943 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002944 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002945
2946 u64_stats_init(&vi->rq[i].stats.syncp);
2947 u64_stats_init(&vi->sq[i].stats.syncp);
Jason Wang986a4f42012-12-07 07:04:56 +00002948 }
2949
2950 return 0;
2951
2952err_rq:
2953 kfree(vi->sq);
2954err_sq:
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002955 kfree(vi->ctrl);
2956err_ctrl:
Jason Wang986a4f42012-12-07 07:04:56 +00002957 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002958}
2959
Amit Shah3f9c10b2011-12-22 16:58:31 +05302960static int init_vqs(struct virtnet_info *vi)
2961{
Jason Wang986a4f42012-12-07 07:04:56 +00002962 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302963
Jason Wang986a4f42012-12-07 07:04:56 +00002964 /* Allocate send & receive queues */
2965 ret = virtnet_alloc_queues(vi);
2966 if (ret)
2967 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302968
Jason Wang986a4f42012-12-07 07:04:56 +00002969 ret = virtnet_find_vqs(vi);
2970 if (ret)
2971 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302972
Wanlong Gao47be2472013-01-24 23:51:29 +00002973 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002974 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002975 put_online_cpus();
2976
Amit Shah3f9c10b2011-12-22 16:58:31 +05302977 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002978
2979err_free:
2980 virtnet_free_queues(vi);
2981err:
2982 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302983}
2984
Michael Daltonfbf28d72014-01-16 22:23:30 -08002985#ifdef CONFIG_SYSFS
2986static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -07002987 char *buf)
Michael Daltonfbf28d72014-01-16 22:23:30 -08002988{
2989 struct virtnet_info *vi = netdev_priv(queue->dev);
2990 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Jason Wang3cc81a92018-03-02 17:29:14 +08002991 unsigned int headroom = virtnet_get_headroom(vi);
2992 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
Johannes Berg5377d7582015-08-19 09:48:40 +02002993 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002994
2995 BUG_ON(queue_index >= vi->max_queue_pairs);
2996 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002997 return sprintf(buf, "%u\n",
Jason Wang3cc81a92018-03-02 17:29:14 +08002998 get_mergeable_buf_len(&vi->rq[queue_index], avg,
2999 SKB_DATA_ALIGN(headroom + tailroom)));
Michael Daltonfbf28d72014-01-16 22:23:30 -08003000}
3001
3002static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3003 __ATTR_RO(mergeable_rx_buffer_size);
3004
3005static struct attribute *virtio_net_mrg_rx_attrs[] = {
3006 &mergeable_rx_buffer_size_attribute.attr,
3007 NULL
3008};
3009
3010static const struct attribute_group virtio_net_mrg_rx_group = {
3011 .name = "virtio_net",
3012 .attrs = virtio_net_mrg_rx_attrs
3013};
3014#endif
3015
Jason Wang892d6eb2014-11-20 17:03:05 +08003016static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3017 unsigned int fbit,
3018 const char *fname, const char *dname)
3019{
3020 if (!virtio_has_feature(vdev, fbit))
3021 return false;
3022
3023 dev_err(&vdev->dev, "device advertises feature %s but not %s",
3024 fname, dname);
3025
3026 return true;
3027}
3028
3029#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
3030 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3031
3032static bool virtnet_validate_features(struct virtio_device *vdev)
3033{
3034 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3035 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3036 "VIRTIO_NET_F_CTRL_VQ") ||
3037 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3038 "VIRTIO_NET_F_CTRL_VQ") ||
3039 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3040 "VIRTIO_NET_F_CTRL_VQ") ||
3041 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3042 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3043 "VIRTIO_NET_F_CTRL_VQ"))) {
3044 return false;
3045 }
3046
3047 return true;
3048}
3049
Jarod Wilsond0c2c992016-10-20 13:55:21 -04003050#define MIN_MTU ETH_MIN_MTU
3051#define MAX_MTU ETH_MAX_MTU
3052
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003053static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10003054{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02003055 if (!vdev->config->get) {
3056 dev_err(&vdev->dev, "%s failure: config access disabled\n",
3057 __func__);
3058 return -EINVAL;
3059 }
3060
Jason Wang892d6eb2014-11-20 17:03:05 +08003061 if (!virtnet_validate_features(vdev))
3062 return -EINVAL;
3063
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003064 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3065 int mtu = virtio_cread16(vdev,
3066 offsetof(struct virtio_net_config,
3067 mtu));
3068 if (mtu < MIN_MTU)
3069 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3070 }
3071
3072 return 0;
3073}
3074
3075static int virtnet_probe(struct virtio_device *vdev)
3076{
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003077 int i, err = -ENOMEM;
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003078 struct net_device *dev;
3079 struct virtnet_info *vi;
3080 u16 max_queue_pairs;
3081 int mtu;
3082
Jason Wang986a4f42012-12-07 07:04:56 +00003083 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10303084 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
3085 struct virtio_net_config,
3086 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00003087
3088 /* We need at least 2 queue's */
3089 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3090 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3091 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3092 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10003093
3094 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00003095 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10003096 if (!dev)
3097 return -ENOMEM;
3098
3099 /* Set up network device as normal. */
Xuan Zhuoab5bd582021-02-18 20:50:17 +00003100 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3101 IFF_TX_SKB_NO_LINEAR;
Stephen Hemminger76288b42009-01-06 10:44:22 -08003102 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10003103 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00003104
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003105 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10003106 SET_NETDEV_DEV(dev, &vdev->dev);
3107
3108 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00003109 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10003110 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08003111 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00003112 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08003113 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00003114
3115 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
David S. Millere078de02017-07-03 06:37:32 -07003116 dev->hw_features |= NETIF_F_TSO
Rusty Russell34a48572008-02-04 23:50:02 -05003117 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
3118 }
Rusty Russell5539ae962008-05-02 21:50:46 -05003119 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00003120 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
3121 dev->hw_features |= NETIF_F_TSO;
3122 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
3123 dev->hw_features |= NETIF_F_TSO6;
3124 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
3125 dev->hw_features |= NETIF_F_TSO_ECN;
Michał Mirosław98e778c2011-03-31 01:01:35 +00003126
Jason Wang41f2f122014-12-24 11:03:52 +08003127 dev->features |= NETIF_F_GSO_ROBUST;
3128
Michał Mirosław98e778c2011-03-31 01:01:35 +00003129 if (gso)
David S. Millere078de02017-07-03 06:37:32 -07003130 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00003131 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10003132 }
Thomas Huth4f491292013-08-27 17:09:02 +02003133 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
3134 dev->features |= NETIF_F_RXCSUM;
Willem de Bruijna02e8962018-12-20 17:14:54 -05003135 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3136 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3137 dev->features |= NETIF_F_LRO;
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04003138 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
Willem de Bruijna02e8962018-12-20 17:14:54 -05003139 dev->hw_features |= NETIF_F_LRO;
Rusty Russell296f96f2007-10-22 11:03:37 +10003140
Jason Wang4fda8302013-04-10 23:32:21 +00003141 dev->vlan_features = dev->features;
3142
Jarod Wilsond0c2c992016-10-20 13:55:21 -04003143 /* MTU range: 68 - 65535 */
3144 dev->min_mtu = MIN_MTU;
3145 dev->max_mtu = MAX_MTU;
3146
Rusty Russell296f96f2007-10-22 11:03:37 +10003147 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10303148 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
3149 virtio_cread_bytes(vdev,
3150 offsetof(struct virtio_net_config, mac),
3151 dev->dev_addr, dev->addr_len);
3152 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00003153 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003154
3155 /* Set up our device-specific information */
3156 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003157 vi->dev = dev;
3158 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01003159 vdev->priv = vi;
John Stultz827da442013-10-07 15:51:58 -07003160
Jason Wang586d17c2012-04-11 20:43:52 +00003161 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10003162
Herbert Xu97402b92008-04-18 11:24:27 +08003163 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00003164 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3165 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05003166 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
3167 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08003168 vi->big_packets = true;
3169
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08003170 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
3171 vi->mergeable_rx_bufs = true;
3172
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03003173 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3174 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03003175 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3176 else
3177 vi->hdr_len = sizeof(struct virtio_net_hdr);
3178
Michael S. Tsirkin75993302015-07-15 15:26:19 +03003179 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
3180 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09303181 vi->any_header_sg = true;
3182
Jason Wang986a4f42012-12-07 07:04:56 +00003183 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3184 vi->has_cvq = true;
3185
Aaron Conole14de9d12016-06-03 16:57:12 -04003186 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3187 mtu = virtio_cread16(vdev,
3188 offsetof(struct virtio_net_config,
3189 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04003190 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003191 /* Should never trigger: MTU was previously validated
3192 * in virtnet_validate.
3193 */
Yuval Shaia7934b482019-04-03 12:10:13 +03003194 dev_err(&vdev->dev,
3195 "device MTU appears to have changed it is now %d < %d",
3196 mtu, dev->min_mtu);
Dan Carpenter411ea232020-12-04 17:23:16 +03003197 err = -EINVAL;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003198 goto free;
Aaron Conole93a205e2016-10-25 16:12:12 -04003199 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02003200
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003201 dev->mtu = mtu;
3202 dev->max_mtu = mtu;
3203
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02003204 /* TODO: size buffers correctly in this case. */
3205 if (dev->mtu > ETH_DATA_LEN)
3206 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04003207 }
3208
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03003209 if (vi->any_header_sg)
3210 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08003211
Jason Wang44900012016-11-25 12:37:26 +08003212 /* Enable multiqueue by default */
3213 if (num_online_cpus() >= max_queue_pairs)
3214 vi->curr_queue_pairs = max_queue_pairs;
3215 else
3216 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00003217 vi->max_queue_pairs = max_queue_pairs;
3218
3219 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05303220 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003221 if (err)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003222 goto free;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003223
Michael Daltonfbf28d72014-01-16 22:23:30 -08003224#ifdef CONFIG_SYSFS
3225 if (vi->mergeable_rx_bufs)
3226 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3227#endif
Zhi Yong Wu0f13b66b2013-11-18 21:19:27 +08003228 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3229 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00003230
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01003231 virtnet_init_settings(dev);
3232
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003233 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3234 vi->failover = net_failover_create(vi->dev);
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00003235 if (IS_ERR(vi->failover)) {
3236 err = PTR_ERR(vi->failover);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003237 goto free_vqs;
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00003238 }
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003239 }
3240
Rusty Russell296f96f2007-10-22 11:03:37 +10003241 err = register_netdev(dev);
3242 if (err) {
3243 pr_debug("virtio_net: registering device failed\n");
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003244 goto free_failover;
Rusty Russell296f96f2007-10-22 11:03:37 +10003245 }
Rusty Russellb3369c12008-02-04 23:50:02 -05003246
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10303247 virtio_device_ready(vdev);
3248
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003249 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003250 if (err) {
3251 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08003252 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003253 }
3254
Jason Wanga2208712016-12-13 14:23:05 +08003255 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08003256
Jason Wang167c25e2010-11-10 14:45:41 +00003257 /* Assume link up if device can't report link status,
3258 otherwise get link status from config. */
Jay Vosburghbda7fab2018-03-22 14:42:41 +00003259 netif_carrier_off(dev);
Jason Wang167c25e2010-11-10 14:45:41 +00003260 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
Tejun Heo3b07e9c2012-08-20 14:51:24 -07003261 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00003262 } else {
3263 vi->status = VIRTIO_NET_S_LINK_UP;
Jason Baronfaa9b392018-01-05 17:44:54 -05003264 virtnet_update_settings(vi);
Jason Wang167c25e2010-11-10 14:45:41 +00003265 netif_carrier_on(dev);
3266 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003267
Jason Wang3f935222017-07-19 16:54:49 +08003268 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3269 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3270 set_bit(guest_offloads[i], &vi->guest_offloads);
Willem de Bruijna02e8962018-12-20 17:14:54 -05003271 vi->guest_offloads_capable = vi->guest_offloads;
Jason Wang3f935222017-07-19 16:54:49 +08003272
Jason Wang986a4f42012-12-07 07:04:56 +00003273 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3274 dev->name, max_queue_pairs);
3275
Rusty Russell296f96f2007-10-22 11:03:37 +10003276 return 0;
3277
wangyunjianf00e35e2016-05-31 11:52:43 +08003278free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10303279 vi->vdev->config->reset(vdev);
3280
Rusty Russellb3369c12008-02-04 23:50:02 -05003281 unregister_netdev(dev);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003282free_failover:
3283 net_failover_destroy(vi->failover);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003284free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00003285 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08003286 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00003287 virtnet_del_vqs(vi);
Rusty Russell296f96f2007-10-22 11:03:37 +10003288free:
3289 free_netdev(dev);
3290 return err;
3291}
3292
Amit Shah04486ed2011-12-22 16:58:32 +05303293static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10003294{
Amit Shah04486ed2011-12-22 16:58:32 +05303295 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00003296
3297 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00003298 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003299
Jason Wang986a4f42012-12-07 07:04:56 +00003300 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003301
Michael Daltonfb518792014-01-16 22:23:26 -08003302 free_receive_page_frags(vi);
3303
Jason Wang986a4f42012-12-07 07:04:56 +00003304 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05303305}
3306
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003307static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05303308{
3309 struct virtnet_info *vi = vdev->priv;
3310
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003311 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003312
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10303313 /* Make sure no work handler is accessing the device. */
3314 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00003315
Amit Shah04486ed2011-12-22 16:58:32 +05303316 unregister_netdev(vi->dev);
3317
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003318 net_failover_destroy(vi->failover);
3319
Amit Shah04486ed2011-12-22 16:58:32 +05303320 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003321
Rusty Russell74b25532007-11-19 11:20:42 -05003322 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003323}
3324
Arnd Bergmann67a75192017-07-25 17:35:50 +02003325static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303326{
3327 struct virtnet_info *vi = vdev->priv;
3328
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003329 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003330 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303331 remove_vq_common(vi);
3332
3333 return 0;
3334}
3335
Arnd Bergmann67a75192017-07-25 17:35:50 +02003336static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303337{
3338 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003339 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05303340
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003341 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303342 if (err)
3343 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00003344 virtnet_set_queues(vi, vi->curr_queue_pairs);
3345
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003346 err = virtnet_cpu_notif_add(vi);
Xie Yongji3f2869c2021-05-17 16:45:16 +08003347 if (err) {
3348 virtnet_freeze_down(vdev);
3349 remove_vq_common(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08003350 return err;
Xie Yongji3f2869c2021-05-17 16:45:16 +08003351 }
Jason Wangec9debb2013-10-29 15:11:07 +08003352
Amit Shah0741bcb2011-12-22 16:58:33 +05303353 return 0;
3354}
Amit Shah0741bcb2011-12-22 16:58:33 +05303355
Rusty Russell296f96f2007-10-22 11:03:37 +10003356static struct virtio_device_id id_table[] = {
3357 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3358 { 0 },
3359};
3360
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003361#define VIRTNET_FEATURES \
3362 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3363 VIRTIO_NET_F_MAC, \
3364 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3365 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3366 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3367 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3368 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3369 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3370 VIRTIO_NET_F_CTRL_MAC_ADDR, \
Jason Baronfaa9b392018-01-05 17:44:54 -05003371 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
Sridhar Samudrala98050692018-05-24 09:55:16 -07003372 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003373
Rusty Russellc45a6812008-05-02 21:50:50 -05003374static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003375 VIRTNET_FEATURES,
3376};
3377
3378static unsigned int features_legacy[] = {
3379 VIRTNET_FEATURES,
3380 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09303381 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05003382};
3383
Uwe Kleine-König22402522009-11-05 01:32:44 -08003384static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05003385 .feature_table = features,
3386 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003387 .feature_table_legacy = features_legacy,
3388 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10003389 .driver.name = KBUILD_MODNAME,
3390 .driver.owner = THIS_MODULE,
3391 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003392 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10003393 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003394 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003395 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09303396#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05303397 .freeze = virtnet_freeze,
3398 .restore = virtnet_restore,
3399#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10003400};
3401
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003402static __init int virtio_net_driver_init(void)
3403{
3404 int ret;
3405
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003406 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003407 virtnet_cpu_online,
3408 virtnet_cpu_down_prep);
3409 if (ret < 0)
3410 goto out;
3411 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003412 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003413 NULL, virtnet_cpu_dead);
3414 if (ret)
3415 goto err_dead;
3416
3417 ret = register_virtio_driver(&virtio_net_driver);
3418 if (ret)
3419 goto err_virtio;
3420 return 0;
3421err_virtio:
3422 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3423err_dead:
3424 cpuhp_remove_multi_state(virtionet_online);
3425out:
3426 return ret;
3427}
3428module_init(virtio_net_driver_init);
3429
3430static __exit void virtio_net_driver_exit(void)
3431{
Andrew Jonescfa0ebc2017-07-24 15:38:32 +02003432 unregister_virtio_driver(&virtio_net_driver);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003433 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3434 cpuhp_remove_multi_state(virtionet_online);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003435}
3436module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10003437
3438MODULE_DEVICE_TABLE(virtio, id_table);
3439MODULE_DESCRIPTION("Virtio network driver");
3440MODULE_LICENSE("GPL");