blob: 765c2d6358daf38203cdb1a50a31cc04f65c1968 [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>
Rusty Russell296f96f2007-10-22 11:03:37 +100026#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080027#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000029#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080030#include <linux/average.h>
Jason Wang91815632014-07-23 16:33:55 +080031#include <net/busy_poll.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100032
Amerigo Wangd34710e2013-05-09 19:50:51 +000033static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020034module_param(napi_weight, int, 0444);
35
Rusty Russelleb939922011-12-19 14:08:01 +000036static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050037module_param(csum, bool, 0444);
38module_param(gso, bool, 0444);
39
Rusty Russell296f96f2007-10-22 11:03:37 +100040/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080041#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080042#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100043
Johannes Berg5377d7582015-08-19 09:48:40 +020044/* RX packet size EWMA. The average packet size is used to determine the packet
45 * buffer size when refilling RX rings. As the entire RX ring may be refilled
46 * at once, the weight is chosen so that the EWMA will be insensitive to short-
47 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080048 */
Johannes Berg5377d7582015-08-19 09:48:40 +020049DECLARE_EWMA(pkt_len, 1, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080050
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020051/* With mergeable buffers we align buffer address and use the low bits to
52 * encode its true size. Buffer size is up to 1 page so we need to align to
53 * square root of page size to ensure we reserve enough bits to encode the true
54 * size.
55 */
56#define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
57
Michael Daltonab7db912014-01-16 22:23:27 -080058/* Minimum alignment for mergeable packet buffers. */
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020059#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
60 1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
Michael Daltonab7db912014-01-16 22:23:27 -080061
Rick Jones66846042011-11-14 14:17:08 +000062#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000063
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000064struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000065 struct u64_stats_sync tx_syncp;
66 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000067 u64 tx_bytes;
68 u64 tx_packets;
69
70 u64 rx_bytes;
71 u64 rx_packets;
72};
73
Jason Wange9d74172012-12-07 07:04:55 +000074/* Internal representation of a send virtqueue */
75struct send_queue {
76 /* Virtqueue associated with this send _queue */
77 struct virtqueue *vq;
78
79 /* TX: fragments + linear part + virtio header */
80 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000081
82 /* Name of the send queue: output.$index */
83 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000084};
85
86/* Internal representation of a receive virtqueue */
87struct receive_queue {
88 /* Virtqueue associated with this receive_queue */
89 struct virtqueue *vq;
90
Rusty Russell296f96f2007-10-22 11:03:37 +100091 struct napi_struct napi;
92
John Fastabendf600b692016-12-15 12:13:24 -080093 struct bpf_prog __rcu *xdp_prog;
94
Jason Wange9d74172012-12-07 07:04:55 +000095 /* Chain pages by the private ptr. */
96 struct page *pages;
97
Michael Daltonab7db912014-01-16 22:23:27 -080098 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +020099 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800100
Michael Daltonfb518792014-01-16 22:23:26 -0800101 /* Page frag for packet buffer allocation. */
102 struct page_frag alloc_frag;
103
Jason Wange9d74172012-12-07 07:04:55 +0000104 /* RX: fragments + linear part + virtio header */
105 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000106
107 /* Name of this receive queue: input.$index */
108 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +0000109};
110
111struct virtnet_info {
112 struct virtio_device *vdev;
113 struct virtqueue *cvq;
114 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000115 struct send_queue *sq;
116 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000117 unsigned int status;
118
Jason Wang986a4f42012-12-07 07:04:56 +0000119 /* Max # of queue pairs supported by the device */
120 u16 max_queue_pairs;
121
122 /* # of queue pairs currently used by the driver */
123 u16 curr_queue_pairs;
124
John Fastabend672aafd2016-12-15 12:13:49 -0800125 /* # of XDP queue pairs currently used by the driver */
126 u16 xdp_queue_pairs;
127
Herbert Xu97402b92008-04-18 11:24:27 +0800128 /* I like... big packets and I cannot lie! */
129 bool big_packets;
130
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800131 /* Host will merge rx buffers for big packets (shake it! shake it!) */
132 bool mergeable_rx_bufs;
133
Jason Wang986a4f42012-12-07 07:04:56 +0000134 /* Has control virtqueue */
135 bool has_cvq;
136
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930137 /* Host can handle any s/g split between our header and packet data */
138 bool any_header_sg;
139
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300140 /* Packet virtio header size */
141 u8 hdr_len;
142
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000143 /* Active statistics */
144 struct virtnet_stats __percpu *stats;
145
Rusty Russell3161e452009-08-26 12:22:32 -0700146 /* Work struct for refilling if we run low on memory. */
147 struct delayed_work refill;
148
Jason Wang586d17c2012-04-11 20:43:52 +0000149 /* Work struct for config space updates */
150 struct work_struct config_work;
151
Jason Wang986a4f42012-12-07 07:04:56 +0000152 /* Does the affinity hint is set for virtqueues? */
153 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000154
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200155 /* CPU hotplug instances for online & dead */
156 struct hlist_node node;
157 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200158
159 /* Control VQ buffers: protected by the rtnl lock */
160 struct virtio_net_ctrl_hdr ctrl_hdr;
161 virtio_net_ctrl_ack ctrl_status;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700162 struct virtio_net_ctrl_mq ctrl_mq;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200163 u8 ctrl_promisc;
164 u8 ctrl_allmulti;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700165 u16 ctrl_vid;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100166
167 /* Ethtool settings */
168 u8 duplex;
169 u32 speed;
Rusty Russell296f96f2007-10-22 11:03:37 +1000170};
171
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000172struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300173 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000174 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300175 * hdr is in a separate sg buffer, and data sg buffer shares same page
176 * with this header sg. This padding makes next sg 16 byte aligned
177 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000178 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300179 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000180};
181
Jason Wang986a4f42012-12-07 07:04:56 +0000182/* Converting between virtqueue no. and kernel tx/rx queue no.
183 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
184 */
185static int vq2txq(struct virtqueue *vq)
186{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000187 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000188}
189
190static int txq2vq(int txq)
191{
192 return txq * 2 + 1;
193}
194
195static int vq2rxq(struct virtqueue *vq)
196{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000197 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000198}
199
200static int rxq2vq(int rxq)
201{
202 return rxq * 2;
203}
204
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300205static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000206{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300207 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000208}
209
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000210/*
211 * private is used to chain pages for big packets, put the whole
212 * most recent used list in the beginning for reuse
213 */
Jason Wange9d74172012-12-07 07:04:55 +0000214static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500215{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000216 struct page *end;
217
Jason Wange9d74172012-12-07 07:04:55 +0000218 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000219 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000220 end->private = (unsigned long)rq->pages;
221 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500222}
223
Jason Wange9d74172012-12-07 07:04:55 +0000224static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500225{
Jason Wange9d74172012-12-07 07:04:55 +0000226 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500227
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000228 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000229 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000230 /* clear private here, it is used to chain pages */
231 p->private = 0;
232 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500233 p = alloc_page(gfp_mask);
234 return p;
235}
236
Jason Wange9d74172012-12-07 07:04:55 +0000237static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000238{
Jason Wange9d74172012-12-07 07:04:55 +0000239 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000240
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500241 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000242 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000243
Rusty Russell363f1512008-06-08 20:51:55 +1000244 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000245 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000246}
247
Michael Daltonab7db912014-01-16 22:23:27 -0800248static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
249{
250 unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
251 return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
252}
253
254static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
255{
256 return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
257
258}
259
260static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
261{
262 unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
263 return (unsigned long)buf | (size - 1);
264}
265
Mike Waychison34646452012-01-04 12:52:32 +0000266/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300267static struct sk_buff *page_to_skb(struct virtnet_info *vi,
268 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700269 struct page *page, unsigned int offset,
270 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000271{
272 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300273 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700274 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000275 char *p;
276
Michael Dalton2613af02013-10-28 15:44:18 -0700277 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000278
279 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100280 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000281 if (unlikely(!skb))
282 return NULL;
283
284 hdr = skb_vnet_hdr(skb);
285
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300286 hdr_len = vi->hdr_len;
287 if (vi->mergeable_rx_bufs)
288 hdr_padded_len = sizeof *hdr;
289 else
Michael Dalton2613af02013-10-28 15:44:18 -0700290 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000291
292 memcpy(hdr, p, hdr_len);
293
294 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700295 offset += hdr_padded_len;
296 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000297
298 copy = len;
299 if (copy > skb_tailroom(skb))
300 copy = skb_tailroom(skb);
301 memcpy(skb_put(skb, copy), p, copy);
302
303 len -= copy;
304 offset += copy;
305
Michael Dalton2613af02013-10-28 15:44:18 -0700306 if (vi->mergeable_rx_bufs) {
307 if (len)
308 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
309 else
310 put_page(page);
311 return skb;
312 }
313
Sasha Levine878d782011-09-28 04:40:54 +0000314 /*
315 * Verify that we can indeed put this data into a skb.
316 * This is here to handle cases when the device erroneously
317 * tries to receive more than is possible. This is usually
318 * the case of a broken device.
319 */
320 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000321 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000322 dev_kfree_skb(skb);
323 return NULL;
324 }
Michael Dalton2613af02013-10-28 15:44:18 -0700325 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000326 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700327 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
328 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
329 frag_size, truesize);
330 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000331 page = (struct page *)page->private;
332 offset = 0;
333 }
334
335 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000336 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000337
338 return skb;
339}
340
John Fastabend56434a02016-12-15 12:14:13 -0800341static void virtnet_xdp_xmit(struct virtnet_info *vi,
342 struct receive_queue *rq,
343 struct send_queue *sq,
Jason Wangbb91acc2016-12-23 22:37:32 +0800344 struct xdp_buff *xdp,
345 void *data)
John Fastabend56434a02016-12-15 12:14:13 -0800346{
John Fastabend56434a02016-12-15 12:14:13 -0800347 struct virtio_net_hdr_mrg_rxbuf *hdr;
348 unsigned int num_sg, len;
349 void *xdp_sent;
350 int err;
351
352 /* Free up any pending old buffers before queueing new ones. */
353 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800354 if (vi->mergeable_rx_bufs) {
355 struct page *sent_page = virt_to_head_page(xdp_sent);
356
357 put_page(sent_page);
358 } else { /* small buffer */
359 struct sk_buff *skb = xdp_sent;
360
361 kfree_skb(skb);
362 }
John Fastabend56434a02016-12-15 12:14:13 -0800363 }
364
Jason Wangbb91acc2016-12-23 22:37:32 +0800365 if (vi->mergeable_rx_bufs) {
366 /* Zero header and leave csum up to XDP layers */
367 hdr = xdp->data;
368 memset(hdr, 0, vi->hdr_len);
John Fastabend56434a02016-12-15 12:14:13 -0800369
Jason Wangbb91acc2016-12-23 22:37:32 +0800370 num_sg = 1;
371 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
372 } else { /* small buffer */
373 struct sk_buff *skb = data;
374
375 /* Zero header and leave csum up to XDP layers */
376 hdr = skb_vnet_hdr(skb);
377 memset(hdr, 0, vi->hdr_len);
378
379 num_sg = 2;
380 sg_init_table(sq->sg, 2);
381 sg_set_buf(sq->sg, hdr, vi->hdr_len);
382 skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
383 }
John Fastabend56434a02016-12-15 12:14:13 -0800384 err = virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
Jason Wangbb91acc2016-12-23 22:37:32 +0800385 data, GFP_ATOMIC);
John Fastabend56434a02016-12-15 12:14:13 -0800386 if (unlikely(err)) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800387 if (vi->mergeable_rx_bufs) {
388 struct page *page = virt_to_head_page(xdp->data);
389
390 put_page(page);
391 } else /* small buffer */
392 kfree_skb(data);
John Fastabend56434a02016-12-15 12:14:13 -0800393 return; // On error abort to avoid unnecessary kick
John Fastabend56434a02016-12-15 12:14:13 -0800394 }
395
396 virtqueue_kick(sq->vq);
397}
398
John Fastabendf600b692016-12-15 12:13:24 -0800399static u32 do_xdp_prog(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800400 struct receive_queue *rq,
John Fastabendf600b692016-12-15 12:13:24 -0800401 struct bpf_prog *xdp_prog,
Jason Wangbb91acc2016-12-23 22:37:32 +0800402 void *data, int len)
John Fastabendf600b692016-12-15 12:13:24 -0800403{
404 int hdr_padded_len;
405 struct xdp_buff xdp;
Jason Wangbb91acc2016-12-23 22:37:32 +0800406 void *buf;
John Fastabend56434a02016-12-15 12:14:13 -0800407 unsigned int qp;
John Fastabendf600b692016-12-15 12:13:24 -0800408 u32 act;
John Fastabendf600b692016-12-15 12:13:24 -0800409
Jason Wangbb91acc2016-12-23 22:37:32 +0800410 if (vi->mergeable_rx_bufs) {
John Fastabendf600b692016-12-15 12:13:24 -0800411 hdr_padded_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Jason Wangbb91acc2016-12-23 22:37:32 +0800412 xdp.data = data + hdr_padded_len;
413 xdp.data_end = xdp.data + (len - vi->hdr_len);
414 buf = data;
415 } else { /* small buffers */
416 struct sk_buff *skb = data;
John Fastabendf600b692016-12-15 12:13:24 -0800417
Jason Wangbb91acc2016-12-23 22:37:32 +0800418 xdp.data = skb->data;
419 xdp.data_end = xdp.data + len;
420 buf = skb->data;
421 }
John Fastabendf600b692016-12-15 12:13:24 -0800422
423 act = bpf_prog_run_xdp(xdp_prog, &xdp);
424 switch (act) {
425 case XDP_PASS:
426 return XDP_PASS;
John Fastabend56434a02016-12-15 12:14:13 -0800427 case XDP_TX:
428 qp = vi->curr_queue_pairs -
429 vi->xdp_queue_pairs +
430 smp_processor_id();
Jason Wangbb91acc2016-12-23 22:37:32 +0800431 xdp.data = buf;
432 virtnet_xdp_xmit(vi, rq, &vi->sq[qp], &xdp, data);
John Fastabend56434a02016-12-15 12:14:13 -0800433 return XDP_TX;
John Fastabendf600b692016-12-15 12:13:24 -0800434 default:
435 bpf_warn_invalid_xdp_action(act);
John Fastabendf600b692016-12-15 12:13:24 -0800436 case XDP_ABORTED:
437 case XDP_DROP:
438 return XDP_DROP;
439 }
440}
441
Jason Wangbb91acc2016-12-23 22:37:32 +0800442static struct sk_buff *receive_small(struct net_device *dev,
443 struct virtnet_info *vi,
444 struct receive_queue *rq,
445 void *buf, unsigned int len)
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200446{
447 struct sk_buff * skb = buf;
Jason Wangbb91acc2016-12-23 22:37:32 +0800448 struct bpf_prog *xdp_prog;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200449
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300450 len -= vi->hdr_len;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200451 skb_trim(skb, len);
452
Jason Wangbb91acc2016-12-23 22:37:32 +0800453 rcu_read_lock();
454 xdp_prog = rcu_dereference(rq->xdp_prog);
455 if (xdp_prog) {
456 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
457 u32 act;
458
459 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
460 goto err_xdp;
461 act = do_xdp_prog(vi, rq, xdp_prog, skb, len);
462 switch (act) {
463 case XDP_PASS:
464 break;
465 case XDP_TX:
466 rcu_read_unlock();
467 goto xdp_xmit;
468 case XDP_DROP:
469 default:
470 goto err_xdp;
471 }
472 }
473 rcu_read_unlock();
474
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200475 return skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800476
477err_xdp:
478 rcu_read_unlock();
479 dev->stats.rx_dropped++;
480 kfree_skb(skb);
481xdp_xmit:
482 return NULL;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200483}
484
485static struct sk_buff *receive_big(struct net_device *dev,
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300486 struct virtnet_info *vi,
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200487 struct receive_queue *rq,
488 void *buf,
489 unsigned int len)
490{
491 struct page *page = buf;
Jason Wangc47a43d2016-12-23 22:37:31 +0800492 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200493
494 if (unlikely(!skb))
495 goto err;
496
497 return skb;
498
499err:
500 dev->stats.rx_dropped++;
501 give_pages(rq, page);
502 return NULL;
503}
504
John Fastabend72979a62016-12-15 12:14:36 -0800505/* The conditions to enable XDP should preclude the underlying device from
506 * sending packets across multiple buffers (num_buf > 1). However per spec
507 * it does not appear to be illegal to do so but rather just against convention.
508 * So in order to avoid making a system unresponsive the packets are pushed
509 * into a page and the XDP program is run. This will be extremely slow and we
510 * push a warning to the user to fix this as soon as possible. Fixing this may
511 * require resolving the underlying hardware to determine why multiple buffers
512 * are being received or simply loading the XDP program in the ingress stack
513 * after the skb is built because there is no advantage to running it here
514 * anymore.
515 */
516static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800517 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800518 struct page *p,
519 int offset,
520 unsigned int *len)
521{
522 struct page *page = alloc_page(GFP_ATOMIC);
523 unsigned int page_off = 0;
524
525 if (!page)
526 return NULL;
527
528 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
529 page_off += *len;
530
Jason Wang56a86f82016-12-23 22:37:26 +0800531 while (--*num_buf) {
John Fastabend72979a62016-12-15 12:14:36 -0800532 unsigned int buflen;
533 unsigned long ctx;
534 void *buf;
535 int off;
536
537 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
538 if (unlikely(!ctx))
539 goto err_buf;
540
John Fastabend72979a62016-12-15 12:14:36 -0800541 buf = mergeable_ctx_to_buf_address(ctx);
542 p = virt_to_head_page(buf);
543 off = buf - page_address(p);
544
Jason Wang56a86f82016-12-23 22:37:26 +0800545 /* guard against a misconfigured or uncooperative backend that
546 * is sending packet larger than the MTU.
547 */
548 if ((page_off + buflen) > PAGE_SIZE) {
549 put_page(p);
550 goto err_buf;
551 }
552
John Fastabend72979a62016-12-15 12:14:36 -0800553 memcpy(page_address(page) + page_off,
554 page_address(p) + off, buflen);
555 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800556 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800557 }
558
559 *len = page_off;
560 return page;
561err_buf:
562 __free_pages(page, 0);
563 return NULL;
564}
565
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200566static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200567 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200568 struct receive_queue *rq,
Michael Daltonab7db912014-01-16 22:23:27 -0800569 unsigned long ctx,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200570 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000571{
Michael Daltonab7db912014-01-16 22:23:27 -0800572 void *buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300573 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
574 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200575 struct page *page = virt_to_head_page(buf);
576 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800577 struct sk_buff *head_skb, *curr_skb;
578 struct bpf_prog *xdp_prog;
579 unsigned int truesize;
Michael Daltonab7db912014-01-16 22:23:27 -0800580
John Fastabend56434a02016-12-15 12:14:13 -0800581 head_skb = NULL;
582
John Fastabendf600b692016-12-15 12:13:24 -0800583 rcu_read_lock();
584 xdp_prog = rcu_dereference(rq->xdp_prog);
585 if (xdp_prog) {
John Fastabend72979a62016-12-15 12:14:36 -0800586 struct page *xdp_page;
John Fastabendf600b692016-12-15 12:13:24 -0800587 u32 act;
588
Jason Wang73b62bd2016-12-23 22:37:24 +0800589 /* This happens when rx buffer size is underestimated */
John Fastabendf600b692016-12-15 12:13:24 -0800590 if (unlikely(num_buf > 1)) {
John Fastabend72979a62016-12-15 12:14:36 -0800591 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800592 xdp_page = xdp_linearize_page(rq, &num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800593 page, offset, &len);
594 if (!xdp_page)
595 goto err_xdp;
596 offset = 0;
597 } else {
598 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800599 }
600
601 /* Transient failure which in theory could occur if
602 * in-flight packets from before XDP was enabled reach
603 * the receive path after XDP is loaded. In practice I
604 * was not able to create this condition.
605 */
Jason Wangb00f70b2016-12-23 22:37:28 +0800606 if (unlikely(hdr->hdr.gso_type))
John Fastabendf600b692016-12-15 12:13:24 -0800607 goto err_xdp;
608
Jason Wangbb91acc2016-12-23 22:37:32 +0800609 act = do_xdp_prog(vi, rq, xdp_prog,
610 page_address(xdp_page) + offset, len);
John Fastabend56434a02016-12-15 12:14:13 -0800611 switch (act) {
612 case XDP_PASS:
Jason Wang1830f892016-12-23 22:37:27 +0800613 /* We can only create skb based on xdp_page. */
614 if (unlikely(xdp_page != page)) {
615 rcu_read_unlock();
616 put_page(page);
617 head_skb = page_to_skb(vi, rq, xdp_page,
618 0, len, PAGE_SIZE);
Jason Wang5c334742016-12-23 22:37:29 +0800619 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
Jason Wang1830f892016-12-23 22:37:27 +0800620 return head_skb;
621 }
John Fastabend56434a02016-12-15 12:14:13 -0800622 break;
623 case XDP_TX:
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;
629 case XDP_DROP:
630 default:
John Fastabend72979a62016-12-15 12:14:36 -0800631 if (unlikely(xdp_page != page))
632 __free_pages(xdp_page, 0);
Jason Wang5c334742016-12-23 22:37:29 +0800633 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabendf600b692016-12-15 12:13:24 -0800634 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800635 }
John Fastabendf600b692016-12-15 12:13:24 -0800636 }
637 rcu_read_unlock();
638
639 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
640 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
641 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000642
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200643 if (unlikely(!curr_skb))
644 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000645 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200646 int num_skb_frags;
647
Michael Daltonab7db912014-01-16 22:23:27 -0800648 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
649 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200650 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200651 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300652 virtio16_to_cpu(vi->vdev,
653 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200654 dev->stats.rx_length_errors++;
655 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000656 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200657
Michael Daltonab7db912014-01-16 22:23:27 -0800658 buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200659 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200660
661 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700662 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
663 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200664
665 if (unlikely(!nskb))
666 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700667 if (curr_skb == head_skb)
668 skb_shinfo(curr_skb)->frag_list = nskb;
669 else
670 curr_skb->next = nskb;
671 curr_skb = nskb;
672 head_skb->truesize += nskb->truesize;
673 num_skb_frags = 0;
674 }
Michael Daltonab7db912014-01-16 22:23:27 -0800675 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
Michael Dalton2613af02013-10-28 15:44:18 -0700676 if (curr_skb != head_skb) {
677 head_skb->data_len += len;
678 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800679 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700680 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200681 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800682 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
683 put_page(page);
684 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800685 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800686 } else {
687 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800688 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800689 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200690 }
691
Johannes Berg5377d7582015-08-19 09:48:40 +0200692 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200693 return head_skb;
694
John Fastabendf600b692016-12-15 12:13:24 -0800695err_xdp:
696 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200697err_skb:
698 put_page(page);
699 while (--num_buf) {
Michael Daltonab7db912014-01-16 22:23:27 -0800700 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
701 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200702 pr_debug("%s: rx error: %d buffers missing\n",
703 dev->name, num_buf);
704 dev->stats.rx_length_errors++;
705 break;
706 }
Michael Daltonab7db912014-01-16 22:23:27 -0800707 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200708 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000709 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200710err_buf:
711 dev->stats.rx_dropped++;
712 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800713xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200714 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000715}
716
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300717static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
718 void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000719{
Jason Wange9d74172012-12-07 07:04:55 +0000720 struct net_device *dev = vi->dev;
Eric Dumazet58472a72012-02-13 06:53:41 +0000721 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000722 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300723 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000724
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300725 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000726 pr_debug("%s: short packet %i\n", dev->name, len);
727 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800728 if (vi->mergeable_rx_bufs) {
729 unsigned long ctx = (unsigned long)buf;
730 void *base = mergeable_ctx_to_buf_address(ctx);
731 put_page(virt_to_head_page(base));
732 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800733 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800734 } else {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000735 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800736 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000737 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000738 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000739
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200740 if (vi->mergeable_rx_bufs)
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200741 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200742 else if (vi->big_packets)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300743 skb = receive_big(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200744 else
Jason Wangbb91acc2016-12-23 22:37:32 +0800745 skb = receive_small(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200746
747 if (unlikely(!skb))
748 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800749
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000750 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000751
Eric Dumazet83a27052012-06-05 22:35:24 +0000752 u64_stats_update_begin(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000753 stats->rx_bytes += skb->len;
754 stats->rx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000755 u64_stats_update_end(&stats->rx_syncp);
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);
Rusty Russell296f96f2007-10-22 11:03:37 +1000773 return;
774
775frame_err:
776 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000777 dev_kfree_skb(skb);
778}
779
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300780static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
781 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000782{
783 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300784 struct virtio_net_hdr_mrg_rxbuf *hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000785 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000786
Michael Dalton5061de32013-11-14 10:41:04 -0800787 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000788 if (unlikely(!skb))
789 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800790
Michael Dalton5061de32013-11-14 10:41:04 -0800791 skb_put(skb, GOOD_PACKET_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000792
793 hdr = skb_vnet_hdr(skb);
Jason Wang547c8902015-08-27 14:53:06 +0800794 sg_init_table(rq->sg, 2);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300795 sg_set_buf(rq->sg, hdr, vi->hdr_len);
Jason Wange9d74172012-12-07 07:04:55 +0000796 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000797
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030798 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000799 if (err < 0)
800 dev_kfree_skb(skb);
801
802 return err;
803}
804
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300805static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
806 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000807{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000808 struct page *first, *list = NULL;
809 char *p;
810 int i, err, offset;
811
Rusty Russella5835442014-09-11 10:17:36 +0930812 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
813
Jason Wange9d74172012-12-07 07:04:55 +0000814 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000815 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000816 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000817 if (!first) {
818 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000819 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000820 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700821 }
Jason Wange9d74172012-12-07 07:04:55 +0000822 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000823
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000824 /* chain new page in list head to match sg */
825 first->private = (unsigned long)list;
826 list = first;
827 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800828
Jason Wange9d74172012-12-07 07:04:55 +0000829 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000830 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000831 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000832 return -ENOMEM;
833 }
834 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800835
Jason Wange9d74172012-12-07 07:04:55 +0000836 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300837 /* a separated rq->sg[0] for header - required in case !any_header_sg */
838 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +0800839
Jason Wange9d74172012-12-07 07:04:55 +0000840 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000841 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000842 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800843
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000844 /* chain first in list head */
845 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030846 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
847 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000848 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000849 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800850
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000851 return err;
852}
Herbert Xu97402b92008-04-18 11:24:27 +0800853
Johannes Berg5377d7582015-08-19 09:48:40 +0200854static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000855{
Michael Daltonab7db912014-01-16 22:23:27 -0800856 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800857 unsigned int len;
858
Johannes Berg5377d7582015-08-19 09:48:40 +0200859 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael Daltonfbf28d72014-01-16 22:23:30 -0800860 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
861 return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
862}
863
864static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
865{
Michael Daltonfb518792014-01-16 22:23:26 -0800866 struct page_frag *alloc_frag = &rq->alloc_frag;
867 char *buf;
Michael Daltonab7db912014-01-16 22:23:27 -0800868 unsigned long ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000869 int err;
Michael Daltonfb518792014-01-16 22:23:26 -0800870 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +1000871
Michael Daltonfbf28d72014-01-16 22:23:30 -0800872 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
Michael Daltonab7db912014-01-16 22:23:27 -0800873 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000874 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -0800875
Michael Daltonfb518792014-01-16 22:23:26 -0800876 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800877 ctx = mergeable_buf_to_ctx(buf, len);
Michael Daltonfb518792014-01-16 22:23:26 -0800878 get_page(alloc_frag->page);
Michael Daltonfb518792014-01-16 22:23:26 -0800879 alloc_frag->offset += len;
880 hole = alloc_frag->size - alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800881 if (hole < len) {
882 /* To avoid internal fragmentation, if there is very likely not
883 * enough space for another buffer, add the remaining space to
884 * the current buffer. This extra space is not included in
885 * the truesize stored in ctx.
886 */
Michael Daltonfb518792014-01-16 22:23:26 -0800887 len += hole;
888 alloc_frag->offset += hole;
889 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000890
Michael Daltonfb518792014-01-16 22:23:26 -0800891 sg_init_one(rq->sg, buf, len);
Michael Daltonab7db912014-01-16 22:23:27 -0800892 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000893 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700894 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000895
896 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000897}
898
Rusty Russellb2baed62011-12-29 00:42:38 +0000899/*
900 * Returns false if we couldn't fill entirely (OOM).
901 *
902 * Normally run in the receive path, but can also be run from ndo_open
903 * before we're receiving packets, or from refill_work which is
904 * careful to disable receiving (using napi_disable).
905 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300906static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
907 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800908{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800909 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000910 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800911
Michael Daltonfb518792014-01-16 22:23:26 -0800912 gfp |= __GFP_COLD;
Amit Shah0aea51c2009-08-26 14:58:28 +0530913 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000914 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000915 err = add_recvbuf_mergeable(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000916 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300917 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000918 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300919 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800920
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000921 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030922 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800923 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800924 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +0800925 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700926 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800927}
928
Rusty Russell18445c42008-02-04 23:49:57 -0500929static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000930{
931 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000932 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000933
Rusty Russell18445c42008-02-04 23:49:57 -0500934 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000935 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300936 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000937 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500938 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000939}
940
Jason Wange9d74172012-12-07 07:04:55 +0000941static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800942{
Jason Wange9d74172012-12-07 07:04:55 +0000943 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800944
945 /* If all buffers were filled by other side before we napi_enabled, we
946 * won't get another interrupt, so process any outstanding packets
947 * now. virtnet_poll wants re-enable the queue, so we disable here.
948 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000949 if (napi_schedule_prep(&rq->napi)) {
950 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300951 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000952 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300953 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800954 }
955}
956
Rusty Russell3161e452009-08-26 12:22:32 -0700957static void refill_work(struct work_struct *work)
958{
Jason Wange9d74172012-12-07 07:04:55 +0000959 struct virtnet_info *vi =
960 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700961 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000962 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700963
Sasha Levin55257d72013-04-29 12:00:08 +0930964 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000965 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700966
Jason Wang986a4f42012-12-07 07:04:56 +0000967 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300968 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +0000969 virtnet_napi_enable(rq);
970
971 /* In theory, this can happen: if we don't get any buffers in
972 * we will *never* try to fill again.
973 */
974 if (still_empty)
975 schedule_delayed_work(&vi->refill, HZ/2);
976 }
Rusty Russell3161e452009-08-26 12:22:32 -0700977}
978
Jason Wang2ffa7592014-07-23 16:33:54 +0800979static int virtnet_receive(struct receive_queue *rq, int budget)
Rusty Russell296f96f2007-10-22 11:03:37 +1000980{
Jason Wange9d74172012-12-07 07:04:55 +0000981 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wang2ffa7592014-07-23 16:33:54 +0800982 unsigned int len, received = 0;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000983 void *buf;
Rusty Russell296f96f2007-10-22 11:03:37 +1000984
Rusty Russell296f96f2007-10-22 11:03:37 +1000985 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +0000986 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300987 receive_buf(vi, rq, buf, len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000988 received++;
989 }
990
Jason Wangbe121f42014-01-16 14:45:24 +0800991 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300992 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700993 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -0700994 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000995
Jason Wang2ffa7592014-07-23 16:33:54 +0800996 return received;
997}
998
999static int virtnet_poll(struct napi_struct *napi, int budget)
1000{
1001 struct receive_queue *rq =
1002 container_of(napi, struct receive_queue, napi);
Li RongQingfaadb052015-03-26 15:39:45 +08001003 unsigned int r, received;
Jason Wang2ffa7592014-07-23 16:33:54 +08001004
Li RongQingfaadb052015-03-26 15:39:45 +08001005 received = virtnet_receive(rq, budget);
Jason Wang2ffa7592014-07-23 16:33:54 +08001006
Rusty Russell8329d982007-11-19 11:20:43 -05001007 /* Out of packets? */
1008 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001009 r = virtqueue_enable_cb_prepare(rq->vq);
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001010 napi_complete_done(napi, received);
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001011 if (unlikely(virtqueue_poll(rq->vq, r)) &&
Joe Perches8e95a202009-12-03 07:58:21 +00001012 napi_schedule_prep(napi)) {
Jason Wange9d74172012-12-07 07:04:55 +00001013 virtqueue_disable_cb(rq->vq);
Ben Hutchings288379f2009-01-19 16:43:59 -08001014 __napi_schedule(napi);
Christian Borntraeger4265f162008-03-14 14:17:05 +01001015 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001016 }
1017
1018 return received;
1019}
1020
Jason Wang91815632014-07-23 16:33:55 +08001021#ifdef CONFIG_NET_RX_BUSY_POLL
1022/* must be called with local_bh_disable()d */
1023static int virtnet_busy_poll(struct napi_struct *napi)
1024{
1025 struct receive_queue *rq =
1026 container_of(napi, struct receive_queue, napi);
1027 struct virtnet_info *vi = rq->vq->vdev->priv;
1028 int r, received = 0, budget = 4;
1029
1030 if (!(vi->status & VIRTIO_NET_S_LINK_UP))
1031 return LL_FLUSH_FAILED;
1032
1033 if (!napi_schedule_prep(napi))
1034 return LL_FLUSH_BUSY;
1035
1036 virtqueue_disable_cb(rq->vq);
1037
1038again:
1039 received += virtnet_receive(rq, budget);
1040
1041 r = virtqueue_enable_cb_prepare(rq->vq);
1042 clear_bit(NAPI_STATE_SCHED, &napi->state);
1043 if (unlikely(virtqueue_poll(rq->vq, r)) &&
1044 napi_schedule_prep(napi)) {
1045 virtqueue_disable_cb(rq->vq);
1046 if (received < budget) {
1047 budget -= received;
1048 goto again;
1049 } else {
1050 __napi_schedule(napi);
1051 }
1052 }
1053
1054 return received;
1055}
1056#endif /* CONFIG_NET_RX_BUSY_POLL */
1057
Jason Wang986a4f42012-12-07 07:04:56 +00001058static int virtnet_open(struct net_device *dev)
1059{
1060 struct virtnet_info *vi = netdev_priv(dev);
1061 int i;
1062
Jason Wange4166622013-05-21 20:03:58 +00001063 for (i = 0; i < vi->max_queue_pairs; i++) {
1064 if (i < vi->curr_queue_pairs)
1065 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001066 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001067 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +00001068 virtnet_napi_enable(&vi->rq[i]);
1069 }
1070
1071 return 0;
1072}
1073
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001074static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001075{
1076 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +10301077 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +00001078 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +00001079 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001080
Jason Wange9d74172012-12-07 07:04:55 +00001081 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001082 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001083
Eric Dumazet83a27052012-06-05 22:35:24 +00001084 u64_stats_update_begin(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001085 stats->tx_bytes += skb->len;
1086 stats->tx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +00001087 u64_stats_update_end(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001088
Eric Dumazeted79bab2009-10-14 14:36:43 +00001089 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001090 }
1091}
1092
Jason Wange9d74172012-12-07 07:04:55 +00001093static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001094{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001095 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001096 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001097 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +10301098 unsigned num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001099 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301100 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001101
Johannes Berge1749612008-10-27 15:59:26 -07001102 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301103
1104 can_push = vi->any_header_sg &&
1105 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1106 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1107 /* Even if we can, don't push here yet as this would skew
1108 * csum_start offset below. */
1109 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001110 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301111 else
1112 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001113
Mike Rapoporte858fae2016-06-08 16:09:21 +03001114 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Jason Wang6391a442017-01-20 14:32:42 +08001115 virtio_is_little_endian(vi->vdev), false))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001116 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001117
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001118 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001119 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001120
Jason Wang547c8902015-08-27 14:53:06 +08001121 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301122 if (can_push) {
1123 __skb_push(skb, hdr_len);
1124 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1125 /* Pull header back to avoid skew in tx bytes calculations. */
1126 __skb_pull(skb, hdr_len);
1127 } else {
1128 sg_set_buf(sq->sg, hdr, hdr_len);
1129 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1130 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301131 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001132}
1133
Stephen Hemminger424efe92009-08-31 19:50:51 +00001134static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001135{
1136 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001137 int qnum = skb_get_queue_mapping(skb);
1138 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301139 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001140 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1141 bool kick = !skb->xmit_more;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001142
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001143 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001144 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001145
Jacob Keller074c3582014-06-25 02:37:13 +00001146 /* timestamp packet in software */
1147 skb_tx_timestamp(skb);
1148
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001149 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001150 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001151
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301152 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001153 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301154 dev->stats.tx_fifo_errors++;
1155 if (net_ratelimit())
1156 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001157 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001158 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001159 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001160 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001161 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001162
Rusty Russell48925e32009-09-24 09:59:20 -06001163 /* Don't wait up for transmitted skbs to be freed. */
1164 skb_orphan(skb);
1165 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -05001166
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001167 /* If running out of space, stop queue to avoid getting packets that we
1168 * are then unable to transmit.
1169 * An alternative would be to force queuing layer to requeue the skb by
1170 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1171 * returned in a normal path of operation: it means that driver is not
1172 * maintaining the TX queue stop/start state properly, and causes
1173 * the stack to do a non-trivial amount of useless work.
1174 * Since most packets only take 1 or 2 ring slots, stopping the queue
1175 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001176 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001177 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001178 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001179 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001180 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001181 free_old_xmit_skbs(sq);
1182 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001183 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001184 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001185 }
1186 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001187 }
Rusty Russell48925e32009-09-24 09:59:20 -06001188
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001189 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001190 virtqueue_kick(sq->vq);
1191
Rusty Russell48925e32009-09-24 09:59:20 -06001192 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001193}
1194
Amos Kong40cbfc32013-01-21 01:17:21 +00001195/*
1196 * Send command via the control virtqueue and check status. Commands
1197 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001198 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001199 */
1200static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001201 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001202{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301203 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001204 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001205
1206 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301207 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001208
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001209 vi->ctrl_status = ~0;
1210 vi->ctrl_hdr.class = class;
1211 vi->ctrl_hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301212 /* Add header */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001213 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301214 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001215
Rusty Russellf7bc9592013-03-20 15:44:28 +10301216 if (out)
1217 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001218
Rusty Russellf7bc9592013-03-20 15:44:28 +10301219 /* Add return status. */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001220 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001221 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001222
stephen hemmingerd24bae32013-12-09 16:17:40 -08001223 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301224 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001225
Heinz Graalfs67975902013-10-29 09:40:02 +10301226 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001227 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001228
1229 /* Spin for a response, the kick causes an ioport write, trapping
1230 * into the hypervisor, so the request should be handled immediately.
1231 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301232 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1233 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001234 cpu_relax();
1235
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001236 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001237}
1238
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001239static int virtnet_set_mac_address(struct net_device *dev, void *p)
1240{
1241 struct virtnet_info *vi = netdev_priv(dev);
1242 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001243 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001244 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001245 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001246
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001247 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1248 if (!addr)
1249 return -ENOMEM;
1250 memcpy(addr, p, sizeof(*addr));
1251
1252 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001253 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001254 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001255
Amos Kong7e58d5a2013-01-21 01:17:23 +00001256 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1257 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1258 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001259 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001260 dev_warn(&vdev->dev,
1261 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001262 ret = -EINVAL;
1263 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001264 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001265 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1266 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301267 unsigned int i;
1268
1269 /* Naturally, this has an atomicity problem. */
1270 for (i = 0; i < dev->addr_len; i++)
1271 virtio_cwrite8(vdev,
1272 offsetof(struct virtio_net_config, mac) +
1273 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001274 }
1275
1276 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001277 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001278
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001279out:
1280 kfree(addr);
1281 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001282}
1283
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001284static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
1285 struct rtnl_link_stats64 *tot)
1286{
1287 struct virtnet_info *vi = netdev_priv(dev);
1288 int cpu;
1289 unsigned int start;
1290
1291 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +00001292 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001293 u64 tpackets, tbytes, rpackets, rbytes;
1294
1295 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001296 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001297 tpackets = stats->tx_packets;
1298 tbytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001299 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001300
1301 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001302 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001303 rpackets = stats->rx_packets;
1304 rbytes = stats->rx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001305 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001306
1307 tot->rx_packets += rpackets;
1308 tot->tx_packets += tpackets;
1309 tot->rx_bytes += rbytes;
1310 tot->tx_bytes += tbytes;
1311 }
1312
1313 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001314 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001315 tot->rx_dropped = dev->stats.rx_dropped;
1316 tot->rx_length_errors = dev->stats.rx_length_errors;
1317 tot->rx_frame_errors = dev->stats.rx_frame_errors;
1318
1319 return tot;
1320}
1321
Amit Shahda74e892008-02-29 16:24:50 +05301322#ifdef CONFIG_NET_POLL_CONTROLLER
1323static void virtnet_netpoll(struct net_device *dev)
1324{
1325 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001326 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301327
Jason Wang986a4f42012-12-07 07:04:56 +00001328 for (i = 0; i < vi->curr_queue_pairs; i++)
1329 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301330}
1331#endif
1332
Jason Wang586d17c2012-04-11 20:43:52 +00001333static void virtnet_ack_link_announce(struct virtnet_info *vi)
1334{
1335 rtnl_lock();
1336 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001337 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001338 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1339 rtnl_unlock();
1340}
1341
Jason Wang986a4f42012-12-07 07:04:56 +00001342static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1343{
1344 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001345 struct net_device *dev = vi->dev;
1346
1347 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1348 return 0;
1349
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001350 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1351 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001352
1353 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001354 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001355 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1356 queue_pairs);
1357 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301358 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001359 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001360 /* virtnet_open() will refill when device is going to up. */
1361 if (dev->flags & IFF_UP)
1362 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301363 }
Jason Wang986a4f42012-12-07 07:04:56 +00001364
1365 return 0;
1366}
1367
Rusty Russell296f96f2007-10-22 11:03:37 +10001368static int virtnet_close(struct net_device *dev)
1369{
1370 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001371 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001372
Rusty Russellb2baed62011-12-29 00:42:38 +00001373 /* Make sure refill_work doesn't re-enable napi! */
1374 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001375
1376 for (i = 0; i < vi->max_queue_pairs; i++)
1377 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001378
Rusty Russell296f96f2007-10-22 11:03:37 +10001379 return 0;
1380}
1381
Alex Williamson2af76982009-02-04 09:02:40 +00001382static void virtnet_set_rx_mode(struct net_device *dev)
1383{
1384 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001385 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001386 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001387 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001388 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001389 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001390 void *buf;
1391 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001392
stephen hemminger788a8b62013-12-09 16:18:45 -08001393 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001394 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1395 return;
1396
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001397 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1398 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001399
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001400 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
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_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001404 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001405 vi->ctrl_promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001406
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001407 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001408
1409 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001410 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001411 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001412 vi->ctrl_allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001413
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001414 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001415 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001416 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001417 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1418 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1419 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001420 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001421 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001422
Alex Williamson23e258e2009-05-01 17:27:56 +00001423 sg_init_table(sg, 2);
1424
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001425 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001426 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001427 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001428 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001429 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001430
1431 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001432 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001433
1434 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001435 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001436
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001437 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001438 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001439 netdev_for_each_mc_addr(ha, dev)
1440 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001441
1442 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001443 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001444
1445 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001446 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001447 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001448
1449 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001450}
1451
Patrick McHardy80d5c362013-04-19 02:04:28 +00001452static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1453 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001454{
1455 struct virtnet_info *vi = netdev_priv(dev);
1456 struct scatterlist sg;
1457
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001458 vi->ctrl_vid = vid;
1459 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001460
1461 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001462 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001463 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001464 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001465}
1466
Patrick McHardy80d5c362013-04-19 02:04:28 +00001467static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1468 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001469{
1470 struct virtnet_info *vi = netdev_priv(dev);
1471 struct scatterlist sg;
1472
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001473 vi->ctrl_vid = vid;
1474 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001475
1476 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001477 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001478 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001479 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001480}
1481
Wanlong Gao8898c212013-01-24 23:51:30 +00001482static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001483{
1484 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001485
1486 if (vi->affinity_hint_set) {
1487 for (i = 0; i < vi->max_queue_pairs; i++) {
1488 virtqueue_set_affinity(vi->rq[i].vq, -1);
1489 virtqueue_set_affinity(vi->sq[i].vq, -1);
1490 }
1491
1492 vi->affinity_hint_set = false;
1493 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001494}
1495
1496static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001497{
1498 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001499 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001500
1501 /* In multiqueue mode, when the number of cpu is equal to the number of
1502 * queue pairs, we let the queue pairs to be private to one cpu by
1503 * setting the affinity hint to eliminate the contention.
1504 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001505 if (vi->curr_queue_pairs == 1 ||
1506 vi->max_queue_pairs != num_online_cpus()) {
1507 virtnet_clean_affinity(vi, -1);
1508 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001509 }
1510
Wanlong Gao8898c212013-01-24 23:51:30 +00001511 i = 0;
1512 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001513 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1514 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001515 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001516 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001517 }
1518
Wanlong Gao8898c212013-01-24 23:51:30 +00001519 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001520}
1521
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001522static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001523{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001524 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1525 node);
1526 virtnet_set_affinity(vi);
1527 return 0;
1528}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001529
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001530static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1531{
1532 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1533 node_dead);
1534 virtnet_set_affinity(vi);
1535 return 0;
1536}
Jason Wang3ab098d2013-10-15 11:18:58 +08001537
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001538static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1539{
1540 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1541 node);
1542
1543 virtnet_clean_affinity(vi, cpu);
1544 return 0;
1545}
1546
1547static enum cpuhp_state virtionet_online;
1548
1549static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1550{
1551 int ret;
1552
1553 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1554 if (ret)
1555 return ret;
1556 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1557 &vi->node_dead);
1558 if (!ret)
1559 return ret;
1560 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1561 return ret;
1562}
1563
1564static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1565{
1566 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1567 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1568 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001569}
1570
Rick Jones8f9f4662011-10-19 08:10:59 +00001571static void virtnet_get_ringparam(struct net_device *dev,
1572 struct ethtool_ringparam *ring)
1573{
1574 struct virtnet_info *vi = netdev_priv(dev);
1575
Jason Wang986a4f42012-12-07 07:04:56 +00001576 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1577 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001578 ring->rx_pending = ring->rx_max_pending;
1579 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001580}
1581
Rick Jones66846042011-11-14 14:17:08 +00001582
1583static void virtnet_get_drvinfo(struct net_device *dev,
1584 struct ethtool_drvinfo *info)
1585{
1586 struct virtnet_info *vi = netdev_priv(dev);
1587 struct virtio_device *vdev = vi->vdev;
1588
1589 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1590 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1591 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1592
1593}
1594
Jason Wangd73bcd22012-12-07 07:04:57 +00001595/* TODO: Eliminate OOO packets during switching */
1596static int virtnet_set_channels(struct net_device *dev,
1597 struct ethtool_channels *channels)
1598{
1599 struct virtnet_info *vi = netdev_priv(dev);
1600 u16 queue_pairs = channels->combined_count;
1601 int err;
1602
1603 /* We don't support separate rx/tx channels.
1604 * We don't allow setting 'other' channels.
1605 */
1606 if (channels->rx_count || channels->tx_count || channels->other_count)
1607 return -EINVAL;
1608
Amos Kongc18e9cd2014-04-18 13:45:41 +08001609 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001610 return -EINVAL;
1611
John Fastabendf600b692016-12-15 12:13:24 -08001612 /* For now we don't support modifying channels while XDP is loaded
1613 * also when XDP is loaded all RX queues have XDP programs so we only
1614 * need to check a single RX queue.
1615 */
1616 if (vi->rq[0].xdp_prog)
1617 return -EINVAL;
1618
Wanlong Gao47be2472013-01-24 23:51:29 +00001619 get_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001620 err = virtnet_set_queues(vi, queue_pairs);
1621 if (!err) {
1622 netif_set_real_num_tx_queues(dev, queue_pairs);
1623 netif_set_real_num_rx_queues(dev, queue_pairs);
1624
Wanlong Gao8898c212013-01-24 23:51:30 +00001625 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001626 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001627 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001628
1629 return err;
1630}
1631
1632static void virtnet_get_channels(struct net_device *dev,
1633 struct ethtool_channels *channels)
1634{
1635 struct virtnet_info *vi = netdev_priv(dev);
1636
1637 channels->combined_count = vi->curr_queue_pairs;
1638 channels->max_combined = vi->max_queue_pairs;
1639 channels->max_other = 0;
1640 channels->rx_count = 0;
1641 channels->tx_count = 0;
1642 channels->other_count = 0;
1643}
1644
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001645/* Check if the user is trying to change anything besides speed/duplex */
1646static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1647{
1648 struct ethtool_cmd diff1 = *cmd;
1649 struct ethtool_cmd diff2 = {};
1650
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001651 /* cmd is always set so we need to clear it, validate the port type
1652 * and also without autonegotiation we can ignore advertising
1653 */
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001654 ethtool_cmd_speed_set(&diff1, 0);
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001655 diff2.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001656 diff1.advertising = 0;
1657 diff1.duplex = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001658 diff1.cmd = 0;
1659
1660 return !memcmp(&diff1, &diff2, sizeof(diff1));
1661}
1662
1663static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1664{
1665 struct virtnet_info *vi = netdev_priv(dev);
1666 u32 speed;
1667
1668 speed = ethtool_cmd_speed(cmd);
1669 /* don't allow custom speed and duplex */
1670 if (!ethtool_validate_speed(speed) ||
1671 !ethtool_validate_duplex(cmd->duplex) ||
1672 !virtnet_validate_ethtool_cmd(cmd))
1673 return -EINVAL;
1674 vi->speed = speed;
1675 vi->duplex = cmd->duplex;
1676
1677 return 0;
1678}
1679
1680static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1681{
1682 struct virtnet_info *vi = netdev_priv(dev);
1683
1684 ethtool_cmd_speed_set(cmd, vi->speed);
1685 cmd->duplex = vi->duplex;
1686 cmd->port = PORT_OTHER;
1687
1688 return 0;
1689}
1690
1691static void virtnet_init_settings(struct net_device *dev)
1692{
1693 struct virtnet_info *vi = netdev_priv(dev);
1694
1695 vi->speed = SPEED_UNKNOWN;
1696 vi->duplex = DUPLEX_UNKNOWN;
1697}
1698
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001699static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001700 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001701 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001702 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001703 .set_channels = virtnet_set_channels,
1704 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00001705 .get_ts_info = ethtool_op_get_ts_info,
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001706 .get_settings = virtnet_get_settings,
1707 .set_settings = virtnet_set_settings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001708};
1709
John Fastabendf600b692016-12-15 12:13:24 -08001710static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1711{
1712 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1713 struct virtnet_info *vi = netdev_priv(dev);
1714 struct bpf_prog *old_prog;
John Fastabend672aafd2016-12-15 12:13:49 -08001715 u16 xdp_qp = 0, curr_qp;
1716 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001717
Jakub Kicinski529ec6a2017-01-25 14:56:36 -08001718 if (prog && prog->xdp_adjust_head) {
1719 netdev_warn(dev, "Does not support bpf_xdp_adjust_head()\n");
1720 return -EOPNOTSUPP;
1721 }
1722
John Fastabendf600b692016-12-15 12:13:24 -08001723 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001724 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1725 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1726 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001727 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1728 return -EOPNOTSUPP;
1729 }
1730
1731 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1732 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1733 return -EINVAL;
1734 }
1735
1736 if (dev->mtu > max_sz) {
1737 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1738 return -EINVAL;
1739 }
1740
John Fastabend672aafd2016-12-15 12:13:49 -08001741 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1742 if (prog)
1743 xdp_qp = nr_cpu_ids;
1744
1745 /* XDP requires extra queues for XDP_TX */
1746 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1747 netdev_warn(dev, "request %i queues but max is %i\n",
1748 curr_qp + xdp_qp, vi->max_queue_pairs);
1749 return -ENOMEM;
1750 }
1751
1752 err = virtnet_set_queues(vi, curr_qp + xdp_qp);
1753 if (err) {
1754 dev_warn(&dev->dev, "XDP Device queue allocation failure.\n");
1755 return err;
1756 }
1757
John Fastabendf600b692016-12-15 12:13:24 -08001758 if (prog) {
1759 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
John Fastabend672aafd2016-12-15 12:13:49 -08001760 if (IS_ERR(prog)) {
1761 virtnet_set_queues(vi, curr_qp);
John Fastabendf600b692016-12-15 12:13:24 -08001762 return PTR_ERR(prog);
John Fastabend672aafd2016-12-15 12:13:49 -08001763 }
John Fastabendf600b692016-12-15 12:13:24 -08001764 }
1765
John Fastabend672aafd2016-12-15 12:13:49 -08001766 vi->xdp_queue_pairs = xdp_qp;
1767 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1768
John Fastabendf600b692016-12-15 12:13:24 -08001769 for (i = 0; i < vi->max_queue_pairs; i++) {
1770 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1771 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1772 if (old_prog)
1773 bpf_prog_put(old_prog);
1774 }
1775
1776 return 0;
1777}
1778
1779static bool virtnet_xdp_query(struct net_device *dev)
1780{
1781 struct virtnet_info *vi = netdev_priv(dev);
1782 int i;
1783
1784 for (i = 0; i < vi->max_queue_pairs; i++) {
1785 if (vi->rq[i].xdp_prog)
1786 return true;
1787 }
1788 return false;
1789}
1790
1791static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1792{
1793 switch (xdp->command) {
1794 case XDP_SETUP_PROG:
1795 return virtnet_xdp_set(dev, xdp->prog);
1796 case XDP_QUERY_PROG:
1797 xdp->prog_attached = virtnet_xdp_query(dev);
1798 return 0;
1799 default:
1800 return -EINVAL;
1801 }
1802}
1803
Stephen Hemminger76288b42009-01-06 10:44:22 -08001804static const struct net_device_ops virtnet_netdev = {
1805 .ndo_open = virtnet_open,
1806 .ndo_stop = virtnet_close,
1807 .ndo_start_xmit = start_xmit,
1808 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001809 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001810 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001811 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001812 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1813 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001814#ifdef CONFIG_NET_POLL_CONTROLLER
1815 .ndo_poll_controller = virtnet_netpoll,
1816#endif
Jason Wang91815632014-07-23 16:33:55 +08001817#ifdef CONFIG_NET_RX_BUSY_POLL
1818 .ndo_busy_poll = virtnet_busy_poll,
1819#endif
John Fastabendf600b692016-12-15 12:13:24 -08001820 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001821};
1822
Jason Wang586d17c2012-04-11 20:43:52 +00001823static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001824{
Jason Wang586d17c2012-04-11 20:43:52 +00001825 struct virtnet_info *vi =
1826 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001827 u16 v;
1828
Rusty Russell855e0c52013-10-14 18:11:51 +10301829 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1830 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301831 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001832
1833 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001834 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001835 virtnet_ack_link_announce(vi);
1836 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001837
1838 /* Ignore unknown (future) status bits */
1839 v &= VIRTIO_NET_S_LINK_UP;
1840
1841 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301842 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001843
1844 vi->status = v;
1845
1846 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1847 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001848 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001849 } else {
1850 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001851 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001852 }
1853}
1854
1855static void virtnet_config_changed(struct virtio_device *vdev)
1856{
1857 struct virtnet_info *vi = vdev->priv;
1858
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001859 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001860}
1861
Jason Wang986a4f42012-12-07 07:04:56 +00001862static void virtnet_free_queues(struct virtnet_info *vi)
1863{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001864 int i;
1865
Jason Wangab3971b2015-03-12 13:57:44 +08001866 for (i = 0; i < vi->max_queue_pairs; i++) {
1867 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001868 netif_napi_del(&vi->rq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08001869 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001870
Eric Dumazet963abe52016-11-15 22:24:12 -08001871 /* We called napi_hash_del() before netif_napi_del(),
1872 * we need to respect an RCU grace period before freeing vi->rq
1873 */
1874 synchronize_net();
1875
Jason Wang986a4f42012-12-07 07:04:56 +00001876 kfree(vi->rq);
1877 kfree(vi->sq);
1878}
1879
1880static void free_receive_bufs(struct virtnet_info *vi)
1881{
John Fastabendf600b692016-12-15 12:13:24 -08001882 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00001883 int i;
1884
John Fastabendf600b692016-12-15 12:13:24 -08001885 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00001886 for (i = 0; i < vi->max_queue_pairs; i++) {
1887 while (vi->rq[i].pages)
1888 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08001889
1890 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1891 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1892 if (old_prog)
1893 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00001894 }
John Fastabendf600b692016-12-15 12:13:24 -08001895 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001896}
1897
Michael Daltonfb518792014-01-16 22:23:26 -08001898static void free_receive_page_frags(struct virtnet_info *vi)
1899{
1900 int i;
1901 for (i = 0; i < vi->max_queue_pairs; i++)
1902 if (vi->rq[i].alloc_frag.page)
1903 put_page(vi->rq[i].alloc_frag.page);
1904}
1905
John Fastabendb68df012017-01-25 18:22:48 -08001906static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08001907{
John Fastabendb68df012017-01-25 18:22:48 -08001908 /* For small receive mode always use kfree_skb variants */
1909 if (!vi->mergeable_rx_bufs)
1910 return false;
1911
John Fastabend56434a02016-12-15 12:14:13 -08001912 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1913 return false;
1914 else if (q < vi->curr_queue_pairs)
1915 return true;
1916 else
1917 return false;
1918}
1919
Jason Wang986a4f42012-12-07 07:04:56 +00001920static void free_unused_bufs(struct virtnet_info *vi)
1921{
1922 void *buf;
1923 int i;
1924
1925 for (i = 0; i < vi->max_queue_pairs; i++) {
1926 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08001927 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08001928 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08001929 dev_kfree_skb(buf);
1930 else
1931 put_page(virt_to_head_page(buf));
1932 }
Jason Wang986a4f42012-12-07 07:04:56 +00001933 }
1934
1935 for (i = 0; i < vi->max_queue_pairs; i++) {
1936 struct virtqueue *vq = vi->rq[i].vq;
1937
1938 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08001939 if (vi->mergeable_rx_bufs) {
1940 unsigned long ctx = (unsigned long)buf;
1941 void *base = mergeable_ctx_to_buf_address(ctx);
1942 put_page(virt_to_head_page(base));
1943 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001944 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001945 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001946 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001947 }
Jason Wang986a4f42012-12-07 07:04:56 +00001948 }
Jason Wang986a4f42012-12-07 07:04:56 +00001949 }
1950}
1951
Jason Wange9d74172012-12-07 07:04:55 +00001952static void virtnet_del_vqs(struct virtnet_info *vi)
1953{
1954 struct virtio_device *vdev = vi->vdev;
1955
Wanlong Gao8898c212013-01-24 23:51:30 +00001956 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00001957
Jason Wange9d74172012-12-07 07:04:55 +00001958 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00001959
1960 virtnet_free_queues(vi);
1961}
1962
1963static int virtnet_find_vqs(struct virtnet_info *vi)
1964{
1965 vq_callback_t **callbacks;
1966 struct virtqueue **vqs;
1967 int ret = -ENOMEM;
1968 int i, total_vqs;
1969 const char **names;
1970
1971 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1972 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1973 * possible control vq.
1974 */
1975 total_vqs = vi->max_queue_pairs * 2 +
1976 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1977
1978 /* Allocate space for find_vqs parameters */
1979 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1980 if (!vqs)
1981 goto err_vq;
1982 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1983 if (!callbacks)
1984 goto err_callback;
1985 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1986 if (!names)
1987 goto err_names;
1988
1989 /* Parameters for control virtqueue, if any */
1990 if (vi->has_cvq) {
1991 callbacks[total_vqs - 1] = NULL;
1992 names[total_vqs - 1] = "control";
1993 }
1994
1995 /* Allocate/initialize parameters for send/receive virtqueues */
1996 for (i = 0; i < vi->max_queue_pairs; i++) {
1997 callbacks[rxq2vq(i)] = skb_recv_done;
1998 callbacks[txq2vq(i)] = skb_xmit_done;
1999 sprintf(vi->rq[i].name, "input.%d", i);
2000 sprintf(vi->sq[i].name, "output.%d", i);
2001 names[rxq2vq(i)] = vi->rq[i].name;
2002 names[txq2vq(i)] = vi->sq[i].name;
2003 }
2004
2005 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
2006 names);
2007 if (ret)
2008 goto err_find;
2009
2010 if (vi->has_cvq) {
2011 vi->cvq = vqs[total_vqs - 1];
2012 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002013 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002014 }
2015
2016 for (i = 0; i < vi->max_queue_pairs; i++) {
2017 vi->rq[i].vq = vqs[rxq2vq(i)];
2018 vi->sq[i].vq = vqs[txq2vq(i)];
2019 }
2020
2021 kfree(names);
2022 kfree(callbacks);
2023 kfree(vqs);
2024
2025 return 0;
2026
2027err_find:
2028 kfree(names);
2029err_names:
2030 kfree(callbacks);
2031err_callback:
2032 kfree(vqs);
2033err_vq:
2034 return ret;
2035}
2036
2037static int virtnet_alloc_queues(struct virtnet_info *vi)
2038{
2039 int i;
2040
2041 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2042 if (!vi->sq)
2043 goto err_sq;
2044 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002045 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002046 goto err_rq;
2047
2048 INIT_DELAYED_WORK(&vi->refill, refill_work);
2049 for (i = 0; i < vi->max_queue_pairs; i++) {
2050 vi->rq[i].pages = NULL;
2051 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2052 napi_weight);
2053
2054 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002055 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002056 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2057 }
2058
2059 return 0;
2060
2061err_rq:
2062 kfree(vi->sq);
2063err_sq:
2064 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002065}
2066
Amit Shah3f9c10b2011-12-22 16:58:31 +05302067static int init_vqs(struct virtnet_info *vi)
2068{
Jason Wang986a4f42012-12-07 07:04:56 +00002069 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302070
Jason Wang986a4f42012-12-07 07:04:56 +00002071 /* Allocate send & receive queues */
2072 ret = virtnet_alloc_queues(vi);
2073 if (ret)
2074 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302075
Jason Wang986a4f42012-12-07 07:04:56 +00002076 ret = virtnet_find_vqs(vi);
2077 if (ret)
2078 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302079
Wanlong Gao47be2472013-01-24 23:51:29 +00002080 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002081 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002082 put_online_cpus();
2083
Amit Shah3f9c10b2011-12-22 16:58:31 +05302084 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002085
2086err_free:
2087 virtnet_free_queues(vi);
2088err:
2089 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302090}
2091
Michael Daltonfbf28d72014-01-16 22:23:30 -08002092#ifdef CONFIG_SYSFS
2093static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2094 struct rx_queue_attribute *attribute, char *buf)
2095{
2096 struct virtnet_info *vi = netdev_priv(queue->dev);
2097 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002098 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002099
2100 BUG_ON(queue_index >= vi->max_queue_pairs);
2101 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2102 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2103}
2104
2105static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2106 __ATTR_RO(mergeable_rx_buffer_size);
2107
2108static struct attribute *virtio_net_mrg_rx_attrs[] = {
2109 &mergeable_rx_buffer_size_attribute.attr,
2110 NULL
2111};
2112
2113static const struct attribute_group virtio_net_mrg_rx_group = {
2114 .name = "virtio_net",
2115 .attrs = virtio_net_mrg_rx_attrs
2116};
2117#endif
2118
Jason Wang892d6eb2014-11-20 17:03:05 +08002119static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2120 unsigned int fbit,
2121 const char *fname, const char *dname)
2122{
2123 if (!virtio_has_feature(vdev, fbit))
2124 return false;
2125
2126 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2127 fname, dname);
2128
2129 return true;
2130}
2131
2132#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2133 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2134
2135static bool virtnet_validate_features(struct virtio_device *vdev)
2136{
2137 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2138 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2139 "VIRTIO_NET_F_CTRL_VQ") ||
2140 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2141 "VIRTIO_NET_F_CTRL_VQ") ||
2142 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2143 "VIRTIO_NET_F_CTRL_VQ") ||
2144 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2145 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2146 "VIRTIO_NET_F_CTRL_VQ"))) {
2147 return false;
2148 }
2149
2150 return true;
2151}
2152
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002153#define MIN_MTU ETH_MIN_MTU
2154#define MAX_MTU ETH_MAX_MTU
2155
Rusty Russell296f96f2007-10-22 11:03:37 +10002156static int virtnet_probe(struct virtio_device *vdev)
2157{
Jason Wang986a4f42012-12-07 07:04:56 +00002158 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10002159 struct net_device *dev;
2160 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00002161 u16 max_queue_pairs;
Aaron Conole14de9d12016-06-03 16:57:12 -04002162 int mtu;
Jason Wang986a4f42012-12-07 07:04:56 +00002163
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002164 if (!vdev->config->get) {
2165 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2166 __func__);
2167 return -EINVAL;
2168 }
2169
Jason Wang892d6eb2014-11-20 17:03:05 +08002170 if (!virtnet_validate_features(vdev))
2171 return -EINVAL;
2172
Jason Wang986a4f42012-12-07 07:04:56 +00002173 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302174 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2175 struct virtio_net_config,
2176 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002177
2178 /* We need at least 2 queue's */
2179 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2180 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2181 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2182 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002183
2184 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002185 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002186 if (!dev)
2187 return -ENOMEM;
2188
2189 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002190 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002191 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002192 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002193
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002194 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002195 SET_NETDEV_DEV(dev, &vdev->dev);
2196
2197 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002198 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002199 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002200 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002201 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002202 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002203
2204 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002205 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002206 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2207 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002208 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002209 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2210 dev->hw_features |= NETIF_F_TSO;
2211 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2212 dev->hw_features |= NETIF_F_TSO6;
2213 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2214 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002215 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2216 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002217
Jason Wang41f2f122014-12-24 11:03:52 +08002218 dev->features |= NETIF_F_GSO_ROBUST;
2219
Michał Mirosław98e778c2011-03-31 01:01:35 +00002220 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002221 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002222 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002223 }
Thomas Huth4f491292013-08-27 17:09:02 +02002224 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2225 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002226
Jason Wang4fda8302013-04-10 23:32:21 +00002227 dev->vlan_features = dev->features;
2228
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002229 /* MTU range: 68 - 65535 */
2230 dev->min_mtu = MIN_MTU;
2231 dev->max_mtu = MAX_MTU;
2232
Rusty Russell296f96f2007-10-22 11:03:37 +10002233 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302234 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2235 virtio_cread_bytes(vdev,
2236 offsetof(struct virtio_net_config, mac),
2237 dev->dev_addr, dev->addr_len);
2238 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002239 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002240
2241 /* Set up our device-specific information */
2242 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002243 vi->dev = dev;
2244 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002245 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002246 vi->stats = alloc_percpu(struct virtnet_stats);
2247 err = -ENOMEM;
2248 if (vi->stats == NULL)
2249 goto free;
2250
John Stultz827da442013-10-07 15:51:58 -07002251 for_each_possible_cpu(i) {
2252 struct virtnet_stats *virtnet_stats;
2253 virtnet_stats = per_cpu_ptr(vi->stats, i);
2254 u64_stats_init(&virtnet_stats->tx_syncp);
2255 u64_stats_init(&virtnet_stats->rx_syncp);
2256 }
2257
Jason Wang586d17c2012-04-11 20:43:52 +00002258 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002259
Herbert Xu97402b92008-04-18 11:24:27 +08002260 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002261 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2262 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002263 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2264 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002265 vi->big_packets = true;
2266
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002267 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2268 vi->mergeable_rx_bufs = true;
2269
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002270 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2271 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002272 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2273 else
2274 vi->hdr_len = sizeof(struct virtio_net_hdr);
2275
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002276 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2277 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302278 vi->any_header_sg = true;
2279
Jason Wang986a4f42012-12-07 07:04:56 +00002280 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2281 vi->has_cvq = true;
2282
Aaron Conole14de9d12016-06-03 16:57:12 -04002283 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2284 mtu = virtio_cread16(vdev,
2285 offsetof(struct virtio_net_config,
2286 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002287 if (mtu < dev->min_mtu) {
Aaron Conole14de9d12016-06-03 16:57:12 -04002288 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
Aaron Conole93a205e2016-10-25 16:12:12 -04002289 } else {
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002290 dev->mtu = mtu;
Aaron Conole93a205e2016-10-25 16:12:12 -04002291 dev->max_mtu = mtu;
2292 }
Aaron Conole14de9d12016-06-03 16:57:12 -04002293 }
2294
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002295 if (vi->any_header_sg)
2296 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002297
Jason Wang44900012016-11-25 12:37:26 +08002298 /* Enable multiqueue by default */
2299 if (num_online_cpus() >= max_queue_pairs)
2300 vi->curr_queue_pairs = max_queue_pairs;
2301 else
2302 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002303 vi->max_queue_pairs = max_queue_pairs;
2304
2305 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302306 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002307 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002308 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002309
Michael Daltonfbf28d72014-01-16 22:23:30 -08002310#ifdef CONFIG_SYSFS
2311 if (vi->mergeable_rx_bufs)
2312 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2313#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08002314 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2315 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002316
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002317 virtnet_init_settings(dev);
2318
Rusty Russell296f96f2007-10-22 11:03:37 +10002319 err = register_netdev(dev);
2320 if (err) {
2321 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002322 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002323 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002324
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302325 virtio_device_ready(vdev);
2326
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002327 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002328 if (err) {
2329 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002330 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002331 }
2332
Jason Wanga2208712016-12-13 14:23:05 +08002333 rtnl_lock();
2334 virtnet_set_queues(vi, vi->curr_queue_pairs);
2335 rtnl_unlock();
Jason Wang44900012016-11-25 12:37:26 +08002336
Jason Wang167c25e2010-11-10 14:45:41 +00002337 /* Assume link up if device can't report link status,
2338 otherwise get link status from config. */
2339 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2340 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002341 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002342 } else {
2343 vi->status = VIRTIO_NET_S_LINK_UP;
2344 netif_carrier_on(dev);
2345 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002346
Jason Wang986a4f42012-12-07 07:04:56 +00002347 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2348 dev->name, max_queue_pairs);
2349
Rusty Russell296f96f2007-10-22 11:03:37 +10002350 return 0;
2351
wangyunjianf00e35e2016-05-31 11:52:43 +08002352free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302353 vi->vdev->config->reset(vdev);
2354
Rusty Russellb3369c12008-02-04 23:50:02 -05002355 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002356free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002357 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002358 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002359 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002360free_stats:
2361 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002362free:
2363 free_netdev(dev);
2364 return err;
2365}
2366
Amit Shah04486ed2011-12-22 16:58:32 +05302367static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002368{
Amit Shah04486ed2011-12-22 16:58:32 +05302369 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002370
2371 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002372 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002373
Jason Wang986a4f42012-12-07 07:04:56 +00002374 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002375
Michael Daltonfb518792014-01-16 22:23:26 -08002376 free_receive_page_frags(vi);
2377
Jason Wang986a4f42012-12-07 07:04:56 +00002378 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302379}
2380
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002381static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302382{
2383 struct virtnet_info *vi = vdev->priv;
2384
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002385 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002386
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302387 /* Make sure no work handler is accessing the device. */
2388 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002389
Amit Shah04486ed2011-12-22 16:58:32 +05302390 unregister_netdev(vi->dev);
2391
2392 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002393
Krishna Kumar2e66f552011-07-20 03:56:02 +00002394 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002395 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002396}
2397
Aaron Lu89107002013-09-17 09:25:23 +09302398#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302399static int virtnet_freeze(struct virtio_device *vdev)
2400{
2401 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002402 int i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302403
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002404 virtnet_cpu_notif_remove(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002405
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302406 /* Make sure no work handler is accessing the device */
2407 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002408
Amit Shah0741bcb2011-12-22 16:58:33 +05302409 netif_device_detach(vi->dev);
2410 cancel_delayed_work_sync(&vi->refill);
2411
Jason Wang91815632014-07-23 16:33:55 +08002412 if (netif_running(vi->dev)) {
Jason Wangab3971b2015-03-12 13:57:44 +08002413 for (i = 0; i < vi->max_queue_pairs; i++)
Jason Wang986a4f42012-12-07 07:04:56 +00002414 napi_disable(&vi->rq[i].napi);
Jason Wang91815632014-07-23 16:33:55 +08002415 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302416
2417 remove_vq_common(vi);
2418
2419 return 0;
2420}
2421
2422static int virtnet_restore(struct virtio_device *vdev)
2423{
2424 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002425 int err, i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302426
2427 err = init_vqs(vi);
2428 if (err)
2429 return err;
2430
Michael S. Tsirkine53fbd12014-10-15 10:22:32 +10302431 virtio_device_ready(vdev);
2432
Jason Wang6cd4ce02013-12-30 11:34:40 +08002433 if (netif_running(vi->dev)) {
2434 for (i = 0; i < vi->curr_queue_pairs; i++)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03002435 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wang6cd4ce02013-12-30 11:34:40 +08002436 schedule_delayed_work(&vi->refill, 0);
2437
Jason Wang986a4f42012-12-07 07:04:56 +00002438 for (i = 0; i < vi->max_queue_pairs; i++)
2439 virtnet_napi_enable(&vi->rq[i]);
Jason Wang6cd4ce02013-12-30 11:34:40 +08002440 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302441
2442 netif_device_attach(vi->dev);
2443
Jason Wang35ed1592013-10-15 11:18:59 +08002444 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00002445 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang35ed1592013-10-15 11:18:59 +08002446 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002447
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002448 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002449 if (err)
2450 return err;
2451
Amit Shah0741bcb2011-12-22 16:58:33 +05302452 return 0;
2453}
2454#endif
2455
Rusty Russell296f96f2007-10-22 11:03:37 +10002456static struct virtio_device_id id_table[] = {
2457 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2458 { 0 },
2459};
2460
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002461#define VIRTNET_FEATURES \
2462 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2463 VIRTIO_NET_F_MAC, \
2464 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2465 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2466 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2467 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2468 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2469 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2470 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2471 VIRTIO_NET_F_MTU
2472
Rusty Russellc45a6812008-05-02 21:50:50 -05002473static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002474 VIRTNET_FEATURES,
2475};
2476
2477static unsigned int features_legacy[] = {
2478 VIRTNET_FEATURES,
2479 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302480 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002481};
2482
Uwe Kleine-König22402522009-11-05 01:32:44 -08002483static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002484 .feature_table = features,
2485 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002486 .feature_table_legacy = features_legacy,
2487 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002488 .driver.name = KBUILD_MODNAME,
2489 .driver.owner = THIS_MODULE,
2490 .id_table = id_table,
2491 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002492 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002493 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302494#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302495 .freeze = virtnet_freeze,
2496 .restore = virtnet_restore,
2497#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002498};
2499
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002500static __init int virtio_net_driver_init(void)
2501{
2502 int ret;
2503
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002504 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002505 virtnet_cpu_online,
2506 virtnet_cpu_down_prep);
2507 if (ret < 0)
2508 goto out;
2509 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002510 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002511 NULL, virtnet_cpu_dead);
2512 if (ret)
2513 goto err_dead;
2514
2515 ret = register_virtio_driver(&virtio_net_driver);
2516 if (ret)
2517 goto err_virtio;
2518 return 0;
2519err_virtio:
2520 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2521err_dead:
2522 cpuhp_remove_multi_state(virtionet_online);
2523out:
2524 return ret;
2525}
2526module_init(virtio_net_driver_init);
2527
2528static __exit void virtio_net_driver_exit(void)
2529{
2530 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2531 cpuhp_remove_multi_state(virtionet_online);
2532 unregister_virtio_driver(&virtio_net_driver);
2533}
2534module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002535
2536MODULE_DEVICE_TABLE(virtio, id_table);
2537MODULE_DESCRIPTION("Virtio network driver");
2538MODULE_LICENSE("GPL");