blob: 3b3c5c571b6d329656bf5c86d51e71245ceda7b5 [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
Rusty Russelleb939922011-12-19 14:08:01 +000036static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050037module_param(csum, bool, 0444);
38module_param(gso, bool, 0444);
39
Rusty Russell296f96f2007-10-22 11:03:37 +100040/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080041#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080042#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100043
Johannes Berg5377d7582015-08-19 09:48:40 +020044/* RX packet size EWMA. The average packet size is used to determine the packet
45 * buffer size when refilling RX rings. As the entire RX ring may be refilled
46 * at once, the weight is chosen so that the EWMA will be insensitive to short-
47 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080048 */
Johannes Berg5377d7582015-08-19 09:48:40 +020049DECLARE_EWMA(pkt_len, 1, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080050
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020051/* With mergeable buffers we align buffer address and use the low bits to
52 * encode its true size. Buffer size is up to 1 page so we need to align to
53 * square root of page size to ensure we reserve enough bits to encode the true
54 * size.
55 */
56#define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
57
Michael Daltonab7db912014-01-16 22:23:27 -080058/* Minimum alignment for mergeable packet buffers. */
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020059#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
60 1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
Michael Daltonab7db912014-01-16 22:23:27 -080061
Rick Jones66846042011-11-14 14:17:08 +000062#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000063
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000064struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000065 struct u64_stats_sync tx_syncp;
66 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000067 u64 tx_bytes;
68 u64 tx_packets;
69
70 u64 rx_bytes;
71 u64 rx_packets;
72};
73
Jason Wange9d74172012-12-07 07:04:55 +000074/* Internal representation of a send virtqueue */
75struct send_queue {
76 /* Virtqueue associated with this send _queue */
77 struct virtqueue *vq;
78
79 /* TX: fragments + linear part + virtio header */
80 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000081
82 /* Name of the send queue: output.$index */
83 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000084};
85
86/* Internal representation of a receive virtqueue */
87struct receive_queue {
88 /* Virtqueue associated with this receive_queue */
89 struct virtqueue *vq;
90
Rusty Russell296f96f2007-10-22 11:03:37 +100091 struct napi_struct napi;
92
John Fastabendf600b692016-12-15 12:13:24 -080093 struct bpf_prog __rcu *xdp_prog;
94
Jason Wange9d74172012-12-07 07:04:55 +000095 /* Chain pages by the private ptr. */
96 struct page *pages;
97
Michael Daltonab7db912014-01-16 22:23:27 -080098 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +020099 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800100
Michael Daltonfb518792014-01-16 22:23:26 -0800101 /* Page frag for packet buffer allocation. */
102 struct page_frag alloc_frag;
103
Jason Wange9d74172012-12-07 07:04:55 +0000104 /* RX: fragments + linear part + virtio header */
105 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000106
107 /* Name of this receive queue: input.$index */
108 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +0000109};
110
111struct virtnet_info {
112 struct virtio_device *vdev;
113 struct virtqueue *cvq;
114 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000115 struct send_queue *sq;
116 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000117 unsigned int status;
118
Jason Wang986a4f42012-12-07 07:04:56 +0000119 /* Max # of queue pairs supported by the device */
120 u16 max_queue_pairs;
121
122 /* # of queue pairs currently used by the driver */
123 u16 curr_queue_pairs;
124
John Fastabend672aafd2016-12-15 12:13:49 -0800125 /* # of XDP queue pairs currently used by the driver */
126 u16 xdp_queue_pairs;
127
Herbert Xu97402b92008-04-18 11:24:27 +0800128 /* I like... big packets and I cannot lie! */
129 bool big_packets;
130
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800131 /* Host will merge rx buffers for big packets (shake it! shake it!) */
132 bool mergeable_rx_bufs;
133
Jason Wang986a4f42012-12-07 07:04:56 +0000134 /* Has control virtqueue */
135 bool has_cvq;
136
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930137 /* Host can handle any s/g split between our header and packet data */
138 bool any_header_sg;
139
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300140 /* Packet virtio header size */
141 u8 hdr_len;
142
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000143 /* Active statistics */
144 struct virtnet_stats __percpu *stats;
145
Rusty Russell3161e452009-08-26 12:22:32 -0700146 /* Work struct for refilling if we run low on memory. */
147 struct delayed_work refill;
148
Jason Wang586d17c2012-04-11 20:43:52 +0000149 /* Work struct for config space updates */
150 struct work_struct config_work;
151
Jason Wang986a4f42012-12-07 07:04:56 +0000152 /* Does the affinity hint is set for virtqueues? */
153 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000154
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200155 /* CPU hotplug instances for online & dead */
156 struct hlist_node node;
157 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200158
159 /* Control VQ buffers: protected by the rtnl lock */
160 struct virtio_net_ctrl_hdr ctrl_hdr;
161 virtio_net_ctrl_ack ctrl_status;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700162 struct virtio_net_ctrl_mq ctrl_mq;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200163 u8 ctrl_promisc;
164 u8 ctrl_allmulti;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700165 u16 ctrl_vid;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100166
167 /* Ethtool settings */
168 u8 duplex;
169 u32 speed;
Rusty Russell296f96f2007-10-22 11:03:37 +1000170};
171
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000172struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300173 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000174 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300175 * hdr is in a separate sg buffer, and data sg buffer shares same page
176 * with this header sg. This padding makes next sg 16 byte aligned
177 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000178 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300179 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000180};
181
Jason Wang986a4f42012-12-07 07:04:56 +0000182/* Converting between virtqueue no. and kernel tx/rx queue no.
183 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
184 */
185static int vq2txq(struct virtqueue *vq)
186{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000187 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000188}
189
190static int txq2vq(int txq)
191{
192 return txq * 2 + 1;
193}
194
195static int vq2rxq(struct virtqueue *vq)
196{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000197 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000198}
199
200static int rxq2vq(int rxq)
201{
202 return rxq * 2;
203}
204
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300205static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000206{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300207 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000208}
209
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000210/*
211 * private is used to chain pages for big packets, put the whole
212 * most recent used list in the beginning for reuse
213 */
Jason Wange9d74172012-12-07 07:04:55 +0000214static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500215{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000216 struct page *end;
217
Jason Wange9d74172012-12-07 07:04:55 +0000218 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000219 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000220 end->private = (unsigned long)rq->pages;
221 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500222}
223
Jason Wange9d74172012-12-07 07:04:55 +0000224static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500225{
Jason Wange9d74172012-12-07 07:04:55 +0000226 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500227
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000228 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000229 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000230 /* clear private here, it is used to chain pages */
231 p->private = 0;
232 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500233 p = alloc_page(gfp_mask);
234 return p;
235}
236
Jason Wange9d74172012-12-07 07:04:55 +0000237static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000238{
Jason Wange9d74172012-12-07 07:04:55 +0000239 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000240
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500241 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000242 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000243
Rusty Russell363f1512008-06-08 20:51:55 +1000244 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000245 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000246}
247
Michael Daltonab7db912014-01-16 22:23:27 -0800248static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
249{
250 unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
251 return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
252}
253
254static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
255{
256 return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
257
258}
259
260static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
261{
262 unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
263 return (unsigned long)buf | (size - 1);
264}
265
Mike Waychison34646452012-01-04 12:52:32 +0000266/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300267static struct sk_buff *page_to_skb(struct virtnet_info *vi,
268 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700269 struct page *page, unsigned int offset,
270 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000271{
272 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300273 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700274 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000275 char *p;
276
Michael Dalton2613af02013-10-28 15:44:18 -0700277 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000278
279 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100280 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000281 if (unlikely(!skb))
282 return NULL;
283
284 hdr = skb_vnet_hdr(skb);
285
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300286 hdr_len = vi->hdr_len;
287 if (vi->mergeable_rx_bufs)
288 hdr_padded_len = sizeof *hdr;
289 else
Michael Dalton2613af02013-10-28 15:44:18 -0700290 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000291
292 memcpy(hdr, p, hdr_len);
293
294 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700295 offset += hdr_padded_len;
296 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000297
298 copy = len;
299 if (copy > skb_tailroom(skb))
300 copy = skb_tailroom(skb);
301 memcpy(skb_put(skb, copy), p, copy);
302
303 len -= copy;
304 offset += copy;
305
Michael Dalton2613af02013-10-28 15:44:18 -0700306 if (vi->mergeable_rx_bufs) {
307 if (len)
308 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
309 else
310 put_page(page);
311 return skb;
312 }
313
Sasha Levine878d782011-09-28 04:40:54 +0000314 /*
315 * Verify that we can indeed put this data into a skb.
316 * This is here to handle cases when the device erroneously
317 * tries to receive more than is possible. This is usually
318 * the case of a broken device.
319 */
320 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000321 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000322 dev_kfree_skb(skb);
323 return NULL;
324 }
Michael Dalton2613af02013-10-28 15:44:18 -0700325 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000326 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700327 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
328 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
329 frag_size, truesize);
330 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000331 page = (struct page *)page->private;
332 offset = 0;
333 }
334
335 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000336 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000337
338 return skb;
339}
340
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100341static bool virtnet_xdp_xmit(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800342 struct receive_queue *rq,
Jason Wangbb91acc2016-12-23 22:37:32 +0800343 struct xdp_buff *xdp,
344 void *data)
John Fastabend56434a02016-12-15 12:14:13 -0800345{
John Fastabend56434a02016-12-15 12:14:13 -0800346 struct virtio_net_hdr_mrg_rxbuf *hdr;
347 unsigned int num_sg, len;
John Fastabend722d8282017-02-02 19:15:32 -0800348 struct send_queue *sq;
349 unsigned int qp;
John Fastabend56434a02016-12-15 12:14:13 -0800350 void *xdp_sent;
351 int err;
352
John Fastabend722d8282017-02-02 19:15:32 -0800353 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
354 sq = &vi->sq[qp];
355
John Fastabend56434a02016-12-15 12:14:13 -0800356 /* Free up any pending old buffers before queueing new ones. */
357 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800358 if (vi->mergeable_rx_bufs) {
359 struct page *sent_page = virt_to_head_page(xdp_sent);
360
361 put_page(sent_page);
362 } else { /* small buffer */
363 struct sk_buff *skb = xdp_sent;
364
365 kfree_skb(skb);
366 }
John Fastabend56434a02016-12-15 12:14:13 -0800367 }
368
Jason Wangbb91acc2016-12-23 22:37:32 +0800369 if (vi->mergeable_rx_bufs) {
370 /* Zero header and leave csum up to XDP layers */
371 hdr = xdp->data;
372 memset(hdr, 0, vi->hdr_len);
John Fastabend56434a02016-12-15 12:14:13 -0800373
Jason Wangbb91acc2016-12-23 22:37:32 +0800374 num_sg = 1;
375 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
376 } else { /* small buffer */
377 struct sk_buff *skb = data;
378
379 /* Zero header and leave csum up to XDP layers */
380 hdr = skb_vnet_hdr(skb);
381 memset(hdr, 0, vi->hdr_len);
382
383 num_sg = 2;
384 sg_init_table(sq->sg, 2);
385 sg_set_buf(sq->sg, hdr, vi->hdr_len);
386 skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
387 }
John Fastabend56434a02016-12-15 12:14:13 -0800388 err = virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
Jason Wangbb91acc2016-12-23 22:37:32 +0800389 data, GFP_ATOMIC);
John Fastabend56434a02016-12-15 12:14:13 -0800390 if (unlikely(err)) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800391 if (vi->mergeable_rx_bufs) {
392 struct page *page = virt_to_head_page(xdp->data);
393
394 put_page(page);
395 } else /* small buffer */
396 kfree_skb(data);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100397 /* On error abort to avoid unnecessary kick */
398 return false;
John Fastabend56434a02016-12-15 12:14:13 -0800399 }
400
401 virtqueue_kick(sq->vq);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100402 return true;
John Fastabend56434a02016-12-15 12:14:13 -0800403}
404
Jason Wangbb91acc2016-12-23 22:37:32 +0800405static struct sk_buff *receive_small(struct net_device *dev,
406 struct virtnet_info *vi,
407 struct receive_queue *rq,
408 void *buf, unsigned int len)
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200409{
410 struct sk_buff * skb = buf;
Jason Wangbb91acc2016-12-23 22:37:32 +0800411 struct bpf_prog *xdp_prog;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200412
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300413 len -= vi->hdr_len;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200414 skb_trim(skb, len);
415
Jason Wangbb91acc2016-12-23 22:37:32 +0800416 rcu_read_lock();
417 xdp_prog = rcu_dereference(rq->xdp_prog);
418 if (xdp_prog) {
419 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
John Fastabend0354e4d2017-02-02 19:15:01 -0800420 struct xdp_buff xdp;
Jason Wangbb91acc2016-12-23 22:37:32 +0800421 u32 act;
422
423 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
424 goto err_xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800425
426 xdp.data = skb->data;
427 xdp.data_end = xdp.data + len;
428 act = bpf_prog_run_xdp(xdp_prog, &xdp);
429
Jason Wangbb91acc2016-12-23 22:37:32 +0800430 switch (act) {
431 case XDP_PASS:
432 break;
433 case XDP_TX:
John Fastabend722d8282017-02-02 19:15:32 -0800434 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp, skb)))
John Fastabend0354e4d2017-02-02 19:15:01 -0800435 trace_xdp_exception(vi->dev, xdp_prog, act);
Jason Wangbb91acc2016-12-23 22:37:32 +0800436 rcu_read_unlock();
437 goto xdp_xmit;
Jason Wangbb91acc2016-12-23 22:37:32 +0800438 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800439 bpf_warn_invalid_xdp_action(act);
440 case XDP_ABORTED:
441 trace_xdp_exception(vi->dev, xdp_prog, act);
442 case XDP_DROP:
Jason Wangbb91acc2016-12-23 22:37:32 +0800443 goto err_xdp;
444 }
445 }
446 rcu_read_unlock();
447
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200448 return skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800449
450err_xdp:
451 rcu_read_unlock();
452 dev->stats.rx_dropped++;
453 kfree_skb(skb);
454xdp_xmit:
455 return NULL;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200456}
457
458static struct sk_buff *receive_big(struct net_device *dev,
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300459 struct virtnet_info *vi,
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200460 struct receive_queue *rq,
461 void *buf,
462 unsigned int len)
463{
464 struct page *page = buf;
Jason Wangc47a43d2016-12-23 22:37:31 +0800465 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200466
467 if (unlikely(!skb))
468 goto err;
469
470 return skb;
471
472err:
473 dev->stats.rx_dropped++;
474 give_pages(rq, page);
475 return NULL;
476}
477
John Fastabend72979a62016-12-15 12:14:36 -0800478/* The conditions to enable XDP should preclude the underlying device from
479 * sending packets across multiple buffers (num_buf > 1). However per spec
480 * it does not appear to be illegal to do so but rather just against convention.
481 * So in order to avoid making a system unresponsive the packets are pushed
482 * into a page and the XDP program is run. This will be extremely slow and we
483 * push a warning to the user to fix this as soon as possible. Fixing this may
484 * require resolving the underlying hardware to determine why multiple buffers
485 * are being received or simply loading the XDP program in the ingress stack
486 * after the skb is built because there is no advantage to running it here
487 * anymore.
488 */
489static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800490 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800491 struct page *p,
492 int offset,
493 unsigned int *len)
494{
495 struct page *page = alloc_page(GFP_ATOMIC);
496 unsigned int page_off = 0;
497
498 if (!page)
499 return NULL;
500
501 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
502 page_off += *len;
503
Jason Wang56a86f82016-12-23 22:37:26 +0800504 while (--*num_buf) {
John Fastabend72979a62016-12-15 12:14:36 -0800505 unsigned int buflen;
506 unsigned long ctx;
507 void *buf;
508 int off;
509
510 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
511 if (unlikely(!ctx))
512 goto err_buf;
513
John Fastabend72979a62016-12-15 12:14:36 -0800514 buf = mergeable_ctx_to_buf_address(ctx);
515 p = virt_to_head_page(buf);
516 off = buf - page_address(p);
517
Jason Wang56a86f82016-12-23 22:37:26 +0800518 /* guard against a misconfigured or uncooperative backend that
519 * is sending packet larger than the MTU.
520 */
521 if ((page_off + buflen) > PAGE_SIZE) {
522 put_page(p);
523 goto err_buf;
524 }
525
John Fastabend72979a62016-12-15 12:14:36 -0800526 memcpy(page_address(page) + page_off,
527 page_address(p) + off, buflen);
528 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800529 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800530 }
531
532 *len = page_off;
533 return page;
534err_buf:
535 __free_pages(page, 0);
536 return NULL;
537}
538
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200539static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200540 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200541 struct receive_queue *rq,
Michael Daltonab7db912014-01-16 22:23:27 -0800542 unsigned long ctx,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200543 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000544{
Michael Daltonab7db912014-01-16 22:23:27 -0800545 void *buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300546 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
547 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200548 struct page *page = virt_to_head_page(buf);
549 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800550 struct sk_buff *head_skb, *curr_skb;
551 struct bpf_prog *xdp_prog;
552 unsigned int truesize;
Michael Daltonab7db912014-01-16 22:23:27 -0800553
John Fastabend56434a02016-12-15 12:14:13 -0800554 head_skb = NULL;
555
John Fastabendf600b692016-12-15 12:13:24 -0800556 rcu_read_lock();
557 xdp_prog = rcu_dereference(rq->xdp_prog);
558 if (xdp_prog) {
John Fastabend72979a62016-12-15 12:14:36 -0800559 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800560 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800561 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800562 u32 act;
563
Jason Wang73b62bd2016-12-23 22:37:24 +0800564 /* This happens when rx buffer size is underestimated */
John Fastabendf600b692016-12-15 12:13:24 -0800565 if (unlikely(num_buf > 1)) {
John Fastabend72979a62016-12-15 12:14:36 -0800566 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800567 xdp_page = xdp_linearize_page(rq, &num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800568 page, offset, &len);
569 if (!xdp_page)
570 goto err_xdp;
571 offset = 0;
572 } else {
573 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800574 }
575
576 /* Transient failure which in theory could occur if
577 * in-flight packets from before XDP was enabled reach
578 * the receive path after XDP is loaded. In practice I
579 * was not able to create this condition.
580 */
Jason Wangb00f70b2016-12-23 22:37:28 +0800581 if (unlikely(hdr->hdr.gso_type))
John Fastabendf600b692016-12-15 12:13:24 -0800582 goto err_xdp;
583
John Fastabend0354e4d2017-02-02 19:15:01 -0800584 data = page_address(xdp_page) + offset;
585 xdp.data = data + vi->hdr_len;
586 xdp.data_end = xdp.data + (len - vi->hdr_len);
587 act = bpf_prog_run_xdp(xdp_prog, &xdp);
588
John Fastabend56434a02016-12-15 12:14:13 -0800589 switch (act) {
590 case XDP_PASS:
Jason Wang1830f892016-12-23 22:37:27 +0800591 /* We can only create skb based on xdp_page. */
592 if (unlikely(xdp_page != page)) {
593 rcu_read_unlock();
594 put_page(page);
595 head_skb = page_to_skb(vi, rq, xdp_page,
596 0, len, PAGE_SIZE);
Jason Wang5c334742016-12-23 22:37:29 +0800597 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
Jason Wang1830f892016-12-23 22:37:27 +0800598 return head_skb;
599 }
John Fastabend56434a02016-12-15 12:14:13 -0800600 break;
601 case XDP_TX:
John Fastabend722d8282017-02-02 19:15:32 -0800602 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp, data)))
John Fastabend0354e4d2017-02-02 19:15:01 -0800603 trace_xdp_exception(vi->dev, xdp_prog, act);
Jason Wang5c334742016-12-23 22:37:29 +0800604 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabend72979a62016-12-15 12:14:36 -0800605 if (unlikely(xdp_page != page))
606 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800607 rcu_read_unlock();
608 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -0800609 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800610 bpf_warn_invalid_xdp_action(act);
611 case XDP_ABORTED:
612 trace_xdp_exception(vi->dev, xdp_prog, act);
613 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -0800614 if (unlikely(xdp_page != page))
615 __free_pages(xdp_page, 0);
Jason Wang5c334742016-12-23 22:37:29 +0800616 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabendf600b692016-12-15 12:13:24 -0800617 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800618 }
John Fastabendf600b692016-12-15 12:13:24 -0800619 }
620 rcu_read_unlock();
621
622 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
623 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
624 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000625
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200626 if (unlikely(!curr_skb))
627 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000628 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200629 int num_skb_frags;
630
Michael Daltonab7db912014-01-16 22:23:27 -0800631 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
632 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200633 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200634 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300635 virtio16_to_cpu(vi->vdev,
636 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200637 dev->stats.rx_length_errors++;
638 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000639 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200640
Michael Daltonab7db912014-01-16 22:23:27 -0800641 buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200642 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200643
644 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700645 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
646 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200647
648 if (unlikely(!nskb))
649 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700650 if (curr_skb == head_skb)
651 skb_shinfo(curr_skb)->frag_list = nskb;
652 else
653 curr_skb->next = nskb;
654 curr_skb = nskb;
655 head_skb->truesize += nskb->truesize;
656 num_skb_frags = 0;
657 }
Michael Daltonab7db912014-01-16 22:23:27 -0800658 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
Michael Dalton2613af02013-10-28 15:44:18 -0700659 if (curr_skb != head_skb) {
660 head_skb->data_len += len;
661 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800662 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700663 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200664 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800665 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
666 put_page(page);
667 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800668 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800669 } else {
670 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800671 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800672 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200673 }
674
Johannes Berg5377d7582015-08-19 09:48:40 +0200675 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200676 return head_skb;
677
John Fastabendf600b692016-12-15 12:13:24 -0800678err_xdp:
679 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200680err_skb:
681 put_page(page);
682 while (--num_buf) {
Michael Daltonab7db912014-01-16 22:23:27 -0800683 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
684 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200685 pr_debug("%s: rx error: %d buffers missing\n",
686 dev->name, num_buf);
687 dev->stats.rx_length_errors++;
688 break;
689 }
Michael Daltonab7db912014-01-16 22:23:27 -0800690 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200691 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000692 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200693err_buf:
694 dev->stats.rx_dropped++;
695 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800696xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200697 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000698}
699
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300700static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
701 void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000702{
Jason Wange9d74172012-12-07 07:04:55 +0000703 struct net_device *dev = vi->dev;
Eric Dumazet58472a72012-02-13 06:53:41 +0000704 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000705 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300706 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000707
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300708 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000709 pr_debug("%s: short packet %i\n", dev->name, len);
710 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800711 if (vi->mergeable_rx_bufs) {
712 unsigned long ctx = (unsigned long)buf;
713 void *base = mergeable_ctx_to_buf_address(ctx);
714 put_page(virt_to_head_page(base));
715 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800716 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800717 } else {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000718 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800719 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000720 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000721 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000722
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200723 if (vi->mergeable_rx_bufs)
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200724 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200725 else if (vi->big_packets)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300726 skb = receive_big(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200727 else
Jason Wangbb91acc2016-12-23 22:37:32 +0800728 skb = receive_small(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200729
730 if (unlikely(!skb))
731 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800732
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000733 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000734
Eric Dumazet83a27052012-06-05 22:35:24 +0000735 u64_stats_update_begin(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000736 stats->rx_bytes += skb->len;
737 stats->rx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000738 u64_stats_update_end(&stats->rx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +1000739
Mike Rapoporte858fae2016-06-08 16:09:21 +0300740 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +0000741 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000742
Mike Rapoporte858fae2016-06-08 16:09:21 +0300743 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
744 virtio_is_little_endian(vi->vdev))) {
745 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
746 dev->name, hdr->hdr.gso_type,
747 hdr->hdr.gso_size);
748 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000749 }
750
Mike Rapoportd1dc06d2016-06-14 08:29:38 +0300751 skb->protocol = eth_type_trans(skb, dev);
752 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
753 ntohs(skb->protocol), skb->len, skb->pkt_type);
754
Eric Dumazet0fbd0502015-07-31 18:25:17 +0200755 napi_gro_receive(&rq->napi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000756 return;
757
758frame_err:
759 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000760 dev_kfree_skb(skb);
761}
762
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300763static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
764 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000765{
766 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300767 struct virtio_net_hdr_mrg_rxbuf *hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000768 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000769
Michael Dalton5061de32013-11-14 10:41:04 -0800770 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000771 if (unlikely(!skb))
772 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800773
Michael Dalton5061de32013-11-14 10:41:04 -0800774 skb_put(skb, GOOD_PACKET_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000775
776 hdr = skb_vnet_hdr(skb);
Jason Wang547c8902015-08-27 14:53:06 +0800777 sg_init_table(rq->sg, 2);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300778 sg_set_buf(rq->sg, hdr, vi->hdr_len);
Jason Wange9d74172012-12-07 07:04:55 +0000779 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000780
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030781 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000782 if (err < 0)
783 dev_kfree_skb(skb);
784
785 return err;
786}
787
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300788static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
789 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000790{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000791 struct page *first, *list = NULL;
792 char *p;
793 int i, err, offset;
794
Rusty Russella5835442014-09-11 10:17:36 +0930795 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
796
Jason Wange9d74172012-12-07 07:04:55 +0000797 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000798 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000799 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000800 if (!first) {
801 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000802 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000803 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700804 }
Jason Wange9d74172012-12-07 07:04:55 +0000805 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000806
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000807 /* chain new page in list head to match sg */
808 first->private = (unsigned long)list;
809 list = first;
810 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800811
Jason Wange9d74172012-12-07 07:04:55 +0000812 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000813 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000814 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000815 return -ENOMEM;
816 }
817 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800818
Jason Wange9d74172012-12-07 07:04:55 +0000819 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300820 /* a separated rq->sg[0] for header - required in case !any_header_sg */
821 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +0800822
Jason Wange9d74172012-12-07 07:04:55 +0000823 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000824 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000825 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800826
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000827 /* chain first in list head */
828 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030829 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
830 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000831 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000832 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800833
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000834 return err;
835}
Herbert Xu97402b92008-04-18 11:24:27 +0800836
Johannes Berg5377d7582015-08-19 09:48:40 +0200837static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000838{
Michael Daltonab7db912014-01-16 22:23:27 -0800839 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800840 unsigned int len;
841
Johannes Berg5377d7582015-08-19 09:48:40 +0200842 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael Daltonfbf28d72014-01-16 22:23:30 -0800843 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
844 return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
845}
846
847static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
848{
Michael Daltonfb518792014-01-16 22:23:26 -0800849 struct page_frag *alloc_frag = &rq->alloc_frag;
850 char *buf;
Michael Daltonab7db912014-01-16 22:23:27 -0800851 unsigned long ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000852 int err;
Michael Daltonfb518792014-01-16 22:23:26 -0800853 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +1000854
Michael Daltonfbf28d72014-01-16 22:23:30 -0800855 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
Michael Daltonab7db912014-01-16 22:23:27 -0800856 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000857 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -0800858
Michael Daltonfb518792014-01-16 22:23:26 -0800859 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800860 ctx = mergeable_buf_to_ctx(buf, len);
Michael Daltonfb518792014-01-16 22:23:26 -0800861 get_page(alloc_frag->page);
Michael Daltonfb518792014-01-16 22:23:26 -0800862 alloc_frag->offset += len;
863 hole = alloc_frag->size - alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800864 if (hole < len) {
865 /* To avoid internal fragmentation, if there is very likely not
866 * enough space for another buffer, add the remaining space to
867 * the current buffer. This extra space is not included in
868 * the truesize stored in ctx.
869 */
Michael Daltonfb518792014-01-16 22:23:26 -0800870 len += hole;
871 alloc_frag->offset += hole;
872 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000873
Michael Daltonfb518792014-01-16 22:23:26 -0800874 sg_init_one(rq->sg, buf, len);
Michael Daltonab7db912014-01-16 22:23:27 -0800875 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000876 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700877 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000878
879 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000880}
881
Rusty Russellb2baed62011-12-29 00:42:38 +0000882/*
883 * Returns false if we couldn't fill entirely (OOM).
884 *
885 * Normally run in the receive path, but can also be run from ndo_open
886 * before we're receiving packets, or from refill_work which is
887 * careful to disable receiving (using napi_disable).
888 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300889static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
890 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800891{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800892 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000893 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800894
Michael Daltonfb518792014-01-16 22:23:26 -0800895 gfp |= __GFP_COLD;
Amit Shah0aea51c2009-08-26 14:58:28 +0530896 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000897 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000898 err = add_recvbuf_mergeable(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000899 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300900 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000901 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300902 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800903
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000904 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030905 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800906 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800907 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +0800908 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700909 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800910}
911
Rusty Russell18445c42008-02-04 23:49:57 -0500912static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000913{
914 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000915 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000916
Rusty Russell18445c42008-02-04 23:49:57 -0500917 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000918 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300919 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000920 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500921 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000922}
923
Jason Wange9d74172012-12-07 07:04:55 +0000924static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800925{
Jason Wange9d74172012-12-07 07:04:55 +0000926 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800927
928 /* If all buffers were filled by other side before we napi_enabled, we
929 * won't get another interrupt, so process any outstanding packets
930 * now. virtnet_poll wants re-enable the queue, so we disable here.
931 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000932 if (napi_schedule_prep(&rq->napi)) {
933 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300934 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000935 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300936 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800937 }
938}
939
Rusty Russell3161e452009-08-26 12:22:32 -0700940static void refill_work(struct work_struct *work)
941{
Jason Wange9d74172012-12-07 07:04:55 +0000942 struct virtnet_info *vi =
943 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700944 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000945 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700946
Sasha Levin55257d72013-04-29 12:00:08 +0930947 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000948 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700949
Jason Wang986a4f42012-12-07 07:04:56 +0000950 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300951 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +0000952 virtnet_napi_enable(rq);
953
954 /* In theory, this can happen: if we don't get any buffers in
955 * we will *never* try to fill again.
956 */
957 if (still_empty)
958 schedule_delayed_work(&vi->refill, HZ/2);
959 }
Rusty Russell3161e452009-08-26 12:22:32 -0700960}
961
Jason Wang2ffa7592014-07-23 16:33:54 +0800962static int virtnet_receive(struct receive_queue *rq, int budget)
Rusty Russell296f96f2007-10-22 11:03:37 +1000963{
Jason Wange9d74172012-12-07 07:04:55 +0000964 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wang2ffa7592014-07-23 16:33:54 +0800965 unsigned int len, received = 0;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000966 void *buf;
Rusty Russell296f96f2007-10-22 11:03:37 +1000967
Rusty Russell296f96f2007-10-22 11:03:37 +1000968 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +0000969 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300970 receive_buf(vi, rq, buf, len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000971 received++;
972 }
973
Jason Wangbe121f42014-01-16 14:45:24 +0800974 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300975 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700976 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -0700977 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000978
Jason Wang2ffa7592014-07-23 16:33:54 +0800979 return received;
980}
981
982static int virtnet_poll(struct napi_struct *napi, int budget)
983{
984 struct receive_queue *rq =
985 container_of(napi, struct receive_queue, napi);
Li RongQingfaadb052015-03-26 15:39:45 +0800986 unsigned int r, received;
Jason Wang2ffa7592014-07-23 16:33:54 +0800987
Li RongQingfaadb052015-03-26 15:39:45 +0800988 received = virtnet_receive(rq, budget);
Jason Wang2ffa7592014-07-23 16:33:54 +0800989
Rusty Russell8329d982007-11-19 11:20:43 -0500990 /* Out of packets? */
991 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +0300992 r = virtqueue_enable_cb_prepare(rq->vq);
Eric Dumazet4d6308aa2017-02-04 07:49:21 -0800993 if (napi_complete_done(napi, received)) {
994 if (unlikely(virtqueue_poll(rq->vq, r)) &&
995 napi_schedule_prep(napi)) {
996 virtqueue_disable_cb(rq->vq);
997 __napi_schedule(napi);
998 }
Christian Borntraeger4265f162008-03-14 14:17:05 +0100999 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001000 }
1001
1002 return received;
1003}
1004
Jason Wang986a4f42012-12-07 07:04:56 +00001005static int virtnet_open(struct net_device *dev)
1006{
1007 struct virtnet_info *vi = netdev_priv(dev);
1008 int i;
1009
Jason Wange4166622013-05-21 20:03:58 +00001010 for (i = 0; i < vi->max_queue_pairs; i++) {
1011 if (i < vi->curr_queue_pairs)
1012 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001013 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001014 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +00001015 virtnet_napi_enable(&vi->rq[i]);
1016 }
1017
1018 return 0;
1019}
1020
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001021static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001022{
1023 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +10301024 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +00001025 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +00001026 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001027
Jason Wange9d74172012-12-07 07:04:55 +00001028 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001029 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001030
Eric Dumazet83a27052012-06-05 22:35:24 +00001031 u64_stats_update_begin(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001032 stats->tx_bytes += skb->len;
1033 stats->tx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +00001034 u64_stats_update_end(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001035
Eric Dumazeted79bab2009-10-14 14:36:43 +00001036 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001037 }
1038}
1039
Jason Wange9d74172012-12-07 07:04:55 +00001040static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001041{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001042 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001043 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001044 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +10301045 unsigned num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001046 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301047 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001048
Johannes Berge1749612008-10-27 15:59:26 -07001049 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301050
1051 can_push = vi->any_header_sg &&
1052 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1053 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1054 /* Even if we can, don't push here yet as this would skew
1055 * csum_start offset below. */
1056 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001057 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301058 else
1059 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001060
Mike Rapoporte858fae2016-06-08 16:09:21 +03001061 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Jason Wang6391a442017-01-20 14:32:42 +08001062 virtio_is_little_endian(vi->vdev), false))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001063 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001064
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001065 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001066 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001067
Jason Wang547c8902015-08-27 14:53:06 +08001068 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301069 if (can_push) {
1070 __skb_push(skb, hdr_len);
1071 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1072 /* Pull header back to avoid skew in tx bytes calculations. */
1073 __skb_pull(skb, hdr_len);
1074 } else {
1075 sg_set_buf(sq->sg, hdr, hdr_len);
1076 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1077 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301078 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001079}
1080
Stephen Hemminger424efe92009-08-31 19:50:51 +00001081static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001082{
1083 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001084 int qnum = skb_get_queue_mapping(skb);
1085 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301086 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001087 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1088 bool kick = !skb->xmit_more;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001089
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001090 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001091 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001092
Jacob Keller074c3582014-06-25 02:37:13 +00001093 /* timestamp packet in software */
1094 skb_tx_timestamp(skb);
1095
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001096 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001097 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001098
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301099 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001100 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301101 dev->stats.tx_fifo_errors++;
1102 if (net_ratelimit())
1103 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001104 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001105 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001106 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001107 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001108 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001109
Rusty Russell48925e32009-09-24 09:59:20 -06001110 /* Don't wait up for transmitted skbs to be freed. */
1111 skb_orphan(skb);
1112 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -05001113
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001114 /* If running out of space, stop queue to avoid getting packets that we
1115 * are then unable to transmit.
1116 * An alternative would be to force queuing layer to requeue the skb by
1117 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1118 * returned in a normal path of operation: it means that driver is not
1119 * maintaining the TX queue stop/start state properly, and causes
1120 * the stack to do a non-trivial amount of useless work.
1121 * Since most packets only take 1 or 2 ring slots, stopping the queue
1122 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001123 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001124 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001125 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001126 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001127 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001128 free_old_xmit_skbs(sq);
1129 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001130 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001131 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001132 }
1133 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001134 }
Rusty Russell48925e32009-09-24 09:59:20 -06001135
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001136 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001137 virtqueue_kick(sq->vq);
1138
Rusty Russell48925e32009-09-24 09:59:20 -06001139 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001140}
1141
Amos Kong40cbfc32013-01-21 01:17:21 +00001142/*
1143 * Send command via the control virtqueue and check status. Commands
1144 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001145 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001146 */
1147static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001148 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001149{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301150 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001151 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001152
1153 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301154 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001155
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001156 vi->ctrl_status = ~0;
1157 vi->ctrl_hdr.class = class;
1158 vi->ctrl_hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301159 /* Add header */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001160 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301161 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001162
Rusty Russellf7bc9592013-03-20 15:44:28 +10301163 if (out)
1164 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001165
Rusty Russellf7bc9592013-03-20 15:44:28 +10301166 /* Add return status. */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001167 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001168 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001169
stephen hemmingerd24bae32013-12-09 16:17:40 -08001170 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301171 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001172
Heinz Graalfs67975902013-10-29 09:40:02 +10301173 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001174 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001175
1176 /* Spin for a response, the kick causes an ioport write, trapping
1177 * into the hypervisor, so the request should be handled immediately.
1178 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301179 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1180 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001181 cpu_relax();
1182
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001183 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001184}
1185
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001186static int virtnet_set_mac_address(struct net_device *dev, void *p)
1187{
1188 struct virtnet_info *vi = netdev_priv(dev);
1189 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001190 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001191 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001192 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001193
Shyam Saini801822d2016-12-24 00:44:58 +05301194 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001195 if (!addr)
1196 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001197
1198 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001199 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001200 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001201
Amos Kong7e58d5a2013-01-21 01:17:23 +00001202 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1203 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1204 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001205 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001206 dev_warn(&vdev->dev,
1207 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001208 ret = -EINVAL;
1209 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001210 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001211 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1212 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301213 unsigned int i;
1214
1215 /* Naturally, this has an atomicity problem. */
1216 for (i = 0; i < dev->addr_len; i++)
1217 virtio_cwrite8(vdev,
1218 offsetof(struct virtio_net_config, mac) +
1219 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001220 }
1221
1222 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001223 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001224
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001225out:
1226 kfree(addr);
1227 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001228}
1229
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001230static void virtnet_stats(struct net_device *dev,
1231 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001232{
1233 struct virtnet_info *vi = netdev_priv(dev);
1234 int cpu;
1235 unsigned int start;
1236
1237 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +00001238 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001239 u64 tpackets, tbytes, rpackets, rbytes;
1240
1241 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001242 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001243 tpackets = stats->tx_packets;
1244 tbytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001245 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001246
1247 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001248 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001249 rpackets = stats->rx_packets;
1250 rbytes = stats->rx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001251 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001252
1253 tot->rx_packets += rpackets;
1254 tot->tx_packets += tpackets;
1255 tot->rx_bytes += rbytes;
1256 tot->tx_bytes += tbytes;
1257 }
1258
1259 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001260 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001261 tot->rx_dropped = dev->stats.rx_dropped;
1262 tot->rx_length_errors = dev->stats.rx_length_errors;
1263 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001264}
1265
Amit Shahda74e892008-02-29 16:24:50 +05301266#ifdef CONFIG_NET_POLL_CONTROLLER
1267static void virtnet_netpoll(struct net_device *dev)
1268{
1269 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001270 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301271
Jason Wang986a4f42012-12-07 07:04:56 +00001272 for (i = 0; i < vi->curr_queue_pairs; i++)
1273 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301274}
1275#endif
1276
Jason Wang586d17c2012-04-11 20:43:52 +00001277static void virtnet_ack_link_announce(struct virtnet_info *vi)
1278{
1279 rtnl_lock();
1280 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001281 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001282 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1283 rtnl_unlock();
1284}
1285
John Fastabend473153292017-02-02 19:14:32 -08001286static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001287{
1288 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001289 struct net_device *dev = vi->dev;
1290
1291 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1292 return 0;
1293
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001294 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1295 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001296
1297 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001298 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001299 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1300 queue_pairs);
1301 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301302 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001303 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001304 /* virtnet_open() will refill when device is going to up. */
1305 if (dev->flags & IFF_UP)
1306 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301307 }
Jason Wang986a4f42012-12-07 07:04:56 +00001308
1309 return 0;
1310}
1311
John Fastabend473153292017-02-02 19:14:32 -08001312static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1313{
1314 int err;
1315
1316 rtnl_lock();
1317 err = _virtnet_set_queues(vi, queue_pairs);
1318 rtnl_unlock();
1319 return err;
1320}
1321
Rusty Russell296f96f2007-10-22 11:03:37 +10001322static int virtnet_close(struct net_device *dev)
1323{
1324 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001325 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001326
Rusty Russellb2baed62011-12-29 00:42:38 +00001327 /* Make sure refill_work doesn't re-enable napi! */
1328 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001329
1330 for (i = 0; i < vi->max_queue_pairs; i++)
1331 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001332
Rusty Russell296f96f2007-10-22 11:03:37 +10001333 return 0;
1334}
1335
Alex Williamson2af76982009-02-04 09:02:40 +00001336static void virtnet_set_rx_mode(struct net_device *dev)
1337{
1338 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001339 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001340 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001341 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001342 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001343 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001344 void *buf;
1345 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001346
stephen hemminger788a8b62013-12-09 16:18:45 -08001347 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001348 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1349 return;
1350
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001351 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1352 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001353
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001354 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001355
1356 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001357 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001358 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001359 vi->ctrl_promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001360
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001361 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001362
1363 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001364 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001365 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001366 vi->ctrl_allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001367
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001368 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001369 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001370 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001371 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1372 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1373 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001374 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001375 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001376
Alex Williamson23e258e2009-05-01 17:27:56 +00001377 sg_init_table(sg, 2);
1378
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001379 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001380 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001381 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001382 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001383 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001384
1385 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001386 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001387
1388 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001389 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001390
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001391 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001392 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001393 netdev_for_each_mc_addr(ha, dev)
1394 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001395
1396 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001397 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001398
1399 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001400 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001401 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001402
1403 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001404}
1405
Patrick McHardy80d5c362013-04-19 02:04:28 +00001406static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1407 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001408{
1409 struct virtnet_info *vi = netdev_priv(dev);
1410 struct scatterlist sg;
1411
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001412 vi->ctrl_vid = vid;
1413 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001414
1415 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001416 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001417 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001418 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001419}
1420
Patrick McHardy80d5c362013-04-19 02:04:28 +00001421static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1422 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001423{
1424 struct virtnet_info *vi = netdev_priv(dev);
1425 struct scatterlist sg;
1426
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001427 vi->ctrl_vid = vid;
1428 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001429
1430 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001431 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001432 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001433 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001434}
1435
Wanlong Gao8898c212013-01-24 23:51:30 +00001436static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001437{
1438 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001439
1440 if (vi->affinity_hint_set) {
1441 for (i = 0; i < vi->max_queue_pairs; i++) {
1442 virtqueue_set_affinity(vi->rq[i].vq, -1);
1443 virtqueue_set_affinity(vi->sq[i].vq, -1);
1444 }
1445
1446 vi->affinity_hint_set = false;
1447 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001448}
1449
1450static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001451{
1452 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001453 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001454
1455 /* In multiqueue mode, when the number of cpu is equal to the number of
1456 * queue pairs, we let the queue pairs to be private to one cpu by
1457 * setting the affinity hint to eliminate the contention.
1458 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001459 if (vi->curr_queue_pairs == 1 ||
1460 vi->max_queue_pairs != num_online_cpus()) {
1461 virtnet_clean_affinity(vi, -1);
1462 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001463 }
1464
Wanlong Gao8898c212013-01-24 23:51:30 +00001465 i = 0;
1466 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001467 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1468 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001469 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001470 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001471 }
1472
Wanlong Gao8898c212013-01-24 23:51:30 +00001473 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001474}
1475
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001476static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001477{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001478 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1479 node);
1480 virtnet_set_affinity(vi);
1481 return 0;
1482}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001483
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001484static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1485{
1486 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1487 node_dead);
1488 virtnet_set_affinity(vi);
1489 return 0;
1490}
Jason Wang3ab098d2013-10-15 11:18:58 +08001491
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001492static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1493{
1494 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1495 node);
1496
1497 virtnet_clean_affinity(vi, cpu);
1498 return 0;
1499}
1500
1501static enum cpuhp_state virtionet_online;
1502
1503static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1504{
1505 int ret;
1506
1507 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1508 if (ret)
1509 return ret;
1510 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1511 &vi->node_dead);
1512 if (!ret)
1513 return ret;
1514 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1515 return ret;
1516}
1517
1518static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1519{
1520 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1521 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1522 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001523}
1524
Rick Jones8f9f4662011-10-19 08:10:59 +00001525static void virtnet_get_ringparam(struct net_device *dev,
1526 struct ethtool_ringparam *ring)
1527{
1528 struct virtnet_info *vi = netdev_priv(dev);
1529
Jason Wang986a4f42012-12-07 07:04:56 +00001530 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1531 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001532 ring->rx_pending = ring->rx_max_pending;
1533 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001534}
1535
Rick Jones66846042011-11-14 14:17:08 +00001536
1537static void virtnet_get_drvinfo(struct net_device *dev,
1538 struct ethtool_drvinfo *info)
1539{
1540 struct virtnet_info *vi = netdev_priv(dev);
1541 struct virtio_device *vdev = vi->vdev;
1542
1543 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1544 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1545 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1546
1547}
1548
Jason Wangd73bcd22012-12-07 07:04:57 +00001549/* TODO: Eliminate OOO packets during switching */
1550static int virtnet_set_channels(struct net_device *dev,
1551 struct ethtool_channels *channels)
1552{
1553 struct virtnet_info *vi = netdev_priv(dev);
1554 u16 queue_pairs = channels->combined_count;
1555 int err;
1556
1557 /* We don't support separate rx/tx channels.
1558 * We don't allow setting 'other' channels.
1559 */
1560 if (channels->rx_count || channels->tx_count || channels->other_count)
1561 return -EINVAL;
1562
Amos Kongc18e9cd2014-04-18 13:45:41 +08001563 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001564 return -EINVAL;
1565
John Fastabendf600b692016-12-15 12:13:24 -08001566 /* For now we don't support modifying channels while XDP is loaded
1567 * also when XDP is loaded all RX queues have XDP programs so we only
1568 * need to check a single RX queue.
1569 */
1570 if (vi->rq[0].xdp_prog)
1571 return -EINVAL;
1572
Wanlong Gao47be2472013-01-24 23:51:29 +00001573 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08001574 err = _virtnet_set_queues(vi, queue_pairs);
Jason Wangd73bcd22012-12-07 07:04:57 +00001575 if (!err) {
1576 netif_set_real_num_tx_queues(dev, queue_pairs);
1577 netif_set_real_num_rx_queues(dev, queue_pairs);
1578
Wanlong Gao8898c212013-01-24 23:51:30 +00001579 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001580 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001581 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001582
1583 return err;
1584}
1585
1586static void virtnet_get_channels(struct net_device *dev,
1587 struct ethtool_channels *channels)
1588{
1589 struct virtnet_info *vi = netdev_priv(dev);
1590
1591 channels->combined_count = vi->curr_queue_pairs;
1592 channels->max_combined = vi->max_queue_pairs;
1593 channels->max_other = 0;
1594 channels->rx_count = 0;
1595 channels->tx_count = 0;
1596 channels->other_count = 0;
1597}
1598
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001599/* Check if the user is trying to change anything besides speed/duplex */
1600static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1601{
1602 struct ethtool_cmd diff1 = *cmd;
1603 struct ethtool_cmd diff2 = {};
1604
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001605 /* cmd is always set so we need to clear it, validate the port type
1606 * and also without autonegotiation we can ignore advertising
1607 */
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001608 ethtool_cmd_speed_set(&diff1, 0);
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001609 diff2.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001610 diff1.advertising = 0;
1611 diff1.duplex = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001612 diff1.cmd = 0;
1613
1614 return !memcmp(&diff1, &diff2, sizeof(diff1));
1615}
1616
1617static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1618{
1619 struct virtnet_info *vi = netdev_priv(dev);
1620 u32 speed;
1621
1622 speed = ethtool_cmd_speed(cmd);
1623 /* don't allow custom speed and duplex */
1624 if (!ethtool_validate_speed(speed) ||
1625 !ethtool_validate_duplex(cmd->duplex) ||
1626 !virtnet_validate_ethtool_cmd(cmd))
1627 return -EINVAL;
1628 vi->speed = speed;
1629 vi->duplex = cmd->duplex;
1630
1631 return 0;
1632}
1633
1634static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1635{
1636 struct virtnet_info *vi = netdev_priv(dev);
1637
1638 ethtool_cmd_speed_set(cmd, vi->speed);
1639 cmd->duplex = vi->duplex;
1640 cmd->port = PORT_OTHER;
1641
1642 return 0;
1643}
1644
1645static void virtnet_init_settings(struct net_device *dev)
1646{
1647 struct virtnet_info *vi = netdev_priv(dev);
1648
1649 vi->speed = SPEED_UNKNOWN;
1650 vi->duplex = DUPLEX_UNKNOWN;
1651}
1652
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001653static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001654 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001655 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001656 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001657 .set_channels = virtnet_set_channels,
1658 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00001659 .get_ts_info = ethtool_op_get_ts_info,
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001660 .get_settings = virtnet_get_settings,
1661 .set_settings = virtnet_set_settings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001662};
1663
John Fastabendf600b692016-12-15 12:13:24 -08001664static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1665{
1666 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1667 struct virtnet_info *vi = netdev_priv(dev);
1668 struct bpf_prog *old_prog;
John Fastabend672aafd2016-12-15 12:13:49 -08001669 u16 xdp_qp = 0, curr_qp;
1670 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001671
Jakub Kicinski529ec6a2017-01-25 14:56:36 -08001672 if (prog && prog->xdp_adjust_head) {
1673 netdev_warn(dev, "Does not support bpf_xdp_adjust_head()\n");
1674 return -EOPNOTSUPP;
1675 }
1676
John Fastabendf600b692016-12-15 12:13:24 -08001677 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001678 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1679 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1680 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001681 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1682 return -EOPNOTSUPP;
1683 }
1684
1685 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1686 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1687 return -EINVAL;
1688 }
1689
1690 if (dev->mtu > max_sz) {
1691 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1692 return -EINVAL;
1693 }
1694
John Fastabend672aafd2016-12-15 12:13:49 -08001695 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1696 if (prog)
1697 xdp_qp = nr_cpu_ids;
1698
1699 /* XDP requires extra queues for XDP_TX */
1700 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1701 netdev_warn(dev, "request %i queues but max is %i\n",
1702 curr_qp + xdp_qp, vi->max_queue_pairs);
1703 return -ENOMEM;
1704 }
1705
John Fastabend473153292017-02-02 19:14:32 -08001706 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
John Fastabend672aafd2016-12-15 12:13:49 -08001707 if (err) {
1708 dev_warn(&dev->dev, "XDP Device queue allocation failure.\n");
1709 return err;
1710 }
1711
John Fastabendf600b692016-12-15 12:13:24 -08001712 if (prog) {
1713 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
John Fastabend672aafd2016-12-15 12:13:49 -08001714 if (IS_ERR(prog)) {
John Fastabend473153292017-02-02 19:14:32 -08001715 _virtnet_set_queues(vi, curr_qp);
John Fastabendf600b692016-12-15 12:13:24 -08001716 return PTR_ERR(prog);
John Fastabend672aafd2016-12-15 12:13:49 -08001717 }
John Fastabendf600b692016-12-15 12:13:24 -08001718 }
1719
John Fastabend672aafd2016-12-15 12:13:49 -08001720 vi->xdp_queue_pairs = xdp_qp;
1721 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1722
John Fastabendf600b692016-12-15 12:13:24 -08001723 for (i = 0; i < vi->max_queue_pairs; i++) {
1724 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1725 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1726 if (old_prog)
1727 bpf_prog_put(old_prog);
1728 }
1729
1730 return 0;
1731}
1732
1733static bool virtnet_xdp_query(struct net_device *dev)
1734{
1735 struct virtnet_info *vi = netdev_priv(dev);
1736 int i;
1737
1738 for (i = 0; i < vi->max_queue_pairs; i++) {
1739 if (vi->rq[i].xdp_prog)
1740 return true;
1741 }
1742 return false;
1743}
1744
1745static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1746{
1747 switch (xdp->command) {
1748 case XDP_SETUP_PROG:
1749 return virtnet_xdp_set(dev, xdp->prog);
1750 case XDP_QUERY_PROG:
1751 xdp->prog_attached = virtnet_xdp_query(dev);
1752 return 0;
1753 default:
1754 return -EINVAL;
1755 }
1756}
1757
Stephen Hemminger76288b42009-01-06 10:44:22 -08001758static const struct net_device_ops virtnet_netdev = {
1759 .ndo_open = virtnet_open,
1760 .ndo_stop = virtnet_close,
1761 .ndo_start_xmit = start_xmit,
1762 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001763 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001764 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001765 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001766 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1767 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001768#ifdef CONFIG_NET_POLL_CONTROLLER
1769 .ndo_poll_controller = virtnet_netpoll,
1770#endif
John Fastabendf600b692016-12-15 12:13:24 -08001771 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001772};
1773
Jason Wang586d17c2012-04-11 20:43:52 +00001774static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001775{
Jason Wang586d17c2012-04-11 20:43:52 +00001776 struct virtnet_info *vi =
1777 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001778 u16 v;
1779
Rusty Russell855e0c52013-10-14 18:11:51 +10301780 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1781 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301782 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001783
1784 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001785 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001786 virtnet_ack_link_announce(vi);
1787 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001788
1789 /* Ignore unknown (future) status bits */
1790 v &= VIRTIO_NET_S_LINK_UP;
1791
1792 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301793 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001794
1795 vi->status = v;
1796
1797 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1798 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001799 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001800 } else {
1801 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001802 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001803 }
1804}
1805
1806static void virtnet_config_changed(struct virtio_device *vdev)
1807{
1808 struct virtnet_info *vi = vdev->priv;
1809
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001810 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001811}
1812
Jason Wang986a4f42012-12-07 07:04:56 +00001813static void virtnet_free_queues(struct virtnet_info *vi)
1814{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001815 int i;
1816
Jason Wangab3971b2015-03-12 13:57:44 +08001817 for (i = 0; i < vi->max_queue_pairs; i++) {
1818 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001819 netif_napi_del(&vi->rq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08001820 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001821
Eric Dumazet963abe52016-11-15 22:24:12 -08001822 /* We called napi_hash_del() before netif_napi_del(),
1823 * we need to respect an RCU grace period before freeing vi->rq
1824 */
1825 synchronize_net();
1826
Jason Wang986a4f42012-12-07 07:04:56 +00001827 kfree(vi->rq);
1828 kfree(vi->sq);
1829}
1830
John Fastabend473153292017-02-02 19:14:32 -08001831static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001832{
John Fastabendf600b692016-12-15 12:13:24 -08001833 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00001834 int i;
1835
1836 for (i = 0; i < vi->max_queue_pairs; i++) {
1837 while (vi->rq[i].pages)
1838 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08001839
1840 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1841 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1842 if (old_prog)
1843 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00001844 }
John Fastabend473153292017-02-02 19:14:32 -08001845}
1846
1847static void free_receive_bufs(struct virtnet_info *vi)
1848{
1849 rtnl_lock();
1850 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08001851 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001852}
1853
Michael Daltonfb518792014-01-16 22:23:26 -08001854static void free_receive_page_frags(struct virtnet_info *vi)
1855{
1856 int i;
1857 for (i = 0; i < vi->max_queue_pairs; i++)
1858 if (vi->rq[i].alloc_frag.page)
1859 put_page(vi->rq[i].alloc_frag.page);
1860}
1861
John Fastabendb68df012017-01-25 18:22:48 -08001862static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08001863{
John Fastabendb68df012017-01-25 18:22:48 -08001864 /* For small receive mode always use kfree_skb variants */
1865 if (!vi->mergeable_rx_bufs)
1866 return false;
1867
John Fastabend56434a02016-12-15 12:14:13 -08001868 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1869 return false;
1870 else if (q < vi->curr_queue_pairs)
1871 return true;
1872 else
1873 return false;
1874}
1875
Jason Wang986a4f42012-12-07 07:04:56 +00001876static void free_unused_bufs(struct virtnet_info *vi)
1877{
1878 void *buf;
1879 int i;
1880
1881 for (i = 0; i < vi->max_queue_pairs; i++) {
1882 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08001883 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08001884 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08001885 dev_kfree_skb(buf);
1886 else
1887 put_page(virt_to_head_page(buf));
1888 }
Jason Wang986a4f42012-12-07 07:04:56 +00001889 }
1890
1891 for (i = 0; i < vi->max_queue_pairs; i++) {
1892 struct virtqueue *vq = vi->rq[i].vq;
1893
1894 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08001895 if (vi->mergeable_rx_bufs) {
1896 unsigned long ctx = (unsigned long)buf;
1897 void *base = mergeable_ctx_to_buf_address(ctx);
1898 put_page(virt_to_head_page(base));
1899 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001900 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001901 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001902 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001903 }
Jason Wang986a4f42012-12-07 07:04:56 +00001904 }
Jason Wang986a4f42012-12-07 07:04:56 +00001905 }
1906}
1907
Jason Wange9d74172012-12-07 07:04:55 +00001908static void virtnet_del_vqs(struct virtnet_info *vi)
1909{
1910 struct virtio_device *vdev = vi->vdev;
1911
Wanlong Gao8898c212013-01-24 23:51:30 +00001912 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00001913
Jason Wange9d74172012-12-07 07:04:55 +00001914 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00001915
1916 virtnet_free_queues(vi);
1917}
1918
1919static int virtnet_find_vqs(struct virtnet_info *vi)
1920{
1921 vq_callback_t **callbacks;
1922 struct virtqueue **vqs;
1923 int ret = -ENOMEM;
1924 int i, total_vqs;
1925 const char **names;
1926
1927 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1928 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1929 * possible control vq.
1930 */
1931 total_vqs = vi->max_queue_pairs * 2 +
1932 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1933
1934 /* Allocate space for find_vqs parameters */
1935 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1936 if (!vqs)
1937 goto err_vq;
1938 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1939 if (!callbacks)
1940 goto err_callback;
1941 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1942 if (!names)
1943 goto err_names;
1944
1945 /* Parameters for control virtqueue, if any */
1946 if (vi->has_cvq) {
1947 callbacks[total_vqs - 1] = NULL;
1948 names[total_vqs - 1] = "control";
1949 }
1950
1951 /* Allocate/initialize parameters for send/receive virtqueues */
1952 for (i = 0; i < vi->max_queue_pairs; i++) {
1953 callbacks[rxq2vq(i)] = skb_recv_done;
1954 callbacks[txq2vq(i)] = skb_xmit_done;
1955 sprintf(vi->rq[i].name, "input.%d", i);
1956 sprintf(vi->sq[i].name, "output.%d", i);
1957 names[rxq2vq(i)] = vi->rq[i].name;
1958 names[txq2vq(i)] = vi->sq[i].name;
1959 }
1960
1961 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1962 names);
1963 if (ret)
1964 goto err_find;
1965
1966 if (vi->has_cvq) {
1967 vi->cvq = vqs[total_vqs - 1];
1968 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00001969 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00001970 }
1971
1972 for (i = 0; i < vi->max_queue_pairs; i++) {
1973 vi->rq[i].vq = vqs[rxq2vq(i)];
1974 vi->sq[i].vq = vqs[txq2vq(i)];
1975 }
1976
1977 kfree(names);
1978 kfree(callbacks);
1979 kfree(vqs);
1980
1981 return 0;
1982
1983err_find:
1984 kfree(names);
1985err_names:
1986 kfree(callbacks);
1987err_callback:
1988 kfree(vqs);
1989err_vq:
1990 return ret;
1991}
1992
1993static int virtnet_alloc_queues(struct virtnet_info *vi)
1994{
1995 int i;
1996
1997 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1998 if (!vi->sq)
1999 goto err_sq;
2000 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002001 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002002 goto err_rq;
2003
2004 INIT_DELAYED_WORK(&vi->refill, refill_work);
2005 for (i = 0; i < vi->max_queue_pairs; i++) {
2006 vi->rq[i].pages = NULL;
2007 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2008 napi_weight);
2009
2010 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002011 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002012 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2013 }
2014
2015 return 0;
2016
2017err_rq:
2018 kfree(vi->sq);
2019err_sq:
2020 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002021}
2022
Amit Shah3f9c10b2011-12-22 16:58:31 +05302023static int init_vqs(struct virtnet_info *vi)
2024{
Jason Wang986a4f42012-12-07 07:04:56 +00002025 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302026
Jason Wang986a4f42012-12-07 07:04:56 +00002027 /* Allocate send & receive queues */
2028 ret = virtnet_alloc_queues(vi);
2029 if (ret)
2030 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302031
Jason Wang986a4f42012-12-07 07:04:56 +00002032 ret = virtnet_find_vqs(vi);
2033 if (ret)
2034 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302035
Wanlong Gao47be2472013-01-24 23:51:29 +00002036 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002037 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002038 put_online_cpus();
2039
Amit Shah3f9c10b2011-12-22 16:58:31 +05302040 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002041
2042err_free:
2043 virtnet_free_queues(vi);
2044err:
2045 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302046}
2047
Michael Daltonfbf28d72014-01-16 22:23:30 -08002048#ifdef CONFIG_SYSFS
2049static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2050 struct rx_queue_attribute *attribute, char *buf)
2051{
2052 struct virtnet_info *vi = netdev_priv(queue->dev);
2053 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002054 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002055
2056 BUG_ON(queue_index >= vi->max_queue_pairs);
2057 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2058 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2059}
2060
2061static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2062 __ATTR_RO(mergeable_rx_buffer_size);
2063
2064static struct attribute *virtio_net_mrg_rx_attrs[] = {
2065 &mergeable_rx_buffer_size_attribute.attr,
2066 NULL
2067};
2068
2069static const struct attribute_group virtio_net_mrg_rx_group = {
2070 .name = "virtio_net",
2071 .attrs = virtio_net_mrg_rx_attrs
2072};
2073#endif
2074
Jason Wang892d6eb2014-11-20 17:03:05 +08002075static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2076 unsigned int fbit,
2077 const char *fname, const char *dname)
2078{
2079 if (!virtio_has_feature(vdev, fbit))
2080 return false;
2081
2082 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2083 fname, dname);
2084
2085 return true;
2086}
2087
2088#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2089 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2090
2091static bool virtnet_validate_features(struct virtio_device *vdev)
2092{
2093 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2094 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2095 "VIRTIO_NET_F_CTRL_VQ") ||
2096 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2097 "VIRTIO_NET_F_CTRL_VQ") ||
2098 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2099 "VIRTIO_NET_F_CTRL_VQ") ||
2100 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2101 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2102 "VIRTIO_NET_F_CTRL_VQ"))) {
2103 return false;
2104 }
2105
2106 return true;
2107}
2108
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002109#define MIN_MTU ETH_MIN_MTU
2110#define MAX_MTU ETH_MAX_MTU
2111
Rusty Russell296f96f2007-10-22 11:03:37 +10002112static int virtnet_probe(struct virtio_device *vdev)
2113{
Jason Wang986a4f42012-12-07 07:04:56 +00002114 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10002115 struct net_device *dev;
2116 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00002117 u16 max_queue_pairs;
Aaron Conole14de9d12016-06-03 16:57:12 -04002118 int mtu;
Jason Wang986a4f42012-12-07 07:04:56 +00002119
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002120 if (!vdev->config->get) {
2121 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2122 __func__);
2123 return -EINVAL;
2124 }
2125
Jason Wang892d6eb2014-11-20 17:03:05 +08002126 if (!virtnet_validate_features(vdev))
2127 return -EINVAL;
2128
Jason Wang986a4f42012-12-07 07:04:56 +00002129 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302130 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2131 struct virtio_net_config,
2132 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002133
2134 /* We need at least 2 queue's */
2135 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2136 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2137 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2138 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002139
2140 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002141 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002142 if (!dev)
2143 return -ENOMEM;
2144
2145 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002146 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002147 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002148 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002149
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002150 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002151 SET_NETDEV_DEV(dev, &vdev->dev);
2152
2153 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002154 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002155 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002156 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002157 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002158 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002159
2160 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002161 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002162 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2163 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002164 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002165 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2166 dev->hw_features |= NETIF_F_TSO;
2167 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2168 dev->hw_features |= NETIF_F_TSO6;
2169 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2170 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002171 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2172 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002173
Jason Wang41f2f122014-12-24 11:03:52 +08002174 dev->features |= NETIF_F_GSO_ROBUST;
2175
Michał Mirosław98e778c2011-03-31 01:01:35 +00002176 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002177 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002178 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002179 }
Thomas Huth4f491292013-08-27 17:09:02 +02002180 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2181 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002182
Jason Wang4fda8302013-04-10 23:32:21 +00002183 dev->vlan_features = dev->features;
2184
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002185 /* MTU range: 68 - 65535 */
2186 dev->min_mtu = MIN_MTU;
2187 dev->max_mtu = MAX_MTU;
2188
Rusty Russell296f96f2007-10-22 11:03:37 +10002189 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302190 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2191 virtio_cread_bytes(vdev,
2192 offsetof(struct virtio_net_config, mac),
2193 dev->dev_addr, dev->addr_len);
2194 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002195 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002196
2197 /* Set up our device-specific information */
2198 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002199 vi->dev = dev;
2200 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002201 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002202 vi->stats = alloc_percpu(struct virtnet_stats);
2203 err = -ENOMEM;
2204 if (vi->stats == NULL)
2205 goto free;
2206
John Stultz827da442013-10-07 15:51:58 -07002207 for_each_possible_cpu(i) {
2208 struct virtnet_stats *virtnet_stats;
2209 virtnet_stats = per_cpu_ptr(vi->stats, i);
2210 u64_stats_init(&virtnet_stats->tx_syncp);
2211 u64_stats_init(&virtnet_stats->rx_syncp);
2212 }
2213
Jason Wang586d17c2012-04-11 20:43:52 +00002214 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002215
Herbert Xu97402b92008-04-18 11:24:27 +08002216 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002217 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2218 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002219 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2220 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002221 vi->big_packets = true;
2222
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002223 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2224 vi->mergeable_rx_bufs = true;
2225
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002226 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2227 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002228 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2229 else
2230 vi->hdr_len = sizeof(struct virtio_net_hdr);
2231
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002232 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2233 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302234 vi->any_header_sg = true;
2235
Jason Wang986a4f42012-12-07 07:04:56 +00002236 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2237 vi->has_cvq = true;
2238
Aaron Conole14de9d12016-06-03 16:57:12 -04002239 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2240 mtu = virtio_cread16(vdev,
2241 offsetof(struct virtio_net_config,
2242 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002243 if (mtu < dev->min_mtu) {
Aaron Conole14de9d12016-06-03 16:57:12 -04002244 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
Aaron Conole93a205e2016-10-25 16:12:12 -04002245 } else {
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002246 dev->mtu = mtu;
Aaron Conole93a205e2016-10-25 16:12:12 -04002247 dev->max_mtu = mtu;
2248 }
Aaron Conole14de9d12016-06-03 16:57:12 -04002249 }
2250
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002251 if (vi->any_header_sg)
2252 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002253
Jason Wang44900012016-11-25 12:37:26 +08002254 /* Enable multiqueue by default */
2255 if (num_online_cpus() >= max_queue_pairs)
2256 vi->curr_queue_pairs = max_queue_pairs;
2257 else
2258 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002259 vi->max_queue_pairs = max_queue_pairs;
2260
2261 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302262 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002263 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002264 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002265
Michael Daltonfbf28d72014-01-16 22:23:30 -08002266#ifdef CONFIG_SYSFS
2267 if (vi->mergeable_rx_bufs)
2268 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2269#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08002270 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2271 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002272
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002273 virtnet_init_settings(dev);
2274
Rusty Russell296f96f2007-10-22 11:03:37 +10002275 err = register_netdev(dev);
2276 if (err) {
2277 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002278 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002279 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002280
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302281 virtio_device_ready(vdev);
2282
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002283 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002284 if (err) {
2285 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002286 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002287 }
2288
Jason Wanga2208712016-12-13 14:23:05 +08002289 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08002290
Jason Wang167c25e2010-11-10 14:45:41 +00002291 /* Assume link up if device can't report link status,
2292 otherwise get link status from config. */
2293 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2294 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002295 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002296 } else {
2297 vi->status = VIRTIO_NET_S_LINK_UP;
2298 netif_carrier_on(dev);
2299 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002300
Jason Wang986a4f42012-12-07 07:04:56 +00002301 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2302 dev->name, max_queue_pairs);
2303
Rusty Russell296f96f2007-10-22 11:03:37 +10002304 return 0;
2305
wangyunjianf00e35e2016-05-31 11:52:43 +08002306free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302307 vi->vdev->config->reset(vdev);
2308
Rusty Russellb3369c12008-02-04 23:50:02 -05002309 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002310free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002311 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002312 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002313 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002314free_stats:
2315 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002316free:
2317 free_netdev(dev);
2318 return err;
2319}
2320
Amit Shah04486ed2011-12-22 16:58:32 +05302321static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002322{
Amit Shah04486ed2011-12-22 16:58:32 +05302323 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002324
2325 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002326 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002327
Jason Wang986a4f42012-12-07 07:04:56 +00002328 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002329
Michael Daltonfb518792014-01-16 22:23:26 -08002330 free_receive_page_frags(vi);
2331
Jason Wang986a4f42012-12-07 07:04:56 +00002332 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302333}
2334
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002335static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302336{
2337 struct virtnet_info *vi = vdev->priv;
2338
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002339 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002340
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302341 /* Make sure no work handler is accessing the device. */
2342 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002343
Amit Shah04486ed2011-12-22 16:58:32 +05302344 unregister_netdev(vi->dev);
2345
2346 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002347
Krishna Kumar2e66f552011-07-20 03:56:02 +00002348 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002349 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002350}
2351
Aaron Lu89107002013-09-17 09:25:23 +09302352#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302353static int virtnet_freeze(struct virtio_device *vdev)
2354{
2355 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002356 int i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302357
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002358 virtnet_cpu_notif_remove(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002359
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302360 /* Make sure no work handler is accessing the device */
2361 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002362
Amit Shah0741bcb2011-12-22 16:58:33 +05302363 netif_device_detach(vi->dev);
2364 cancel_delayed_work_sync(&vi->refill);
2365
Jason Wang91815632014-07-23 16:33:55 +08002366 if (netif_running(vi->dev)) {
Jason Wangab3971b2015-03-12 13:57:44 +08002367 for (i = 0; i < vi->max_queue_pairs; i++)
Jason Wang986a4f42012-12-07 07:04:56 +00002368 napi_disable(&vi->rq[i].napi);
Jason Wang91815632014-07-23 16:33:55 +08002369 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302370
2371 remove_vq_common(vi);
2372
2373 return 0;
2374}
2375
2376static int virtnet_restore(struct virtio_device *vdev)
2377{
2378 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002379 int err, i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302380
2381 err = init_vqs(vi);
2382 if (err)
2383 return err;
2384
Michael S. Tsirkine53fbd12014-10-15 10:22:32 +10302385 virtio_device_ready(vdev);
2386
Jason Wang6cd4ce02013-12-30 11:34:40 +08002387 if (netif_running(vi->dev)) {
2388 for (i = 0; i < vi->curr_queue_pairs; i++)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03002389 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wang6cd4ce02013-12-30 11:34:40 +08002390 schedule_delayed_work(&vi->refill, 0);
2391
Jason Wang986a4f42012-12-07 07:04:56 +00002392 for (i = 0; i < vi->max_queue_pairs; i++)
2393 virtnet_napi_enable(&vi->rq[i]);
Jason Wang6cd4ce02013-12-30 11:34:40 +08002394 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302395
2396 netif_device_attach(vi->dev);
2397
Jason Wang986a4f42012-12-07 07:04:56 +00002398 virtnet_set_queues(vi, vi->curr_queue_pairs);
2399
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002400 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002401 if (err)
2402 return err;
2403
Amit Shah0741bcb2011-12-22 16:58:33 +05302404 return 0;
2405}
2406#endif
2407
Rusty Russell296f96f2007-10-22 11:03:37 +10002408static struct virtio_device_id id_table[] = {
2409 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2410 { 0 },
2411};
2412
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002413#define VIRTNET_FEATURES \
2414 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2415 VIRTIO_NET_F_MAC, \
2416 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2417 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2418 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2419 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2420 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2421 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2422 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2423 VIRTIO_NET_F_MTU
2424
Rusty Russellc45a6812008-05-02 21:50:50 -05002425static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002426 VIRTNET_FEATURES,
2427};
2428
2429static unsigned int features_legacy[] = {
2430 VIRTNET_FEATURES,
2431 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302432 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002433};
2434
Uwe Kleine-König22402522009-11-05 01:32:44 -08002435static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002436 .feature_table = features,
2437 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002438 .feature_table_legacy = features_legacy,
2439 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002440 .driver.name = KBUILD_MODNAME,
2441 .driver.owner = THIS_MODULE,
2442 .id_table = id_table,
2443 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002444 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002445 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302446#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302447 .freeze = virtnet_freeze,
2448 .restore = virtnet_restore,
2449#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002450};
2451
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002452static __init int virtio_net_driver_init(void)
2453{
2454 int ret;
2455
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002456 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002457 virtnet_cpu_online,
2458 virtnet_cpu_down_prep);
2459 if (ret < 0)
2460 goto out;
2461 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002462 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002463 NULL, virtnet_cpu_dead);
2464 if (ret)
2465 goto err_dead;
2466
2467 ret = register_virtio_driver(&virtio_net_driver);
2468 if (ret)
2469 goto err_virtio;
2470 return 0;
2471err_virtio:
2472 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2473err_dead:
2474 cpuhp_remove_multi_state(virtionet_online);
2475out:
2476 return ret;
2477}
2478module_init(virtio_net_driver_init);
2479
2480static __exit void virtio_net_driver_exit(void)
2481{
2482 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2483 cpuhp_remove_multi_state(virtionet_online);
2484 unregister_virtio_driver(&virtio_net_driver);
2485}
2486module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002487
2488MODULE_DEVICE_TABLE(virtio, id_table);
2489MODULE_DESCRIPTION("Virtio network driver");
2490MODULE_LICENSE("GPL");