blob: d03bfc4fce8e6f63c365ce4928daca65bc56543f [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Rusty Russell296f96f2007-10-22 11:03:37 +100017 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080021#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100022#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
John Fastabendf600b692016-12-15 12:13:24 -080025#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010026#include <linux/bpf_trace.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100027#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080028#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000030#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080031#include <linux/average.h>
Jason Wang186b3c92017-09-19 17:42:43 +080032#include <linux/filter.h>
Sridhar Samudralaba5e4422018-05-24 09:55:17 -070033#include <linux/netdevice.h>
34#include <linux/pci.h>
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +020035#include <net/route.h>
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +010036#include <net/xdp.h>
Sridhar Samudralaba5e4422018-05-24 09:55:17 -070037#include <net/net_failover.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100038
Amerigo Wangd34710e2013-05-09 19:50:51 +000039static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020040module_param(napi_weight, int, 0444);
41
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040042static bool csum = true, gso = true, napi_tx;
Rusty Russell34a48572008-02-04 23:50:02 -050043module_param(csum, bool, 0444);
44module_param(gso, bool, 0444);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040045module_param(napi_tx, bool, 0644);
Rusty Russell34a48572008-02-04 23:50:02 -050046
Rusty Russell296f96f2007-10-22 11:03:37 +100047/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080048#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080049#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100050
Jason Wangf6b10202017-02-21 16:46:28 +080051#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
52
John Fastabend2de2f7f2017-02-02 19:16:29 -080053/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
54#define VIRTIO_XDP_HEADROOM 256
55
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +020056/* Separating two types of XDP xmit */
57#define VIRTIO_XDP_TX BIT(0)
58#define VIRTIO_XDP_REDIR BIT(1)
59
Johannes Berg5377d7582015-08-19 09:48:40 +020060/* RX packet size EWMA. The average packet size is used to determine the packet
61 * buffer size when refilling RX rings. As the entire RX ring may be refilled
62 * at once, the weight is chosen so that the EWMA will be insensitive to short-
63 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080064 */
Johannes Bergeb1e0112017-02-15 09:49:26 +010065DECLARE_EWMA(pkt_len, 0, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080066
Rick Jones66846042011-11-14 14:17:08 +000067#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000068
Colin Ian King7acd4322017-08-12 22:45:53 +010069static const unsigned long guest_offloads[] = {
70 VIRTIO_NET_F_GUEST_TSO4,
71 VIRTIO_NET_F_GUEST_TSO6,
72 VIRTIO_NET_F_GUEST_ECN,
73 VIRTIO_NET_F_GUEST_UFO
74};
Jason Wang3f935222017-07-19 16:54:49 +080075
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090076struct virtnet_stat_desc {
77 char desc[ETH_GSTRING_LEN];
78 size_t offset;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000079};
80
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090081struct virtnet_sq_stats {
82 struct u64_stats_sync syncp;
83 u64 packets;
84 u64 bytes;
85};
86
Toshiaki Makitaa0929a42018-07-23 23:36:05 +090087struct virtnet_rq_stat_items {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090088 u64 packets;
89 u64 bytes;
90};
91
Toshiaki Makitaa0929a42018-07-23 23:36:05 +090092struct virtnet_rq_stats {
93 struct u64_stats_sync syncp;
94 struct virtnet_rq_stat_items items;
95};
96
97struct virtnet_rx_stats {
98 struct virtnet_rq_stat_items rx;
99};
100
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900101#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900102#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stat_items, m)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900103
104static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
105 { "packets", VIRTNET_SQ_STAT(packets) },
106 { "bytes", VIRTNET_SQ_STAT(bytes) },
107};
108
109static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
110 { "packets", VIRTNET_RQ_STAT(packets) },
111 { "bytes", VIRTNET_RQ_STAT(bytes) },
112};
113
114#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
115#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
116
Jason Wange9d74172012-12-07 07:04:55 +0000117/* Internal representation of a send virtqueue */
118struct send_queue {
119 /* Virtqueue associated with this send _queue */
120 struct virtqueue *vq;
121
122 /* TX: fragments + linear part + virtio header */
123 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000124
125 /* Name of the send queue: output.$index */
126 char name[40];
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400127
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900128 struct virtnet_sq_stats stats;
129
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400130 struct napi_struct napi;
Jason Wange9d74172012-12-07 07:04:55 +0000131};
132
133/* Internal representation of a receive virtqueue */
134struct receive_queue {
135 /* Virtqueue associated with this receive_queue */
136 struct virtqueue *vq;
137
Rusty Russell296f96f2007-10-22 11:03:37 +1000138 struct napi_struct napi;
139
John Fastabendf600b692016-12-15 12:13:24 -0800140 struct bpf_prog __rcu *xdp_prog;
141
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900142 struct virtnet_rq_stats stats;
143
Jason Wange9d74172012-12-07 07:04:55 +0000144 /* Chain pages by the private ptr. */
145 struct page *pages;
146
Michael Daltonab7db912014-01-16 22:23:27 -0800147 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200148 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800149
Michael Daltonfb518792014-01-16 22:23:26 -0800150 /* Page frag for packet buffer allocation. */
151 struct page_frag alloc_frag;
152
Jason Wange9d74172012-12-07 07:04:55 +0000153 /* RX: fragments + linear part + virtio header */
154 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000155
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +0200156 /* Min single buffer size for mergeable buffers case. */
157 unsigned int min_buf_len;
158
Jason Wang986a4f42012-12-07 07:04:56 +0000159 /* Name of this receive queue: input.$index */
160 char name[40];
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100161
162 struct xdp_rxq_info xdp_rxq;
Jason Wange9d74172012-12-07 07:04:55 +0000163};
164
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300165/* Control VQ buffers: protected by the rtnl lock */
166struct control_buf {
167 struct virtio_net_ctrl_hdr hdr;
168 virtio_net_ctrl_ack status;
169 struct virtio_net_ctrl_mq mq;
170 u8 promisc;
171 u8 allmulti;
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +0300172 __virtio16 vid;
Michael S. Tsirkinf4ee7032018-04-19 08:30:50 +0300173 __virtio64 offloads;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300174};
175
Jason Wange9d74172012-12-07 07:04:55 +0000176struct virtnet_info {
177 struct virtio_device *vdev;
178 struct virtqueue *cvq;
179 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000180 struct send_queue *sq;
181 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000182 unsigned int status;
183
Jason Wang986a4f42012-12-07 07:04:56 +0000184 /* Max # of queue pairs supported by the device */
185 u16 max_queue_pairs;
186
187 /* # of queue pairs currently used by the driver */
188 u16 curr_queue_pairs;
189
John Fastabend672aafd2016-12-15 12:13:49 -0800190 /* # of XDP queue pairs currently used by the driver */
191 u16 xdp_queue_pairs;
192
Herbert Xu97402b92008-04-18 11:24:27 +0800193 /* I like... big packets and I cannot lie! */
194 bool big_packets;
195
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800196 /* Host will merge rx buffers for big packets (shake it! shake it!) */
197 bool mergeable_rx_bufs;
198
Jason Wang986a4f42012-12-07 07:04:56 +0000199 /* Has control virtqueue */
200 bool has_cvq;
201
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930202 /* Host can handle any s/g split between our header and packet data */
203 bool any_header_sg;
204
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300205 /* Packet virtio header size */
206 u8 hdr_len;
207
Rusty Russell3161e452009-08-26 12:22:32 -0700208 /* Work struct for refilling if we run low on memory. */
209 struct delayed_work refill;
210
Jason Wang586d17c2012-04-11 20:43:52 +0000211 /* Work struct for config space updates */
212 struct work_struct config_work;
213
Jason Wang986a4f42012-12-07 07:04:56 +0000214 /* Does the affinity hint is set for virtqueues? */
215 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000216
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200217 /* CPU hotplug instances for online & dead */
218 struct hlist_node node;
219 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200220
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300221 struct control_buf *ctrl;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100222
223 /* Ethtool settings */
224 u8 duplex;
225 u32 speed;
Jason Wang3f935222017-07-19 16:54:49 +0800226
227 unsigned long guest_offloads;
Sridhar Samudralaba5e4422018-05-24 09:55:17 -0700228
229 /* failover when STANDBY feature enabled */
230 struct failover *failover;
Rusty Russell296f96f2007-10-22 11:03:37 +1000231};
232
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000233struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300234 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000235 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300236 * hdr is in a separate sg buffer, and data sg buffer shares same page
237 * with this header sg. This padding makes next sg 16 byte aligned
238 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000239 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300240 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000241};
242
Jason Wang986a4f42012-12-07 07:04:56 +0000243/* Converting between virtqueue no. and kernel tx/rx queue no.
244 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
245 */
246static int vq2txq(struct virtqueue *vq)
247{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000248 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000249}
250
251static int txq2vq(int txq)
252{
253 return txq * 2 + 1;
254}
255
256static int vq2rxq(struct virtqueue *vq)
257{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000258 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000259}
260
261static int rxq2vq(int rxq)
262{
263 return rxq * 2;
264}
265
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300266static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000267{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300268 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000269}
270
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000271/*
272 * private is used to chain pages for big packets, put the whole
273 * most recent used list in the beginning for reuse
274 */
Jason Wange9d74172012-12-07 07:04:55 +0000275static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500276{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000277 struct page *end;
278
Jason Wange9d74172012-12-07 07:04:55 +0000279 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000280 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000281 end->private = (unsigned long)rq->pages;
282 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500283}
284
Jason Wange9d74172012-12-07 07:04:55 +0000285static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500286{
Jason Wange9d74172012-12-07 07:04:55 +0000287 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500288
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000289 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000290 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000291 /* clear private here, it is used to chain pages */
292 p->private = 0;
293 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500294 p = alloc_page(gfp_mask);
295 return p;
296}
297
Willem de Bruijne4e84522017-04-24 13:49:26 -0400298static void virtqueue_napi_schedule(struct napi_struct *napi,
299 struct virtqueue *vq)
300{
301 if (napi_schedule_prep(napi)) {
302 virtqueue_disable_cb(vq);
303 __napi_schedule(napi);
304 }
305}
306
307static void virtqueue_napi_complete(struct napi_struct *napi,
308 struct virtqueue *vq, int processed)
309{
310 int opaque;
311
312 opaque = virtqueue_enable_cb_prepare(vq);
Toshiaki Makitafdaa7672017-12-07 13:15:15 +0900313 if (napi_complete_done(napi, processed)) {
314 if (unlikely(virtqueue_poll(vq, opaque)))
315 virtqueue_napi_schedule(napi, vq);
316 } else {
317 virtqueue_disable_cb(vq);
318 }
Willem de Bruijne4e84522017-04-24 13:49:26 -0400319}
320
Jason Wange9d74172012-12-07 07:04:55 +0000321static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000322{
Jason Wange9d74172012-12-07 07:04:55 +0000323 struct virtnet_info *vi = vq->vdev->priv;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400324 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000325
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500326 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000327 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000328
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400329 if (napi->weight)
330 virtqueue_napi_schedule(napi, vq);
331 else
332 /* We were probably waiting for more output buffers. */
333 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000334}
335
Jason Wang28b39bc2017-07-19 16:54:46 +0800336#define MRG_CTX_HEADER_SHIFT 22
337static void *mergeable_len_to_ctx(unsigned int truesize,
338 unsigned int headroom)
339{
340 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
341}
342
343static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
344{
345 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
346}
347
348static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
349{
350 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
351}
352
Mike Waychison34646452012-01-04 12:52:32 +0000353/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300354static struct sk_buff *page_to_skb(struct virtnet_info *vi,
355 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700356 struct page *page, unsigned int offset,
357 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000358{
359 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300360 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700361 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000362 char *p;
363
Michael Dalton2613af02013-10-28 15:44:18 -0700364 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000365
366 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100367 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000368 if (unlikely(!skb))
369 return NULL;
370
371 hdr = skb_vnet_hdr(skb);
372
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300373 hdr_len = vi->hdr_len;
374 if (vi->mergeable_rx_bufs)
stephen hemmingera4a76502017-08-15 10:29:17 -0700375 hdr_padded_len = sizeof(*hdr);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300376 else
Michael Dalton2613af02013-10-28 15:44:18 -0700377 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000378
379 memcpy(hdr, p, hdr_len);
380
381 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700382 offset += hdr_padded_len;
383 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000384
385 copy = len;
386 if (copy > skb_tailroom(skb))
387 copy = skb_tailroom(skb);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200388 skb_put_data(skb, p, copy);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000389
390 len -= copy;
391 offset += copy;
392
Michael Dalton2613af02013-10-28 15:44:18 -0700393 if (vi->mergeable_rx_bufs) {
394 if (len)
395 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
396 else
397 put_page(page);
398 return skb;
399 }
400
Sasha Levine878d782011-09-28 04:40:54 +0000401 /*
402 * Verify that we can indeed put this data into a skb.
403 * This is here to handle cases when the device erroneously
404 * tries to receive more than is possible. This is usually
405 * the case of a broken device.
406 */
407 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000408 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000409 dev_kfree_skb(skb);
410 return NULL;
411 }
Michael Dalton2613af02013-10-28 15:44:18 -0700412 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000413 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700414 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
415 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
416 frag_size, truesize);
417 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000418 page = (struct page *)page->private;
419 offset = 0;
420 }
421
422 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000423 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000424
425 return skb;
426}
427
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200428static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
429 struct send_queue *sq,
430 struct xdp_frame *xdpf)
John Fastabend56434a02016-12-15 12:14:13 -0800431{
John Fastabend56434a02016-12-15 12:14:13 -0800432 struct virtio_net_hdr_mrg_rxbuf *hdr;
John Fastabend56434a02016-12-15 12:14:13 -0800433 int err;
434
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200435 /* virtqueue want to use data area in-front of packet */
436 if (unlikely(xdpf->metasize > 0))
437 return -EOPNOTSUPP;
438
439 if (unlikely(xdpf->headroom < vi->hdr_len))
440 return -EOVERFLOW;
441
442 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
443 xdpf->data -= vi->hdr_len;
Jason Wangf6b10202017-02-21 16:46:28 +0800444 /* Zero header and leave csum up to XDP layers */
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200445 hdr = xdpf->data;
Jason Wangf6b10202017-02-21 16:46:28 +0800446 memset(hdr, 0, vi->hdr_len);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200447 xdpf->len += vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800448
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200449 sg_init_one(sq->sg, xdpf->data, xdpf->len);
Jason Wangbb91acc2016-12-23 22:37:32 +0800450
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200451 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdpf, GFP_ATOMIC);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100452 if (unlikely(err))
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200453 return -ENOSPC; /* Caller handle free/refcnt */
John Fastabend56434a02016-12-15 12:14:13 -0800454
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200455 return 0;
John Fastabend56434a02016-12-15 12:14:13 -0800456}
457
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200458static int __virtnet_xdp_tx_xmit(struct virtnet_info *vi,
459 struct xdp_frame *xdpf)
460{
461 struct xdp_frame *xdpf_sent;
462 struct send_queue *sq;
463 unsigned int len;
464 unsigned int qp;
465
466 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
467 sq = &vi->sq[qp];
468
469 /* Free up any pending old buffers before queueing new ones. */
470 while ((xdpf_sent = virtqueue_get_buf(sq->vq, &len)) != NULL)
471 xdp_return_frame(xdpf_sent);
472
473 return __virtnet_xdp_xmit_one(vi, sq, xdpf);
474}
475
476static int virtnet_xdp_xmit(struct net_device *dev,
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +0200477 int n, struct xdp_frame **frames, u32 flags)
Jason Wang186b3c92017-09-19 17:42:43 +0800478{
479 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100480 struct receive_queue *rq = vi->rq;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200481 struct xdp_frame *xdpf_sent;
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100482 struct bpf_prog *xdp_prog;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200483 struct send_queue *sq;
484 unsigned int len;
485 unsigned int qp;
486 int drops = 0;
487 int err;
488 int i;
489
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200490 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +0200491 return -EINVAL;
492
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200493 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
494 sq = &vi->sq[qp];
Jason Wang186b3c92017-09-19 17:42:43 +0800495
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100496 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
497 * indicate XDP resources have been successfully allocated.
498 */
499 xdp_prog = rcu_dereference(rq->xdp_prog);
500 if (!xdp_prog)
501 return -ENXIO;
502
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200503 /* Free up any pending old buffers before queueing new ones. */
504 while ((xdpf_sent = virtqueue_get_buf(sq->vq, &len)) != NULL)
505 xdp_return_frame(xdpf_sent);
506
507 for (i = 0; i < n; i++) {
508 struct xdp_frame *xdpf = frames[i];
509
510 err = __virtnet_xdp_xmit_one(vi, sq, xdpf);
511 if (err) {
512 xdp_return_frame_rx_napi(xdpf);
513 drops++;
514 }
515 }
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200516
517 if (flags & XDP_XMIT_FLUSH)
518 virtqueue_kick(sq->vq);
519
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200520 return n - drops;
Jason Wang186b3c92017-09-19 17:42:43 +0800521}
522
Jason Wangf6b10202017-02-21 16:46:28 +0800523static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
524{
525 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
526}
527
Jason Wang4941d472017-07-19 16:54:48 +0800528/* We copy the packet for XDP in the following cases:
529 *
530 * 1) Packet is scattered across multiple rx buffers.
531 * 2) Headroom space is insufficient.
532 *
533 * This is inefficient but it's a temporary condition that
534 * we hit right after XDP is enabled and until queue is refilled
535 * with large buffers with sufficient headroom - so it should affect
536 * at most queue size packets.
537 * Afterwards, the conditions to enable
538 * XDP should preclude the underlying device from sending packets
539 * across multiple buffers (num_buf > 1), and we make sure buffers
540 * have enough headroom.
John Fastabend72979a62016-12-15 12:14:36 -0800541 */
542static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800543 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800544 struct page *p,
545 int offset,
Jason Wang4941d472017-07-19 16:54:48 +0800546 int page_off,
John Fastabend72979a62016-12-15 12:14:36 -0800547 unsigned int *len)
548{
549 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend72979a62016-12-15 12:14:36 -0800550
551 if (!page)
552 return NULL;
553
554 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
555 page_off += *len;
556
Jason Wang56a86f82016-12-23 22:37:26 +0800557 while (--*num_buf) {
Jason Wang3cc81a92018-03-02 17:29:14 +0800558 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
John Fastabend72979a62016-12-15 12:14:36 -0800559 unsigned int buflen;
John Fastabend72979a62016-12-15 12:14:36 -0800560 void *buf;
561 int off;
562
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200563 buf = virtqueue_get_buf(rq->vq, &buflen);
564 if (unlikely(!buf))
John Fastabend72979a62016-12-15 12:14:36 -0800565 goto err_buf;
566
John Fastabend72979a62016-12-15 12:14:36 -0800567 p = virt_to_head_page(buf);
568 off = buf - page_address(p);
569
Jason Wang56a86f82016-12-23 22:37:26 +0800570 /* guard against a misconfigured or uncooperative backend that
571 * is sending packet larger than the MTU.
572 */
Jason Wang3cc81a92018-03-02 17:29:14 +0800573 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
Jason Wang56a86f82016-12-23 22:37:26 +0800574 put_page(p);
575 goto err_buf;
576 }
577
John Fastabend72979a62016-12-15 12:14:36 -0800578 memcpy(page_address(page) + page_off,
579 page_address(p) + off, buflen);
580 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800581 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800582 }
583
John Fastabend2de2f7f2017-02-02 19:16:29 -0800584 /* Headroom does not contribute to packet length */
585 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800586 return page;
587err_buf:
588 __free_pages(page, 0);
589 return NULL;
590}
591
Jason Wang4941d472017-07-19 16:54:48 +0800592static struct sk_buff *receive_small(struct net_device *dev,
593 struct virtnet_info *vi,
594 struct receive_queue *rq,
595 void *buf, void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800596 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900597 unsigned int *xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900598 struct virtnet_rx_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800599{
600 struct sk_buff *skb;
601 struct bpf_prog *xdp_prog;
602 unsigned int xdp_headroom = (unsigned long)ctx;
603 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
604 unsigned int headroom = vi->hdr_len + header_offset;
605 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
606 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
607 struct page *page = virt_to_head_page(buf);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100608 unsigned int delta = 0;
Jason Wang4941d472017-07-19 16:54:48 +0800609 struct page *xdp_page;
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100610 int err;
611
Jason Wang4941d472017-07-19 16:54:48 +0800612 len -= vi->hdr_len;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900613 stats->rx.bytes += len;
Jason Wang4941d472017-07-19 16:54:48 +0800614
615 rcu_read_lock();
616 xdp_prog = rcu_dereference(rq->xdp_prog);
617 if (xdp_prog) {
618 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200619 struct xdp_frame *xdpf;
Jason Wang4941d472017-07-19 16:54:48 +0800620 struct xdp_buff xdp;
621 void *orig_data;
622 u32 act;
623
Jesper Dangaard Brouer95dbe9e2018-02-20 14:32:10 +0100624 if (unlikely(hdr->hdr.gso_type))
Jason Wang4941d472017-07-19 16:54:48 +0800625 goto err_xdp;
626
627 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
628 int offset = buf - page_address(page) + header_offset;
629 unsigned int tlen = len + vi->hdr_len;
630 u16 num_buf = 1;
631
632 xdp_headroom = virtnet_get_headroom(vi);
633 header_offset = VIRTNET_RX_PAD + xdp_headroom;
634 headroom = vi->hdr_len + header_offset;
635 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
636 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
637 xdp_page = xdp_linearize_page(rq, &num_buf, page,
638 offset, header_offset,
639 &tlen);
640 if (!xdp_page)
641 goto err_xdp;
642
643 buf = page_address(xdp_page);
644 put_page(page);
645 page = xdp_page;
646 }
647
648 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
649 xdp.data = xdp.data_hard_start + xdp_headroom;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200650 xdp_set_data_meta_invalid(&xdp);
Jason Wang4941d472017-07-19 16:54:48 +0800651 xdp.data_end = xdp.data + len;
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100652 xdp.rxq = &rq->xdp_rxq;
Jason Wang4941d472017-07-19 16:54:48 +0800653 orig_data = xdp.data;
654 act = bpf_prog_run_xdp(xdp_prog, &xdp);
655
656 switch (act) {
657 case XDP_PASS:
658 /* Recalculate length in case bpf program changed it */
659 delta = orig_data - xdp.data;
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700660 len = xdp.data_end - xdp.data;
Jason Wang4941d472017-07-19 16:54:48 +0800661 break;
662 case XDP_TX:
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200663 xdpf = convert_to_xdp_frame(&xdp);
664 if (unlikely(!xdpf))
665 goto err_xdp;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200666 err = __virtnet_xdp_tx_xmit(vi, xdpf);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200667 if (unlikely(err)) {
Jason Wang4941d472017-07-19 16:54:48 +0800668 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100669 goto err_xdp;
670 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200671 *xdp_xmit |= VIRTIO_XDP_TX;
Jason Wang186b3c92017-09-19 17:42:43 +0800672 rcu_read_unlock();
673 goto xdp_xmit;
674 case XDP_REDIRECT:
675 err = xdp_do_redirect(dev, &xdp, xdp_prog);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100676 if (err)
677 goto err_xdp;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200678 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang4941d472017-07-19 16:54:48 +0800679 rcu_read_unlock();
680 goto xdp_xmit;
681 default:
682 bpf_warn_invalid_xdp_action(act);
683 case XDP_ABORTED:
684 trace_xdp_exception(vi->dev, xdp_prog, act);
685 case XDP_DROP:
686 goto err_xdp;
687 }
688 }
689 rcu_read_unlock();
690
691 skb = build_skb(buf, buflen);
692 if (!skb) {
693 put_page(page);
694 goto err;
695 }
696 skb_reserve(skb, headroom - delta);
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700697 skb_put(skb, len);
Jason Wang4941d472017-07-19 16:54:48 +0800698 if (!delta) {
699 buf += header_offset;
700 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
701 } /* keep zeroed vnet hdr since packet was changed by bpf */
702
703err:
704 return skb;
705
706err_xdp:
707 rcu_read_unlock();
708 dev->stats.rx_dropped++;
709 put_page(page);
710xdp_xmit:
711 return NULL;
712}
713
714static struct sk_buff *receive_big(struct net_device *dev,
715 struct virtnet_info *vi,
716 struct receive_queue *rq,
717 void *buf,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900718 unsigned int len,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900719 struct virtnet_rx_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800720{
721 struct page *page = buf;
722 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
723
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900724 stats->rx.bytes += len - vi->hdr_len;
Jason Wang4941d472017-07-19 16:54:48 +0800725 if (unlikely(!skb))
726 goto err;
727
728 return skb;
729
730err:
731 dev->stats.rx_dropped++;
732 give_pages(rq, page);
733 return NULL;
734}
735
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200736static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200737 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200738 struct receive_queue *rq,
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200739 void *buf,
740 void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800741 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900742 unsigned int *xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900743 struct virtnet_rx_stats *stats)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000744{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300745 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
746 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200747 struct page *page = virt_to_head_page(buf);
748 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800749 struct sk_buff *head_skb, *curr_skb;
750 struct bpf_prog *xdp_prog;
751 unsigned int truesize;
Jason Wang4941d472017-07-19 16:54:48 +0800752 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
Jason Wang3cc81a92018-03-02 17:29:14 +0800753 int err;
Michael Daltonab7db912014-01-16 22:23:27 -0800754
John Fastabend56434a02016-12-15 12:14:13 -0800755 head_skb = NULL;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900756 stats->rx.bytes += len - vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800757
John Fastabendf600b692016-12-15 12:13:24 -0800758 rcu_read_lock();
759 xdp_prog = rcu_dereference(rq->xdp_prog);
760 if (xdp_prog) {
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200761 struct xdp_frame *xdpf;
John Fastabend72979a62016-12-15 12:14:36 -0800762 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800763 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800764 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800765 u32 act;
766
Jason Wang3d62b2a2018-05-22 11:44:31 +0800767 /* Transient failure which in theory could occur if
768 * in-flight packets from before XDP was enabled reach
769 * the receive path after XDP is loaded.
770 */
771 if (unlikely(hdr->hdr.gso_type))
772 goto err_xdp;
773
Jason Wang3cc81a92018-03-02 17:29:14 +0800774 /* This happens when rx buffer size is underestimated
775 * or headroom is not enough because of the buffer
776 * was refilled before XDP is set. This should only
777 * happen for the first several packets, so we don't
778 * care much about its performance.
779 */
Jason Wang4941d472017-07-19 16:54:48 +0800780 if (unlikely(num_buf > 1 ||
781 headroom < virtnet_get_headroom(vi))) {
John Fastabend72979a62016-12-15 12:14:36 -0800782 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800783 xdp_page = xdp_linearize_page(rq, &num_buf,
Jason Wang4941d472017-07-19 16:54:48 +0800784 page, offset,
785 VIRTIO_XDP_HEADROOM,
786 &len);
John Fastabend72979a62016-12-15 12:14:36 -0800787 if (!xdp_page)
788 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800789 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800790 } else {
791 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800792 }
793
John Fastabend2de2f7f2017-02-02 19:16:29 -0800794 /* Allow consuming headroom but reserve enough space to push
795 * the descriptor on if we get an XDP_TX return code.
796 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800797 data = page_address(xdp_page) + offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800798 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
John Fastabend0354e4d2017-02-02 19:15:01 -0800799 xdp.data = data + vi->hdr_len;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200800 xdp_set_data_meta_invalid(&xdp);
John Fastabend0354e4d2017-02-02 19:15:01 -0800801 xdp.data_end = xdp.data + (len - vi->hdr_len);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100802 xdp.rxq = &rq->xdp_rxq;
803
John Fastabend0354e4d2017-02-02 19:15:01 -0800804 act = bpf_prog_run_xdp(xdp_prog, &xdp);
805
John Fastabend56434a02016-12-15 12:14:13 -0800806 switch (act) {
807 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800808 /* recalculate offset to account for any header
809 * adjustments. Note other cases do not build an
810 * skb and avoid using offset
811 */
812 offset = xdp.data -
813 page_address(xdp_page) - vi->hdr_len;
814
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700815 /* recalculate len if xdp.data or xdp.data_end were
816 * adjusted
817 */
Nikita V. Shirokovaaa64522018-04-22 21:16:48 -0700818 len = xdp.data_end - xdp.data + vi->hdr_len;
Jason Wang1830f892016-12-23 22:37:27 +0800819 /* We can only create skb based on xdp_page. */
820 if (unlikely(xdp_page != page)) {
821 rcu_read_unlock();
822 put_page(page);
823 head_skb = page_to_skb(vi, rq, xdp_page,
John Fastabend2de2f7f2017-02-02 19:16:29 -0800824 offset, len, PAGE_SIZE);
Jason Wang1830f892016-12-23 22:37:27 +0800825 return head_skb;
826 }
John Fastabend56434a02016-12-15 12:14:13 -0800827 break;
828 case XDP_TX:
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200829 xdpf = convert_to_xdp_frame(&xdp);
830 if (unlikely(!xdpf))
831 goto err_xdp;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200832 err = __virtnet_xdp_tx_xmit(vi, xdpf);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200833 if (unlikely(err)) {
John Fastabend0354e4d2017-02-02 19:15:01 -0800834 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100835 if (unlikely(xdp_page != page))
836 put_page(xdp_page);
837 goto err_xdp;
838 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200839 *xdp_xmit |= VIRTIO_XDP_TX;
John Fastabend72979a62016-12-15 12:14:36 -0800840 if (unlikely(xdp_page != page))
Jason Wang5d458a12018-05-22 11:44:29 +0800841 put_page(page);
John Fastabend56434a02016-12-15 12:14:13 -0800842 rcu_read_unlock();
843 goto xdp_xmit;
Jason Wang3cc81a92018-03-02 17:29:14 +0800844 case XDP_REDIRECT:
845 err = xdp_do_redirect(dev, &xdp, xdp_prog);
846 if (err) {
847 if (unlikely(xdp_page != page))
848 put_page(xdp_page);
849 goto err_xdp;
850 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200851 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang3cc81a92018-03-02 17:29:14 +0800852 if (unlikely(xdp_page != page))
Jason Wang68904182018-05-22 11:44:28 +0800853 put_page(page);
Jason Wang3cc81a92018-03-02 17:29:14 +0800854 rcu_read_unlock();
855 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -0800856 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800857 bpf_warn_invalid_xdp_action(act);
858 case XDP_ABORTED:
859 trace_xdp_exception(vi->dev, xdp_prog, act);
860 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -0800861 if (unlikely(xdp_page != page))
862 __free_pages(xdp_page, 0);
John Fastabendf600b692016-12-15 12:13:24 -0800863 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800864 }
John Fastabendf600b692016-12-15 12:13:24 -0800865 }
866 rcu_read_unlock();
867
Jason Wang28b39bc2017-07-19 16:54:46 +0800868 truesize = mergeable_ctx_to_truesize(ctx);
869 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +0300870 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200871 dev->name, len, (unsigned long)ctx);
872 dev->stats.rx_length_errors++;
873 goto err_skb;
874 }
Jason Wang28b39bc2017-07-19 16:54:46 +0800875
John Fastabendf600b692016-12-15 12:13:24 -0800876 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
877 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000878
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200879 if (unlikely(!curr_skb))
880 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000881 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200882 int num_skb_frags;
883
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200884 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
Yunjian Wang03e9f8a2017-12-04 14:02:19 +0800885 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200886 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200887 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300888 virtio16_to_cpu(vi->vdev,
889 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200890 dev->stats.rx_length_errors++;
891 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000892 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200893
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900894 stats->rx.bytes += len;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200895 page = virt_to_head_page(buf);
Jason Wang28b39bc2017-07-19 16:54:46 +0800896
897 truesize = mergeable_ctx_to_truesize(ctx);
898 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +0300899 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200900 dev->name, len, (unsigned long)ctx);
901 dev->stats.rx_length_errors++;
902 goto err_skb;
903 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200904
905 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700906 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
907 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200908
909 if (unlikely(!nskb))
910 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700911 if (curr_skb == head_skb)
912 skb_shinfo(curr_skb)->frag_list = nskb;
913 else
914 curr_skb->next = nskb;
915 curr_skb = nskb;
916 head_skb->truesize += nskb->truesize;
917 num_skb_frags = 0;
918 }
919 if (curr_skb != head_skb) {
920 head_skb->data_len += len;
921 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800922 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700923 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200924 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800925 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
926 put_page(page);
927 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800928 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800929 } else {
930 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800931 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800932 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200933 }
934
Johannes Berg5377d7582015-08-19 09:48:40 +0200935 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200936 return head_skb;
937
John Fastabendf600b692016-12-15 12:13:24 -0800938err_xdp:
939 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200940err_skb:
941 put_page(page);
Jason Wang850e0882018-05-22 11:44:30 +0800942 while (num_buf-- > 1) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200943 buf = virtqueue_get_buf(rq->vq, &len);
944 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200945 pr_debug("%s: rx error: %d buffers missing\n",
946 dev->name, num_buf);
947 dev->stats.rx_length_errors++;
948 break;
949 }
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900950 stats->rx.bytes += len;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200951 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200952 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000953 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200954err_buf:
955 dev->stats.rx_dropped++;
956 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800957xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200958 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000959}
960
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900961static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
962 void *buf, unsigned int len, void **ctx,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900963 unsigned int *xdp_xmit,
964 struct virtnet_rx_stats *stats)
Rusty Russell296f96f2007-10-22 11:03:37 +1000965{
Jason Wange9d74172012-12-07 07:04:55 +0000966 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000967 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300968 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000969
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300970 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000971 pr_debug("%s: short packet %i\n", dev->name, len);
972 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800973 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200974 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -0800975 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800976 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800977 } else {
Jason Wangf6b10202017-02-21 16:46:28 +0800978 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -0800979 }
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900980 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000981 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000982
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200983 if (vi->mergeable_rx_bufs)
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900984 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900985 stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200986 else if (vi->big_packets)
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900987 skb = receive_big(dev, vi, rq, buf, len, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200988 else
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900989 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200990
991 if (unlikely(!skb))
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900992 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800993
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000994 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000995
Mike Rapoporte858fae2016-06-08 16:09:21 +0300996 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +0000997 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000998
Mike Rapoporte858fae2016-06-08 16:09:21 +0300999 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1000 virtio_is_little_endian(vi->vdev))) {
1001 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1002 dev->name, hdr->hdr.gso_type,
1003 hdr->hdr.gso_size);
1004 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001005 }
1006
Mike Rapoportd1dc06d2016-06-14 08:29:38 +03001007 skb->protocol = eth_type_trans(skb, dev);
1008 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1009 ntohs(skb->protocol), skb->len, skb->pkt_type);
1010
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001011 napi_gro_receive(&rq->napi, skb);
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001012 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001013
1014frame_err:
1015 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +10001016 dev_kfree_skb(skb);
1017}
1018
Jason Wang192f68c2017-07-19 16:54:47 +08001019/* Unlike mergeable buffers, all buffers are allocated to the
1020 * same size, except for the headroom. For this reason we do
1021 * not need to use mergeable_len_to_ctx here - it is enough
1022 * to store the headroom as the context ignoring the truesize.
1023 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001024static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1025 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +10001026{
Jason Wangf6b10202017-02-21 16:46:28 +08001027 struct page_frag *alloc_frag = &rq->alloc_frag;
1028 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001029 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wang192f68c2017-07-19 16:54:47 +08001030 void *ctx = (void *)(unsigned long)xdp_headroom;
Jason Wangf6b10202017-02-21 16:46:28 +08001031 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001032 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001033
Jason Wangf6b10202017-02-21 16:46:28 +08001034 len = SKB_DATA_ALIGN(len) +
1035 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1036 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001037 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001038
Jason Wangf6b10202017-02-21 16:46:28 +08001039 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1040 get_page(alloc_frag->page);
1041 alloc_frag->offset += len;
1042 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1043 vi->hdr_len + GOOD_PACKET_LEN);
Jason Wang192f68c2017-07-19 16:54:47 +08001044 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001045 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +08001046 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001047 return err;
1048}
1049
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001050static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1051 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001052{
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001053 struct page *first, *list = NULL;
1054 char *p;
1055 int i, err, offset;
1056
Rusty Russella5835442014-09-11 10:17:36 +09301057 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1058
Jason Wange9d74172012-12-07 07:04:55 +00001059 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001060 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +00001061 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001062 if (!first) {
1063 if (list)
Jason Wange9d74172012-12-07 07:04:55 +00001064 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001065 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -07001066 }
Jason Wange9d74172012-12-07 07:04:55 +00001067 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +10001068
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001069 /* chain new page in list head to match sg */
1070 first->private = (unsigned long)list;
1071 list = first;
1072 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001073
Jason Wange9d74172012-12-07 07:04:55 +00001074 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001075 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +00001076 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001077 return -ENOMEM;
1078 }
1079 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +08001080
Jason Wange9d74172012-12-07 07:04:55 +00001081 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001082 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1083 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +08001084
Jason Wange9d74172012-12-07 07:04:55 +00001085 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001086 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +00001087 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +08001088
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001089 /* chain first in list head */
1090 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301091 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1092 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001093 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +00001094 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +08001095
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001096 return err;
1097}
Herbert Xu97402b92008-04-18 11:24:27 +08001098
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02001099static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
Jason Wang3cc81a92018-03-02 17:29:14 +08001100 struct ewma_pkt_len *avg_pkt_len,
1101 unsigned int room)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001102{
Michael Daltonab7db912014-01-16 22:23:27 -08001103 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001104 unsigned int len;
1105
Jason Wang3cc81a92018-03-02 17:29:14 +08001106 if (room)
1107 return PAGE_SIZE - room;
1108
1109 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03001110 rq->min_buf_len, PAGE_SIZE - hdr_len);
Jason Wang3cc81a92018-03-02 17:29:14 +08001111
Michael S. Tsirkine377fcc2017-03-06 22:21:35 +02001112 return ALIGN(len, L1_CACHE_BYTES);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001113}
1114
John Fastabend2de2f7f2017-02-02 19:16:29 -08001115static int add_recvbuf_mergeable(struct virtnet_info *vi,
1116 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -08001117{
Michael Daltonfb518792014-01-16 22:23:26 -08001118 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001119 unsigned int headroom = virtnet_get_headroom(vi);
Jason Wang3cc81a92018-03-02 17:29:14 +08001120 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1121 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
Michael Daltonfb518792014-01-16 22:23:26 -08001122 char *buf;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001123 void *ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001124 int err;
Michael Daltonfb518792014-01-16 22:23:26 -08001125 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +10001126
Jason Wang3cc81a92018-03-02 17:29:14 +08001127 /* Extra tailroom is needed to satisfy XDP's assumption. This
1128 * means rx frags coalescing won't work, but consider we've
1129 * disabled GSO for XDP, it won't be a big issue.
1130 */
1131 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1132 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001133 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -08001134
Michael Daltonfb518792014-01-16 22:23:26 -08001135 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001136 buf += headroom; /* advance address leaving hole at front of pkt */
Michael Daltonfb518792014-01-16 22:23:26 -08001137 get_page(alloc_frag->page);
Jason Wang3cc81a92018-03-02 17:29:14 +08001138 alloc_frag->offset += len + room;
Michael Daltonfb518792014-01-16 22:23:26 -08001139 hole = alloc_frag->size - alloc_frag->offset;
Jason Wang3cc81a92018-03-02 17:29:14 +08001140 if (hole < len + room) {
Michael Daltonab7db912014-01-16 22:23:27 -08001141 /* To avoid internal fragmentation, if there is very likely not
1142 * enough space for another buffer, add the remaining space to
Michael S. Tsirkin1daa8792017-07-31 21:49:49 +03001143 * the current buffer.
Michael Daltonab7db912014-01-16 22:23:27 -08001144 */
Michael Daltonfb518792014-01-16 22:23:26 -08001145 len += hole;
1146 alloc_frag->offset += hole;
1147 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001148
Michael Daltonfb518792014-01-16 22:23:26 -08001149 sg_init_one(rq->sg, buf, len);
David S. Miller29fda252017-08-01 10:07:50 -07001150 ctx = mergeable_len_to_ctx(len, headroom);
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001151 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001152 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -07001153 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001154
1155 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001156}
1157
Rusty Russellb2baed62011-12-29 00:42:38 +00001158/*
1159 * Returns false if we couldn't fill entirely (OOM).
1160 *
1161 * Normally run in the receive path, but can also be run from ndo_open
1162 * before we're receiving packets, or from refill_work which is
1163 * careful to disable receiving (using napi_disable).
1164 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001165static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1166 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001167{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001168 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001169 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001170
Amit Shah0aea51c2009-08-26 14:58:28 +05301171 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001172 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001173 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001174 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001175 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001176 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001177 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001178
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001179 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301180 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001181 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001182 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +08001183 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -07001184 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001185}
1186
Rusty Russell18445c42008-02-04 23:49:57 -05001187static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001188{
1189 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001190 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +00001191
Willem de Bruijne4e84522017-04-24 13:49:26 -04001192 virtqueue_napi_schedule(&rq->napi, rvq);
Rusty Russell296f96f2007-10-22 11:03:37 +10001193}
1194
Willem de Bruijne4e84522017-04-24 13:49:26 -04001195static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001196{
Willem de Bruijne4e84522017-04-24 13:49:26 -04001197 napi_enable(napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001198
1199 /* If all buffers were filled by other side before we napi_enabled, we
Willem de Bruijne4e84522017-04-24 13:49:26 -04001200 * won't get another interrupt, so process any outstanding packets now.
1201 * Call local_bh_enable after to trigger softIRQ processing.
1202 */
1203 local_bh_disable();
1204 virtqueue_napi_schedule(napi, vq);
1205 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001206}
1207
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001208static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1209 struct virtqueue *vq,
1210 struct napi_struct *napi)
1211{
1212 if (!napi->weight)
1213 return;
1214
1215 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1216 * enable the feature if this is likely affine with the transmit path.
1217 */
1218 if (!vi->affinity_hint_set) {
1219 napi->weight = 0;
1220 return;
1221 }
1222
1223 return virtnet_napi_enable(vq, napi);
1224}
1225
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001226static void virtnet_napi_tx_disable(struct napi_struct *napi)
1227{
1228 if (napi->weight)
1229 napi_disable(napi);
1230}
1231
Rusty Russell3161e452009-08-26 12:22:32 -07001232static void refill_work(struct work_struct *work)
1233{
Jason Wange9d74172012-12-07 07:04:55 +00001234 struct virtnet_info *vi =
1235 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -07001236 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +00001237 int i;
Rusty Russell3161e452009-08-26 12:22:32 -07001238
Sasha Levin55257d72013-04-29 12:00:08 +09301239 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001240 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -07001241
Jason Wang986a4f42012-12-07 07:04:56 +00001242 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001243 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001244 virtnet_napi_enable(rq->vq, &rq->napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001245
1246 /* In theory, this can happen: if we don't get any buffers in
1247 * we will *never* try to fill again.
1248 */
1249 if (still_empty)
1250 schedule_delayed_work(&vi->refill, HZ/2);
1251 }
Rusty Russell3161e452009-08-26 12:22:32 -07001252}
1253
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001254static int virtnet_receive(struct receive_queue *rq, int budget,
1255 unsigned int *xdp_xmit)
Rusty Russell296f96f2007-10-22 11:03:37 +10001256{
Jason Wange9d74172012-12-07 07:04:55 +00001257 struct virtnet_info *vi = rq->vq->vdev->priv;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001258 struct virtnet_rx_stats stats = {};
1259 unsigned int len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001260 void *buf;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001261 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001262
Jason Wang192f68c2017-07-19 16:54:47 +08001263 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001264 void *ctx;
1265
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001266 while (stats.rx.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001267 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001268 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1269 stats.rx.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001270 }
1271 } else {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001272 while (stats.rx.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001273 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001274 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1275 stats.rx.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001276 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001277 }
1278
Jason Wangbe121f42014-01-16 14:45:24 +08001279 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001280 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001281 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -07001282 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001283
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001284 u64_stats_update_begin(&rq->stats.syncp);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001285 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1286 size_t offset = virtnet_rq_stats_desc[i].offset;
1287 u64 *item;
1288
1289 item = (u64 *)((u8 *)&rq->stats.items + offset);
1290 *item += *(u64 *)((u8 *)&stats.rx + offset);
1291 }
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001292 u64_stats_update_end(&rq->stats.syncp);
Jason Wang61845d22017-02-17 11:33:09 +08001293
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001294 return stats.rx.packets;
Jason Wang2ffa7592014-07-23 16:33:54 +08001295}
1296
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001297static void free_old_xmit_skbs(struct send_queue *sq)
1298{
1299 struct sk_buff *skb;
1300 unsigned int len;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001301 unsigned int packets = 0;
1302 unsigned int bytes = 0;
1303
1304 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1305 pr_debug("Sent skb %p\n", skb);
1306
1307 bytes += skb->len;
1308 packets++;
1309
Eric Dumazetdadc0732017-08-24 09:02:49 -07001310 dev_consume_skb_any(skb);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001311 }
1312
1313 /* Avoid overhead when no packets have been processed
1314 * happens when called speculatively from start_xmit.
1315 */
1316 if (!packets)
1317 return;
1318
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001319 u64_stats_update_begin(&sq->stats.syncp);
1320 sq->stats.bytes += bytes;
1321 sq->stats.packets += packets;
1322 u64_stats_update_end(&sq->stats.syncp);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001323}
1324
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001325static void virtnet_poll_cleantx(struct receive_queue *rq)
1326{
1327 struct virtnet_info *vi = rq->vq->vdev->priv;
1328 unsigned int index = vq2rxq(rq->vq);
1329 struct send_queue *sq = &vi->sq[index];
1330 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1331
1332 if (!sq->napi.weight)
1333 return;
1334
1335 if (__netif_tx_trylock(txq)) {
1336 free_old_xmit_skbs(sq);
1337 __netif_tx_unlock(txq);
1338 }
1339
1340 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1341 netif_tx_wake_queue(txq);
1342}
1343
Jason Wang2ffa7592014-07-23 16:33:54 +08001344static int virtnet_poll(struct napi_struct *napi, int budget)
1345{
1346 struct receive_queue *rq =
1347 container_of(napi, struct receive_queue, napi);
Jason Wang9267c432018-04-13 14:58:25 +08001348 struct virtnet_info *vi = rq->vq->vdev->priv;
1349 struct send_queue *sq;
1350 unsigned int received, qp;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001351 unsigned int xdp_xmit = 0;
Jason Wang2ffa7592014-07-23 16:33:54 +08001352
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001353 virtnet_poll_cleantx(rq);
1354
Jason Wang186b3c92017-09-19 17:42:43 +08001355 received = virtnet_receive(rq, budget, &xdp_xmit);
Jason Wang2ffa7592014-07-23 16:33:54 +08001356
Rusty Russell8329d982007-11-19 11:20:43 -05001357 /* Out of packets? */
Willem de Bruijne4e84522017-04-24 13:49:26 -04001358 if (received < budget)
1359 virtqueue_napi_complete(napi, rq->vq, received);
Rusty Russell296f96f2007-10-22 11:03:37 +10001360
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001361 if (xdp_xmit & VIRTIO_XDP_REDIR)
1362 xdp_do_flush_map();
1363
1364 if (xdp_xmit & VIRTIO_XDP_TX) {
Jason Wang9267c432018-04-13 14:58:25 +08001365 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs +
1366 smp_processor_id();
1367 sq = &vi->sq[qp];
1368 virtqueue_kick(sq->vq);
Jason Wang9267c432018-04-13 14:58:25 +08001369 }
Jason Wang186b3c92017-09-19 17:42:43 +08001370
Rusty Russell296f96f2007-10-22 11:03:37 +10001371 return received;
1372}
1373
Jason Wang986a4f42012-12-07 07:04:56 +00001374static int virtnet_open(struct net_device *dev)
1375{
1376 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001377 int i, err;
Jason Wang986a4f42012-12-07 07:04:56 +00001378
Jason Wange4166622013-05-21 20:03:58 +00001379 for (i = 0; i < vi->max_queue_pairs; i++) {
1380 if (i < vi->curr_queue_pairs)
1381 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001382 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001383 schedule_delayed_work(&vi->refill, 0);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001384
1385 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i);
1386 if (err < 0)
1387 return err;
1388
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +02001389 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1390 MEM_TYPE_PAGE_SHARED, NULL);
1391 if (err < 0) {
1392 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1393 return err;
1394 }
1395
Willem de Bruijne4e84522017-04-24 13:49:26 -04001396 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001397 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001398 }
1399
1400 return 0;
1401}
1402
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001403static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1404{
1405 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1406 struct virtnet_info *vi = sq->vq->vdev->priv;
1407 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
1408
1409 __netif_tx_lock(txq, raw_smp_processor_id());
1410 free_old_xmit_skbs(sq);
1411 __netif_tx_unlock(txq);
1412
1413 virtqueue_napi_complete(napi, sq->vq, 0);
1414
1415 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1416 netif_tx_wake_queue(txq);
1417
1418 return 0;
1419}
1420
Jason Wange9d74172012-12-07 07:04:55 +00001421static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001422{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001423 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001424 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001425 struct virtnet_info *vi = sq->vq->vdev->priv;
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001426 int num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001427 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301428 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001429
Johannes Berge1749612008-10-27 15:59:26 -07001430 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301431
1432 can_push = vi->any_header_sg &&
1433 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1434 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1435 /* Even if we can, don't push here yet as this would skew
1436 * csum_start offset below. */
1437 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001438 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301439 else
1440 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001441
Mike Rapoporte858fae2016-06-08 16:09:21 +03001442 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04001443 virtio_is_little_endian(vi->vdev), false,
1444 0))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001445 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001446
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001447 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001448 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001449
Jason Wang547c8902015-08-27 14:53:06 +08001450 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301451 if (can_push) {
1452 __skb_push(skb, hdr_len);
1453 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001454 if (unlikely(num_sg < 0))
1455 return num_sg;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301456 /* Pull header back to avoid skew in tx bytes calculations. */
1457 __skb_pull(skb, hdr_len);
1458 } else {
1459 sg_set_buf(sq->sg, hdr, hdr_len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001460 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1461 if (unlikely(num_sg < 0))
1462 return num_sg;
1463 num_sg++;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301464 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301465 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001466}
1467
Stephen Hemminger424efe92009-08-31 19:50:51 +00001468static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001469{
1470 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001471 int qnum = skb_get_queue_mapping(skb);
1472 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301473 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001474 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1475 bool kick = !skb->xmit_more;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001476 bool use_napi = sq->napi.weight;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001477
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001478 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001479 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001480
Willem de Bruijnbdb12e02017-04-24 13:49:30 -04001481 if (use_napi && kick)
1482 virtqueue_enable_cb_delayed(sq->vq);
1483
Jacob Keller074c3582014-06-25 02:37:13 +00001484 /* timestamp packet in software */
1485 skb_tx_timestamp(skb);
1486
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001487 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001488 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001489
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301490 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001491 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301492 dev->stats.tx_fifo_errors++;
1493 if (net_ratelimit())
1494 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001495 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001496 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001497 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001498 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001499 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001500
Rusty Russell48925e32009-09-24 09:59:20 -06001501 /* Don't wait up for transmitted skbs to be freed. */
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001502 if (!use_napi) {
1503 skb_orphan(skb);
1504 nf_reset(skb);
1505 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001506
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001507 /* If running out of space, stop queue to avoid getting packets that we
1508 * are then unable to transmit.
1509 * An alternative would be to force queuing layer to requeue the skb by
1510 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1511 * returned in a normal path of operation: it means that driver is not
1512 * maintaining the TX queue stop/start state properly, and causes
1513 * the stack to do a non-trivial amount of useless work.
1514 * Since most packets only take 1 or 2 ring slots, stopping the queue
1515 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001516 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001517 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001518 netif_stop_subqueue(dev, qnum);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001519 if (!use_napi &&
1520 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001521 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001522 free_old_xmit_skbs(sq);
1523 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001524 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001525 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001526 }
1527 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001528 }
Rusty Russell48925e32009-09-24 09:59:20 -06001529
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001530 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001531 virtqueue_kick(sq->vq);
1532
Rusty Russell48925e32009-09-24 09:59:20 -06001533 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001534}
1535
Amos Kong40cbfc32013-01-21 01:17:21 +00001536/*
1537 * Send command via the control virtqueue and check status. Commands
1538 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001539 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001540 */
1541static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001542 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001543{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301544 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001545 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001546
1547 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301548 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001549
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001550 vi->ctrl->status = ~0;
1551 vi->ctrl->hdr.class = class;
1552 vi->ctrl->hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301553 /* Add header */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001554 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301555 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001556
Rusty Russellf7bc9592013-03-20 15:44:28 +10301557 if (out)
1558 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001559
Rusty Russellf7bc9592013-03-20 15:44:28 +10301560 /* Add return status. */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001561 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001562 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001563
stephen hemmingerd24bae32013-12-09 16:17:40 -08001564 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301565 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001566
Heinz Graalfs67975902013-10-29 09:40:02 +10301567 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001568 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001569
1570 /* Spin for a response, the kick causes an ioport write, trapping
1571 * into the hypervisor, so the request should be handled immediately.
1572 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301573 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1574 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001575 cpu_relax();
1576
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001577 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001578}
1579
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001580static int virtnet_set_mac_address(struct net_device *dev, void *p)
1581{
1582 struct virtnet_info *vi = netdev_priv(dev);
1583 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001584 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001585 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001586 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001587
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07001588 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1589 return -EOPNOTSUPP;
1590
Shyam Saini801822d2016-12-24 00:44:58 +05301591 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001592 if (!addr)
1593 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001594
1595 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001596 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001597 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001598
Amos Kong7e58d5a2013-01-21 01:17:23 +00001599 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1600 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1601 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001602 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001603 dev_warn(&vdev->dev,
1604 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001605 ret = -EINVAL;
1606 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001607 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001608 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1609 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301610 unsigned int i;
1611
1612 /* Naturally, this has an atomicity problem. */
1613 for (i = 0; i < dev->addr_len; i++)
1614 virtio_cwrite8(vdev,
1615 offsetof(struct virtio_net_config, mac) +
1616 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001617 }
1618
1619 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001620 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001621
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001622out:
1623 kfree(addr);
1624 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001625}
1626
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001627static void virtnet_stats(struct net_device *dev,
1628 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001629{
1630 struct virtnet_info *vi = netdev_priv(dev);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001631 unsigned int start;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001632 int i;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001633
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001634 for (i = 0; i < vi->max_queue_pairs; i++) {
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001635 u64 tpackets, tbytes, rpackets, rbytes;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001636 struct receive_queue *rq = &vi->rq[i];
1637 struct send_queue *sq = &vi->sq[i];
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001638
1639 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001640 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1641 tpackets = sq->stats.packets;
1642 tbytes = sq->stats.bytes;
1643 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001644
1645 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001646 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001647 rpackets = rq->stats.items.packets;
1648 rbytes = rq->stats.items.bytes;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001649 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001650
1651 tot->rx_packets += rpackets;
1652 tot->tx_packets += tpackets;
1653 tot->rx_bytes += rbytes;
1654 tot->tx_bytes += tbytes;
1655 }
1656
1657 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001658 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001659 tot->rx_dropped = dev->stats.rx_dropped;
1660 tot->rx_length_errors = dev->stats.rx_length_errors;
1661 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001662}
1663
Amit Shahda74e892008-02-29 16:24:50 +05301664#ifdef CONFIG_NET_POLL_CONTROLLER
1665static void virtnet_netpoll(struct net_device *dev)
1666{
1667 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001668 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301669
Jason Wang986a4f42012-12-07 07:04:56 +00001670 for (i = 0; i < vi->curr_queue_pairs; i++)
1671 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301672}
1673#endif
1674
Jason Wang586d17c2012-04-11 20:43:52 +00001675static void virtnet_ack_link_announce(struct virtnet_info *vi)
1676{
1677 rtnl_lock();
1678 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001679 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001680 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1681 rtnl_unlock();
1682}
1683
John Fastabend473153292017-02-02 19:14:32 -08001684static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001685{
1686 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001687 struct net_device *dev = vi->dev;
1688
1689 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1690 return 0;
1691
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001692 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1693 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001694
1695 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001696 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001697 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1698 queue_pairs);
1699 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301700 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001701 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001702 /* virtnet_open() will refill when device is going to up. */
1703 if (dev->flags & IFF_UP)
1704 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301705 }
Jason Wang986a4f42012-12-07 07:04:56 +00001706
1707 return 0;
1708}
1709
John Fastabend473153292017-02-02 19:14:32 -08001710static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1711{
1712 int err;
1713
1714 rtnl_lock();
1715 err = _virtnet_set_queues(vi, queue_pairs);
1716 rtnl_unlock();
1717 return err;
1718}
1719
Rusty Russell296f96f2007-10-22 11:03:37 +10001720static int virtnet_close(struct net_device *dev)
1721{
1722 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001723 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001724
Rusty Russellb2baed62011-12-29 00:42:38 +00001725 /* Make sure refill_work doesn't re-enable napi! */
1726 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001727
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001728 for (i = 0; i < vi->max_queue_pairs; i++) {
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001729 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
Jason Wang986a4f42012-12-07 07:04:56 +00001730 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001731 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001732 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001733
Rusty Russell296f96f2007-10-22 11:03:37 +10001734 return 0;
1735}
1736
Alex Williamson2af76982009-02-04 09:02:40 +00001737static void virtnet_set_rx_mode(struct net_device *dev)
1738{
1739 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001740 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001741 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001742 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001743 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001744 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001745 void *buf;
1746 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001747
stephen hemminger788a8b62013-12-09 16:18:45 -08001748 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001749 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1750 return;
1751
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001752 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1753 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001754
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001755 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001756
1757 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001758 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001759 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001760 vi->ctrl->promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001761
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001762 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001763
1764 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001765 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001766 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001767 vi->ctrl->allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001768
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001769 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001770 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001771 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001772 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1773 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1774 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001775 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001776 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001777
Alex Williamson23e258e2009-05-01 17:27:56 +00001778 sg_init_table(sg, 2);
1779
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001780 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001781 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001782 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001783 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001784 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001785
1786 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001787 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001788
1789 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001790 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001791
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001792 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001793 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001794 netdev_for_each_mc_addr(ha, dev)
1795 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001796
1797 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001798 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001799
1800 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001801 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001802 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001803
1804 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001805}
1806
Patrick McHardy80d5c362013-04-19 02:04:28 +00001807static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1808 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001809{
1810 struct virtnet_info *vi = netdev_priv(dev);
1811 struct scatterlist sg;
1812
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03001813 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001814 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001815
1816 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001817 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001818 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001819 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001820}
1821
Patrick McHardy80d5c362013-04-19 02:04:28 +00001822static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1823 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001824{
1825 struct virtnet_info *vi = netdev_priv(dev);
1826 struct scatterlist sg;
1827
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03001828 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001829 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001830
1831 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001832 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001833 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001834 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001835}
1836
Wanlong Gao8898c212013-01-24 23:51:30 +00001837static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001838{
1839 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001840
1841 if (vi->affinity_hint_set) {
1842 for (i = 0; i < vi->max_queue_pairs; i++) {
1843 virtqueue_set_affinity(vi->rq[i].vq, -1);
1844 virtqueue_set_affinity(vi->sq[i].vq, -1);
1845 }
1846
1847 vi->affinity_hint_set = false;
1848 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001849}
1850
1851static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001852{
1853 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001854 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001855
1856 /* In multiqueue mode, when the number of cpu is equal to the number of
1857 * queue pairs, we let the queue pairs to be private to one cpu by
1858 * setting the affinity hint to eliminate the contention.
1859 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001860 if (vi->curr_queue_pairs == 1 ||
1861 vi->max_queue_pairs != num_online_cpus()) {
1862 virtnet_clean_affinity(vi, -1);
1863 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001864 }
1865
Wanlong Gao8898c212013-01-24 23:51:30 +00001866 i = 0;
1867 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001868 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1869 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001870 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001871 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001872 }
1873
Wanlong Gao8898c212013-01-24 23:51:30 +00001874 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001875}
1876
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001877static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001878{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001879 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1880 node);
1881 virtnet_set_affinity(vi);
1882 return 0;
1883}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001884
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001885static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1886{
1887 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1888 node_dead);
1889 virtnet_set_affinity(vi);
1890 return 0;
1891}
Jason Wang3ab098d2013-10-15 11:18:58 +08001892
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001893static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1894{
1895 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1896 node);
1897
1898 virtnet_clean_affinity(vi, cpu);
1899 return 0;
1900}
1901
1902static enum cpuhp_state virtionet_online;
1903
1904static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1905{
1906 int ret;
1907
1908 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1909 if (ret)
1910 return ret;
1911 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1912 &vi->node_dead);
1913 if (!ret)
1914 return ret;
1915 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1916 return ret;
1917}
1918
1919static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1920{
1921 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1922 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1923 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001924}
1925
Rick Jones8f9f4662011-10-19 08:10:59 +00001926static void virtnet_get_ringparam(struct net_device *dev,
1927 struct ethtool_ringparam *ring)
1928{
1929 struct virtnet_info *vi = netdev_priv(dev);
1930
Jason Wang986a4f42012-12-07 07:04:56 +00001931 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1932 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001933 ring->rx_pending = ring->rx_max_pending;
1934 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001935}
1936
Rick Jones66846042011-11-14 14:17:08 +00001937
1938static void virtnet_get_drvinfo(struct net_device *dev,
1939 struct ethtool_drvinfo *info)
1940{
1941 struct virtnet_info *vi = netdev_priv(dev);
1942 struct virtio_device *vdev = vi->vdev;
1943
1944 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1945 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1946 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1947
1948}
1949
Jason Wangd73bcd22012-12-07 07:04:57 +00001950/* TODO: Eliminate OOO packets during switching */
1951static int virtnet_set_channels(struct net_device *dev,
1952 struct ethtool_channels *channels)
1953{
1954 struct virtnet_info *vi = netdev_priv(dev);
1955 u16 queue_pairs = channels->combined_count;
1956 int err;
1957
1958 /* We don't support separate rx/tx channels.
1959 * We don't allow setting 'other' channels.
1960 */
1961 if (channels->rx_count || channels->tx_count || channels->other_count)
1962 return -EINVAL;
1963
Amos Kongc18e9cd2014-04-18 13:45:41 +08001964 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001965 return -EINVAL;
1966
John Fastabendf600b692016-12-15 12:13:24 -08001967 /* For now we don't support modifying channels while XDP is loaded
1968 * also when XDP is loaded all RX queues have XDP programs so we only
1969 * need to check a single RX queue.
1970 */
1971 if (vi->rq[0].xdp_prog)
1972 return -EINVAL;
1973
Wanlong Gao47be2472013-01-24 23:51:29 +00001974 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08001975 err = _virtnet_set_queues(vi, queue_pairs);
Jason Wangd73bcd22012-12-07 07:04:57 +00001976 if (!err) {
1977 netif_set_real_num_tx_queues(dev, queue_pairs);
1978 netif_set_real_num_rx_queues(dev, queue_pairs);
1979
Wanlong Gao8898c212013-01-24 23:51:30 +00001980 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001981 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001982 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001983
1984 return err;
1985}
1986
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001987static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1988{
1989 struct virtnet_info *vi = netdev_priv(dev);
1990 char *p = (char *)data;
1991 unsigned int i, j;
1992
1993 switch (stringset) {
1994 case ETH_SS_STATS:
1995 for (i = 0; i < vi->curr_queue_pairs; i++) {
1996 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
1997 snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s",
1998 i, virtnet_rq_stats_desc[j].desc);
1999 p += ETH_GSTRING_LEN;
2000 }
2001 }
2002
2003 for (i = 0; i < vi->curr_queue_pairs; i++) {
2004 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2005 snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s",
2006 i, virtnet_sq_stats_desc[j].desc);
2007 p += ETH_GSTRING_LEN;
2008 }
2009 }
2010 break;
2011 }
2012}
2013
2014static int virtnet_get_sset_count(struct net_device *dev, int sset)
2015{
2016 struct virtnet_info *vi = netdev_priv(dev);
2017
2018 switch (sset) {
2019 case ETH_SS_STATS:
2020 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2021 VIRTNET_SQ_STATS_LEN);
2022 default:
2023 return -EOPNOTSUPP;
2024 }
2025}
2026
2027static void virtnet_get_ethtool_stats(struct net_device *dev,
2028 struct ethtool_stats *stats, u64 *data)
2029{
2030 struct virtnet_info *vi = netdev_priv(dev);
2031 unsigned int idx = 0, start, i, j;
2032 const u8 *stats_base;
2033 size_t offset;
2034
2035 for (i = 0; i < vi->curr_queue_pairs; i++) {
2036 struct receive_queue *rq = &vi->rq[i];
2037
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09002038 stats_base = (u8 *)&rq->stats.items;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002039 do {
2040 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2041 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2042 offset = virtnet_rq_stats_desc[j].offset;
2043 data[idx + j] = *(u64 *)(stats_base + offset);
2044 }
2045 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2046 idx += VIRTNET_RQ_STATS_LEN;
2047 }
2048
2049 for (i = 0; i < vi->curr_queue_pairs; i++) {
2050 struct send_queue *sq = &vi->sq[i];
2051
2052 stats_base = (u8 *)&sq->stats;
2053 do {
2054 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2055 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2056 offset = virtnet_sq_stats_desc[j].offset;
2057 data[idx + j] = *(u64 *)(stats_base + offset);
2058 }
2059 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2060 idx += VIRTNET_SQ_STATS_LEN;
2061 }
2062}
2063
Jason Wangd73bcd22012-12-07 07:04:57 +00002064static void virtnet_get_channels(struct net_device *dev,
2065 struct ethtool_channels *channels)
2066{
2067 struct virtnet_info *vi = netdev_priv(dev);
2068
2069 channels->combined_count = vi->curr_queue_pairs;
2070 channels->max_combined = vi->max_queue_pairs;
2071 channels->max_other = 0;
2072 channels->rx_count = 0;
2073 channels->tx_count = 0;
2074 channels->other_count = 0;
2075}
2076
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002077/* Check if the user is trying to change anything besides speed/duplex */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002078static bool
2079virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002080{
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002081 struct ethtool_link_ksettings diff1 = *cmd;
2082 struct ethtool_link_ksettings diff2 = {};
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002083
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01002084 /* cmd is always set so we need to clear it, validate the port type
2085 * and also without autonegotiation we can ignore advertising
2086 */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002087 diff1.base.speed = 0;
2088 diff2.base.port = PORT_OTHER;
2089 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
2090 diff1.base.duplex = 0;
2091 diff1.base.cmd = 0;
2092 diff1.base.link_mode_masks_nwords = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002093
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002094 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
2095 bitmap_empty(diff1.link_modes.supported,
2096 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2097 bitmap_empty(diff1.link_modes.advertising,
2098 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2099 bitmap_empty(diff1.link_modes.lp_advertising,
2100 __ETHTOOL_LINK_MODE_MASK_NBITS);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002101}
2102
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002103static int virtnet_set_link_ksettings(struct net_device *dev,
2104 const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002105{
2106 struct virtnet_info *vi = netdev_priv(dev);
2107 u32 speed;
2108
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002109 speed = cmd->base.speed;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002110 /* don't allow custom speed and duplex */
2111 if (!ethtool_validate_speed(speed) ||
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002112 !ethtool_validate_duplex(cmd->base.duplex) ||
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002113 !virtnet_validate_ethtool_cmd(cmd))
2114 return -EINVAL;
2115 vi->speed = speed;
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002116 vi->duplex = cmd->base.duplex;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002117
2118 return 0;
2119}
2120
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002121static int virtnet_get_link_ksettings(struct net_device *dev,
2122 struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002123{
2124 struct virtnet_info *vi = netdev_priv(dev);
2125
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002126 cmd->base.speed = vi->speed;
2127 cmd->base.duplex = vi->duplex;
2128 cmd->base.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002129
2130 return 0;
2131}
2132
2133static void virtnet_init_settings(struct net_device *dev)
2134{
2135 struct virtnet_info *vi = netdev_priv(dev);
2136
2137 vi->speed = SPEED_UNKNOWN;
2138 vi->duplex = DUPLEX_UNKNOWN;
2139}
2140
Jason Baronfaa9b392018-01-05 17:44:54 -05002141static void virtnet_update_settings(struct virtnet_info *vi)
2142{
2143 u32 speed;
2144 u8 duplex;
2145
2146 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2147 return;
2148
2149 speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config,
2150 speed));
2151 if (ethtool_validate_speed(speed))
2152 vi->speed = speed;
2153 duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config,
2154 duplex));
2155 if (ethtool_validate_duplex(duplex))
2156 vi->duplex = duplex;
2157}
2158
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002159static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00002160 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002161 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00002162 .get_ringparam = virtnet_get_ringparam,
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002163 .get_strings = virtnet_get_strings,
2164 .get_sset_count = virtnet_get_sset_count,
2165 .get_ethtool_stats = virtnet_get_ethtool_stats,
Jason Wangd73bcd22012-12-07 07:04:57 +00002166 .set_channels = virtnet_set_channels,
2167 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00002168 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002169 .get_link_ksettings = virtnet_get_link_ksettings,
2170 .set_link_ksettings = virtnet_set_link_ksettings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08002171};
2172
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002173static void virtnet_freeze_down(struct virtio_device *vdev)
2174{
2175 struct virtnet_info *vi = vdev->priv;
2176 int i;
2177
2178 /* Make sure no work handler is accessing the device */
2179 flush_work(&vi->config_work);
2180
2181 netif_device_detach(vi->dev);
Jason Wang713a98d2017-06-28 09:51:03 +08002182 netif_tx_disable(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002183 cancel_delayed_work_sync(&vi->refill);
2184
2185 if (netif_running(vi->dev)) {
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002186 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002187 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04002188 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002189 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002190 }
2191}
2192
2193static int init_vqs(struct virtnet_info *vi);
2194
2195static int virtnet_restore_up(struct virtio_device *vdev)
2196{
2197 struct virtnet_info *vi = vdev->priv;
2198 int err, i;
2199
2200 err = init_vqs(vi);
2201 if (err)
2202 return err;
2203
2204 virtio_device_ready(vdev);
2205
2206 if (netif_running(vi->dev)) {
2207 for (i = 0; i < vi->curr_queue_pairs; i++)
2208 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2209 schedule_delayed_work(&vi->refill, 0);
2210
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002211 for (i = 0; i < vi->max_queue_pairs; i++) {
Willem de Bruijne4e84522017-04-24 13:49:26 -04002212 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002213 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2214 &vi->sq[i].napi);
2215 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002216 }
2217
2218 netif_device_attach(vi->dev);
2219 return err;
2220}
2221
Jason Wang3f935222017-07-19 16:54:49 +08002222static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2223{
2224 struct scatterlist sg;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002225 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
Jason Wang3f935222017-07-19 16:54:49 +08002226
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002227 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
Jason Wang3f935222017-07-19 16:54:49 +08002228
2229 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2230 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2231 dev_warn(&vi->dev->dev, "Fail to set guest offload. \n");
2232 return -EINVAL;
2233 }
2234
2235 return 0;
2236}
2237
2238static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2239{
2240 u64 offloads = 0;
2241
2242 if (!vi->guest_offloads)
2243 return 0;
2244
2245 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
2246 offloads = 1ULL << VIRTIO_NET_F_GUEST_CSUM;
2247
2248 return virtnet_set_guest_offloads(vi, offloads);
2249}
2250
2251static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2252{
2253 u64 offloads = vi->guest_offloads;
2254
2255 if (!vi->guest_offloads)
2256 return 0;
2257 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
2258 offloads |= 1ULL << VIRTIO_NET_F_GUEST_CSUM;
2259
2260 return virtnet_set_guest_offloads(vi, offloads);
2261}
2262
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002263static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2264 struct netlink_ext_ack *extack)
John Fastabendf600b692016-12-15 12:13:24 -08002265{
2266 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2267 struct virtnet_info *vi = netdev_priv(dev);
2268 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08002269 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002270 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08002271
Jason Wang3f935222017-07-19 16:54:49 +08002272 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2273 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2274 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2275 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2276 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO))) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002277 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first");
John Fastabendf600b692016-12-15 12:13:24 -08002278 return -EOPNOTSUPP;
2279 }
2280
2281 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002282 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
John Fastabendf600b692016-12-15 12:13:24 -08002283 return -EINVAL;
2284 }
2285
2286 if (dev->mtu > max_sz) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002287 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
John Fastabendf600b692016-12-15 12:13:24 -08002288 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2289 return -EINVAL;
2290 }
2291
John Fastabend672aafd2016-12-15 12:13:49 -08002292 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2293 if (prog)
2294 xdp_qp = nr_cpu_ids;
2295
2296 /* XDP requires extra queues for XDP_TX */
2297 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002298 NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
John Fastabend672aafd2016-12-15 12:13:49 -08002299 netdev_warn(dev, "request %i queues but max is %i\n",
2300 curr_qp + xdp_qp, vi->max_queue_pairs);
2301 return -ENOMEM;
2302 }
2303
John Fastabend2de2f7f2017-02-02 19:16:29 -08002304 if (prog) {
2305 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
2306 if (IS_ERR(prog))
2307 return PTR_ERR(prog);
2308 }
2309
Jason Wang4941d472017-07-19 16:54:48 +08002310 /* Make sure NAPI is not using any XDP TX queues for RX. */
Jason Wang4e09ff52018-02-28 18:20:04 +08002311 if (netif_running(dev))
2312 for (i = 0; i < vi->max_queue_pairs; i++)
2313 napi_disable(&vi->rq[i].napi);
John Fastabendf600b692016-12-15 12:13:24 -08002314
John Fastabend672aafd2016-12-15 12:13:49 -08002315 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
Jason Wang4941d472017-07-19 16:54:48 +08002316 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2317 if (err)
2318 goto err;
2319 vi->xdp_queue_pairs = xdp_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002320
John Fastabendf600b692016-12-15 12:13:24 -08002321 for (i = 0; i < vi->max_queue_pairs; i++) {
2322 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2323 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
Jason Wang3f935222017-07-19 16:54:49 +08002324 if (i == 0) {
2325 if (!old_prog)
2326 virtnet_clear_guest_offloads(vi);
2327 if (!prog)
2328 virtnet_restore_guest_offloads(vi);
2329 }
John Fastabendf600b692016-12-15 12:13:24 -08002330 if (old_prog)
2331 bpf_prog_put(old_prog);
Jason Wang4e09ff52018-02-28 18:20:04 +08002332 if (netif_running(dev))
2333 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
John Fastabendf600b692016-12-15 12:13:24 -08002334 }
2335
2336 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08002337
Jason Wang4941d472017-07-19 16:54:48 +08002338err:
2339 for (i = 0; i < vi->max_queue_pairs; i++)
2340 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
John Fastabend2de2f7f2017-02-02 19:16:29 -08002341 if (prog)
2342 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2343 return err;
John Fastabendf600b692016-12-15 12:13:24 -08002344}
2345
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002346static u32 virtnet_xdp_query(struct net_device *dev)
John Fastabendf600b692016-12-15 12:13:24 -08002347{
2348 struct virtnet_info *vi = netdev_priv(dev);
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002349 const struct bpf_prog *xdp_prog;
John Fastabendf600b692016-12-15 12:13:24 -08002350 int i;
2351
2352 for (i = 0; i < vi->max_queue_pairs; i++) {
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002353 xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2354 if (xdp_prog)
2355 return xdp_prog->aux->id;
John Fastabendf600b692016-12-15 12:13:24 -08002356 }
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002357 return 0;
John Fastabendf600b692016-12-15 12:13:24 -08002358}
2359
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002360static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
John Fastabendf600b692016-12-15 12:13:24 -08002361{
2362 switch (xdp->command) {
2363 case XDP_SETUP_PROG:
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002364 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
John Fastabendf600b692016-12-15 12:13:24 -08002365 case XDP_QUERY_PROG:
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002366 xdp->prog_id = virtnet_xdp_query(dev);
John Fastabendf600b692016-12-15 12:13:24 -08002367 return 0;
2368 default:
2369 return -EINVAL;
2370 }
2371}
2372
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002373static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2374 size_t len)
2375{
2376 struct virtnet_info *vi = netdev_priv(dev);
2377 int ret;
2378
2379 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2380 return -EOPNOTSUPP;
2381
2382 ret = snprintf(buf, len, "sby");
2383 if (ret >= len)
2384 return -EOPNOTSUPP;
2385
2386 return 0;
2387}
2388
Stephen Hemminger76288b42009-01-06 10:44:22 -08002389static const struct net_device_ops virtnet_netdev = {
2390 .ndo_open = virtnet_open,
2391 .ndo_stop = virtnet_close,
2392 .ndo_start_xmit = start_xmit,
2393 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08002394 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00002395 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002396 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00002397 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2398 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002399#ifdef CONFIG_NET_POLL_CONTROLLER
2400 .ndo_poll_controller = virtnet_netpoll,
2401#endif
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002402 .ndo_bpf = virtnet_xdp,
Jason Wang186b3c92017-09-19 17:42:43 +08002403 .ndo_xdp_xmit = virtnet_xdp_xmit,
Vlad Yasevich2836b4f2017-05-23 13:38:43 -04002404 .ndo_features_check = passthru_features_check,
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002405 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002406};
2407
Jason Wang586d17c2012-04-11 20:43:52 +00002408static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002409{
Jason Wang586d17c2012-04-11 20:43:52 +00002410 struct virtnet_info *vi =
2411 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002412 u16 v;
2413
Rusty Russell855e0c52013-10-14 18:11:51 +10302414 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2415 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302416 return;
Jason Wang586d17c2012-04-11 20:43:52 +00002417
2418 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00002419 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00002420 virtnet_ack_link_announce(vi);
2421 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002422
2423 /* Ignore unknown (future) status bits */
2424 v &= VIRTIO_NET_S_LINK_UP;
2425
2426 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302427 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002428
2429 vi->status = v;
2430
2431 if (vi->status & VIRTIO_NET_S_LINK_UP) {
Jason Baronfaa9b392018-01-05 17:44:54 -05002432 virtnet_update_settings(vi);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002433 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002434 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002435 } else {
2436 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002437 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002438 }
2439}
2440
2441static void virtnet_config_changed(struct virtio_device *vdev)
2442{
2443 struct virtnet_info *vi = vdev->priv;
2444
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002445 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002446}
2447
Jason Wang986a4f42012-12-07 07:04:56 +00002448static void virtnet_free_queues(struct virtnet_info *vi)
2449{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002450 int i;
2451
Jason Wangab3971b2015-03-12 13:57:44 +08002452 for (i = 0; i < vi->max_queue_pairs; i++) {
2453 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002454 netif_napi_del(&vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002455 netif_napi_del(&vi->sq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08002456 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002457
Eric Dumazet963abe52016-11-15 22:24:12 -08002458 /* We called napi_hash_del() before netif_napi_del(),
2459 * we need to respect an RCU grace period before freeing vi->rq
2460 */
2461 synchronize_net();
2462
Jason Wang986a4f42012-12-07 07:04:56 +00002463 kfree(vi->rq);
2464 kfree(vi->sq);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002465 kfree(vi->ctrl);
Jason Wang986a4f42012-12-07 07:04:56 +00002466}
2467
John Fastabend473153292017-02-02 19:14:32 -08002468static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002469{
John Fastabendf600b692016-12-15 12:13:24 -08002470 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00002471 int i;
2472
2473 for (i = 0; i < vi->max_queue_pairs; i++) {
2474 while (vi->rq[i].pages)
2475 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08002476
2477 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2478 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2479 if (old_prog)
2480 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00002481 }
John Fastabend473153292017-02-02 19:14:32 -08002482}
2483
2484static void free_receive_bufs(struct virtnet_info *vi)
2485{
2486 rtnl_lock();
2487 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08002488 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002489}
2490
Michael Daltonfb518792014-01-16 22:23:26 -08002491static void free_receive_page_frags(struct virtnet_info *vi)
2492{
2493 int i;
2494 for (i = 0; i < vi->max_queue_pairs; i++)
2495 if (vi->rq[i].alloc_frag.page)
2496 put_page(vi->rq[i].alloc_frag.page);
2497}
2498
John Fastabendb68df012017-01-25 18:22:48 -08002499static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08002500{
2501 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
2502 return false;
2503 else if (q < vi->curr_queue_pairs)
2504 return true;
2505 else
2506 return false;
2507}
2508
Jason Wang986a4f42012-12-07 07:04:56 +00002509static void free_unused_bufs(struct virtnet_info *vi)
2510{
2511 void *buf;
2512 int i;
2513
2514 for (i = 0; i < vi->max_queue_pairs; i++) {
2515 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002516 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08002517 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08002518 dev_kfree_skb(buf);
2519 else
2520 put_page(virt_to_head_page(buf));
2521 }
Jason Wang986a4f42012-12-07 07:04:56 +00002522 }
2523
2524 for (i = 0; i < vi->max_queue_pairs; i++) {
2525 struct virtqueue *vq = vi->rq[i].vq;
2526
2527 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002528 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02002529 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002530 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002531 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002532 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002533 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002534 }
Jason Wang986a4f42012-12-07 07:04:56 +00002535 }
Jason Wang986a4f42012-12-07 07:04:56 +00002536 }
2537}
2538
Jason Wange9d74172012-12-07 07:04:55 +00002539static void virtnet_del_vqs(struct virtnet_info *vi)
2540{
2541 struct virtio_device *vdev = vi->vdev;
2542
Wanlong Gao8898c212013-01-24 23:51:30 +00002543 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00002544
Jason Wange9d74172012-12-07 07:04:55 +00002545 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002546
2547 virtnet_free_queues(vi);
2548}
2549
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002550/* How large should a single buffer be so a queue full of these can fit at
2551 * least one full packet?
2552 * Logic below assumes the mergeable buffer header is used.
2553 */
2554static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2555{
2556 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2557 unsigned int rq_size = virtqueue_get_vring_size(vq);
2558 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2559 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2560 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2561
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03002562 return max(max(min_buf_len, hdr_len) - hdr_len,
2563 (unsigned int)GOOD_PACKET_LEN);
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002564}
2565
Jason Wang986a4f42012-12-07 07:04:56 +00002566static int virtnet_find_vqs(struct virtnet_info *vi)
2567{
2568 vq_callback_t **callbacks;
2569 struct virtqueue **vqs;
2570 int ret = -ENOMEM;
2571 int i, total_vqs;
2572 const char **names;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002573 bool *ctx;
Jason Wang986a4f42012-12-07 07:04:56 +00002574
2575 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2576 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2577 * possible control vq.
2578 */
2579 total_vqs = vi->max_queue_pairs * 2 +
2580 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2581
2582 /* Allocate space for find_vqs parameters */
Kees Cook6396bb22018-06-12 14:03:40 -07002583 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002584 if (!vqs)
2585 goto err_vq;
Kees Cook6da2ec52018-06-12 13:55:00 -07002586 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002587 if (!callbacks)
2588 goto err_callback;
Kees Cook6da2ec52018-06-12 13:55:00 -07002589 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002590 if (!names)
2591 goto err_names;
Jason Wang192f68c2017-07-19 16:54:47 +08002592 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Kees Cook6396bb22018-06-12 14:03:40 -07002593 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002594 if (!ctx)
2595 goto err_ctx;
2596 } else {
2597 ctx = NULL;
2598 }
Jason Wang986a4f42012-12-07 07:04:56 +00002599
2600 /* Parameters for control virtqueue, if any */
2601 if (vi->has_cvq) {
2602 callbacks[total_vqs - 1] = NULL;
2603 names[total_vqs - 1] = "control";
2604 }
2605
2606 /* Allocate/initialize parameters for send/receive virtqueues */
2607 for (i = 0; i < vi->max_queue_pairs; i++) {
2608 callbacks[rxq2vq(i)] = skb_recv_done;
2609 callbacks[txq2vq(i)] = skb_xmit_done;
2610 sprintf(vi->rq[i].name, "input.%d", i);
2611 sprintf(vi->sq[i].name, "output.%d", i);
2612 names[rxq2vq(i)] = vi->rq[i].name;
2613 names[txq2vq(i)] = vi->sq[i].name;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002614 if (ctx)
2615 ctx[rxq2vq(i)] = true;
Jason Wang986a4f42012-12-07 07:04:56 +00002616 }
2617
2618 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002619 names, ctx, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002620 if (ret)
2621 goto err_find;
2622
2623 if (vi->has_cvq) {
2624 vi->cvq = vqs[total_vqs - 1];
2625 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002626 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002627 }
2628
2629 for (i = 0; i < vi->max_queue_pairs; i++) {
2630 vi->rq[i].vq = vqs[rxq2vq(i)];
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002631 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
Jason Wang986a4f42012-12-07 07:04:56 +00002632 vi->sq[i].vq = vqs[txq2vq(i)];
2633 }
2634
Tonghao Zhang2fa3c8a2018-05-31 07:16:32 -07002635 /* run here: ret == 0. */
Jason Wang986a4f42012-12-07 07:04:56 +00002636
Jason Wang986a4f42012-12-07 07:04:56 +00002637
2638err_find:
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002639 kfree(ctx);
2640err_ctx:
Jason Wang986a4f42012-12-07 07:04:56 +00002641 kfree(names);
2642err_names:
2643 kfree(callbacks);
2644err_callback:
2645 kfree(vqs);
2646err_vq:
2647 return ret;
2648}
2649
2650static int virtnet_alloc_queues(struct virtnet_info *vi)
2651{
2652 int i;
2653
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002654 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2655 if (!vi->ctrl)
2656 goto err_ctrl;
Kees Cook6396bb22018-06-12 14:03:40 -07002657 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002658 if (!vi->sq)
2659 goto err_sq;
Kees Cook6396bb22018-06-12 14:03:40 -07002660 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002661 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002662 goto err_rq;
2663
2664 INIT_DELAYED_WORK(&vi->refill, refill_work);
2665 for (i = 0; i < vi->max_queue_pairs; i++) {
2666 vi->rq[i].pages = NULL;
2667 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2668 napi_weight);
Willem de Bruijn1d11e732017-04-27 20:37:58 -04002669 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2670 napi_tx ? napi_weight : 0);
Jason Wang986a4f42012-12-07 07:04:56 +00002671
2672 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002673 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002674 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002675
2676 u64_stats_init(&vi->rq[i].stats.syncp);
2677 u64_stats_init(&vi->sq[i].stats.syncp);
Jason Wang986a4f42012-12-07 07:04:56 +00002678 }
2679
2680 return 0;
2681
2682err_rq:
2683 kfree(vi->sq);
2684err_sq:
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002685 kfree(vi->ctrl);
2686err_ctrl:
Jason Wang986a4f42012-12-07 07:04:56 +00002687 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002688}
2689
Amit Shah3f9c10b2011-12-22 16:58:31 +05302690static int init_vqs(struct virtnet_info *vi)
2691{
Jason Wang986a4f42012-12-07 07:04:56 +00002692 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302693
Jason Wang986a4f42012-12-07 07:04:56 +00002694 /* Allocate send & receive queues */
2695 ret = virtnet_alloc_queues(vi);
2696 if (ret)
2697 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302698
Jason Wang986a4f42012-12-07 07:04:56 +00002699 ret = virtnet_find_vqs(vi);
2700 if (ret)
2701 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302702
Wanlong Gao47be2472013-01-24 23:51:29 +00002703 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002704 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002705 put_online_cpus();
2706
Amit Shah3f9c10b2011-12-22 16:58:31 +05302707 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002708
2709err_free:
2710 virtnet_free_queues(vi);
2711err:
2712 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302713}
2714
Michael Daltonfbf28d72014-01-16 22:23:30 -08002715#ifdef CONFIG_SYSFS
2716static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -07002717 char *buf)
Michael Daltonfbf28d72014-01-16 22:23:30 -08002718{
2719 struct virtnet_info *vi = netdev_priv(queue->dev);
2720 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Jason Wang3cc81a92018-03-02 17:29:14 +08002721 unsigned int headroom = virtnet_get_headroom(vi);
2722 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
Johannes Berg5377d7582015-08-19 09:48:40 +02002723 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002724
2725 BUG_ON(queue_index >= vi->max_queue_pairs);
2726 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002727 return sprintf(buf, "%u\n",
Jason Wang3cc81a92018-03-02 17:29:14 +08002728 get_mergeable_buf_len(&vi->rq[queue_index], avg,
2729 SKB_DATA_ALIGN(headroom + tailroom)));
Michael Daltonfbf28d72014-01-16 22:23:30 -08002730}
2731
2732static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2733 __ATTR_RO(mergeable_rx_buffer_size);
2734
2735static struct attribute *virtio_net_mrg_rx_attrs[] = {
2736 &mergeable_rx_buffer_size_attribute.attr,
2737 NULL
2738};
2739
2740static const struct attribute_group virtio_net_mrg_rx_group = {
2741 .name = "virtio_net",
2742 .attrs = virtio_net_mrg_rx_attrs
2743};
2744#endif
2745
Jason Wang892d6eb2014-11-20 17:03:05 +08002746static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2747 unsigned int fbit,
2748 const char *fname, const char *dname)
2749{
2750 if (!virtio_has_feature(vdev, fbit))
2751 return false;
2752
2753 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2754 fname, dname);
2755
2756 return true;
2757}
2758
2759#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2760 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2761
2762static bool virtnet_validate_features(struct virtio_device *vdev)
2763{
2764 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2765 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2766 "VIRTIO_NET_F_CTRL_VQ") ||
2767 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2768 "VIRTIO_NET_F_CTRL_VQ") ||
2769 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2770 "VIRTIO_NET_F_CTRL_VQ") ||
2771 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2772 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2773 "VIRTIO_NET_F_CTRL_VQ"))) {
2774 return false;
2775 }
2776
2777 return true;
2778}
2779
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002780#define MIN_MTU ETH_MIN_MTU
2781#define MAX_MTU ETH_MAX_MTU
2782
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002783static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10002784{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002785 if (!vdev->config->get) {
2786 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2787 __func__);
2788 return -EINVAL;
2789 }
2790
Jason Wang892d6eb2014-11-20 17:03:05 +08002791 if (!virtnet_validate_features(vdev))
2792 return -EINVAL;
2793
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002794 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2795 int mtu = virtio_cread16(vdev,
2796 offsetof(struct virtio_net_config,
2797 mtu));
2798 if (mtu < MIN_MTU)
2799 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2800 }
2801
2802 return 0;
2803}
2804
2805static int virtnet_probe(struct virtio_device *vdev)
2806{
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002807 int i, err = -ENOMEM;
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002808 struct net_device *dev;
2809 struct virtnet_info *vi;
2810 u16 max_queue_pairs;
2811 int mtu;
2812
Jason Wang986a4f42012-12-07 07:04:56 +00002813 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302814 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2815 struct virtio_net_config,
2816 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002817
2818 /* We need at least 2 queue's */
2819 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2820 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2821 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2822 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002823
2824 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002825 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002826 if (!dev)
2827 return -ENOMEM;
2828
2829 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002830 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002831 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002832 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002833
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002834 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002835 SET_NETDEV_DEV(dev, &vdev->dev);
2836
2837 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002838 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002839 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002840 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002841 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002842 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002843
2844 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
David S. Millere078de02017-07-03 06:37:32 -07002845 dev->hw_features |= NETIF_F_TSO
Rusty Russell34a48572008-02-04 23:50:02 -05002846 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2847 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002848 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002849 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2850 dev->hw_features |= NETIF_F_TSO;
2851 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2852 dev->hw_features |= NETIF_F_TSO6;
2853 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2854 dev->hw_features |= NETIF_F_TSO_ECN;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002855
Jason Wang41f2f122014-12-24 11:03:52 +08002856 dev->features |= NETIF_F_GSO_ROBUST;
2857
Michał Mirosław98e778c2011-03-31 01:01:35 +00002858 if (gso)
David S. Millere078de02017-07-03 06:37:32 -07002859 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002860 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002861 }
Thomas Huth4f491292013-08-27 17:09:02 +02002862 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2863 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002864
Jason Wang4fda8302013-04-10 23:32:21 +00002865 dev->vlan_features = dev->features;
2866
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002867 /* MTU range: 68 - 65535 */
2868 dev->min_mtu = MIN_MTU;
2869 dev->max_mtu = MAX_MTU;
2870
Rusty Russell296f96f2007-10-22 11:03:37 +10002871 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302872 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2873 virtio_cread_bytes(vdev,
2874 offsetof(struct virtio_net_config, mac),
2875 dev->dev_addr, dev->addr_len);
2876 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002877 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002878
2879 /* Set up our device-specific information */
2880 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002881 vi->dev = dev;
2882 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002883 vdev->priv = vi;
John Stultz827da442013-10-07 15:51:58 -07002884
Jason Wang586d17c2012-04-11 20:43:52 +00002885 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002886
Herbert Xu97402b92008-04-18 11:24:27 +08002887 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002888 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2889 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002890 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2891 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002892 vi->big_packets = true;
2893
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002894 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2895 vi->mergeable_rx_bufs = true;
2896
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002897 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2898 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002899 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2900 else
2901 vi->hdr_len = sizeof(struct virtio_net_hdr);
2902
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002903 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2904 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302905 vi->any_header_sg = true;
2906
Jason Wang986a4f42012-12-07 07:04:56 +00002907 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2908 vi->has_cvq = true;
2909
Aaron Conole14de9d12016-06-03 16:57:12 -04002910 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2911 mtu = virtio_cread16(vdev,
2912 offsetof(struct virtio_net_config,
2913 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002914 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002915 /* Should never trigger: MTU was previously validated
2916 * in virtnet_validate.
2917 */
2918 dev_err(&vdev->dev, "device MTU appears to have changed "
2919 "it is now %d < %d", mtu, dev->min_mtu);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002920 goto free;
Aaron Conole93a205e2016-10-25 16:12:12 -04002921 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002922
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002923 dev->mtu = mtu;
2924 dev->max_mtu = mtu;
2925
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002926 /* TODO: size buffers correctly in this case. */
2927 if (dev->mtu > ETH_DATA_LEN)
2928 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04002929 }
2930
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002931 if (vi->any_header_sg)
2932 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002933
Jason Wang44900012016-11-25 12:37:26 +08002934 /* Enable multiqueue by default */
2935 if (num_online_cpus() >= max_queue_pairs)
2936 vi->curr_queue_pairs = max_queue_pairs;
2937 else
2938 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002939 vi->max_queue_pairs = max_queue_pairs;
2940
2941 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302942 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002943 if (err)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002944 goto free;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002945
Michael Daltonfbf28d72014-01-16 22:23:30 -08002946#ifdef CONFIG_SYSFS
2947 if (vi->mergeable_rx_bufs)
2948 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2949#endif
Zhi Yong Wu0f13b66b2013-11-18 21:19:27 +08002950 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2951 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002952
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002953 virtnet_init_settings(dev);
2954
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002955 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
2956 vi->failover = net_failover_create(vi->dev);
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00002957 if (IS_ERR(vi->failover)) {
2958 err = PTR_ERR(vi->failover);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002959 goto free_vqs;
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00002960 }
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002961 }
2962
Rusty Russell296f96f2007-10-22 11:03:37 +10002963 err = register_netdev(dev);
2964 if (err) {
2965 pr_debug("virtio_net: registering device failed\n");
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002966 goto free_failover;
Rusty Russell296f96f2007-10-22 11:03:37 +10002967 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002968
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302969 virtio_device_ready(vdev);
2970
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002971 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002972 if (err) {
2973 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002974 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002975 }
2976
Jason Wanga2208712016-12-13 14:23:05 +08002977 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08002978
Jason Wang167c25e2010-11-10 14:45:41 +00002979 /* Assume link up if device can't report link status,
2980 otherwise get link status from config. */
Jay Vosburghbda7fab2018-03-22 14:42:41 +00002981 netif_carrier_off(dev);
Jason Wang167c25e2010-11-10 14:45:41 +00002982 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002983 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002984 } else {
2985 vi->status = VIRTIO_NET_S_LINK_UP;
Jason Baronfaa9b392018-01-05 17:44:54 -05002986 virtnet_update_settings(vi);
Jason Wang167c25e2010-11-10 14:45:41 +00002987 netif_carrier_on(dev);
2988 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002989
Jason Wang3f935222017-07-19 16:54:49 +08002990 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
2991 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
2992 set_bit(guest_offloads[i], &vi->guest_offloads);
2993
Jason Wang986a4f42012-12-07 07:04:56 +00002994 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2995 dev->name, max_queue_pairs);
2996
Rusty Russell296f96f2007-10-22 11:03:37 +10002997 return 0;
2998
wangyunjianf00e35e2016-05-31 11:52:43 +08002999free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10303000 vi->vdev->config->reset(vdev);
3001
Rusty Russellb3369c12008-02-04 23:50:02 -05003002 unregister_netdev(dev);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003003free_failover:
3004 net_failover_destroy(vi->failover);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003005free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00003006 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08003007 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00003008 virtnet_del_vqs(vi);
Rusty Russell296f96f2007-10-22 11:03:37 +10003009free:
3010 free_netdev(dev);
3011 return err;
3012}
3013
Amit Shah04486ed2011-12-22 16:58:32 +05303014static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10003015{
Amit Shah04486ed2011-12-22 16:58:32 +05303016 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00003017
3018 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00003019 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003020
Jason Wang986a4f42012-12-07 07:04:56 +00003021 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003022
Michael Daltonfb518792014-01-16 22:23:26 -08003023 free_receive_page_frags(vi);
3024
Jason Wang986a4f42012-12-07 07:04:56 +00003025 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05303026}
3027
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003028static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05303029{
3030 struct virtnet_info *vi = vdev->priv;
3031
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003032 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003033
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10303034 /* Make sure no work handler is accessing the device. */
3035 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00003036
Amit Shah04486ed2011-12-22 16:58:32 +05303037 unregister_netdev(vi->dev);
3038
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003039 net_failover_destroy(vi->failover);
3040
Amit Shah04486ed2011-12-22 16:58:32 +05303041 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003042
Rusty Russell74b25532007-11-19 11:20:42 -05003043 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003044}
3045
Arnd Bergmann67a75192017-07-25 17:35:50 +02003046static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303047{
3048 struct virtnet_info *vi = vdev->priv;
3049
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003050 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003051 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303052 remove_vq_common(vi);
3053
3054 return 0;
3055}
3056
Arnd Bergmann67a75192017-07-25 17:35:50 +02003057static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303058{
3059 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003060 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05303061
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003062 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303063 if (err)
3064 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00003065 virtnet_set_queues(vi, vi->curr_queue_pairs);
3066
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003067 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08003068 if (err)
3069 return err;
3070
Amit Shah0741bcb2011-12-22 16:58:33 +05303071 return 0;
3072}
Amit Shah0741bcb2011-12-22 16:58:33 +05303073
Rusty Russell296f96f2007-10-22 11:03:37 +10003074static struct virtio_device_id id_table[] = {
3075 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3076 { 0 },
3077};
3078
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003079#define VIRTNET_FEATURES \
3080 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3081 VIRTIO_NET_F_MAC, \
3082 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3083 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3084 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3085 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3086 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3087 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3088 VIRTIO_NET_F_CTRL_MAC_ADDR, \
Jason Baronfaa9b392018-01-05 17:44:54 -05003089 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
Sridhar Samudrala98050692018-05-24 09:55:16 -07003090 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003091
Rusty Russellc45a6812008-05-02 21:50:50 -05003092static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003093 VIRTNET_FEATURES,
3094};
3095
3096static unsigned int features_legacy[] = {
3097 VIRTNET_FEATURES,
3098 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09303099 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05003100};
3101
Uwe Kleine-König22402522009-11-05 01:32:44 -08003102static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05003103 .feature_table = features,
3104 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003105 .feature_table_legacy = features_legacy,
3106 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10003107 .driver.name = KBUILD_MODNAME,
3108 .driver.owner = THIS_MODULE,
3109 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003110 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10003111 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003112 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003113 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09303114#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05303115 .freeze = virtnet_freeze,
3116 .restore = virtnet_restore,
3117#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10003118};
3119
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003120static __init int virtio_net_driver_init(void)
3121{
3122 int ret;
3123
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003124 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003125 virtnet_cpu_online,
3126 virtnet_cpu_down_prep);
3127 if (ret < 0)
3128 goto out;
3129 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003130 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003131 NULL, virtnet_cpu_dead);
3132 if (ret)
3133 goto err_dead;
3134
3135 ret = register_virtio_driver(&virtio_net_driver);
3136 if (ret)
3137 goto err_virtio;
3138 return 0;
3139err_virtio:
3140 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3141err_dead:
3142 cpuhp_remove_multi_state(virtionet_online);
3143out:
3144 return ret;
3145}
3146module_init(virtio_net_driver_init);
3147
3148static __exit void virtio_net_driver_exit(void)
3149{
Andrew Jonescfa0ebc2017-07-24 15:38:32 +02003150 unregister_virtio_driver(&virtio_net_driver);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003151 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3152 cpuhp_remove_multi_state(virtionet_online);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003153}
3154module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10003155
3156MODULE_DEVICE_TABLE(virtio, id_table);
3157MODULE_DESCRIPTION("Virtio network driver");
3158MODULE_LICENSE("GPL");