blob: b0d241d110ec608848ed65054dfd54014e0afb86 [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Rusty Russell296f96f2007-10-22 11:03:37 +100017 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080021#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100022#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
John Fastabendf600b692016-12-15 12:13:24 -080025#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010026#include <linux/bpf_trace.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100027#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080028#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000030#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080031#include <linux/average.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100032
Amerigo Wangd34710e2013-05-09 19:50:51 +000033static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020034module_param(napi_weight, int, 0444);
35
Rusty Russelleb939922011-12-19 14:08:01 +000036static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050037module_param(csum, bool, 0444);
38module_param(gso, bool, 0444);
39
Rusty Russell296f96f2007-10-22 11:03:37 +100040/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080041#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080042#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100043
Jason Wangf6b10202017-02-21 16:46:28 +080044#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
45
John Fastabend2de2f7f2017-02-02 19:16:29 -080046/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
47#define VIRTIO_XDP_HEADROOM 256
48
Johannes Berg5377d7582015-08-19 09:48:40 +020049/* RX packet size EWMA. The average packet size is used to determine the packet
50 * buffer size when refilling RX rings. As the entire RX ring may be refilled
51 * at once, the weight is chosen so that the EWMA will be insensitive to short-
52 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080053 */
Johannes Bergeb1e0112017-02-15 09:49:26 +010054DECLARE_EWMA(pkt_len, 0, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080055
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020056/* With mergeable buffers we align buffer address and use the low bits to
57 * encode its true size. Buffer size is up to 1 page so we need to align to
58 * square root of page size to ensure we reserve enough bits to encode the true
59 * size.
60 */
61#define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
62
Michael Daltonab7db912014-01-16 22:23:27 -080063/* Minimum alignment for mergeable packet buffers. */
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020064#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
65 1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
Michael Daltonab7db912014-01-16 22:23:27 -080066
Rick Jones66846042011-11-14 14:17:08 +000067#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000068
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000069struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000070 struct u64_stats_sync tx_syncp;
71 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000072 u64 tx_bytes;
73 u64 tx_packets;
74
75 u64 rx_bytes;
76 u64 rx_packets;
77};
78
Jason Wange9d74172012-12-07 07:04:55 +000079/* Internal representation of a send virtqueue */
80struct send_queue {
81 /* Virtqueue associated with this send _queue */
82 struct virtqueue *vq;
83
84 /* TX: fragments + linear part + virtio header */
85 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000086
87 /* Name of the send queue: output.$index */
88 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000089};
90
91/* Internal representation of a receive virtqueue */
92struct receive_queue {
93 /* Virtqueue associated with this receive_queue */
94 struct virtqueue *vq;
95
Rusty Russell296f96f2007-10-22 11:03:37 +100096 struct napi_struct napi;
97
John Fastabendf600b692016-12-15 12:13:24 -080098 struct bpf_prog __rcu *xdp_prog;
99
Jason Wange9d74172012-12-07 07:04:55 +0000100 /* Chain pages by the private ptr. */
101 struct page *pages;
102
Michael Daltonab7db912014-01-16 22:23:27 -0800103 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200104 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800105
Michael Daltonfb518792014-01-16 22:23:26 -0800106 /* Page frag for packet buffer allocation. */
107 struct page_frag alloc_frag;
108
Jason Wange9d74172012-12-07 07:04:55 +0000109 /* RX: fragments + linear part + virtio header */
110 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000111
112 /* Name of this receive queue: input.$index */
113 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +0000114};
115
116struct virtnet_info {
117 struct virtio_device *vdev;
118 struct virtqueue *cvq;
119 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000120 struct send_queue *sq;
121 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000122 unsigned int status;
123
Jason Wang986a4f42012-12-07 07:04:56 +0000124 /* Max # of queue pairs supported by the device */
125 u16 max_queue_pairs;
126
127 /* # of queue pairs currently used by the driver */
128 u16 curr_queue_pairs;
129
John Fastabend672aafd2016-12-15 12:13:49 -0800130 /* # of XDP queue pairs currently used by the driver */
131 u16 xdp_queue_pairs;
132
Herbert Xu97402b92008-04-18 11:24:27 +0800133 /* I like... big packets and I cannot lie! */
134 bool big_packets;
135
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800136 /* Host will merge rx buffers for big packets (shake it! shake it!) */
137 bool mergeable_rx_bufs;
138
Jason Wang986a4f42012-12-07 07:04:56 +0000139 /* Has control virtqueue */
140 bool has_cvq;
141
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930142 /* Host can handle any s/g split between our header and packet data */
143 bool any_header_sg;
144
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300145 /* Packet virtio header size */
146 u8 hdr_len;
147
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000148 /* Active statistics */
149 struct virtnet_stats __percpu *stats;
150
Rusty Russell3161e452009-08-26 12:22:32 -0700151 /* Work struct for refilling if we run low on memory. */
152 struct delayed_work refill;
153
Jason Wang586d17c2012-04-11 20:43:52 +0000154 /* Work struct for config space updates */
155 struct work_struct config_work;
156
Jason Wang986a4f42012-12-07 07:04:56 +0000157 /* Does the affinity hint is set for virtqueues? */
158 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000159
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200160 /* CPU hotplug instances for online & dead */
161 struct hlist_node node;
162 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200163
164 /* Control VQ buffers: protected by the rtnl lock */
165 struct virtio_net_ctrl_hdr ctrl_hdr;
166 virtio_net_ctrl_ack ctrl_status;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700167 struct virtio_net_ctrl_mq ctrl_mq;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200168 u8 ctrl_promisc;
169 u8 ctrl_allmulti;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700170 u16 ctrl_vid;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100171
172 /* Ethtool settings */
173 u8 duplex;
174 u32 speed;
Rusty Russell296f96f2007-10-22 11:03:37 +1000175};
176
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000177struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300178 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000179 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300180 * hdr is in a separate sg buffer, and data sg buffer shares same page
181 * with this header sg. This padding makes next sg 16 byte aligned
182 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000183 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300184 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000185};
186
Jason Wang986a4f42012-12-07 07:04:56 +0000187/* Converting between virtqueue no. and kernel tx/rx queue no.
188 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
189 */
190static int vq2txq(struct virtqueue *vq)
191{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000192 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000193}
194
195static int txq2vq(int txq)
196{
197 return txq * 2 + 1;
198}
199
200static int vq2rxq(struct virtqueue *vq)
201{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000202 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000203}
204
205static int rxq2vq(int rxq)
206{
207 return rxq * 2;
208}
209
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300210static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000211{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300212 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000213}
214
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000215/*
216 * private is used to chain pages for big packets, put the whole
217 * most recent used list in the beginning for reuse
218 */
Jason Wange9d74172012-12-07 07:04:55 +0000219static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500220{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000221 struct page *end;
222
Jason Wange9d74172012-12-07 07:04:55 +0000223 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000224 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000225 end->private = (unsigned long)rq->pages;
226 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500227}
228
Jason Wange9d74172012-12-07 07:04:55 +0000229static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500230{
Jason Wange9d74172012-12-07 07:04:55 +0000231 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500232
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000233 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000234 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000235 /* clear private here, it is used to chain pages */
236 p->private = 0;
237 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500238 p = alloc_page(gfp_mask);
239 return p;
240}
241
Jason Wange9d74172012-12-07 07:04:55 +0000242static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000243{
Jason Wange9d74172012-12-07 07:04:55 +0000244 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000245
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500246 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000247 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000248
Rusty Russell363f1512008-06-08 20:51:55 +1000249 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000250 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000251}
252
Michael Daltonab7db912014-01-16 22:23:27 -0800253static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
254{
255 unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
256 return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
257}
258
259static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
260{
261 return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
262
263}
264
265static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
266{
267 unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
268 return (unsigned long)buf | (size - 1);
269}
270
Mike Waychison34646452012-01-04 12:52:32 +0000271/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300272static struct sk_buff *page_to_skb(struct virtnet_info *vi,
273 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700274 struct page *page, unsigned int offset,
275 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000276{
277 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300278 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700279 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000280 char *p;
281
Michael Dalton2613af02013-10-28 15:44:18 -0700282 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000283
284 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100285 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000286 if (unlikely(!skb))
287 return NULL;
288
289 hdr = skb_vnet_hdr(skb);
290
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300291 hdr_len = vi->hdr_len;
292 if (vi->mergeable_rx_bufs)
293 hdr_padded_len = sizeof *hdr;
294 else
Michael Dalton2613af02013-10-28 15:44:18 -0700295 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000296
297 memcpy(hdr, p, hdr_len);
298
299 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700300 offset += hdr_padded_len;
301 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000302
303 copy = len;
304 if (copy > skb_tailroom(skb))
305 copy = skb_tailroom(skb);
306 memcpy(skb_put(skb, copy), p, copy);
307
308 len -= copy;
309 offset += copy;
310
Michael Dalton2613af02013-10-28 15:44:18 -0700311 if (vi->mergeable_rx_bufs) {
312 if (len)
313 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
314 else
315 put_page(page);
316 return skb;
317 }
318
Sasha Levine878d782011-09-28 04:40:54 +0000319 /*
320 * Verify that we can indeed put this data into a skb.
321 * This is here to handle cases when the device erroneously
322 * tries to receive more than is possible. This is usually
323 * the case of a broken device.
324 */
325 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000326 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000327 dev_kfree_skb(skb);
328 return NULL;
329 }
Michael Dalton2613af02013-10-28 15:44:18 -0700330 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000331 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700332 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
333 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
334 frag_size, truesize);
335 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000336 page = (struct page *)page->private;
337 offset = 0;
338 }
339
340 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000341 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000342
343 return skb;
344}
345
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100346static bool virtnet_xdp_xmit(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800347 struct receive_queue *rq,
Jason Wangf6b10202017-02-21 16:46:28 +0800348 struct xdp_buff *xdp)
John Fastabend56434a02016-12-15 12:14:13 -0800349{
John Fastabend56434a02016-12-15 12:14:13 -0800350 struct virtio_net_hdr_mrg_rxbuf *hdr;
Jason Wangf6b10202017-02-21 16:46:28 +0800351 unsigned int len;
John Fastabend722d8282017-02-02 19:15:32 -0800352 struct send_queue *sq;
353 unsigned int qp;
John Fastabend56434a02016-12-15 12:14:13 -0800354 void *xdp_sent;
355 int err;
356
John Fastabend722d8282017-02-02 19:15:32 -0800357 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
358 sq = &vi->sq[qp];
359
John Fastabend56434a02016-12-15 12:14:13 -0800360 /* Free up any pending old buffers before queueing new ones. */
361 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Jason Wangf6b10202017-02-21 16:46:28 +0800362 struct page *sent_page = virt_to_head_page(xdp_sent);
Jason Wangbb91acc2016-12-23 22:37:32 +0800363
Jason Wangf6b10202017-02-21 16:46:28 +0800364 put_page(sent_page);
John Fastabend56434a02016-12-15 12:14:13 -0800365 }
366
Jason Wangf6b10202017-02-21 16:46:28 +0800367 xdp->data -= vi->hdr_len;
368 /* Zero header and leave csum up to XDP layers */
369 hdr = xdp->data;
370 memset(hdr, 0, vi->hdr_len);
John Fastabend56434a02016-12-15 12:14:13 -0800371
Jason Wangf6b10202017-02-21 16:46:28 +0800372 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
Jason Wangbb91acc2016-12-23 22:37:32 +0800373
Jason Wangf6b10202017-02-21 16:46:28 +0800374 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC);
John Fastabend56434a02016-12-15 12:14:13 -0800375 if (unlikely(err)) {
Jason Wangf6b10202017-02-21 16:46:28 +0800376 struct page *page = virt_to_head_page(xdp->data);
Jason Wangbb91acc2016-12-23 22:37:32 +0800377
Jason Wangf6b10202017-02-21 16:46:28 +0800378 put_page(page);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100379 return false;
John Fastabend56434a02016-12-15 12:14:13 -0800380 }
381
382 virtqueue_kick(sq->vq);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100383 return true;
John Fastabend56434a02016-12-15 12:14:13 -0800384}
385
Jason Wangf6b10202017-02-21 16:46:28 +0800386static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
387{
388 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
389}
390
Jason Wangbb91acc2016-12-23 22:37:32 +0800391static struct sk_buff *receive_small(struct net_device *dev,
392 struct virtnet_info *vi,
393 struct receive_queue *rq,
394 void *buf, unsigned int len)
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200395{
Jason Wangf6b10202017-02-21 16:46:28 +0800396 struct sk_buff *skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800397 struct bpf_prog *xdp_prog;
Jason Wangf6b10202017-02-21 16:46:28 +0800398 unsigned int xdp_headroom = virtnet_get_headroom(vi);
399 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
400 unsigned int headroom = vi->hdr_len + header_offset;
401 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
402 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
403 unsigned int delta = 0;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300404 len -= vi->hdr_len;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200405
Jason Wangbb91acc2016-12-23 22:37:32 +0800406 rcu_read_lock();
407 xdp_prog = rcu_dereference(rq->xdp_prog);
408 if (xdp_prog) {
Jason Wangf6b10202017-02-21 16:46:28 +0800409 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
John Fastabend0354e4d2017-02-02 19:15:01 -0800410 struct xdp_buff xdp;
Jason Wangf6b10202017-02-21 16:46:28 +0800411 void *orig_data;
Jason Wangbb91acc2016-12-23 22:37:32 +0800412 u32 act;
413
414 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
415 goto err_xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800416
Jason Wangf6b10202017-02-21 16:46:28 +0800417 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
418 xdp.data = xdp.data_hard_start + xdp_headroom;
John Fastabend0354e4d2017-02-02 19:15:01 -0800419 xdp.data_end = xdp.data + len;
Jason Wangf6b10202017-02-21 16:46:28 +0800420 orig_data = xdp.data;
John Fastabend0354e4d2017-02-02 19:15:01 -0800421 act = bpf_prog_run_xdp(xdp_prog, &xdp);
422
Jason Wangbb91acc2016-12-23 22:37:32 +0800423 switch (act) {
424 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800425 /* Recalculate length in case bpf program changed it */
Jason Wangf6b10202017-02-21 16:46:28 +0800426 delta = orig_data - xdp.data;
Jason Wangbb91acc2016-12-23 22:37:32 +0800427 break;
428 case XDP_TX:
Jason Wangf6b10202017-02-21 16:46:28 +0800429 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
John Fastabend0354e4d2017-02-02 19:15:01 -0800430 trace_xdp_exception(vi->dev, xdp_prog, act);
Jason Wangbb91acc2016-12-23 22:37:32 +0800431 rcu_read_unlock();
432 goto xdp_xmit;
Jason Wangbb91acc2016-12-23 22:37:32 +0800433 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800434 bpf_warn_invalid_xdp_action(act);
435 case XDP_ABORTED:
436 trace_xdp_exception(vi->dev, xdp_prog, act);
437 case XDP_DROP:
Jason Wangbb91acc2016-12-23 22:37:32 +0800438 goto err_xdp;
439 }
440 }
441 rcu_read_unlock();
442
Jason Wangf6b10202017-02-21 16:46:28 +0800443 skb = build_skb(buf, buflen);
444 if (!skb) {
445 put_page(virt_to_head_page(buf));
446 goto err;
447 }
448 skb_reserve(skb, headroom - delta);
449 skb_put(skb, len + delta);
450 if (!delta) {
451 buf += header_offset;
452 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
453 } /* keep zeroed vnet hdr since packet was changed by bpf */
454
455err:
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200456 return skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800457
458err_xdp:
459 rcu_read_unlock();
460 dev->stats.rx_dropped++;
Jason Wangf6b10202017-02-21 16:46:28 +0800461 put_page(virt_to_head_page(buf));
Jason Wangbb91acc2016-12-23 22:37:32 +0800462xdp_xmit:
463 return NULL;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200464}
465
466static struct sk_buff *receive_big(struct net_device *dev,
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300467 struct virtnet_info *vi,
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200468 struct receive_queue *rq,
469 void *buf,
470 unsigned int len)
471{
472 struct page *page = buf;
Jason Wangc47a43d2016-12-23 22:37:31 +0800473 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200474
475 if (unlikely(!skb))
476 goto err;
477
478 return skb;
479
480err:
481 dev->stats.rx_dropped++;
482 give_pages(rq, page);
483 return NULL;
484}
485
John Fastabend72979a62016-12-15 12:14:36 -0800486/* The conditions to enable XDP should preclude the underlying device from
487 * sending packets across multiple buffers (num_buf > 1). However per spec
488 * it does not appear to be illegal to do so but rather just against convention.
489 * So in order to avoid making a system unresponsive the packets are pushed
490 * into a page and the XDP program is run. This will be extremely slow and we
491 * push a warning to the user to fix this as soon as possible. Fixing this may
492 * require resolving the underlying hardware to determine why multiple buffers
493 * are being received or simply loading the XDP program in the ingress stack
494 * after the skb is built because there is no advantage to running it here
495 * anymore.
496 */
497static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800498 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800499 struct page *p,
500 int offset,
501 unsigned int *len)
502{
503 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800504 unsigned int page_off = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800505
506 if (!page)
507 return NULL;
508
509 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
510 page_off += *len;
511
Jason Wang56a86f82016-12-23 22:37:26 +0800512 while (--*num_buf) {
John Fastabend72979a62016-12-15 12:14:36 -0800513 unsigned int buflen;
514 unsigned long ctx;
515 void *buf;
516 int off;
517
518 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
519 if (unlikely(!ctx))
520 goto err_buf;
521
John Fastabend72979a62016-12-15 12:14:36 -0800522 buf = mergeable_ctx_to_buf_address(ctx);
523 p = virt_to_head_page(buf);
524 off = buf - page_address(p);
525
Jason Wang56a86f82016-12-23 22:37:26 +0800526 /* guard against a misconfigured or uncooperative backend that
527 * is sending packet larger than the MTU.
528 */
529 if ((page_off + buflen) > PAGE_SIZE) {
530 put_page(p);
531 goto err_buf;
532 }
533
John Fastabend72979a62016-12-15 12:14:36 -0800534 memcpy(page_address(page) + page_off,
535 page_address(p) + off, buflen);
536 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800537 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800538 }
539
John Fastabend2de2f7f2017-02-02 19:16:29 -0800540 /* Headroom does not contribute to packet length */
541 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800542 return page;
543err_buf:
544 __free_pages(page, 0);
545 return NULL;
546}
547
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200548static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200549 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200550 struct receive_queue *rq,
Michael Daltonab7db912014-01-16 22:23:27 -0800551 unsigned long ctx,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200552 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000553{
Michael Daltonab7db912014-01-16 22:23:27 -0800554 void *buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300555 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
556 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200557 struct page *page = virt_to_head_page(buf);
558 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800559 struct sk_buff *head_skb, *curr_skb;
560 struct bpf_prog *xdp_prog;
561 unsigned int truesize;
Michael Daltonab7db912014-01-16 22:23:27 -0800562
John Fastabend56434a02016-12-15 12:14:13 -0800563 head_skb = NULL;
564
John Fastabendf600b692016-12-15 12:13:24 -0800565 rcu_read_lock();
566 xdp_prog = rcu_dereference(rq->xdp_prog);
567 if (xdp_prog) {
John Fastabend72979a62016-12-15 12:14:36 -0800568 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800569 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800570 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800571 u32 act;
572
Jason Wang73b62bd2016-12-23 22:37:24 +0800573 /* This happens when rx buffer size is underestimated */
John Fastabendf600b692016-12-15 12:13:24 -0800574 if (unlikely(num_buf > 1)) {
John Fastabend72979a62016-12-15 12:14:36 -0800575 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800576 xdp_page = xdp_linearize_page(rq, &num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800577 page, offset, &len);
578 if (!xdp_page)
579 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800580 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800581 } else {
582 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800583 }
584
585 /* Transient failure which in theory could occur if
586 * in-flight packets from before XDP was enabled reach
587 * the receive path after XDP is loaded. In practice I
588 * was not able to create this condition.
589 */
Jason Wangb00f70b2016-12-23 22:37:28 +0800590 if (unlikely(hdr->hdr.gso_type))
John Fastabendf600b692016-12-15 12:13:24 -0800591 goto err_xdp;
592
John Fastabend2de2f7f2017-02-02 19:16:29 -0800593 /* Allow consuming headroom but reserve enough space to push
594 * the descriptor on if we get an XDP_TX return code.
595 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800596 data = page_address(xdp_page) + offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800597 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
John Fastabend0354e4d2017-02-02 19:15:01 -0800598 xdp.data = data + vi->hdr_len;
599 xdp.data_end = xdp.data + (len - vi->hdr_len);
600 act = bpf_prog_run_xdp(xdp_prog, &xdp);
601
John Fastabend56434a02016-12-15 12:14:13 -0800602 switch (act) {
603 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800604 /* recalculate offset to account for any header
605 * adjustments. Note other cases do not build an
606 * skb and avoid using offset
607 */
608 offset = xdp.data -
609 page_address(xdp_page) - vi->hdr_len;
610
Jason Wang1830f892016-12-23 22:37:27 +0800611 /* We can only create skb based on xdp_page. */
612 if (unlikely(xdp_page != page)) {
613 rcu_read_unlock();
614 put_page(page);
615 head_skb = page_to_skb(vi, rq, xdp_page,
John Fastabend2de2f7f2017-02-02 19:16:29 -0800616 offset, len, PAGE_SIZE);
Jason Wang5c334742016-12-23 22:37:29 +0800617 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
Jason Wang1830f892016-12-23 22:37:27 +0800618 return head_skb;
619 }
John Fastabend56434a02016-12-15 12:14:13 -0800620 break;
621 case XDP_TX:
Jason Wangf6b10202017-02-21 16:46:28 +0800622 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
John Fastabend0354e4d2017-02-02 19:15:01 -0800623 trace_xdp_exception(vi->dev, xdp_prog, act);
Jason Wang5c334742016-12-23 22:37:29 +0800624 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabend72979a62016-12-15 12:14:36 -0800625 if (unlikely(xdp_page != page))
626 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800627 rcu_read_unlock();
628 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -0800629 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800630 bpf_warn_invalid_xdp_action(act);
631 case XDP_ABORTED:
632 trace_xdp_exception(vi->dev, xdp_prog, act);
633 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -0800634 if (unlikely(xdp_page != page))
635 __free_pages(xdp_page, 0);
Jason Wang5c334742016-12-23 22:37:29 +0800636 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabendf600b692016-12-15 12:13:24 -0800637 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800638 }
John Fastabendf600b692016-12-15 12:13:24 -0800639 }
640 rcu_read_unlock();
641
642 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
643 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
644 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000645
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200646 if (unlikely(!curr_skb))
647 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000648 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200649 int num_skb_frags;
650
Michael Daltonab7db912014-01-16 22:23:27 -0800651 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
652 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200653 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200654 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300655 virtio16_to_cpu(vi->vdev,
656 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200657 dev->stats.rx_length_errors++;
658 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000659 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200660
Michael Daltonab7db912014-01-16 22:23:27 -0800661 buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200662 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200663
664 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700665 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
666 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200667
668 if (unlikely(!nskb))
669 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700670 if (curr_skb == head_skb)
671 skb_shinfo(curr_skb)->frag_list = nskb;
672 else
673 curr_skb->next = nskb;
674 curr_skb = nskb;
675 head_skb->truesize += nskb->truesize;
676 num_skb_frags = 0;
677 }
Michael Daltonab7db912014-01-16 22:23:27 -0800678 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
Michael Dalton2613af02013-10-28 15:44:18 -0700679 if (curr_skb != head_skb) {
680 head_skb->data_len += len;
681 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800682 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700683 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200684 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800685 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
686 put_page(page);
687 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800688 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800689 } else {
690 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800691 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800692 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200693 }
694
Johannes Berg5377d7582015-08-19 09:48:40 +0200695 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200696 return head_skb;
697
John Fastabendf600b692016-12-15 12:13:24 -0800698err_xdp:
699 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200700err_skb:
701 put_page(page);
702 while (--num_buf) {
Michael Daltonab7db912014-01-16 22:23:27 -0800703 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
704 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200705 pr_debug("%s: rx error: %d buffers missing\n",
706 dev->name, num_buf);
707 dev->stats.rx_length_errors++;
708 break;
709 }
Michael Daltonab7db912014-01-16 22:23:27 -0800710 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200711 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000712 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200713err_buf:
714 dev->stats.rx_dropped++;
715 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800716xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200717 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000718}
719
Jason Wang61845d22017-02-17 11:33:09 +0800720static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
721 void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000722{
Jason Wange9d74172012-12-07 07:04:55 +0000723 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000724 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300725 struct virtio_net_hdr_mrg_rxbuf *hdr;
Jason Wang61845d22017-02-17 11:33:09 +0800726 int ret;
Rusty Russell296f96f2007-10-22 11:03:37 +1000727
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300728 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000729 pr_debug("%s: short packet %i\n", dev->name, len);
730 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800731 if (vi->mergeable_rx_bufs) {
732 unsigned long ctx = (unsigned long)buf;
733 void *base = mergeable_ctx_to_buf_address(ctx);
734 put_page(virt_to_head_page(base));
735 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800736 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800737 } else {
Jason Wangf6b10202017-02-21 16:46:28 +0800738 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -0800739 }
Jason Wang61845d22017-02-17 11:33:09 +0800740 return 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000741 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000742
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200743 if (vi->mergeable_rx_bufs)
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200744 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200745 else if (vi->big_packets)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300746 skb = receive_big(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200747 else
Jason Wangbb91acc2016-12-23 22:37:32 +0800748 skb = receive_small(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200749
750 if (unlikely(!skb))
Jason Wang61845d22017-02-17 11:33:09 +0800751 return 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800752
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000753 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000754
Jason Wang61845d22017-02-17 11:33:09 +0800755 ret = skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000756
Mike Rapoporte858fae2016-06-08 16:09:21 +0300757 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +0000758 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000759
Mike Rapoporte858fae2016-06-08 16:09:21 +0300760 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
761 virtio_is_little_endian(vi->vdev))) {
762 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
763 dev->name, hdr->hdr.gso_type,
764 hdr->hdr.gso_size);
765 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000766 }
767
Mike Rapoportd1dc06d2016-06-14 08:29:38 +0300768 skb->protocol = eth_type_trans(skb, dev);
769 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
770 ntohs(skb->protocol), skb->len, skb->pkt_type);
771
Eric Dumazet0fbd0502015-07-31 18:25:17 +0200772 napi_gro_receive(&rq->napi, skb);
Jason Wang61845d22017-02-17 11:33:09 +0800773 return ret;
Rusty Russell296f96f2007-10-22 11:03:37 +1000774
775frame_err:
776 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000777 dev_kfree_skb(skb);
Jason Wang61845d22017-02-17 11:33:09 +0800778 return 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000779}
780
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300781static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
782 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000783{
Jason Wangf6b10202017-02-21 16:46:28 +0800784 struct page_frag *alloc_frag = &rq->alloc_frag;
785 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800786 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wangf6b10202017-02-21 16:46:28 +0800787 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000788 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000789
Jason Wangf6b10202017-02-21 16:46:28 +0800790 len = SKB_DATA_ALIGN(len) +
791 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
792 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000793 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800794
Jason Wangf6b10202017-02-21 16:46:28 +0800795 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
796 get_page(alloc_frag->page);
797 alloc_frag->offset += len;
798 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
799 vi->hdr_len + GOOD_PACKET_LEN);
800 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000801 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +0800802 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000803
804 return err;
805}
806
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300807static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
808 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000809{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000810 struct page *first, *list = NULL;
811 char *p;
812 int i, err, offset;
813
Rusty Russella5835442014-09-11 10:17:36 +0930814 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
815
Jason Wange9d74172012-12-07 07:04:55 +0000816 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000817 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000818 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000819 if (!first) {
820 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000821 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000822 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700823 }
Jason Wange9d74172012-12-07 07:04:55 +0000824 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000825
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000826 /* chain new page in list head to match sg */
827 first->private = (unsigned long)list;
828 list = first;
829 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800830
Jason Wange9d74172012-12-07 07:04:55 +0000831 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000832 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000833 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000834 return -ENOMEM;
835 }
836 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800837
Jason Wange9d74172012-12-07 07:04:55 +0000838 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300839 /* a separated rq->sg[0] for header - required in case !any_header_sg */
840 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +0800841
Jason Wange9d74172012-12-07 07:04:55 +0000842 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000843 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000844 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800845
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000846 /* chain first in list head */
847 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030848 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
849 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000850 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000851 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800852
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000853 return err;
854}
Herbert Xu97402b92008-04-18 11:24:27 +0800855
Johannes Berg5377d7582015-08-19 09:48:40 +0200856static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000857{
Michael Daltonab7db912014-01-16 22:23:27 -0800858 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800859 unsigned int len;
860
Johannes Berg5377d7582015-08-19 09:48:40 +0200861 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael Daltonfbf28d72014-01-16 22:23:30 -0800862 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
863 return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
864}
865
John Fastabend2de2f7f2017-02-02 19:16:29 -0800866static int add_recvbuf_mergeable(struct virtnet_info *vi,
867 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -0800868{
Michael Daltonfb518792014-01-16 22:23:26 -0800869 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800870 unsigned int headroom = virtnet_get_headroom(vi);
Michael Daltonfb518792014-01-16 22:23:26 -0800871 char *buf;
Michael Daltonab7db912014-01-16 22:23:27 -0800872 unsigned long ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000873 int err;
Michael Daltonfb518792014-01-16 22:23:26 -0800874 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +1000875
Michael Daltonfbf28d72014-01-16 22:23:30 -0800876 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800877 if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000878 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -0800879
Michael Daltonfb518792014-01-16 22:23:26 -0800880 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800881 buf += headroom; /* advance address leaving hole at front of pkt */
Michael Daltonab7db912014-01-16 22:23:27 -0800882 ctx = mergeable_buf_to_ctx(buf, len);
Michael Daltonfb518792014-01-16 22:23:26 -0800883 get_page(alloc_frag->page);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800884 alloc_frag->offset += len + headroom;
Michael Daltonfb518792014-01-16 22:23:26 -0800885 hole = alloc_frag->size - alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800886 if (hole < len + headroom) {
Michael Daltonab7db912014-01-16 22:23:27 -0800887 /* To avoid internal fragmentation, if there is very likely not
888 * enough space for another buffer, add the remaining space to
889 * the current buffer. This extra space is not included in
890 * the truesize stored in ctx.
891 */
Michael Daltonfb518792014-01-16 22:23:26 -0800892 len += hole;
893 alloc_frag->offset += hole;
894 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000895
Michael Daltonfb518792014-01-16 22:23:26 -0800896 sg_init_one(rq->sg, buf, len);
Michael Daltonab7db912014-01-16 22:23:27 -0800897 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000898 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700899 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000900
901 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000902}
903
Rusty Russellb2baed62011-12-29 00:42:38 +0000904/*
905 * Returns false if we couldn't fill entirely (OOM).
906 *
907 * Normally run in the receive path, but can also be run from ndo_open
908 * before we're receiving packets, or from refill_work which is
909 * careful to disable receiving (using napi_disable).
910 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300911static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
912 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800913{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800914 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000915 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800916
Michael Daltonfb518792014-01-16 22:23:26 -0800917 gfp |= __GFP_COLD;
Amit Shah0aea51c2009-08-26 14:58:28 +0530918 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000919 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -0800920 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000921 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300922 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000923 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300924 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800925
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000926 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030927 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800928 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800929 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +0800930 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700931 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800932}
933
Rusty Russell18445c42008-02-04 23:49:57 -0500934static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000935{
936 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000937 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000938
Rusty Russell18445c42008-02-04 23:49:57 -0500939 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000940 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300941 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000942 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500943 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000944}
945
Jason Wange9d74172012-12-07 07:04:55 +0000946static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800947{
Jason Wange9d74172012-12-07 07:04:55 +0000948 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800949
950 /* If all buffers were filled by other side before we napi_enabled, we
951 * won't get another interrupt, so process any outstanding packets
952 * now. virtnet_poll wants re-enable the queue, so we disable here.
953 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000954 if (napi_schedule_prep(&rq->napi)) {
955 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300956 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000957 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300958 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800959 }
960}
961
Rusty Russell3161e452009-08-26 12:22:32 -0700962static void refill_work(struct work_struct *work)
963{
Jason Wange9d74172012-12-07 07:04:55 +0000964 struct virtnet_info *vi =
965 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700966 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000967 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700968
Sasha Levin55257d72013-04-29 12:00:08 +0930969 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000970 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700971
Jason Wang986a4f42012-12-07 07:04:56 +0000972 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300973 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +0000974 virtnet_napi_enable(rq);
975
976 /* In theory, this can happen: if we don't get any buffers in
977 * we will *never* try to fill again.
978 */
979 if (still_empty)
980 schedule_delayed_work(&vi->refill, HZ/2);
981 }
Rusty Russell3161e452009-08-26 12:22:32 -0700982}
983
Jason Wang2ffa7592014-07-23 16:33:54 +0800984static int virtnet_receive(struct receive_queue *rq, int budget)
Rusty Russell296f96f2007-10-22 11:03:37 +1000985{
Jason Wange9d74172012-12-07 07:04:55 +0000986 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wang61845d22017-02-17 11:33:09 +0800987 unsigned int len, received = 0, bytes = 0;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000988 void *buf;
Jason Wang61845d22017-02-17 11:33:09 +0800989 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
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) {
Jason Wang61845d22017-02-17 11:33:09 +0800993 bytes += 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 Wang61845d22017-02-17 11:33:09 +08001002 u64_stats_update_begin(&stats->rx_syncp);
1003 stats->rx_bytes += bytes;
1004 stats->rx_packets += received;
1005 u64_stats_update_end(&stats->rx_syncp);
1006
Jason Wang2ffa7592014-07-23 16:33:54 +08001007 return received;
1008}
1009
1010static int virtnet_poll(struct napi_struct *napi, int budget)
1011{
1012 struct receive_queue *rq =
1013 container_of(napi, struct receive_queue, napi);
Li RongQingfaadb052015-03-26 15:39:45 +08001014 unsigned int r, received;
Jason Wang2ffa7592014-07-23 16:33:54 +08001015
Li RongQingfaadb052015-03-26 15:39:45 +08001016 received = virtnet_receive(rq, budget);
Jason Wang2ffa7592014-07-23 16:33:54 +08001017
Rusty Russell8329d982007-11-19 11:20:43 -05001018 /* Out of packets? */
1019 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001020 r = virtqueue_enable_cb_prepare(rq->vq);
Eric Dumazet4d6308aa2017-02-04 07:49:21 -08001021 if (napi_complete_done(napi, received)) {
1022 if (unlikely(virtqueue_poll(rq->vq, r)) &&
1023 napi_schedule_prep(napi)) {
1024 virtqueue_disable_cb(rq->vq);
1025 __napi_schedule(napi);
1026 }
Christian Borntraeger4265f162008-03-14 14:17:05 +01001027 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001028 }
1029
1030 return received;
1031}
1032
Jason Wang986a4f42012-12-07 07:04:56 +00001033static int virtnet_open(struct net_device *dev)
1034{
1035 struct virtnet_info *vi = netdev_priv(dev);
1036 int i;
1037
Jason Wange4166622013-05-21 20:03:58 +00001038 for (i = 0; i < vi->max_queue_pairs; i++) {
1039 if (i < vi->curr_queue_pairs)
1040 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001041 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001042 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +00001043 virtnet_napi_enable(&vi->rq[i]);
1044 }
1045
1046 return 0;
1047}
1048
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001049static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001050{
1051 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +10301052 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +00001053 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +00001054 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Jason Wang61845d22017-02-17 11:33:09 +08001055 unsigned int packets = 0;
1056 unsigned int bytes = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +10001057
Jason Wange9d74172012-12-07 07:04:55 +00001058 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001059 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001060
Jason Wang61845d22017-02-17 11:33:09 +08001061 bytes += skb->len;
1062 packets++;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001063
Eric Dumazeted79bab2009-10-14 14:36:43 +00001064 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001065 }
Jason Wang61845d22017-02-17 11:33:09 +08001066
1067 /* Avoid overhead when no packets have been processed
1068 * happens when called speculatively from start_xmit.
1069 */
1070 if (!packets)
1071 return;
1072
1073 u64_stats_update_begin(&stats->tx_syncp);
1074 stats->tx_bytes += bytes;
1075 stats->tx_packets += packets;
1076 u64_stats_update_end(&stats->tx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +10001077}
1078
Jason Wange9d74172012-12-07 07:04:55 +00001079static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001080{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001081 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001082 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001083 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +10301084 unsigned num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001085 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301086 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001087
Johannes Berge1749612008-10-27 15:59:26 -07001088 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301089
1090 can_push = vi->any_header_sg &&
1091 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1092 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1093 /* Even if we can, don't push here yet as this would skew
1094 * csum_start offset below. */
1095 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001096 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301097 else
1098 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001099
Mike Rapoporte858fae2016-06-08 16:09:21 +03001100 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Jason Wang6391a442017-01-20 14:32:42 +08001101 virtio_is_little_endian(vi->vdev), false))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001102 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001103
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001104 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001105 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001106
Jason Wang547c8902015-08-27 14:53:06 +08001107 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301108 if (can_push) {
1109 __skb_push(skb, hdr_len);
1110 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1111 /* Pull header back to avoid skew in tx bytes calculations. */
1112 __skb_pull(skb, hdr_len);
1113 } else {
1114 sg_set_buf(sq->sg, hdr, hdr_len);
1115 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1116 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301117 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001118}
1119
Stephen Hemminger424efe92009-08-31 19:50:51 +00001120static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001121{
1122 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001123 int qnum = skb_get_queue_mapping(skb);
1124 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301125 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001126 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1127 bool kick = !skb->xmit_more;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001128
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001129 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001130 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001131
Jacob Keller074c3582014-06-25 02:37:13 +00001132 /* timestamp packet in software */
1133 skb_tx_timestamp(skb);
1134
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001135 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001136 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001137
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301138 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001139 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301140 dev->stats.tx_fifo_errors++;
1141 if (net_ratelimit())
1142 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001143 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001144 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001145 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001146 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001147 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001148
Rusty Russell48925e32009-09-24 09:59:20 -06001149 /* Don't wait up for transmitted skbs to be freed. */
1150 skb_orphan(skb);
1151 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -05001152
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001153 /* If running out of space, stop queue to avoid getting packets that we
1154 * are then unable to transmit.
1155 * An alternative would be to force queuing layer to requeue the skb by
1156 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1157 * returned in a normal path of operation: it means that driver is not
1158 * maintaining the TX queue stop/start state properly, and causes
1159 * the stack to do a non-trivial amount of useless work.
1160 * Since most packets only take 1 or 2 ring slots, stopping the queue
1161 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001162 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001163 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001164 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001165 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001166 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001167 free_old_xmit_skbs(sq);
1168 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001169 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001170 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001171 }
1172 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001173 }
Rusty Russell48925e32009-09-24 09:59:20 -06001174
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001175 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001176 virtqueue_kick(sq->vq);
1177
Rusty Russell48925e32009-09-24 09:59:20 -06001178 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001179}
1180
Amos Kong40cbfc32013-01-21 01:17:21 +00001181/*
1182 * Send command via the control virtqueue and check status. Commands
1183 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001184 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001185 */
1186static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001187 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001188{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301189 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001190 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001191
1192 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301193 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001194
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001195 vi->ctrl_status = ~0;
1196 vi->ctrl_hdr.class = class;
1197 vi->ctrl_hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301198 /* Add header */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001199 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301200 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001201
Rusty Russellf7bc9592013-03-20 15:44:28 +10301202 if (out)
1203 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001204
Rusty Russellf7bc9592013-03-20 15:44:28 +10301205 /* Add return status. */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001206 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001207 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001208
stephen hemmingerd24bae32013-12-09 16:17:40 -08001209 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301210 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001211
Heinz Graalfs67975902013-10-29 09:40:02 +10301212 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001213 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001214
1215 /* Spin for a response, the kick causes an ioport write, trapping
1216 * into the hypervisor, so the request should be handled immediately.
1217 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301218 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1219 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001220 cpu_relax();
1221
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001222 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001223}
1224
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001225static int virtnet_set_mac_address(struct net_device *dev, void *p)
1226{
1227 struct virtnet_info *vi = netdev_priv(dev);
1228 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001229 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001230 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001231 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001232
Shyam Saini801822d2016-12-24 00:44:58 +05301233 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001234 if (!addr)
1235 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001236
1237 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001238 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001239 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001240
Amos Kong7e58d5a2013-01-21 01:17:23 +00001241 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1242 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1243 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001244 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001245 dev_warn(&vdev->dev,
1246 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001247 ret = -EINVAL;
1248 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001249 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001250 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1251 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301252 unsigned int i;
1253
1254 /* Naturally, this has an atomicity problem. */
1255 for (i = 0; i < dev->addr_len; i++)
1256 virtio_cwrite8(vdev,
1257 offsetof(struct virtio_net_config, mac) +
1258 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001259 }
1260
1261 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001262 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001263
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001264out:
1265 kfree(addr);
1266 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001267}
1268
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001269static void virtnet_stats(struct net_device *dev,
1270 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001271{
1272 struct virtnet_info *vi = netdev_priv(dev);
1273 int cpu;
1274 unsigned int start;
1275
1276 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +00001277 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001278 u64 tpackets, tbytes, rpackets, rbytes;
1279
1280 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001281 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001282 tpackets = stats->tx_packets;
1283 tbytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001284 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001285
1286 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001287 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001288 rpackets = stats->rx_packets;
1289 rbytes = stats->rx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001290 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001291
1292 tot->rx_packets += rpackets;
1293 tot->tx_packets += tpackets;
1294 tot->rx_bytes += rbytes;
1295 tot->tx_bytes += tbytes;
1296 }
1297
1298 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001299 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001300 tot->rx_dropped = dev->stats.rx_dropped;
1301 tot->rx_length_errors = dev->stats.rx_length_errors;
1302 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001303}
1304
Amit Shahda74e892008-02-29 16:24:50 +05301305#ifdef CONFIG_NET_POLL_CONTROLLER
1306static void virtnet_netpoll(struct net_device *dev)
1307{
1308 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001309 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301310
Jason Wang986a4f42012-12-07 07:04:56 +00001311 for (i = 0; i < vi->curr_queue_pairs; i++)
1312 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301313}
1314#endif
1315
Jason Wang586d17c2012-04-11 20:43:52 +00001316static void virtnet_ack_link_announce(struct virtnet_info *vi)
1317{
1318 rtnl_lock();
1319 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001320 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001321 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1322 rtnl_unlock();
1323}
1324
John Fastabend473153292017-02-02 19:14:32 -08001325static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001326{
1327 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001328 struct net_device *dev = vi->dev;
1329
1330 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1331 return 0;
1332
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001333 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1334 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001335
1336 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001337 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001338 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1339 queue_pairs);
1340 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301341 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001342 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001343 /* virtnet_open() will refill when device is going to up. */
1344 if (dev->flags & IFF_UP)
1345 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301346 }
Jason Wang986a4f42012-12-07 07:04:56 +00001347
1348 return 0;
1349}
1350
John Fastabend473153292017-02-02 19:14:32 -08001351static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1352{
1353 int err;
1354
1355 rtnl_lock();
1356 err = _virtnet_set_queues(vi, queue_pairs);
1357 rtnl_unlock();
1358 return err;
1359}
1360
Rusty Russell296f96f2007-10-22 11:03:37 +10001361static int virtnet_close(struct net_device *dev)
1362{
1363 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001364 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001365
Rusty Russellb2baed62011-12-29 00:42:38 +00001366 /* Make sure refill_work doesn't re-enable napi! */
1367 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001368
1369 for (i = 0; i < vi->max_queue_pairs; i++)
1370 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001371
Rusty Russell296f96f2007-10-22 11:03:37 +10001372 return 0;
1373}
1374
Alex Williamson2af76982009-02-04 09:02:40 +00001375static void virtnet_set_rx_mode(struct net_device *dev)
1376{
1377 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001378 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001379 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001380 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001381 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001382 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001383 void *buf;
1384 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001385
stephen hemminger788a8b62013-12-09 16:18:45 -08001386 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001387 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1388 return;
1389
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001390 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1391 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001392
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001393 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001394
1395 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001396 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001397 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001398 vi->ctrl_promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001399
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001400 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001401
1402 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001403 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001404 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001405 vi->ctrl_allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001406
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001407 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001408 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001409 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001410 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1411 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1412 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001413 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001414 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001415
Alex Williamson23e258e2009-05-01 17:27:56 +00001416 sg_init_table(sg, 2);
1417
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001418 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001419 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001420 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001421 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001422 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001423
1424 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001425 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001426
1427 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001428 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001429
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001430 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001431 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001432 netdev_for_each_mc_addr(ha, dev)
1433 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001434
1435 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001436 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001437
1438 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001439 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001440 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001441
1442 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001443}
1444
Patrick McHardy80d5c362013-04-19 02:04:28 +00001445static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1446 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001447{
1448 struct virtnet_info *vi = netdev_priv(dev);
1449 struct scatterlist sg;
1450
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001451 vi->ctrl_vid = vid;
1452 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001453
1454 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001455 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001456 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001457 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001458}
1459
Patrick McHardy80d5c362013-04-19 02:04:28 +00001460static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1461 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001462{
1463 struct virtnet_info *vi = netdev_priv(dev);
1464 struct scatterlist sg;
1465
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001466 vi->ctrl_vid = vid;
1467 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001468
1469 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001470 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001471 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001472 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001473}
1474
Wanlong Gao8898c212013-01-24 23:51:30 +00001475static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001476{
1477 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001478
1479 if (vi->affinity_hint_set) {
1480 for (i = 0; i < vi->max_queue_pairs; i++) {
1481 virtqueue_set_affinity(vi->rq[i].vq, -1);
1482 virtqueue_set_affinity(vi->sq[i].vq, -1);
1483 }
1484
1485 vi->affinity_hint_set = false;
1486 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001487}
1488
1489static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001490{
1491 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001492 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001493
1494 /* In multiqueue mode, when the number of cpu is equal to the number of
1495 * queue pairs, we let the queue pairs to be private to one cpu by
1496 * setting the affinity hint to eliminate the contention.
1497 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001498 if (vi->curr_queue_pairs == 1 ||
1499 vi->max_queue_pairs != num_online_cpus()) {
1500 virtnet_clean_affinity(vi, -1);
1501 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001502 }
1503
Wanlong Gao8898c212013-01-24 23:51:30 +00001504 i = 0;
1505 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001506 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1507 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001508 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001509 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001510 }
1511
Wanlong Gao8898c212013-01-24 23:51:30 +00001512 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001513}
1514
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001515static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001516{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001517 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1518 node);
1519 virtnet_set_affinity(vi);
1520 return 0;
1521}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001522
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001523static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1524{
1525 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1526 node_dead);
1527 virtnet_set_affinity(vi);
1528 return 0;
1529}
Jason Wang3ab098d2013-10-15 11:18:58 +08001530
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001531static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1532{
1533 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1534 node);
1535
1536 virtnet_clean_affinity(vi, cpu);
1537 return 0;
1538}
1539
1540static enum cpuhp_state virtionet_online;
1541
1542static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1543{
1544 int ret;
1545
1546 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1547 if (ret)
1548 return ret;
1549 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1550 &vi->node_dead);
1551 if (!ret)
1552 return ret;
1553 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1554 return ret;
1555}
1556
1557static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1558{
1559 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1560 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1561 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001562}
1563
Rick Jones8f9f4662011-10-19 08:10:59 +00001564static void virtnet_get_ringparam(struct net_device *dev,
1565 struct ethtool_ringparam *ring)
1566{
1567 struct virtnet_info *vi = netdev_priv(dev);
1568
Jason Wang986a4f42012-12-07 07:04:56 +00001569 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1570 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001571 ring->rx_pending = ring->rx_max_pending;
1572 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001573}
1574
Rick Jones66846042011-11-14 14:17:08 +00001575
1576static void virtnet_get_drvinfo(struct net_device *dev,
1577 struct ethtool_drvinfo *info)
1578{
1579 struct virtnet_info *vi = netdev_priv(dev);
1580 struct virtio_device *vdev = vi->vdev;
1581
1582 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1583 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1584 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1585
1586}
1587
Jason Wangd73bcd22012-12-07 07:04:57 +00001588/* TODO: Eliminate OOO packets during switching */
1589static int virtnet_set_channels(struct net_device *dev,
1590 struct ethtool_channels *channels)
1591{
1592 struct virtnet_info *vi = netdev_priv(dev);
1593 u16 queue_pairs = channels->combined_count;
1594 int err;
1595
1596 /* We don't support separate rx/tx channels.
1597 * We don't allow setting 'other' channels.
1598 */
1599 if (channels->rx_count || channels->tx_count || channels->other_count)
1600 return -EINVAL;
1601
Amos Kongc18e9cd2014-04-18 13:45:41 +08001602 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001603 return -EINVAL;
1604
John Fastabendf600b692016-12-15 12:13:24 -08001605 /* For now we don't support modifying channels while XDP is loaded
1606 * also when XDP is loaded all RX queues have XDP programs so we only
1607 * need to check a single RX queue.
1608 */
1609 if (vi->rq[0].xdp_prog)
1610 return -EINVAL;
1611
Wanlong Gao47be2472013-01-24 23:51:29 +00001612 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08001613 err = _virtnet_set_queues(vi, queue_pairs);
Jason Wangd73bcd22012-12-07 07:04:57 +00001614 if (!err) {
1615 netif_set_real_num_tx_queues(dev, queue_pairs);
1616 netif_set_real_num_rx_queues(dev, queue_pairs);
1617
Wanlong Gao8898c212013-01-24 23:51:30 +00001618 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001619 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001620 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001621
1622 return err;
1623}
1624
1625static void virtnet_get_channels(struct net_device *dev,
1626 struct ethtool_channels *channels)
1627{
1628 struct virtnet_info *vi = netdev_priv(dev);
1629
1630 channels->combined_count = vi->curr_queue_pairs;
1631 channels->max_combined = vi->max_queue_pairs;
1632 channels->max_other = 0;
1633 channels->rx_count = 0;
1634 channels->tx_count = 0;
1635 channels->other_count = 0;
1636}
1637
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001638/* Check if the user is trying to change anything besides speed/duplex */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001639static bool
1640virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001641{
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001642 struct ethtool_link_ksettings diff1 = *cmd;
1643 struct ethtool_link_ksettings diff2 = {};
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001644
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001645 /* cmd is always set so we need to clear it, validate the port type
1646 * and also without autonegotiation we can ignore advertising
1647 */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001648 diff1.base.speed = 0;
1649 diff2.base.port = PORT_OTHER;
1650 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
1651 diff1.base.duplex = 0;
1652 diff1.base.cmd = 0;
1653 diff1.base.link_mode_masks_nwords = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001654
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001655 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
1656 bitmap_empty(diff1.link_modes.supported,
1657 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
1658 bitmap_empty(diff1.link_modes.advertising,
1659 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
1660 bitmap_empty(diff1.link_modes.lp_advertising,
1661 __ETHTOOL_LINK_MODE_MASK_NBITS);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001662}
1663
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001664static int virtnet_set_link_ksettings(struct net_device *dev,
1665 const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001666{
1667 struct virtnet_info *vi = netdev_priv(dev);
1668 u32 speed;
1669
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001670 speed = cmd->base.speed;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001671 /* don't allow custom speed and duplex */
1672 if (!ethtool_validate_speed(speed) ||
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001673 !ethtool_validate_duplex(cmd->base.duplex) ||
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001674 !virtnet_validate_ethtool_cmd(cmd))
1675 return -EINVAL;
1676 vi->speed = speed;
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001677 vi->duplex = cmd->base.duplex;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001678
1679 return 0;
1680}
1681
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001682static int virtnet_get_link_ksettings(struct net_device *dev,
1683 struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001684{
1685 struct virtnet_info *vi = netdev_priv(dev);
1686
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001687 cmd->base.speed = vi->speed;
1688 cmd->base.duplex = vi->duplex;
1689 cmd->base.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001690
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,
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01001709 .get_link_ksettings = virtnet_get_link_ksettings,
1710 .set_link_ksettings = virtnet_set_link_ksettings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001711};
1712
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001713static void virtnet_freeze_down(struct virtio_device *vdev)
1714{
1715 struct virtnet_info *vi = vdev->priv;
1716 int i;
1717
1718 /* Make sure no work handler is accessing the device */
1719 flush_work(&vi->config_work);
1720
1721 netif_device_detach(vi->dev);
1722 cancel_delayed_work_sync(&vi->refill);
1723
1724 if (netif_running(vi->dev)) {
1725 for (i = 0; i < vi->max_queue_pairs; i++)
1726 napi_disable(&vi->rq[i].napi);
1727 }
1728}
1729
1730static int init_vqs(struct virtnet_info *vi);
John Fastabend2de2f7f2017-02-02 19:16:29 -08001731static void _remove_vq_common(struct virtnet_info *vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001732
1733static int virtnet_restore_up(struct virtio_device *vdev)
1734{
1735 struct virtnet_info *vi = vdev->priv;
1736 int err, i;
1737
1738 err = init_vqs(vi);
1739 if (err)
1740 return err;
1741
1742 virtio_device_ready(vdev);
1743
1744 if (netif_running(vi->dev)) {
1745 for (i = 0; i < vi->curr_queue_pairs; i++)
1746 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1747 schedule_delayed_work(&vi->refill, 0);
1748
1749 for (i = 0; i < vi->max_queue_pairs; i++)
1750 virtnet_napi_enable(&vi->rq[i]);
1751 }
1752
1753 netif_device_attach(vi->dev);
1754 return err;
1755}
1756
Jason Wang017b29c2017-02-20 11:50:20 +08001757static int virtnet_reset(struct virtnet_info *vi, int curr_qp, int xdp_qp)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001758{
1759 struct virtio_device *dev = vi->vdev;
1760 int ret;
1761
1762 virtio_config_disable(dev);
1763 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
1764 virtnet_freeze_down(dev);
1765 _remove_vq_common(vi);
1766
1767 dev->config->reset(dev);
1768 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
1769 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
1770
1771 ret = virtio_finalize_features(dev);
1772 if (ret)
1773 goto err;
1774
Jason Wang017b29c2017-02-20 11:50:20 +08001775 vi->xdp_queue_pairs = xdp_qp;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001776 ret = virtnet_restore_up(dev);
1777 if (ret)
1778 goto err;
Jason Wang017b29c2017-02-20 11:50:20 +08001779 ret = _virtnet_set_queues(vi, curr_qp);
John Fastabend2de2f7f2017-02-02 19:16:29 -08001780 if (ret)
1781 goto err;
1782
1783 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
1784 virtio_config_enable(dev);
1785 return 0;
1786err:
1787 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
1788 return ret;
1789}
1790
John Fastabendf600b692016-12-15 12:13:24 -08001791static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1792{
1793 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1794 struct virtnet_info *vi = netdev_priv(dev);
1795 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08001796 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08001797 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001798
1799 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001800 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1801 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1802 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001803 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1804 return -EOPNOTSUPP;
1805 }
1806
1807 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1808 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1809 return -EINVAL;
1810 }
1811
1812 if (dev->mtu > max_sz) {
1813 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1814 return -EINVAL;
1815 }
1816
John Fastabend672aafd2016-12-15 12:13:49 -08001817 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1818 if (prog)
1819 xdp_qp = nr_cpu_ids;
1820
1821 /* XDP requires extra queues for XDP_TX */
1822 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1823 netdev_warn(dev, "request %i queues but max is %i\n",
1824 curr_qp + xdp_qp, vi->max_queue_pairs);
1825 return -ENOMEM;
1826 }
1827
John Fastabend2de2f7f2017-02-02 19:16:29 -08001828 if (prog) {
1829 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
1830 if (IS_ERR(prog))
1831 return PTR_ERR(prog);
1832 }
1833
John Fastabend2de2f7f2017-02-02 19:16:29 -08001834 /* Changing the headroom in buffers is a disruptive operation because
1835 * existing buffers must be flushed and reallocated. This will happen
1836 * when a xdp program is initially added or xdp is disabled by removing
1837 * the xdp program resulting in number of XDP queues changing.
1838 */
1839 if (vi->xdp_queue_pairs != xdp_qp) {
Jason Wang017b29c2017-02-20 11:50:20 +08001840 err = virtnet_reset(vi, curr_qp + xdp_qp, xdp_qp);
1841 if (err) {
1842 dev_warn(&dev->dev, "XDP reset failure.\n");
John Fastabend2de2f7f2017-02-02 19:16:29 -08001843 goto virtio_reset_err;
Jason Wang017b29c2017-02-20 11:50:20 +08001844 }
John Fastabendf600b692016-12-15 12:13:24 -08001845 }
1846
John Fastabend672aafd2016-12-15 12:13:49 -08001847 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1848
John Fastabendf600b692016-12-15 12:13:24 -08001849 for (i = 0; i < vi->max_queue_pairs; i++) {
1850 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1851 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1852 if (old_prog)
1853 bpf_prog_put(old_prog);
1854 }
1855
1856 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001857
1858virtio_reset_err:
1859 /* On reset error do our best to unwind XDP changes inflight and return
1860 * error up to user space for resolution. The underlying reset hung on
1861 * us so not much we can do here.
1862 */
John Fastabend2de2f7f2017-02-02 19:16:29 -08001863 if (prog)
1864 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
1865 return err;
John Fastabendf600b692016-12-15 12:13:24 -08001866}
1867
1868static bool virtnet_xdp_query(struct net_device *dev)
1869{
1870 struct virtnet_info *vi = netdev_priv(dev);
1871 int i;
1872
1873 for (i = 0; i < vi->max_queue_pairs; i++) {
1874 if (vi->rq[i].xdp_prog)
1875 return true;
1876 }
1877 return false;
1878}
1879
1880static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1881{
1882 switch (xdp->command) {
1883 case XDP_SETUP_PROG:
1884 return virtnet_xdp_set(dev, xdp->prog);
1885 case XDP_QUERY_PROG:
1886 xdp->prog_attached = virtnet_xdp_query(dev);
1887 return 0;
1888 default:
1889 return -EINVAL;
1890 }
1891}
1892
Stephen Hemminger76288b42009-01-06 10:44:22 -08001893static const struct net_device_ops virtnet_netdev = {
1894 .ndo_open = virtnet_open,
1895 .ndo_stop = virtnet_close,
1896 .ndo_start_xmit = start_xmit,
1897 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001898 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001899 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001900 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001901 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1902 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001903#ifdef CONFIG_NET_POLL_CONTROLLER
1904 .ndo_poll_controller = virtnet_netpoll,
1905#endif
John Fastabendf600b692016-12-15 12:13:24 -08001906 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001907};
1908
Jason Wang586d17c2012-04-11 20:43:52 +00001909static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001910{
Jason Wang586d17c2012-04-11 20:43:52 +00001911 struct virtnet_info *vi =
1912 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001913 u16 v;
1914
Rusty Russell855e0c52013-10-14 18:11:51 +10301915 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1916 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301917 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001918
1919 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001920 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001921 virtnet_ack_link_announce(vi);
1922 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001923
1924 /* Ignore unknown (future) status bits */
1925 v &= VIRTIO_NET_S_LINK_UP;
1926
1927 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301928 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001929
1930 vi->status = v;
1931
1932 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1933 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001934 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001935 } else {
1936 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001937 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001938 }
1939}
1940
1941static void virtnet_config_changed(struct virtio_device *vdev)
1942{
1943 struct virtnet_info *vi = vdev->priv;
1944
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001945 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001946}
1947
Jason Wang986a4f42012-12-07 07:04:56 +00001948static void virtnet_free_queues(struct virtnet_info *vi)
1949{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001950 int i;
1951
Jason Wangab3971b2015-03-12 13:57:44 +08001952 for (i = 0; i < vi->max_queue_pairs; i++) {
1953 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001954 netif_napi_del(&vi->rq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08001955 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001956
Eric Dumazet963abe52016-11-15 22:24:12 -08001957 /* We called napi_hash_del() before netif_napi_del(),
1958 * we need to respect an RCU grace period before freeing vi->rq
1959 */
1960 synchronize_net();
1961
Jason Wang986a4f42012-12-07 07:04:56 +00001962 kfree(vi->rq);
1963 kfree(vi->sq);
1964}
1965
John Fastabend473153292017-02-02 19:14:32 -08001966static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001967{
John Fastabendf600b692016-12-15 12:13:24 -08001968 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00001969 int i;
1970
1971 for (i = 0; i < vi->max_queue_pairs; i++) {
1972 while (vi->rq[i].pages)
1973 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08001974
1975 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1976 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1977 if (old_prog)
1978 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00001979 }
John Fastabend473153292017-02-02 19:14:32 -08001980}
1981
1982static void free_receive_bufs(struct virtnet_info *vi)
1983{
1984 rtnl_lock();
1985 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08001986 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001987}
1988
Michael Daltonfb518792014-01-16 22:23:26 -08001989static void free_receive_page_frags(struct virtnet_info *vi)
1990{
1991 int i;
1992 for (i = 0; i < vi->max_queue_pairs; i++)
1993 if (vi->rq[i].alloc_frag.page)
1994 put_page(vi->rq[i].alloc_frag.page);
1995}
1996
John Fastabendb68df012017-01-25 18:22:48 -08001997static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08001998{
1999 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
2000 return false;
2001 else if (q < vi->curr_queue_pairs)
2002 return true;
2003 else
2004 return false;
2005}
2006
Jason Wang986a4f42012-12-07 07:04:56 +00002007static void free_unused_bufs(struct virtnet_info *vi)
2008{
2009 void *buf;
2010 int i;
2011
2012 for (i = 0; i < vi->max_queue_pairs; i++) {
2013 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002014 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08002015 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08002016 dev_kfree_skb(buf);
2017 else
2018 put_page(virt_to_head_page(buf));
2019 }
Jason Wang986a4f42012-12-07 07:04:56 +00002020 }
2021
2022 for (i = 0; i < vi->max_queue_pairs; i++) {
2023 struct virtqueue *vq = vi->rq[i].vq;
2024
2025 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002026 if (vi->mergeable_rx_bufs) {
2027 unsigned long ctx = (unsigned long)buf;
2028 void *base = mergeable_ctx_to_buf_address(ctx);
2029 put_page(virt_to_head_page(base));
2030 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002031 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002032 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002033 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002034 }
Jason Wang986a4f42012-12-07 07:04:56 +00002035 }
Jason Wang986a4f42012-12-07 07:04:56 +00002036 }
2037}
2038
Jason Wange9d74172012-12-07 07:04:55 +00002039static void virtnet_del_vqs(struct virtnet_info *vi)
2040{
2041 struct virtio_device *vdev = vi->vdev;
2042
Wanlong Gao8898c212013-01-24 23:51:30 +00002043 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00002044
Jason Wange9d74172012-12-07 07:04:55 +00002045 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002046
2047 virtnet_free_queues(vi);
2048}
2049
2050static int virtnet_find_vqs(struct virtnet_info *vi)
2051{
2052 vq_callback_t **callbacks;
2053 struct virtqueue **vqs;
2054 int ret = -ENOMEM;
2055 int i, total_vqs;
2056 const char **names;
2057
2058 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2059 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2060 * possible control vq.
2061 */
2062 total_vqs = vi->max_queue_pairs * 2 +
2063 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2064
2065 /* Allocate space for find_vqs parameters */
2066 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
2067 if (!vqs)
2068 goto err_vq;
2069 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
2070 if (!callbacks)
2071 goto err_callback;
2072 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
2073 if (!names)
2074 goto err_names;
2075
2076 /* Parameters for control virtqueue, if any */
2077 if (vi->has_cvq) {
2078 callbacks[total_vqs - 1] = NULL;
2079 names[total_vqs - 1] = "control";
2080 }
2081
2082 /* Allocate/initialize parameters for send/receive virtqueues */
2083 for (i = 0; i < vi->max_queue_pairs; i++) {
2084 callbacks[rxq2vq(i)] = skb_recv_done;
2085 callbacks[txq2vq(i)] = skb_xmit_done;
2086 sprintf(vi->rq[i].name, "input.%d", i);
2087 sprintf(vi->sq[i].name, "output.%d", i);
2088 names[rxq2vq(i)] = vi->rq[i].name;
2089 names[txq2vq(i)] = vi->sq[i].name;
2090 }
2091
2092 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +01002093 names, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002094 if (ret)
2095 goto err_find;
2096
2097 if (vi->has_cvq) {
2098 vi->cvq = vqs[total_vqs - 1];
2099 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002100 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002101 }
2102
2103 for (i = 0; i < vi->max_queue_pairs; i++) {
2104 vi->rq[i].vq = vqs[rxq2vq(i)];
2105 vi->sq[i].vq = vqs[txq2vq(i)];
2106 }
2107
2108 kfree(names);
2109 kfree(callbacks);
2110 kfree(vqs);
2111
2112 return 0;
2113
2114err_find:
2115 kfree(names);
2116err_names:
2117 kfree(callbacks);
2118err_callback:
2119 kfree(vqs);
2120err_vq:
2121 return ret;
2122}
2123
2124static int virtnet_alloc_queues(struct virtnet_info *vi)
2125{
2126 int i;
2127
2128 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2129 if (!vi->sq)
2130 goto err_sq;
2131 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002132 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002133 goto err_rq;
2134
2135 INIT_DELAYED_WORK(&vi->refill, refill_work);
2136 for (i = 0; i < vi->max_queue_pairs; i++) {
2137 vi->rq[i].pages = NULL;
2138 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2139 napi_weight);
2140
2141 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002142 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002143 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2144 }
2145
2146 return 0;
2147
2148err_rq:
2149 kfree(vi->sq);
2150err_sq:
2151 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002152}
2153
Amit Shah3f9c10b2011-12-22 16:58:31 +05302154static int init_vqs(struct virtnet_info *vi)
2155{
Jason Wang986a4f42012-12-07 07:04:56 +00002156 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302157
Jason Wang986a4f42012-12-07 07:04:56 +00002158 /* Allocate send & receive queues */
2159 ret = virtnet_alloc_queues(vi);
2160 if (ret)
2161 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302162
Jason Wang986a4f42012-12-07 07:04:56 +00002163 ret = virtnet_find_vqs(vi);
2164 if (ret)
2165 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302166
Wanlong Gao47be2472013-01-24 23:51:29 +00002167 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002168 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002169 put_online_cpus();
2170
Amit Shah3f9c10b2011-12-22 16:58:31 +05302171 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002172
2173err_free:
2174 virtnet_free_queues(vi);
2175err:
2176 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302177}
2178
Michael Daltonfbf28d72014-01-16 22:23:30 -08002179#ifdef CONFIG_SYSFS
2180static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2181 struct rx_queue_attribute *attribute, char *buf)
2182{
2183 struct virtnet_info *vi = netdev_priv(queue->dev);
2184 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002185 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002186
2187 BUG_ON(queue_index >= vi->max_queue_pairs);
2188 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2189 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2190}
2191
2192static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2193 __ATTR_RO(mergeable_rx_buffer_size);
2194
2195static struct attribute *virtio_net_mrg_rx_attrs[] = {
2196 &mergeable_rx_buffer_size_attribute.attr,
2197 NULL
2198};
2199
2200static const struct attribute_group virtio_net_mrg_rx_group = {
2201 .name = "virtio_net",
2202 .attrs = virtio_net_mrg_rx_attrs
2203};
2204#endif
2205
Jason Wang892d6eb2014-11-20 17:03:05 +08002206static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2207 unsigned int fbit,
2208 const char *fname, const char *dname)
2209{
2210 if (!virtio_has_feature(vdev, fbit))
2211 return false;
2212
2213 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2214 fname, dname);
2215
2216 return true;
2217}
2218
2219#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2220 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2221
2222static bool virtnet_validate_features(struct virtio_device *vdev)
2223{
2224 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2225 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2226 "VIRTIO_NET_F_CTRL_VQ") ||
2227 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2228 "VIRTIO_NET_F_CTRL_VQ") ||
2229 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2230 "VIRTIO_NET_F_CTRL_VQ") ||
2231 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2232 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2233 "VIRTIO_NET_F_CTRL_VQ"))) {
2234 return false;
2235 }
2236
2237 return true;
2238}
2239
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002240#define MIN_MTU ETH_MIN_MTU
2241#define MAX_MTU ETH_MAX_MTU
2242
Rusty Russell296f96f2007-10-22 11:03:37 +10002243static int virtnet_probe(struct virtio_device *vdev)
2244{
Jason Wang986a4f42012-12-07 07:04:56 +00002245 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10002246 struct net_device *dev;
2247 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00002248 u16 max_queue_pairs;
Aaron Conole14de9d12016-06-03 16:57:12 -04002249 int mtu;
Jason Wang986a4f42012-12-07 07:04:56 +00002250
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002251 if (!vdev->config->get) {
2252 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2253 __func__);
2254 return -EINVAL;
2255 }
2256
Jason Wang892d6eb2014-11-20 17:03:05 +08002257 if (!virtnet_validate_features(vdev))
2258 return -EINVAL;
2259
Jason Wang986a4f42012-12-07 07:04:56 +00002260 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302261 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2262 struct virtio_net_config,
2263 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002264
2265 /* We need at least 2 queue's */
2266 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2267 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2268 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2269 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002270
2271 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002272 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002273 if (!dev)
2274 return -ENOMEM;
2275
2276 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002277 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002278 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002279 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002280
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002281 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002282 SET_NETDEV_DEV(dev, &vdev->dev);
2283
2284 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002285 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002286 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002287 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002288 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002289 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002290
2291 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002292 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002293 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2294 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002295 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002296 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2297 dev->hw_features |= NETIF_F_TSO;
2298 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2299 dev->hw_features |= NETIF_F_TSO6;
2300 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2301 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002302 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2303 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002304
Jason Wang41f2f122014-12-24 11:03:52 +08002305 dev->features |= NETIF_F_GSO_ROBUST;
2306
Michał Mirosław98e778c2011-03-31 01:01:35 +00002307 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002308 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002309 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002310 }
Thomas Huth4f491292013-08-27 17:09:02 +02002311 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2312 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002313
Jason Wang4fda8302013-04-10 23:32:21 +00002314 dev->vlan_features = dev->features;
2315
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002316 /* MTU range: 68 - 65535 */
2317 dev->min_mtu = MIN_MTU;
2318 dev->max_mtu = MAX_MTU;
2319
Rusty Russell296f96f2007-10-22 11:03:37 +10002320 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302321 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2322 virtio_cread_bytes(vdev,
2323 offsetof(struct virtio_net_config, mac),
2324 dev->dev_addr, dev->addr_len);
2325 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002326 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002327
2328 /* Set up our device-specific information */
2329 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002330 vi->dev = dev;
2331 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002332 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002333 vi->stats = alloc_percpu(struct virtnet_stats);
2334 err = -ENOMEM;
2335 if (vi->stats == NULL)
2336 goto free;
2337
John Stultz827da442013-10-07 15:51:58 -07002338 for_each_possible_cpu(i) {
2339 struct virtnet_stats *virtnet_stats;
2340 virtnet_stats = per_cpu_ptr(vi->stats, i);
2341 u64_stats_init(&virtnet_stats->tx_syncp);
2342 u64_stats_init(&virtnet_stats->rx_syncp);
2343 }
2344
Jason Wang586d17c2012-04-11 20:43:52 +00002345 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002346
Herbert Xu97402b92008-04-18 11:24:27 +08002347 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002348 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2349 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002350 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2351 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002352 vi->big_packets = true;
2353
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002354 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2355 vi->mergeable_rx_bufs = true;
2356
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002357 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2358 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002359 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2360 else
2361 vi->hdr_len = sizeof(struct virtio_net_hdr);
2362
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002363 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2364 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302365 vi->any_header_sg = true;
2366
Jason Wang986a4f42012-12-07 07:04:56 +00002367 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2368 vi->has_cvq = true;
2369
Aaron Conole14de9d12016-06-03 16:57:12 -04002370 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2371 mtu = virtio_cread16(vdev,
2372 offsetof(struct virtio_net_config,
2373 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002374 if (mtu < dev->min_mtu) {
Aaron Conole14de9d12016-06-03 16:57:12 -04002375 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
Aaron Conole93a205e2016-10-25 16:12:12 -04002376 } else {
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002377 dev->mtu = mtu;
Aaron Conole93a205e2016-10-25 16:12:12 -04002378 dev->max_mtu = mtu;
2379 }
Aaron Conole14de9d12016-06-03 16:57:12 -04002380 }
2381
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002382 if (vi->any_header_sg)
2383 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002384
Jason Wang44900012016-11-25 12:37:26 +08002385 /* Enable multiqueue by default */
2386 if (num_online_cpus() >= max_queue_pairs)
2387 vi->curr_queue_pairs = max_queue_pairs;
2388 else
2389 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002390 vi->max_queue_pairs = max_queue_pairs;
2391
2392 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302393 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002394 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002395 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002396
Michael Daltonfbf28d72014-01-16 22:23:30 -08002397#ifdef CONFIG_SYSFS
2398 if (vi->mergeable_rx_bufs)
2399 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2400#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08002401 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2402 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002403
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002404 virtnet_init_settings(dev);
2405
Rusty Russell296f96f2007-10-22 11:03:37 +10002406 err = register_netdev(dev);
2407 if (err) {
2408 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002409 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002410 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002411
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302412 virtio_device_ready(vdev);
2413
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002414 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002415 if (err) {
2416 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002417 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002418 }
2419
Jason Wanga2208712016-12-13 14:23:05 +08002420 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08002421
Jason Wang167c25e2010-11-10 14:45:41 +00002422 /* Assume link up if device can't report link status,
2423 otherwise get link status from config. */
2424 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2425 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002426 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002427 } else {
2428 vi->status = VIRTIO_NET_S_LINK_UP;
2429 netif_carrier_on(dev);
2430 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002431
Jason Wang986a4f42012-12-07 07:04:56 +00002432 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2433 dev->name, max_queue_pairs);
2434
Rusty Russell296f96f2007-10-22 11:03:37 +10002435 return 0;
2436
wangyunjianf00e35e2016-05-31 11:52:43 +08002437free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302438 vi->vdev->config->reset(vdev);
2439
Rusty Russellb3369c12008-02-04 23:50:02 -05002440 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002441free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002442 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002443 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002444 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002445free_stats:
2446 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002447free:
2448 free_netdev(dev);
2449 return err;
2450}
2451
John Fastabend2de2f7f2017-02-02 19:16:29 -08002452static void _remove_vq_common(struct virtnet_info *vi)
2453{
2454 vi->vdev->config->reset(vi->vdev);
2455 free_unused_bufs(vi);
2456 _free_receive_bufs(vi);
2457 free_receive_page_frags(vi);
2458 virtnet_del_vqs(vi);
2459}
2460
Amit Shah04486ed2011-12-22 16:58:32 +05302461static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002462{
Amit Shah04486ed2011-12-22 16:58:32 +05302463 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002464
2465 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002466 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002467
Jason Wang986a4f42012-12-07 07:04:56 +00002468 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002469
Michael Daltonfb518792014-01-16 22:23:26 -08002470 free_receive_page_frags(vi);
2471
Jason Wang986a4f42012-12-07 07:04:56 +00002472 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302473}
2474
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002475static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302476{
2477 struct virtnet_info *vi = vdev->priv;
2478
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002479 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002480
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302481 /* Make sure no work handler is accessing the device. */
2482 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002483
Amit Shah04486ed2011-12-22 16:58:32 +05302484 unregister_netdev(vi->dev);
2485
2486 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002487
Krishna Kumar2e66f552011-07-20 03:56:02 +00002488 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002489 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002490}
2491
Aaron Lu89107002013-09-17 09:25:23 +09302492#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302493static int virtnet_freeze(struct virtio_device *vdev)
2494{
2495 struct virtnet_info *vi = vdev->priv;
2496
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002497 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002498 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05302499 remove_vq_common(vi);
2500
2501 return 0;
2502}
2503
2504static int virtnet_restore(struct virtio_device *vdev)
2505{
2506 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002507 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05302508
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002509 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05302510 if (err)
2511 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00002512 virtnet_set_queues(vi, vi->curr_queue_pairs);
2513
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002514 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002515 if (err)
2516 return err;
2517
Amit Shah0741bcb2011-12-22 16:58:33 +05302518 return 0;
2519}
2520#endif
2521
Rusty Russell296f96f2007-10-22 11:03:37 +10002522static struct virtio_device_id id_table[] = {
2523 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2524 { 0 },
2525};
2526
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002527#define VIRTNET_FEATURES \
2528 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2529 VIRTIO_NET_F_MAC, \
2530 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2531 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2532 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2533 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2534 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2535 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2536 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2537 VIRTIO_NET_F_MTU
2538
Rusty Russellc45a6812008-05-02 21:50:50 -05002539static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002540 VIRTNET_FEATURES,
2541};
2542
2543static unsigned int features_legacy[] = {
2544 VIRTNET_FEATURES,
2545 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302546 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002547};
2548
Uwe Kleine-König22402522009-11-05 01:32:44 -08002549static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002550 .feature_table = features,
2551 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002552 .feature_table_legacy = features_legacy,
2553 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002554 .driver.name = KBUILD_MODNAME,
2555 .driver.owner = THIS_MODULE,
2556 .id_table = id_table,
2557 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002558 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002559 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302560#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302561 .freeze = virtnet_freeze,
2562 .restore = virtnet_restore,
2563#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002564};
2565
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002566static __init int virtio_net_driver_init(void)
2567{
2568 int ret;
2569
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002570 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002571 virtnet_cpu_online,
2572 virtnet_cpu_down_prep);
2573 if (ret < 0)
2574 goto out;
2575 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002576 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002577 NULL, virtnet_cpu_dead);
2578 if (ret)
2579 goto err_dead;
2580
2581 ret = register_virtio_driver(&virtio_net_driver);
2582 if (ret)
2583 goto err_virtio;
2584 return 0;
2585err_virtio:
2586 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2587err_dead:
2588 cpuhp_remove_multi_state(virtionet_online);
2589out:
2590 return ret;
2591}
2592module_init(virtio_net_driver_init);
2593
2594static __exit void virtio_net_driver_exit(void)
2595{
2596 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2597 cpuhp_remove_multi_state(virtionet_online);
2598 unregister_virtio_driver(&virtio_net_driver);
2599}
2600module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002601
2602MODULE_DEVICE_TABLE(virtio, id_table);
2603MODULE_DESCRIPTION("Virtio network driver");
2604MODULE_LICENSE("GPL");