blob: 356d18481ee478b0a6caf3a77659faeebfc3790c [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>
Rusty Russell296f96f2007-10-22 11:03:37 +100032
Amerigo Wangd34710e2013-05-09 19:50:51 +000033static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020034module_param(napi_weight, int, 0444);
35
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040036static bool csum = true, gso = true, napi_tx;
Rusty Russell34a48572008-02-04 23:50:02 -050037module_param(csum, bool, 0444);
38module_param(gso, bool, 0444);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040039module_param(napi_tx, bool, 0644);
Rusty Russell34a48572008-02-04 23:50:02 -050040
Rusty Russell296f96f2007-10-22 11:03:37 +100041/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080042#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080043#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100044
Jason Wangf6b10202017-02-21 16:46:28 +080045#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
46
John Fastabend2de2f7f2017-02-02 19:16:29 -080047/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
48#define VIRTIO_XDP_HEADROOM 256
49
Johannes Berg5377d7582015-08-19 09:48:40 +020050/* RX packet size EWMA. The average packet size is used to determine the packet
51 * buffer size when refilling RX rings. As the entire RX ring may be refilled
52 * at once, the weight is chosen so that the EWMA will be insensitive to short-
53 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080054 */
Johannes Bergeb1e0112017-02-15 09:49:26 +010055DECLARE_EWMA(pkt_len, 0, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080056
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020057/* With mergeable buffers we align buffer address and use the low bits to
58 * encode its true size. Buffer size is up to 1 page so we need to align to
59 * square root of page size to ensure we reserve enough bits to encode the true
60 * size.
61 */
62#define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
63
Michael Daltonab7db912014-01-16 22:23:27 -080064/* Minimum alignment for mergeable packet buffers. */
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020065#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
66 1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
Michael Daltonab7db912014-01-16 22:23:27 -080067
Rick Jones66846042011-11-14 14:17:08 +000068#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000069
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000070struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000071 struct u64_stats_sync tx_syncp;
72 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000073 u64 tx_bytes;
74 u64 tx_packets;
75
76 u64 rx_bytes;
77 u64 rx_packets;
78};
79
Jason Wange9d74172012-12-07 07:04:55 +000080/* Internal representation of a send virtqueue */
81struct send_queue {
82 /* Virtqueue associated with this send _queue */
83 struct virtqueue *vq;
84
85 /* TX: fragments + linear part + virtio header */
86 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000087
88 /* Name of the send queue: output.$index */
89 char name[40];
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040090
91 struct napi_struct napi;
Jason Wange9d74172012-12-07 07:04:55 +000092};
93
94/* Internal representation of a receive virtqueue */
95struct receive_queue {
96 /* Virtqueue associated with this receive_queue */
97 struct virtqueue *vq;
98
Rusty Russell296f96f2007-10-22 11:03:37 +100099 struct napi_struct napi;
100
John Fastabendf600b692016-12-15 12:13:24 -0800101 struct bpf_prog __rcu *xdp_prog;
102
Jason Wange9d74172012-12-07 07:04:55 +0000103 /* Chain pages by the private ptr. */
104 struct page *pages;
105
Michael Daltonab7db912014-01-16 22:23:27 -0800106 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200107 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800108
Michael Daltonfb518792014-01-16 22:23:26 -0800109 /* Page frag for packet buffer allocation. */
110 struct page_frag alloc_frag;
111
Jason Wange9d74172012-12-07 07:04:55 +0000112 /* RX: fragments + linear part + virtio header */
113 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000114
115 /* Name of this receive queue: input.$index */
116 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +0000117};
118
119struct virtnet_info {
120 struct virtio_device *vdev;
121 struct virtqueue *cvq;
122 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000123 struct send_queue *sq;
124 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000125 unsigned int status;
126
Jason Wang986a4f42012-12-07 07:04:56 +0000127 /* Max # of queue pairs supported by the device */
128 u16 max_queue_pairs;
129
130 /* # of queue pairs currently used by the driver */
131 u16 curr_queue_pairs;
132
John Fastabend672aafd2016-12-15 12:13:49 -0800133 /* # of XDP queue pairs currently used by the driver */
134 u16 xdp_queue_pairs;
135
Herbert Xu97402b92008-04-18 11:24:27 +0800136 /* I like... big packets and I cannot lie! */
137 bool big_packets;
138
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800139 /* Host will merge rx buffers for big packets (shake it! shake it!) */
140 bool mergeable_rx_bufs;
141
Jason Wang986a4f42012-12-07 07:04:56 +0000142 /* Has control virtqueue */
143 bool has_cvq;
144
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930145 /* Host can handle any s/g split between our header and packet data */
146 bool any_header_sg;
147
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300148 /* Packet virtio header size */
149 u8 hdr_len;
150
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000151 /* Active statistics */
152 struct virtnet_stats __percpu *stats;
153
Rusty Russell3161e452009-08-26 12:22:32 -0700154 /* Work struct for refilling if we run low on memory. */
155 struct delayed_work refill;
156
Jason Wang586d17c2012-04-11 20:43:52 +0000157 /* Work struct for config space updates */
158 struct work_struct config_work;
159
Jason Wang986a4f42012-12-07 07:04:56 +0000160 /* Does the affinity hint is set for virtqueues? */
161 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000162
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200163 /* CPU hotplug instances for online & dead */
164 struct hlist_node node;
165 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200166
167 /* Control VQ buffers: protected by the rtnl lock */
168 struct virtio_net_ctrl_hdr ctrl_hdr;
169 virtio_net_ctrl_ack ctrl_status;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700170 struct virtio_net_ctrl_mq ctrl_mq;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200171 u8 ctrl_promisc;
172 u8 ctrl_allmulti;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700173 u16 ctrl_vid;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100174
175 /* Ethtool settings */
176 u8 duplex;
177 u32 speed;
Rusty Russell296f96f2007-10-22 11:03:37 +1000178};
179
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000180struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300181 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000182 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300183 * hdr is in a separate sg buffer, and data sg buffer shares same page
184 * with this header sg. This padding makes next sg 16 byte aligned
185 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000186 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300187 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000188};
189
Jason Wang986a4f42012-12-07 07:04:56 +0000190/* Converting between virtqueue no. and kernel tx/rx queue no.
191 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
192 */
193static int vq2txq(struct virtqueue *vq)
194{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000195 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000196}
197
198static int txq2vq(int txq)
199{
200 return txq * 2 + 1;
201}
202
203static int vq2rxq(struct virtqueue *vq)
204{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000205 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000206}
207
208static int rxq2vq(int rxq)
209{
210 return rxq * 2;
211}
212
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300213static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000214{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300215 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000216}
217
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000218/*
219 * private is used to chain pages for big packets, put the whole
220 * most recent used list in the beginning for reuse
221 */
Jason Wange9d74172012-12-07 07:04:55 +0000222static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500223{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000224 struct page *end;
225
Jason Wange9d74172012-12-07 07:04:55 +0000226 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000227 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000228 end->private = (unsigned long)rq->pages;
229 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500230}
231
Jason Wange9d74172012-12-07 07:04:55 +0000232static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500233{
Jason Wange9d74172012-12-07 07:04:55 +0000234 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500235
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000236 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000237 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000238 /* clear private here, it is used to chain pages */
239 p->private = 0;
240 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500241 p = alloc_page(gfp_mask);
242 return p;
243}
244
Willem de Bruijne4e84522017-04-24 13:49:26 -0400245static void virtqueue_napi_schedule(struct napi_struct *napi,
246 struct virtqueue *vq)
247{
248 if (napi_schedule_prep(napi)) {
249 virtqueue_disable_cb(vq);
250 __napi_schedule(napi);
251 }
252}
253
254static void virtqueue_napi_complete(struct napi_struct *napi,
255 struct virtqueue *vq, int processed)
256{
257 int opaque;
258
259 opaque = virtqueue_enable_cb_prepare(vq);
260 if (napi_complete_done(napi, processed) &&
261 unlikely(virtqueue_poll(vq, opaque)))
262 virtqueue_napi_schedule(napi, vq);
263}
264
Jason Wange9d74172012-12-07 07:04:55 +0000265static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000266{
Jason Wange9d74172012-12-07 07:04:55 +0000267 struct virtnet_info *vi = vq->vdev->priv;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400268 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000269
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500270 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000271 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000272
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400273 if (napi->weight)
274 virtqueue_napi_schedule(napi, vq);
275 else
276 /* We were probably waiting for more output buffers. */
277 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000278}
279
Michael Daltonab7db912014-01-16 22:23:27 -0800280static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
281{
282 unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
283 return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
284}
285
286static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
287{
288 return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
289
290}
291
292static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
293{
294 unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
295 return (unsigned long)buf | (size - 1);
296}
297
Mike Waychison34646452012-01-04 12:52:32 +0000298/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300299static struct sk_buff *page_to_skb(struct virtnet_info *vi,
300 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700301 struct page *page, unsigned int offset,
302 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000303{
304 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300305 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700306 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000307 char *p;
308
Michael Dalton2613af02013-10-28 15:44:18 -0700309 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000310
311 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100312 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000313 if (unlikely(!skb))
314 return NULL;
315
316 hdr = skb_vnet_hdr(skb);
317
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300318 hdr_len = vi->hdr_len;
319 if (vi->mergeable_rx_bufs)
320 hdr_padded_len = sizeof *hdr;
321 else
Michael Dalton2613af02013-10-28 15:44:18 -0700322 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000323
324 memcpy(hdr, p, hdr_len);
325
326 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700327 offset += hdr_padded_len;
328 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000329
330 copy = len;
331 if (copy > skb_tailroom(skb))
332 copy = skb_tailroom(skb);
333 memcpy(skb_put(skb, copy), p, copy);
334
335 len -= copy;
336 offset += copy;
337
Michael Dalton2613af02013-10-28 15:44:18 -0700338 if (vi->mergeable_rx_bufs) {
339 if (len)
340 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
341 else
342 put_page(page);
343 return skb;
344 }
345
Sasha Levine878d782011-09-28 04:40:54 +0000346 /*
347 * Verify that we can indeed put this data into a skb.
348 * This is here to handle cases when the device erroneously
349 * tries to receive more than is possible. This is usually
350 * the case of a broken device.
351 */
352 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000353 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000354 dev_kfree_skb(skb);
355 return NULL;
356 }
Michael Dalton2613af02013-10-28 15:44:18 -0700357 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000358 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700359 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
360 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
361 frag_size, truesize);
362 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000363 page = (struct page *)page->private;
364 offset = 0;
365 }
366
367 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000368 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000369
370 return skb;
371}
372
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100373static bool virtnet_xdp_xmit(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800374 struct receive_queue *rq,
Jason Wangf6b10202017-02-21 16:46:28 +0800375 struct xdp_buff *xdp)
John Fastabend56434a02016-12-15 12:14:13 -0800376{
John Fastabend56434a02016-12-15 12:14:13 -0800377 struct virtio_net_hdr_mrg_rxbuf *hdr;
Jason Wangf6b10202017-02-21 16:46:28 +0800378 unsigned int len;
John Fastabend722d8282017-02-02 19:15:32 -0800379 struct send_queue *sq;
380 unsigned int qp;
John Fastabend56434a02016-12-15 12:14:13 -0800381 void *xdp_sent;
382 int err;
383
John Fastabend722d8282017-02-02 19:15:32 -0800384 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
385 sq = &vi->sq[qp];
386
John Fastabend56434a02016-12-15 12:14:13 -0800387 /* Free up any pending old buffers before queueing new ones. */
388 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Jason Wangf6b10202017-02-21 16:46:28 +0800389 struct page *sent_page = virt_to_head_page(xdp_sent);
Jason Wangbb91acc2016-12-23 22:37:32 +0800390
Jason Wangf6b10202017-02-21 16:46:28 +0800391 put_page(sent_page);
John Fastabend56434a02016-12-15 12:14:13 -0800392 }
393
Jason Wangf6b10202017-02-21 16:46:28 +0800394 xdp->data -= vi->hdr_len;
395 /* Zero header and leave csum up to XDP layers */
396 hdr = xdp->data;
397 memset(hdr, 0, vi->hdr_len);
John Fastabend56434a02016-12-15 12:14:13 -0800398
Jason Wangf6b10202017-02-21 16:46:28 +0800399 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
Jason Wangbb91acc2016-12-23 22:37:32 +0800400
Jason Wangf6b10202017-02-21 16:46:28 +0800401 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC);
John Fastabend56434a02016-12-15 12:14:13 -0800402 if (unlikely(err)) {
Jason Wangf6b10202017-02-21 16:46:28 +0800403 struct page *page = virt_to_head_page(xdp->data);
Jason Wangbb91acc2016-12-23 22:37:32 +0800404
Jason Wangf6b10202017-02-21 16:46:28 +0800405 put_page(page);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100406 return false;
John Fastabend56434a02016-12-15 12:14:13 -0800407 }
408
409 virtqueue_kick(sq->vq);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100410 return true;
John Fastabend56434a02016-12-15 12:14:13 -0800411}
412
Jason Wangf6b10202017-02-21 16:46:28 +0800413static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
414{
415 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
416}
417
Jason Wangbb91acc2016-12-23 22:37:32 +0800418static struct sk_buff *receive_small(struct net_device *dev,
419 struct virtnet_info *vi,
420 struct receive_queue *rq,
421 void *buf, unsigned int len)
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200422{
Jason Wangf6b10202017-02-21 16:46:28 +0800423 struct sk_buff *skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800424 struct bpf_prog *xdp_prog;
Jason Wangf6b10202017-02-21 16:46:28 +0800425 unsigned int xdp_headroom = virtnet_get_headroom(vi);
426 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
427 unsigned int headroom = vi->hdr_len + header_offset;
428 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
429 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
430 unsigned int delta = 0;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300431 len -= vi->hdr_len;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200432
Jason Wangbb91acc2016-12-23 22:37:32 +0800433 rcu_read_lock();
434 xdp_prog = rcu_dereference(rq->xdp_prog);
435 if (xdp_prog) {
Jason Wangf6b10202017-02-21 16:46:28 +0800436 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
John Fastabend0354e4d2017-02-02 19:15:01 -0800437 struct xdp_buff xdp;
Jason Wangf6b10202017-02-21 16:46:28 +0800438 void *orig_data;
Jason Wangbb91acc2016-12-23 22:37:32 +0800439 u32 act;
440
441 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
442 goto err_xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800443
Jason Wangf6b10202017-02-21 16:46:28 +0800444 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
445 xdp.data = xdp.data_hard_start + xdp_headroom;
John Fastabend0354e4d2017-02-02 19:15:01 -0800446 xdp.data_end = xdp.data + len;
Jason Wangf6b10202017-02-21 16:46:28 +0800447 orig_data = xdp.data;
John Fastabend0354e4d2017-02-02 19:15:01 -0800448 act = bpf_prog_run_xdp(xdp_prog, &xdp);
449
Jason Wangbb91acc2016-12-23 22:37:32 +0800450 switch (act) {
451 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800452 /* Recalculate length in case bpf program changed it */
Jason Wangf6b10202017-02-21 16:46:28 +0800453 delta = orig_data - xdp.data;
Jason Wangbb91acc2016-12-23 22:37:32 +0800454 break;
455 case XDP_TX:
Jason Wangf6b10202017-02-21 16:46:28 +0800456 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
John Fastabend0354e4d2017-02-02 19:15:01 -0800457 trace_xdp_exception(vi->dev, xdp_prog, act);
Jason Wangbb91acc2016-12-23 22:37:32 +0800458 rcu_read_unlock();
459 goto xdp_xmit;
Jason Wangbb91acc2016-12-23 22:37:32 +0800460 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800461 bpf_warn_invalid_xdp_action(act);
462 case XDP_ABORTED:
463 trace_xdp_exception(vi->dev, xdp_prog, act);
464 case XDP_DROP:
Jason Wangbb91acc2016-12-23 22:37:32 +0800465 goto err_xdp;
466 }
467 }
468 rcu_read_unlock();
469
Jason Wangf6b10202017-02-21 16:46:28 +0800470 skb = build_skb(buf, buflen);
471 if (!skb) {
472 put_page(virt_to_head_page(buf));
473 goto err;
474 }
475 skb_reserve(skb, headroom - delta);
476 skb_put(skb, len + delta);
477 if (!delta) {
478 buf += header_offset;
479 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
480 } /* keep zeroed vnet hdr since packet was changed by bpf */
481
482err:
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200483 return skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800484
485err_xdp:
486 rcu_read_unlock();
487 dev->stats.rx_dropped++;
Jason Wangf6b10202017-02-21 16:46:28 +0800488 put_page(virt_to_head_page(buf));
Jason Wangbb91acc2016-12-23 22:37:32 +0800489xdp_xmit:
490 return NULL;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200491}
492
493static struct sk_buff *receive_big(struct net_device *dev,
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300494 struct virtnet_info *vi,
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200495 struct receive_queue *rq,
496 void *buf,
497 unsigned int len)
498{
499 struct page *page = buf;
Jason Wangc47a43d2016-12-23 22:37:31 +0800500 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200501
502 if (unlikely(!skb))
503 goto err;
504
505 return skb;
506
507err:
508 dev->stats.rx_dropped++;
509 give_pages(rq, page);
510 return NULL;
511}
512
John Fastabend72979a62016-12-15 12:14:36 -0800513/* The conditions to enable XDP should preclude the underlying device from
514 * sending packets across multiple buffers (num_buf > 1). However per spec
515 * it does not appear to be illegal to do so but rather just against convention.
516 * So in order to avoid making a system unresponsive the packets are pushed
517 * into a page and the XDP program is run. This will be extremely slow and we
518 * push a warning to the user to fix this as soon as possible. Fixing this may
519 * require resolving the underlying hardware to determine why multiple buffers
520 * are being received or simply loading the XDP program in the ingress stack
521 * after the skb is built because there is no advantage to running it here
522 * anymore.
523 */
524static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800525 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800526 struct page *p,
527 int offset,
528 unsigned int *len)
529{
530 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800531 unsigned int page_off = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800532
533 if (!page)
534 return NULL;
535
536 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
537 page_off += *len;
538
Jason Wang56a86f82016-12-23 22:37:26 +0800539 while (--*num_buf) {
John Fastabend72979a62016-12-15 12:14:36 -0800540 unsigned int buflen;
541 unsigned long ctx;
542 void *buf;
543 int off;
544
545 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
546 if (unlikely(!ctx))
547 goto err_buf;
548
John Fastabend72979a62016-12-15 12:14:36 -0800549 buf = mergeable_ctx_to_buf_address(ctx);
550 p = virt_to_head_page(buf);
551 off = buf - page_address(p);
552
Jason Wang56a86f82016-12-23 22:37:26 +0800553 /* guard against a misconfigured or uncooperative backend that
554 * is sending packet larger than the MTU.
555 */
556 if ((page_off + buflen) > PAGE_SIZE) {
557 put_page(p);
558 goto err_buf;
559 }
560
John Fastabend72979a62016-12-15 12:14:36 -0800561 memcpy(page_address(page) + page_off,
562 page_address(p) + off, buflen);
563 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800564 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800565 }
566
John Fastabend2de2f7f2017-02-02 19:16:29 -0800567 /* Headroom does not contribute to packet length */
568 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800569 return page;
570err_buf:
571 __free_pages(page, 0);
572 return NULL;
573}
574
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200575static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200576 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200577 struct receive_queue *rq,
Michael Daltonab7db912014-01-16 22:23:27 -0800578 unsigned long ctx,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200579 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000580{
Michael Daltonab7db912014-01-16 22:23:27 -0800581 void *buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300582 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
583 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200584 struct page *page = virt_to_head_page(buf);
585 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800586 struct sk_buff *head_skb, *curr_skb;
587 struct bpf_prog *xdp_prog;
588 unsigned int truesize;
Michael Daltonab7db912014-01-16 22:23:27 -0800589
John Fastabend56434a02016-12-15 12:14:13 -0800590 head_skb = NULL;
591
John Fastabendf600b692016-12-15 12:13:24 -0800592 rcu_read_lock();
593 xdp_prog = rcu_dereference(rq->xdp_prog);
594 if (xdp_prog) {
John Fastabend72979a62016-12-15 12:14:36 -0800595 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800596 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800597 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800598 u32 act;
599
Jason Wang73b62bd2016-12-23 22:37:24 +0800600 /* This happens when rx buffer size is underestimated */
John Fastabendf600b692016-12-15 12:13:24 -0800601 if (unlikely(num_buf > 1)) {
John Fastabend72979a62016-12-15 12:14:36 -0800602 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800603 xdp_page = xdp_linearize_page(rq, &num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800604 page, offset, &len);
605 if (!xdp_page)
606 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800607 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800608 } else {
609 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800610 }
611
612 /* Transient failure which in theory could occur if
613 * in-flight packets from before XDP was enabled reach
614 * the receive path after XDP is loaded. In practice I
615 * was not able to create this condition.
616 */
Jason Wangb00f70b2016-12-23 22:37:28 +0800617 if (unlikely(hdr->hdr.gso_type))
John Fastabendf600b692016-12-15 12:13:24 -0800618 goto err_xdp;
619
John Fastabend2de2f7f2017-02-02 19:16:29 -0800620 /* Allow consuming headroom but reserve enough space to push
621 * the descriptor on if we get an XDP_TX return code.
622 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800623 data = page_address(xdp_page) + offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800624 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
John Fastabend0354e4d2017-02-02 19:15:01 -0800625 xdp.data = data + vi->hdr_len;
626 xdp.data_end = xdp.data + (len - vi->hdr_len);
627 act = bpf_prog_run_xdp(xdp_prog, &xdp);
628
John Fastabend56434a02016-12-15 12:14:13 -0800629 switch (act) {
630 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800631 /* recalculate offset to account for any header
632 * adjustments. Note other cases do not build an
633 * skb and avoid using offset
634 */
635 offset = xdp.data -
636 page_address(xdp_page) - vi->hdr_len;
637
Jason Wang1830f892016-12-23 22:37:27 +0800638 /* We can only create skb based on xdp_page. */
639 if (unlikely(xdp_page != page)) {
640 rcu_read_unlock();
641 put_page(page);
642 head_skb = page_to_skb(vi, rq, xdp_page,
John Fastabend2de2f7f2017-02-02 19:16:29 -0800643 offset, len, PAGE_SIZE);
Jason Wang5c334742016-12-23 22:37:29 +0800644 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
Jason Wang1830f892016-12-23 22:37:27 +0800645 return head_skb;
646 }
John Fastabend56434a02016-12-15 12:14:13 -0800647 break;
648 case XDP_TX:
Jason Wangf6b10202017-02-21 16:46:28 +0800649 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
John Fastabend0354e4d2017-02-02 19:15:01 -0800650 trace_xdp_exception(vi->dev, xdp_prog, act);
Jason Wang5c334742016-12-23 22:37:29 +0800651 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabend72979a62016-12-15 12:14:36 -0800652 if (unlikely(xdp_page != page))
653 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800654 rcu_read_unlock();
655 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -0800656 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800657 bpf_warn_invalid_xdp_action(act);
658 case XDP_ABORTED:
659 trace_xdp_exception(vi->dev, xdp_prog, act);
660 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -0800661 if (unlikely(xdp_page != page))
662 __free_pages(xdp_page, 0);
Jason Wang5c334742016-12-23 22:37:29 +0800663 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabendf600b692016-12-15 12:13:24 -0800664 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800665 }
John Fastabendf600b692016-12-15 12:13:24 -0800666 }
667 rcu_read_unlock();
668
669 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
670 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
671 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000672
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200673 if (unlikely(!curr_skb))
674 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000675 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200676 int num_skb_frags;
677
Michael Daltonab7db912014-01-16 22:23:27 -0800678 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
679 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200680 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200681 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300682 virtio16_to_cpu(vi->vdev,
683 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200684 dev->stats.rx_length_errors++;
685 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000686 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200687
Michael Daltonab7db912014-01-16 22:23:27 -0800688 buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200689 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200690
691 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700692 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
693 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200694
695 if (unlikely(!nskb))
696 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700697 if (curr_skb == head_skb)
698 skb_shinfo(curr_skb)->frag_list = nskb;
699 else
700 curr_skb->next = nskb;
701 curr_skb = nskb;
702 head_skb->truesize += nskb->truesize;
703 num_skb_frags = 0;
704 }
Michael Daltonab7db912014-01-16 22:23:27 -0800705 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
Michael Dalton2613af02013-10-28 15:44:18 -0700706 if (curr_skb != head_skb) {
707 head_skb->data_len += len;
708 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800709 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700710 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200711 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800712 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
713 put_page(page);
714 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800715 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800716 } else {
717 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800718 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800719 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200720 }
721
Johannes Berg5377d7582015-08-19 09:48:40 +0200722 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200723 return head_skb;
724
John Fastabendf600b692016-12-15 12:13:24 -0800725err_xdp:
726 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200727err_skb:
728 put_page(page);
729 while (--num_buf) {
Michael Daltonab7db912014-01-16 22:23:27 -0800730 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
731 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200732 pr_debug("%s: rx error: %d buffers missing\n",
733 dev->name, num_buf);
734 dev->stats.rx_length_errors++;
735 break;
736 }
Michael Daltonab7db912014-01-16 22:23:27 -0800737 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200738 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000739 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200740err_buf:
741 dev->stats.rx_dropped++;
742 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800743xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200744 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000745}
746
Jason Wang61845d22017-02-17 11:33:09 +0800747static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
748 void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000749{
Jason Wange9d74172012-12-07 07:04:55 +0000750 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000751 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300752 struct virtio_net_hdr_mrg_rxbuf *hdr;
Jason Wang61845d22017-02-17 11:33:09 +0800753 int ret;
Rusty Russell296f96f2007-10-22 11:03:37 +1000754
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300755 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000756 pr_debug("%s: short packet %i\n", dev->name, len);
757 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800758 if (vi->mergeable_rx_bufs) {
759 unsigned long ctx = (unsigned long)buf;
760 void *base = mergeable_ctx_to_buf_address(ctx);
761 put_page(virt_to_head_page(base));
762 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800763 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800764 } else {
Jason Wangf6b10202017-02-21 16:46:28 +0800765 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -0800766 }
Jason Wang61845d22017-02-17 11:33:09 +0800767 return 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000768 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000769
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200770 if (vi->mergeable_rx_bufs)
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200771 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200772 else if (vi->big_packets)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300773 skb = receive_big(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200774 else
Jason Wangbb91acc2016-12-23 22:37:32 +0800775 skb = receive_small(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200776
777 if (unlikely(!skb))
Jason Wang61845d22017-02-17 11:33:09 +0800778 return 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800779
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000780 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000781
Jason Wang61845d22017-02-17 11:33:09 +0800782 ret = skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000783
Mike Rapoporte858fae2016-06-08 16:09:21 +0300784 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +0000785 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000786
Mike Rapoporte858fae2016-06-08 16:09:21 +0300787 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
788 virtio_is_little_endian(vi->vdev))) {
789 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
790 dev->name, hdr->hdr.gso_type,
791 hdr->hdr.gso_size);
792 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000793 }
794
Mike Rapoportd1dc06d2016-06-14 08:29:38 +0300795 skb->protocol = eth_type_trans(skb, dev);
796 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
797 ntohs(skb->protocol), skb->len, skb->pkt_type);
798
Eric Dumazet0fbd0502015-07-31 18:25:17 +0200799 napi_gro_receive(&rq->napi, skb);
Jason Wang61845d22017-02-17 11:33:09 +0800800 return ret;
Rusty Russell296f96f2007-10-22 11:03:37 +1000801
802frame_err:
803 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000804 dev_kfree_skb(skb);
Jason Wang61845d22017-02-17 11:33:09 +0800805 return 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000806}
807
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300808static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
809 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000810{
Jason Wangf6b10202017-02-21 16:46:28 +0800811 struct page_frag *alloc_frag = &rq->alloc_frag;
812 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800813 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wangf6b10202017-02-21 16:46:28 +0800814 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000815 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000816
Jason Wangf6b10202017-02-21 16:46:28 +0800817 len = SKB_DATA_ALIGN(len) +
818 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
819 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000820 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800821
Jason Wangf6b10202017-02-21 16:46:28 +0800822 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
823 get_page(alloc_frag->page);
824 alloc_frag->offset += len;
825 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
826 vi->hdr_len + GOOD_PACKET_LEN);
827 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000828 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +0800829 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000830
831 return err;
832}
833
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300834static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
835 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000836{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000837 struct page *first, *list = NULL;
838 char *p;
839 int i, err, offset;
840
Rusty Russella5835442014-09-11 10:17:36 +0930841 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
842
Jason Wange9d74172012-12-07 07:04:55 +0000843 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000844 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000845 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000846 if (!first) {
847 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000848 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000849 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700850 }
Jason Wange9d74172012-12-07 07:04:55 +0000851 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000852
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000853 /* chain new page in list head to match sg */
854 first->private = (unsigned long)list;
855 list = first;
856 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800857
Jason Wange9d74172012-12-07 07:04:55 +0000858 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000859 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000860 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000861 return -ENOMEM;
862 }
863 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800864
Jason Wange9d74172012-12-07 07:04:55 +0000865 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300866 /* a separated rq->sg[0] for header - required in case !any_header_sg */
867 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +0800868
Jason Wange9d74172012-12-07 07:04:55 +0000869 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000870 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000871 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800872
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000873 /* chain first in list head */
874 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030875 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
876 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000877 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000878 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800879
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000880 return err;
881}
Herbert Xu97402b92008-04-18 11:24:27 +0800882
Johannes Berg5377d7582015-08-19 09:48:40 +0200883static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000884{
Michael Daltonab7db912014-01-16 22:23:27 -0800885 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800886 unsigned int len;
887
Johannes Berg5377d7582015-08-19 09:48:40 +0200888 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael Daltonfbf28d72014-01-16 22:23:30 -0800889 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
890 return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
891}
892
John Fastabend2de2f7f2017-02-02 19:16:29 -0800893static int add_recvbuf_mergeable(struct virtnet_info *vi,
894 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -0800895{
Michael Daltonfb518792014-01-16 22:23:26 -0800896 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800897 unsigned int headroom = virtnet_get_headroom(vi);
Michael Daltonfb518792014-01-16 22:23:26 -0800898 char *buf;
Michael Daltonab7db912014-01-16 22:23:27 -0800899 unsigned long ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000900 int err;
Michael Daltonfb518792014-01-16 22:23:26 -0800901 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +1000902
Michael Daltonfbf28d72014-01-16 22:23:30 -0800903 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800904 if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000905 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -0800906
Michael Daltonfb518792014-01-16 22:23:26 -0800907 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800908 buf += headroom; /* advance address leaving hole at front of pkt */
Michael Daltonab7db912014-01-16 22:23:27 -0800909 ctx = mergeable_buf_to_ctx(buf, len);
Michael Daltonfb518792014-01-16 22:23:26 -0800910 get_page(alloc_frag->page);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800911 alloc_frag->offset += len + headroom;
Michael Daltonfb518792014-01-16 22:23:26 -0800912 hole = alloc_frag->size - alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800913 if (hole < len + headroom) {
Michael Daltonab7db912014-01-16 22:23:27 -0800914 /* To avoid internal fragmentation, if there is very likely not
915 * enough space for another buffer, add the remaining space to
916 * the current buffer. This extra space is not included in
917 * the truesize stored in ctx.
918 */
Michael Daltonfb518792014-01-16 22:23:26 -0800919 len += hole;
920 alloc_frag->offset += hole;
921 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000922
Michael Daltonfb518792014-01-16 22:23:26 -0800923 sg_init_one(rq->sg, buf, len);
Michael Daltonab7db912014-01-16 22:23:27 -0800924 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000925 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700926 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000927
928 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000929}
930
Rusty Russellb2baed62011-12-29 00:42:38 +0000931/*
932 * Returns false if we couldn't fill entirely (OOM).
933 *
934 * Normally run in the receive path, but can also be run from ndo_open
935 * before we're receiving packets, or from refill_work which is
936 * careful to disable receiving (using napi_disable).
937 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300938static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
939 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800940{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800941 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000942 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800943
Michael Daltonfb518792014-01-16 22:23:26 -0800944 gfp |= __GFP_COLD;
Amit Shah0aea51c2009-08-26 14:58:28 +0530945 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000946 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -0800947 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000948 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300949 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000950 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300951 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800952
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000953 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030954 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800955 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800956 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +0800957 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700958 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800959}
960
Rusty Russell18445c42008-02-04 23:49:57 -0500961static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000962{
963 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000964 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000965
Willem de Bruijne4e84522017-04-24 13:49:26 -0400966 virtqueue_napi_schedule(&rq->napi, rvq);
Rusty Russell296f96f2007-10-22 11:03:37 +1000967}
968
Willem de Bruijne4e84522017-04-24 13:49:26 -0400969static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800970{
Willem de Bruijne4e84522017-04-24 13:49:26 -0400971 napi_enable(napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800972
973 /* If all buffers were filled by other side before we napi_enabled, we
Willem de Bruijne4e84522017-04-24 13:49:26 -0400974 * won't get another interrupt, so process any outstanding packets now.
975 * Call local_bh_enable after to trigger softIRQ processing.
976 */
977 local_bh_disable();
978 virtqueue_napi_schedule(napi, vq);
979 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800980}
981
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400982static void virtnet_napi_tx_enable(struct virtnet_info *vi,
983 struct virtqueue *vq,
984 struct napi_struct *napi)
985{
986 if (!napi->weight)
987 return;
988
989 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
990 * enable the feature if this is likely affine with the transmit path.
991 */
992 if (!vi->affinity_hint_set) {
993 napi->weight = 0;
994 return;
995 }
996
997 return virtnet_napi_enable(vq, napi);
998}
999
Rusty Russell3161e452009-08-26 12:22:32 -07001000static void refill_work(struct work_struct *work)
1001{
Jason Wange9d74172012-12-07 07:04:55 +00001002 struct virtnet_info *vi =
1003 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -07001004 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +00001005 int i;
Rusty Russell3161e452009-08-26 12:22:32 -07001006
Sasha Levin55257d72013-04-29 12:00:08 +09301007 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001008 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -07001009
Jason Wang986a4f42012-12-07 07:04:56 +00001010 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001011 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001012 virtnet_napi_enable(rq->vq, &rq->napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001013
1014 /* In theory, this can happen: if we don't get any buffers in
1015 * we will *never* try to fill again.
1016 */
1017 if (still_empty)
1018 schedule_delayed_work(&vi->refill, HZ/2);
1019 }
Rusty Russell3161e452009-08-26 12:22:32 -07001020}
1021
Jason Wang2ffa7592014-07-23 16:33:54 +08001022static int virtnet_receive(struct receive_queue *rq, int budget)
Rusty Russell296f96f2007-10-22 11:03:37 +10001023{
Jason Wange9d74172012-12-07 07:04:55 +00001024 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wang61845d22017-02-17 11:33:09 +08001025 unsigned int len, received = 0, bytes = 0;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001026 void *buf;
Jason Wang61845d22017-02-17 11:33:09 +08001027 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001028
Rusty Russell296f96f2007-10-22 11:03:37 +10001029 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +00001030 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Jason Wang61845d22017-02-17 11:33:09 +08001031 bytes += receive_buf(vi, rq, buf, len);
Rusty Russell296f96f2007-10-22 11:03:37 +10001032 received++;
1033 }
1034
Jason Wangbe121f42014-01-16 14:45:24 +08001035 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001036 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001037 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -07001038 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001039
Jason Wang61845d22017-02-17 11:33:09 +08001040 u64_stats_update_begin(&stats->rx_syncp);
1041 stats->rx_bytes += bytes;
1042 stats->rx_packets += received;
1043 u64_stats_update_end(&stats->rx_syncp);
1044
Jason Wang2ffa7592014-07-23 16:33:54 +08001045 return received;
1046}
1047
1048static int virtnet_poll(struct napi_struct *napi, int budget)
1049{
1050 struct receive_queue *rq =
1051 container_of(napi, struct receive_queue, napi);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001052 unsigned int received;
Jason Wang2ffa7592014-07-23 16:33:54 +08001053
Li RongQingfaadb052015-03-26 15:39:45 +08001054 received = virtnet_receive(rq, budget);
Jason Wang2ffa7592014-07-23 16:33:54 +08001055
Rusty Russell8329d982007-11-19 11:20:43 -05001056 /* Out of packets? */
Willem de Bruijne4e84522017-04-24 13:49:26 -04001057 if (received < budget)
1058 virtqueue_napi_complete(napi, rq->vq, received);
Rusty Russell296f96f2007-10-22 11:03:37 +10001059
1060 return received;
1061}
1062
Jason Wang986a4f42012-12-07 07:04:56 +00001063static int virtnet_open(struct net_device *dev)
1064{
1065 struct virtnet_info *vi = netdev_priv(dev);
1066 int i;
1067
Jason Wange4166622013-05-21 20:03:58 +00001068 for (i = 0; i < vi->max_queue_pairs; i++) {
1069 if (i < vi->curr_queue_pairs)
1070 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001071 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001072 schedule_delayed_work(&vi->refill, 0);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001073 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001074 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001075 }
1076
1077 return 0;
1078}
1079
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001080static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001081{
1082 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +10301083 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +00001084 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +00001085 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Jason Wang61845d22017-02-17 11:33:09 +08001086 unsigned int packets = 0;
1087 unsigned int bytes = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +10001088
Jason Wange9d74172012-12-07 07:04:55 +00001089 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001090 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001091
Jason Wang61845d22017-02-17 11:33:09 +08001092 bytes += skb->len;
1093 packets++;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001094
Eric Dumazeted79bab2009-10-14 14:36:43 +00001095 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001096 }
Jason Wang61845d22017-02-17 11:33:09 +08001097
1098 /* Avoid overhead when no packets have been processed
1099 * happens when called speculatively from start_xmit.
1100 */
1101 if (!packets)
1102 return;
1103
1104 u64_stats_update_begin(&stats->tx_syncp);
1105 stats->tx_bytes += bytes;
1106 stats->tx_packets += packets;
1107 u64_stats_update_end(&stats->tx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +10001108}
1109
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001110static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1111{
1112 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1113 struct virtnet_info *vi = sq->vq->vdev->priv;
1114 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
1115
1116 __netif_tx_lock(txq, raw_smp_processor_id());
1117 free_old_xmit_skbs(sq);
1118 __netif_tx_unlock(txq);
1119
1120 virtqueue_napi_complete(napi, sq->vq, 0);
1121
1122 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1123 netif_tx_wake_queue(txq);
1124
1125 return 0;
1126}
1127
Jason Wange9d74172012-12-07 07:04:55 +00001128static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001129{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001130 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001131 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001132 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +10301133 unsigned num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001134 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301135 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001136
Johannes Berge1749612008-10-27 15:59:26 -07001137 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301138
1139 can_push = vi->any_header_sg &&
1140 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1141 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1142 /* Even if we can, don't push here yet as this would skew
1143 * csum_start offset below. */
1144 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001145 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301146 else
1147 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001148
Mike Rapoporte858fae2016-06-08 16:09:21 +03001149 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Jason Wang6391a442017-01-20 14:32:42 +08001150 virtio_is_little_endian(vi->vdev), false))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001151 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001152
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001153 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001154 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001155
Jason Wang547c8902015-08-27 14:53:06 +08001156 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301157 if (can_push) {
1158 __skb_push(skb, hdr_len);
1159 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1160 /* Pull header back to avoid skew in tx bytes calculations. */
1161 __skb_pull(skb, hdr_len);
1162 } else {
1163 sg_set_buf(sq->sg, hdr, hdr_len);
1164 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1165 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301166 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001167}
1168
Stephen Hemminger424efe92009-08-31 19:50:51 +00001169static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001170{
1171 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001172 int qnum = skb_get_queue_mapping(skb);
1173 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301174 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001175 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1176 bool kick = !skb->xmit_more;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001177 bool use_napi = sq->napi.weight;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001178
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001179 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001180 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001181
Jacob Keller074c3582014-06-25 02:37:13 +00001182 /* timestamp packet in software */
1183 skb_tx_timestamp(skb);
1184
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001185 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001186 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001187
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301188 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001189 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301190 dev->stats.tx_fifo_errors++;
1191 if (net_ratelimit())
1192 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001193 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001194 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001195 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001196 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001197 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001198
Rusty Russell48925e32009-09-24 09:59:20 -06001199 /* Don't wait up for transmitted skbs to be freed. */
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001200 if (!use_napi) {
1201 skb_orphan(skb);
1202 nf_reset(skb);
1203 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001204
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001205 /* If running out of space, stop queue to avoid getting packets that we
1206 * are then unable to transmit.
1207 * An alternative would be to force queuing layer to requeue the skb by
1208 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1209 * returned in a normal path of operation: it means that driver is not
1210 * maintaining the TX queue stop/start state properly, and causes
1211 * the stack to do a non-trivial amount of useless work.
1212 * Since most packets only take 1 or 2 ring slots, stopping the queue
1213 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001214 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001215 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001216 netif_stop_subqueue(dev, qnum);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001217 if (!use_napi &&
1218 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001219 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001220 free_old_xmit_skbs(sq);
1221 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001222 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001223 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001224 }
1225 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001226 }
Rusty Russell48925e32009-09-24 09:59:20 -06001227
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001228 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001229 virtqueue_kick(sq->vq);
1230
Rusty Russell48925e32009-09-24 09:59:20 -06001231 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001232}
1233
Amos Kong40cbfc32013-01-21 01:17:21 +00001234/*
1235 * Send command via the control virtqueue and check status. Commands
1236 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001237 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001238 */
1239static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001240 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001241{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301242 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001243 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001244
1245 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301246 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001247
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001248 vi->ctrl_status = ~0;
1249 vi->ctrl_hdr.class = class;
1250 vi->ctrl_hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301251 /* Add header */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001252 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301253 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001254
Rusty Russellf7bc9592013-03-20 15:44:28 +10301255 if (out)
1256 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001257
Rusty Russellf7bc9592013-03-20 15:44:28 +10301258 /* Add return status. */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001259 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001260 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001261
stephen hemmingerd24bae32013-12-09 16:17:40 -08001262 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301263 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001264
Heinz Graalfs67975902013-10-29 09:40:02 +10301265 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001266 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001267
1268 /* Spin for a response, the kick causes an ioport write, trapping
1269 * into the hypervisor, so the request should be handled immediately.
1270 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301271 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1272 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001273 cpu_relax();
1274
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001275 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001276}
1277
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001278static int virtnet_set_mac_address(struct net_device *dev, void *p)
1279{
1280 struct virtnet_info *vi = netdev_priv(dev);
1281 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001282 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001283 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001284 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001285
Shyam Saini801822d2016-12-24 00:44:58 +05301286 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001287 if (!addr)
1288 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001289
1290 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001291 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001292 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001293
Amos Kong7e58d5a2013-01-21 01:17:23 +00001294 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1295 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1296 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001297 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001298 dev_warn(&vdev->dev,
1299 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001300 ret = -EINVAL;
1301 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001302 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001303 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1304 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301305 unsigned int i;
1306
1307 /* Naturally, this has an atomicity problem. */
1308 for (i = 0; i < dev->addr_len; i++)
1309 virtio_cwrite8(vdev,
1310 offsetof(struct virtio_net_config, mac) +
1311 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001312 }
1313
1314 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001315 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001316
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001317out:
1318 kfree(addr);
1319 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001320}
1321
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001322static void virtnet_stats(struct net_device *dev,
1323 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001324{
1325 struct virtnet_info *vi = netdev_priv(dev);
1326 int cpu;
1327 unsigned int start;
1328
1329 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +00001330 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001331 u64 tpackets, tbytes, rpackets, rbytes;
1332
1333 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001334 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001335 tpackets = stats->tx_packets;
1336 tbytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001337 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001338
1339 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001340 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001341 rpackets = stats->rx_packets;
1342 rbytes = stats->rx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001343 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001344
1345 tot->rx_packets += rpackets;
1346 tot->tx_packets += tpackets;
1347 tot->rx_bytes += rbytes;
1348 tot->tx_bytes += tbytes;
1349 }
1350
1351 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001352 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001353 tot->rx_dropped = dev->stats.rx_dropped;
1354 tot->rx_length_errors = dev->stats.rx_length_errors;
1355 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001356}
1357
Amit Shahda74e892008-02-29 16:24:50 +05301358#ifdef CONFIG_NET_POLL_CONTROLLER
1359static void virtnet_netpoll(struct net_device *dev)
1360{
1361 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001362 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301363
Jason Wang986a4f42012-12-07 07:04:56 +00001364 for (i = 0; i < vi->curr_queue_pairs; i++)
1365 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301366}
1367#endif
1368
Jason Wang586d17c2012-04-11 20:43:52 +00001369static void virtnet_ack_link_announce(struct virtnet_info *vi)
1370{
1371 rtnl_lock();
1372 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001373 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001374 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1375 rtnl_unlock();
1376}
1377
John Fastabend473153292017-02-02 19:14:32 -08001378static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001379{
1380 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001381 struct net_device *dev = vi->dev;
1382
1383 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1384 return 0;
1385
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001386 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1387 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001388
1389 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001390 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001391 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1392 queue_pairs);
1393 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301394 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001395 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001396 /* virtnet_open() will refill when device is going to up. */
1397 if (dev->flags & IFF_UP)
1398 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301399 }
Jason Wang986a4f42012-12-07 07:04:56 +00001400
1401 return 0;
1402}
1403
John Fastabend473153292017-02-02 19:14:32 -08001404static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1405{
1406 int err;
1407
1408 rtnl_lock();
1409 err = _virtnet_set_queues(vi, queue_pairs);
1410 rtnl_unlock();
1411 return err;
1412}
1413
Rusty Russell296f96f2007-10-22 11:03:37 +10001414static int virtnet_close(struct net_device *dev)
1415{
1416 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001417 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001418
Rusty Russellb2baed62011-12-29 00:42:38 +00001419 /* Make sure refill_work doesn't re-enable napi! */
1420 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001421
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001422 for (i = 0; i < vi->max_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001423 napi_disable(&vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001424 napi_disable(&vi->sq[i].napi);
1425 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001426
Rusty Russell296f96f2007-10-22 11:03:37 +10001427 return 0;
1428}
1429
Alex Williamson2af76982009-02-04 09:02:40 +00001430static void virtnet_set_rx_mode(struct net_device *dev)
1431{
1432 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001433 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001434 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001435 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001436 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001437 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001438 void *buf;
1439 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001440
stephen hemminger788a8b62013-12-09 16:18:45 -08001441 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001442 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1443 return;
1444
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001445 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1446 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001447
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001448 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001449
1450 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001451 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001452 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001453 vi->ctrl_promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001454
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001455 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001456
1457 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001458 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001459 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001460 vi->ctrl_allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001461
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001462 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001463 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001464 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001465 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1466 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1467 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001468 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001469 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001470
Alex Williamson23e258e2009-05-01 17:27:56 +00001471 sg_init_table(sg, 2);
1472
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001473 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001474 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001475 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001476 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001477 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001478
1479 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001480 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001481
1482 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001483 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001484
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001485 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001486 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001487 netdev_for_each_mc_addr(ha, dev)
1488 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001489
1490 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001491 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001492
1493 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001494 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001495 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001496
1497 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001498}
1499
Patrick McHardy80d5c362013-04-19 02:04:28 +00001500static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1501 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001502{
1503 struct virtnet_info *vi = netdev_priv(dev);
1504 struct scatterlist sg;
1505
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001506 vi->ctrl_vid = vid;
1507 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001508
1509 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001510 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001511 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001512 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001513}
1514
Patrick McHardy80d5c362013-04-19 02:04:28 +00001515static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1516 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001517{
1518 struct virtnet_info *vi = netdev_priv(dev);
1519 struct scatterlist sg;
1520
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001521 vi->ctrl_vid = vid;
1522 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001523
1524 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001525 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001526 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001527 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001528}
1529
Wanlong Gao8898c212013-01-24 23:51:30 +00001530static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001531{
1532 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001533
1534 if (vi->affinity_hint_set) {
1535 for (i = 0; i < vi->max_queue_pairs; i++) {
1536 virtqueue_set_affinity(vi->rq[i].vq, -1);
1537 virtqueue_set_affinity(vi->sq[i].vq, -1);
1538 }
1539
1540 vi->affinity_hint_set = false;
1541 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001542}
1543
1544static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001545{
1546 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001547 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001548
1549 /* In multiqueue mode, when the number of cpu is equal to the number of
1550 * queue pairs, we let the queue pairs to be private to one cpu by
1551 * setting the affinity hint to eliminate the contention.
1552 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001553 if (vi->curr_queue_pairs == 1 ||
1554 vi->max_queue_pairs != num_online_cpus()) {
1555 virtnet_clean_affinity(vi, -1);
1556 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001557 }
1558
Wanlong Gao8898c212013-01-24 23:51:30 +00001559 i = 0;
1560 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001561 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1562 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001563 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001564 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001565 }
1566
Wanlong Gao8898c212013-01-24 23:51:30 +00001567 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001568}
1569
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001570static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001571{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001572 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1573 node);
1574 virtnet_set_affinity(vi);
1575 return 0;
1576}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001577
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001578static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1579{
1580 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1581 node_dead);
1582 virtnet_set_affinity(vi);
1583 return 0;
1584}
Jason Wang3ab098d2013-10-15 11:18:58 +08001585
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001586static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1587{
1588 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1589 node);
1590
1591 virtnet_clean_affinity(vi, cpu);
1592 return 0;
1593}
1594
1595static enum cpuhp_state virtionet_online;
1596
1597static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1598{
1599 int ret;
1600
1601 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1602 if (ret)
1603 return ret;
1604 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1605 &vi->node_dead);
1606 if (!ret)
1607 return ret;
1608 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1609 return ret;
1610}
1611
1612static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1613{
1614 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1615 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1616 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001617}
1618
Rick Jones8f9f4662011-10-19 08:10:59 +00001619static void virtnet_get_ringparam(struct net_device *dev,
1620 struct ethtool_ringparam *ring)
1621{
1622 struct virtnet_info *vi = netdev_priv(dev);
1623
Jason Wang986a4f42012-12-07 07:04:56 +00001624 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1625 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001626 ring->rx_pending = ring->rx_max_pending;
1627 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001628}
1629
Rick Jones66846042011-11-14 14:17:08 +00001630
1631static void virtnet_get_drvinfo(struct net_device *dev,
1632 struct ethtool_drvinfo *info)
1633{
1634 struct virtnet_info *vi = netdev_priv(dev);
1635 struct virtio_device *vdev = vi->vdev;
1636
1637 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1638 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1639 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1640
1641}
1642
Jason Wangd73bcd22012-12-07 07:04:57 +00001643/* TODO: Eliminate OOO packets during switching */
1644static int virtnet_set_channels(struct net_device *dev,
1645 struct ethtool_channels *channels)
1646{
1647 struct virtnet_info *vi = netdev_priv(dev);
1648 u16 queue_pairs = channels->combined_count;
1649 int err;
1650
1651 /* We don't support separate rx/tx channels.
1652 * We don't allow setting 'other' channels.
1653 */
1654 if (channels->rx_count || channels->tx_count || channels->other_count)
1655 return -EINVAL;
1656
Amos Kongc18e9cd2014-04-18 13:45:41 +08001657 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001658 return -EINVAL;
1659
John Fastabendf600b692016-12-15 12:13:24 -08001660 /* For now we don't support modifying channels while XDP is loaded
1661 * also when XDP is loaded all RX queues have XDP programs so we only
1662 * need to check a single RX queue.
1663 */
1664 if (vi->rq[0].xdp_prog)
1665 return -EINVAL;
1666
Wanlong Gao47be2472013-01-24 23:51:29 +00001667 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08001668 err = _virtnet_set_queues(vi, queue_pairs);
Jason Wangd73bcd22012-12-07 07:04:57 +00001669 if (!err) {
1670 netif_set_real_num_tx_queues(dev, queue_pairs);
1671 netif_set_real_num_rx_queues(dev, queue_pairs);
1672
Wanlong Gao8898c212013-01-24 23:51:30 +00001673 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001674 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001675 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001676
1677 return err;
1678}
1679
1680static void virtnet_get_channels(struct net_device *dev,
1681 struct ethtool_channels *channels)
1682{
1683 struct virtnet_info *vi = netdev_priv(dev);
1684
1685 channels->combined_count = vi->curr_queue_pairs;
1686 channels->max_combined = vi->max_queue_pairs;
1687 channels->max_other = 0;
1688 channels->rx_count = 0;
1689 channels->tx_count = 0;
1690 channels->other_count = 0;
1691}
1692
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001693/* Check if the user is trying to change anything besides speed/duplex */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001694static bool
1695virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001696{
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001697 struct ethtool_link_ksettings diff1 = *cmd;
1698 struct ethtool_link_ksettings diff2 = {};
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001699
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001700 /* cmd is always set so we need to clear it, validate the port type
1701 * and also without autonegotiation we can ignore advertising
1702 */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001703 diff1.base.speed = 0;
1704 diff2.base.port = PORT_OTHER;
1705 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
1706 diff1.base.duplex = 0;
1707 diff1.base.cmd = 0;
1708 diff1.base.link_mode_masks_nwords = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001709
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001710 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
1711 bitmap_empty(diff1.link_modes.supported,
1712 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
1713 bitmap_empty(diff1.link_modes.advertising,
1714 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
1715 bitmap_empty(diff1.link_modes.lp_advertising,
1716 __ETHTOOL_LINK_MODE_MASK_NBITS);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001717}
1718
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001719static int virtnet_set_link_ksettings(struct net_device *dev,
1720 const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001721{
1722 struct virtnet_info *vi = netdev_priv(dev);
1723 u32 speed;
1724
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001725 speed = cmd->base.speed;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001726 /* don't allow custom speed and duplex */
1727 if (!ethtool_validate_speed(speed) ||
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001728 !ethtool_validate_duplex(cmd->base.duplex) ||
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001729 !virtnet_validate_ethtool_cmd(cmd))
1730 return -EINVAL;
1731 vi->speed = speed;
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001732 vi->duplex = cmd->base.duplex;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001733
1734 return 0;
1735}
1736
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001737static int virtnet_get_link_ksettings(struct net_device *dev,
1738 struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001739{
1740 struct virtnet_info *vi = netdev_priv(dev);
1741
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001742 cmd->base.speed = vi->speed;
1743 cmd->base.duplex = vi->duplex;
1744 cmd->base.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001745
1746 return 0;
1747}
1748
1749static void virtnet_init_settings(struct net_device *dev)
1750{
1751 struct virtnet_info *vi = netdev_priv(dev);
1752
1753 vi->speed = SPEED_UNKNOWN;
1754 vi->duplex = DUPLEX_UNKNOWN;
1755}
1756
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001757static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001758 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001759 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001760 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001761 .set_channels = virtnet_set_channels,
1762 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00001763 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001764 .get_link_ksettings = virtnet_get_link_ksettings,
1765 .set_link_ksettings = virtnet_set_link_ksettings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001766};
1767
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001768static void virtnet_freeze_down(struct virtio_device *vdev)
1769{
1770 struct virtnet_info *vi = vdev->priv;
1771 int i;
1772
1773 /* Make sure no work handler is accessing the device */
1774 flush_work(&vi->config_work);
1775
1776 netif_device_detach(vi->dev);
1777 cancel_delayed_work_sync(&vi->refill);
1778
1779 if (netif_running(vi->dev)) {
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001780 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001781 napi_disable(&vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001782 napi_disable(&vi->sq[i].napi);
1783 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001784 }
1785}
1786
1787static int init_vqs(struct virtnet_info *vi);
John Fastabend2de2f7f2017-02-02 19:16:29 -08001788static void _remove_vq_common(struct virtnet_info *vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001789
1790static int virtnet_restore_up(struct virtio_device *vdev)
1791{
1792 struct virtnet_info *vi = vdev->priv;
1793 int err, i;
1794
1795 err = init_vqs(vi);
1796 if (err)
1797 return err;
1798
1799 virtio_device_ready(vdev);
1800
1801 if (netif_running(vi->dev)) {
1802 for (i = 0; i < vi->curr_queue_pairs; i++)
1803 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1804 schedule_delayed_work(&vi->refill, 0);
1805
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001806 for (i = 0; i < vi->max_queue_pairs; i++) {
Willem de Bruijne4e84522017-04-24 13:49:26 -04001807 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001808 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
1809 &vi->sq[i].napi);
1810 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001811 }
1812
1813 netif_device_attach(vi->dev);
1814 return err;
1815}
1816
Jason Wang017b29c2017-02-20 11:50:20 +08001817static int virtnet_reset(struct virtnet_info *vi, int curr_qp, int xdp_qp)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001818{
1819 struct virtio_device *dev = vi->vdev;
1820 int ret;
1821
1822 virtio_config_disable(dev);
1823 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
1824 virtnet_freeze_down(dev);
1825 _remove_vq_common(vi);
1826
1827 dev->config->reset(dev);
1828 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
1829 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
1830
1831 ret = virtio_finalize_features(dev);
1832 if (ret)
1833 goto err;
1834
Jason Wang017b29c2017-02-20 11:50:20 +08001835 vi->xdp_queue_pairs = xdp_qp;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001836 ret = virtnet_restore_up(dev);
1837 if (ret)
1838 goto err;
Jason Wang017b29c2017-02-20 11:50:20 +08001839 ret = _virtnet_set_queues(vi, curr_qp);
John Fastabend2de2f7f2017-02-02 19:16:29 -08001840 if (ret)
1841 goto err;
1842
1843 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
1844 virtio_config_enable(dev);
1845 return 0;
1846err:
1847 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
1848 return ret;
1849}
1850
John Fastabendf600b692016-12-15 12:13:24 -08001851static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1852{
1853 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1854 struct virtnet_info *vi = netdev_priv(dev);
1855 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08001856 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08001857 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001858
1859 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001860 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1861 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1862 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001863 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1864 return -EOPNOTSUPP;
1865 }
1866
1867 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1868 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1869 return -EINVAL;
1870 }
1871
1872 if (dev->mtu > max_sz) {
1873 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1874 return -EINVAL;
1875 }
1876
John Fastabend672aafd2016-12-15 12:13:49 -08001877 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1878 if (prog)
1879 xdp_qp = nr_cpu_ids;
1880
1881 /* XDP requires extra queues for XDP_TX */
1882 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1883 netdev_warn(dev, "request %i queues but max is %i\n",
1884 curr_qp + xdp_qp, vi->max_queue_pairs);
1885 return -ENOMEM;
1886 }
1887
John Fastabend2de2f7f2017-02-02 19:16:29 -08001888 if (prog) {
1889 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
1890 if (IS_ERR(prog))
1891 return PTR_ERR(prog);
1892 }
1893
John Fastabend2de2f7f2017-02-02 19:16:29 -08001894 /* Changing the headroom in buffers is a disruptive operation because
1895 * existing buffers must be flushed and reallocated. This will happen
1896 * when a xdp program is initially added or xdp is disabled by removing
1897 * the xdp program resulting in number of XDP queues changing.
1898 */
1899 if (vi->xdp_queue_pairs != xdp_qp) {
Jason Wang017b29c2017-02-20 11:50:20 +08001900 err = virtnet_reset(vi, curr_qp + xdp_qp, xdp_qp);
1901 if (err) {
1902 dev_warn(&dev->dev, "XDP reset failure.\n");
John Fastabend2de2f7f2017-02-02 19:16:29 -08001903 goto virtio_reset_err;
Jason Wang017b29c2017-02-20 11:50:20 +08001904 }
John Fastabendf600b692016-12-15 12:13:24 -08001905 }
1906
John Fastabend672aafd2016-12-15 12:13:49 -08001907 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1908
John Fastabendf600b692016-12-15 12:13:24 -08001909 for (i = 0; i < vi->max_queue_pairs; i++) {
1910 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1911 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1912 if (old_prog)
1913 bpf_prog_put(old_prog);
1914 }
1915
1916 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001917
1918virtio_reset_err:
1919 /* On reset error do our best to unwind XDP changes inflight and return
1920 * error up to user space for resolution. The underlying reset hung on
1921 * us so not much we can do here.
1922 */
John Fastabend2de2f7f2017-02-02 19:16:29 -08001923 if (prog)
1924 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
1925 return err;
John Fastabendf600b692016-12-15 12:13:24 -08001926}
1927
1928static bool virtnet_xdp_query(struct net_device *dev)
1929{
1930 struct virtnet_info *vi = netdev_priv(dev);
1931 int i;
1932
1933 for (i = 0; i < vi->max_queue_pairs; i++) {
1934 if (vi->rq[i].xdp_prog)
1935 return true;
1936 }
1937 return false;
1938}
1939
1940static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1941{
1942 switch (xdp->command) {
1943 case XDP_SETUP_PROG:
1944 return virtnet_xdp_set(dev, xdp->prog);
1945 case XDP_QUERY_PROG:
1946 xdp->prog_attached = virtnet_xdp_query(dev);
1947 return 0;
1948 default:
1949 return -EINVAL;
1950 }
1951}
1952
Stephen Hemminger76288b42009-01-06 10:44:22 -08001953static const struct net_device_ops virtnet_netdev = {
1954 .ndo_open = virtnet_open,
1955 .ndo_stop = virtnet_close,
1956 .ndo_start_xmit = start_xmit,
1957 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001958 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001959 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001960 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001961 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1962 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001963#ifdef CONFIG_NET_POLL_CONTROLLER
1964 .ndo_poll_controller = virtnet_netpoll,
1965#endif
John Fastabendf600b692016-12-15 12:13:24 -08001966 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001967};
1968
Jason Wang586d17c2012-04-11 20:43:52 +00001969static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001970{
Jason Wang586d17c2012-04-11 20:43:52 +00001971 struct virtnet_info *vi =
1972 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001973 u16 v;
1974
Rusty Russell855e0c52013-10-14 18:11:51 +10301975 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1976 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301977 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001978
1979 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001980 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001981 virtnet_ack_link_announce(vi);
1982 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001983
1984 /* Ignore unknown (future) status bits */
1985 v &= VIRTIO_NET_S_LINK_UP;
1986
1987 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301988 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001989
1990 vi->status = v;
1991
1992 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1993 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001994 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001995 } else {
1996 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001997 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001998 }
1999}
2000
2001static void virtnet_config_changed(struct virtio_device *vdev)
2002{
2003 struct virtnet_info *vi = vdev->priv;
2004
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002005 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002006}
2007
Jason Wang986a4f42012-12-07 07:04:56 +00002008static void virtnet_free_queues(struct virtnet_info *vi)
2009{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002010 int i;
2011
Jason Wangab3971b2015-03-12 13:57:44 +08002012 for (i = 0; i < vi->max_queue_pairs; i++) {
2013 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002014 netif_napi_del(&vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002015 netif_napi_del(&vi->sq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08002016 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002017
Eric Dumazet963abe52016-11-15 22:24:12 -08002018 /* We called napi_hash_del() before netif_napi_del(),
2019 * we need to respect an RCU grace period before freeing vi->rq
2020 */
2021 synchronize_net();
2022
Jason Wang986a4f42012-12-07 07:04:56 +00002023 kfree(vi->rq);
2024 kfree(vi->sq);
2025}
2026
John Fastabend473153292017-02-02 19:14:32 -08002027static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002028{
John Fastabendf600b692016-12-15 12:13:24 -08002029 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00002030 int i;
2031
2032 for (i = 0; i < vi->max_queue_pairs; i++) {
2033 while (vi->rq[i].pages)
2034 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08002035
2036 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2037 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2038 if (old_prog)
2039 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00002040 }
John Fastabend473153292017-02-02 19:14:32 -08002041}
2042
2043static void free_receive_bufs(struct virtnet_info *vi)
2044{
2045 rtnl_lock();
2046 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08002047 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002048}
2049
Michael Daltonfb518792014-01-16 22:23:26 -08002050static void free_receive_page_frags(struct virtnet_info *vi)
2051{
2052 int i;
2053 for (i = 0; i < vi->max_queue_pairs; i++)
2054 if (vi->rq[i].alloc_frag.page)
2055 put_page(vi->rq[i].alloc_frag.page);
2056}
2057
John Fastabendb68df012017-01-25 18:22:48 -08002058static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08002059{
2060 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
2061 return false;
2062 else if (q < vi->curr_queue_pairs)
2063 return true;
2064 else
2065 return false;
2066}
2067
Jason Wang986a4f42012-12-07 07:04:56 +00002068static void free_unused_bufs(struct virtnet_info *vi)
2069{
2070 void *buf;
2071 int i;
2072
2073 for (i = 0; i < vi->max_queue_pairs; i++) {
2074 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002075 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08002076 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08002077 dev_kfree_skb(buf);
2078 else
2079 put_page(virt_to_head_page(buf));
2080 }
Jason Wang986a4f42012-12-07 07:04:56 +00002081 }
2082
2083 for (i = 0; i < vi->max_queue_pairs; i++) {
2084 struct virtqueue *vq = vi->rq[i].vq;
2085
2086 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002087 if (vi->mergeable_rx_bufs) {
2088 unsigned long ctx = (unsigned long)buf;
2089 void *base = mergeable_ctx_to_buf_address(ctx);
2090 put_page(virt_to_head_page(base));
2091 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002092 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002093 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002094 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002095 }
Jason Wang986a4f42012-12-07 07:04:56 +00002096 }
Jason Wang986a4f42012-12-07 07:04:56 +00002097 }
2098}
2099
Jason Wange9d74172012-12-07 07:04:55 +00002100static void virtnet_del_vqs(struct virtnet_info *vi)
2101{
2102 struct virtio_device *vdev = vi->vdev;
2103
Wanlong Gao8898c212013-01-24 23:51:30 +00002104 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00002105
Jason Wange9d74172012-12-07 07:04:55 +00002106 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002107
2108 virtnet_free_queues(vi);
2109}
2110
2111static int virtnet_find_vqs(struct virtnet_info *vi)
2112{
2113 vq_callback_t **callbacks;
2114 struct virtqueue **vqs;
2115 int ret = -ENOMEM;
2116 int i, total_vqs;
2117 const char **names;
2118
2119 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2120 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2121 * possible control vq.
2122 */
2123 total_vqs = vi->max_queue_pairs * 2 +
2124 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2125
2126 /* Allocate space for find_vqs parameters */
2127 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
2128 if (!vqs)
2129 goto err_vq;
2130 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
2131 if (!callbacks)
2132 goto err_callback;
2133 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
2134 if (!names)
2135 goto err_names;
2136
2137 /* Parameters for control virtqueue, if any */
2138 if (vi->has_cvq) {
2139 callbacks[total_vqs - 1] = NULL;
2140 names[total_vqs - 1] = "control";
2141 }
2142
2143 /* Allocate/initialize parameters for send/receive virtqueues */
2144 for (i = 0; i < vi->max_queue_pairs; i++) {
2145 callbacks[rxq2vq(i)] = skb_recv_done;
2146 callbacks[txq2vq(i)] = skb_xmit_done;
2147 sprintf(vi->rq[i].name, "input.%d", i);
2148 sprintf(vi->sq[i].name, "output.%d", i);
2149 names[rxq2vq(i)] = vi->rq[i].name;
2150 names[txq2vq(i)] = vi->sq[i].name;
2151 }
2152
2153 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +01002154 names, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002155 if (ret)
2156 goto err_find;
2157
2158 if (vi->has_cvq) {
2159 vi->cvq = vqs[total_vqs - 1];
2160 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002161 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002162 }
2163
2164 for (i = 0; i < vi->max_queue_pairs; i++) {
2165 vi->rq[i].vq = vqs[rxq2vq(i)];
2166 vi->sq[i].vq = vqs[txq2vq(i)];
2167 }
2168
2169 kfree(names);
2170 kfree(callbacks);
2171 kfree(vqs);
2172
2173 return 0;
2174
2175err_find:
2176 kfree(names);
2177err_names:
2178 kfree(callbacks);
2179err_callback:
2180 kfree(vqs);
2181err_vq:
2182 return ret;
2183}
2184
2185static int virtnet_alloc_queues(struct virtnet_info *vi)
2186{
2187 int i;
2188
2189 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2190 if (!vi->sq)
2191 goto err_sq;
2192 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002193 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002194 goto err_rq;
2195
2196 INIT_DELAYED_WORK(&vi->refill, refill_work);
2197 for (i = 0; i < vi->max_queue_pairs; i++) {
2198 vi->rq[i].pages = NULL;
2199 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2200 napi_weight);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002201 netif_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2202 napi_tx ? napi_weight : 0);
Jason Wang986a4f42012-12-07 07:04:56 +00002203
2204 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002205 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002206 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2207 }
2208
2209 return 0;
2210
2211err_rq:
2212 kfree(vi->sq);
2213err_sq:
2214 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002215}
2216
Amit Shah3f9c10b2011-12-22 16:58:31 +05302217static int init_vqs(struct virtnet_info *vi)
2218{
Jason Wang986a4f42012-12-07 07:04:56 +00002219 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302220
Jason Wang986a4f42012-12-07 07:04:56 +00002221 /* Allocate send & receive queues */
2222 ret = virtnet_alloc_queues(vi);
2223 if (ret)
2224 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302225
Jason Wang986a4f42012-12-07 07:04:56 +00002226 ret = virtnet_find_vqs(vi);
2227 if (ret)
2228 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302229
Wanlong Gao47be2472013-01-24 23:51:29 +00002230 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002231 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002232 put_online_cpus();
2233
Amit Shah3f9c10b2011-12-22 16:58:31 +05302234 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002235
2236err_free:
2237 virtnet_free_queues(vi);
2238err:
2239 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302240}
2241
Michael Daltonfbf28d72014-01-16 22:23:30 -08002242#ifdef CONFIG_SYSFS
2243static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2244 struct rx_queue_attribute *attribute, char *buf)
2245{
2246 struct virtnet_info *vi = netdev_priv(queue->dev);
2247 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002248 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002249
2250 BUG_ON(queue_index >= vi->max_queue_pairs);
2251 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2252 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2253}
2254
2255static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2256 __ATTR_RO(mergeable_rx_buffer_size);
2257
2258static struct attribute *virtio_net_mrg_rx_attrs[] = {
2259 &mergeable_rx_buffer_size_attribute.attr,
2260 NULL
2261};
2262
2263static const struct attribute_group virtio_net_mrg_rx_group = {
2264 .name = "virtio_net",
2265 .attrs = virtio_net_mrg_rx_attrs
2266};
2267#endif
2268
Jason Wang892d6eb2014-11-20 17:03:05 +08002269static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2270 unsigned int fbit,
2271 const char *fname, const char *dname)
2272{
2273 if (!virtio_has_feature(vdev, fbit))
2274 return false;
2275
2276 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2277 fname, dname);
2278
2279 return true;
2280}
2281
2282#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2283 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2284
2285static bool virtnet_validate_features(struct virtio_device *vdev)
2286{
2287 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2288 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2289 "VIRTIO_NET_F_CTRL_VQ") ||
2290 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2291 "VIRTIO_NET_F_CTRL_VQ") ||
2292 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2293 "VIRTIO_NET_F_CTRL_VQ") ||
2294 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2295 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2296 "VIRTIO_NET_F_CTRL_VQ"))) {
2297 return false;
2298 }
2299
2300 return true;
2301}
2302
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002303#define MIN_MTU ETH_MIN_MTU
2304#define MAX_MTU ETH_MAX_MTU
2305
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002306static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10002307{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002308 if (!vdev->config->get) {
2309 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2310 __func__);
2311 return -EINVAL;
2312 }
2313
Jason Wang892d6eb2014-11-20 17:03:05 +08002314 if (!virtnet_validate_features(vdev))
2315 return -EINVAL;
2316
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002317 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2318 int mtu = virtio_cread16(vdev,
2319 offsetof(struct virtio_net_config,
2320 mtu));
2321 if (mtu < MIN_MTU)
2322 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2323 }
2324
2325 return 0;
2326}
2327
2328static int virtnet_probe(struct virtio_device *vdev)
2329{
2330 int i, err;
2331 struct net_device *dev;
2332 struct virtnet_info *vi;
2333 u16 max_queue_pairs;
2334 int mtu;
2335
Jason Wang986a4f42012-12-07 07:04:56 +00002336 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302337 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2338 struct virtio_net_config,
2339 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002340
2341 /* We need at least 2 queue's */
2342 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2343 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2344 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2345 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002346
2347 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002348 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002349 if (!dev)
2350 return -ENOMEM;
2351
2352 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002353 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002354 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002355 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002356
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002357 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002358 SET_NETDEV_DEV(dev, &vdev->dev);
2359
2360 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002361 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002362 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002363 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002364 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002365 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002366
2367 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002368 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002369 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2370 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002371 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002372 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2373 dev->hw_features |= NETIF_F_TSO;
2374 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2375 dev->hw_features |= NETIF_F_TSO6;
2376 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2377 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002378 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2379 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002380
Jason Wang41f2f122014-12-24 11:03:52 +08002381 dev->features |= NETIF_F_GSO_ROBUST;
2382
Michał Mirosław98e778c2011-03-31 01:01:35 +00002383 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002384 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002385 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002386 }
Thomas Huth4f491292013-08-27 17:09:02 +02002387 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2388 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002389
Jason Wang4fda8302013-04-10 23:32:21 +00002390 dev->vlan_features = dev->features;
2391
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002392 /* MTU range: 68 - 65535 */
2393 dev->min_mtu = MIN_MTU;
2394 dev->max_mtu = MAX_MTU;
2395
Rusty Russell296f96f2007-10-22 11:03:37 +10002396 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302397 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2398 virtio_cread_bytes(vdev,
2399 offsetof(struct virtio_net_config, mac),
2400 dev->dev_addr, dev->addr_len);
2401 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002402 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002403
2404 /* Set up our device-specific information */
2405 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002406 vi->dev = dev;
2407 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002408 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002409 vi->stats = alloc_percpu(struct virtnet_stats);
2410 err = -ENOMEM;
2411 if (vi->stats == NULL)
2412 goto free;
2413
John Stultz827da442013-10-07 15:51:58 -07002414 for_each_possible_cpu(i) {
2415 struct virtnet_stats *virtnet_stats;
2416 virtnet_stats = per_cpu_ptr(vi->stats, i);
2417 u64_stats_init(&virtnet_stats->tx_syncp);
2418 u64_stats_init(&virtnet_stats->rx_syncp);
2419 }
2420
Jason Wang586d17c2012-04-11 20:43:52 +00002421 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002422
Herbert Xu97402b92008-04-18 11:24:27 +08002423 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002424 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2425 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002426 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2427 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002428 vi->big_packets = true;
2429
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002430 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2431 vi->mergeable_rx_bufs = true;
2432
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002433 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2434 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002435 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2436 else
2437 vi->hdr_len = sizeof(struct virtio_net_hdr);
2438
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002439 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2440 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302441 vi->any_header_sg = true;
2442
Jason Wang986a4f42012-12-07 07:04:56 +00002443 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2444 vi->has_cvq = true;
2445
Aaron Conole14de9d12016-06-03 16:57:12 -04002446 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2447 mtu = virtio_cread16(vdev,
2448 offsetof(struct virtio_net_config,
2449 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002450 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002451 /* Should never trigger: MTU was previously validated
2452 * in virtnet_validate.
2453 */
2454 dev_err(&vdev->dev, "device MTU appears to have changed "
2455 "it is now %d < %d", mtu, dev->min_mtu);
2456 goto free_stats;
Aaron Conole93a205e2016-10-25 16:12:12 -04002457 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002458
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002459 dev->mtu = mtu;
2460 dev->max_mtu = mtu;
2461
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002462 /* TODO: size buffers correctly in this case. */
2463 if (dev->mtu > ETH_DATA_LEN)
2464 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04002465 }
2466
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002467 if (vi->any_header_sg)
2468 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002469
Jason Wang44900012016-11-25 12:37:26 +08002470 /* Enable multiqueue by default */
2471 if (num_online_cpus() >= max_queue_pairs)
2472 vi->curr_queue_pairs = max_queue_pairs;
2473 else
2474 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002475 vi->max_queue_pairs = max_queue_pairs;
2476
2477 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302478 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002479 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002480 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002481
Michael Daltonfbf28d72014-01-16 22:23:30 -08002482#ifdef CONFIG_SYSFS
2483 if (vi->mergeable_rx_bufs)
2484 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2485#endif
Zhi Yong Wu0f13b66b2013-11-18 21:19:27 +08002486 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2487 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002488
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002489 virtnet_init_settings(dev);
2490
Rusty Russell296f96f2007-10-22 11:03:37 +10002491 err = register_netdev(dev);
2492 if (err) {
2493 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002494 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002495 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002496
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302497 virtio_device_ready(vdev);
2498
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002499 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002500 if (err) {
2501 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002502 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002503 }
2504
Jason Wanga2208712016-12-13 14:23:05 +08002505 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08002506
Jason Wang167c25e2010-11-10 14:45:41 +00002507 /* Assume link up if device can't report link status,
2508 otherwise get link status from config. */
2509 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2510 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002511 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002512 } else {
2513 vi->status = VIRTIO_NET_S_LINK_UP;
2514 netif_carrier_on(dev);
2515 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002516
Jason Wang986a4f42012-12-07 07:04:56 +00002517 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2518 dev->name, max_queue_pairs);
2519
Rusty Russell296f96f2007-10-22 11:03:37 +10002520 return 0;
2521
wangyunjianf00e35e2016-05-31 11:52:43 +08002522free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302523 vi->vdev->config->reset(vdev);
2524
Rusty Russellb3369c12008-02-04 23:50:02 -05002525 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002526free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002527 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002528 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002529 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002530free_stats:
2531 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002532free:
2533 free_netdev(dev);
2534 return err;
2535}
2536
John Fastabend2de2f7f2017-02-02 19:16:29 -08002537static void _remove_vq_common(struct virtnet_info *vi)
2538{
2539 vi->vdev->config->reset(vi->vdev);
2540 free_unused_bufs(vi);
2541 _free_receive_bufs(vi);
2542 free_receive_page_frags(vi);
2543 virtnet_del_vqs(vi);
2544}
2545
Amit Shah04486ed2011-12-22 16:58:32 +05302546static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002547{
Amit Shah04486ed2011-12-22 16:58:32 +05302548 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002549
2550 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002551 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002552
Jason Wang986a4f42012-12-07 07:04:56 +00002553 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002554
Michael Daltonfb518792014-01-16 22:23:26 -08002555 free_receive_page_frags(vi);
2556
Jason Wang986a4f42012-12-07 07:04:56 +00002557 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302558}
2559
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002560static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302561{
2562 struct virtnet_info *vi = vdev->priv;
2563
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002564 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002565
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302566 /* Make sure no work handler is accessing the device. */
2567 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002568
Amit Shah04486ed2011-12-22 16:58:32 +05302569 unregister_netdev(vi->dev);
2570
2571 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002572
Krishna Kumar2e66f552011-07-20 03:56:02 +00002573 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002574 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002575}
2576
Aaron Lu89107002013-09-17 09:25:23 +09302577#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302578static int virtnet_freeze(struct virtio_device *vdev)
2579{
2580 struct virtnet_info *vi = vdev->priv;
2581
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002582 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002583 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05302584 remove_vq_common(vi);
2585
2586 return 0;
2587}
2588
2589static int virtnet_restore(struct virtio_device *vdev)
2590{
2591 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002592 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05302593
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002594 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05302595 if (err)
2596 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00002597 virtnet_set_queues(vi, vi->curr_queue_pairs);
2598
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002599 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002600 if (err)
2601 return err;
2602
Amit Shah0741bcb2011-12-22 16:58:33 +05302603 return 0;
2604}
2605#endif
2606
Rusty Russell296f96f2007-10-22 11:03:37 +10002607static struct virtio_device_id id_table[] = {
2608 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2609 { 0 },
2610};
2611
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002612#define VIRTNET_FEATURES \
2613 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2614 VIRTIO_NET_F_MAC, \
2615 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2616 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2617 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2618 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2619 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2620 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2621 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2622 VIRTIO_NET_F_MTU
2623
Rusty Russellc45a6812008-05-02 21:50:50 -05002624static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002625 VIRTNET_FEATURES,
2626};
2627
2628static unsigned int features_legacy[] = {
2629 VIRTNET_FEATURES,
2630 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302631 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002632};
2633
Uwe Kleine-König22402522009-11-05 01:32:44 -08002634static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002635 .feature_table = features,
2636 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002637 .feature_table_legacy = features_legacy,
2638 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002639 .driver.name = KBUILD_MODNAME,
2640 .driver.owner = THIS_MODULE,
2641 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002642 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10002643 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002644 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002645 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302646#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302647 .freeze = virtnet_freeze,
2648 .restore = virtnet_restore,
2649#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002650};
2651
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002652static __init int virtio_net_driver_init(void)
2653{
2654 int ret;
2655
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002656 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002657 virtnet_cpu_online,
2658 virtnet_cpu_down_prep);
2659 if (ret < 0)
2660 goto out;
2661 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002662 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002663 NULL, virtnet_cpu_dead);
2664 if (ret)
2665 goto err_dead;
2666
2667 ret = register_virtio_driver(&virtio_net_driver);
2668 if (ret)
2669 goto err_virtio;
2670 return 0;
2671err_virtio:
2672 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2673err_dead:
2674 cpuhp_remove_multi_state(virtionet_online);
2675out:
2676 return ret;
2677}
2678module_init(virtio_net_driver_init);
2679
2680static __exit void virtio_net_driver_exit(void)
2681{
2682 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2683 cpuhp_remove_multi_state(virtionet_online);
2684 unregister_virtio_driver(&virtio_net_driver);
2685}
2686module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002687
2688MODULE_DEVICE_TABLE(virtio, id_table);
2689MODULE_DESCRIPTION("Virtio network driver");
2690MODULE_LICENSE("GPL");