blob: bd22cf306a9267cecf8c7ce03dccff8ebd16089e [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Rusty Russell296f96f2007-10-22 11:03:37 +100017 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080021#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100022#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
John Fastabendf600b692016-12-15 12:13:24 -080025#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010026#include <linux/bpf_trace.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100027#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080028#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000030#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080031#include <linux/average.h>
Jason Wang91815632014-07-23 16:33:55 +080032#include <net/busy_poll.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100033
Amerigo Wangd34710e2013-05-09 19:50:51 +000034static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020035module_param(napi_weight, int, 0444);
36
Rusty Russelleb939922011-12-19 14:08:01 +000037static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050038module_param(csum, bool, 0444);
39module_param(gso, bool, 0444);
40
Rusty Russell296f96f2007-10-22 11:03:37 +100041/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080042#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080043#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100044
Johannes Berg5377d7582015-08-19 09:48:40 +020045/* RX packet size EWMA. The average packet size is used to determine the packet
46 * buffer size when refilling RX rings. As the entire RX ring may be refilled
47 * at once, the weight is chosen so that the EWMA will be insensitive to short-
48 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080049 */
Johannes Berg5377d7582015-08-19 09:48:40 +020050DECLARE_EWMA(pkt_len, 1, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080051
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020052/* With mergeable buffers we align buffer address and use the low bits to
53 * encode its true size. Buffer size is up to 1 page so we need to align to
54 * square root of page size to ensure we reserve enough bits to encode the true
55 * size.
56 */
57#define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
58
Michael Daltonab7db912014-01-16 22:23:27 -080059/* Minimum alignment for mergeable packet buffers. */
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020060#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
61 1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
Michael Daltonab7db912014-01-16 22:23:27 -080062
Rick Jones66846042011-11-14 14:17:08 +000063#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000064
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000065struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000066 struct u64_stats_sync tx_syncp;
67 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000068 u64 tx_bytes;
69 u64 tx_packets;
70
71 u64 rx_bytes;
72 u64 rx_packets;
73};
74
Jason Wange9d74172012-12-07 07:04:55 +000075/* Internal representation of a send virtqueue */
76struct send_queue {
77 /* Virtqueue associated with this send _queue */
78 struct virtqueue *vq;
79
80 /* TX: fragments + linear part + virtio header */
81 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000082
83 /* Name of the send queue: output.$index */
84 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000085};
86
87/* Internal representation of a receive virtqueue */
88struct receive_queue {
89 /* Virtqueue associated with this receive_queue */
90 struct virtqueue *vq;
91
Rusty Russell296f96f2007-10-22 11:03:37 +100092 struct napi_struct napi;
93
John Fastabendf600b692016-12-15 12:13:24 -080094 struct bpf_prog __rcu *xdp_prog;
95
Jason Wange9d74172012-12-07 07:04:55 +000096 /* Chain pages by the private ptr. */
97 struct page *pages;
98
Michael Daltonab7db912014-01-16 22:23:27 -080099 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200100 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800101
Michael Daltonfb518792014-01-16 22:23:26 -0800102 /* Page frag for packet buffer allocation. */
103 struct page_frag alloc_frag;
104
Jason Wange9d74172012-12-07 07:04:55 +0000105 /* RX: fragments + linear part + virtio header */
106 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000107
108 /* Name of this receive queue: input.$index */
109 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +0000110};
111
112struct virtnet_info {
113 struct virtio_device *vdev;
114 struct virtqueue *cvq;
115 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000116 struct send_queue *sq;
117 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000118 unsigned int status;
119
Jason Wang986a4f42012-12-07 07:04:56 +0000120 /* Max # of queue pairs supported by the device */
121 u16 max_queue_pairs;
122
123 /* # of queue pairs currently used by the driver */
124 u16 curr_queue_pairs;
125
John Fastabend672aafd2016-12-15 12:13:49 -0800126 /* # of XDP queue pairs currently used by the driver */
127 u16 xdp_queue_pairs;
128
Herbert Xu97402b92008-04-18 11:24:27 +0800129 /* I like... big packets and I cannot lie! */
130 bool big_packets;
131
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800132 /* Host will merge rx buffers for big packets (shake it! shake it!) */
133 bool mergeable_rx_bufs;
134
Jason Wang986a4f42012-12-07 07:04:56 +0000135 /* Has control virtqueue */
136 bool has_cvq;
137
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930138 /* Host can handle any s/g split between our header and packet data */
139 bool any_header_sg;
140
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300141 /* Packet virtio header size */
142 u8 hdr_len;
143
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000144 /* Active statistics */
145 struct virtnet_stats __percpu *stats;
146
Rusty Russell3161e452009-08-26 12:22:32 -0700147 /* Work struct for refilling if we run low on memory. */
148 struct delayed_work refill;
149
Jason Wang586d17c2012-04-11 20:43:52 +0000150 /* Work struct for config space updates */
151 struct work_struct config_work;
152
Jason Wang986a4f42012-12-07 07:04:56 +0000153 /* Does the affinity hint is set for virtqueues? */
154 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000155
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200156 /* CPU hotplug instances for online & dead */
157 struct hlist_node node;
158 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200159
160 /* Control VQ buffers: protected by the rtnl lock */
161 struct virtio_net_ctrl_hdr ctrl_hdr;
162 virtio_net_ctrl_ack ctrl_status;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700163 struct virtio_net_ctrl_mq ctrl_mq;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200164 u8 ctrl_promisc;
165 u8 ctrl_allmulti;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700166 u16 ctrl_vid;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100167
168 /* Ethtool settings */
169 u8 duplex;
170 u32 speed;
Rusty Russell296f96f2007-10-22 11:03:37 +1000171};
172
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000173struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300174 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000175 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300176 * hdr is in a separate sg buffer, and data sg buffer shares same page
177 * with this header sg. This padding makes next sg 16 byte aligned
178 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000179 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300180 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000181};
182
Jason Wang986a4f42012-12-07 07:04:56 +0000183/* Converting between virtqueue no. and kernel tx/rx queue no.
184 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
185 */
186static int vq2txq(struct virtqueue *vq)
187{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000188 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000189}
190
191static int txq2vq(int txq)
192{
193 return txq * 2 + 1;
194}
195
196static int vq2rxq(struct virtqueue *vq)
197{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000198 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000199}
200
201static int rxq2vq(int rxq)
202{
203 return rxq * 2;
204}
205
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300206static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000207{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300208 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000209}
210
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000211/*
212 * private is used to chain pages for big packets, put the whole
213 * most recent used list in the beginning for reuse
214 */
Jason Wange9d74172012-12-07 07:04:55 +0000215static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500216{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000217 struct page *end;
218
Jason Wange9d74172012-12-07 07:04:55 +0000219 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000220 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000221 end->private = (unsigned long)rq->pages;
222 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500223}
224
Jason Wange9d74172012-12-07 07:04:55 +0000225static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500226{
Jason Wange9d74172012-12-07 07:04:55 +0000227 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500228
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000229 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000230 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000231 /* clear private here, it is used to chain pages */
232 p->private = 0;
233 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500234 p = alloc_page(gfp_mask);
235 return p;
236}
237
Jason Wange9d74172012-12-07 07:04:55 +0000238static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000239{
Jason Wange9d74172012-12-07 07:04:55 +0000240 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000241
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500242 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000243 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000244
Rusty Russell363f1512008-06-08 20:51:55 +1000245 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000246 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000247}
248
Michael Daltonab7db912014-01-16 22:23:27 -0800249static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
250{
251 unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
252 return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
253}
254
255static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
256{
257 return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
258
259}
260
261static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
262{
263 unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
264 return (unsigned long)buf | (size - 1);
265}
266
Mike Waychison34646452012-01-04 12:52:32 +0000267/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300268static struct sk_buff *page_to_skb(struct virtnet_info *vi,
269 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700270 struct page *page, unsigned int offset,
271 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000272{
273 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300274 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700275 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000276 char *p;
277
Michael Dalton2613af02013-10-28 15:44:18 -0700278 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000279
280 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100281 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000282 if (unlikely(!skb))
283 return NULL;
284
285 hdr = skb_vnet_hdr(skb);
286
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300287 hdr_len = vi->hdr_len;
288 if (vi->mergeable_rx_bufs)
289 hdr_padded_len = sizeof *hdr;
290 else
Michael Dalton2613af02013-10-28 15:44:18 -0700291 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000292
293 memcpy(hdr, p, hdr_len);
294
295 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700296 offset += hdr_padded_len;
297 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000298
299 copy = len;
300 if (copy > skb_tailroom(skb))
301 copy = skb_tailroom(skb);
302 memcpy(skb_put(skb, copy), p, copy);
303
304 len -= copy;
305 offset += copy;
306
Michael Dalton2613af02013-10-28 15:44:18 -0700307 if (vi->mergeable_rx_bufs) {
308 if (len)
309 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
310 else
311 put_page(page);
312 return skb;
313 }
314
Sasha Levine878d782011-09-28 04:40:54 +0000315 /*
316 * Verify that we can indeed put this data into a skb.
317 * This is here to handle cases when the device erroneously
318 * tries to receive more than is possible. This is usually
319 * the case of a broken device.
320 */
321 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000322 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000323 dev_kfree_skb(skb);
324 return NULL;
325 }
Michael Dalton2613af02013-10-28 15:44:18 -0700326 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000327 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700328 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
329 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
330 frag_size, truesize);
331 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000332 page = (struct page *)page->private;
333 offset = 0;
334 }
335
336 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000337 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000338
339 return skb;
340}
341
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100342static bool virtnet_xdp_xmit(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800343 struct receive_queue *rq,
344 struct send_queue *sq,
Jason Wangbb91acc2016-12-23 22:37:32 +0800345 struct xdp_buff *xdp,
346 void *data)
John Fastabend56434a02016-12-15 12:14:13 -0800347{
John Fastabend56434a02016-12-15 12:14:13 -0800348 struct virtio_net_hdr_mrg_rxbuf *hdr;
349 unsigned int num_sg, len;
350 void *xdp_sent;
351 int err;
352
353 /* Free up any pending old buffers before queueing new ones. */
354 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800355 if (vi->mergeable_rx_bufs) {
356 struct page *sent_page = virt_to_head_page(xdp_sent);
357
358 put_page(sent_page);
359 } else { /* small buffer */
360 struct sk_buff *skb = xdp_sent;
361
362 kfree_skb(skb);
363 }
John Fastabend56434a02016-12-15 12:14:13 -0800364 }
365
Jason Wangbb91acc2016-12-23 22:37:32 +0800366 if (vi->mergeable_rx_bufs) {
367 /* Zero header and leave csum up to XDP layers */
368 hdr = xdp->data;
369 memset(hdr, 0, vi->hdr_len);
John Fastabend56434a02016-12-15 12:14:13 -0800370
Jason Wangbb91acc2016-12-23 22:37:32 +0800371 num_sg = 1;
372 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
373 } else { /* small buffer */
374 struct sk_buff *skb = data;
375
376 /* Zero header and leave csum up to XDP layers */
377 hdr = skb_vnet_hdr(skb);
378 memset(hdr, 0, vi->hdr_len);
379
380 num_sg = 2;
381 sg_init_table(sq->sg, 2);
382 sg_set_buf(sq->sg, hdr, vi->hdr_len);
383 skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
384 }
John Fastabend56434a02016-12-15 12:14:13 -0800385 err = virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
Jason Wangbb91acc2016-12-23 22:37:32 +0800386 data, GFP_ATOMIC);
John Fastabend56434a02016-12-15 12:14:13 -0800387 if (unlikely(err)) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800388 if (vi->mergeable_rx_bufs) {
389 struct page *page = virt_to_head_page(xdp->data);
390
391 put_page(page);
392 } else /* small buffer */
393 kfree_skb(data);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100394 /* On error abort to avoid unnecessary kick */
395 return false;
John Fastabend56434a02016-12-15 12:14:13 -0800396 }
397
398 virtqueue_kick(sq->vq);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100399 return true;
John Fastabend56434a02016-12-15 12:14:13 -0800400}
401
John Fastabendf600b692016-12-15 12:13:24 -0800402static u32 do_xdp_prog(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800403 struct receive_queue *rq,
John Fastabendf600b692016-12-15 12:13:24 -0800404 struct bpf_prog *xdp_prog,
Jason Wangbb91acc2016-12-23 22:37:32 +0800405 void *data, int len)
John Fastabendf600b692016-12-15 12:13:24 -0800406{
407 int hdr_padded_len;
408 struct xdp_buff xdp;
Jason Wangbb91acc2016-12-23 22:37:32 +0800409 void *buf;
John Fastabend56434a02016-12-15 12:14:13 -0800410 unsigned int qp;
John Fastabendf600b692016-12-15 12:13:24 -0800411 u32 act;
John Fastabendf600b692016-12-15 12:13:24 -0800412
Jason Wangbb91acc2016-12-23 22:37:32 +0800413 if (vi->mergeable_rx_bufs) {
John Fastabendf600b692016-12-15 12:13:24 -0800414 hdr_padded_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Jason Wangbb91acc2016-12-23 22:37:32 +0800415 xdp.data = data + hdr_padded_len;
416 xdp.data_end = xdp.data + (len - vi->hdr_len);
417 buf = data;
418 } else { /* small buffers */
419 struct sk_buff *skb = data;
John Fastabendf600b692016-12-15 12:13:24 -0800420
Jason Wangbb91acc2016-12-23 22:37:32 +0800421 xdp.data = skb->data;
422 xdp.data_end = xdp.data + len;
423 buf = skb->data;
424 }
John Fastabendf600b692016-12-15 12:13:24 -0800425
426 act = bpf_prog_run_xdp(xdp_prog, &xdp);
427 switch (act) {
428 case XDP_PASS:
429 return XDP_PASS;
John Fastabend56434a02016-12-15 12:14:13 -0800430 case XDP_TX:
431 qp = vi->curr_queue_pairs -
432 vi->xdp_queue_pairs +
433 smp_processor_id();
Jason Wangbb91acc2016-12-23 22:37:32 +0800434 xdp.data = buf;
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100435 if (unlikely(!virtnet_xdp_xmit(vi, rq, &vi->sq[qp], &xdp,
436 data)))
437 trace_xdp_exception(vi->dev, xdp_prog, act);
John Fastabend56434a02016-12-15 12:14:13 -0800438 return XDP_TX;
John Fastabendf600b692016-12-15 12:13:24 -0800439 default:
440 bpf_warn_invalid_xdp_action(act);
John Fastabendf600b692016-12-15 12:13:24 -0800441 case XDP_ABORTED:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100442 trace_xdp_exception(vi->dev, xdp_prog, act);
John Fastabendf600b692016-12-15 12:13:24 -0800443 case XDP_DROP:
444 return XDP_DROP;
445 }
446}
447
Jason Wangbb91acc2016-12-23 22:37:32 +0800448static struct sk_buff *receive_small(struct net_device *dev,
449 struct virtnet_info *vi,
450 struct receive_queue *rq,
451 void *buf, unsigned int len)
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200452{
453 struct sk_buff * skb = buf;
Jason Wangbb91acc2016-12-23 22:37:32 +0800454 struct bpf_prog *xdp_prog;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200455
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300456 len -= vi->hdr_len;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200457 skb_trim(skb, len);
458
Jason Wangbb91acc2016-12-23 22:37:32 +0800459 rcu_read_lock();
460 xdp_prog = rcu_dereference(rq->xdp_prog);
461 if (xdp_prog) {
462 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
463 u32 act;
464
465 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
466 goto err_xdp;
467 act = do_xdp_prog(vi, rq, xdp_prog, skb, len);
468 switch (act) {
469 case XDP_PASS:
470 break;
471 case XDP_TX:
472 rcu_read_unlock();
473 goto xdp_xmit;
474 case XDP_DROP:
475 default:
476 goto err_xdp;
477 }
478 }
479 rcu_read_unlock();
480
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200481 return skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800482
483err_xdp:
484 rcu_read_unlock();
485 dev->stats.rx_dropped++;
486 kfree_skb(skb);
487xdp_xmit:
488 return NULL;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200489}
490
491static struct sk_buff *receive_big(struct net_device *dev,
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300492 struct virtnet_info *vi,
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200493 struct receive_queue *rq,
494 void *buf,
495 unsigned int len)
496{
497 struct page *page = buf;
Jason Wangc47a43d2016-12-23 22:37:31 +0800498 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200499
500 if (unlikely(!skb))
501 goto err;
502
503 return skb;
504
505err:
506 dev->stats.rx_dropped++;
507 give_pages(rq, page);
508 return NULL;
509}
510
John Fastabend72979a62016-12-15 12:14:36 -0800511/* The conditions to enable XDP should preclude the underlying device from
512 * sending packets across multiple buffers (num_buf > 1). However per spec
513 * it does not appear to be illegal to do so but rather just against convention.
514 * So in order to avoid making a system unresponsive the packets are pushed
515 * into a page and the XDP program is run. This will be extremely slow and we
516 * push a warning to the user to fix this as soon as possible. Fixing this may
517 * require resolving the underlying hardware to determine why multiple buffers
518 * are being received or simply loading the XDP program in the ingress stack
519 * after the skb is built because there is no advantage to running it here
520 * anymore.
521 */
522static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800523 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800524 struct page *p,
525 int offset,
526 unsigned int *len)
527{
528 struct page *page = alloc_page(GFP_ATOMIC);
529 unsigned int page_off = 0;
530
531 if (!page)
532 return NULL;
533
534 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
535 page_off += *len;
536
Jason Wang56a86f82016-12-23 22:37:26 +0800537 while (--*num_buf) {
John Fastabend72979a62016-12-15 12:14:36 -0800538 unsigned int buflen;
539 unsigned long ctx;
540 void *buf;
541 int off;
542
543 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
544 if (unlikely(!ctx))
545 goto err_buf;
546
John Fastabend72979a62016-12-15 12:14:36 -0800547 buf = mergeable_ctx_to_buf_address(ctx);
548 p = virt_to_head_page(buf);
549 off = buf - page_address(p);
550
Jason Wang56a86f82016-12-23 22:37:26 +0800551 /* guard against a misconfigured or uncooperative backend that
552 * is sending packet larger than the MTU.
553 */
554 if ((page_off + buflen) > PAGE_SIZE) {
555 put_page(p);
556 goto err_buf;
557 }
558
John Fastabend72979a62016-12-15 12:14:36 -0800559 memcpy(page_address(page) + page_off,
560 page_address(p) + off, buflen);
561 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800562 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800563 }
564
565 *len = page_off;
566 return page;
567err_buf:
568 __free_pages(page, 0);
569 return NULL;
570}
571
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200572static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200573 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200574 struct receive_queue *rq,
Michael Daltonab7db912014-01-16 22:23:27 -0800575 unsigned long ctx,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200576 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000577{
Michael Daltonab7db912014-01-16 22:23:27 -0800578 void *buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300579 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
580 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200581 struct page *page = virt_to_head_page(buf);
582 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800583 struct sk_buff *head_skb, *curr_skb;
584 struct bpf_prog *xdp_prog;
585 unsigned int truesize;
Michael Daltonab7db912014-01-16 22:23:27 -0800586
John Fastabend56434a02016-12-15 12:14:13 -0800587 head_skb = NULL;
588
John Fastabendf600b692016-12-15 12:13:24 -0800589 rcu_read_lock();
590 xdp_prog = rcu_dereference(rq->xdp_prog);
591 if (xdp_prog) {
John Fastabend72979a62016-12-15 12:14:36 -0800592 struct page *xdp_page;
John Fastabendf600b692016-12-15 12:13:24 -0800593 u32 act;
594
Jason Wang73b62bd2016-12-23 22:37:24 +0800595 /* This happens when rx buffer size is underestimated */
John Fastabendf600b692016-12-15 12:13:24 -0800596 if (unlikely(num_buf > 1)) {
John Fastabend72979a62016-12-15 12:14:36 -0800597 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800598 xdp_page = xdp_linearize_page(rq, &num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800599 page, offset, &len);
600 if (!xdp_page)
601 goto err_xdp;
602 offset = 0;
603 } else {
604 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800605 }
606
607 /* Transient failure which in theory could occur if
608 * in-flight packets from before XDP was enabled reach
609 * the receive path after XDP is loaded. In practice I
610 * was not able to create this condition.
611 */
Jason Wangb00f70b2016-12-23 22:37:28 +0800612 if (unlikely(hdr->hdr.gso_type))
John Fastabendf600b692016-12-15 12:13:24 -0800613 goto err_xdp;
614
Jason Wangbb91acc2016-12-23 22:37:32 +0800615 act = do_xdp_prog(vi, rq, xdp_prog,
616 page_address(xdp_page) + offset, len);
John Fastabend56434a02016-12-15 12:14:13 -0800617 switch (act) {
618 case XDP_PASS:
Jason Wang1830f892016-12-23 22:37:27 +0800619 /* We can only create skb based on xdp_page. */
620 if (unlikely(xdp_page != page)) {
621 rcu_read_unlock();
622 put_page(page);
623 head_skb = page_to_skb(vi, rq, xdp_page,
624 0, len, PAGE_SIZE);
Jason Wang5c334742016-12-23 22:37:29 +0800625 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
Jason Wang1830f892016-12-23 22:37:27 +0800626 return head_skb;
627 }
John Fastabend56434a02016-12-15 12:14:13 -0800628 break;
629 case XDP_TX:
Jason Wang5c334742016-12-23 22:37:29 +0800630 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabend72979a62016-12-15 12:14:36 -0800631 if (unlikely(xdp_page != page))
632 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800633 rcu_read_unlock();
634 goto xdp_xmit;
635 case XDP_DROP:
636 default:
John Fastabend72979a62016-12-15 12:14:36 -0800637 if (unlikely(xdp_page != page))
638 __free_pages(xdp_page, 0);
Jason Wang5c334742016-12-23 22:37:29 +0800639 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabendf600b692016-12-15 12:13:24 -0800640 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800641 }
John Fastabendf600b692016-12-15 12:13:24 -0800642 }
643 rcu_read_unlock();
644
645 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
646 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
647 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000648
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200649 if (unlikely(!curr_skb))
650 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000651 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200652 int num_skb_frags;
653
Michael Daltonab7db912014-01-16 22:23:27 -0800654 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
655 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200656 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200657 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300658 virtio16_to_cpu(vi->vdev,
659 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200660 dev->stats.rx_length_errors++;
661 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000662 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200663
Michael Daltonab7db912014-01-16 22:23:27 -0800664 buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200665 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200666
667 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700668 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
669 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200670
671 if (unlikely(!nskb))
672 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700673 if (curr_skb == head_skb)
674 skb_shinfo(curr_skb)->frag_list = nskb;
675 else
676 curr_skb->next = nskb;
677 curr_skb = nskb;
678 head_skb->truesize += nskb->truesize;
679 num_skb_frags = 0;
680 }
Michael Daltonab7db912014-01-16 22:23:27 -0800681 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
Michael Dalton2613af02013-10-28 15:44:18 -0700682 if (curr_skb != head_skb) {
683 head_skb->data_len += len;
684 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800685 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700686 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200687 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800688 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
689 put_page(page);
690 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800691 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800692 } else {
693 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800694 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800695 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200696 }
697
Johannes Berg5377d7582015-08-19 09:48:40 +0200698 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200699 return head_skb;
700
John Fastabendf600b692016-12-15 12:13:24 -0800701err_xdp:
702 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200703err_skb:
704 put_page(page);
705 while (--num_buf) {
Michael Daltonab7db912014-01-16 22:23:27 -0800706 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
707 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200708 pr_debug("%s: rx error: %d buffers missing\n",
709 dev->name, num_buf);
710 dev->stats.rx_length_errors++;
711 break;
712 }
Michael Daltonab7db912014-01-16 22:23:27 -0800713 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200714 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000715 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200716err_buf:
717 dev->stats.rx_dropped++;
718 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800719xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200720 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000721}
722
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300723static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
724 void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000725{
Jason Wange9d74172012-12-07 07:04:55 +0000726 struct net_device *dev = vi->dev;
Eric Dumazet58472a72012-02-13 06:53:41 +0000727 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000728 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300729 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000730
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300731 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000732 pr_debug("%s: short packet %i\n", dev->name, len);
733 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800734 if (vi->mergeable_rx_bufs) {
735 unsigned long ctx = (unsigned long)buf;
736 void *base = mergeable_ctx_to_buf_address(ctx);
737 put_page(virt_to_head_page(base));
738 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800739 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800740 } else {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000741 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800742 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000743 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000744 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000745
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200746 if (vi->mergeable_rx_bufs)
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200747 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200748 else if (vi->big_packets)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300749 skb = receive_big(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200750 else
Jason Wangbb91acc2016-12-23 22:37:32 +0800751 skb = receive_small(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200752
753 if (unlikely(!skb))
754 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800755
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000756 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000757
Eric Dumazet83a27052012-06-05 22:35:24 +0000758 u64_stats_update_begin(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000759 stats->rx_bytes += skb->len;
760 stats->rx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000761 u64_stats_update_end(&stats->rx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +1000762
Mike Rapoporte858fae2016-06-08 16:09:21 +0300763 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +0000764 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000765
Mike Rapoporte858fae2016-06-08 16:09:21 +0300766 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
767 virtio_is_little_endian(vi->vdev))) {
768 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
769 dev->name, hdr->hdr.gso_type,
770 hdr->hdr.gso_size);
771 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000772 }
773
Mike Rapoportd1dc06d2016-06-14 08:29:38 +0300774 skb->protocol = eth_type_trans(skb, dev);
775 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
776 ntohs(skb->protocol), skb->len, skb->pkt_type);
777
Eric Dumazet0fbd0502015-07-31 18:25:17 +0200778 napi_gro_receive(&rq->napi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000779 return;
780
781frame_err:
782 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000783 dev_kfree_skb(skb);
784}
785
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300786static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
787 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000788{
789 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300790 struct virtio_net_hdr_mrg_rxbuf *hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000791 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000792
Michael Dalton5061de32013-11-14 10:41:04 -0800793 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000794 if (unlikely(!skb))
795 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800796
Michael Dalton5061de32013-11-14 10:41:04 -0800797 skb_put(skb, GOOD_PACKET_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000798
799 hdr = skb_vnet_hdr(skb);
Jason Wang547c8902015-08-27 14:53:06 +0800800 sg_init_table(rq->sg, 2);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300801 sg_set_buf(rq->sg, hdr, vi->hdr_len);
Jason Wange9d74172012-12-07 07:04:55 +0000802 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000803
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030804 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000805 if (err < 0)
806 dev_kfree_skb(skb);
807
808 return err;
809}
810
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300811static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
812 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000813{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000814 struct page *first, *list = NULL;
815 char *p;
816 int i, err, offset;
817
Rusty Russella5835442014-09-11 10:17:36 +0930818 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
819
Jason Wange9d74172012-12-07 07:04:55 +0000820 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000821 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000822 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000823 if (!first) {
824 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000825 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000826 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700827 }
Jason Wange9d74172012-12-07 07:04:55 +0000828 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000829
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000830 /* chain new page in list head to match sg */
831 first->private = (unsigned long)list;
832 list = first;
833 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800834
Jason Wange9d74172012-12-07 07:04:55 +0000835 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000836 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000837 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000838 return -ENOMEM;
839 }
840 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800841
Jason Wange9d74172012-12-07 07:04:55 +0000842 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300843 /* a separated rq->sg[0] for header - required in case !any_header_sg */
844 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +0800845
Jason Wange9d74172012-12-07 07:04:55 +0000846 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000847 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000848 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800849
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000850 /* chain first in list head */
851 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030852 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
853 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000854 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000855 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800856
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000857 return err;
858}
Herbert Xu97402b92008-04-18 11:24:27 +0800859
Johannes Berg5377d7582015-08-19 09:48:40 +0200860static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000861{
Michael Daltonab7db912014-01-16 22:23:27 -0800862 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800863 unsigned int len;
864
Johannes Berg5377d7582015-08-19 09:48:40 +0200865 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael Daltonfbf28d72014-01-16 22:23:30 -0800866 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
867 return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
868}
869
870static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
871{
Michael Daltonfb518792014-01-16 22:23:26 -0800872 struct page_frag *alloc_frag = &rq->alloc_frag;
873 char *buf;
Michael Daltonab7db912014-01-16 22:23:27 -0800874 unsigned long ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000875 int err;
Michael Daltonfb518792014-01-16 22:23:26 -0800876 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +1000877
Michael Daltonfbf28d72014-01-16 22:23:30 -0800878 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
Michael Daltonab7db912014-01-16 22:23:27 -0800879 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000880 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -0800881
Michael Daltonfb518792014-01-16 22:23:26 -0800882 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800883 ctx = mergeable_buf_to_ctx(buf, len);
Michael Daltonfb518792014-01-16 22:23:26 -0800884 get_page(alloc_frag->page);
Michael Daltonfb518792014-01-16 22:23:26 -0800885 alloc_frag->offset += len;
886 hole = alloc_frag->size - alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800887 if (hole < len) {
888 /* To avoid internal fragmentation, if there is very likely not
889 * enough space for another buffer, add the remaining space to
890 * the current buffer. This extra space is not included in
891 * the truesize stored in ctx.
892 */
Michael Daltonfb518792014-01-16 22:23:26 -0800893 len += hole;
894 alloc_frag->offset += hole;
895 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000896
Michael Daltonfb518792014-01-16 22:23:26 -0800897 sg_init_one(rq->sg, buf, len);
Michael Daltonab7db912014-01-16 22:23:27 -0800898 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000899 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700900 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000901
902 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000903}
904
Rusty Russellb2baed62011-12-29 00:42:38 +0000905/*
906 * Returns false if we couldn't fill entirely (OOM).
907 *
908 * Normally run in the receive path, but can also be run from ndo_open
909 * before we're receiving packets, or from refill_work which is
910 * careful to disable receiving (using napi_disable).
911 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300912static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
913 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800914{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800915 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000916 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800917
Michael Daltonfb518792014-01-16 22:23:26 -0800918 gfp |= __GFP_COLD;
Amit Shah0aea51c2009-08-26 14:58:28 +0530919 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000920 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000921 err = add_recvbuf_mergeable(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000922 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300923 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000924 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300925 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800926
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000927 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030928 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800929 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800930 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +0800931 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700932 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800933}
934
Rusty Russell18445c42008-02-04 23:49:57 -0500935static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000936{
937 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000938 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000939
Rusty Russell18445c42008-02-04 23:49:57 -0500940 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000941 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300942 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000943 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500944 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000945}
946
Jason Wange9d74172012-12-07 07:04:55 +0000947static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800948{
Jason Wange9d74172012-12-07 07:04:55 +0000949 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800950
951 /* If all buffers were filled by other side before we napi_enabled, we
952 * won't get another interrupt, so process any outstanding packets
953 * now. virtnet_poll wants re-enable the queue, so we disable here.
954 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000955 if (napi_schedule_prep(&rq->napi)) {
956 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300957 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000958 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300959 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800960 }
961}
962
Rusty Russell3161e452009-08-26 12:22:32 -0700963static void refill_work(struct work_struct *work)
964{
Jason Wange9d74172012-12-07 07:04:55 +0000965 struct virtnet_info *vi =
966 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700967 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000968 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700969
Sasha Levin55257d72013-04-29 12:00:08 +0930970 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000971 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700972
Jason Wang986a4f42012-12-07 07:04:56 +0000973 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300974 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +0000975 virtnet_napi_enable(rq);
976
977 /* In theory, this can happen: if we don't get any buffers in
978 * we will *never* try to fill again.
979 */
980 if (still_empty)
981 schedule_delayed_work(&vi->refill, HZ/2);
982 }
Rusty Russell3161e452009-08-26 12:22:32 -0700983}
984
Jason Wang2ffa7592014-07-23 16:33:54 +0800985static int virtnet_receive(struct receive_queue *rq, int budget)
Rusty Russell296f96f2007-10-22 11:03:37 +1000986{
Jason Wange9d74172012-12-07 07:04:55 +0000987 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wang2ffa7592014-07-23 16:33:54 +0800988 unsigned int len, received = 0;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000989 void *buf;
Rusty Russell296f96f2007-10-22 11:03:37 +1000990
Rusty Russell296f96f2007-10-22 11:03:37 +1000991 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +0000992 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300993 receive_buf(vi, rq, buf, len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000994 received++;
995 }
996
Jason Wangbe121f42014-01-16 14:45:24 +0800997 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300998 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700999 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -07001000 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001001
Jason Wang2ffa7592014-07-23 16:33:54 +08001002 return received;
1003}
1004
1005static int virtnet_poll(struct napi_struct *napi, int budget)
1006{
1007 struct receive_queue *rq =
1008 container_of(napi, struct receive_queue, napi);
Li RongQingfaadb052015-03-26 15:39:45 +08001009 unsigned int r, received;
Jason Wang2ffa7592014-07-23 16:33:54 +08001010
Li RongQingfaadb052015-03-26 15:39:45 +08001011 received = virtnet_receive(rq, budget);
Jason Wang2ffa7592014-07-23 16:33:54 +08001012
Rusty Russell8329d982007-11-19 11:20:43 -05001013 /* Out of packets? */
1014 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001015 r = virtqueue_enable_cb_prepare(rq->vq);
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001016 napi_complete_done(napi, received);
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001017 if (unlikely(virtqueue_poll(rq->vq, r)) &&
Joe Perches8e95a202009-12-03 07:58:21 +00001018 napi_schedule_prep(napi)) {
Jason Wange9d74172012-12-07 07:04:55 +00001019 virtqueue_disable_cb(rq->vq);
Ben Hutchings288379f2009-01-19 16:43:59 -08001020 __napi_schedule(napi);
Christian Borntraeger4265f162008-03-14 14:17:05 +01001021 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001022 }
1023
1024 return received;
1025}
1026
Jason Wang91815632014-07-23 16:33:55 +08001027#ifdef CONFIG_NET_RX_BUSY_POLL
1028/* must be called with local_bh_disable()d */
1029static int virtnet_busy_poll(struct napi_struct *napi)
1030{
1031 struct receive_queue *rq =
1032 container_of(napi, struct receive_queue, napi);
1033 struct virtnet_info *vi = rq->vq->vdev->priv;
1034 int r, received = 0, budget = 4;
1035
1036 if (!(vi->status & VIRTIO_NET_S_LINK_UP))
1037 return LL_FLUSH_FAILED;
1038
1039 if (!napi_schedule_prep(napi))
1040 return LL_FLUSH_BUSY;
1041
1042 virtqueue_disable_cb(rq->vq);
1043
1044again:
1045 received += virtnet_receive(rq, budget);
1046
1047 r = virtqueue_enable_cb_prepare(rq->vq);
1048 clear_bit(NAPI_STATE_SCHED, &napi->state);
1049 if (unlikely(virtqueue_poll(rq->vq, r)) &&
1050 napi_schedule_prep(napi)) {
1051 virtqueue_disable_cb(rq->vq);
1052 if (received < budget) {
1053 budget -= received;
1054 goto again;
1055 } else {
1056 __napi_schedule(napi);
1057 }
1058 }
1059
1060 return received;
1061}
1062#endif /* CONFIG_NET_RX_BUSY_POLL */
1063
Jason Wang986a4f42012-12-07 07:04:56 +00001064static int virtnet_open(struct net_device *dev)
1065{
1066 struct virtnet_info *vi = netdev_priv(dev);
1067 int i;
1068
Jason Wange4166622013-05-21 20:03:58 +00001069 for (i = 0; i < vi->max_queue_pairs; i++) {
1070 if (i < vi->curr_queue_pairs)
1071 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001072 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001073 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +00001074 virtnet_napi_enable(&vi->rq[i]);
1075 }
1076
1077 return 0;
1078}
1079
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001080static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001081{
1082 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +10301083 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +00001084 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +00001085 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001086
Jason Wange9d74172012-12-07 07:04:55 +00001087 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001088 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001089
Eric Dumazet83a27052012-06-05 22:35:24 +00001090 u64_stats_update_begin(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001091 stats->tx_bytes += skb->len;
1092 stats->tx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +00001093 u64_stats_update_end(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001094
Eric Dumazeted79bab2009-10-14 14:36:43 +00001095 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001096 }
1097}
1098
Jason Wange9d74172012-12-07 07:04:55 +00001099static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001100{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001101 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001102 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001103 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +10301104 unsigned num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001105 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301106 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001107
Johannes Berge1749612008-10-27 15:59:26 -07001108 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301109
1110 can_push = vi->any_header_sg &&
1111 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1112 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1113 /* Even if we can, don't push here yet as this would skew
1114 * csum_start offset below. */
1115 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001116 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301117 else
1118 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001119
Mike Rapoporte858fae2016-06-08 16:09:21 +03001120 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Jason Wang6391a442017-01-20 14:32:42 +08001121 virtio_is_little_endian(vi->vdev), false))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001122 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001123
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001124 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001125 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001126
Jason Wang547c8902015-08-27 14:53:06 +08001127 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301128 if (can_push) {
1129 __skb_push(skb, hdr_len);
1130 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1131 /* Pull header back to avoid skew in tx bytes calculations. */
1132 __skb_pull(skb, hdr_len);
1133 } else {
1134 sg_set_buf(sq->sg, hdr, hdr_len);
1135 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1136 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301137 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001138}
1139
Stephen Hemminger424efe92009-08-31 19:50:51 +00001140static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001141{
1142 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001143 int qnum = skb_get_queue_mapping(skb);
1144 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301145 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001146 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1147 bool kick = !skb->xmit_more;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001148
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001149 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001150 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001151
Jacob Keller074c3582014-06-25 02:37:13 +00001152 /* timestamp packet in software */
1153 skb_tx_timestamp(skb);
1154
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001155 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001156 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001157
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301158 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001159 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301160 dev->stats.tx_fifo_errors++;
1161 if (net_ratelimit())
1162 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001163 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001164 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001165 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001166 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001167 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001168
Rusty Russell48925e32009-09-24 09:59:20 -06001169 /* Don't wait up for transmitted skbs to be freed. */
1170 skb_orphan(skb);
1171 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -05001172
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001173 /* If running out of space, stop queue to avoid getting packets that we
1174 * are then unable to transmit.
1175 * An alternative would be to force queuing layer to requeue the skb by
1176 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1177 * returned in a normal path of operation: it means that driver is not
1178 * maintaining the TX queue stop/start state properly, and causes
1179 * the stack to do a non-trivial amount of useless work.
1180 * Since most packets only take 1 or 2 ring slots, stopping the queue
1181 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001182 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001183 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001184 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001185 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001186 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001187 free_old_xmit_skbs(sq);
1188 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001189 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001190 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001191 }
1192 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001193 }
Rusty Russell48925e32009-09-24 09:59:20 -06001194
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001195 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001196 virtqueue_kick(sq->vq);
1197
Rusty Russell48925e32009-09-24 09:59:20 -06001198 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001199}
1200
Amos Kong40cbfc32013-01-21 01:17:21 +00001201/*
1202 * Send command via the control virtqueue and check status. Commands
1203 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001204 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001205 */
1206static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001207 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001208{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301209 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001210 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001211
1212 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301213 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001214
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001215 vi->ctrl_status = ~0;
1216 vi->ctrl_hdr.class = class;
1217 vi->ctrl_hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301218 /* Add header */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001219 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301220 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001221
Rusty Russellf7bc9592013-03-20 15:44:28 +10301222 if (out)
1223 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001224
Rusty Russellf7bc9592013-03-20 15:44:28 +10301225 /* Add return status. */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001226 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001227 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001228
stephen hemmingerd24bae32013-12-09 16:17:40 -08001229 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301230 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001231
Heinz Graalfs67975902013-10-29 09:40:02 +10301232 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001233 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001234
1235 /* Spin for a response, the kick causes an ioport write, trapping
1236 * into the hypervisor, so the request should be handled immediately.
1237 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301238 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1239 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001240 cpu_relax();
1241
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001242 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001243}
1244
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001245static int virtnet_set_mac_address(struct net_device *dev, void *p)
1246{
1247 struct virtnet_info *vi = netdev_priv(dev);
1248 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001249 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001250 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001251 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001252
Shyam Saini801822d2016-12-24 00:44:58 +05301253 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001254 if (!addr)
1255 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001256
1257 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001258 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001259 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001260
Amos Kong7e58d5a2013-01-21 01:17:23 +00001261 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1262 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1263 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001264 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001265 dev_warn(&vdev->dev,
1266 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001267 ret = -EINVAL;
1268 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001269 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001270 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1271 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301272 unsigned int i;
1273
1274 /* Naturally, this has an atomicity problem. */
1275 for (i = 0; i < dev->addr_len; i++)
1276 virtio_cwrite8(vdev,
1277 offsetof(struct virtio_net_config, mac) +
1278 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001279 }
1280
1281 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001282 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001283
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001284out:
1285 kfree(addr);
1286 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001287}
1288
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001289static void virtnet_stats(struct net_device *dev,
1290 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001291{
1292 struct virtnet_info *vi = netdev_priv(dev);
1293 int cpu;
1294 unsigned int start;
1295
1296 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +00001297 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001298 u64 tpackets, tbytes, rpackets, rbytes;
1299
1300 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001301 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001302 tpackets = stats->tx_packets;
1303 tbytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001304 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001305
1306 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001307 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001308 rpackets = stats->rx_packets;
1309 rbytes = stats->rx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001310 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001311
1312 tot->rx_packets += rpackets;
1313 tot->tx_packets += tpackets;
1314 tot->rx_bytes += rbytes;
1315 tot->tx_bytes += tbytes;
1316 }
1317
1318 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001319 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001320 tot->rx_dropped = dev->stats.rx_dropped;
1321 tot->rx_length_errors = dev->stats.rx_length_errors;
1322 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001323}
1324
Amit Shahda74e892008-02-29 16:24:50 +05301325#ifdef CONFIG_NET_POLL_CONTROLLER
1326static void virtnet_netpoll(struct net_device *dev)
1327{
1328 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001329 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301330
Jason Wang986a4f42012-12-07 07:04:56 +00001331 for (i = 0; i < vi->curr_queue_pairs; i++)
1332 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301333}
1334#endif
1335
Jason Wang586d17c2012-04-11 20:43:52 +00001336static void virtnet_ack_link_announce(struct virtnet_info *vi)
1337{
1338 rtnl_lock();
1339 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001340 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001341 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1342 rtnl_unlock();
1343}
1344
Jason Wang986a4f42012-12-07 07:04:56 +00001345static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1346{
1347 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001348 struct net_device *dev = vi->dev;
1349
1350 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1351 return 0;
1352
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001353 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1354 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001355
1356 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001357 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001358 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1359 queue_pairs);
1360 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301361 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001362 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001363 /* virtnet_open() will refill when device is going to up. */
1364 if (dev->flags & IFF_UP)
1365 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301366 }
Jason Wang986a4f42012-12-07 07:04:56 +00001367
1368 return 0;
1369}
1370
Rusty Russell296f96f2007-10-22 11:03:37 +10001371static int virtnet_close(struct net_device *dev)
1372{
1373 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001374 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001375
Rusty Russellb2baed62011-12-29 00:42:38 +00001376 /* Make sure refill_work doesn't re-enable napi! */
1377 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001378
1379 for (i = 0; i < vi->max_queue_pairs; i++)
1380 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001381
Rusty Russell296f96f2007-10-22 11:03:37 +10001382 return 0;
1383}
1384
Alex Williamson2af76982009-02-04 09:02:40 +00001385static void virtnet_set_rx_mode(struct net_device *dev)
1386{
1387 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001388 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001389 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001390 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001391 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001392 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001393 void *buf;
1394 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001395
stephen hemminger788a8b62013-12-09 16:18:45 -08001396 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001397 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1398 return;
1399
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001400 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1401 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001402
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001403 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001404
1405 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001406 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001407 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001408 vi->ctrl_promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001409
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001410 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001411
1412 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001413 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001414 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001415 vi->ctrl_allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001416
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001417 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001418 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001419 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001420 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1421 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1422 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001423 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001424 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001425
Alex Williamson23e258e2009-05-01 17:27:56 +00001426 sg_init_table(sg, 2);
1427
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001428 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001429 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001430 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001431 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001432 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001433
1434 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001435 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001436
1437 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001438 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001439
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001440 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001441 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001442 netdev_for_each_mc_addr(ha, dev)
1443 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001444
1445 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001446 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001447
1448 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001449 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001450 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001451
1452 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001453}
1454
Patrick McHardy80d5c362013-04-19 02:04:28 +00001455static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1456 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001457{
1458 struct virtnet_info *vi = netdev_priv(dev);
1459 struct scatterlist sg;
1460
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001461 vi->ctrl_vid = vid;
1462 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001463
1464 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001465 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001466 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001467 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001468}
1469
Patrick McHardy80d5c362013-04-19 02:04:28 +00001470static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1471 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001472{
1473 struct virtnet_info *vi = netdev_priv(dev);
1474 struct scatterlist sg;
1475
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001476 vi->ctrl_vid = vid;
1477 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001478
1479 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001480 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001481 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001482 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001483}
1484
Wanlong Gao8898c212013-01-24 23:51:30 +00001485static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001486{
1487 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001488
1489 if (vi->affinity_hint_set) {
1490 for (i = 0; i < vi->max_queue_pairs; i++) {
1491 virtqueue_set_affinity(vi->rq[i].vq, -1);
1492 virtqueue_set_affinity(vi->sq[i].vq, -1);
1493 }
1494
1495 vi->affinity_hint_set = false;
1496 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001497}
1498
1499static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001500{
1501 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001502 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001503
1504 /* In multiqueue mode, when the number of cpu is equal to the number of
1505 * queue pairs, we let the queue pairs to be private to one cpu by
1506 * setting the affinity hint to eliminate the contention.
1507 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001508 if (vi->curr_queue_pairs == 1 ||
1509 vi->max_queue_pairs != num_online_cpus()) {
1510 virtnet_clean_affinity(vi, -1);
1511 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001512 }
1513
Wanlong Gao8898c212013-01-24 23:51:30 +00001514 i = 0;
1515 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001516 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1517 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001518 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001519 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001520 }
1521
Wanlong Gao8898c212013-01-24 23:51:30 +00001522 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001523}
1524
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001525static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001526{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001527 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1528 node);
1529 virtnet_set_affinity(vi);
1530 return 0;
1531}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001532
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001533static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1534{
1535 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1536 node_dead);
1537 virtnet_set_affinity(vi);
1538 return 0;
1539}
Jason Wang3ab098d2013-10-15 11:18:58 +08001540
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001541static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1542{
1543 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1544 node);
1545
1546 virtnet_clean_affinity(vi, cpu);
1547 return 0;
1548}
1549
1550static enum cpuhp_state virtionet_online;
1551
1552static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1553{
1554 int ret;
1555
1556 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1557 if (ret)
1558 return ret;
1559 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1560 &vi->node_dead);
1561 if (!ret)
1562 return ret;
1563 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1564 return ret;
1565}
1566
1567static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1568{
1569 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1570 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1571 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001572}
1573
Rick Jones8f9f4662011-10-19 08:10:59 +00001574static void virtnet_get_ringparam(struct net_device *dev,
1575 struct ethtool_ringparam *ring)
1576{
1577 struct virtnet_info *vi = netdev_priv(dev);
1578
Jason Wang986a4f42012-12-07 07:04:56 +00001579 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1580 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001581 ring->rx_pending = ring->rx_max_pending;
1582 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001583}
1584
Rick Jones66846042011-11-14 14:17:08 +00001585
1586static void virtnet_get_drvinfo(struct net_device *dev,
1587 struct ethtool_drvinfo *info)
1588{
1589 struct virtnet_info *vi = netdev_priv(dev);
1590 struct virtio_device *vdev = vi->vdev;
1591
1592 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1593 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1594 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1595
1596}
1597
Jason Wangd73bcd22012-12-07 07:04:57 +00001598/* TODO: Eliminate OOO packets during switching */
1599static int virtnet_set_channels(struct net_device *dev,
1600 struct ethtool_channels *channels)
1601{
1602 struct virtnet_info *vi = netdev_priv(dev);
1603 u16 queue_pairs = channels->combined_count;
1604 int err;
1605
1606 /* We don't support separate rx/tx channels.
1607 * We don't allow setting 'other' channels.
1608 */
1609 if (channels->rx_count || channels->tx_count || channels->other_count)
1610 return -EINVAL;
1611
Amos Kongc18e9cd2014-04-18 13:45:41 +08001612 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001613 return -EINVAL;
1614
John Fastabendf600b692016-12-15 12:13:24 -08001615 /* For now we don't support modifying channels while XDP is loaded
1616 * also when XDP is loaded all RX queues have XDP programs so we only
1617 * need to check a single RX queue.
1618 */
1619 if (vi->rq[0].xdp_prog)
1620 return -EINVAL;
1621
Wanlong Gao47be2472013-01-24 23:51:29 +00001622 get_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001623 err = virtnet_set_queues(vi, queue_pairs);
1624 if (!err) {
1625 netif_set_real_num_tx_queues(dev, queue_pairs);
1626 netif_set_real_num_rx_queues(dev, queue_pairs);
1627
Wanlong Gao8898c212013-01-24 23:51:30 +00001628 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001629 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001630 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001631
1632 return err;
1633}
1634
1635static void virtnet_get_channels(struct net_device *dev,
1636 struct ethtool_channels *channels)
1637{
1638 struct virtnet_info *vi = netdev_priv(dev);
1639
1640 channels->combined_count = vi->curr_queue_pairs;
1641 channels->max_combined = vi->max_queue_pairs;
1642 channels->max_other = 0;
1643 channels->rx_count = 0;
1644 channels->tx_count = 0;
1645 channels->other_count = 0;
1646}
1647
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001648/* Check if the user is trying to change anything besides speed/duplex */
1649static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1650{
1651 struct ethtool_cmd diff1 = *cmd;
1652 struct ethtool_cmd diff2 = {};
1653
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001654 /* cmd is always set so we need to clear it, validate the port type
1655 * and also without autonegotiation we can ignore advertising
1656 */
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001657 ethtool_cmd_speed_set(&diff1, 0);
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001658 diff2.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001659 diff1.advertising = 0;
1660 diff1.duplex = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001661 diff1.cmd = 0;
1662
1663 return !memcmp(&diff1, &diff2, sizeof(diff1));
1664}
1665
1666static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1667{
1668 struct virtnet_info *vi = netdev_priv(dev);
1669 u32 speed;
1670
1671 speed = ethtool_cmd_speed(cmd);
1672 /* don't allow custom speed and duplex */
1673 if (!ethtool_validate_speed(speed) ||
1674 !ethtool_validate_duplex(cmd->duplex) ||
1675 !virtnet_validate_ethtool_cmd(cmd))
1676 return -EINVAL;
1677 vi->speed = speed;
1678 vi->duplex = cmd->duplex;
1679
1680 return 0;
1681}
1682
1683static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1684{
1685 struct virtnet_info *vi = netdev_priv(dev);
1686
1687 ethtool_cmd_speed_set(cmd, vi->speed);
1688 cmd->duplex = vi->duplex;
1689 cmd->port = PORT_OTHER;
1690
1691 return 0;
1692}
1693
1694static void virtnet_init_settings(struct net_device *dev)
1695{
1696 struct virtnet_info *vi = netdev_priv(dev);
1697
1698 vi->speed = SPEED_UNKNOWN;
1699 vi->duplex = DUPLEX_UNKNOWN;
1700}
1701
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001702static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001703 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001704 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001705 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001706 .set_channels = virtnet_set_channels,
1707 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00001708 .get_ts_info = ethtool_op_get_ts_info,
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001709 .get_settings = virtnet_get_settings,
1710 .set_settings = virtnet_set_settings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001711};
1712
John Fastabendf600b692016-12-15 12:13:24 -08001713static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1714{
1715 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1716 struct virtnet_info *vi = netdev_priv(dev);
1717 struct bpf_prog *old_prog;
John Fastabend672aafd2016-12-15 12:13:49 -08001718 u16 xdp_qp = 0, curr_qp;
1719 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001720
Jakub Kicinski529ec6a2017-01-25 14:56:36 -08001721 if (prog && prog->xdp_adjust_head) {
1722 netdev_warn(dev, "Does not support bpf_xdp_adjust_head()\n");
1723 return -EOPNOTSUPP;
1724 }
1725
John Fastabendf600b692016-12-15 12:13:24 -08001726 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001727 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1728 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1729 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001730 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1731 return -EOPNOTSUPP;
1732 }
1733
1734 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1735 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1736 return -EINVAL;
1737 }
1738
1739 if (dev->mtu > max_sz) {
1740 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1741 return -EINVAL;
1742 }
1743
John Fastabend672aafd2016-12-15 12:13:49 -08001744 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1745 if (prog)
1746 xdp_qp = nr_cpu_ids;
1747
1748 /* XDP requires extra queues for XDP_TX */
1749 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1750 netdev_warn(dev, "request %i queues but max is %i\n",
1751 curr_qp + xdp_qp, vi->max_queue_pairs);
1752 return -ENOMEM;
1753 }
1754
1755 err = virtnet_set_queues(vi, curr_qp + xdp_qp);
1756 if (err) {
1757 dev_warn(&dev->dev, "XDP Device queue allocation failure.\n");
1758 return err;
1759 }
1760
John Fastabendf600b692016-12-15 12:13:24 -08001761 if (prog) {
1762 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
John Fastabend672aafd2016-12-15 12:13:49 -08001763 if (IS_ERR(prog)) {
1764 virtnet_set_queues(vi, curr_qp);
John Fastabendf600b692016-12-15 12:13:24 -08001765 return PTR_ERR(prog);
John Fastabend672aafd2016-12-15 12:13:49 -08001766 }
John Fastabendf600b692016-12-15 12:13:24 -08001767 }
1768
John Fastabend672aafd2016-12-15 12:13:49 -08001769 vi->xdp_queue_pairs = xdp_qp;
1770 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1771
John Fastabendf600b692016-12-15 12:13:24 -08001772 for (i = 0; i < vi->max_queue_pairs; i++) {
1773 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1774 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1775 if (old_prog)
1776 bpf_prog_put(old_prog);
1777 }
1778
1779 return 0;
1780}
1781
1782static bool virtnet_xdp_query(struct net_device *dev)
1783{
1784 struct virtnet_info *vi = netdev_priv(dev);
1785 int i;
1786
1787 for (i = 0; i < vi->max_queue_pairs; i++) {
1788 if (vi->rq[i].xdp_prog)
1789 return true;
1790 }
1791 return false;
1792}
1793
1794static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1795{
1796 switch (xdp->command) {
1797 case XDP_SETUP_PROG:
1798 return virtnet_xdp_set(dev, xdp->prog);
1799 case XDP_QUERY_PROG:
1800 xdp->prog_attached = virtnet_xdp_query(dev);
1801 return 0;
1802 default:
1803 return -EINVAL;
1804 }
1805}
1806
Stephen Hemminger76288b42009-01-06 10:44:22 -08001807static const struct net_device_ops virtnet_netdev = {
1808 .ndo_open = virtnet_open,
1809 .ndo_stop = virtnet_close,
1810 .ndo_start_xmit = start_xmit,
1811 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001812 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001813 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001814 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001815 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1816 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001817#ifdef CONFIG_NET_POLL_CONTROLLER
1818 .ndo_poll_controller = virtnet_netpoll,
1819#endif
Jason Wang91815632014-07-23 16:33:55 +08001820#ifdef CONFIG_NET_RX_BUSY_POLL
1821 .ndo_busy_poll = virtnet_busy_poll,
1822#endif
John Fastabendf600b692016-12-15 12:13:24 -08001823 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001824};
1825
Jason Wang586d17c2012-04-11 20:43:52 +00001826static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001827{
Jason Wang586d17c2012-04-11 20:43:52 +00001828 struct virtnet_info *vi =
1829 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001830 u16 v;
1831
Rusty Russell855e0c52013-10-14 18:11:51 +10301832 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1833 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301834 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001835
1836 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001837 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001838 virtnet_ack_link_announce(vi);
1839 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001840
1841 /* Ignore unknown (future) status bits */
1842 v &= VIRTIO_NET_S_LINK_UP;
1843
1844 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301845 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001846
1847 vi->status = v;
1848
1849 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1850 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001851 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001852 } else {
1853 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001854 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001855 }
1856}
1857
1858static void virtnet_config_changed(struct virtio_device *vdev)
1859{
1860 struct virtnet_info *vi = vdev->priv;
1861
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001862 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001863}
1864
Jason Wang986a4f42012-12-07 07:04:56 +00001865static void virtnet_free_queues(struct virtnet_info *vi)
1866{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001867 int i;
1868
Jason Wangab3971b2015-03-12 13:57:44 +08001869 for (i = 0; i < vi->max_queue_pairs; i++) {
1870 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001871 netif_napi_del(&vi->rq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08001872 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001873
Eric Dumazet963abe52016-11-15 22:24:12 -08001874 /* We called napi_hash_del() before netif_napi_del(),
1875 * we need to respect an RCU grace period before freeing vi->rq
1876 */
1877 synchronize_net();
1878
Jason Wang986a4f42012-12-07 07:04:56 +00001879 kfree(vi->rq);
1880 kfree(vi->sq);
1881}
1882
1883static void free_receive_bufs(struct virtnet_info *vi)
1884{
John Fastabendf600b692016-12-15 12:13:24 -08001885 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00001886 int i;
1887
John Fastabendf600b692016-12-15 12:13:24 -08001888 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00001889 for (i = 0; i < vi->max_queue_pairs; i++) {
1890 while (vi->rq[i].pages)
1891 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08001892
1893 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1894 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1895 if (old_prog)
1896 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00001897 }
John Fastabendf600b692016-12-15 12:13:24 -08001898 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001899}
1900
Michael Daltonfb518792014-01-16 22:23:26 -08001901static void free_receive_page_frags(struct virtnet_info *vi)
1902{
1903 int i;
1904 for (i = 0; i < vi->max_queue_pairs; i++)
1905 if (vi->rq[i].alloc_frag.page)
1906 put_page(vi->rq[i].alloc_frag.page);
1907}
1908
John Fastabendb68df012017-01-25 18:22:48 -08001909static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08001910{
John Fastabendb68df012017-01-25 18:22:48 -08001911 /* For small receive mode always use kfree_skb variants */
1912 if (!vi->mergeable_rx_bufs)
1913 return false;
1914
John Fastabend56434a02016-12-15 12:14:13 -08001915 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1916 return false;
1917 else if (q < vi->curr_queue_pairs)
1918 return true;
1919 else
1920 return false;
1921}
1922
Jason Wang986a4f42012-12-07 07:04:56 +00001923static void free_unused_bufs(struct virtnet_info *vi)
1924{
1925 void *buf;
1926 int i;
1927
1928 for (i = 0; i < vi->max_queue_pairs; i++) {
1929 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08001930 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08001931 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08001932 dev_kfree_skb(buf);
1933 else
1934 put_page(virt_to_head_page(buf));
1935 }
Jason Wang986a4f42012-12-07 07:04:56 +00001936 }
1937
1938 for (i = 0; i < vi->max_queue_pairs; i++) {
1939 struct virtqueue *vq = vi->rq[i].vq;
1940
1941 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08001942 if (vi->mergeable_rx_bufs) {
1943 unsigned long ctx = (unsigned long)buf;
1944 void *base = mergeable_ctx_to_buf_address(ctx);
1945 put_page(virt_to_head_page(base));
1946 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001947 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001948 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001949 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001950 }
Jason Wang986a4f42012-12-07 07:04:56 +00001951 }
Jason Wang986a4f42012-12-07 07:04:56 +00001952 }
1953}
1954
Jason Wange9d74172012-12-07 07:04:55 +00001955static void virtnet_del_vqs(struct virtnet_info *vi)
1956{
1957 struct virtio_device *vdev = vi->vdev;
1958
Wanlong Gao8898c212013-01-24 23:51:30 +00001959 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00001960
Jason Wange9d74172012-12-07 07:04:55 +00001961 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00001962
1963 virtnet_free_queues(vi);
1964}
1965
1966static int virtnet_find_vqs(struct virtnet_info *vi)
1967{
1968 vq_callback_t **callbacks;
1969 struct virtqueue **vqs;
1970 int ret = -ENOMEM;
1971 int i, total_vqs;
1972 const char **names;
1973
1974 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1975 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1976 * possible control vq.
1977 */
1978 total_vqs = vi->max_queue_pairs * 2 +
1979 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1980
1981 /* Allocate space for find_vqs parameters */
1982 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1983 if (!vqs)
1984 goto err_vq;
1985 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1986 if (!callbacks)
1987 goto err_callback;
1988 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1989 if (!names)
1990 goto err_names;
1991
1992 /* Parameters for control virtqueue, if any */
1993 if (vi->has_cvq) {
1994 callbacks[total_vqs - 1] = NULL;
1995 names[total_vqs - 1] = "control";
1996 }
1997
1998 /* Allocate/initialize parameters for send/receive virtqueues */
1999 for (i = 0; i < vi->max_queue_pairs; i++) {
2000 callbacks[rxq2vq(i)] = skb_recv_done;
2001 callbacks[txq2vq(i)] = skb_xmit_done;
2002 sprintf(vi->rq[i].name, "input.%d", i);
2003 sprintf(vi->sq[i].name, "output.%d", i);
2004 names[rxq2vq(i)] = vi->rq[i].name;
2005 names[txq2vq(i)] = vi->sq[i].name;
2006 }
2007
2008 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
2009 names);
2010 if (ret)
2011 goto err_find;
2012
2013 if (vi->has_cvq) {
2014 vi->cvq = vqs[total_vqs - 1];
2015 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002016 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002017 }
2018
2019 for (i = 0; i < vi->max_queue_pairs; i++) {
2020 vi->rq[i].vq = vqs[rxq2vq(i)];
2021 vi->sq[i].vq = vqs[txq2vq(i)];
2022 }
2023
2024 kfree(names);
2025 kfree(callbacks);
2026 kfree(vqs);
2027
2028 return 0;
2029
2030err_find:
2031 kfree(names);
2032err_names:
2033 kfree(callbacks);
2034err_callback:
2035 kfree(vqs);
2036err_vq:
2037 return ret;
2038}
2039
2040static int virtnet_alloc_queues(struct virtnet_info *vi)
2041{
2042 int i;
2043
2044 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2045 if (!vi->sq)
2046 goto err_sq;
2047 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002048 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002049 goto err_rq;
2050
2051 INIT_DELAYED_WORK(&vi->refill, refill_work);
2052 for (i = 0; i < vi->max_queue_pairs; i++) {
2053 vi->rq[i].pages = NULL;
2054 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2055 napi_weight);
2056
2057 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002058 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002059 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2060 }
2061
2062 return 0;
2063
2064err_rq:
2065 kfree(vi->sq);
2066err_sq:
2067 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002068}
2069
Amit Shah3f9c10b2011-12-22 16:58:31 +05302070static int init_vqs(struct virtnet_info *vi)
2071{
Jason Wang986a4f42012-12-07 07:04:56 +00002072 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302073
Jason Wang986a4f42012-12-07 07:04:56 +00002074 /* Allocate send & receive queues */
2075 ret = virtnet_alloc_queues(vi);
2076 if (ret)
2077 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302078
Jason Wang986a4f42012-12-07 07:04:56 +00002079 ret = virtnet_find_vqs(vi);
2080 if (ret)
2081 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302082
Wanlong Gao47be2472013-01-24 23:51:29 +00002083 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002084 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002085 put_online_cpus();
2086
Amit Shah3f9c10b2011-12-22 16:58:31 +05302087 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002088
2089err_free:
2090 virtnet_free_queues(vi);
2091err:
2092 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302093}
2094
Michael Daltonfbf28d72014-01-16 22:23:30 -08002095#ifdef CONFIG_SYSFS
2096static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2097 struct rx_queue_attribute *attribute, char *buf)
2098{
2099 struct virtnet_info *vi = netdev_priv(queue->dev);
2100 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002101 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002102
2103 BUG_ON(queue_index >= vi->max_queue_pairs);
2104 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2105 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2106}
2107
2108static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2109 __ATTR_RO(mergeable_rx_buffer_size);
2110
2111static struct attribute *virtio_net_mrg_rx_attrs[] = {
2112 &mergeable_rx_buffer_size_attribute.attr,
2113 NULL
2114};
2115
2116static const struct attribute_group virtio_net_mrg_rx_group = {
2117 .name = "virtio_net",
2118 .attrs = virtio_net_mrg_rx_attrs
2119};
2120#endif
2121
Jason Wang892d6eb2014-11-20 17:03:05 +08002122static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2123 unsigned int fbit,
2124 const char *fname, const char *dname)
2125{
2126 if (!virtio_has_feature(vdev, fbit))
2127 return false;
2128
2129 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2130 fname, dname);
2131
2132 return true;
2133}
2134
2135#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2136 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2137
2138static bool virtnet_validate_features(struct virtio_device *vdev)
2139{
2140 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2141 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2142 "VIRTIO_NET_F_CTRL_VQ") ||
2143 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2144 "VIRTIO_NET_F_CTRL_VQ") ||
2145 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2146 "VIRTIO_NET_F_CTRL_VQ") ||
2147 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2148 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2149 "VIRTIO_NET_F_CTRL_VQ"))) {
2150 return false;
2151 }
2152
2153 return true;
2154}
2155
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002156#define MIN_MTU ETH_MIN_MTU
2157#define MAX_MTU ETH_MAX_MTU
2158
Rusty Russell296f96f2007-10-22 11:03:37 +10002159static int virtnet_probe(struct virtio_device *vdev)
2160{
Jason Wang986a4f42012-12-07 07:04:56 +00002161 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10002162 struct net_device *dev;
2163 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00002164 u16 max_queue_pairs;
Aaron Conole14de9d12016-06-03 16:57:12 -04002165 int mtu;
Jason Wang986a4f42012-12-07 07:04:56 +00002166
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002167 if (!vdev->config->get) {
2168 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2169 __func__);
2170 return -EINVAL;
2171 }
2172
Jason Wang892d6eb2014-11-20 17:03:05 +08002173 if (!virtnet_validate_features(vdev))
2174 return -EINVAL;
2175
Jason Wang986a4f42012-12-07 07:04:56 +00002176 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302177 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2178 struct virtio_net_config,
2179 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002180
2181 /* We need at least 2 queue's */
2182 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2183 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2184 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2185 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002186
2187 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002188 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002189 if (!dev)
2190 return -ENOMEM;
2191
2192 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002193 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002194 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002195 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002196
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002197 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002198 SET_NETDEV_DEV(dev, &vdev->dev);
2199
2200 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002201 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002202 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002203 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002204 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002205 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002206
2207 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002208 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002209 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2210 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002211 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002212 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2213 dev->hw_features |= NETIF_F_TSO;
2214 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2215 dev->hw_features |= NETIF_F_TSO6;
2216 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2217 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002218 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2219 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002220
Jason Wang41f2f122014-12-24 11:03:52 +08002221 dev->features |= NETIF_F_GSO_ROBUST;
2222
Michał Mirosław98e778c2011-03-31 01:01:35 +00002223 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002224 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002225 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002226 }
Thomas Huth4f491292013-08-27 17:09:02 +02002227 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2228 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002229
Jason Wang4fda8302013-04-10 23:32:21 +00002230 dev->vlan_features = dev->features;
2231
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002232 /* MTU range: 68 - 65535 */
2233 dev->min_mtu = MIN_MTU;
2234 dev->max_mtu = MAX_MTU;
2235
Rusty Russell296f96f2007-10-22 11:03:37 +10002236 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302237 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2238 virtio_cread_bytes(vdev,
2239 offsetof(struct virtio_net_config, mac),
2240 dev->dev_addr, dev->addr_len);
2241 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002242 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002243
2244 /* Set up our device-specific information */
2245 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002246 vi->dev = dev;
2247 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002248 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002249 vi->stats = alloc_percpu(struct virtnet_stats);
2250 err = -ENOMEM;
2251 if (vi->stats == NULL)
2252 goto free;
2253
John Stultz827da442013-10-07 15:51:58 -07002254 for_each_possible_cpu(i) {
2255 struct virtnet_stats *virtnet_stats;
2256 virtnet_stats = per_cpu_ptr(vi->stats, i);
2257 u64_stats_init(&virtnet_stats->tx_syncp);
2258 u64_stats_init(&virtnet_stats->rx_syncp);
2259 }
2260
Jason Wang586d17c2012-04-11 20:43:52 +00002261 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002262
Herbert Xu97402b92008-04-18 11:24:27 +08002263 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002264 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2265 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002266 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2267 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002268 vi->big_packets = true;
2269
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002270 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2271 vi->mergeable_rx_bufs = true;
2272
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002273 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2274 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002275 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2276 else
2277 vi->hdr_len = sizeof(struct virtio_net_hdr);
2278
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002279 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2280 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302281 vi->any_header_sg = true;
2282
Jason Wang986a4f42012-12-07 07:04:56 +00002283 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2284 vi->has_cvq = true;
2285
Aaron Conole14de9d12016-06-03 16:57:12 -04002286 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2287 mtu = virtio_cread16(vdev,
2288 offsetof(struct virtio_net_config,
2289 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002290 if (mtu < dev->min_mtu) {
Aaron Conole14de9d12016-06-03 16:57:12 -04002291 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
Aaron Conole93a205e2016-10-25 16:12:12 -04002292 } else {
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002293 dev->mtu = mtu;
Aaron Conole93a205e2016-10-25 16:12:12 -04002294 dev->max_mtu = mtu;
2295 }
Aaron Conole14de9d12016-06-03 16:57:12 -04002296 }
2297
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002298 if (vi->any_header_sg)
2299 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002300
Jason Wang44900012016-11-25 12:37:26 +08002301 /* Enable multiqueue by default */
2302 if (num_online_cpus() >= max_queue_pairs)
2303 vi->curr_queue_pairs = max_queue_pairs;
2304 else
2305 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002306 vi->max_queue_pairs = max_queue_pairs;
2307
2308 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302309 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002310 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002311 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002312
Michael Daltonfbf28d72014-01-16 22:23:30 -08002313#ifdef CONFIG_SYSFS
2314 if (vi->mergeable_rx_bufs)
2315 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2316#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08002317 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2318 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002319
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002320 virtnet_init_settings(dev);
2321
Rusty Russell296f96f2007-10-22 11:03:37 +10002322 err = register_netdev(dev);
2323 if (err) {
2324 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002325 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002326 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002327
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302328 virtio_device_ready(vdev);
2329
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002330 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002331 if (err) {
2332 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002333 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002334 }
2335
Jason Wanga2208712016-12-13 14:23:05 +08002336 rtnl_lock();
2337 virtnet_set_queues(vi, vi->curr_queue_pairs);
2338 rtnl_unlock();
Jason Wang44900012016-11-25 12:37:26 +08002339
Jason Wang167c25e2010-11-10 14:45:41 +00002340 /* Assume link up if device can't report link status,
2341 otherwise get link status from config. */
2342 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2343 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002344 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002345 } else {
2346 vi->status = VIRTIO_NET_S_LINK_UP;
2347 netif_carrier_on(dev);
2348 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002349
Jason Wang986a4f42012-12-07 07:04:56 +00002350 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2351 dev->name, max_queue_pairs);
2352
Rusty Russell296f96f2007-10-22 11:03:37 +10002353 return 0;
2354
wangyunjianf00e35e2016-05-31 11:52:43 +08002355free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302356 vi->vdev->config->reset(vdev);
2357
Rusty Russellb3369c12008-02-04 23:50:02 -05002358 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002359free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002360 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002361 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002362 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002363free_stats:
2364 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002365free:
2366 free_netdev(dev);
2367 return err;
2368}
2369
Amit Shah04486ed2011-12-22 16:58:32 +05302370static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002371{
Amit Shah04486ed2011-12-22 16:58:32 +05302372 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002373
2374 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002375 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002376
Jason Wang986a4f42012-12-07 07:04:56 +00002377 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002378
Michael Daltonfb518792014-01-16 22:23:26 -08002379 free_receive_page_frags(vi);
2380
Jason Wang986a4f42012-12-07 07:04:56 +00002381 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302382}
2383
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002384static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302385{
2386 struct virtnet_info *vi = vdev->priv;
2387
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002388 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002389
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302390 /* Make sure no work handler is accessing the device. */
2391 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002392
Amit Shah04486ed2011-12-22 16:58:32 +05302393 unregister_netdev(vi->dev);
2394
2395 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002396
Krishna Kumar2e66f552011-07-20 03:56:02 +00002397 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002398 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002399}
2400
Aaron Lu89107002013-09-17 09:25:23 +09302401#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302402static int virtnet_freeze(struct virtio_device *vdev)
2403{
2404 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002405 int i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302406
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002407 virtnet_cpu_notif_remove(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002408
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302409 /* Make sure no work handler is accessing the device */
2410 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002411
Amit Shah0741bcb2011-12-22 16:58:33 +05302412 netif_device_detach(vi->dev);
2413 cancel_delayed_work_sync(&vi->refill);
2414
Jason Wang91815632014-07-23 16:33:55 +08002415 if (netif_running(vi->dev)) {
Jason Wangab3971b2015-03-12 13:57:44 +08002416 for (i = 0; i < vi->max_queue_pairs; i++)
Jason Wang986a4f42012-12-07 07:04:56 +00002417 napi_disable(&vi->rq[i].napi);
Jason Wang91815632014-07-23 16:33:55 +08002418 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302419
2420 remove_vq_common(vi);
2421
2422 return 0;
2423}
2424
2425static int virtnet_restore(struct virtio_device *vdev)
2426{
2427 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002428 int err, i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302429
2430 err = init_vqs(vi);
2431 if (err)
2432 return err;
2433
Michael S. Tsirkine53fbd12014-10-15 10:22:32 +10302434 virtio_device_ready(vdev);
2435
Jason Wang6cd4ce02013-12-30 11:34:40 +08002436 if (netif_running(vi->dev)) {
2437 for (i = 0; i < vi->curr_queue_pairs; i++)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03002438 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wang6cd4ce02013-12-30 11:34:40 +08002439 schedule_delayed_work(&vi->refill, 0);
2440
Jason Wang986a4f42012-12-07 07:04:56 +00002441 for (i = 0; i < vi->max_queue_pairs; i++)
2442 virtnet_napi_enable(&vi->rq[i]);
Jason Wang6cd4ce02013-12-30 11:34:40 +08002443 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302444
2445 netif_device_attach(vi->dev);
2446
Jason Wang35ed1592013-10-15 11:18:59 +08002447 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00002448 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang35ed1592013-10-15 11:18:59 +08002449 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002450
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002451 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002452 if (err)
2453 return err;
2454
Amit Shah0741bcb2011-12-22 16:58:33 +05302455 return 0;
2456}
2457#endif
2458
Rusty Russell296f96f2007-10-22 11:03:37 +10002459static struct virtio_device_id id_table[] = {
2460 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2461 { 0 },
2462};
2463
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002464#define VIRTNET_FEATURES \
2465 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2466 VIRTIO_NET_F_MAC, \
2467 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2468 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2469 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2470 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2471 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2472 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2473 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2474 VIRTIO_NET_F_MTU
2475
Rusty Russellc45a6812008-05-02 21:50:50 -05002476static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002477 VIRTNET_FEATURES,
2478};
2479
2480static unsigned int features_legacy[] = {
2481 VIRTNET_FEATURES,
2482 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302483 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002484};
2485
Uwe Kleine-König22402522009-11-05 01:32:44 -08002486static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002487 .feature_table = features,
2488 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002489 .feature_table_legacy = features_legacy,
2490 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002491 .driver.name = KBUILD_MODNAME,
2492 .driver.owner = THIS_MODULE,
2493 .id_table = id_table,
2494 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002495 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002496 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302497#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302498 .freeze = virtnet_freeze,
2499 .restore = virtnet_restore,
2500#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002501};
2502
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002503static __init int virtio_net_driver_init(void)
2504{
2505 int ret;
2506
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002507 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002508 virtnet_cpu_online,
2509 virtnet_cpu_down_prep);
2510 if (ret < 0)
2511 goto out;
2512 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002513 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002514 NULL, virtnet_cpu_dead);
2515 if (ret)
2516 goto err_dead;
2517
2518 ret = register_virtio_driver(&virtio_net_driver);
2519 if (ret)
2520 goto err_virtio;
2521 return 0;
2522err_virtio:
2523 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2524err_dead:
2525 cpuhp_remove_multi_state(virtionet_online);
2526out:
2527 return ret;
2528}
2529module_init(virtio_net_driver_init);
2530
2531static __exit void virtio_net_driver_exit(void)
2532{
2533 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2534 cpuhp_remove_multi_state(virtionet_online);
2535 unregister_virtio_driver(&virtio_net_driver);
2536}
2537module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002538
2539MODULE_DEVICE_TABLE(virtio, id_table);
2540MODULE_DESCRIPTION("Virtio network driver");
2541MODULE_LICENSE("GPL");