blob: f36584616e7d6825c7e69137b4a31a3d55779688 [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 */
1639static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1640{
1641 struct ethtool_cmd diff1 = *cmd;
1642 struct ethtool_cmd diff2 = {};
1643
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001644 /* cmd is always set so we need to clear it, validate the port type
1645 * and also without autonegotiation we can ignore advertising
1646 */
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001647 ethtool_cmd_speed_set(&diff1, 0);
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001648 diff2.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001649 diff1.advertising = 0;
1650 diff1.duplex = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001651 diff1.cmd = 0;
1652
1653 return !memcmp(&diff1, &diff2, sizeof(diff1));
1654}
1655
1656static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1657{
1658 struct virtnet_info *vi = netdev_priv(dev);
1659 u32 speed;
1660
1661 speed = ethtool_cmd_speed(cmd);
1662 /* don't allow custom speed and duplex */
1663 if (!ethtool_validate_speed(speed) ||
1664 !ethtool_validate_duplex(cmd->duplex) ||
1665 !virtnet_validate_ethtool_cmd(cmd))
1666 return -EINVAL;
1667 vi->speed = speed;
1668 vi->duplex = cmd->duplex;
1669
1670 return 0;
1671}
1672
1673static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1674{
1675 struct virtnet_info *vi = netdev_priv(dev);
1676
1677 ethtool_cmd_speed_set(cmd, vi->speed);
1678 cmd->duplex = vi->duplex;
1679 cmd->port = PORT_OTHER;
1680
1681 return 0;
1682}
1683
1684static void virtnet_init_settings(struct net_device *dev)
1685{
1686 struct virtnet_info *vi = netdev_priv(dev);
1687
1688 vi->speed = SPEED_UNKNOWN;
1689 vi->duplex = DUPLEX_UNKNOWN;
1690}
1691
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001692static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001693 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001694 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001695 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001696 .set_channels = virtnet_set_channels,
1697 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00001698 .get_ts_info = ethtool_op_get_ts_info,
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001699 .get_settings = virtnet_get_settings,
1700 .set_settings = virtnet_set_settings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001701};
1702
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001703static void virtnet_freeze_down(struct virtio_device *vdev)
1704{
1705 struct virtnet_info *vi = vdev->priv;
1706 int i;
1707
1708 /* Make sure no work handler is accessing the device */
1709 flush_work(&vi->config_work);
1710
1711 netif_device_detach(vi->dev);
1712 cancel_delayed_work_sync(&vi->refill);
1713
1714 if (netif_running(vi->dev)) {
1715 for (i = 0; i < vi->max_queue_pairs; i++)
1716 napi_disable(&vi->rq[i].napi);
1717 }
1718}
1719
1720static int init_vqs(struct virtnet_info *vi);
John Fastabend2de2f7f2017-02-02 19:16:29 -08001721static void _remove_vq_common(struct virtnet_info *vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001722
1723static int virtnet_restore_up(struct virtio_device *vdev)
1724{
1725 struct virtnet_info *vi = vdev->priv;
1726 int err, i;
1727
1728 err = init_vqs(vi);
1729 if (err)
1730 return err;
1731
1732 virtio_device_ready(vdev);
1733
1734 if (netif_running(vi->dev)) {
1735 for (i = 0; i < vi->curr_queue_pairs; i++)
1736 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1737 schedule_delayed_work(&vi->refill, 0);
1738
1739 for (i = 0; i < vi->max_queue_pairs; i++)
1740 virtnet_napi_enable(&vi->rq[i]);
1741 }
1742
1743 netif_device_attach(vi->dev);
1744 return err;
1745}
1746
Jason Wang017b29c2017-02-20 11:50:20 +08001747static int virtnet_reset(struct virtnet_info *vi, int curr_qp, int xdp_qp)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001748{
1749 struct virtio_device *dev = vi->vdev;
1750 int ret;
1751
1752 virtio_config_disable(dev);
1753 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
1754 virtnet_freeze_down(dev);
1755 _remove_vq_common(vi);
1756
1757 dev->config->reset(dev);
1758 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
1759 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
1760
1761 ret = virtio_finalize_features(dev);
1762 if (ret)
1763 goto err;
1764
Jason Wang017b29c2017-02-20 11:50:20 +08001765 vi->xdp_queue_pairs = xdp_qp;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001766 ret = virtnet_restore_up(dev);
1767 if (ret)
1768 goto err;
Jason Wang017b29c2017-02-20 11:50:20 +08001769 ret = _virtnet_set_queues(vi, curr_qp);
John Fastabend2de2f7f2017-02-02 19:16:29 -08001770 if (ret)
1771 goto err;
1772
1773 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
1774 virtio_config_enable(dev);
1775 return 0;
1776err:
1777 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
1778 return ret;
1779}
1780
John Fastabendf600b692016-12-15 12:13:24 -08001781static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1782{
1783 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1784 struct virtnet_info *vi = netdev_priv(dev);
1785 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08001786 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08001787 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001788
1789 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001790 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1791 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1792 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001793 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1794 return -EOPNOTSUPP;
1795 }
1796
1797 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1798 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1799 return -EINVAL;
1800 }
1801
1802 if (dev->mtu > max_sz) {
1803 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1804 return -EINVAL;
1805 }
1806
John Fastabend672aafd2016-12-15 12:13:49 -08001807 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1808 if (prog)
1809 xdp_qp = nr_cpu_ids;
1810
1811 /* XDP requires extra queues for XDP_TX */
1812 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1813 netdev_warn(dev, "request %i queues but max is %i\n",
1814 curr_qp + xdp_qp, vi->max_queue_pairs);
1815 return -ENOMEM;
1816 }
1817
John Fastabend2de2f7f2017-02-02 19:16:29 -08001818 if (prog) {
1819 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
1820 if (IS_ERR(prog))
1821 return PTR_ERR(prog);
1822 }
1823
John Fastabend2de2f7f2017-02-02 19:16:29 -08001824 /* Changing the headroom in buffers is a disruptive operation because
1825 * existing buffers must be flushed and reallocated. This will happen
1826 * when a xdp program is initially added or xdp is disabled by removing
1827 * the xdp program resulting in number of XDP queues changing.
1828 */
1829 if (vi->xdp_queue_pairs != xdp_qp) {
Jason Wang017b29c2017-02-20 11:50:20 +08001830 err = virtnet_reset(vi, curr_qp + xdp_qp, xdp_qp);
1831 if (err) {
1832 dev_warn(&dev->dev, "XDP reset failure.\n");
John Fastabend2de2f7f2017-02-02 19:16:29 -08001833 goto virtio_reset_err;
Jason Wang017b29c2017-02-20 11:50:20 +08001834 }
John Fastabendf600b692016-12-15 12:13:24 -08001835 }
1836
John Fastabend672aafd2016-12-15 12:13:49 -08001837 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1838
John Fastabendf600b692016-12-15 12:13:24 -08001839 for (i = 0; i < vi->max_queue_pairs; i++) {
1840 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1841 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1842 if (old_prog)
1843 bpf_prog_put(old_prog);
1844 }
1845
1846 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001847
1848virtio_reset_err:
1849 /* On reset error do our best to unwind XDP changes inflight and return
1850 * error up to user space for resolution. The underlying reset hung on
1851 * us so not much we can do here.
1852 */
John Fastabend2de2f7f2017-02-02 19:16:29 -08001853 if (prog)
1854 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
1855 return err;
John Fastabendf600b692016-12-15 12:13:24 -08001856}
1857
1858static bool virtnet_xdp_query(struct net_device *dev)
1859{
1860 struct virtnet_info *vi = netdev_priv(dev);
1861 int i;
1862
1863 for (i = 0; i < vi->max_queue_pairs; i++) {
1864 if (vi->rq[i].xdp_prog)
1865 return true;
1866 }
1867 return false;
1868}
1869
1870static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1871{
1872 switch (xdp->command) {
1873 case XDP_SETUP_PROG:
1874 return virtnet_xdp_set(dev, xdp->prog);
1875 case XDP_QUERY_PROG:
1876 xdp->prog_attached = virtnet_xdp_query(dev);
1877 return 0;
1878 default:
1879 return -EINVAL;
1880 }
1881}
1882
Stephen Hemminger76288b42009-01-06 10:44:22 -08001883static const struct net_device_ops virtnet_netdev = {
1884 .ndo_open = virtnet_open,
1885 .ndo_stop = virtnet_close,
1886 .ndo_start_xmit = start_xmit,
1887 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001888 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001889 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001890 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001891 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1892 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001893#ifdef CONFIG_NET_POLL_CONTROLLER
1894 .ndo_poll_controller = virtnet_netpoll,
1895#endif
John Fastabendf600b692016-12-15 12:13:24 -08001896 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001897};
1898
Jason Wang586d17c2012-04-11 20:43:52 +00001899static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001900{
Jason Wang586d17c2012-04-11 20:43:52 +00001901 struct virtnet_info *vi =
1902 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001903 u16 v;
1904
Rusty Russell855e0c52013-10-14 18:11:51 +10301905 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1906 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301907 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001908
1909 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001910 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001911 virtnet_ack_link_announce(vi);
1912 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001913
1914 /* Ignore unknown (future) status bits */
1915 v &= VIRTIO_NET_S_LINK_UP;
1916
1917 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301918 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001919
1920 vi->status = v;
1921
1922 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1923 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001924 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001925 } else {
1926 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001927 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001928 }
1929}
1930
1931static void virtnet_config_changed(struct virtio_device *vdev)
1932{
1933 struct virtnet_info *vi = vdev->priv;
1934
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001935 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001936}
1937
Jason Wang986a4f42012-12-07 07:04:56 +00001938static void virtnet_free_queues(struct virtnet_info *vi)
1939{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001940 int i;
1941
Jason Wangab3971b2015-03-12 13:57:44 +08001942 for (i = 0; i < vi->max_queue_pairs; i++) {
1943 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001944 netif_napi_del(&vi->rq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08001945 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001946
Eric Dumazet963abe52016-11-15 22:24:12 -08001947 /* We called napi_hash_del() before netif_napi_del(),
1948 * we need to respect an RCU grace period before freeing vi->rq
1949 */
1950 synchronize_net();
1951
Jason Wang986a4f42012-12-07 07:04:56 +00001952 kfree(vi->rq);
1953 kfree(vi->sq);
1954}
1955
John Fastabend473153292017-02-02 19:14:32 -08001956static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001957{
John Fastabendf600b692016-12-15 12:13:24 -08001958 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00001959 int i;
1960
1961 for (i = 0; i < vi->max_queue_pairs; i++) {
1962 while (vi->rq[i].pages)
1963 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08001964
1965 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1966 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1967 if (old_prog)
1968 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00001969 }
John Fastabend473153292017-02-02 19:14:32 -08001970}
1971
1972static void free_receive_bufs(struct virtnet_info *vi)
1973{
1974 rtnl_lock();
1975 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08001976 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001977}
1978
Michael Daltonfb518792014-01-16 22:23:26 -08001979static void free_receive_page_frags(struct virtnet_info *vi)
1980{
1981 int i;
1982 for (i = 0; i < vi->max_queue_pairs; i++)
1983 if (vi->rq[i].alloc_frag.page)
1984 put_page(vi->rq[i].alloc_frag.page);
1985}
1986
John Fastabendb68df012017-01-25 18:22:48 -08001987static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08001988{
1989 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1990 return false;
1991 else if (q < vi->curr_queue_pairs)
1992 return true;
1993 else
1994 return false;
1995}
1996
Jason Wang986a4f42012-12-07 07:04:56 +00001997static void free_unused_bufs(struct virtnet_info *vi)
1998{
1999 void *buf;
2000 int i;
2001
2002 for (i = 0; i < vi->max_queue_pairs; i++) {
2003 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002004 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08002005 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08002006 dev_kfree_skb(buf);
2007 else
2008 put_page(virt_to_head_page(buf));
2009 }
Jason Wang986a4f42012-12-07 07:04:56 +00002010 }
2011
2012 for (i = 0; i < vi->max_queue_pairs; i++) {
2013 struct virtqueue *vq = vi->rq[i].vq;
2014
2015 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002016 if (vi->mergeable_rx_bufs) {
2017 unsigned long ctx = (unsigned long)buf;
2018 void *base = mergeable_ctx_to_buf_address(ctx);
2019 put_page(virt_to_head_page(base));
2020 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002021 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002022 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002023 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002024 }
Jason Wang986a4f42012-12-07 07:04:56 +00002025 }
Jason Wang986a4f42012-12-07 07:04:56 +00002026 }
2027}
2028
Jason Wange9d74172012-12-07 07:04:55 +00002029static void virtnet_del_vqs(struct virtnet_info *vi)
2030{
2031 struct virtio_device *vdev = vi->vdev;
2032
Wanlong Gao8898c212013-01-24 23:51:30 +00002033 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00002034
Jason Wange9d74172012-12-07 07:04:55 +00002035 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002036
2037 virtnet_free_queues(vi);
2038}
2039
2040static int virtnet_find_vqs(struct virtnet_info *vi)
2041{
2042 vq_callback_t **callbacks;
2043 struct virtqueue **vqs;
2044 int ret = -ENOMEM;
2045 int i, total_vqs;
2046 const char **names;
2047
2048 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2049 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2050 * possible control vq.
2051 */
2052 total_vqs = vi->max_queue_pairs * 2 +
2053 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2054
2055 /* Allocate space for find_vqs parameters */
2056 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
2057 if (!vqs)
2058 goto err_vq;
2059 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
2060 if (!callbacks)
2061 goto err_callback;
2062 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
2063 if (!names)
2064 goto err_names;
2065
2066 /* Parameters for control virtqueue, if any */
2067 if (vi->has_cvq) {
2068 callbacks[total_vqs - 1] = NULL;
2069 names[total_vqs - 1] = "control";
2070 }
2071
2072 /* Allocate/initialize parameters for send/receive virtqueues */
2073 for (i = 0; i < vi->max_queue_pairs; i++) {
2074 callbacks[rxq2vq(i)] = skb_recv_done;
2075 callbacks[txq2vq(i)] = skb_xmit_done;
2076 sprintf(vi->rq[i].name, "input.%d", i);
2077 sprintf(vi->sq[i].name, "output.%d", i);
2078 names[rxq2vq(i)] = vi->rq[i].name;
2079 names[txq2vq(i)] = vi->sq[i].name;
2080 }
2081
2082 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +01002083 names, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002084 if (ret)
2085 goto err_find;
2086
2087 if (vi->has_cvq) {
2088 vi->cvq = vqs[total_vqs - 1];
2089 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002090 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002091 }
2092
2093 for (i = 0; i < vi->max_queue_pairs; i++) {
2094 vi->rq[i].vq = vqs[rxq2vq(i)];
2095 vi->sq[i].vq = vqs[txq2vq(i)];
2096 }
2097
2098 kfree(names);
2099 kfree(callbacks);
2100 kfree(vqs);
2101
2102 return 0;
2103
2104err_find:
2105 kfree(names);
2106err_names:
2107 kfree(callbacks);
2108err_callback:
2109 kfree(vqs);
2110err_vq:
2111 return ret;
2112}
2113
2114static int virtnet_alloc_queues(struct virtnet_info *vi)
2115{
2116 int i;
2117
2118 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2119 if (!vi->sq)
2120 goto err_sq;
2121 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002122 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002123 goto err_rq;
2124
2125 INIT_DELAYED_WORK(&vi->refill, refill_work);
2126 for (i = 0; i < vi->max_queue_pairs; i++) {
2127 vi->rq[i].pages = NULL;
2128 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2129 napi_weight);
2130
2131 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002132 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002133 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2134 }
2135
2136 return 0;
2137
2138err_rq:
2139 kfree(vi->sq);
2140err_sq:
2141 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002142}
2143
Amit Shah3f9c10b2011-12-22 16:58:31 +05302144static int init_vqs(struct virtnet_info *vi)
2145{
Jason Wang986a4f42012-12-07 07:04:56 +00002146 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302147
Jason Wang986a4f42012-12-07 07:04:56 +00002148 /* Allocate send & receive queues */
2149 ret = virtnet_alloc_queues(vi);
2150 if (ret)
2151 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302152
Jason Wang986a4f42012-12-07 07:04:56 +00002153 ret = virtnet_find_vqs(vi);
2154 if (ret)
2155 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302156
Wanlong Gao47be2472013-01-24 23:51:29 +00002157 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002158 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002159 put_online_cpus();
2160
Amit Shah3f9c10b2011-12-22 16:58:31 +05302161 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002162
2163err_free:
2164 virtnet_free_queues(vi);
2165err:
2166 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302167}
2168
Michael Daltonfbf28d72014-01-16 22:23:30 -08002169#ifdef CONFIG_SYSFS
2170static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2171 struct rx_queue_attribute *attribute, char *buf)
2172{
2173 struct virtnet_info *vi = netdev_priv(queue->dev);
2174 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002175 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002176
2177 BUG_ON(queue_index >= vi->max_queue_pairs);
2178 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2179 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2180}
2181
2182static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2183 __ATTR_RO(mergeable_rx_buffer_size);
2184
2185static struct attribute *virtio_net_mrg_rx_attrs[] = {
2186 &mergeable_rx_buffer_size_attribute.attr,
2187 NULL
2188};
2189
2190static const struct attribute_group virtio_net_mrg_rx_group = {
2191 .name = "virtio_net",
2192 .attrs = virtio_net_mrg_rx_attrs
2193};
2194#endif
2195
Jason Wang892d6eb2014-11-20 17:03:05 +08002196static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2197 unsigned int fbit,
2198 const char *fname, const char *dname)
2199{
2200 if (!virtio_has_feature(vdev, fbit))
2201 return false;
2202
2203 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2204 fname, dname);
2205
2206 return true;
2207}
2208
2209#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2210 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2211
2212static bool virtnet_validate_features(struct virtio_device *vdev)
2213{
2214 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2215 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2216 "VIRTIO_NET_F_CTRL_VQ") ||
2217 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2218 "VIRTIO_NET_F_CTRL_VQ") ||
2219 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2220 "VIRTIO_NET_F_CTRL_VQ") ||
2221 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2222 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2223 "VIRTIO_NET_F_CTRL_VQ"))) {
2224 return false;
2225 }
2226
2227 return true;
2228}
2229
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002230#define MIN_MTU ETH_MIN_MTU
2231#define MAX_MTU ETH_MAX_MTU
2232
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002233static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10002234{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002235 if (!vdev->config->get) {
2236 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2237 __func__);
2238 return -EINVAL;
2239 }
2240
Jason Wang892d6eb2014-11-20 17:03:05 +08002241 if (!virtnet_validate_features(vdev))
2242 return -EINVAL;
2243
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002244 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2245 int mtu = virtio_cread16(vdev,
2246 offsetof(struct virtio_net_config,
2247 mtu));
2248 if (mtu < MIN_MTU)
2249 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2250 }
2251
2252 return 0;
2253}
2254
2255static int virtnet_probe(struct virtio_device *vdev)
2256{
2257 int i, err;
2258 struct net_device *dev;
2259 struct virtnet_info *vi;
2260 u16 max_queue_pairs;
2261 int mtu;
2262
Jason Wang986a4f42012-12-07 07:04:56 +00002263 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302264 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2265 struct virtio_net_config,
2266 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002267
2268 /* We need at least 2 queue's */
2269 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2270 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2271 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2272 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002273
2274 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002275 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002276 if (!dev)
2277 return -ENOMEM;
2278
2279 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002280 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002281 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002282 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002283
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002284 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002285 SET_NETDEV_DEV(dev, &vdev->dev);
2286
2287 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002288 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002289 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002290 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002291 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002292 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002293
2294 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002295 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002296 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2297 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002298 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002299 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2300 dev->hw_features |= NETIF_F_TSO;
2301 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2302 dev->hw_features |= NETIF_F_TSO6;
2303 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2304 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002305 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2306 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002307
Jason Wang41f2f122014-12-24 11:03:52 +08002308 dev->features |= NETIF_F_GSO_ROBUST;
2309
Michał Mirosław98e778c2011-03-31 01:01:35 +00002310 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002311 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002312 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002313 }
Thomas Huth4f491292013-08-27 17:09:02 +02002314 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2315 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002316
Jason Wang4fda8302013-04-10 23:32:21 +00002317 dev->vlan_features = dev->features;
2318
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002319 /* MTU range: 68 - 65535 */
2320 dev->min_mtu = MIN_MTU;
2321 dev->max_mtu = MAX_MTU;
2322
Rusty Russell296f96f2007-10-22 11:03:37 +10002323 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302324 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2325 virtio_cread_bytes(vdev,
2326 offsetof(struct virtio_net_config, mac),
2327 dev->dev_addr, dev->addr_len);
2328 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002329 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002330
2331 /* Set up our device-specific information */
2332 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002333 vi->dev = dev;
2334 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002335 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002336 vi->stats = alloc_percpu(struct virtnet_stats);
2337 err = -ENOMEM;
2338 if (vi->stats == NULL)
2339 goto free;
2340
John Stultz827da442013-10-07 15:51:58 -07002341 for_each_possible_cpu(i) {
2342 struct virtnet_stats *virtnet_stats;
2343 virtnet_stats = per_cpu_ptr(vi->stats, i);
2344 u64_stats_init(&virtnet_stats->tx_syncp);
2345 u64_stats_init(&virtnet_stats->rx_syncp);
2346 }
2347
Jason Wang586d17c2012-04-11 20:43:52 +00002348 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002349
Herbert Xu97402b92008-04-18 11:24:27 +08002350 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002351 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2352 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002353 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2354 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002355 vi->big_packets = true;
2356
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002357 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2358 vi->mergeable_rx_bufs = true;
2359
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002360 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2361 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002362 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2363 else
2364 vi->hdr_len = sizeof(struct virtio_net_hdr);
2365
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002366 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2367 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302368 vi->any_header_sg = true;
2369
Jason Wang986a4f42012-12-07 07:04:56 +00002370 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2371 vi->has_cvq = true;
2372
Aaron Conole14de9d12016-06-03 16:57:12 -04002373 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2374 mtu = virtio_cread16(vdev,
2375 offsetof(struct virtio_net_config,
2376 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002377 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002378 /* Should never trigger: MTU was previously validated
2379 * in virtnet_validate.
2380 */
2381 dev_err(&vdev->dev, "device MTU appears to have changed "
2382 "it is now %d < %d", mtu, dev->min_mtu);
2383 goto free_stats;
Aaron Conole93a205e2016-10-25 16:12:12 -04002384 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002385
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002386 dev->mtu = mtu;
2387 dev->max_mtu = mtu;
2388
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002389 /* TODO: size buffers correctly in this case. */
2390 if (dev->mtu > ETH_DATA_LEN)
2391 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04002392 }
2393
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002394 if (vi->any_header_sg)
2395 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002396
Jason Wang44900012016-11-25 12:37:26 +08002397 /* Enable multiqueue by default */
2398 if (num_online_cpus() >= max_queue_pairs)
2399 vi->curr_queue_pairs = max_queue_pairs;
2400 else
2401 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002402 vi->max_queue_pairs = max_queue_pairs;
2403
2404 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302405 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002406 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002407 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002408
Michael Daltonfbf28d72014-01-16 22:23:30 -08002409#ifdef CONFIG_SYSFS
2410 if (vi->mergeable_rx_bufs)
2411 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2412#endif
Zhi Yong Wu0f13b66b2013-11-18 21:19:27 +08002413 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2414 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002415
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002416 virtnet_init_settings(dev);
2417
Rusty Russell296f96f2007-10-22 11:03:37 +10002418 err = register_netdev(dev);
2419 if (err) {
2420 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002421 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002422 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002423
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302424 virtio_device_ready(vdev);
2425
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002426 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002427 if (err) {
2428 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002429 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002430 }
2431
Jason Wanga2208712016-12-13 14:23:05 +08002432 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08002433
Jason Wang167c25e2010-11-10 14:45:41 +00002434 /* Assume link up if device can't report link status,
2435 otherwise get link status from config. */
2436 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2437 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002438 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002439 } else {
2440 vi->status = VIRTIO_NET_S_LINK_UP;
2441 netif_carrier_on(dev);
2442 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002443
Jason Wang986a4f42012-12-07 07:04:56 +00002444 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2445 dev->name, max_queue_pairs);
2446
Rusty Russell296f96f2007-10-22 11:03:37 +10002447 return 0;
2448
wangyunjianf00e35e2016-05-31 11:52:43 +08002449free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302450 vi->vdev->config->reset(vdev);
2451
Rusty Russellb3369c12008-02-04 23:50:02 -05002452 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002453free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002454 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002455 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002456 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002457free_stats:
2458 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002459free:
2460 free_netdev(dev);
2461 return err;
2462}
2463
John Fastabend2de2f7f2017-02-02 19:16:29 -08002464static void _remove_vq_common(struct virtnet_info *vi)
2465{
2466 vi->vdev->config->reset(vi->vdev);
2467 free_unused_bufs(vi);
2468 _free_receive_bufs(vi);
2469 free_receive_page_frags(vi);
2470 virtnet_del_vqs(vi);
2471}
2472
Amit Shah04486ed2011-12-22 16:58:32 +05302473static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002474{
Amit Shah04486ed2011-12-22 16:58:32 +05302475 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002476
2477 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002478 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002479
Jason Wang986a4f42012-12-07 07:04:56 +00002480 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002481
Michael Daltonfb518792014-01-16 22:23:26 -08002482 free_receive_page_frags(vi);
2483
Jason Wang986a4f42012-12-07 07:04:56 +00002484 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302485}
2486
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002487static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302488{
2489 struct virtnet_info *vi = vdev->priv;
2490
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002491 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002492
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302493 /* Make sure no work handler is accessing the device. */
2494 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002495
Amit Shah04486ed2011-12-22 16:58:32 +05302496 unregister_netdev(vi->dev);
2497
2498 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002499
Krishna Kumar2e66f552011-07-20 03:56:02 +00002500 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002501 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002502}
2503
Aaron Lu89107002013-09-17 09:25:23 +09302504#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302505static int virtnet_freeze(struct virtio_device *vdev)
2506{
2507 struct virtnet_info *vi = vdev->priv;
2508
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002509 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002510 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05302511 remove_vq_common(vi);
2512
2513 return 0;
2514}
2515
2516static int virtnet_restore(struct virtio_device *vdev)
2517{
2518 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002519 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05302520
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002521 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05302522 if (err)
2523 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00002524 virtnet_set_queues(vi, vi->curr_queue_pairs);
2525
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002526 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002527 if (err)
2528 return err;
2529
Amit Shah0741bcb2011-12-22 16:58:33 +05302530 return 0;
2531}
2532#endif
2533
Rusty Russell296f96f2007-10-22 11:03:37 +10002534static struct virtio_device_id id_table[] = {
2535 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2536 { 0 },
2537};
2538
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002539#define VIRTNET_FEATURES \
2540 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2541 VIRTIO_NET_F_MAC, \
2542 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2543 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2544 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2545 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2546 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2547 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2548 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2549 VIRTIO_NET_F_MTU
2550
Rusty Russellc45a6812008-05-02 21:50:50 -05002551static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002552 VIRTNET_FEATURES,
2553};
2554
2555static unsigned int features_legacy[] = {
2556 VIRTNET_FEATURES,
2557 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302558 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002559};
2560
Uwe Kleine-König22402522009-11-05 01:32:44 -08002561static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002562 .feature_table = features,
2563 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002564 .feature_table_legacy = features_legacy,
2565 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002566 .driver.name = KBUILD_MODNAME,
2567 .driver.owner = THIS_MODULE,
2568 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002569 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10002570 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002571 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002572 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302573#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302574 .freeze = virtnet_freeze,
2575 .restore = virtnet_restore,
2576#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002577};
2578
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002579static __init int virtio_net_driver_init(void)
2580{
2581 int ret;
2582
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002583 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002584 virtnet_cpu_online,
2585 virtnet_cpu_down_prep);
2586 if (ret < 0)
2587 goto out;
2588 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002589 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002590 NULL, virtnet_cpu_dead);
2591 if (ret)
2592 goto err_dead;
2593
2594 ret = register_virtio_driver(&virtio_net_driver);
2595 if (ret)
2596 goto err_virtio;
2597 return 0;
2598err_virtio:
2599 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2600err_dead:
2601 cpuhp_remove_multi_state(virtionet_online);
2602out:
2603 return ret;
2604}
2605module_init(virtio_net_driver_init);
2606
2607static __exit void virtio_net_driver_exit(void)
2608{
2609 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2610 cpuhp_remove_multi_state(virtionet_online);
2611 unregister_virtio_driver(&virtio_net_driver);
2612}
2613module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002614
2615MODULE_DEVICE_TABLE(virtio, id_table);
2616MODULE_DESCRIPTION("Virtio network driver");
2617MODULE_LICENSE("GPL");