blob: 6e47e653a05d0485d03f66ea9118d1f5943cab17 [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Rusty Russell296f96f2007-10-22 11:03:37 +100017 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080021#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100022#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
John Fastabendf600b692016-12-15 12:13:24 -080025#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010026#include <linux/bpf_trace.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100027#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080028#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000030#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080031#include <linux/average.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100032
Amerigo Wangd34710e2013-05-09 19:50:51 +000033static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020034module_param(napi_weight, int, 0444);
35
Rusty Russelleb939922011-12-19 14:08:01 +000036static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050037module_param(csum, bool, 0444);
38module_param(gso, bool, 0444);
39
Rusty Russell296f96f2007-10-22 11:03:37 +100040/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080041#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080042#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100043
Jason Wangf6b10202017-02-21 16:46:28 +080044#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
45
John Fastabend2de2f7f2017-02-02 19:16:29 -080046/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
47#define VIRTIO_XDP_HEADROOM 256
48
Johannes Berg5377d7582015-08-19 09:48:40 +020049/* RX packet size EWMA. The average packet size is used to determine the packet
50 * buffer size when refilling RX rings. As the entire RX ring may be refilled
51 * at once, the weight is chosen so that the EWMA will be insensitive to short-
52 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080053 */
Johannes Bergeb1e0112017-02-15 09:49:26 +010054DECLARE_EWMA(pkt_len, 0, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080055
Rick Jones66846042011-11-14 14:17:08 +000056#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000057
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000058struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000059 struct u64_stats_sync tx_syncp;
60 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000061 u64 tx_bytes;
62 u64 tx_packets;
63
64 u64 rx_bytes;
65 u64 rx_packets;
66};
67
Jason Wange9d74172012-12-07 07:04:55 +000068/* Internal representation of a send virtqueue */
69struct send_queue {
70 /* Virtqueue associated with this send _queue */
71 struct virtqueue *vq;
72
73 /* TX: fragments + linear part + virtio header */
74 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000075
76 /* Name of the send queue: output.$index */
77 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000078};
79
80/* Internal representation of a receive virtqueue */
81struct receive_queue {
82 /* Virtqueue associated with this receive_queue */
83 struct virtqueue *vq;
84
Rusty Russell296f96f2007-10-22 11:03:37 +100085 struct napi_struct napi;
86
John Fastabendf600b692016-12-15 12:13:24 -080087 struct bpf_prog __rcu *xdp_prog;
88
Jason Wange9d74172012-12-07 07:04:55 +000089 /* Chain pages by the private ptr. */
90 struct page *pages;
91
Michael Daltonab7db912014-01-16 22:23:27 -080092 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +020093 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -080094
Michael Daltonfb518792014-01-16 22:23:26 -080095 /* Page frag for packet buffer allocation. */
96 struct page_frag alloc_frag;
97
Jason Wange9d74172012-12-07 07:04:55 +000098 /* RX: fragments + linear part + virtio header */
99 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000100
101 /* Name of this receive queue: input.$index */
102 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +0000103};
104
105struct virtnet_info {
106 struct virtio_device *vdev;
107 struct virtqueue *cvq;
108 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000109 struct send_queue *sq;
110 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000111 unsigned int status;
112
Jason Wang986a4f42012-12-07 07:04:56 +0000113 /* Max # of queue pairs supported by the device */
114 u16 max_queue_pairs;
115
116 /* # of queue pairs currently used by the driver */
117 u16 curr_queue_pairs;
118
John Fastabend672aafd2016-12-15 12:13:49 -0800119 /* # of XDP queue pairs currently used by the driver */
120 u16 xdp_queue_pairs;
121
Herbert Xu97402b92008-04-18 11:24:27 +0800122 /* I like... big packets and I cannot lie! */
123 bool big_packets;
124
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800125 /* Host will merge rx buffers for big packets (shake it! shake it!) */
126 bool mergeable_rx_bufs;
127
Jason Wang986a4f42012-12-07 07:04:56 +0000128 /* Has control virtqueue */
129 bool has_cvq;
130
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930131 /* Host can handle any s/g split between our header and packet data */
132 bool any_header_sg;
133
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300134 /* Packet virtio header size */
135 u8 hdr_len;
136
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000137 /* Active statistics */
138 struct virtnet_stats __percpu *stats;
139
Rusty Russell3161e452009-08-26 12:22:32 -0700140 /* Work struct for refilling if we run low on memory. */
141 struct delayed_work refill;
142
Jason Wang586d17c2012-04-11 20:43:52 +0000143 /* Work struct for config space updates */
144 struct work_struct config_work;
145
Jason Wang986a4f42012-12-07 07:04:56 +0000146 /* Does the affinity hint is set for virtqueues? */
147 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000148
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200149 /* CPU hotplug instances for online & dead */
150 struct hlist_node node;
151 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200152
153 /* Control VQ buffers: protected by the rtnl lock */
154 struct virtio_net_ctrl_hdr ctrl_hdr;
155 virtio_net_ctrl_ack ctrl_status;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700156 struct virtio_net_ctrl_mq ctrl_mq;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200157 u8 ctrl_promisc;
158 u8 ctrl_allmulti;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700159 u16 ctrl_vid;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100160
161 /* Ethtool settings */
162 u8 duplex;
163 u32 speed;
Rusty Russell296f96f2007-10-22 11:03:37 +1000164};
165
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000166struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300167 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000168 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300169 * hdr is in a separate sg buffer, and data sg buffer shares same page
170 * with this header sg. This padding makes next sg 16 byte aligned
171 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000172 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300173 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000174};
175
Jason Wang986a4f42012-12-07 07:04:56 +0000176/* Converting between virtqueue no. and kernel tx/rx queue no.
177 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
178 */
179static int vq2txq(struct virtqueue *vq)
180{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000181 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000182}
183
184static int txq2vq(int txq)
185{
186 return txq * 2 + 1;
187}
188
189static int vq2rxq(struct virtqueue *vq)
190{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000191 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000192}
193
194static int rxq2vq(int rxq)
195{
196 return rxq * 2;
197}
198
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300199static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000200{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300201 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000202}
203
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000204/*
205 * private is used to chain pages for big packets, put the whole
206 * most recent used list in the beginning for reuse
207 */
Jason Wange9d74172012-12-07 07:04:55 +0000208static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500209{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000210 struct page *end;
211
Jason Wange9d74172012-12-07 07:04:55 +0000212 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000213 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000214 end->private = (unsigned long)rq->pages;
215 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500216}
217
Jason Wange9d74172012-12-07 07:04:55 +0000218static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500219{
Jason Wange9d74172012-12-07 07:04:55 +0000220 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500221
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000222 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000223 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000224 /* clear private here, it is used to chain pages */
225 p->private = 0;
226 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500227 p = alloc_page(gfp_mask);
228 return p;
229}
230
Jason Wange9d74172012-12-07 07:04:55 +0000231static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000232{
Jason Wange9d74172012-12-07 07:04:55 +0000233 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000234
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500235 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000236 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000237
Rusty Russell363f1512008-06-08 20:51:55 +1000238 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000239 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000240}
241
Mike Waychison34646452012-01-04 12:52:32 +0000242/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300243static struct sk_buff *page_to_skb(struct virtnet_info *vi,
244 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700245 struct page *page, unsigned int offset,
246 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000247{
248 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300249 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700250 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000251 char *p;
252
Michael Dalton2613af02013-10-28 15:44:18 -0700253 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000254
255 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100256 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000257 if (unlikely(!skb))
258 return NULL;
259
260 hdr = skb_vnet_hdr(skb);
261
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300262 hdr_len = vi->hdr_len;
263 if (vi->mergeable_rx_bufs)
264 hdr_padded_len = sizeof *hdr;
265 else
Michael Dalton2613af02013-10-28 15:44:18 -0700266 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000267
268 memcpy(hdr, p, hdr_len);
269
270 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700271 offset += hdr_padded_len;
272 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000273
274 copy = len;
275 if (copy > skb_tailroom(skb))
276 copy = skb_tailroom(skb);
277 memcpy(skb_put(skb, copy), p, copy);
278
279 len -= copy;
280 offset += copy;
281
Michael Dalton2613af02013-10-28 15:44:18 -0700282 if (vi->mergeable_rx_bufs) {
283 if (len)
284 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
285 else
286 put_page(page);
287 return skb;
288 }
289
Sasha Levine878d782011-09-28 04:40:54 +0000290 /*
291 * Verify that we can indeed put this data into a skb.
292 * This is here to handle cases when the device erroneously
293 * tries to receive more than is possible. This is usually
294 * the case of a broken device.
295 */
296 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000297 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000298 dev_kfree_skb(skb);
299 return NULL;
300 }
Michael Dalton2613af02013-10-28 15:44:18 -0700301 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000302 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700303 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
304 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
305 frag_size, truesize);
306 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000307 page = (struct page *)page->private;
308 offset = 0;
309 }
310
311 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000312 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000313
314 return skb;
315}
316
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100317static bool virtnet_xdp_xmit(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800318 struct receive_queue *rq,
Jason Wangf6b10202017-02-21 16:46:28 +0800319 struct xdp_buff *xdp)
John Fastabend56434a02016-12-15 12:14:13 -0800320{
John Fastabend56434a02016-12-15 12:14:13 -0800321 struct virtio_net_hdr_mrg_rxbuf *hdr;
Jason Wangf6b10202017-02-21 16:46:28 +0800322 unsigned int len;
John Fastabend722d8282017-02-02 19:15:32 -0800323 struct send_queue *sq;
324 unsigned int qp;
John Fastabend56434a02016-12-15 12:14:13 -0800325 void *xdp_sent;
326 int err;
327
John Fastabend722d8282017-02-02 19:15:32 -0800328 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
329 sq = &vi->sq[qp];
330
John Fastabend56434a02016-12-15 12:14:13 -0800331 /* Free up any pending old buffers before queueing new ones. */
332 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Jason Wangf6b10202017-02-21 16:46:28 +0800333 struct page *sent_page = virt_to_head_page(xdp_sent);
Jason Wangbb91acc2016-12-23 22:37:32 +0800334
Jason Wangf6b10202017-02-21 16:46:28 +0800335 put_page(sent_page);
John Fastabend56434a02016-12-15 12:14:13 -0800336 }
337
Jason Wangf6b10202017-02-21 16:46:28 +0800338 xdp->data -= vi->hdr_len;
339 /* Zero header and leave csum up to XDP layers */
340 hdr = xdp->data;
341 memset(hdr, 0, vi->hdr_len);
John Fastabend56434a02016-12-15 12:14:13 -0800342
Jason Wangf6b10202017-02-21 16:46:28 +0800343 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
Jason Wangbb91acc2016-12-23 22:37:32 +0800344
Jason Wangf6b10202017-02-21 16:46:28 +0800345 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC);
John Fastabend56434a02016-12-15 12:14:13 -0800346 if (unlikely(err)) {
Jason Wangf6b10202017-02-21 16:46:28 +0800347 struct page *page = virt_to_head_page(xdp->data);
Jason Wangbb91acc2016-12-23 22:37:32 +0800348
Jason Wangf6b10202017-02-21 16:46:28 +0800349 put_page(page);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100350 return false;
John Fastabend56434a02016-12-15 12:14:13 -0800351 }
352
353 virtqueue_kick(sq->vq);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100354 return true;
John Fastabend56434a02016-12-15 12:14:13 -0800355}
356
Jason Wangf6b10202017-02-21 16:46:28 +0800357static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
358{
359 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
360}
361
Jason Wangbb91acc2016-12-23 22:37:32 +0800362static struct sk_buff *receive_small(struct net_device *dev,
363 struct virtnet_info *vi,
364 struct receive_queue *rq,
365 void *buf, unsigned int len)
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200366{
Jason Wangf6b10202017-02-21 16:46:28 +0800367 struct sk_buff *skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800368 struct bpf_prog *xdp_prog;
Jason Wangf6b10202017-02-21 16:46:28 +0800369 unsigned int xdp_headroom = virtnet_get_headroom(vi);
370 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
371 unsigned int headroom = vi->hdr_len + header_offset;
372 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
373 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
374 unsigned int delta = 0;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300375 len -= vi->hdr_len;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200376
Jason Wangbb91acc2016-12-23 22:37:32 +0800377 rcu_read_lock();
378 xdp_prog = rcu_dereference(rq->xdp_prog);
379 if (xdp_prog) {
Jason Wangf6b10202017-02-21 16:46:28 +0800380 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
John Fastabend0354e4d2017-02-02 19:15:01 -0800381 struct xdp_buff xdp;
Jason Wangf6b10202017-02-21 16:46:28 +0800382 void *orig_data;
Jason Wangbb91acc2016-12-23 22:37:32 +0800383 u32 act;
384
385 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
386 goto err_xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800387
Jason Wangf6b10202017-02-21 16:46:28 +0800388 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
389 xdp.data = xdp.data_hard_start + xdp_headroom;
John Fastabend0354e4d2017-02-02 19:15:01 -0800390 xdp.data_end = xdp.data + len;
Jason Wangf6b10202017-02-21 16:46:28 +0800391 orig_data = xdp.data;
John Fastabend0354e4d2017-02-02 19:15:01 -0800392 act = bpf_prog_run_xdp(xdp_prog, &xdp);
393
Jason Wangbb91acc2016-12-23 22:37:32 +0800394 switch (act) {
395 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800396 /* Recalculate length in case bpf program changed it */
Jason Wangf6b10202017-02-21 16:46:28 +0800397 delta = orig_data - xdp.data;
Jason Wangbb91acc2016-12-23 22:37:32 +0800398 break;
399 case XDP_TX:
Jason Wangf6b10202017-02-21 16:46:28 +0800400 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
John Fastabend0354e4d2017-02-02 19:15:01 -0800401 trace_xdp_exception(vi->dev, xdp_prog, act);
Jason Wangbb91acc2016-12-23 22:37:32 +0800402 rcu_read_unlock();
403 goto xdp_xmit;
Jason Wangbb91acc2016-12-23 22:37:32 +0800404 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800405 bpf_warn_invalid_xdp_action(act);
406 case XDP_ABORTED:
407 trace_xdp_exception(vi->dev, xdp_prog, act);
408 case XDP_DROP:
Jason Wangbb91acc2016-12-23 22:37:32 +0800409 goto err_xdp;
410 }
411 }
412 rcu_read_unlock();
413
Jason Wangf6b10202017-02-21 16:46:28 +0800414 skb = build_skb(buf, buflen);
415 if (!skb) {
416 put_page(virt_to_head_page(buf));
417 goto err;
418 }
419 skb_reserve(skb, headroom - delta);
420 skb_put(skb, len + delta);
421 if (!delta) {
422 buf += header_offset;
423 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
424 } /* keep zeroed vnet hdr since packet was changed by bpf */
425
426err:
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200427 return skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800428
429err_xdp:
430 rcu_read_unlock();
431 dev->stats.rx_dropped++;
Jason Wangf6b10202017-02-21 16:46:28 +0800432 put_page(virt_to_head_page(buf));
Jason Wangbb91acc2016-12-23 22:37:32 +0800433xdp_xmit:
434 return NULL;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200435}
436
437static struct sk_buff *receive_big(struct net_device *dev,
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300438 struct virtnet_info *vi,
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200439 struct receive_queue *rq,
440 void *buf,
441 unsigned int len)
442{
443 struct page *page = buf;
Jason Wangc47a43d2016-12-23 22:37:31 +0800444 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200445
446 if (unlikely(!skb))
447 goto err;
448
449 return skb;
450
451err:
452 dev->stats.rx_dropped++;
453 give_pages(rq, page);
454 return NULL;
455}
456
John Fastabend72979a62016-12-15 12:14:36 -0800457/* The conditions to enable XDP should preclude the underlying device from
458 * sending packets across multiple buffers (num_buf > 1). However per spec
459 * it does not appear to be illegal to do so but rather just against convention.
460 * So in order to avoid making a system unresponsive the packets are pushed
461 * into a page and the XDP program is run. This will be extremely slow and we
462 * push a warning to the user to fix this as soon as possible. Fixing this may
463 * require resolving the underlying hardware to determine why multiple buffers
464 * are being received or simply loading the XDP program in the ingress stack
465 * after the skb is built because there is no advantage to running it here
466 * anymore.
467 */
468static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800469 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800470 struct page *p,
471 int offset,
472 unsigned int *len)
473{
474 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800475 unsigned int page_off = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800476
477 if (!page)
478 return NULL;
479
480 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
481 page_off += *len;
482
Jason Wang56a86f82016-12-23 22:37:26 +0800483 while (--*num_buf) {
John Fastabend72979a62016-12-15 12:14:36 -0800484 unsigned int buflen;
John Fastabend72979a62016-12-15 12:14:36 -0800485 void *buf;
486 int off;
487
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200488 buf = virtqueue_get_buf(rq->vq, &buflen);
489 if (unlikely(!buf))
John Fastabend72979a62016-12-15 12:14:36 -0800490 goto err_buf;
491
John Fastabend72979a62016-12-15 12:14:36 -0800492 p = virt_to_head_page(buf);
493 off = buf - page_address(p);
494
Jason Wang56a86f82016-12-23 22:37:26 +0800495 /* guard against a misconfigured or uncooperative backend that
496 * is sending packet larger than the MTU.
497 */
498 if ((page_off + buflen) > PAGE_SIZE) {
499 put_page(p);
500 goto err_buf;
501 }
502
John Fastabend72979a62016-12-15 12:14:36 -0800503 memcpy(page_address(page) + page_off,
504 page_address(p) + off, buflen);
505 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800506 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800507 }
508
John Fastabend2de2f7f2017-02-02 19:16:29 -0800509 /* Headroom does not contribute to packet length */
510 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800511 return page;
512err_buf:
513 __free_pages(page, 0);
514 return NULL;
515}
516
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200517static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200518 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200519 struct receive_queue *rq,
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200520 void *buf,
521 void *ctx,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200522 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000523{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300524 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
525 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200526 struct page *page = virt_to_head_page(buf);
527 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800528 struct sk_buff *head_skb, *curr_skb;
529 struct bpf_prog *xdp_prog;
530 unsigned int truesize;
Michael Daltonab7db912014-01-16 22:23:27 -0800531
John Fastabend56434a02016-12-15 12:14:13 -0800532 head_skb = NULL;
533
John Fastabendf600b692016-12-15 12:13:24 -0800534 rcu_read_lock();
535 xdp_prog = rcu_dereference(rq->xdp_prog);
536 if (xdp_prog) {
John Fastabend72979a62016-12-15 12:14:36 -0800537 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800538 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800539 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800540 u32 act;
541
Jason Wang73b62bd2016-12-23 22:37:24 +0800542 /* This happens when rx buffer size is underestimated */
John Fastabendf600b692016-12-15 12:13:24 -0800543 if (unlikely(num_buf > 1)) {
John Fastabend72979a62016-12-15 12:14:36 -0800544 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800545 xdp_page = xdp_linearize_page(rq, &num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800546 page, offset, &len);
547 if (!xdp_page)
548 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800549 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800550 } else {
551 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800552 }
553
554 /* Transient failure which in theory could occur if
555 * in-flight packets from before XDP was enabled reach
556 * the receive path after XDP is loaded. In practice I
557 * was not able to create this condition.
558 */
Jason Wangb00f70b2016-12-23 22:37:28 +0800559 if (unlikely(hdr->hdr.gso_type))
John Fastabendf600b692016-12-15 12:13:24 -0800560 goto err_xdp;
561
John Fastabend2de2f7f2017-02-02 19:16:29 -0800562 /* Allow consuming headroom but reserve enough space to push
563 * the descriptor on if we get an XDP_TX return code.
564 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800565 data = page_address(xdp_page) + offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800566 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
John Fastabend0354e4d2017-02-02 19:15:01 -0800567 xdp.data = data + vi->hdr_len;
568 xdp.data_end = xdp.data + (len - vi->hdr_len);
569 act = bpf_prog_run_xdp(xdp_prog, &xdp);
570
John Fastabend56434a02016-12-15 12:14:13 -0800571 switch (act) {
572 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800573 /* recalculate offset to account for any header
574 * adjustments. Note other cases do not build an
575 * skb and avoid using offset
576 */
577 offset = xdp.data -
578 page_address(xdp_page) - vi->hdr_len;
579
Jason Wang1830f892016-12-23 22:37:27 +0800580 /* We can only create skb based on xdp_page. */
581 if (unlikely(xdp_page != page)) {
582 rcu_read_unlock();
583 put_page(page);
584 head_skb = page_to_skb(vi, rq, xdp_page,
John Fastabend2de2f7f2017-02-02 19:16:29 -0800585 offset, len, PAGE_SIZE);
Jason Wang5c334742016-12-23 22:37:29 +0800586 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
Jason Wang1830f892016-12-23 22:37:27 +0800587 return head_skb;
588 }
John Fastabend56434a02016-12-15 12:14:13 -0800589 break;
590 case XDP_TX:
Jason Wangf6b10202017-02-21 16:46:28 +0800591 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
John Fastabend0354e4d2017-02-02 19:15:01 -0800592 trace_xdp_exception(vi->dev, xdp_prog, act);
Jason Wang5c334742016-12-23 22:37:29 +0800593 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabend72979a62016-12-15 12:14:36 -0800594 if (unlikely(xdp_page != page))
595 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800596 rcu_read_unlock();
597 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -0800598 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800599 bpf_warn_invalid_xdp_action(act);
600 case XDP_ABORTED:
601 trace_xdp_exception(vi->dev, xdp_prog, act);
602 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -0800603 if (unlikely(xdp_page != page))
604 __free_pages(xdp_page, 0);
Jason Wang5c334742016-12-23 22:37:29 +0800605 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabendf600b692016-12-15 12:13:24 -0800606 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800607 }
John Fastabendf600b692016-12-15 12:13:24 -0800608 }
609 rcu_read_unlock();
610
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200611 if (unlikely(len > (unsigned long)ctx)) {
612 pr_debug("%s: rx error: len %u exceeds truesize 0x%lu\n",
613 dev->name, len, (unsigned long)ctx);
614 dev->stats.rx_length_errors++;
615 goto err_skb;
616 }
617 truesize = (unsigned long)ctx;
John Fastabendf600b692016-12-15 12:13:24 -0800618 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
619 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000620
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200621 if (unlikely(!curr_skb))
622 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000623 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200624 int num_skb_frags;
625
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200626 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
Michael Daltonab7db912014-01-16 22:23:27 -0800627 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200628 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200629 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300630 virtio16_to_cpu(vi->vdev,
631 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200632 dev->stats.rx_length_errors++;
633 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000634 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200635
636 page = virt_to_head_page(buf);
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200637 if (unlikely(len > (unsigned long)ctx)) {
638 pr_debug("%s: rx error: len %u exceeds truesize 0x%lu\n",
639 dev->name, len, (unsigned long)ctx);
640 dev->stats.rx_length_errors++;
641 goto err_skb;
642 }
643 truesize = (unsigned long)ctx;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200644
645 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700646 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
647 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200648
649 if (unlikely(!nskb))
650 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700651 if (curr_skb == head_skb)
652 skb_shinfo(curr_skb)->frag_list = nskb;
653 else
654 curr_skb->next = nskb;
655 curr_skb = nskb;
656 head_skb->truesize += nskb->truesize;
657 num_skb_frags = 0;
658 }
659 if (curr_skb != head_skb) {
660 head_skb->data_len += len;
661 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800662 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700663 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200664 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800665 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
666 put_page(page);
667 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800668 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800669 } else {
670 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800671 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800672 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200673 }
674
Johannes Berg5377d7582015-08-19 09:48:40 +0200675 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200676 return head_skb;
677
John Fastabendf600b692016-12-15 12:13:24 -0800678err_xdp:
679 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200680err_skb:
681 put_page(page);
682 while (--num_buf) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200683 buf = virtqueue_get_buf(rq->vq, &len);
684 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200685 pr_debug("%s: rx error: %d buffers missing\n",
686 dev->name, num_buf);
687 dev->stats.rx_length_errors++;
688 break;
689 }
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200690 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200691 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000692 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200693err_buf:
694 dev->stats.rx_dropped++;
695 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800696xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200697 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000698}
699
Jason Wang61845d22017-02-17 11:33:09 +0800700static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200701 void *buf, unsigned int len, void **ctx)
Rusty Russell296f96f2007-10-22 11:03:37 +1000702{
Jason Wange9d74172012-12-07 07:04:55 +0000703 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000704 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300705 struct virtio_net_hdr_mrg_rxbuf *hdr;
Jason Wang61845d22017-02-17 11:33:09 +0800706 int ret;
Rusty Russell296f96f2007-10-22 11:03:37 +1000707
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300708 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000709 pr_debug("%s: short packet %i\n", dev->name, len);
710 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800711 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200712 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -0800713 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800714 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800715 } else {
Jason Wangf6b10202017-02-21 16:46:28 +0800716 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -0800717 }
Jason Wang61845d22017-02-17 11:33:09 +0800718 return 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000719 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000720
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200721 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200722 skb = receive_mergeable(dev, vi, rq, buf, ctx, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200723 else if (vi->big_packets)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300724 skb = receive_big(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200725 else
Jason Wangbb91acc2016-12-23 22:37:32 +0800726 skb = receive_small(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200727
728 if (unlikely(!skb))
Jason Wang61845d22017-02-17 11:33:09 +0800729 return 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800730
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000731 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000732
Jason Wang61845d22017-02-17 11:33:09 +0800733 ret = skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000734
Mike Rapoporte858fae2016-06-08 16:09:21 +0300735 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +0000736 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000737
Mike Rapoporte858fae2016-06-08 16:09:21 +0300738 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
739 virtio_is_little_endian(vi->vdev))) {
740 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
741 dev->name, hdr->hdr.gso_type,
742 hdr->hdr.gso_size);
743 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000744 }
745
Mike Rapoportd1dc06d2016-06-14 08:29:38 +0300746 skb->protocol = eth_type_trans(skb, dev);
747 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
748 ntohs(skb->protocol), skb->len, skb->pkt_type);
749
Eric Dumazet0fbd0502015-07-31 18:25:17 +0200750 napi_gro_receive(&rq->napi, skb);
Jason Wang61845d22017-02-17 11:33:09 +0800751 return ret;
Rusty Russell296f96f2007-10-22 11:03:37 +1000752
753frame_err:
754 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000755 dev_kfree_skb(skb);
Jason Wang61845d22017-02-17 11:33:09 +0800756 return 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000757}
758
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300759static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
760 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000761{
Jason Wangf6b10202017-02-21 16:46:28 +0800762 struct page_frag *alloc_frag = &rq->alloc_frag;
763 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800764 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wangf6b10202017-02-21 16:46:28 +0800765 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000766 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000767
Jason Wangf6b10202017-02-21 16:46:28 +0800768 len = SKB_DATA_ALIGN(len) +
769 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
770 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000771 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800772
Jason Wangf6b10202017-02-21 16:46:28 +0800773 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
774 get_page(alloc_frag->page);
775 alloc_frag->offset += len;
776 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
777 vi->hdr_len + GOOD_PACKET_LEN);
778 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000779 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +0800780 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000781
782 return err;
783}
784
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300785static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
786 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000787{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000788 struct page *first, *list = NULL;
789 char *p;
790 int i, err, offset;
791
Rusty Russella5835442014-09-11 10:17:36 +0930792 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
793
Jason Wange9d74172012-12-07 07:04:55 +0000794 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000795 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000796 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000797 if (!first) {
798 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000799 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000800 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700801 }
Jason Wange9d74172012-12-07 07:04:55 +0000802 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000803
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000804 /* chain new page in list head to match sg */
805 first->private = (unsigned long)list;
806 list = first;
807 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800808
Jason Wange9d74172012-12-07 07:04:55 +0000809 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000810 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000811 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000812 return -ENOMEM;
813 }
814 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800815
Jason Wange9d74172012-12-07 07:04:55 +0000816 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300817 /* a separated rq->sg[0] for header - required in case !any_header_sg */
818 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +0800819
Jason Wange9d74172012-12-07 07:04:55 +0000820 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000821 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000822 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800823
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000824 /* chain first in list head */
825 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030826 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
827 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000828 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000829 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800830
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000831 return err;
832}
Herbert Xu97402b92008-04-18 11:24:27 +0800833
Johannes Berg5377d7582015-08-19 09:48:40 +0200834static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000835{
Michael Daltonab7db912014-01-16 22:23:27 -0800836 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800837 unsigned int len;
838
Johannes Berg5377d7582015-08-19 09:48:40 +0200839 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael Daltonfbf28d72014-01-16 22:23:30 -0800840 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
Michael S. Tsirkine377fcc2017-03-06 22:21:35 +0200841 return ALIGN(len, L1_CACHE_BYTES);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800842}
843
John Fastabend2de2f7f2017-02-02 19:16:29 -0800844static int add_recvbuf_mergeable(struct virtnet_info *vi,
845 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -0800846{
Michael Daltonfb518792014-01-16 22:23:26 -0800847 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800848 unsigned int headroom = virtnet_get_headroom(vi);
Michael Daltonfb518792014-01-16 22:23:26 -0800849 char *buf;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200850 void *ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000851 int err;
Michael Daltonfb518792014-01-16 22:23:26 -0800852 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +1000853
Michael Daltonfbf28d72014-01-16 22:23:30 -0800854 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800855 if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000856 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -0800857
Michael Daltonfb518792014-01-16 22:23:26 -0800858 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800859 buf += headroom; /* advance address leaving hole at front of pkt */
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200860 ctx = (void *)(unsigned long)len;
Michael Daltonfb518792014-01-16 22:23:26 -0800861 get_page(alloc_frag->page);
John Fastabend2de2f7f2017-02-02 19:16:29 -0800862 alloc_frag->offset += len + headroom;
Michael Daltonfb518792014-01-16 22:23:26 -0800863 hole = alloc_frag->size - alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800864 if (hole < len + headroom) {
Michael Daltonab7db912014-01-16 22:23:27 -0800865 /* To avoid internal fragmentation, if there is very likely not
866 * enough space for another buffer, add the remaining space to
867 * the current buffer. This extra space is not included in
868 * the truesize stored in ctx.
869 */
Michael Daltonfb518792014-01-16 22:23:26 -0800870 len += hole;
871 alloc_frag->offset += hole;
872 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000873
Michael Daltonfb518792014-01-16 22:23:26 -0800874 sg_init_one(rq->sg, buf, len);
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200875 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000876 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700877 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000878
879 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000880}
881
Rusty Russellb2baed62011-12-29 00:42:38 +0000882/*
883 * Returns false if we couldn't fill entirely (OOM).
884 *
885 * Normally run in the receive path, but can also be run from ndo_open
886 * before we're receiving packets, or from refill_work which is
887 * careful to disable receiving (using napi_disable).
888 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300889static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
890 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800891{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800892 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000893 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800894
Michael Daltonfb518792014-01-16 22:23:26 -0800895 gfp |= __GFP_COLD;
Amit Shah0aea51c2009-08-26 14:58:28 +0530896 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000897 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -0800898 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000899 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300900 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000901 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300902 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800903
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000904 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030905 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800906 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800907 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +0800908 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700909 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800910}
911
Rusty Russell18445c42008-02-04 23:49:57 -0500912static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000913{
914 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000915 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000916
Rusty Russell18445c42008-02-04 23:49:57 -0500917 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000918 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300919 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000920 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500921 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000922}
923
Jason Wange9d74172012-12-07 07:04:55 +0000924static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800925{
Jason Wange9d74172012-12-07 07:04:55 +0000926 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800927
928 /* If all buffers were filled by other side before we napi_enabled, we
929 * won't get another interrupt, so process any outstanding packets
930 * now. virtnet_poll wants re-enable the queue, so we disable here.
931 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000932 if (napi_schedule_prep(&rq->napi)) {
933 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300934 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000935 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300936 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800937 }
938}
939
Rusty Russell3161e452009-08-26 12:22:32 -0700940static void refill_work(struct work_struct *work)
941{
Jason Wange9d74172012-12-07 07:04:55 +0000942 struct virtnet_info *vi =
943 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700944 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000945 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700946
Sasha Levin55257d72013-04-29 12:00:08 +0930947 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000948 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700949
Jason Wang986a4f42012-12-07 07:04:56 +0000950 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300951 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +0000952 virtnet_napi_enable(rq);
953
954 /* In theory, this can happen: if we don't get any buffers in
955 * we will *never* try to fill again.
956 */
957 if (still_empty)
958 schedule_delayed_work(&vi->refill, HZ/2);
959 }
Rusty Russell3161e452009-08-26 12:22:32 -0700960}
961
Jason Wang2ffa7592014-07-23 16:33:54 +0800962static int virtnet_receive(struct receive_queue *rq, int budget)
Rusty Russell296f96f2007-10-22 11:03:37 +1000963{
Jason Wange9d74172012-12-07 07:04:55 +0000964 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wang61845d22017-02-17 11:33:09 +0800965 unsigned int len, received = 0, bytes = 0;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000966 void *buf;
Jason Wang61845d22017-02-17 11:33:09 +0800967 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +1000968
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200969 if (vi->mergeable_rx_bufs) {
970 void *ctx;
971
972 while (received < budget &&
973 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
974 bytes += receive_buf(vi, rq, buf, len, ctx);
975 received++;
976 }
977 } else {
978 while (received < budget &&
979 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
980 bytes += receive_buf(vi, rq, buf, len, NULL);
981 received++;
982 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000983 }
984
Jason Wangbe121f42014-01-16 14:45:24 +0800985 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300986 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700987 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -0700988 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000989
Jason Wang61845d22017-02-17 11:33:09 +0800990 u64_stats_update_begin(&stats->rx_syncp);
991 stats->rx_bytes += bytes;
992 stats->rx_packets += received;
993 u64_stats_update_end(&stats->rx_syncp);
994
Jason Wang2ffa7592014-07-23 16:33:54 +0800995 return received;
996}
997
998static int virtnet_poll(struct napi_struct *napi, int budget)
999{
1000 struct receive_queue *rq =
1001 container_of(napi, struct receive_queue, napi);
Li RongQingfaadb052015-03-26 15:39:45 +08001002 unsigned int r, received;
Jason Wang2ffa7592014-07-23 16:33:54 +08001003
Li RongQingfaadb052015-03-26 15:39:45 +08001004 received = virtnet_receive(rq, budget);
Jason Wang2ffa7592014-07-23 16:33:54 +08001005
Rusty Russell8329d982007-11-19 11:20:43 -05001006 /* Out of packets? */
1007 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001008 r = virtqueue_enable_cb_prepare(rq->vq);
Eric Dumazet4d6308aa2017-02-04 07:49:21 -08001009 if (napi_complete_done(napi, received)) {
1010 if (unlikely(virtqueue_poll(rq->vq, r)) &&
1011 napi_schedule_prep(napi)) {
1012 virtqueue_disable_cb(rq->vq);
1013 __napi_schedule(napi);
1014 }
Christian Borntraeger4265f162008-03-14 14:17:05 +01001015 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001016 }
1017
1018 return received;
1019}
1020
Jason Wang986a4f42012-12-07 07:04:56 +00001021static int virtnet_open(struct net_device *dev)
1022{
1023 struct virtnet_info *vi = netdev_priv(dev);
1024 int i;
1025
Jason Wange4166622013-05-21 20:03:58 +00001026 for (i = 0; i < vi->max_queue_pairs; i++) {
1027 if (i < vi->curr_queue_pairs)
1028 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001029 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001030 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +00001031 virtnet_napi_enable(&vi->rq[i]);
1032 }
1033
1034 return 0;
1035}
1036
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001037static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001038{
1039 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +10301040 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +00001041 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +00001042 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Jason Wang61845d22017-02-17 11:33:09 +08001043 unsigned int packets = 0;
1044 unsigned int bytes = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +10001045
Jason Wange9d74172012-12-07 07:04:55 +00001046 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001047 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001048
Jason Wang61845d22017-02-17 11:33:09 +08001049 bytes += skb->len;
1050 packets++;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001051
Eric Dumazeted79bab2009-10-14 14:36:43 +00001052 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001053 }
Jason Wang61845d22017-02-17 11:33:09 +08001054
1055 /* Avoid overhead when no packets have been processed
1056 * happens when called speculatively from start_xmit.
1057 */
1058 if (!packets)
1059 return;
1060
1061 u64_stats_update_begin(&stats->tx_syncp);
1062 stats->tx_bytes += bytes;
1063 stats->tx_packets += packets;
1064 u64_stats_update_end(&stats->tx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +10001065}
1066
Jason Wange9d74172012-12-07 07:04:55 +00001067static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001068{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001069 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001070 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001071 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +10301072 unsigned num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001073 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301074 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001075
Johannes Berge1749612008-10-27 15:59:26 -07001076 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301077
1078 can_push = vi->any_header_sg &&
1079 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1080 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1081 /* Even if we can, don't push here yet as this would skew
1082 * csum_start offset below. */
1083 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001084 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301085 else
1086 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001087
Mike Rapoporte858fae2016-06-08 16:09:21 +03001088 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Jason Wang6391a442017-01-20 14:32:42 +08001089 virtio_is_little_endian(vi->vdev), false))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001090 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001091
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001092 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001093 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001094
Jason Wang547c8902015-08-27 14:53:06 +08001095 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301096 if (can_push) {
1097 __skb_push(skb, hdr_len);
1098 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1099 /* Pull header back to avoid skew in tx bytes calculations. */
1100 __skb_pull(skb, hdr_len);
1101 } else {
1102 sg_set_buf(sq->sg, hdr, hdr_len);
1103 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1104 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301105 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001106}
1107
Stephen Hemminger424efe92009-08-31 19:50:51 +00001108static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001109{
1110 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001111 int qnum = skb_get_queue_mapping(skb);
1112 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301113 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001114 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1115 bool kick = !skb->xmit_more;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001116
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001117 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001118 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001119
Jacob Keller074c3582014-06-25 02:37:13 +00001120 /* timestamp packet in software */
1121 skb_tx_timestamp(skb);
1122
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001123 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001124 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001125
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301126 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001127 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301128 dev->stats.tx_fifo_errors++;
1129 if (net_ratelimit())
1130 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001131 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001132 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001133 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001134 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001135 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001136
Rusty Russell48925e32009-09-24 09:59:20 -06001137 /* Don't wait up for transmitted skbs to be freed. */
1138 skb_orphan(skb);
1139 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -05001140
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001141 /* If running out of space, stop queue to avoid getting packets that we
1142 * are then unable to transmit.
1143 * An alternative would be to force queuing layer to requeue the skb by
1144 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1145 * returned in a normal path of operation: it means that driver is not
1146 * maintaining the TX queue stop/start state properly, and causes
1147 * the stack to do a non-trivial amount of useless work.
1148 * Since most packets only take 1 or 2 ring slots, stopping the queue
1149 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001150 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001151 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001152 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001153 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001154 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001155 free_old_xmit_skbs(sq);
1156 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001157 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001158 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001159 }
1160 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001161 }
Rusty Russell48925e32009-09-24 09:59:20 -06001162
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001163 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001164 virtqueue_kick(sq->vq);
1165
Rusty Russell48925e32009-09-24 09:59:20 -06001166 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001167}
1168
Amos Kong40cbfc32013-01-21 01:17:21 +00001169/*
1170 * Send command via the control virtqueue and check status. Commands
1171 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001172 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001173 */
1174static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001175 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001176{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301177 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001178 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001179
1180 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301181 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001182
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001183 vi->ctrl_status = ~0;
1184 vi->ctrl_hdr.class = class;
1185 vi->ctrl_hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301186 /* Add header */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001187 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301188 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001189
Rusty Russellf7bc9592013-03-20 15:44:28 +10301190 if (out)
1191 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001192
Rusty Russellf7bc9592013-03-20 15:44:28 +10301193 /* Add return status. */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001194 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001195 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001196
stephen hemmingerd24bae32013-12-09 16:17:40 -08001197 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301198 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001199
Heinz Graalfs67975902013-10-29 09:40:02 +10301200 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001201 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001202
1203 /* Spin for a response, the kick causes an ioport write, trapping
1204 * into the hypervisor, so the request should be handled immediately.
1205 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301206 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1207 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001208 cpu_relax();
1209
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001210 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001211}
1212
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001213static int virtnet_set_mac_address(struct net_device *dev, void *p)
1214{
1215 struct virtnet_info *vi = netdev_priv(dev);
1216 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001217 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001218 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001219 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001220
Shyam Saini801822d2016-12-24 00:44:58 +05301221 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001222 if (!addr)
1223 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001224
1225 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001226 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001227 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001228
Amos Kong7e58d5a2013-01-21 01:17:23 +00001229 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1230 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1231 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001232 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001233 dev_warn(&vdev->dev,
1234 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001235 ret = -EINVAL;
1236 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001237 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001238 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1239 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301240 unsigned int i;
1241
1242 /* Naturally, this has an atomicity problem. */
1243 for (i = 0; i < dev->addr_len; i++)
1244 virtio_cwrite8(vdev,
1245 offsetof(struct virtio_net_config, mac) +
1246 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001247 }
1248
1249 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001250 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001251
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001252out:
1253 kfree(addr);
1254 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001255}
1256
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001257static void virtnet_stats(struct net_device *dev,
1258 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001259{
1260 struct virtnet_info *vi = netdev_priv(dev);
1261 int cpu;
1262 unsigned int start;
1263
1264 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +00001265 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001266 u64 tpackets, tbytes, rpackets, rbytes;
1267
1268 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001269 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001270 tpackets = stats->tx_packets;
1271 tbytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001272 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001273
1274 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001275 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001276 rpackets = stats->rx_packets;
1277 rbytes = stats->rx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001278 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001279
1280 tot->rx_packets += rpackets;
1281 tot->tx_packets += tpackets;
1282 tot->rx_bytes += rbytes;
1283 tot->tx_bytes += tbytes;
1284 }
1285
1286 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001287 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001288 tot->rx_dropped = dev->stats.rx_dropped;
1289 tot->rx_length_errors = dev->stats.rx_length_errors;
1290 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001291}
1292
Amit Shahda74e892008-02-29 16:24:50 +05301293#ifdef CONFIG_NET_POLL_CONTROLLER
1294static void virtnet_netpoll(struct net_device *dev)
1295{
1296 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001297 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301298
Jason Wang986a4f42012-12-07 07:04:56 +00001299 for (i = 0; i < vi->curr_queue_pairs; i++)
1300 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301301}
1302#endif
1303
Jason Wang586d17c2012-04-11 20:43:52 +00001304static void virtnet_ack_link_announce(struct virtnet_info *vi)
1305{
1306 rtnl_lock();
1307 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001308 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001309 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1310 rtnl_unlock();
1311}
1312
John Fastabend473153292017-02-02 19:14:32 -08001313static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001314{
1315 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001316 struct net_device *dev = vi->dev;
1317
1318 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1319 return 0;
1320
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001321 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1322 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001323
1324 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001325 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001326 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1327 queue_pairs);
1328 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301329 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001330 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001331 /* virtnet_open() will refill when device is going to up. */
1332 if (dev->flags & IFF_UP)
1333 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301334 }
Jason Wang986a4f42012-12-07 07:04:56 +00001335
1336 return 0;
1337}
1338
John Fastabend473153292017-02-02 19:14:32 -08001339static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1340{
1341 int err;
1342
1343 rtnl_lock();
1344 err = _virtnet_set_queues(vi, queue_pairs);
1345 rtnl_unlock();
1346 return err;
1347}
1348
Rusty Russell296f96f2007-10-22 11:03:37 +10001349static int virtnet_close(struct net_device *dev)
1350{
1351 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001352 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001353
Rusty Russellb2baed62011-12-29 00:42:38 +00001354 /* Make sure refill_work doesn't re-enable napi! */
1355 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001356
1357 for (i = 0; i < vi->max_queue_pairs; i++)
1358 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001359
Rusty Russell296f96f2007-10-22 11:03:37 +10001360 return 0;
1361}
1362
Alex Williamson2af76982009-02-04 09:02:40 +00001363static void virtnet_set_rx_mode(struct net_device *dev)
1364{
1365 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001366 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001367 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001368 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001369 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001370 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001371 void *buf;
1372 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001373
stephen hemminger788a8b62013-12-09 16:18:45 -08001374 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001375 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1376 return;
1377
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001378 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1379 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001380
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001381 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001382
1383 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001384 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001385 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001386 vi->ctrl_promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001387
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001388 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001389
1390 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001391 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001392 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001393 vi->ctrl_allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001394
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001395 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001396 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001397 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001398 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1399 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1400 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001401 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001402 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001403
Alex Williamson23e258e2009-05-01 17:27:56 +00001404 sg_init_table(sg, 2);
1405
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001406 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001407 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001408 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001409 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001410 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001411
1412 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001413 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001414
1415 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001416 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001417
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001418 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001419 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001420 netdev_for_each_mc_addr(ha, dev)
1421 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001422
1423 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001424 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001425
1426 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001427 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001428 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001429
1430 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001431}
1432
Patrick McHardy80d5c362013-04-19 02:04:28 +00001433static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1434 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001435{
1436 struct virtnet_info *vi = netdev_priv(dev);
1437 struct scatterlist sg;
1438
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001439 vi->ctrl_vid = vid;
1440 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001441
1442 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001443 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001444 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001445 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001446}
1447
Patrick McHardy80d5c362013-04-19 02:04:28 +00001448static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1449 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001450{
1451 struct virtnet_info *vi = netdev_priv(dev);
1452 struct scatterlist sg;
1453
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001454 vi->ctrl_vid = vid;
1455 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001456
1457 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001458 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001459 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001460 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001461}
1462
Wanlong Gao8898c212013-01-24 23:51:30 +00001463static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001464{
1465 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001466
1467 if (vi->affinity_hint_set) {
1468 for (i = 0; i < vi->max_queue_pairs; i++) {
1469 virtqueue_set_affinity(vi->rq[i].vq, -1);
1470 virtqueue_set_affinity(vi->sq[i].vq, -1);
1471 }
1472
1473 vi->affinity_hint_set = false;
1474 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001475}
1476
1477static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001478{
1479 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001480 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001481
1482 /* In multiqueue mode, when the number of cpu is equal to the number of
1483 * queue pairs, we let the queue pairs to be private to one cpu by
1484 * setting the affinity hint to eliminate the contention.
1485 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001486 if (vi->curr_queue_pairs == 1 ||
1487 vi->max_queue_pairs != num_online_cpus()) {
1488 virtnet_clean_affinity(vi, -1);
1489 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001490 }
1491
Wanlong Gao8898c212013-01-24 23:51:30 +00001492 i = 0;
1493 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001494 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1495 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001496 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001497 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001498 }
1499
Wanlong Gao8898c212013-01-24 23:51:30 +00001500 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001501}
1502
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001503static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001504{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001505 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1506 node);
1507 virtnet_set_affinity(vi);
1508 return 0;
1509}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001510
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001511static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1512{
1513 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1514 node_dead);
1515 virtnet_set_affinity(vi);
1516 return 0;
1517}
Jason Wang3ab098d2013-10-15 11:18:58 +08001518
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001519static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1520{
1521 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1522 node);
1523
1524 virtnet_clean_affinity(vi, cpu);
1525 return 0;
1526}
1527
1528static enum cpuhp_state virtionet_online;
1529
1530static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1531{
1532 int ret;
1533
1534 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1535 if (ret)
1536 return ret;
1537 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1538 &vi->node_dead);
1539 if (!ret)
1540 return ret;
1541 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1542 return ret;
1543}
1544
1545static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1546{
1547 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1548 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1549 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001550}
1551
Rick Jones8f9f4662011-10-19 08:10:59 +00001552static void virtnet_get_ringparam(struct net_device *dev,
1553 struct ethtool_ringparam *ring)
1554{
1555 struct virtnet_info *vi = netdev_priv(dev);
1556
Jason Wang986a4f42012-12-07 07:04:56 +00001557 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1558 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001559 ring->rx_pending = ring->rx_max_pending;
1560 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001561}
1562
Rick Jones66846042011-11-14 14:17:08 +00001563
1564static void virtnet_get_drvinfo(struct net_device *dev,
1565 struct ethtool_drvinfo *info)
1566{
1567 struct virtnet_info *vi = netdev_priv(dev);
1568 struct virtio_device *vdev = vi->vdev;
1569
1570 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1571 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1572 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1573
1574}
1575
Jason Wangd73bcd22012-12-07 07:04:57 +00001576/* TODO: Eliminate OOO packets during switching */
1577static int virtnet_set_channels(struct net_device *dev,
1578 struct ethtool_channels *channels)
1579{
1580 struct virtnet_info *vi = netdev_priv(dev);
1581 u16 queue_pairs = channels->combined_count;
1582 int err;
1583
1584 /* We don't support separate rx/tx channels.
1585 * We don't allow setting 'other' channels.
1586 */
1587 if (channels->rx_count || channels->tx_count || channels->other_count)
1588 return -EINVAL;
1589
Amos Kongc18e9cd2014-04-18 13:45:41 +08001590 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001591 return -EINVAL;
1592
John Fastabendf600b692016-12-15 12:13:24 -08001593 /* For now we don't support modifying channels while XDP is loaded
1594 * also when XDP is loaded all RX queues have XDP programs so we only
1595 * need to check a single RX queue.
1596 */
1597 if (vi->rq[0].xdp_prog)
1598 return -EINVAL;
1599
Wanlong Gao47be2472013-01-24 23:51:29 +00001600 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08001601 err = _virtnet_set_queues(vi, queue_pairs);
Jason Wangd73bcd22012-12-07 07:04:57 +00001602 if (!err) {
1603 netif_set_real_num_tx_queues(dev, queue_pairs);
1604 netif_set_real_num_rx_queues(dev, queue_pairs);
1605
Wanlong Gao8898c212013-01-24 23:51:30 +00001606 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001607 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001608 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001609
1610 return err;
1611}
1612
1613static void virtnet_get_channels(struct net_device *dev,
1614 struct ethtool_channels *channels)
1615{
1616 struct virtnet_info *vi = netdev_priv(dev);
1617
1618 channels->combined_count = vi->curr_queue_pairs;
1619 channels->max_combined = vi->max_queue_pairs;
1620 channels->max_other = 0;
1621 channels->rx_count = 0;
1622 channels->tx_count = 0;
1623 channels->other_count = 0;
1624}
1625
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001626/* Check if the user is trying to change anything besides speed/duplex */
1627static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1628{
1629 struct ethtool_cmd diff1 = *cmd;
1630 struct ethtool_cmd diff2 = {};
1631
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001632 /* cmd is always set so we need to clear it, validate the port type
1633 * and also without autonegotiation we can ignore advertising
1634 */
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001635 ethtool_cmd_speed_set(&diff1, 0);
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001636 diff2.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001637 diff1.advertising = 0;
1638 diff1.duplex = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001639 diff1.cmd = 0;
1640
1641 return !memcmp(&diff1, &diff2, sizeof(diff1));
1642}
1643
1644static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1645{
1646 struct virtnet_info *vi = netdev_priv(dev);
1647 u32 speed;
1648
1649 speed = ethtool_cmd_speed(cmd);
1650 /* don't allow custom speed and duplex */
1651 if (!ethtool_validate_speed(speed) ||
1652 !ethtool_validate_duplex(cmd->duplex) ||
1653 !virtnet_validate_ethtool_cmd(cmd))
1654 return -EINVAL;
1655 vi->speed = speed;
1656 vi->duplex = cmd->duplex;
1657
1658 return 0;
1659}
1660
1661static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1662{
1663 struct virtnet_info *vi = netdev_priv(dev);
1664
1665 ethtool_cmd_speed_set(cmd, vi->speed);
1666 cmd->duplex = vi->duplex;
1667 cmd->port = PORT_OTHER;
1668
1669 return 0;
1670}
1671
1672static void virtnet_init_settings(struct net_device *dev)
1673{
1674 struct virtnet_info *vi = netdev_priv(dev);
1675
1676 vi->speed = SPEED_UNKNOWN;
1677 vi->duplex = DUPLEX_UNKNOWN;
1678}
1679
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001680static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001681 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001682 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001683 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001684 .set_channels = virtnet_set_channels,
1685 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00001686 .get_ts_info = ethtool_op_get_ts_info,
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001687 .get_settings = virtnet_get_settings,
1688 .set_settings = virtnet_set_settings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001689};
1690
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001691static void virtnet_freeze_down(struct virtio_device *vdev)
1692{
1693 struct virtnet_info *vi = vdev->priv;
1694 int i;
1695
1696 /* Make sure no work handler is accessing the device */
1697 flush_work(&vi->config_work);
1698
1699 netif_device_detach(vi->dev);
1700 cancel_delayed_work_sync(&vi->refill);
1701
1702 if (netif_running(vi->dev)) {
1703 for (i = 0; i < vi->max_queue_pairs; i++)
1704 napi_disable(&vi->rq[i].napi);
1705 }
1706}
1707
1708static int init_vqs(struct virtnet_info *vi);
John Fastabend2de2f7f2017-02-02 19:16:29 -08001709static void _remove_vq_common(struct virtnet_info *vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08001710
1711static int virtnet_restore_up(struct virtio_device *vdev)
1712{
1713 struct virtnet_info *vi = vdev->priv;
1714 int err, i;
1715
1716 err = init_vqs(vi);
1717 if (err)
1718 return err;
1719
1720 virtio_device_ready(vdev);
1721
1722 if (netif_running(vi->dev)) {
1723 for (i = 0; i < vi->curr_queue_pairs; i++)
1724 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1725 schedule_delayed_work(&vi->refill, 0);
1726
1727 for (i = 0; i < vi->max_queue_pairs; i++)
1728 virtnet_napi_enable(&vi->rq[i]);
1729 }
1730
1731 netif_device_attach(vi->dev);
1732 return err;
1733}
1734
Jason Wang017b29c2017-02-20 11:50:20 +08001735static int virtnet_reset(struct virtnet_info *vi, int curr_qp, int xdp_qp)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001736{
1737 struct virtio_device *dev = vi->vdev;
1738 int ret;
1739
1740 virtio_config_disable(dev);
1741 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
1742 virtnet_freeze_down(dev);
1743 _remove_vq_common(vi);
1744
1745 dev->config->reset(dev);
1746 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
1747 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
1748
1749 ret = virtio_finalize_features(dev);
1750 if (ret)
1751 goto err;
1752
Jason Wang017b29c2017-02-20 11:50:20 +08001753 vi->xdp_queue_pairs = xdp_qp;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001754 ret = virtnet_restore_up(dev);
1755 if (ret)
1756 goto err;
Jason Wang017b29c2017-02-20 11:50:20 +08001757 ret = _virtnet_set_queues(vi, curr_qp);
John Fastabend2de2f7f2017-02-02 19:16:29 -08001758 if (ret)
1759 goto err;
1760
1761 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
1762 virtio_config_enable(dev);
1763 return 0;
1764err:
1765 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
1766 return ret;
1767}
1768
John Fastabendf600b692016-12-15 12:13:24 -08001769static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1770{
1771 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1772 struct virtnet_info *vi = netdev_priv(dev);
1773 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08001774 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08001775 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001776
1777 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001778 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1779 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1780 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001781 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1782 return -EOPNOTSUPP;
1783 }
1784
1785 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1786 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1787 return -EINVAL;
1788 }
1789
1790 if (dev->mtu > max_sz) {
1791 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1792 return -EINVAL;
1793 }
1794
John Fastabend672aafd2016-12-15 12:13:49 -08001795 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1796 if (prog)
1797 xdp_qp = nr_cpu_ids;
1798
1799 /* XDP requires extra queues for XDP_TX */
1800 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1801 netdev_warn(dev, "request %i queues but max is %i\n",
1802 curr_qp + xdp_qp, vi->max_queue_pairs);
1803 return -ENOMEM;
1804 }
1805
John Fastabend2de2f7f2017-02-02 19:16:29 -08001806 if (prog) {
1807 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
1808 if (IS_ERR(prog))
1809 return PTR_ERR(prog);
1810 }
1811
John Fastabend2de2f7f2017-02-02 19:16:29 -08001812 /* Changing the headroom in buffers is a disruptive operation because
1813 * existing buffers must be flushed and reallocated. This will happen
1814 * when a xdp program is initially added or xdp is disabled by removing
1815 * the xdp program resulting in number of XDP queues changing.
1816 */
1817 if (vi->xdp_queue_pairs != xdp_qp) {
Jason Wang017b29c2017-02-20 11:50:20 +08001818 err = virtnet_reset(vi, curr_qp + xdp_qp, xdp_qp);
1819 if (err) {
1820 dev_warn(&dev->dev, "XDP reset failure.\n");
John Fastabend2de2f7f2017-02-02 19:16:29 -08001821 goto virtio_reset_err;
Jason Wang017b29c2017-02-20 11:50:20 +08001822 }
John Fastabendf600b692016-12-15 12:13:24 -08001823 }
1824
John Fastabend672aafd2016-12-15 12:13:49 -08001825 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1826
John Fastabendf600b692016-12-15 12:13:24 -08001827 for (i = 0; i < vi->max_queue_pairs; i++) {
1828 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1829 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1830 if (old_prog)
1831 bpf_prog_put(old_prog);
1832 }
1833
1834 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001835
1836virtio_reset_err:
1837 /* On reset error do our best to unwind XDP changes inflight and return
1838 * error up to user space for resolution. The underlying reset hung on
1839 * us so not much we can do here.
1840 */
John Fastabend2de2f7f2017-02-02 19:16:29 -08001841 if (prog)
1842 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
1843 return err;
John Fastabendf600b692016-12-15 12:13:24 -08001844}
1845
1846static bool virtnet_xdp_query(struct net_device *dev)
1847{
1848 struct virtnet_info *vi = netdev_priv(dev);
1849 int i;
1850
1851 for (i = 0; i < vi->max_queue_pairs; i++) {
1852 if (vi->rq[i].xdp_prog)
1853 return true;
1854 }
1855 return false;
1856}
1857
1858static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1859{
1860 switch (xdp->command) {
1861 case XDP_SETUP_PROG:
1862 return virtnet_xdp_set(dev, xdp->prog);
1863 case XDP_QUERY_PROG:
1864 xdp->prog_attached = virtnet_xdp_query(dev);
1865 return 0;
1866 default:
1867 return -EINVAL;
1868 }
1869}
1870
Stephen Hemminger76288b42009-01-06 10:44:22 -08001871static const struct net_device_ops virtnet_netdev = {
1872 .ndo_open = virtnet_open,
1873 .ndo_stop = virtnet_close,
1874 .ndo_start_xmit = start_xmit,
1875 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001876 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001877 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001878 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001879 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1880 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001881#ifdef CONFIG_NET_POLL_CONTROLLER
1882 .ndo_poll_controller = virtnet_netpoll,
1883#endif
John Fastabendf600b692016-12-15 12:13:24 -08001884 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001885};
1886
Jason Wang586d17c2012-04-11 20:43:52 +00001887static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001888{
Jason Wang586d17c2012-04-11 20:43:52 +00001889 struct virtnet_info *vi =
1890 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001891 u16 v;
1892
Rusty Russell855e0c52013-10-14 18:11:51 +10301893 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1894 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301895 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001896
1897 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001898 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001899 virtnet_ack_link_announce(vi);
1900 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001901
1902 /* Ignore unknown (future) status bits */
1903 v &= VIRTIO_NET_S_LINK_UP;
1904
1905 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301906 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001907
1908 vi->status = v;
1909
1910 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1911 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001912 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001913 } else {
1914 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001915 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001916 }
1917}
1918
1919static void virtnet_config_changed(struct virtio_device *vdev)
1920{
1921 struct virtnet_info *vi = vdev->priv;
1922
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001923 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001924}
1925
Jason Wang986a4f42012-12-07 07:04:56 +00001926static void virtnet_free_queues(struct virtnet_info *vi)
1927{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001928 int i;
1929
Jason Wangab3971b2015-03-12 13:57:44 +08001930 for (i = 0; i < vi->max_queue_pairs; i++) {
1931 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001932 netif_napi_del(&vi->rq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08001933 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001934
Eric Dumazet963abe52016-11-15 22:24:12 -08001935 /* We called napi_hash_del() before netif_napi_del(),
1936 * we need to respect an RCU grace period before freeing vi->rq
1937 */
1938 synchronize_net();
1939
Jason Wang986a4f42012-12-07 07:04:56 +00001940 kfree(vi->rq);
1941 kfree(vi->sq);
1942}
1943
John Fastabend473153292017-02-02 19:14:32 -08001944static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001945{
John Fastabendf600b692016-12-15 12:13:24 -08001946 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00001947 int i;
1948
1949 for (i = 0; i < vi->max_queue_pairs; i++) {
1950 while (vi->rq[i].pages)
1951 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08001952
1953 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1954 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1955 if (old_prog)
1956 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00001957 }
John Fastabend473153292017-02-02 19:14:32 -08001958}
1959
1960static void free_receive_bufs(struct virtnet_info *vi)
1961{
1962 rtnl_lock();
1963 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08001964 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001965}
1966
Michael Daltonfb518792014-01-16 22:23:26 -08001967static void free_receive_page_frags(struct virtnet_info *vi)
1968{
1969 int i;
1970 for (i = 0; i < vi->max_queue_pairs; i++)
1971 if (vi->rq[i].alloc_frag.page)
1972 put_page(vi->rq[i].alloc_frag.page);
1973}
1974
John Fastabendb68df012017-01-25 18:22:48 -08001975static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08001976{
1977 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1978 return false;
1979 else if (q < vi->curr_queue_pairs)
1980 return true;
1981 else
1982 return false;
1983}
1984
Jason Wang986a4f42012-12-07 07:04:56 +00001985static void free_unused_bufs(struct virtnet_info *vi)
1986{
1987 void *buf;
1988 int i;
1989
1990 for (i = 0; i < vi->max_queue_pairs; i++) {
1991 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08001992 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08001993 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08001994 dev_kfree_skb(buf);
1995 else
1996 put_page(virt_to_head_page(buf));
1997 }
Jason Wang986a4f42012-12-07 07:04:56 +00001998 }
1999
2000 for (i = 0; i < vi->max_queue_pairs; i++) {
2001 struct virtqueue *vq = vi->rq[i].vq;
2002
2003 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002004 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02002005 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002006 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002007 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002008 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002009 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002010 }
Jason Wang986a4f42012-12-07 07:04:56 +00002011 }
Jason Wang986a4f42012-12-07 07:04:56 +00002012 }
2013}
2014
Jason Wange9d74172012-12-07 07:04:55 +00002015static void virtnet_del_vqs(struct virtnet_info *vi)
2016{
2017 struct virtio_device *vdev = vi->vdev;
2018
Wanlong Gao8898c212013-01-24 23:51:30 +00002019 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00002020
Jason Wange9d74172012-12-07 07:04:55 +00002021 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002022
2023 virtnet_free_queues(vi);
2024}
2025
2026static int virtnet_find_vqs(struct virtnet_info *vi)
2027{
2028 vq_callback_t **callbacks;
2029 struct virtqueue **vqs;
2030 int ret = -ENOMEM;
2031 int i, total_vqs;
2032 const char **names;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002033 bool *ctx;
Jason Wang986a4f42012-12-07 07:04:56 +00002034
2035 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2036 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2037 * possible control vq.
2038 */
2039 total_vqs = vi->max_queue_pairs * 2 +
2040 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2041
2042 /* Allocate space for find_vqs parameters */
2043 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
2044 if (!vqs)
2045 goto err_vq;
2046 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
2047 if (!callbacks)
2048 goto err_callback;
2049 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
2050 if (!names)
2051 goto err_names;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002052 if (vi->mergeable_rx_bufs) {
2053 ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
2054 if (!ctx)
2055 goto err_ctx;
2056 } else {
2057 ctx = NULL;
2058 }
Jason Wang986a4f42012-12-07 07:04:56 +00002059
2060 /* Parameters for control virtqueue, if any */
2061 if (vi->has_cvq) {
2062 callbacks[total_vqs - 1] = NULL;
2063 names[total_vqs - 1] = "control";
2064 }
2065
2066 /* Allocate/initialize parameters for send/receive virtqueues */
2067 for (i = 0; i < vi->max_queue_pairs; i++) {
2068 callbacks[rxq2vq(i)] = skb_recv_done;
2069 callbacks[txq2vq(i)] = skb_xmit_done;
2070 sprintf(vi->rq[i].name, "input.%d", i);
2071 sprintf(vi->sq[i].name, "output.%d", i);
2072 names[rxq2vq(i)] = vi->rq[i].name;
2073 names[txq2vq(i)] = vi->sq[i].name;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002074 if (ctx)
2075 ctx[rxq2vq(i)] = true;
Jason Wang986a4f42012-12-07 07:04:56 +00002076 }
2077
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002078 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
2079 names, ctx, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002080 if (ret)
2081 goto err_find;
2082
2083 if (vi->has_cvq) {
2084 vi->cvq = vqs[total_vqs - 1];
2085 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002086 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002087 }
2088
2089 for (i = 0; i < vi->max_queue_pairs; i++) {
2090 vi->rq[i].vq = vqs[rxq2vq(i)];
2091 vi->sq[i].vq = vqs[txq2vq(i)];
2092 }
2093
2094 kfree(names);
2095 kfree(callbacks);
2096 kfree(vqs);
2097
2098 return 0;
2099
2100err_find:
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002101 kfree(ctx);
2102err_ctx:
Jason Wang986a4f42012-12-07 07:04:56 +00002103 kfree(names);
2104err_names:
2105 kfree(callbacks);
2106err_callback:
2107 kfree(vqs);
2108err_vq:
2109 return ret;
2110}
2111
2112static int virtnet_alloc_queues(struct virtnet_info *vi)
2113{
2114 int i;
2115
2116 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2117 if (!vi->sq)
2118 goto err_sq;
2119 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002120 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002121 goto err_rq;
2122
2123 INIT_DELAYED_WORK(&vi->refill, refill_work);
2124 for (i = 0; i < vi->max_queue_pairs; i++) {
2125 vi->rq[i].pages = NULL;
2126 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2127 napi_weight);
2128
2129 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002130 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002131 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2132 }
2133
2134 return 0;
2135
2136err_rq:
2137 kfree(vi->sq);
2138err_sq:
2139 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002140}
2141
Amit Shah3f9c10b2011-12-22 16:58:31 +05302142static int init_vqs(struct virtnet_info *vi)
2143{
Jason Wang986a4f42012-12-07 07:04:56 +00002144 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302145
Jason Wang986a4f42012-12-07 07:04:56 +00002146 /* Allocate send & receive queues */
2147 ret = virtnet_alloc_queues(vi);
2148 if (ret)
2149 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302150
Jason Wang986a4f42012-12-07 07:04:56 +00002151 ret = virtnet_find_vqs(vi);
2152 if (ret)
2153 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302154
Wanlong Gao47be2472013-01-24 23:51:29 +00002155 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002156 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002157 put_online_cpus();
2158
Amit Shah3f9c10b2011-12-22 16:58:31 +05302159 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002160
2161err_free:
2162 virtnet_free_queues(vi);
2163err:
2164 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302165}
2166
Michael Daltonfbf28d72014-01-16 22:23:30 -08002167#ifdef CONFIG_SYSFS
2168static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2169 struct rx_queue_attribute *attribute, char *buf)
2170{
2171 struct virtnet_info *vi = netdev_priv(queue->dev);
2172 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002173 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002174
2175 BUG_ON(queue_index >= vi->max_queue_pairs);
2176 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2177 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2178}
2179
2180static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2181 __ATTR_RO(mergeable_rx_buffer_size);
2182
2183static struct attribute *virtio_net_mrg_rx_attrs[] = {
2184 &mergeable_rx_buffer_size_attribute.attr,
2185 NULL
2186};
2187
2188static const struct attribute_group virtio_net_mrg_rx_group = {
2189 .name = "virtio_net",
2190 .attrs = virtio_net_mrg_rx_attrs
2191};
2192#endif
2193
Jason Wang892d6eb2014-11-20 17:03:05 +08002194static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2195 unsigned int fbit,
2196 const char *fname, const char *dname)
2197{
2198 if (!virtio_has_feature(vdev, fbit))
2199 return false;
2200
2201 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2202 fname, dname);
2203
2204 return true;
2205}
2206
2207#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2208 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2209
2210static bool virtnet_validate_features(struct virtio_device *vdev)
2211{
2212 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2213 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2214 "VIRTIO_NET_F_CTRL_VQ") ||
2215 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2216 "VIRTIO_NET_F_CTRL_VQ") ||
2217 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2218 "VIRTIO_NET_F_CTRL_VQ") ||
2219 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2220 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2221 "VIRTIO_NET_F_CTRL_VQ"))) {
2222 return false;
2223 }
2224
2225 return true;
2226}
2227
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002228#define MIN_MTU ETH_MIN_MTU
2229#define MAX_MTU ETH_MAX_MTU
2230
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002231static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10002232{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002233 if (!vdev->config->get) {
2234 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2235 __func__);
2236 return -EINVAL;
2237 }
2238
Jason Wang892d6eb2014-11-20 17:03:05 +08002239 if (!virtnet_validate_features(vdev))
2240 return -EINVAL;
2241
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002242 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2243 int mtu = virtio_cread16(vdev,
2244 offsetof(struct virtio_net_config,
2245 mtu));
2246 if (mtu < MIN_MTU)
2247 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2248 }
2249
2250 return 0;
2251}
2252
2253static int virtnet_probe(struct virtio_device *vdev)
2254{
2255 int i, err;
2256 struct net_device *dev;
2257 struct virtnet_info *vi;
2258 u16 max_queue_pairs;
2259 int mtu;
2260
Jason Wang986a4f42012-12-07 07:04:56 +00002261 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302262 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2263 struct virtio_net_config,
2264 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002265
2266 /* We need at least 2 queue's */
2267 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2268 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2269 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2270 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002271
2272 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002273 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002274 if (!dev)
2275 return -ENOMEM;
2276
2277 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002278 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002279 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002280 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002281
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002282 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002283 SET_NETDEV_DEV(dev, &vdev->dev);
2284
2285 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002286 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002287 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002288 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002289 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002290 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002291
2292 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002293 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002294 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2295 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002296 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002297 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2298 dev->hw_features |= NETIF_F_TSO;
2299 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2300 dev->hw_features |= NETIF_F_TSO6;
2301 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2302 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002303 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2304 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002305
Jason Wang41f2f122014-12-24 11:03:52 +08002306 dev->features |= NETIF_F_GSO_ROBUST;
2307
Michał Mirosław98e778c2011-03-31 01:01:35 +00002308 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002309 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002310 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002311 }
Thomas Huth4f491292013-08-27 17:09:02 +02002312 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2313 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002314
Jason Wang4fda8302013-04-10 23:32:21 +00002315 dev->vlan_features = dev->features;
2316
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002317 /* MTU range: 68 - 65535 */
2318 dev->min_mtu = MIN_MTU;
2319 dev->max_mtu = MAX_MTU;
2320
Rusty Russell296f96f2007-10-22 11:03:37 +10002321 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302322 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2323 virtio_cread_bytes(vdev,
2324 offsetof(struct virtio_net_config, mac),
2325 dev->dev_addr, dev->addr_len);
2326 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002327 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002328
2329 /* Set up our device-specific information */
2330 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002331 vi->dev = dev;
2332 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002333 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002334 vi->stats = alloc_percpu(struct virtnet_stats);
2335 err = -ENOMEM;
2336 if (vi->stats == NULL)
2337 goto free;
2338
John Stultz827da442013-10-07 15:51:58 -07002339 for_each_possible_cpu(i) {
2340 struct virtnet_stats *virtnet_stats;
2341 virtnet_stats = per_cpu_ptr(vi->stats, i);
2342 u64_stats_init(&virtnet_stats->tx_syncp);
2343 u64_stats_init(&virtnet_stats->rx_syncp);
2344 }
2345
Jason Wang586d17c2012-04-11 20:43:52 +00002346 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002347
Herbert Xu97402b92008-04-18 11:24:27 +08002348 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002349 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2350 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002351 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2352 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002353 vi->big_packets = true;
2354
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002355 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2356 vi->mergeable_rx_bufs = true;
2357
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002358 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2359 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002360 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2361 else
2362 vi->hdr_len = sizeof(struct virtio_net_hdr);
2363
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002364 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2365 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302366 vi->any_header_sg = true;
2367
Jason Wang986a4f42012-12-07 07:04:56 +00002368 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2369 vi->has_cvq = true;
2370
Aaron Conole14de9d12016-06-03 16:57:12 -04002371 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2372 mtu = virtio_cread16(vdev,
2373 offsetof(struct virtio_net_config,
2374 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002375 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002376 /* Should never trigger: MTU was previously validated
2377 * in virtnet_validate.
2378 */
2379 dev_err(&vdev->dev, "device MTU appears to have changed "
2380 "it is now %d < %d", mtu, dev->min_mtu);
2381 goto free_stats;
Aaron Conole93a205e2016-10-25 16:12:12 -04002382 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002383
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002384 dev->mtu = mtu;
2385 dev->max_mtu = mtu;
2386
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002387 /* TODO: size buffers correctly in this case. */
2388 if (dev->mtu > ETH_DATA_LEN)
2389 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04002390 }
2391
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002392 if (vi->any_header_sg)
2393 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002394
Jason Wang44900012016-11-25 12:37:26 +08002395 /* Enable multiqueue by default */
2396 if (num_online_cpus() >= max_queue_pairs)
2397 vi->curr_queue_pairs = max_queue_pairs;
2398 else
2399 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002400 vi->max_queue_pairs = max_queue_pairs;
2401
2402 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302403 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002404 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002405 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002406
Michael Daltonfbf28d72014-01-16 22:23:30 -08002407#ifdef CONFIG_SYSFS
2408 if (vi->mergeable_rx_bufs)
2409 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2410#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08002411 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2412 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002413
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002414 virtnet_init_settings(dev);
2415
Rusty Russell296f96f2007-10-22 11:03:37 +10002416 err = register_netdev(dev);
2417 if (err) {
2418 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002419 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002420 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002421
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302422 virtio_device_ready(vdev);
2423
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002424 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002425 if (err) {
2426 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002427 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002428 }
2429
Jason Wanga2208712016-12-13 14:23:05 +08002430 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08002431
Jason Wang167c25e2010-11-10 14:45:41 +00002432 /* Assume link up if device can't report link status,
2433 otherwise get link status from config. */
2434 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2435 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002436 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002437 } else {
2438 vi->status = VIRTIO_NET_S_LINK_UP;
2439 netif_carrier_on(dev);
2440 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002441
Jason Wang986a4f42012-12-07 07:04:56 +00002442 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2443 dev->name, max_queue_pairs);
2444
Rusty Russell296f96f2007-10-22 11:03:37 +10002445 return 0;
2446
wangyunjianf00e35e2016-05-31 11:52:43 +08002447free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302448 vi->vdev->config->reset(vdev);
2449
Rusty Russellb3369c12008-02-04 23:50:02 -05002450 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002451free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002452 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002453 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002454 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002455free_stats:
2456 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002457free:
2458 free_netdev(dev);
2459 return err;
2460}
2461
John Fastabend2de2f7f2017-02-02 19:16:29 -08002462static void _remove_vq_common(struct virtnet_info *vi)
2463{
2464 vi->vdev->config->reset(vi->vdev);
2465 free_unused_bufs(vi);
2466 _free_receive_bufs(vi);
2467 free_receive_page_frags(vi);
2468 virtnet_del_vqs(vi);
2469}
2470
Amit Shah04486ed2011-12-22 16:58:32 +05302471static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002472{
Amit Shah04486ed2011-12-22 16:58:32 +05302473 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002474
2475 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002476 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002477
Jason Wang986a4f42012-12-07 07:04:56 +00002478 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002479
Michael Daltonfb518792014-01-16 22:23:26 -08002480 free_receive_page_frags(vi);
2481
Jason Wang986a4f42012-12-07 07:04:56 +00002482 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302483}
2484
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002485static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302486{
2487 struct virtnet_info *vi = vdev->priv;
2488
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002489 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002490
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302491 /* Make sure no work handler is accessing the device. */
2492 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002493
Amit Shah04486ed2011-12-22 16:58:32 +05302494 unregister_netdev(vi->dev);
2495
2496 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002497
Krishna Kumar2e66f552011-07-20 03:56:02 +00002498 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002499 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002500}
2501
Aaron Lu89107002013-09-17 09:25:23 +09302502#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302503static int virtnet_freeze(struct virtio_device *vdev)
2504{
2505 struct virtnet_info *vi = vdev->priv;
2506
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002507 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002508 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05302509 remove_vq_common(vi);
2510
2511 return 0;
2512}
2513
2514static int virtnet_restore(struct virtio_device *vdev)
2515{
2516 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002517 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05302518
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002519 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05302520 if (err)
2521 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00002522 virtnet_set_queues(vi, vi->curr_queue_pairs);
2523
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002524 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002525 if (err)
2526 return err;
2527
Amit Shah0741bcb2011-12-22 16:58:33 +05302528 return 0;
2529}
2530#endif
2531
Rusty Russell296f96f2007-10-22 11:03:37 +10002532static struct virtio_device_id id_table[] = {
2533 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2534 { 0 },
2535};
2536
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002537#define VIRTNET_FEATURES \
2538 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2539 VIRTIO_NET_F_MAC, \
2540 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2541 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2542 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2543 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2544 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2545 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2546 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2547 VIRTIO_NET_F_MTU
2548
Rusty Russellc45a6812008-05-02 21:50:50 -05002549static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002550 VIRTNET_FEATURES,
2551};
2552
2553static unsigned int features_legacy[] = {
2554 VIRTNET_FEATURES,
2555 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302556 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002557};
2558
Uwe Kleine-König22402522009-11-05 01:32:44 -08002559static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002560 .feature_table = features,
2561 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002562 .feature_table_legacy = features_legacy,
2563 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002564 .driver.name = KBUILD_MODNAME,
2565 .driver.owner = THIS_MODULE,
2566 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002567 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10002568 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002569 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002570 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302571#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302572 .freeze = virtnet_freeze,
2573 .restore = virtnet_restore,
2574#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002575};
2576
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002577static __init int virtio_net_driver_init(void)
2578{
2579 int ret;
2580
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002581 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002582 virtnet_cpu_online,
2583 virtnet_cpu_down_prep);
2584 if (ret < 0)
2585 goto out;
2586 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002587 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002588 NULL, virtnet_cpu_dead);
2589 if (ret)
2590 goto err_dead;
2591
2592 ret = register_virtio_driver(&virtio_net_driver);
2593 if (ret)
2594 goto err_virtio;
2595 return 0;
2596err_virtio:
2597 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2598err_dead:
2599 cpuhp_remove_multi_state(virtionet_online);
2600out:
2601 return ret;
2602}
2603module_init(virtio_net_driver_init);
2604
2605static __exit void virtio_net_driver_exit(void)
2606{
2607 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2608 cpuhp_remove_multi_state(virtionet_online);
2609 unregister_virtio_driver(&virtio_net_driver);
2610}
2611module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002612
2613MODULE_DEVICE_TABLE(virtio, id_table);
2614MODULE_DESCRIPTION("Virtio network driver");
2615MODULE_LICENSE("GPL");