blob: 14f661cbae185436deb8ae573ca27d8f6d0d2d76 [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Rusty Russell296f96f2007-10-22 11:03:37 +100017 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080021#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100022#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
John Fastabendf600b692016-12-15 12:13:24 -080025#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010026#include <linux/bpf_trace.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100027#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080028#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000030#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080031#include <linux/average.h>
Jason Wang186b3c92017-09-19 17:42:43 +080032#include <linux/filter.h>
Sridhar Samudralaba5e4422018-05-24 09:55:17 -070033#include <linux/netdevice.h>
34#include <linux/pci.h>
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +020035#include <net/route.h>
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +010036#include <net/xdp.h>
Sridhar Samudralaba5e4422018-05-24 09:55:17 -070037#include <net/net_failover.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100038
Amerigo Wangd34710e2013-05-09 19:50:51 +000039static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020040module_param(napi_weight, int, 0444);
41
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040042static bool csum = true, gso = true, napi_tx;
Rusty Russell34a48572008-02-04 23:50:02 -050043module_param(csum, bool, 0444);
44module_param(gso, bool, 0444);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040045module_param(napi_tx, bool, 0644);
Rusty Russell34a48572008-02-04 23:50:02 -050046
Rusty Russell296f96f2007-10-22 11:03:37 +100047/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080048#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080049#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100050
Jason Wangf6b10202017-02-21 16:46:28 +080051#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
52
John Fastabend2de2f7f2017-02-02 19:16:29 -080053/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
54#define VIRTIO_XDP_HEADROOM 256
55
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +020056/* Separating two types of XDP xmit */
57#define VIRTIO_XDP_TX BIT(0)
58#define VIRTIO_XDP_REDIR BIT(1)
59
Johannes Berg5377d7582015-08-19 09:48:40 +020060/* RX packet size EWMA. The average packet size is used to determine the packet
61 * buffer size when refilling RX rings. As the entire RX ring may be refilled
62 * at once, the weight is chosen so that the EWMA will be insensitive to short-
63 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080064 */
Johannes Bergeb1e0112017-02-15 09:49:26 +010065DECLARE_EWMA(pkt_len, 0, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080066
Rick Jones66846042011-11-14 14:17:08 +000067#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000068
Colin Ian King7acd4322017-08-12 22:45:53 +010069static const unsigned long guest_offloads[] = {
70 VIRTIO_NET_F_GUEST_TSO4,
71 VIRTIO_NET_F_GUEST_TSO6,
72 VIRTIO_NET_F_GUEST_ECN,
73 VIRTIO_NET_F_GUEST_UFO
74};
Jason Wang3f935222017-07-19 16:54:49 +080075
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090076struct virtnet_stat_desc {
77 char desc[ETH_GSTRING_LEN];
78 size_t offset;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000079};
80
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090081struct virtnet_sq_stats {
82 struct u64_stats_sync syncp;
83 u64 packets;
84 u64 bytes;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090085 u64 xdp_tx;
86 u64 xdp_tx_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +090087 u64 kicks;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090088};
89
Jason Wangd46eeea2018-07-31 17:43:39 +080090struct virtnet_rq_stats {
91 struct u64_stats_sync syncp;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090092 u64 packets;
93 u64 bytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +090094 u64 drops;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090095 u64 xdp_packets;
96 u64 xdp_tx;
97 u64 xdp_redirects;
98 u64 xdp_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +090099 u64 kicks;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900100};
101
102#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
Jason Wangd46eeea2018-07-31 17:43:39 +0800103#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900104
105static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900106 { "packets", VIRTNET_SQ_STAT(packets) },
107 { "bytes", VIRTNET_SQ_STAT(bytes) },
108 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
109 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900110 { "kicks", VIRTNET_SQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900111};
112
113static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900114 { "packets", VIRTNET_RQ_STAT(packets) },
115 { "bytes", VIRTNET_RQ_STAT(bytes) },
116 { "drops", VIRTNET_RQ_STAT(drops) },
117 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
118 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
119 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
120 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900121 { "kicks", VIRTNET_RQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900122};
123
124#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
125#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
126
Jason Wange9d74172012-12-07 07:04:55 +0000127/* Internal representation of a send virtqueue */
128struct send_queue {
129 /* Virtqueue associated with this send _queue */
130 struct virtqueue *vq;
131
132 /* TX: fragments + linear part + virtio header */
133 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000134
135 /* Name of the send queue: output.$index */
136 char name[40];
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400137
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900138 struct virtnet_sq_stats stats;
139
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400140 struct napi_struct napi;
Jason Wange9d74172012-12-07 07:04:55 +0000141};
142
143/* Internal representation of a receive virtqueue */
144struct receive_queue {
145 /* Virtqueue associated with this receive_queue */
146 struct virtqueue *vq;
147
Rusty Russell296f96f2007-10-22 11:03:37 +1000148 struct napi_struct napi;
149
John Fastabendf600b692016-12-15 12:13:24 -0800150 struct bpf_prog __rcu *xdp_prog;
151
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900152 struct virtnet_rq_stats stats;
153
Jason Wange9d74172012-12-07 07:04:55 +0000154 /* Chain pages by the private ptr. */
155 struct page *pages;
156
Michael Daltonab7db912014-01-16 22:23:27 -0800157 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200158 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800159
Michael Daltonfb518792014-01-16 22:23:26 -0800160 /* Page frag for packet buffer allocation. */
161 struct page_frag alloc_frag;
162
Jason Wange9d74172012-12-07 07:04:55 +0000163 /* RX: fragments + linear part + virtio header */
164 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000165
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +0200166 /* Min single buffer size for mergeable buffers case. */
167 unsigned int min_buf_len;
168
Jason Wang986a4f42012-12-07 07:04:56 +0000169 /* Name of this receive queue: input.$index */
170 char name[40];
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100171
172 struct xdp_rxq_info xdp_rxq;
Jason Wange9d74172012-12-07 07:04:55 +0000173};
174
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300175/* Control VQ buffers: protected by the rtnl lock */
176struct control_buf {
177 struct virtio_net_ctrl_hdr hdr;
178 virtio_net_ctrl_ack status;
179 struct virtio_net_ctrl_mq mq;
180 u8 promisc;
181 u8 allmulti;
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +0300182 __virtio16 vid;
Michael S. Tsirkinf4ee7032018-04-19 08:30:50 +0300183 __virtio64 offloads;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300184};
185
Jason Wange9d74172012-12-07 07:04:55 +0000186struct virtnet_info {
187 struct virtio_device *vdev;
188 struct virtqueue *cvq;
189 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000190 struct send_queue *sq;
191 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000192 unsigned int status;
193
Jason Wang986a4f42012-12-07 07:04:56 +0000194 /* Max # of queue pairs supported by the device */
195 u16 max_queue_pairs;
196
197 /* # of queue pairs currently used by the driver */
198 u16 curr_queue_pairs;
199
John Fastabend672aafd2016-12-15 12:13:49 -0800200 /* # of XDP queue pairs currently used by the driver */
201 u16 xdp_queue_pairs;
202
Herbert Xu97402b92008-04-18 11:24:27 +0800203 /* I like... big packets and I cannot lie! */
204 bool big_packets;
205
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800206 /* Host will merge rx buffers for big packets (shake it! shake it!) */
207 bool mergeable_rx_bufs;
208
Jason Wang986a4f42012-12-07 07:04:56 +0000209 /* Has control virtqueue */
210 bool has_cvq;
211
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930212 /* Host can handle any s/g split between our header and packet data */
213 bool any_header_sg;
214
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300215 /* Packet virtio header size */
216 u8 hdr_len;
217
Rusty Russell3161e452009-08-26 12:22:32 -0700218 /* Work struct for refilling if we run low on memory. */
219 struct delayed_work refill;
220
Jason Wang586d17c2012-04-11 20:43:52 +0000221 /* Work struct for config space updates */
222 struct work_struct config_work;
223
Jason Wang986a4f42012-12-07 07:04:56 +0000224 /* Does the affinity hint is set for virtqueues? */
225 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000226
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200227 /* CPU hotplug instances for online & dead */
228 struct hlist_node node;
229 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200230
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300231 struct control_buf *ctrl;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100232
233 /* Ethtool settings */
234 u8 duplex;
235 u32 speed;
Jason Wang3f935222017-07-19 16:54:49 +0800236
237 unsigned long guest_offloads;
Sridhar Samudralaba5e4422018-05-24 09:55:17 -0700238
239 /* failover when STANDBY feature enabled */
240 struct failover *failover;
Rusty Russell296f96f2007-10-22 11:03:37 +1000241};
242
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000243struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300244 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000245 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300246 * hdr is in a separate sg buffer, and data sg buffer shares same page
247 * with this header sg. This padding makes next sg 16 byte aligned
248 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000249 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300250 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000251};
252
Jason Wang986a4f42012-12-07 07:04:56 +0000253/* Converting between virtqueue no. and kernel tx/rx queue no.
254 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
255 */
256static int vq2txq(struct virtqueue *vq)
257{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000258 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000259}
260
261static int txq2vq(int txq)
262{
263 return txq * 2 + 1;
264}
265
266static int vq2rxq(struct virtqueue *vq)
267{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000268 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000269}
270
271static int rxq2vq(int rxq)
272{
273 return rxq * 2;
274}
275
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300276static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000277{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300278 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000279}
280
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000281/*
282 * private is used to chain pages for big packets, put the whole
283 * most recent used list in the beginning for reuse
284 */
Jason Wange9d74172012-12-07 07:04:55 +0000285static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500286{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000287 struct page *end;
288
Jason Wange9d74172012-12-07 07:04:55 +0000289 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000290 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000291 end->private = (unsigned long)rq->pages;
292 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500293}
294
Jason Wange9d74172012-12-07 07:04:55 +0000295static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500296{
Jason Wange9d74172012-12-07 07:04:55 +0000297 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500298
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000299 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000300 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000301 /* clear private here, it is used to chain pages */
302 p->private = 0;
303 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500304 p = alloc_page(gfp_mask);
305 return p;
306}
307
Willem de Bruijne4e84522017-04-24 13:49:26 -0400308static void virtqueue_napi_schedule(struct napi_struct *napi,
309 struct virtqueue *vq)
310{
311 if (napi_schedule_prep(napi)) {
312 virtqueue_disable_cb(vq);
313 __napi_schedule(napi);
314 }
315}
316
317static void virtqueue_napi_complete(struct napi_struct *napi,
318 struct virtqueue *vq, int processed)
319{
320 int opaque;
321
322 opaque = virtqueue_enable_cb_prepare(vq);
Toshiaki Makitafdaa7672017-12-07 13:15:15 +0900323 if (napi_complete_done(napi, processed)) {
324 if (unlikely(virtqueue_poll(vq, opaque)))
325 virtqueue_napi_schedule(napi, vq);
326 } else {
327 virtqueue_disable_cb(vq);
328 }
Willem de Bruijne4e84522017-04-24 13:49:26 -0400329}
330
Jason Wange9d74172012-12-07 07:04:55 +0000331static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000332{
Jason Wange9d74172012-12-07 07:04:55 +0000333 struct virtnet_info *vi = vq->vdev->priv;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400334 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000335
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500336 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000337 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000338
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400339 if (napi->weight)
340 virtqueue_napi_schedule(napi, vq);
341 else
342 /* We were probably waiting for more output buffers. */
343 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000344}
345
Jason Wang28b39bc2017-07-19 16:54:46 +0800346#define MRG_CTX_HEADER_SHIFT 22
347static void *mergeable_len_to_ctx(unsigned int truesize,
348 unsigned int headroom)
349{
350 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
351}
352
353static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
354{
355 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
356}
357
358static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
359{
360 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
361}
362
Mike Waychison34646452012-01-04 12:52:32 +0000363/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300364static struct sk_buff *page_to_skb(struct virtnet_info *vi,
365 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700366 struct page *page, unsigned int offset,
367 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000368{
369 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300370 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700371 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000372 char *p;
373
Michael Dalton2613af02013-10-28 15:44:18 -0700374 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000375
376 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100377 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000378 if (unlikely(!skb))
379 return NULL;
380
381 hdr = skb_vnet_hdr(skb);
382
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300383 hdr_len = vi->hdr_len;
384 if (vi->mergeable_rx_bufs)
stephen hemmingera4a76502017-08-15 10:29:17 -0700385 hdr_padded_len = sizeof(*hdr);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300386 else
Michael Dalton2613af02013-10-28 15:44:18 -0700387 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000388
389 memcpy(hdr, p, hdr_len);
390
391 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700392 offset += hdr_padded_len;
393 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000394
395 copy = len;
396 if (copy > skb_tailroom(skb))
397 copy = skb_tailroom(skb);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200398 skb_put_data(skb, p, copy);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000399
400 len -= copy;
401 offset += copy;
402
Michael Dalton2613af02013-10-28 15:44:18 -0700403 if (vi->mergeable_rx_bufs) {
404 if (len)
405 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
406 else
407 put_page(page);
408 return skb;
409 }
410
Sasha Levine878d782011-09-28 04:40:54 +0000411 /*
412 * Verify that we can indeed put this data into a skb.
413 * This is here to handle cases when the device erroneously
414 * tries to receive more than is possible. This is usually
415 * the case of a broken device.
416 */
417 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000418 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000419 dev_kfree_skb(skb);
420 return NULL;
421 }
Michael Dalton2613af02013-10-28 15:44:18 -0700422 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000423 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700424 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
425 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
426 frag_size, truesize);
427 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000428 page = (struct page *)page->private;
429 offset = 0;
430 }
431
432 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000433 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000434
435 return skb;
436}
437
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200438static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
439 struct send_queue *sq,
440 struct xdp_frame *xdpf)
John Fastabend56434a02016-12-15 12:14:13 -0800441{
John Fastabend56434a02016-12-15 12:14:13 -0800442 struct virtio_net_hdr_mrg_rxbuf *hdr;
John Fastabend56434a02016-12-15 12:14:13 -0800443 int err;
444
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200445 /* virtqueue want to use data area in-front of packet */
446 if (unlikely(xdpf->metasize > 0))
447 return -EOPNOTSUPP;
448
449 if (unlikely(xdpf->headroom < vi->hdr_len))
450 return -EOVERFLOW;
451
452 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
453 xdpf->data -= vi->hdr_len;
Jason Wangf6b10202017-02-21 16:46:28 +0800454 /* Zero header and leave csum up to XDP layers */
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200455 hdr = xdpf->data;
Jason Wangf6b10202017-02-21 16:46:28 +0800456 memset(hdr, 0, vi->hdr_len);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200457 xdpf->len += vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800458
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200459 sg_init_one(sq->sg, xdpf->data, xdpf->len);
Jason Wangbb91acc2016-12-23 22:37:32 +0800460
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200461 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdpf, GFP_ATOMIC);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100462 if (unlikely(err))
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200463 return -ENOSPC; /* Caller handle free/refcnt */
John Fastabend56434a02016-12-15 12:14:13 -0800464
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200465 return 0;
John Fastabend56434a02016-12-15 12:14:13 -0800466}
467
Toshiaki Makita2a435652018-07-23 23:36:07 +0900468static struct send_queue *virtnet_xdp_sq(struct virtnet_info *vi)
469{
470 unsigned int qp;
471
472 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
473 return &vi->sq[qp];
474}
475
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200476static int virtnet_xdp_xmit(struct net_device *dev,
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +0200477 int n, struct xdp_frame **frames, u32 flags)
Jason Wang186b3c92017-09-19 17:42:43 +0800478{
479 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100480 struct receive_queue *rq = vi->rq;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200481 struct xdp_frame *xdpf_sent;
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100482 struct bpf_prog *xdp_prog;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200483 struct send_queue *sq;
484 unsigned int len;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200485 int drops = 0;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900486 int kicks = 0;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900487 int ret, err;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200488 int i;
489
Toshiaki Makita2a435652018-07-23 23:36:07 +0900490 sq = virtnet_xdp_sq(vi);
Jason Wang186b3c92017-09-19 17:42:43 +0800491
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900492 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
493 ret = -EINVAL;
494 drops = n;
495 goto out;
496 }
497
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100498 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
499 * indicate XDP resources have been successfully allocated.
500 */
501 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900502 if (!xdp_prog) {
503 ret = -ENXIO;
504 drops = n;
505 goto out;
506 }
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100507
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200508 /* Free up any pending old buffers before queueing new ones. */
509 while ((xdpf_sent = virtqueue_get_buf(sq->vq, &len)) != NULL)
510 xdp_return_frame(xdpf_sent);
511
512 for (i = 0; i < n; i++) {
513 struct xdp_frame *xdpf = frames[i];
514
515 err = __virtnet_xdp_xmit_one(vi, sq, xdpf);
516 if (err) {
517 xdp_return_frame_rx_napi(xdpf);
518 drops++;
519 }
520 }
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900521 ret = n - drops;
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200522
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900523 if (flags & XDP_XMIT_FLUSH) {
524 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
525 kicks = 1;
526 }
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900527out:
528 u64_stats_update_begin(&sq->stats.syncp);
529 sq->stats.xdp_tx += n;
530 sq->stats.xdp_tx_drops += drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900531 sq->stats.kicks += kicks;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900532 u64_stats_update_end(&sq->stats.syncp);
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200533
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900534 return ret;
Jason Wang186b3c92017-09-19 17:42:43 +0800535}
536
Jason Wangf6b10202017-02-21 16:46:28 +0800537static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
538{
539 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
540}
541
Jason Wang4941d472017-07-19 16:54:48 +0800542/* We copy the packet for XDP in the following cases:
543 *
544 * 1) Packet is scattered across multiple rx buffers.
545 * 2) Headroom space is insufficient.
546 *
547 * This is inefficient but it's a temporary condition that
548 * we hit right after XDP is enabled and until queue is refilled
549 * with large buffers with sufficient headroom - so it should affect
550 * at most queue size packets.
551 * Afterwards, the conditions to enable
552 * XDP should preclude the underlying device from sending packets
553 * across multiple buffers (num_buf > 1), and we make sure buffers
554 * have enough headroom.
John Fastabend72979a62016-12-15 12:14:36 -0800555 */
556static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800557 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800558 struct page *p,
559 int offset,
Jason Wang4941d472017-07-19 16:54:48 +0800560 int page_off,
John Fastabend72979a62016-12-15 12:14:36 -0800561 unsigned int *len)
562{
563 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend72979a62016-12-15 12:14:36 -0800564
565 if (!page)
566 return NULL;
567
568 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
569 page_off += *len;
570
Jason Wang56a86f82016-12-23 22:37:26 +0800571 while (--*num_buf) {
Jason Wang3cc81a92018-03-02 17:29:14 +0800572 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
John Fastabend72979a62016-12-15 12:14:36 -0800573 unsigned int buflen;
John Fastabend72979a62016-12-15 12:14:36 -0800574 void *buf;
575 int off;
576
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200577 buf = virtqueue_get_buf(rq->vq, &buflen);
578 if (unlikely(!buf))
John Fastabend72979a62016-12-15 12:14:36 -0800579 goto err_buf;
580
John Fastabend72979a62016-12-15 12:14:36 -0800581 p = virt_to_head_page(buf);
582 off = buf - page_address(p);
583
Jason Wang56a86f82016-12-23 22:37:26 +0800584 /* guard against a misconfigured or uncooperative backend that
585 * is sending packet larger than the MTU.
586 */
Jason Wang3cc81a92018-03-02 17:29:14 +0800587 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
Jason Wang56a86f82016-12-23 22:37:26 +0800588 put_page(p);
589 goto err_buf;
590 }
591
John Fastabend72979a62016-12-15 12:14:36 -0800592 memcpy(page_address(page) + page_off,
593 page_address(p) + off, buflen);
594 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800595 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800596 }
597
John Fastabend2de2f7f2017-02-02 19:16:29 -0800598 /* Headroom does not contribute to packet length */
599 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800600 return page;
601err_buf:
602 __free_pages(page, 0);
603 return NULL;
604}
605
Jason Wang4941d472017-07-19 16:54:48 +0800606static struct sk_buff *receive_small(struct net_device *dev,
607 struct virtnet_info *vi,
608 struct receive_queue *rq,
609 void *buf, void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800610 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900611 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800612 struct virtnet_rq_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800613{
614 struct sk_buff *skb;
615 struct bpf_prog *xdp_prog;
616 unsigned int xdp_headroom = (unsigned long)ctx;
617 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
618 unsigned int headroom = vi->hdr_len + header_offset;
619 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
620 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
621 struct page *page = virt_to_head_page(buf);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100622 unsigned int delta = 0;
Jason Wang4941d472017-07-19 16:54:48 +0800623 struct page *xdp_page;
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100624 int err;
625
Jason Wang4941d472017-07-19 16:54:48 +0800626 len -= vi->hdr_len;
Jason Wangd46eeea2018-07-31 17:43:39 +0800627 stats->bytes += len;
Jason Wang4941d472017-07-19 16:54:48 +0800628
629 rcu_read_lock();
630 xdp_prog = rcu_dereference(rq->xdp_prog);
631 if (xdp_prog) {
632 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200633 struct xdp_frame *xdpf;
Jason Wang4941d472017-07-19 16:54:48 +0800634 struct xdp_buff xdp;
635 void *orig_data;
636 u32 act;
637
Jesper Dangaard Brouer95dbe9e2018-02-20 14:32:10 +0100638 if (unlikely(hdr->hdr.gso_type))
Jason Wang4941d472017-07-19 16:54:48 +0800639 goto err_xdp;
640
641 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
642 int offset = buf - page_address(page) + header_offset;
643 unsigned int tlen = len + vi->hdr_len;
644 u16 num_buf = 1;
645
646 xdp_headroom = virtnet_get_headroom(vi);
647 header_offset = VIRTNET_RX_PAD + xdp_headroom;
648 headroom = vi->hdr_len + header_offset;
649 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
650 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
651 xdp_page = xdp_linearize_page(rq, &num_buf, page,
652 offset, header_offset,
653 &tlen);
654 if (!xdp_page)
655 goto err_xdp;
656
657 buf = page_address(xdp_page);
658 put_page(page);
659 page = xdp_page;
660 }
661
662 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
663 xdp.data = xdp.data_hard_start + xdp_headroom;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200664 xdp_set_data_meta_invalid(&xdp);
Jason Wang4941d472017-07-19 16:54:48 +0800665 xdp.data_end = xdp.data + len;
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100666 xdp.rxq = &rq->xdp_rxq;
Jason Wang4941d472017-07-19 16:54:48 +0800667 orig_data = xdp.data;
668 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Jason Wangd46eeea2018-07-31 17:43:39 +0800669 stats->xdp_packets++;
Jason Wang4941d472017-07-19 16:54:48 +0800670
671 switch (act) {
672 case XDP_PASS:
673 /* Recalculate length in case bpf program changed it */
674 delta = orig_data - xdp.data;
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700675 len = xdp.data_end - xdp.data;
Jason Wang4941d472017-07-19 16:54:48 +0800676 break;
677 case XDP_TX:
Jason Wangd46eeea2018-07-31 17:43:39 +0800678 stats->xdp_tx++;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200679 xdpf = convert_to_xdp_frame(&xdp);
680 if (unlikely(!xdpf))
681 goto err_xdp;
Jason Wangca9e83b2018-07-31 17:43:38 +0800682 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
683 if (unlikely(err < 0)) {
Jason Wang4941d472017-07-19 16:54:48 +0800684 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100685 goto err_xdp;
686 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200687 *xdp_xmit |= VIRTIO_XDP_TX;
Jason Wang186b3c92017-09-19 17:42:43 +0800688 rcu_read_unlock();
689 goto xdp_xmit;
690 case XDP_REDIRECT:
Jason Wangd46eeea2018-07-31 17:43:39 +0800691 stats->xdp_redirects++;
Jason Wang186b3c92017-09-19 17:42:43 +0800692 err = xdp_do_redirect(dev, &xdp, xdp_prog);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100693 if (err)
694 goto err_xdp;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200695 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang4941d472017-07-19 16:54:48 +0800696 rcu_read_unlock();
697 goto xdp_xmit;
698 default:
699 bpf_warn_invalid_xdp_action(act);
700 case XDP_ABORTED:
701 trace_xdp_exception(vi->dev, xdp_prog, act);
702 case XDP_DROP:
703 goto err_xdp;
704 }
705 }
706 rcu_read_unlock();
707
708 skb = build_skb(buf, buflen);
709 if (!skb) {
710 put_page(page);
711 goto err;
712 }
713 skb_reserve(skb, headroom - delta);
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700714 skb_put(skb, len);
Jason Wang4941d472017-07-19 16:54:48 +0800715 if (!delta) {
716 buf += header_offset;
717 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
718 } /* keep zeroed vnet hdr since packet was changed by bpf */
719
720err:
721 return skb;
722
723err_xdp:
724 rcu_read_unlock();
Jason Wangd46eeea2018-07-31 17:43:39 +0800725 stats->xdp_drops++;
726 stats->drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800727 put_page(page);
728xdp_xmit:
729 return NULL;
730}
731
732static struct sk_buff *receive_big(struct net_device *dev,
733 struct virtnet_info *vi,
734 struct receive_queue *rq,
735 void *buf,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900736 unsigned int len,
Jason Wangd46eeea2018-07-31 17:43:39 +0800737 struct virtnet_rq_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800738{
739 struct page *page = buf;
740 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
741
Jason Wangd46eeea2018-07-31 17:43:39 +0800742 stats->bytes += len - vi->hdr_len;
Jason Wang4941d472017-07-19 16:54:48 +0800743 if (unlikely(!skb))
744 goto err;
745
746 return skb;
747
748err:
Jason Wangd46eeea2018-07-31 17:43:39 +0800749 stats->drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800750 give_pages(rq, page);
751 return NULL;
752}
753
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200754static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200755 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200756 struct receive_queue *rq,
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200757 void *buf,
758 void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800759 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900760 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800761 struct virtnet_rq_stats *stats)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000762{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300763 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
764 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200765 struct page *page = virt_to_head_page(buf);
766 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800767 struct sk_buff *head_skb, *curr_skb;
768 struct bpf_prog *xdp_prog;
769 unsigned int truesize;
Jason Wang4941d472017-07-19 16:54:48 +0800770 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
Jason Wang3cc81a92018-03-02 17:29:14 +0800771 int err;
Michael Daltonab7db912014-01-16 22:23:27 -0800772
John Fastabend56434a02016-12-15 12:14:13 -0800773 head_skb = NULL;
Jason Wangd46eeea2018-07-31 17:43:39 +0800774 stats->bytes += len - vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800775
John Fastabendf600b692016-12-15 12:13:24 -0800776 rcu_read_lock();
777 xdp_prog = rcu_dereference(rq->xdp_prog);
778 if (xdp_prog) {
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200779 struct xdp_frame *xdpf;
John Fastabend72979a62016-12-15 12:14:36 -0800780 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800781 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800782 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800783 u32 act;
784
Jason Wang3d62b2a2018-05-22 11:44:31 +0800785 /* Transient failure which in theory could occur if
786 * in-flight packets from before XDP was enabled reach
787 * the receive path after XDP is loaded.
788 */
789 if (unlikely(hdr->hdr.gso_type))
790 goto err_xdp;
791
Jason Wang3cc81a92018-03-02 17:29:14 +0800792 /* This happens when rx buffer size is underestimated
793 * or headroom is not enough because of the buffer
794 * was refilled before XDP is set. This should only
795 * happen for the first several packets, so we don't
796 * care much about its performance.
797 */
Jason Wang4941d472017-07-19 16:54:48 +0800798 if (unlikely(num_buf > 1 ||
799 headroom < virtnet_get_headroom(vi))) {
John Fastabend72979a62016-12-15 12:14:36 -0800800 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800801 xdp_page = xdp_linearize_page(rq, &num_buf,
Jason Wang4941d472017-07-19 16:54:48 +0800802 page, offset,
803 VIRTIO_XDP_HEADROOM,
804 &len);
John Fastabend72979a62016-12-15 12:14:36 -0800805 if (!xdp_page)
806 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800807 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800808 } else {
809 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800810 }
811
John Fastabend2de2f7f2017-02-02 19:16:29 -0800812 /* Allow consuming headroom but reserve enough space to push
813 * the descriptor on if we get an XDP_TX return code.
814 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800815 data = page_address(xdp_page) + offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800816 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
John Fastabend0354e4d2017-02-02 19:15:01 -0800817 xdp.data = data + vi->hdr_len;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200818 xdp_set_data_meta_invalid(&xdp);
John Fastabend0354e4d2017-02-02 19:15:01 -0800819 xdp.data_end = xdp.data + (len - vi->hdr_len);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100820 xdp.rxq = &rq->xdp_rxq;
821
John Fastabend0354e4d2017-02-02 19:15:01 -0800822 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Jason Wangd46eeea2018-07-31 17:43:39 +0800823 stats->xdp_packets++;
John Fastabend0354e4d2017-02-02 19:15:01 -0800824
John Fastabend56434a02016-12-15 12:14:13 -0800825 switch (act) {
826 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800827 /* recalculate offset to account for any header
828 * adjustments. Note other cases do not build an
829 * skb and avoid using offset
830 */
831 offset = xdp.data -
832 page_address(xdp_page) - vi->hdr_len;
833
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700834 /* recalculate len if xdp.data or xdp.data_end were
835 * adjusted
836 */
Nikita V. Shirokovaaa64522018-04-22 21:16:48 -0700837 len = xdp.data_end - xdp.data + vi->hdr_len;
Jason Wang1830f892016-12-23 22:37:27 +0800838 /* We can only create skb based on xdp_page. */
839 if (unlikely(xdp_page != page)) {
840 rcu_read_unlock();
841 put_page(page);
842 head_skb = page_to_skb(vi, rq, xdp_page,
John Fastabend2de2f7f2017-02-02 19:16:29 -0800843 offset, len, PAGE_SIZE);
Jason Wang1830f892016-12-23 22:37:27 +0800844 return head_skb;
845 }
John Fastabend56434a02016-12-15 12:14:13 -0800846 break;
847 case XDP_TX:
Jason Wangd46eeea2018-07-31 17:43:39 +0800848 stats->xdp_tx++;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200849 xdpf = convert_to_xdp_frame(&xdp);
850 if (unlikely(!xdpf))
851 goto err_xdp;
Jason Wangca9e83b2018-07-31 17:43:38 +0800852 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
853 if (unlikely(err < 0)) {
John Fastabend0354e4d2017-02-02 19:15:01 -0800854 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100855 if (unlikely(xdp_page != page))
856 put_page(xdp_page);
857 goto err_xdp;
858 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200859 *xdp_xmit |= VIRTIO_XDP_TX;
John Fastabend72979a62016-12-15 12:14:36 -0800860 if (unlikely(xdp_page != page))
Jason Wang5d458a12018-05-22 11:44:29 +0800861 put_page(page);
John Fastabend56434a02016-12-15 12:14:13 -0800862 rcu_read_unlock();
863 goto xdp_xmit;
Jason Wang3cc81a92018-03-02 17:29:14 +0800864 case XDP_REDIRECT:
Jason Wangd46eeea2018-07-31 17:43:39 +0800865 stats->xdp_redirects++;
Jason Wang3cc81a92018-03-02 17:29:14 +0800866 err = xdp_do_redirect(dev, &xdp, xdp_prog);
867 if (err) {
868 if (unlikely(xdp_page != page))
869 put_page(xdp_page);
870 goto err_xdp;
871 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200872 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang3cc81a92018-03-02 17:29:14 +0800873 if (unlikely(xdp_page != page))
Jason Wang68904182018-05-22 11:44:28 +0800874 put_page(page);
Jason Wang3cc81a92018-03-02 17:29:14 +0800875 rcu_read_unlock();
876 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -0800877 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800878 bpf_warn_invalid_xdp_action(act);
879 case XDP_ABORTED:
880 trace_xdp_exception(vi->dev, xdp_prog, act);
881 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -0800882 if (unlikely(xdp_page != page))
883 __free_pages(xdp_page, 0);
John Fastabendf600b692016-12-15 12:13:24 -0800884 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800885 }
John Fastabendf600b692016-12-15 12:13:24 -0800886 }
887 rcu_read_unlock();
888
Jason Wang28b39bc2017-07-19 16:54:46 +0800889 truesize = mergeable_ctx_to_truesize(ctx);
890 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +0300891 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200892 dev->name, len, (unsigned long)ctx);
893 dev->stats.rx_length_errors++;
894 goto err_skb;
895 }
Jason Wang28b39bc2017-07-19 16:54:46 +0800896
John Fastabendf600b692016-12-15 12:13:24 -0800897 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
898 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000899
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200900 if (unlikely(!curr_skb))
901 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000902 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200903 int num_skb_frags;
904
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200905 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
Yunjian Wang03e9f8a2017-12-04 14:02:19 +0800906 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200907 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200908 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300909 virtio16_to_cpu(vi->vdev,
910 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200911 dev->stats.rx_length_errors++;
912 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000913 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200914
Jason Wangd46eeea2018-07-31 17:43:39 +0800915 stats->bytes += len;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200916 page = virt_to_head_page(buf);
Jason Wang28b39bc2017-07-19 16:54:46 +0800917
918 truesize = mergeable_ctx_to_truesize(ctx);
919 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +0300920 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200921 dev->name, len, (unsigned long)ctx);
922 dev->stats.rx_length_errors++;
923 goto err_skb;
924 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200925
926 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700927 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
928 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200929
930 if (unlikely(!nskb))
931 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700932 if (curr_skb == head_skb)
933 skb_shinfo(curr_skb)->frag_list = nskb;
934 else
935 curr_skb->next = nskb;
936 curr_skb = nskb;
937 head_skb->truesize += nskb->truesize;
938 num_skb_frags = 0;
939 }
940 if (curr_skb != head_skb) {
941 head_skb->data_len += len;
942 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800943 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700944 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200945 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800946 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
947 put_page(page);
948 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800949 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800950 } else {
951 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800952 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800953 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200954 }
955
Johannes Berg5377d7582015-08-19 09:48:40 +0200956 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200957 return head_skb;
958
John Fastabendf600b692016-12-15 12:13:24 -0800959err_xdp:
960 rcu_read_unlock();
Jason Wangd46eeea2018-07-31 17:43:39 +0800961 stats->xdp_drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200962err_skb:
963 put_page(page);
Jason Wang850e0882018-05-22 11:44:30 +0800964 while (num_buf-- > 1) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200965 buf = virtqueue_get_buf(rq->vq, &len);
966 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200967 pr_debug("%s: rx error: %d buffers missing\n",
968 dev->name, num_buf);
969 dev->stats.rx_length_errors++;
970 break;
971 }
Jason Wangd46eeea2018-07-31 17:43:39 +0800972 stats->bytes += len;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200973 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200974 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000975 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200976err_buf:
Jason Wangd46eeea2018-07-31 17:43:39 +0800977 stats->drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200978 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800979xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200980 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000981}
982
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900983static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
984 void *buf, unsigned int len, void **ctx,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900985 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800986 struct virtnet_rq_stats *stats)
Rusty Russell296f96f2007-10-22 11:03:37 +1000987{
Jason Wange9d74172012-12-07 07:04:55 +0000988 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000989 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300990 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000991
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300992 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000993 pr_debug("%s: short packet %i\n", dev->name, len);
994 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800995 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200996 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -0800997 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800998 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800999 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08001000 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001001 }
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001002 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001003 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001004
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001005 if (vi->mergeable_rx_bufs)
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001006 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001007 stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001008 else if (vi->big_packets)
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001009 skb = receive_big(dev, vi, rq, buf, len, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001010 else
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001011 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001012
1013 if (unlikely(!skb))
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001014 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001015
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001016 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001017
Mike Rapoporte858fae2016-06-08 16:09:21 +03001018 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +00001019 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +10001020
Mike Rapoporte858fae2016-06-08 16:09:21 +03001021 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1022 virtio_is_little_endian(vi->vdev))) {
1023 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1024 dev->name, hdr->hdr.gso_type,
1025 hdr->hdr.gso_size);
1026 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001027 }
1028
Mike Rapoportd1dc06d2016-06-14 08:29:38 +03001029 skb->protocol = eth_type_trans(skb, dev);
1030 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1031 ntohs(skb->protocol), skb->len, skb->pkt_type);
1032
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001033 napi_gro_receive(&rq->napi, skb);
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001034 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001035
1036frame_err:
1037 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +10001038 dev_kfree_skb(skb);
1039}
1040
Jason Wang192f68c2017-07-19 16:54:47 +08001041/* Unlike mergeable buffers, all buffers are allocated to the
1042 * same size, except for the headroom. For this reason we do
1043 * not need to use mergeable_len_to_ctx here - it is enough
1044 * to store the headroom as the context ignoring the truesize.
1045 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001046static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1047 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +10001048{
Jason Wangf6b10202017-02-21 16:46:28 +08001049 struct page_frag *alloc_frag = &rq->alloc_frag;
1050 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001051 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wang192f68c2017-07-19 16:54:47 +08001052 void *ctx = (void *)(unsigned long)xdp_headroom;
Jason Wangf6b10202017-02-21 16:46:28 +08001053 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001054 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001055
Jason Wangf6b10202017-02-21 16:46:28 +08001056 len = SKB_DATA_ALIGN(len) +
1057 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1058 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001059 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001060
Jason Wangf6b10202017-02-21 16:46:28 +08001061 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1062 get_page(alloc_frag->page);
1063 alloc_frag->offset += len;
1064 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1065 vi->hdr_len + GOOD_PACKET_LEN);
Jason Wang192f68c2017-07-19 16:54:47 +08001066 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001067 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +08001068 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001069 return err;
1070}
1071
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001072static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1073 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001074{
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001075 struct page *first, *list = NULL;
1076 char *p;
1077 int i, err, offset;
1078
Rusty Russella5835442014-09-11 10:17:36 +09301079 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1080
Jason Wange9d74172012-12-07 07:04:55 +00001081 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001082 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +00001083 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001084 if (!first) {
1085 if (list)
Jason Wange9d74172012-12-07 07:04:55 +00001086 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001087 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -07001088 }
Jason Wange9d74172012-12-07 07:04:55 +00001089 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +10001090
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001091 /* chain new page in list head to match sg */
1092 first->private = (unsigned long)list;
1093 list = first;
1094 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001095
Jason Wange9d74172012-12-07 07:04:55 +00001096 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001097 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +00001098 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001099 return -ENOMEM;
1100 }
1101 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +08001102
Jason Wange9d74172012-12-07 07:04:55 +00001103 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001104 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1105 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +08001106
Jason Wange9d74172012-12-07 07:04:55 +00001107 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001108 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +00001109 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +08001110
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001111 /* chain first in list head */
1112 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301113 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1114 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001115 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +00001116 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +08001117
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001118 return err;
1119}
Herbert Xu97402b92008-04-18 11:24:27 +08001120
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02001121static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
Jason Wang3cc81a92018-03-02 17:29:14 +08001122 struct ewma_pkt_len *avg_pkt_len,
1123 unsigned int room)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001124{
Michael Daltonab7db912014-01-16 22:23:27 -08001125 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001126 unsigned int len;
1127
Jason Wang3cc81a92018-03-02 17:29:14 +08001128 if (room)
1129 return PAGE_SIZE - room;
1130
1131 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03001132 rq->min_buf_len, PAGE_SIZE - hdr_len);
Jason Wang3cc81a92018-03-02 17:29:14 +08001133
Michael S. Tsirkine377fcc2017-03-06 22:21:35 +02001134 return ALIGN(len, L1_CACHE_BYTES);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001135}
1136
John Fastabend2de2f7f2017-02-02 19:16:29 -08001137static int add_recvbuf_mergeable(struct virtnet_info *vi,
1138 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -08001139{
Michael Daltonfb518792014-01-16 22:23:26 -08001140 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001141 unsigned int headroom = virtnet_get_headroom(vi);
Jason Wang3cc81a92018-03-02 17:29:14 +08001142 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1143 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
Michael Daltonfb518792014-01-16 22:23:26 -08001144 char *buf;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001145 void *ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001146 int err;
Michael Daltonfb518792014-01-16 22:23:26 -08001147 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +10001148
Jason Wang3cc81a92018-03-02 17:29:14 +08001149 /* Extra tailroom is needed to satisfy XDP's assumption. This
1150 * means rx frags coalescing won't work, but consider we've
1151 * disabled GSO for XDP, it won't be a big issue.
1152 */
1153 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1154 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001155 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -08001156
Michael Daltonfb518792014-01-16 22:23:26 -08001157 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001158 buf += headroom; /* advance address leaving hole at front of pkt */
Michael Daltonfb518792014-01-16 22:23:26 -08001159 get_page(alloc_frag->page);
Jason Wang3cc81a92018-03-02 17:29:14 +08001160 alloc_frag->offset += len + room;
Michael Daltonfb518792014-01-16 22:23:26 -08001161 hole = alloc_frag->size - alloc_frag->offset;
Jason Wang3cc81a92018-03-02 17:29:14 +08001162 if (hole < len + room) {
Michael Daltonab7db912014-01-16 22:23:27 -08001163 /* To avoid internal fragmentation, if there is very likely not
1164 * enough space for another buffer, add the remaining space to
Michael S. Tsirkin1daa8792017-07-31 21:49:49 +03001165 * the current buffer.
Michael Daltonab7db912014-01-16 22:23:27 -08001166 */
Michael Daltonfb518792014-01-16 22:23:26 -08001167 len += hole;
1168 alloc_frag->offset += hole;
1169 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001170
Michael Daltonfb518792014-01-16 22:23:26 -08001171 sg_init_one(rq->sg, buf, len);
David S. Miller29fda252017-08-01 10:07:50 -07001172 ctx = mergeable_len_to_ctx(len, headroom);
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001173 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001174 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -07001175 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001176
1177 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001178}
1179
Rusty Russellb2baed62011-12-29 00:42:38 +00001180/*
1181 * Returns false if we couldn't fill entirely (OOM).
1182 *
1183 * Normally run in the receive path, but can also be run from ndo_open
1184 * before we're receiving packets, or from refill_work which is
1185 * careful to disable receiving (using napi_disable).
1186 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001187static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1188 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001189{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001190 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001191 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001192
Amit Shah0aea51c2009-08-26 14:58:28 +05301193 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001194 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001195 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001196 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001197 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001198 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001199 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001200
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001201 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301202 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001203 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001204 } while (rq->vq->num_free);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001205 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1206 u64_stats_update_begin(&rq->stats.syncp);
Jason Wangd46eeea2018-07-31 17:43:39 +08001207 rq->stats.kicks++;
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001208 u64_stats_update_end(&rq->stats.syncp);
1209 }
1210
Rusty Russell3161e452009-08-26 12:22:32 -07001211 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001212}
1213
Rusty Russell18445c42008-02-04 23:49:57 -05001214static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001215{
1216 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001217 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +00001218
Willem de Bruijne4e84522017-04-24 13:49:26 -04001219 virtqueue_napi_schedule(&rq->napi, rvq);
Rusty Russell296f96f2007-10-22 11:03:37 +10001220}
1221
Willem de Bruijne4e84522017-04-24 13:49:26 -04001222static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001223{
Willem de Bruijne4e84522017-04-24 13:49:26 -04001224 napi_enable(napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001225
1226 /* If all buffers were filled by other side before we napi_enabled, we
Willem de Bruijne4e84522017-04-24 13:49:26 -04001227 * won't get another interrupt, so process any outstanding packets now.
1228 * Call local_bh_enable after to trigger softIRQ processing.
1229 */
1230 local_bh_disable();
1231 virtqueue_napi_schedule(napi, vq);
1232 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001233}
1234
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001235static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1236 struct virtqueue *vq,
1237 struct napi_struct *napi)
1238{
1239 if (!napi->weight)
1240 return;
1241
1242 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1243 * enable the feature if this is likely affine with the transmit path.
1244 */
1245 if (!vi->affinity_hint_set) {
1246 napi->weight = 0;
1247 return;
1248 }
1249
1250 return virtnet_napi_enable(vq, napi);
1251}
1252
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001253static void virtnet_napi_tx_disable(struct napi_struct *napi)
1254{
1255 if (napi->weight)
1256 napi_disable(napi);
1257}
1258
Rusty Russell3161e452009-08-26 12:22:32 -07001259static void refill_work(struct work_struct *work)
1260{
Jason Wange9d74172012-12-07 07:04:55 +00001261 struct virtnet_info *vi =
1262 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -07001263 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +00001264 int i;
Rusty Russell3161e452009-08-26 12:22:32 -07001265
Sasha Levin55257d72013-04-29 12:00:08 +09301266 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001267 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -07001268
Jason Wang986a4f42012-12-07 07:04:56 +00001269 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001270 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001271 virtnet_napi_enable(rq->vq, &rq->napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001272
1273 /* In theory, this can happen: if we don't get any buffers in
1274 * we will *never* try to fill again.
1275 */
1276 if (still_empty)
1277 schedule_delayed_work(&vi->refill, HZ/2);
1278 }
Rusty Russell3161e452009-08-26 12:22:32 -07001279}
1280
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001281static int virtnet_receive(struct receive_queue *rq, int budget,
1282 unsigned int *xdp_xmit)
Rusty Russell296f96f2007-10-22 11:03:37 +10001283{
Jason Wange9d74172012-12-07 07:04:55 +00001284 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wangd46eeea2018-07-31 17:43:39 +08001285 struct virtnet_rq_stats stats = {};
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001286 unsigned int len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001287 void *buf;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001288 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001289
Jason Wang192f68c2017-07-19 16:54:47 +08001290 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001291 void *ctx;
1292
Jason Wangd46eeea2018-07-31 17:43:39 +08001293 while (stats.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001294 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001295 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
Jason Wangd46eeea2018-07-31 17:43:39 +08001296 stats.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001297 }
1298 } else {
Jason Wangd46eeea2018-07-31 17:43:39 +08001299 while (stats.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001300 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001301 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
Jason Wangd46eeea2018-07-31 17:43:39 +08001302 stats.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001303 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001304 }
1305
Jason Wangbe121f42014-01-16 14:45:24 +08001306 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001307 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001308 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -07001309 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001310
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001311 u64_stats_update_begin(&rq->stats.syncp);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001312 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1313 size_t offset = virtnet_rq_stats_desc[i].offset;
1314 u64 *item;
1315
Jason Wangd46eeea2018-07-31 17:43:39 +08001316 item = (u64 *)((u8 *)&rq->stats + offset);
1317 *item += *(u64 *)((u8 *)&stats + offset);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001318 }
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001319 u64_stats_update_end(&rq->stats.syncp);
Jason Wang61845d22017-02-17 11:33:09 +08001320
Jason Wangd46eeea2018-07-31 17:43:39 +08001321 return stats.packets;
Jason Wang2ffa7592014-07-23 16:33:54 +08001322}
1323
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001324static void free_old_xmit_skbs(struct send_queue *sq)
1325{
1326 struct sk_buff *skb;
1327 unsigned int len;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001328 unsigned int packets = 0;
1329 unsigned int bytes = 0;
1330
1331 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1332 pr_debug("Sent skb %p\n", skb);
1333
1334 bytes += skb->len;
1335 packets++;
1336
Eric Dumazetdadc0732017-08-24 09:02:49 -07001337 dev_consume_skb_any(skb);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001338 }
1339
1340 /* Avoid overhead when no packets have been processed
1341 * happens when called speculatively from start_xmit.
1342 */
1343 if (!packets)
1344 return;
1345
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001346 u64_stats_update_begin(&sq->stats.syncp);
1347 sq->stats.bytes += bytes;
1348 sq->stats.packets += packets;
1349 u64_stats_update_end(&sq->stats.syncp);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001350}
1351
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001352static void virtnet_poll_cleantx(struct receive_queue *rq)
1353{
1354 struct virtnet_info *vi = rq->vq->vdev->priv;
1355 unsigned int index = vq2rxq(rq->vq);
1356 struct send_queue *sq = &vi->sq[index];
1357 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1358
1359 if (!sq->napi.weight)
1360 return;
1361
1362 if (__netif_tx_trylock(txq)) {
1363 free_old_xmit_skbs(sq);
1364 __netif_tx_unlock(txq);
1365 }
1366
1367 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1368 netif_tx_wake_queue(txq);
1369}
1370
Jason Wang2ffa7592014-07-23 16:33:54 +08001371static int virtnet_poll(struct napi_struct *napi, int budget)
1372{
1373 struct receive_queue *rq =
1374 container_of(napi, struct receive_queue, napi);
Jason Wang9267c432018-04-13 14:58:25 +08001375 struct virtnet_info *vi = rq->vq->vdev->priv;
1376 struct send_queue *sq;
Toshiaki Makita2a435652018-07-23 23:36:07 +09001377 unsigned int received;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001378 unsigned int xdp_xmit = 0;
Jason Wang2ffa7592014-07-23 16:33:54 +08001379
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001380 virtnet_poll_cleantx(rq);
1381
Jason Wang186b3c92017-09-19 17:42:43 +08001382 received = virtnet_receive(rq, budget, &xdp_xmit);
Jason Wang2ffa7592014-07-23 16:33:54 +08001383
Rusty Russell8329d982007-11-19 11:20:43 -05001384 /* Out of packets? */
Willem de Bruijne4e84522017-04-24 13:49:26 -04001385 if (received < budget)
1386 virtqueue_napi_complete(napi, rq->vq, received);
Rusty Russell296f96f2007-10-22 11:03:37 +10001387
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001388 if (xdp_xmit & VIRTIO_XDP_REDIR)
1389 xdp_do_flush_map();
1390
1391 if (xdp_xmit & VIRTIO_XDP_TX) {
Toshiaki Makita2a435652018-07-23 23:36:07 +09001392 sq = virtnet_xdp_sq(vi);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001393 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1394 u64_stats_update_begin(&sq->stats.syncp);
1395 sq->stats.kicks++;
1396 u64_stats_update_end(&sq->stats.syncp);
1397 }
Jason Wang9267c432018-04-13 14:58:25 +08001398 }
Jason Wang186b3c92017-09-19 17:42:43 +08001399
Rusty Russell296f96f2007-10-22 11:03:37 +10001400 return received;
1401}
1402
Jason Wang986a4f42012-12-07 07:04:56 +00001403static int virtnet_open(struct net_device *dev)
1404{
1405 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001406 int i, err;
Jason Wang986a4f42012-12-07 07:04:56 +00001407
Jason Wange4166622013-05-21 20:03:58 +00001408 for (i = 0; i < vi->max_queue_pairs; i++) {
1409 if (i < vi->curr_queue_pairs)
1410 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001411 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001412 schedule_delayed_work(&vi->refill, 0);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001413
1414 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i);
1415 if (err < 0)
1416 return err;
1417
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +02001418 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1419 MEM_TYPE_PAGE_SHARED, NULL);
1420 if (err < 0) {
1421 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1422 return err;
1423 }
1424
Willem de Bruijne4e84522017-04-24 13:49:26 -04001425 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001426 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001427 }
1428
1429 return 0;
1430}
1431
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001432static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1433{
1434 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1435 struct virtnet_info *vi = sq->vq->vdev->priv;
1436 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
1437
1438 __netif_tx_lock(txq, raw_smp_processor_id());
1439 free_old_xmit_skbs(sq);
1440 __netif_tx_unlock(txq);
1441
1442 virtqueue_napi_complete(napi, sq->vq, 0);
1443
1444 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1445 netif_tx_wake_queue(txq);
1446
1447 return 0;
1448}
1449
Jason Wange9d74172012-12-07 07:04:55 +00001450static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001451{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001452 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001453 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001454 struct virtnet_info *vi = sq->vq->vdev->priv;
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001455 int num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001456 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301457 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001458
Johannes Berge1749612008-10-27 15:59:26 -07001459 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301460
1461 can_push = vi->any_header_sg &&
1462 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1463 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1464 /* Even if we can, don't push here yet as this would skew
1465 * csum_start offset below. */
1466 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001467 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301468 else
1469 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001470
Mike Rapoporte858fae2016-06-08 16:09:21 +03001471 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04001472 virtio_is_little_endian(vi->vdev), false,
1473 0))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001474 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001475
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001476 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001477 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001478
Jason Wang547c8902015-08-27 14:53:06 +08001479 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301480 if (can_push) {
1481 __skb_push(skb, hdr_len);
1482 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001483 if (unlikely(num_sg < 0))
1484 return num_sg;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301485 /* Pull header back to avoid skew in tx bytes calculations. */
1486 __skb_pull(skb, hdr_len);
1487 } else {
1488 sg_set_buf(sq->sg, hdr, hdr_len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001489 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1490 if (unlikely(num_sg < 0))
1491 return num_sg;
1492 num_sg++;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301493 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301494 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001495}
1496
Stephen Hemminger424efe92009-08-31 19:50:51 +00001497static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001498{
1499 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001500 int qnum = skb_get_queue_mapping(skb);
1501 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301502 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001503 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1504 bool kick = !skb->xmit_more;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001505 bool use_napi = sq->napi.weight;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001506
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001507 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001508 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001509
Willem de Bruijnbdb12e02017-04-24 13:49:30 -04001510 if (use_napi && kick)
1511 virtqueue_enable_cb_delayed(sq->vq);
1512
Jacob Keller074c3582014-06-25 02:37:13 +00001513 /* timestamp packet in software */
1514 skb_tx_timestamp(skb);
1515
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001516 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001517 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001518
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301519 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001520 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301521 dev->stats.tx_fifo_errors++;
1522 if (net_ratelimit())
1523 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001524 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001525 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001526 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001527 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001528 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001529
Rusty Russell48925e32009-09-24 09:59:20 -06001530 /* Don't wait up for transmitted skbs to be freed. */
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001531 if (!use_napi) {
1532 skb_orphan(skb);
1533 nf_reset(skb);
1534 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001535
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001536 /* If running out of space, stop queue to avoid getting packets that we
1537 * are then unable to transmit.
1538 * An alternative would be to force queuing layer to requeue the skb by
1539 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1540 * returned in a normal path of operation: it means that driver is not
1541 * maintaining the TX queue stop/start state properly, and causes
1542 * the stack to do a non-trivial amount of useless work.
1543 * Since most packets only take 1 or 2 ring slots, stopping the queue
1544 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001545 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001546 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001547 netif_stop_subqueue(dev, qnum);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001548 if (!use_napi &&
1549 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001550 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001551 free_old_xmit_skbs(sq);
1552 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001553 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001554 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001555 }
1556 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001557 }
Rusty Russell48925e32009-09-24 09:59:20 -06001558
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001559 if (kick || netif_xmit_stopped(txq)) {
1560 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1561 u64_stats_update_begin(&sq->stats.syncp);
1562 sq->stats.kicks++;
1563 u64_stats_update_end(&sq->stats.syncp);
1564 }
1565 }
David S. Miller0b725a22014-08-25 15:51:53 -07001566
Rusty Russell48925e32009-09-24 09:59:20 -06001567 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001568}
1569
Amos Kong40cbfc32013-01-21 01:17:21 +00001570/*
1571 * Send command via the control virtqueue and check status. Commands
1572 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001573 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001574 */
1575static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001576 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001577{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301578 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001579 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001580
1581 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301582 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001583
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001584 vi->ctrl->status = ~0;
1585 vi->ctrl->hdr.class = class;
1586 vi->ctrl->hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301587 /* Add header */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001588 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301589 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001590
Rusty Russellf7bc9592013-03-20 15:44:28 +10301591 if (out)
1592 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001593
Rusty Russellf7bc9592013-03-20 15:44:28 +10301594 /* Add return status. */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001595 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001596 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001597
stephen hemmingerd24bae32013-12-09 16:17:40 -08001598 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301599 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001600
Heinz Graalfs67975902013-10-29 09:40:02 +10301601 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001602 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001603
1604 /* Spin for a response, the kick causes an ioport write, trapping
1605 * into the hypervisor, so the request should be handled immediately.
1606 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301607 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1608 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001609 cpu_relax();
1610
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001611 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001612}
1613
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001614static int virtnet_set_mac_address(struct net_device *dev, void *p)
1615{
1616 struct virtnet_info *vi = netdev_priv(dev);
1617 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001618 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001619 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001620 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001621
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07001622 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1623 return -EOPNOTSUPP;
1624
Shyam Saini801822d2016-12-24 00:44:58 +05301625 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001626 if (!addr)
1627 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001628
1629 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001630 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001631 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001632
Amos Kong7e58d5a2013-01-21 01:17:23 +00001633 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1634 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1635 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001636 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001637 dev_warn(&vdev->dev,
1638 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001639 ret = -EINVAL;
1640 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001641 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001642 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1643 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301644 unsigned int i;
1645
1646 /* Naturally, this has an atomicity problem. */
1647 for (i = 0; i < dev->addr_len; i++)
1648 virtio_cwrite8(vdev,
1649 offsetof(struct virtio_net_config, mac) +
1650 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001651 }
1652
1653 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001654 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001655
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001656out:
1657 kfree(addr);
1658 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001659}
1660
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001661static void virtnet_stats(struct net_device *dev,
1662 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001663{
1664 struct virtnet_info *vi = netdev_priv(dev);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001665 unsigned int start;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001666 int i;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001667
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001668 for (i = 0; i < vi->max_queue_pairs; i++) {
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001669 u64 tpackets, tbytes, rpackets, rbytes, rdrops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001670 struct receive_queue *rq = &vi->rq[i];
1671 struct send_queue *sq = &vi->sq[i];
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001672
1673 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001674 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1675 tpackets = sq->stats.packets;
1676 tbytes = sq->stats.bytes;
1677 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001678
1679 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001680 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
Jason Wangd46eeea2018-07-31 17:43:39 +08001681 rpackets = rq->stats.packets;
1682 rbytes = rq->stats.bytes;
1683 rdrops = rq->stats.drops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001684 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001685
1686 tot->rx_packets += rpackets;
1687 tot->tx_packets += tpackets;
1688 tot->rx_bytes += rbytes;
1689 tot->tx_bytes += tbytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001690 tot->rx_dropped += rdrops;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001691 }
1692
1693 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001694 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001695 tot->rx_length_errors = dev->stats.rx_length_errors;
1696 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001697}
1698
Amit Shahda74e892008-02-29 16:24:50 +05301699#ifdef CONFIG_NET_POLL_CONTROLLER
1700static void virtnet_netpoll(struct net_device *dev)
1701{
1702 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001703 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301704
Jason Wang986a4f42012-12-07 07:04:56 +00001705 for (i = 0; i < vi->curr_queue_pairs; i++)
1706 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301707}
1708#endif
1709
Jason Wang586d17c2012-04-11 20:43:52 +00001710static void virtnet_ack_link_announce(struct virtnet_info *vi)
1711{
1712 rtnl_lock();
1713 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001714 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001715 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1716 rtnl_unlock();
1717}
1718
John Fastabend473153292017-02-02 19:14:32 -08001719static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001720{
1721 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001722 struct net_device *dev = vi->dev;
1723
1724 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1725 return 0;
1726
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001727 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1728 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001729
1730 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001731 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001732 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1733 queue_pairs);
1734 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301735 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001736 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001737 /* virtnet_open() will refill when device is going to up. */
1738 if (dev->flags & IFF_UP)
1739 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301740 }
Jason Wang986a4f42012-12-07 07:04:56 +00001741
1742 return 0;
1743}
1744
John Fastabend473153292017-02-02 19:14:32 -08001745static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1746{
1747 int err;
1748
1749 rtnl_lock();
1750 err = _virtnet_set_queues(vi, queue_pairs);
1751 rtnl_unlock();
1752 return err;
1753}
1754
Rusty Russell296f96f2007-10-22 11:03:37 +10001755static int virtnet_close(struct net_device *dev)
1756{
1757 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001758 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001759
Rusty Russellb2baed62011-12-29 00:42:38 +00001760 /* Make sure refill_work doesn't re-enable napi! */
1761 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001762
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001763 for (i = 0; i < vi->max_queue_pairs; i++) {
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001764 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
Jason Wang986a4f42012-12-07 07:04:56 +00001765 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001766 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001767 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001768
Rusty Russell296f96f2007-10-22 11:03:37 +10001769 return 0;
1770}
1771
Alex Williamson2af76982009-02-04 09:02:40 +00001772static void virtnet_set_rx_mode(struct net_device *dev)
1773{
1774 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001775 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001776 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001777 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001778 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001779 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001780 void *buf;
1781 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001782
stephen hemminger788a8b62013-12-09 16:18:45 -08001783 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001784 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1785 return;
1786
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001787 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1788 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001789
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001790 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001791
1792 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001793 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001794 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001795 vi->ctrl->promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001796
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001797 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001798
1799 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001800 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001801 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001802 vi->ctrl->allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001803
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001804 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001805 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001806 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001807 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1808 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1809 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001810 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001811 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001812
Alex Williamson23e258e2009-05-01 17:27:56 +00001813 sg_init_table(sg, 2);
1814
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001815 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001816 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001817 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001818 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001819 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001820
1821 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001822 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001823
1824 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001825 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001826
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001827 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001828 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001829 netdev_for_each_mc_addr(ha, dev)
1830 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001831
1832 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001833 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001834
1835 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001836 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001837 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001838
1839 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001840}
1841
Patrick McHardy80d5c362013-04-19 02:04:28 +00001842static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1843 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001844{
1845 struct virtnet_info *vi = netdev_priv(dev);
1846 struct scatterlist sg;
1847
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03001848 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001849 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001850
1851 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001852 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001853 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001854 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001855}
1856
Patrick McHardy80d5c362013-04-19 02:04:28 +00001857static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1858 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001859{
1860 struct virtnet_info *vi = netdev_priv(dev);
1861 struct scatterlist sg;
1862
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03001863 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001864 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001865
1866 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001867 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001868 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001869 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001870}
1871
Wanlong Gao8898c212013-01-24 23:51:30 +00001872static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001873{
1874 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001875
1876 if (vi->affinity_hint_set) {
1877 for (i = 0; i < vi->max_queue_pairs; i++) {
1878 virtqueue_set_affinity(vi->rq[i].vq, -1);
1879 virtqueue_set_affinity(vi->sq[i].vq, -1);
1880 }
1881
1882 vi->affinity_hint_set = false;
1883 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001884}
1885
1886static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001887{
1888 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001889 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001890
1891 /* In multiqueue mode, when the number of cpu is equal to the number of
1892 * queue pairs, we let the queue pairs to be private to one cpu by
1893 * setting the affinity hint to eliminate the contention.
1894 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001895 if (vi->curr_queue_pairs == 1 ||
1896 vi->max_queue_pairs != num_online_cpus()) {
1897 virtnet_clean_affinity(vi, -1);
1898 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001899 }
1900
Wanlong Gao8898c212013-01-24 23:51:30 +00001901 i = 0;
1902 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001903 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1904 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001905 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001906 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001907 }
1908
Wanlong Gao8898c212013-01-24 23:51:30 +00001909 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001910}
1911
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001912static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001913{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001914 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1915 node);
1916 virtnet_set_affinity(vi);
1917 return 0;
1918}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001919
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001920static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1921{
1922 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1923 node_dead);
1924 virtnet_set_affinity(vi);
1925 return 0;
1926}
Jason Wang3ab098d2013-10-15 11:18:58 +08001927
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001928static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1929{
1930 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1931 node);
1932
1933 virtnet_clean_affinity(vi, cpu);
1934 return 0;
1935}
1936
1937static enum cpuhp_state virtionet_online;
1938
1939static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1940{
1941 int ret;
1942
1943 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1944 if (ret)
1945 return ret;
1946 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1947 &vi->node_dead);
1948 if (!ret)
1949 return ret;
1950 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1951 return ret;
1952}
1953
1954static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1955{
1956 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1957 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1958 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001959}
1960
Rick Jones8f9f4662011-10-19 08:10:59 +00001961static void virtnet_get_ringparam(struct net_device *dev,
1962 struct ethtool_ringparam *ring)
1963{
1964 struct virtnet_info *vi = netdev_priv(dev);
1965
Jason Wang986a4f42012-12-07 07:04:56 +00001966 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1967 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001968 ring->rx_pending = ring->rx_max_pending;
1969 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001970}
1971
Rick Jones66846042011-11-14 14:17:08 +00001972
1973static void virtnet_get_drvinfo(struct net_device *dev,
1974 struct ethtool_drvinfo *info)
1975{
1976 struct virtnet_info *vi = netdev_priv(dev);
1977 struct virtio_device *vdev = vi->vdev;
1978
1979 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1980 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1981 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1982
1983}
1984
Jason Wangd73bcd22012-12-07 07:04:57 +00001985/* TODO: Eliminate OOO packets during switching */
1986static int virtnet_set_channels(struct net_device *dev,
1987 struct ethtool_channels *channels)
1988{
1989 struct virtnet_info *vi = netdev_priv(dev);
1990 u16 queue_pairs = channels->combined_count;
1991 int err;
1992
1993 /* We don't support separate rx/tx channels.
1994 * We don't allow setting 'other' channels.
1995 */
1996 if (channels->rx_count || channels->tx_count || channels->other_count)
1997 return -EINVAL;
1998
Amos Kongc18e9cd2014-04-18 13:45:41 +08001999 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00002000 return -EINVAL;
2001
John Fastabendf600b692016-12-15 12:13:24 -08002002 /* For now we don't support modifying channels while XDP is loaded
2003 * also when XDP is loaded all RX queues have XDP programs so we only
2004 * need to check a single RX queue.
2005 */
2006 if (vi->rq[0].xdp_prog)
2007 return -EINVAL;
2008
Wanlong Gao47be2472013-01-24 23:51:29 +00002009 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08002010 err = _virtnet_set_queues(vi, queue_pairs);
Jason Wangd73bcd22012-12-07 07:04:57 +00002011 if (!err) {
2012 netif_set_real_num_tx_queues(dev, queue_pairs);
2013 netif_set_real_num_rx_queues(dev, queue_pairs);
2014
Wanlong Gao8898c212013-01-24 23:51:30 +00002015 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00002016 }
Wanlong Gao47be2472013-01-24 23:51:29 +00002017 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00002018
2019 return err;
2020}
2021
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002022static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2023{
2024 struct virtnet_info *vi = netdev_priv(dev);
2025 char *p = (char *)data;
2026 unsigned int i, j;
2027
2028 switch (stringset) {
2029 case ETH_SS_STATS:
2030 for (i = 0; i < vi->curr_queue_pairs; i++) {
2031 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2032 snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s",
2033 i, virtnet_rq_stats_desc[j].desc);
2034 p += ETH_GSTRING_LEN;
2035 }
2036 }
2037
2038 for (i = 0; i < vi->curr_queue_pairs; i++) {
2039 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2040 snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s",
2041 i, virtnet_sq_stats_desc[j].desc);
2042 p += ETH_GSTRING_LEN;
2043 }
2044 }
2045 break;
2046 }
2047}
2048
2049static int virtnet_get_sset_count(struct net_device *dev, int sset)
2050{
2051 struct virtnet_info *vi = netdev_priv(dev);
2052
2053 switch (sset) {
2054 case ETH_SS_STATS:
2055 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2056 VIRTNET_SQ_STATS_LEN);
2057 default:
2058 return -EOPNOTSUPP;
2059 }
2060}
2061
2062static void virtnet_get_ethtool_stats(struct net_device *dev,
2063 struct ethtool_stats *stats, u64 *data)
2064{
2065 struct virtnet_info *vi = netdev_priv(dev);
2066 unsigned int idx = 0, start, i, j;
2067 const u8 *stats_base;
2068 size_t offset;
2069
2070 for (i = 0; i < vi->curr_queue_pairs; i++) {
2071 struct receive_queue *rq = &vi->rq[i];
2072
Jason Wangd46eeea2018-07-31 17:43:39 +08002073 stats_base = (u8 *)&rq->stats;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002074 do {
2075 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2076 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2077 offset = virtnet_rq_stats_desc[j].offset;
2078 data[idx + j] = *(u64 *)(stats_base + offset);
2079 }
2080 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2081 idx += VIRTNET_RQ_STATS_LEN;
2082 }
2083
2084 for (i = 0; i < vi->curr_queue_pairs; i++) {
2085 struct send_queue *sq = &vi->sq[i];
2086
2087 stats_base = (u8 *)&sq->stats;
2088 do {
2089 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2090 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2091 offset = virtnet_sq_stats_desc[j].offset;
2092 data[idx + j] = *(u64 *)(stats_base + offset);
2093 }
2094 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2095 idx += VIRTNET_SQ_STATS_LEN;
2096 }
2097}
2098
Jason Wangd73bcd22012-12-07 07:04:57 +00002099static void virtnet_get_channels(struct net_device *dev,
2100 struct ethtool_channels *channels)
2101{
2102 struct virtnet_info *vi = netdev_priv(dev);
2103
2104 channels->combined_count = vi->curr_queue_pairs;
2105 channels->max_combined = vi->max_queue_pairs;
2106 channels->max_other = 0;
2107 channels->rx_count = 0;
2108 channels->tx_count = 0;
2109 channels->other_count = 0;
2110}
2111
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002112/* Check if the user is trying to change anything besides speed/duplex */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002113static bool
2114virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002115{
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002116 struct ethtool_link_ksettings diff1 = *cmd;
2117 struct ethtool_link_ksettings diff2 = {};
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002118
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01002119 /* cmd is always set so we need to clear it, validate the port type
2120 * and also without autonegotiation we can ignore advertising
2121 */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002122 diff1.base.speed = 0;
2123 diff2.base.port = PORT_OTHER;
2124 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
2125 diff1.base.duplex = 0;
2126 diff1.base.cmd = 0;
2127 diff1.base.link_mode_masks_nwords = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002128
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002129 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
2130 bitmap_empty(diff1.link_modes.supported,
2131 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2132 bitmap_empty(diff1.link_modes.advertising,
2133 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2134 bitmap_empty(diff1.link_modes.lp_advertising,
2135 __ETHTOOL_LINK_MODE_MASK_NBITS);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002136}
2137
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002138static int virtnet_set_link_ksettings(struct net_device *dev,
2139 const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002140{
2141 struct virtnet_info *vi = netdev_priv(dev);
2142 u32 speed;
2143
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002144 speed = cmd->base.speed;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002145 /* don't allow custom speed and duplex */
2146 if (!ethtool_validate_speed(speed) ||
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002147 !ethtool_validate_duplex(cmd->base.duplex) ||
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002148 !virtnet_validate_ethtool_cmd(cmd))
2149 return -EINVAL;
2150 vi->speed = speed;
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002151 vi->duplex = cmd->base.duplex;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002152
2153 return 0;
2154}
2155
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002156static int virtnet_get_link_ksettings(struct net_device *dev,
2157 struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002158{
2159 struct virtnet_info *vi = netdev_priv(dev);
2160
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002161 cmd->base.speed = vi->speed;
2162 cmd->base.duplex = vi->duplex;
2163 cmd->base.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002164
2165 return 0;
2166}
2167
2168static void virtnet_init_settings(struct net_device *dev)
2169{
2170 struct virtnet_info *vi = netdev_priv(dev);
2171
2172 vi->speed = SPEED_UNKNOWN;
2173 vi->duplex = DUPLEX_UNKNOWN;
2174}
2175
Jason Baronfaa9b392018-01-05 17:44:54 -05002176static void virtnet_update_settings(struct virtnet_info *vi)
2177{
2178 u32 speed;
2179 u8 duplex;
2180
2181 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2182 return;
2183
2184 speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config,
2185 speed));
2186 if (ethtool_validate_speed(speed))
2187 vi->speed = speed;
2188 duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config,
2189 duplex));
2190 if (ethtool_validate_duplex(duplex))
2191 vi->duplex = duplex;
2192}
2193
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002194static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00002195 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002196 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00002197 .get_ringparam = virtnet_get_ringparam,
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002198 .get_strings = virtnet_get_strings,
2199 .get_sset_count = virtnet_get_sset_count,
2200 .get_ethtool_stats = virtnet_get_ethtool_stats,
Jason Wangd73bcd22012-12-07 07:04:57 +00002201 .set_channels = virtnet_set_channels,
2202 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00002203 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002204 .get_link_ksettings = virtnet_get_link_ksettings,
2205 .set_link_ksettings = virtnet_set_link_ksettings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08002206};
2207
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002208static void virtnet_freeze_down(struct virtio_device *vdev)
2209{
2210 struct virtnet_info *vi = vdev->priv;
2211 int i;
2212
2213 /* Make sure no work handler is accessing the device */
2214 flush_work(&vi->config_work);
2215
2216 netif_device_detach(vi->dev);
Jason Wang713a98d2017-06-28 09:51:03 +08002217 netif_tx_disable(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002218 cancel_delayed_work_sync(&vi->refill);
2219
2220 if (netif_running(vi->dev)) {
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002221 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002222 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04002223 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002224 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002225 }
2226}
2227
2228static int init_vqs(struct virtnet_info *vi);
2229
2230static int virtnet_restore_up(struct virtio_device *vdev)
2231{
2232 struct virtnet_info *vi = vdev->priv;
2233 int err, i;
2234
2235 err = init_vqs(vi);
2236 if (err)
2237 return err;
2238
2239 virtio_device_ready(vdev);
2240
2241 if (netif_running(vi->dev)) {
2242 for (i = 0; i < vi->curr_queue_pairs; i++)
2243 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2244 schedule_delayed_work(&vi->refill, 0);
2245
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002246 for (i = 0; i < vi->max_queue_pairs; i++) {
Willem de Bruijne4e84522017-04-24 13:49:26 -04002247 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002248 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2249 &vi->sq[i].napi);
2250 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002251 }
2252
2253 netif_device_attach(vi->dev);
2254 return err;
2255}
2256
Jason Wang3f935222017-07-19 16:54:49 +08002257static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2258{
2259 struct scatterlist sg;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002260 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
Jason Wang3f935222017-07-19 16:54:49 +08002261
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002262 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
Jason Wang3f935222017-07-19 16:54:49 +08002263
2264 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2265 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2266 dev_warn(&vi->dev->dev, "Fail to set guest offload. \n");
2267 return -EINVAL;
2268 }
2269
2270 return 0;
2271}
2272
2273static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2274{
2275 u64 offloads = 0;
2276
2277 if (!vi->guest_offloads)
2278 return 0;
2279
2280 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
2281 offloads = 1ULL << VIRTIO_NET_F_GUEST_CSUM;
2282
2283 return virtnet_set_guest_offloads(vi, offloads);
2284}
2285
2286static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2287{
2288 u64 offloads = vi->guest_offloads;
2289
2290 if (!vi->guest_offloads)
2291 return 0;
2292 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
2293 offloads |= 1ULL << VIRTIO_NET_F_GUEST_CSUM;
2294
2295 return virtnet_set_guest_offloads(vi, offloads);
2296}
2297
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002298static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2299 struct netlink_ext_ack *extack)
John Fastabendf600b692016-12-15 12:13:24 -08002300{
2301 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2302 struct virtnet_info *vi = netdev_priv(dev);
2303 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08002304 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002305 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08002306
Jason Wang3f935222017-07-19 16:54:49 +08002307 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2308 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2309 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2310 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2311 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO))) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002312 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first");
John Fastabendf600b692016-12-15 12:13:24 -08002313 return -EOPNOTSUPP;
2314 }
2315
2316 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002317 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
John Fastabendf600b692016-12-15 12:13:24 -08002318 return -EINVAL;
2319 }
2320
2321 if (dev->mtu > max_sz) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002322 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
John Fastabendf600b692016-12-15 12:13:24 -08002323 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2324 return -EINVAL;
2325 }
2326
John Fastabend672aafd2016-12-15 12:13:49 -08002327 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2328 if (prog)
2329 xdp_qp = nr_cpu_ids;
2330
2331 /* XDP requires extra queues for XDP_TX */
2332 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002333 NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
John Fastabend672aafd2016-12-15 12:13:49 -08002334 netdev_warn(dev, "request %i queues but max is %i\n",
2335 curr_qp + xdp_qp, vi->max_queue_pairs);
2336 return -ENOMEM;
2337 }
2338
John Fastabend2de2f7f2017-02-02 19:16:29 -08002339 if (prog) {
2340 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
2341 if (IS_ERR(prog))
2342 return PTR_ERR(prog);
2343 }
2344
Jason Wang4941d472017-07-19 16:54:48 +08002345 /* Make sure NAPI is not using any XDP TX queues for RX. */
Jason Wang4e09ff52018-02-28 18:20:04 +08002346 if (netif_running(dev))
2347 for (i = 0; i < vi->max_queue_pairs; i++)
2348 napi_disable(&vi->rq[i].napi);
John Fastabendf600b692016-12-15 12:13:24 -08002349
John Fastabend672aafd2016-12-15 12:13:49 -08002350 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
Jason Wang4941d472017-07-19 16:54:48 +08002351 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2352 if (err)
2353 goto err;
2354 vi->xdp_queue_pairs = xdp_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002355
John Fastabendf600b692016-12-15 12:13:24 -08002356 for (i = 0; i < vi->max_queue_pairs; i++) {
2357 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2358 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
Jason Wang3f935222017-07-19 16:54:49 +08002359 if (i == 0) {
2360 if (!old_prog)
2361 virtnet_clear_guest_offloads(vi);
2362 if (!prog)
2363 virtnet_restore_guest_offloads(vi);
2364 }
John Fastabendf600b692016-12-15 12:13:24 -08002365 if (old_prog)
2366 bpf_prog_put(old_prog);
Jason Wang4e09ff52018-02-28 18:20:04 +08002367 if (netif_running(dev))
2368 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
John Fastabendf600b692016-12-15 12:13:24 -08002369 }
2370
2371 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08002372
Jason Wang4941d472017-07-19 16:54:48 +08002373err:
2374 for (i = 0; i < vi->max_queue_pairs; i++)
2375 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
John Fastabend2de2f7f2017-02-02 19:16:29 -08002376 if (prog)
2377 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2378 return err;
John Fastabendf600b692016-12-15 12:13:24 -08002379}
2380
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002381static u32 virtnet_xdp_query(struct net_device *dev)
John Fastabendf600b692016-12-15 12:13:24 -08002382{
2383 struct virtnet_info *vi = netdev_priv(dev);
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002384 const struct bpf_prog *xdp_prog;
John Fastabendf600b692016-12-15 12:13:24 -08002385 int i;
2386
2387 for (i = 0; i < vi->max_queue_pairs; i++) {
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002388 xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2389 if (xdp_prog)
2390 return xdp_prog->aux->id;
John Fastabendf600b692016-12-15 12:13:24 -08002391 }
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002392 return 0;
John Fastabendf600b692016-12-15 12:13:24 -08002393}
2394
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002395static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
John Fastabendf600b692016-12-15 12:13:24 -08002396{
2397 switch (xdp->command) {
2398 case XDP_SETUP_PROG:
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002399 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
John Fastabendf600b692016-12-15 12:13:24 -08002400 case XDP_QUERY_PROG:
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002401 xdp->prog_id = virtnet_xdp_query(dev);
John Fastabendf600b692016-12-15 12:13:24 -08002402 return 0;
2403 default:
2404 return -EINVAL;
2405 }
2406}
2407
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002408static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2409 size_t len)
2410{
2411 struct virtnet_info *vi = netdev_priv(dev);
2412 int ret;
2413
2414 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2415 return -EOPNOTSUPP;
2416
2417 ret = snprintf(buf, len, "sby");
2418 if (ret >= len)
2419 return -EOPNOTSUPP;
2420
2421 return 0;
2422}
2423
Stephen Hemminger76288b42009-01-06 10:44:22 -08002424static const struct net_device_ops virtnet_netdev = {
2425 .ndo_open = virtnet_open,
2426 .ndo_stop = virtnet_close,
2427 .ndo_start_xmit = start_xmit,
2428 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08002429 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00002430 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002431 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00002432 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2433 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002434#ifdef CONFIG_NET_POLL_CONTROLLER
2435 .ndo_poll_controller = virtnet_netpoll,
2436#endif
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002437 .ndo_bpf = virtnet_xdp,
Jason Wang186b3c92017-09-19 17:42:43 +08002438 .ndo_xdp_xmit = virtnet_xdp_xmit,
Vlad Yasevich2836b4f2017-05-23 13:38:43 -04002439 .ndo_features_check = passthru_features_check,
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002440 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002441};
2442
Jason Wang586d17c2012-04-11 20:43:52 +00002443static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002444{
Jason Wang586d17c2012-04-11 20:43:52 +00002445 struct virtnet_info *vi =
2446 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002447 u16 v;
2448
Rusty Russell855e0c52013-10-14 18:11:51 +10302449 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2450 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302451 return;
Jason Wang586d17c2012-04-11 20:43:52 +00002452
2453 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00002454 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00002455 virtnet_ack_link_announce(vi);
2456 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002457
2458 /* Ignore unknown (future) status bits */
2459 v &= VIRTIO_NET_S_LINK_UP;
2460
2461 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302462 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002463
2464 vi->status = v;
2465
2466 if (vi->status & VIRTIO_NET_S_LINK_UP) {
Jason Baronfaa9b392018-01-05 17:44:54 -05002467 virtnet_update_settings(vi);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002468 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002469 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002470 } else {
2471 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002472 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002473 }
2474}
2475
2476static void virtnet_config_changed(struct virtio_device *vdev)
2477{
2478 struct virtnet_info *vi = vdev->priv;
2479
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002480 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002481}
2482
Jason Wang986a4f42012-12-07 07:04:56 +00002483static void virtnet_free_queues(struct virtnet_info *vi)
2484{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002485 int i;
2486
Jason Wangab3971b2015-03-12 13:57:44 +08002487 for (i = 0; i < vi->max_queue_pairs; i++) {
2488 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002489 netif_napi_del(&vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002490 netif_napi_del(&vi->sq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08002491 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002492
Eric Dumazet963abe52016-11-15 22:24:12 -08002493 /* We called napi_hash_del() before netif_napi_del(),
2494 * we need to respect an RCU grace period before freeing vi->rq
2495 */
2496 synchronize_net();
2497
Jason Wang986a4f42012-12-07 07:04:56 +00002498 kfree(vi->rq);
2499 kfree(vi->sq);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002500 kfree(vi->ctrl);
Jason Wang986a4f42012-12-07 07:04:56 +00002501}
2502
John Fastabend473153292017-02-02 19:14:32 -08002503static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002504{
John Fastabendf600b692016-12-15 12:13:24 -08002505 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00002506 int i;
2507
2508 for (i = 0; i < vi->max_queue_pairs; i++) {
2509 while (vi->rq[i].pages)
2510 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08002511
2512 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2513 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2514 if (old_prog)
2515 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00002516 }
John Fastabend473153292017-02-02 19:14:32 -08002517}
2518
2519static void free_receive_bufs(struct virtnet_info *vi)
2520{
2521 rtnl_lock();
2522 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08002523 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002524}
2525
Michael Daltonfb518792014-01-16 22:23:26 -08002526static void free_receive_page_frags(struct virtnet_info *vi)
2527{
2528 int i;
2529 for (i = 0; i < vi->max_queue_pairs; i++)
2530 if (vi->rq[i].alloc_frag.page)
2531 put_page(vi->rq[i].alloc_frag.page);
2532}
2533
John Fastabendb68df012017-01-25 18:22:48 -08002534static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08002535{
2536 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
2537 return false;
2538 else if (q < vi->curr_queue_pairs)
2539 return true;
2540 else
2541 return false;
2542}
2543
Jason Wang986a4f42012-12-07 07:04:56 +00002544static void free_unused_bufs(struct virtnet_info *vi)
2545{
2546 void *buf;
2547 int i;
2548
2549 for (i = 0; i < vi->max_queue_pairs; i++) {
2550 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002551 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08002552 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08002553 dev_kfree_skb(buf);
2554 else
2555 put_page(virt_to_head_page(buf));
2556 }
Jason Wang986a4f42012-12-07 07:04:56 +00002557 }
2558
2559 for (i = 0; i < vi->max_queue_pairs; i++) {
2560 struct virtqueue *vq = vi->rq[i].vq;
2561
2562 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002563 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02002564 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002565 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002566 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002567 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002568 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002569 }
Jason Wang986a4f42012-12-07 07:04:56 +00002570 }
Jason Wang986a4f42012-12-07 07:04:56 +00002571 }
2572}
2573
Jason Wange9d74172012-12-07 07:04:55 +00002574static void virtnet_del_vqs(struct virtnet_info *vi)
2575{
2576 struct virtio_device *vdev = vi->vdev;
2577
Wanlong Gao8898c212013-01-24 23:51:30 +00002578 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00002579
Jason Wange9d74172012-12-07 07:04:55 +00002580 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002581
2582 virtnet_free_queues(vi);
2583}
2584
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002585/* How large should a single buffer be so a queue full of these can fit at
2586 * least one full packet?
2587 * Logic below assumes the mergeable buffer header is used.
2588 */
2589static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2590{
2591 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2592 unsigned int rq_size = virtqueue_get_vring_size(vq);
2593 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2594 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2595 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2596
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03002597 return max(max(min_buf_len, hdr_len) - hdr_len,
2598 (unsigned int)GOOD_PACKET_LEN);
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002599}
2600
Jason Wang986a4f42012-12-07 07:04:56 +00002601static int virtnet_find_vqs(struct virtnet_info *vi)
2602{
2603 vq_callback_t **callbacks;
2604 struct virtqueue **vqs;
2605 int ret = -ENOMEM;
2606 int i, total_vqs;
2607 const char **names;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002608 bool *ctx;
Jason Wang986a4f42012-12-07 07:04:56 +00002609
2610 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2611 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2612 * possible control vq.
2613 */
2614 total_vqs = vi->max_queue_pairs * 2 +
2615 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2616
2617 /* Allocate space for find_vqs parameters */
Kees Cook6396bb22018-06-12 14:03:40 -07002618 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002619 if (!vqs)
2620 goto err_vq;
Kees Cook6da2ec52018-06-12 13:55:00 -07002621 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002622 if (!callbacks)
2623 goto err_callback;
Kees Cook6da2ec52018-06-12 13:55:00 -07002624 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002625 if (!names)
2626 goto err_names;
Jason Wang192f68c2017-07-19 16:54:47 +08002627 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Kees Cook6396bb22018-06-12 14:03:40 -07002628 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002629 if (!ctx)
2630 goto err_ctx;
2631 } else {
2632 ctx = NULL;
2633 }
Jason Wang986a4f42012-12-07 07:04:56 +00002634
2635 /* Parameters for control virtqueue, if any */
2636 if (vi->has_cvq) {
2637 callbacks[total_vqs - 1] = NULL;
2638 names[total_vqs - 1] = "control";
2639 }
2640
2641 /* Allocate/initialize parameters for send/receive virtqueues */
2642 for (i = 0; i < vi->max_queue_pairs; i++) {
2643 callbacks[rxq2vq(i)] = skb_recv_done;
2644 callbacks[txq2vq(i)] = skb_xmit_done;
2645 sprintf(vi->rq[i].name, "input.%d", i);
2646 sprintf(vi->sq[i].name, "output.%d", i);
2647 names[rxq2vq(i)] = vi->rq[i].name;
2648 names[txq2vq(i)] = vi->sq[i].name;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002649 if (ctx)
2650 ctx[rxq2vq(i)] = true;
Jason Wang986a4f42012-12-07 07:04:56 +00002651 }
2652
2653 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002654 names, ctx, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002655 if (ret)
2656 goto err_find;
2657
2658 if (vi->has_cvq) {
2659 vi->cvq = vqs[total_vqs - 1];
2660 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002661 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002662 }
2663
2664 for (i = 0; i < vi->max_queue_pairs; i++) {
2665 vi->rq[i].vq = vqs[rxq2vq(i)];
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002666 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
Jason Wang986a4f42012-12-07 07:04:56 +00002667 vi->sq[i].vq = vqs[txq2vq(i)];
2668 }
2669
Tonghao Zhang2fa3c8a2018-05-31 07:16:32 -07002670 /* run here: ret == 0. */
Jason Wang986a4f42012-12-07 07:04:56 +00002671
Jason Wang986a4f42012-12-07 07:04:56 +00002672
2673err_find:
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002674 kfree(ctx);
2675err_ctx:
Jason Wang986a4f42012-12-07 07:04:56 +00002676 kfree(names);
2677err_names:
2678 kfree(callbacks);
2679err_callback:
2680 kfree(vqs);
2681err_vq:
2682 return ret;
2683}
2684
2685static int virtnet_alloc_queues(struct virtnet_info *vi)
2686{
2687 int i;
2688
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002689 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2690 if (!vi->ctrl)
2691 goto err_ctrl;
Kees Cook6396bb22018-06-12 14:03:40 -07002692 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002693 if (!vi->sq)
2694 goto err_sq;
Kees Cook6396bb22018-06-12 14:03:40 -07002695 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002696 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002697 goto err_rq;
2698
2699 INIT_DELAYED_WORK(&vi->refill, refill_work);
2700 for (i = 0; i < vi->max_queue_pairs; i++) {
2701 vi->rq[i].pages = NULL;
2702 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2703 napi_weight);
Willem de Bruijn1d11e732017-04-27 20:37:58 -04002704 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2705 napi_tx ? napi_weight : 0);
Jason Wang986a4f42012-12-07 07:04:56 +00002706
2707 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002708 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002709 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002710
2711 u64_stats_init(&vi->rq[i].stats.syncp);
2712 u64_stats_init(&vi->sq[i].stats.syncp);
Jason Wang986a4f42012-12-07 07:04:56 +00002713 }
2714
2715 return 0;
2716
2717err_rq:
2718 kfree(vi->sq);
2719err_sq:
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002720 kfree(vi->ctrl);
2721err_ctrl:
Jason Wang986a4f42012-12-07 07:04:56 +00002722 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002723}
2724
Amit Shah3f9c10b2011-12-22 16:58:31 +05302725static int init_vqs(struct virtnet_info *vi)
2726{
Jason Wang986a4f42012-12-07 07:04:56 +00002727 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302728
Jason Wang986a4f42012-12-07 07:04:56 +00002729 /* Allocate send & receive queues */
2730 ret = virtnet_alloc_queues(vi);
2731 if (ret)
2732 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302733
Jason Wang986a4f42012-12-07 07:04:56 +00002734 ret = virtnet_find_vqs(vi);
2735 if (ret)
2736 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302737
Wanlong Gao47be2472013-01-24 23:51:29 +00002738 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002739 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002740 put_online_cpus();
2741
Amit Shah3f9c10b2011-12-22 16:58:31 +05302742 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002743
2744err_free:
2745 virtnet_free_queues(vi);
2746err:
2747 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302748}
2749
Michael Daltonfbf28d72014-01-16 22:23:30 -08002750#ifdef CONFIG_SYSFS
2751static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -07002752 char *buf)
Michael Daltonfbf28d72014-01-16 22:23:30 -08002753{
2754 struct virtnet_info *vi = netdev_priv(queue->dev);
2755 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Jason Wang3cc81a92018-03-02 17:29:14 +08002756 unsigned int headroom = virtnet_get_headroom(vi);
2757 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
Johannes Berg5377d7582015-08-19 09:48:40 +02002758 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002759
2760 BUG_ON(queue_index >= vi->max_queue_pairs);
2761 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002762 return sprintf(buf, "%u\n",
Jason Wang3cc81a92018-03-02 17:29:14 +08002763 get_mergeable_buf_len(&vi->rq[queue_index], avg,
2764 SKB_DATA_ALIGN(headroom + tailroom)));
Michael Daltonfbf28d72014-01-16 22:23:30 -08002765}
2766
2767static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2768 __ATTR_RO(mergeable_rx_buffer_size);
2769
2770static struct attribute *virtio_net_mrg_rx_attrs[] = {
2771 &mergeable_rx_buffer_size_attribute.attr,
2772 NULL
2773};
2774
2775static const struct attribute_group virtio_net_mrg_rx_group = {
2776 .name = "virtio_net",
2777 .attrs = virtio_net_mrg_rx_attrs
2778};
2779#endif
2780
Jason Wang892d6eb2014-11-20 17:03:05 +08002781static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2782 unsigned int fbit,
2783 const char *fname, const char *dname)
2784{
2785 if (!virtio_has_feature(vdev, fbit))
2786 return false;
2787
2788 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2789 fname, dname);
2790
2791 return true;
2792}
2793
2794#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2795 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2796
2797static bool virtnet_validate_features(struct virtio_device *vdev)
2798{
2799 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2800 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2801 "VIRTIO_NET_F_CTRL_VQ") ||
2802 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2803 "VIRTIO_NET_F_CTRL_VQ") ||
2804 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2805 "VIRTIO_NET_F_CTRL_VQ") ||
2806 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2807 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2808 "VIRTIO_NET_F_CTRL_VQ"))) {
2809 return false;
2810 }
2811
2812 return true;
2813}
2814
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002815#define MIN_MTU ETH_MIN_MTU
2816#define MAX_MTU ETH_MAX_MTU
2817
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002818static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10002819{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002820 if (!vdev->config->get) {
2821 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2822 __func__);
2823 return -EINVAL;
2824 }
2825
Jason Wang892d6eb2014-11-20 17:03:05 +08002826 if (!virtnet_validate_features(vdev))
2827 return -EINVAL;
2828
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002829 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2830 int mtu = virtio_cread16(vdev,
2831 offsetof(struct virtio_net_config,
2832 mtu));
2833 if (mtu < MIN_MTU)
2834 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2835 }
2836
2837 return 0;
2838}
2839
2840static int virtnet_probe(struct virtio_device *vdev)
2841{
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002842 int i, err = -ENOMEM;
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002843 struct net_device *dev;
2844 struct virtnet_info *vi;
2845 u16 max_queue_pairs;
2846 int mtu;
2847
Jason Wang986a4f42012-12-07 07:04:56 +00002848 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302849 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2850 struct virtio_net_config,
2851 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002852
2853 /* We need at least 2 queue's */
2854 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2855 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2856 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2857 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002858
2859 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002860 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002861 if (!dev)
2862 return -ENOMEM;
2863
2864 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002865 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002866 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002867 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002868
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002869 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002870 SET_NETDEV_DEV(dev, &vdev->dev);
2871
2872 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002873 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002874 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002875 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002876 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002877 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002878
2879 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
David S. Millere078de02017-07-03 06:37:32 -07002880 dev->hw_features |= NETIF_F_TSO
Rusty Russell34a48572008-02-04 23:50:02 -05002881 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2882 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002883 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002884 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2885 dev->hw_features |= NETIF_F_TSO;
2886 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2887 dev->hw_features |= NETIF_F_TSO6;
2888 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2889 dev->hw_features |= NETIF_F_TSO_ECN;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002890
Jason Wang41f2f122014-12-24 11:03:52 +08002891 dev->features |= NETIF_F_GSO_ROBUST;
2892
Michał Mirosław98e778c2011-03-31 01:01:35 +00002893 if (gso)
David S. Millere078de02017-07-03 06:37:32 -07002894 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002895 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002896 }
Thomas Huth4f491292013-08-27 17:09:02 +02002897 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2898 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002899
Jason Wang4fda8302013-04-10 23:32:21 +00002900 dev->vlan_features = dev->features;
2901
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002902 /* MTU range: 68 - 65535 */
2903 dev->min_mtu = MIN_MTU;
2904 dev->max_mtu = MAX_MTU;
2905
Rusty Russell296f96f2007-10-22 11:03:37 +10002906 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302907 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2908 virtio_cread_bytes(vdev,
2909 offsetof(struct virtio_net_config, mac),
2910 dev->dev_addr, dev->addr_len);
2911 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002912 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002913
2914 /* Set up our device-specific information */
2915 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002916 vi->dev = dev;
2917 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002918 vdev->priv = vi;
John Stultz827da442013-10-07 15:51:58 -07002919
Jason Wang586d17c2012-04-11 20:43:52 +00002920 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002921
Herbert Xu97402b92008-04-18 11:24:27 +08002922 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002923 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2924 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002925 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2926 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002927 vi->big_packets = true;
2928
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002929 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2930 vi->mergeable_rx_bufs = true;
2931
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002932 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2933 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002934 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2935 else
2936 vi->hdr_len = sizeof(struct virtio_net_hdr);
2937
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002938 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2939 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302940 vi->any_header_sg = true;
2941
Jason Wang986a4f42012-12-07 07:04:56 +00002942 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2943 vi->has_cvq = true;
2944
Aaron Conole14de9d12016-06-03 16:57:12 -04002945 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2946 mtu = virtio_cread16(vdev,
2947 offsetof(struct virtio_net_config,
2948 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002949 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002950 /* Should never trigger: MTU was previously validated
2951 * in virtnet_validate.
2952 */
2953 dev_err(&vdev->dev, "device MTU appears to have changed "
2954 "it is now %d < %d", mtu, dev->min_mtu);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002955 goto free;
Aaron Conole93a205e2016-10-25 16:12:12 -04002956 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002957
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002958 dev->mtu = mtu;
2959 dev->max_mtu = mtu;
2960
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002961 /* TODO: size buffers correctly in this case. */
2962 if (dev->mtu > ETH_DATA_LEN)
2963 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04002964 }
2965
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002966 if (vi->any_header_sg)
2967 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002968
Jason Wang44900012016-11-25 12:37:26 +08002969 /* Enable multiqueue by default */
2970 if (num_online_cpus() >= max_queue_pairs)
2971 vi->curr_queue_pairs = max_queue_pairs;
2972 else
2973 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002974 vi->max_queue_pairs = max_queue_pairs;
2975
2976 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302977 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002978 if (err)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002979 goto free;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002980
Michael Daltonfbf28d72014-01-16 22:23:30 -08002981#ifdef CONFIG_SYSFS
2982 if (vi->mergeable_rx_bufs)
2983 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2984#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08002985 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2986 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002987
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002988 virtnet_init_settings(dev);
2989
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002990 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
2991 vi->failover = net_failover_create(vi->dev);
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00002992 if (IS_ERR(vi->failover)) {
2993 err = PTR_ERR(vi->failover);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002994 goto free_vqs;
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00002995 }
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002996 }
2997
Rusty Russell296f96f2007-10-22 11:03:37 +10002998 err = register_netdev(dev);
2999 if (err) {
3000 pr_debug("virtio_net: registering device failed\n");
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003001 goto free_failover;
Rusty Russell296f96f2007-10-22 11:03:37 +10003002 }
Rusty Russellb3369c12008-02-04 23:50:02 -05003003
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10303004 virtio_device_ready(vdev);
3005
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003006 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003007 if (err) {
3008 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08003009 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003010 }
3011
Jason Wanga2208712016-12-13 14:23:05 +08003012 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08003013
Jason Wang167c25e2010-11-10 14:45:41 +00003014 /* Assume link up if device can't report link status,
3015 otherwise get link status from config. */
Jay Vosburghbda7fab2018-03-22 14:42:41 +00003016 netif_carrier_off(dev);
Jason Wang167c25e2010-11-10 14:45:41 +00003017 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
Tejun Heo3b07e9c2012-08-20 14:51:24 -07003018 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00003019 } else {
3020 vi->status = VIRTIO_NET_S_LINK_UP;
Jason Baronfaa9b392018-01-05 17:44:54 -05003021 virtnet_update_settings(vi);
Jason Wang167c25e2010-11-10 14:45:41 +00003022 netif_carrier_on(dev);
3023 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003024
Jason Wang3f935222017-07-19 16:54:49 +08003025 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3026 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3027 set_bit(guest_offloads[i], &vi->guest_offloads);
3028
Jason Wang986a4f42012-12-07 07:04:56 +00003029 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3030 dev->name, max_queue_pairs);
3031
Rusty Russell296f96f2007-10-22 11:03:37 +10003032 return 0;
3033
wangyunjianf00e35e2016-05-31 11:52:43 +08003034free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10303035 vi->vdev->config->reset(vdev);
3036
Rusty Russellb3369c12008-02-04 23:50:02 -05003037 unregister_netdev(dev);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003038free_failover:
3039 net_failover_destroy(vi->failover);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003040free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00003041 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08003042 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00003043 virtnet_del_vqs(vi);
Rusty Russell296f96f2007-10-22 11:03:37 +10003044free:
3045 free_netdev(dev);
3046 return err;
3047}
3048
Amit Shah04486ed2011-12-22 16:58:32 +05303049static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10003050{
Amit Shah04486ed2011-12-22 16:58:32 +05303051 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00003052
3053 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00003054 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003055
Jason Wang986a4f42012-12-07 07:04:56 +00003056 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003057
Michael Daltonfb518792014-01-16 22:23:26 -08003058 free_receive_page_frags(vi);
3059
Jason Wang986a4f42012-12-07 07:04:56 +00003060 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05303061}
3062
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003063static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05303064{
3065 struct virtnet_info *vi = vdev->priv;
3066
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003067 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003068
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10303069 /* Make sure no work handler is accessing the device. */
3070 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00003071
Amit Shah04486ed2011-12-22 16:58:32 +05303072 unregister_netdev(vi->dev);
3073
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003074 net_failover_destroy(vi->failover);
3075
Amit Shah04486ed2011-12-22 16:58:32 +05303076 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003077
Rusty Russell74b25532007-11-19 11:20:42 -05003078 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003079}
3080
Arnd Bergmann67a75192017-07-25 17:35:50 +02003081static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303082{
3083 struct virtnet_info *vi = vdev->priv;
3084
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003085 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003086 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303087 remove_vq_common(vi);
3088
3089 return 0;
3090}
3091
Arnd Bergmann67a75192017-07-25 17:35:50 +02003092static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303093{
3094 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003095 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05303096
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003097 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303098 if (err)
3099 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00003100 virtnet_set_queues(vi, vi->curr_queue_pairs);
3101
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003102 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08003103 if (err)
3104 return err;
3105
Amit Shah0741bcb2011-12-22 16:58:33 +05303106 return 0;
3107}
Amit Shah0741bcb2011-12-22 16:58:33 +05303108
Rusty Russell296f96f2007-10-22 11:03:37 +10003109static struct virtio_device_id id_table[] = {
3110 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3111 { 0 },
3112};
3113
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003114#define VIRTNET_FEATURES \
3115 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3116 VIRTIO_NET_F_MAC, \
3117 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3118 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3119 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3120 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3121 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3122 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3123 VIRTIO_NET_F_CTRL_MAC_ADDR, \
Jason Baronfaa9b392018-01-05 17:44:54 -05003124 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
Sridhar Samudrala98050692018-05-24 09:55:16 -07003125 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003126
Rusty Russellc45a6812008-05-02 21:50:50 -05003127static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003128 VIRTNET_FEATURES,
3129};
3130
3131static unsigned int features_legacy[] = {
3132 VIRTNET_FEATURES,
3133 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09303134 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05003135};
3136
Uwe Kleine-König22402522009-11-05 01:32:44 -08003137static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05003138 .feature_table = features,
3139 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003140 .feature_table_legacy = features_legacy,
3141 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10003142 .driver.name = KBUILD_MODNAME,
3143 .driver.owner = THIS_MODULE,
3144 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003145 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10003146 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003147 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003148 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09303149#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05303150 .freeze = virtnet_freeze,
3151 .restore = virtnet_restore,
3152#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10003153};
3154
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003155static __init int virtio_net_driver_init(void)
3156{
3157 int ret;
3158
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003159 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003160 virtnet_cpu_online,
3161 virtnet_cpu_down_prep);
3162 if (ret < 0)
3163 goto out;
3164 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003165 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003166 NULL, virtnet_cpu_dead);
3167 if (ret)
3168 goto err_dead;
3169
3170 ret = register_virtio_driver(&virtio_net_driver);
3171 if (ret)
3172 goto err_virtio;
3173 return 0;
3174err_virtio:
3175 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3176err_dead:
3177 cpuhp_remove_multi_state(virtionet_online);
3178out:
3179 return ret;
3180}
3181module_init(virtio_net_driver_init);
3182
3183static __exit void virtio_net_driver_exit(void)
3184{
Andrew Jonescfa0ebc2017-07-24 15:38:32 +02003185 unregister_virtio_driver(&virtio_net_driver);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003186 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3187 cpuhp_remove_multi_state(virtionet_online);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003188}
3189module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10003190
3191MODULE_DEVICE_TABLE(virtio, id_table);
3192MODULE_DESCRIPTION("Virtio network driver");
3193MODULE_LICENSE("GPL");