blob: 1880c86e84b48d7f2fa20956a6a5938352e5671a [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
Toshiaki Makitaa0929a42018-07-23 23:36:05 +090090struct virtnet_rq_stat_items {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090091 u64 packets;
92 u64 bytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +090093 u64 drops;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090094 u64 xdp_packets;
95 u64 xdp_tx;
96 u64 xdp_redirects;
97 u64 xdp_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +090098 u64 kicks;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090099};
100
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900101struct virtnet_rq_stats {
102 struct u64_stats_sync syncp;
103 struct virtnet_rq_stat_items items;
104};
105
106struct virtnet_rx_stats {
107 struct virtnet_rq_stat_items rx;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900108 struct {
109 unsigned int xdp_tx;
110 unsigned int xdp_tx_drops;
111 } tx;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900112};
113
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900114#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900115#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stat_items, m)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900116
117static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900118 { "packets", VIRTNET_SQ_STAT(packets) },
119 { "bytes", VIRTNET_SQ_STAT(bytes) },
120 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
121 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900122 { "kicks", VIRTNET_SQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900123};
124
125static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900126 { "packets", VIRTNET_RQ_STAT(packets) },
127 { "bytes", VIRTNET_RQ_STAT(bytes) },
128 { "drops", VIRTNET_RQ_STAT(drops) },
129 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
130 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
131 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
132 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900133 { "kicks", VIRTNET_RQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900134};
135
136#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
137#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
138
Jason Wange9d74172012-12-07 07:04:55 +0000139/* Internal representation of a send virtqueue */
140struct send_queue {
141 /* Virtqueue associated with this send _queue */
142 struct virtqueue *vq;
143
144 /* TX: fragments + linear part + virtio header */
145 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000146
147 /* Name of the send queue: output.$index */
148 char name[40];
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400149
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900150 struct virtnet_sq_stats stats;
151
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400152 struct napi_struct napi;
Jason Wange9d74172012-12-07 07:04:55 +0000153};
154
155/* Internal representation of a receive virtqueue */
156struct receive_queue {
157 /* Virtqueue associated with this receive_queue */
158 struct virtqueue *vq;
159
Rusty Russell296f96f2007-10-22 11:03:37 +1000160 struct napi_struct napi;
161
John Fastabendf600b692016-12-15 12:13:24 -0800162 struct bpf_prog __rcu *xdp_prog;
163
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900164 struct virtnet_rq_stats stats;
165
Jason Wange9d74172012-12-07 07:04:55 +0000166 /* Chain pages by the private ptr. */
167 struct page *pages;
168
Michael Daltonab7db912014-01-16 22:23:27 -0800169 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200170 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800171
Michael Daltonfb518792014-01-16 22:23:26 -0800172 /* Page frag for packet buffer allocation. */
173 struct page_frag alloc_frag;
174
Jason Wange9d74172012-12-07 07:04:55 +0000175 /* RX: fragments + linear part + virtio header */
176 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000177
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +0200178 /* Min single buffer size for mergeable buffers case. */
179 unsigned int min_buf_len;
180
Jason Wang986a4f42012-12-07 07:04:56 +0000181 /* Name of this receive queue: input.$index */
182 char name[40];
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100183
184 struct xdp_rxq_info xdp_rxq;
Jason Wange9d74172012-12-07 07:04:55 +0000185};
186
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300187/* Control VQ buffers: protected by the rtnl lock */
188struct control_buf {
189 struct virtio_net_ctrl_hdr hdr;
190 virtio_net_ctrl_ack status;
191 struct virtio_net_ctrl_mq mq;
192 u8 promisc;
193 u8 allmulti;
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +0300194 __virtio16 vid;
Michael S. Tsirkinf4ee7032018-04-19 08:30:50 +0300195 __virtio64 offloads;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300196};
197
Jason Wange9d74172012-12-07 07:04:55 +0000198struct virtnet_info {
199 struct virtio_device *vdev;
200 struct virtqueue *cvq;
201 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000202 struct send_queue *sq;
203 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000204 unsigned int status;
205
Jason Wang986a4f42012-12-07 07:04:56 +0000206 /* Max # of queue pairs supported by the device */
207 u16 max_queue_pairs;
208
209 /* # of queue pairs currently used by the driver */
210 u16 curr_queue_pairs;
211
John Fastabend672aafd2016-12-15 12:13:49 -0800212 /* # of XDP queue pairs currently used by the driver */
213 u16 xdp_queue_pairs;
214
Herbert Xu97402b92008-04-18 11:24:27 +0800215 /* I like... big packets and I cannot lie! */
216 bool big_packets;
217
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800218 /* Host will merge rx buffers for big packets (shake it! shake it!) */
219 bool mergeable_rx_bufs;
220
Jason Wang986a4f42012-12-07 07:04:56 +0000221 /* Has control virtqueue */
222 bool has_cvq;
223
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930224 /* Host can handle any s/g split between our header and packet data */
225 bool any_header_sg;
226
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300227 /* Packet virtio header size */
228 u8 hdr_len;
229
Rusty Russell3161e452009-08-26 12:22:32 -0700230 /* Work struct for refilling if we run low on memory. */
231 struct delayed_work refill;
232
Jason Wang586d17c2012-04-11 20:43:52 +0000233 /* Work struct for config space updates */
234 struct work_struct config_work;
235
Jason Wang986a4f42012-12-07 07:04:56 +0000236 /* Does the affinity hint is set for virtqueues? */
237 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000238
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200239 /* CPU hotplug instances for online & dead */
240 struct hlist_node node;
241 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200242
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300243 struct control_buf *ctrl;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100244
245 /* Ethtool settings */
246 u8 duplex;
247 u32 speed;
Jason Wang3f935222017-07-19 16:54:49 +0800248
249 unsigned long guest_offloads;
Sridhar Samudralaba5e4422018-05-24 09:55:17 -0700250
251 /* failover when STANDBY feature enabled */
252 struct failover *failover;
Rusty Russell296f96f2007-10-22 11:03:37 +1000253};
254
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000255struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300256 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000257 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300258 * hdr is in a separate sg buffer, and data sg buffer shares same page
259 * with this header sg. This padding makes next sg 16 byte aligned
260 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000261 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300262 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000263};
264
Jason Wang986a4f42012-12-07 07:04:56 +0000265/* Converting between virtqueue no. and kernel tx/rx queue no.
266 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
267 */
268static int vq2txq(struct virtqueue *vq)
269{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000270 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000271}
272
273static int txq2vq(int txq)
274{
275 return txq * 2 + 1;
276}
277
278static int vq2rxq(struct virtqueue *vq)
279{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000280 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000281}
282
283static int rxq2vq(int rxq)
284{
285 return rxq * 2;
286}
287
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300288static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000289{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300290 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000291}
292
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000293/*
294 * private is used to chain pages for big packets, put the whole
295 * most recent used list in the beginning for reuse
296 */
Jason Wange9d74172012-12-07 07:04:55 +0000297static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500298{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000299 struct page *end;
300
Jason Wange9d74172012-12-07 07:04:55 +0000301 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000302 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000303 end->private = (unsigned long)rq->pages;
304 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500305}
306
Jason Wange9d74172012-12-07 07:04:55 +0000307static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500308{
Jason Wange9d74172012-12-07 07:04:55 +0000309 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500310
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000311 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000312 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000313 /* clear private here, it is used to chain pages */
314 p->private = 0;
315 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500316 p = alloc_page(gfp_mask);
317 return p;
318}
319
Willem de Bruijne4e84522017-04-24 13:49:26 -0400320static void virtqueue_napi_schedule(struct napi_struct *napi,
321 struct virtqueue *vq)
322{
323 if (napi_schedule_prep(napi)) {
324 virtqueue_disable_cb(vq);
325 __napi_schedule(napi);
326 }
327}
328
329static void virtqueue_napi_complete(struct napi_struct *napi,
330 struct virtqueue *vq, int processed)
331{
332 int opaque;
333
334 opaque = virtqueue_enable_cb_prepare(vq);
Toshiaki Makitafdaa7672017-12-07 13:15:15 +0900335 if (napi_complete_done(napi, processed)) {
336 if (unlikely(virtqueue_poll(vq, opaque)))
337 virtqueue_napi_schedule(napi, vq);
338 } else {
339 virtqueue_disable_cb(vq);
340 }
Willem de Bruijne4e84522017-04-24 13:49:26 -0400341}
342
Jason Wange9d74172012-12-07 07:04:55 +0000343static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000344{
Jason Wange9d74172012-12-07 07:04:55 +0000345 struct virtnet_info *vi = vq->vdev->priv;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400346 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000347
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500348 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000349 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000350
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400351 if (napi->weight)
352 virtqueue_napi_schedule(napi, vq);
353 else
354 /* We were probably waiting for more output buffers. */
355 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000356}
357
Jason Wang28b39bc2017-07-19 16:54:46 +0800358#define MRG_CTX_HEADER_SHIFT 22
359static void *mergeable_len_to_ctx(unsigned int truesize,
360 unsigned int headroom)
361{
362 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
363}
364
365static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
366{
367 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
368}
369
370static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
371{
372 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
373}
374
Mike Waychison34646452012-01-04 12:52:32 +0000375/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300376static struct sk_buff *page_to_skb(struct virtnet_info *vi,
377 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700378 struct page *page, unsigned int offset,
379 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000380{
381 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300382 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700383 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000384 char *p;
385
Michael Dalton2613af02013-10-28 15:44:18 -0700386 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000387
388 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100389 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000390 if (unlikely(!skb))
391 return NULL;
392
393 hdr = skb_vnet_hdr(skb);
394
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300395 hdr_len = vi->hdr_len;
396 if (vi->mergeable_rx_bufs)
stephen hemmingera4a76502017-08-15 10:29:17 -0700397 hdr_padded_len = sizeof(*hdr);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300398 else
Michael Dalton2613af02013-10-28 15:44:18 -0700399 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000400
401 memcpy(hdr, p, hdr_len);
402
403 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700404 offset += hdr_padded_len;
405 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000406
407 copy = len;
408 if (copy > skb_tailroom(skb))
409 copy = skb_tailroom(skb);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200410 skb_put_data(skb, p, copy);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000411
412 len -= copy;
413 offset += copy;
414
Michael Dalton2613af02013-10-28 15:44:18 -0700415 if (vi->mergeable_rx_bufs) {
416 if (len)
417 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
418 else
419 put_page(page);
420 return skb;
421 }
422
Sasha Levine878d782011-09-28 04:40:54 +0000423 /*
424 * Verify that we can indeed put this data into a skb.
425 * This is here to handle cases when the device erroneously
426 * tries to receive more than is possible. This is usually
427 * the case of a broken device.
428 */
429 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000430 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000431 dev_kfree_skb(skb);
432 return NULL;
433 }
Michael Dalton2613af02013-10-28 15:44:18 -0700434 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000435 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700436 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
437 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
438 frag_size, truesize);
439 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000440 page = (struct page *)page->private;
441 offset = 0;
442 }
443
444 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000445 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000446
447 return skb;
448}
449
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200450static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
451 struct send_queue *sq,
452 struct xdp_frame *xdpf)
John Fastabend56434a02016-12-15 12:14:13 -0800453{
John Fastabend56434a02016-12-15 12:14:13 -0800454 struct virtio_net_hdr_mrg_rxbuf *hdr;
John Fastabend56434a02016-12-15 12:14:13 -0800455 int err;
456
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200457 /* virtqueue want to use data area in-front of packet */
458 if (unlikely(xdpf->metasize > 0))
459 return -EOPNOTSUPP;
460
461 if (unlikely(xdpf->headroom < vi->hdr_len))
462 return -EOVERFLOW;
463
464 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
465 xdpf->data -= vi->hdr_len;
Jason Wangf6b10202017-02-21 16:46:28 +0800466 /* Zero header and leave csum up to XDP layers */
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200467 hdr = xdpf->data;
Jason Wangf6b10202017-02-21 16:46:28 +0800468 memset(hdr, 0, vi->hdr_len);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200469 xdpf->len += vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800470
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200471 sg_init_one(sq->sg, xdpf->data, xdpf->len);
Jason Wangbb91acc2016-12-23 22:37:32 +0800472
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200473 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdpf, GFP_ATOMIC);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100474 if (unlikely(err))
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200475 return -ENOSPC; /* Caller handle free/refcnt */
John Fastabend56434a02016-12-15 12:14:13 -0800476
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200477 return 0;
John Fastabend56434a02016-12-15 12:14:13 -0800478}
479
Toshiaki Makita2a435652018-07-23 23:36:07 +0900480static struct send_queue *virtnet_xdp_sq(struct virtnet_info *vi)
481{
482 unsigned int qp;
483
484 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
485 return &vi->sq[qp];
486}
487
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200488static int __virtnet_xdp_tx_xmit(struct virtnet_info *vi,
489 struct xdp_frame *xdpf)
490{
491 struct xdp_frame *xdpf_sent;
492 struct send_queue *sq;
493 unsigned int len;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200494
Toshiaki Makita2a435652018-07-23 23:36:07 +0900495 sq = virtnet_xdp_sq(vi);
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200496
497 /* Free up any pending old buffers before queueing new ones. */
498 while ((xdpf_sent = virtqueue_get_buf(sq->vq, &len)) != NULL)
499 xdp_return_frame(xdpf_sent);
500
501 return __virtnet_xdp_xmit_one(vi, sq, xdpf);
502}
503
504static int virtnet_xdp_xmit(struct net_device *dev,
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +0200505 int n, struct xdp_frame **frames, u32 flags)
Jason Wang186b3c92017-09-19 17:42:43 +0800506{
507 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100508 struct receive_queue *rq = vi->rq;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200509 struct xdp_frame *xdpf_sent;
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100510 struct bpf_prog *xdp_prog;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200511 struct send_queue *sq;
512 unsigned int len;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200513 int drops = 0;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900514 int kicks = 0;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900515 int ret, err;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200516 int i;
517
Toshiaki Makita2a435652018-07-23 23:36:07 +0900518 sq = virtnet_xdp_sq(vi);
Jason Wang186b3c92017-09-19 17:42:43 +0800519
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900520 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
521 ret = -EINVAL;
522 drops = n;
523 goto out;
524 }
525
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100526 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
527 * indicate XDP resources have been successfully allocated.
528 */
529 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900530 if (!xdp_prog) {
531 ret = -ENXIO;
532 drops = n;
533 goto out;
534 }
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100535
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200536 /* Free up any pending old buffers before queueing new ones. */
537 while ((xdpf_sent = virtqueue_get_buf(sq->vq, &len)) != NULL)
538 xdp_return_frame(xdpf_sent);
539
540 for (i = 0; i < n; i++) {
541 struct xdp_frame *xdpf = frames[i];
542
543 err = __virtnet_xdp_xmit_one(vi, sq, xdpf);
544 if (err) {
545 xdp_return_frame_rx_napi(xdpf);
546 drops++;
547 }
548 }
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900549 ret = n - drops;
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200550
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900551 if (flags & XDP_XMIT_FLUSH) {
552 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
553 kicks = 1;
554 }
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900555out:
556 u64_stats_update_begin(&sq->stats.syncp);
557 sq->stats.xdp_tx += n;
558 sq->stats.xdp_tx_drops += drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900559 sq->stats.kicks += kicks;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900560 u64_stats_update_end(&sq->stats.syncp);
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200561
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900562 return ret;
Jason Wang186b3c92017-09-19 17:42:43 +0800563}
564
Jason Wangf6b10202017-02-21 16:46:28 +0800565static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
566{
567 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
568}
569
Jason Wang4941d472017-07-19 16:54:48 +0800570/* We copy the packet for XDP in the following cases:
571 *
572 * 1) Packet is scattered across multiple rx buffers.
573 * 2) Headroom space is insufficient.
574 *
575 * This is inefficient but it's a temporary condition that
576 * we hit right after XDP is enabled and until queue is refilled
577 * with large buffers with sufficient headroom - so it should affect
578 * at most queue size packets.
579 * Afterwards, the conditions to enable
580 * XDP should preclude the underlying device from sending packets
581 * across multiple buffers (num_buf > 1), and we make sure buffers
582 * have enough headroom.
John Fastabend72979a62016-12-15 12:14:36 -0800583 */
584static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800585 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800586 struct page *p,
587 int offset,
Jason Wang4941d472017-07-19 16:54:48 +0800588 int page_off,
John Fastabend72979a62016-12-15 12:14:36 -0800589 unsigned int *len)
590{
591 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend72979a62016-12-15 12:14:36 -0800592
593 if (!page)
594 return NULL;
595
596 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
597 page_off += *len;
598
Jason Wang56a86f82016-12-23 22:37:26 +0800599 while (--*num_buf) {
Jason Wang3cc81a92018-03-02 17:29:14 +0800600 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
John Fastabend72979a62016-12-15 12:14:36 -0800601 unsigned int buflen;
John Fastabend72979a62016-12-15 12:14:36 -0800602 void *buf;
603 int off;
604
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200605 buf = virtqueue_get_buf(rq->vq, &buflen);
606 if (unlikely(!buf))
John Fastabend72979a62016-12-15 12:14:36 -0800607 goto err_buf;
608
John Fastabend72979a62016-12-15 12:14:36 -0800609 p = virt_to_head_page(buf);
610 off = buf - page_address(p);
611
Jason Wang56a86f82016-12-23 22:37:26 +0800612 /* guard against a misconfigured or uncooperative backend that
613 * is sending packet larger than the MTU.
614 */
Jason Wang3cc81a92018-03-02 17:29:14 +0800615 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
Jason Wang56a86f82016-12-23 22:37:26 +0800616 put_page(p);
617 goto err_buf;
618 }
619
John Fastabend72979a62016-12-15 12:14:36 -0800620 memcpy(page_address(page) + page_off,
621 page_address(p) + off, buflen);
622 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800623 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800624 }
625
John Fastabend2de2f7f2017-02-02 19:16:29 -0800626 /* Headroom does not contribute to packet length */
627 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800628 return page;
629err_buf:
630 __free_pages(page, 0);
631 return NULL;
632}
633
Jason Wang4941d472017-07-19 16:54:48 +0800634static struct sk_buff *receive_small(struct net_device *dev,
635 struct virtnet_info *vi,
636 struct receive_queue *rq,
637 void *buf, void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800638 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900639 unsigned int *xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900640 struct virtnet_rx_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800641{
642 struct sk_buff *skb;
643 struct bpf_prog *xdp_prog;
644 unsigned int xdp_headroom = (unsigned long)ctx;
645 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
646 unsigned int headroom = vi->hdr_len + header_offset;
647 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
648 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
649 struct page *page = virt_to_head_page(buf);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100650 unsigned int delta = 0;
Jason Wang4941d472017-07-19 16:54:48 +0800651 struct page *xdp_page;
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100652 int err;
653
Jason Wang4941d472017-07-19 16:54:48 +0800654 len -= vi->hdr_len;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900655 stats->rx.bytes += len;
Jason Wang4941d472017-07-19 16:54:48 +0800656
657 rcu_read_lock();
658 xdp_prog = rcu_dereference(rq->xdp_prog);
659 if (xdp_prog) {
660 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200661 struct xdp_frame *xdpf;
Jason Wang4941d472017-07-19 16:54:48 +0800662 struct xdp_buff xdp;
663 void *orig_data;
664 u32 act;
665
Jesper Dangaard Brouer95dbe9e2018-02-20 14:32:10 +0100666 if (unlikely(hdr->hdr.gso_type))
Jason Wang4941d472017-07-19 16:54:48 +0800667 goto err_xdp;
668
669 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
670 int offset = buf - page_address(page) + header_offset;
671 unsigned int tlen = len + vi->hdr_len;
672 u16 num_buf = 1;
673
674 xdp_headroom = virtnet_get_headroom(vi);
675 header_offset = VIRTNET_RX_PAD + xdp_headroom;
676 headroom = vi->hdr_len + header_offset;
677 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
678 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
679 xdp_page = xdp_linearize_page(rq, &num_buf, page,
680 offset, header_offset,
681 &tlen);
682 if (!xdp_page)
683 goto err_xdp;
684
685 buf = page_address(xdp_page);
686 put_page(page);
687 page = xdp_page;
688 }
689
690 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
691 xdp.data = xdp.data_hard_start + xdp_headroom;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200692 xdp_set_data_meta_invalid(&xdp);
Jason Wang4941d472017-07-19 16:54:48 +0800693 xdp.data_end = xdp.data + len;
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100694 xdp.rxq = &rq->xdp_rxq;
Jason Wang4941d472017-07-19 16:54:48 +0800695 orig_data = xdp.data;
696 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900697 stats->rx.xdp_packets++;
Jason Wang4941d472017-07-19 16:54:48 +0800698
699 switch (act) {
700 case XDP_PASS:
701 /* Recalculate length in case bpf program changed it */
702 delta = orig_data - xdp.data;
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700703 len = xdp.data_end - xdp.data;
Jason Wang4941d472017-07-19 16:54:48 +0800704 break;
705 case XDP_TX:
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900706 stats->rx.xdp_tx++;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200707 xdpf = convert_to_xdp_frame(&xdp);
708 if (unlikely(!xdpf))
709 goto err_xdp;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900710 stats->tx.xdp_tx++;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200711 err = __virtnet_xdp_tx_xmit(vi, xdpf);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200712 if (unlikely(err)) {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900713 stats->tx.xdp_tx_drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800714 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100715 goto err_xdp;
716 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200717 *xdp_xmit |= VIRTIO_XDP_TX;
Jason Wang186b3c92017-09-19 17:42:43 +0800718 rcu_read_unlock();
719 goto xdp_xmit;
720 case XDP_REDIRECT:
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900721 stats->rx.xdp_redirects++;
Jason Wang186b3c92017-09-19 17:42:43 +0800722 err = xdp_do_redirect(dev, &xdp, xdp_prog);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100723 if (err)
724 goto err_xdp;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200725 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang4941d472017-07-19 16:54:48 +0800726 rcu_read_unlock();
727 goto xdp_xmit;
728 default:
729 bpf_warn_invalid_xdp_action(act);
730 case XDP_ABORTED:
731 trace_xdp_exception(vi->dev, xdp_prog, act);
732 case XDP_DROP:
733 goto err_xdp;
734 }
735 }
736 rcu_read_unlock();
737
738 skb = build_skb(buf, buflen);
739 if (!skb) {
740 put_page(page);
741 goto err;
742 }
743 skb_reserve(skb, headroom - delta);
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700744 skb_put(skb, len);
Jason Wang4941d472017-07-19 16:54:48 +0800745 if (!delta) {
746 buf += header_offset;
747 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
748 } /* keep zeroed vnet hdr since packet was changed by bpf */
749
750err:
751 return skb;
752
753err_xdp:
754 rcu_read_unlock();
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900755 stats->rx.xdp_drops++;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +0900756 stats->rx.drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800757 put_page(page);
758xdp_xmit:
759 return NULL;
760}
761
762static struct sk_buff *receive_big(struct net_device *dev,
763 struct virtnet_info *vi,
764 struct receive_queue *rq,
765 void *buf,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900766 unsigned int len,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900767 struct virtnet_rx_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800768{
769 struct page *page = buf;
770 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
771
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900772 stats->rx.bytes += len - vi->hdr_len;
Jason Wang4941d472017-07-19 16:54:48 +0800773 if (unlikely(!skb))
774 goto err;
775
776 return skb;
777
778err:
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +0900779 stats->rx.drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800780 give_pages(rq, page);
781 return NULL;
782}
783
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200784static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200785 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200786 struct receive_queue *rq,
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200787 void *buf,
788 void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800789 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900790 unsigned int *xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900791 struct virtnet_rx_stats *stats)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000792{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300793 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
794 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200795 struct page *page = virt_to_head_page(buf);
796 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800797 struct sk_buff *head_skb, *curr_skb;
798 struct bpf_prog *xdp_prog;
799 unsigned int truesize;
Jason Wang4941d472017-07-19 16:54:48 +0800800 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
Jason Wang3cc81a92018-03-02 17:29:14 +0800801 int err;
Michael Daltonab7db912014-01-16 22:23:27 -0800802
John Fastabend56434a02016-12-15 12:14:13 -0800803 head_skb = NULL;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900804 stats->rx.bytes += len - vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800805
John Fastabendf600b692016-12-15 12:13:24 -0800806 rcu_read_lock();
807 xdp_prog = rcu_dereference(rq->xdp_prog);
808 if (xdp_prog) {
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200809 struct xdp_frame *xdpf;
John Fastabend72979a62016-12-15 12:14:36 -0800810 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800811 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800812 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800813 u32 act;
814
Jason Wang3d62b2a2018-05-22 11:44:31 +0800815 /* Transient failure which in theory could occur if
816 * in-flight packets from before XDP was enabled reach
817 * the receive path after XDP is loaded.
818 */
819 if (unlikely(hdr->hdr.gso_type))
820 goto err_xdp;
821
Jason Wang3cc81a92018-03-02 17:29:14 +0800822 /* This happens when rx buffer size is underestimated
823 * or headroom is not enough because of the buffer
824 * was refilled before XDP is set. This should only
825 * happen for the first several packets, so we don't
826 * care much about its performance.
827 */
Jason Wang4941d472017-07-19 16:54:48 +0800828 if (unlikely(num_buf > 1 ||
829 headroom < virtnet_get_headroom(vi))) {
John Fastabend72979a62016-12-15 12:14:36 -0800830 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800831 xdp_page = xdp_linearize_page(rq, &num_buf,
Jason Wang4941d472017-07-19 16:54:48 +0800832 page, offset,
833 VIRTIO_XDP_HEADROOM,
834 &len);
John Fastabend72979a62016-12-15 12:14:36 -0800835 if (!xdp_page)
836 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800837 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800838 } else {
839 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800840 }
841
John Fastabend2de2f7f2017-02-02 19:16:29 -0800842 /* Allow consuming headroom but reserve enough space to push
843 * the descriptor on if we get an XDP_TX return code.
844 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800845 data = page_address(xdp_page) + offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800846 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
John Fastabend0354e4d2017-02-02 19:15:01 -0800847 xdp.data = data + vi->hdr_len;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200848 xdp_set_data_meta_invalid(&xdp);
John Fastabend0354e4d2017-02-02 19:15:01 -0800849 xdp.data_end = xdp.data + (len - vi->hdr_len);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100850 xdp.rxq = &rq->xdp_rxq;
851
John Fastabend0354e4d2017-02-02 19:15:01 -0800852 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900853 stats->rx.xdp_packets++;
John Fastabend0354e4d2017-02-02 19:15:01 -0800854
John Fastabend56434a02016-12-15 12:14:13 -0800855 switch (act) {
856 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800857 /* recalculate offset to account for any header
858 * adjustments. Note other cases do not build an
859 * skb and avoid using offset
860 */
861 offset = xdp.data -
862 page_address(xdp_page) - vi->hdr_len;
863
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700864 /* recalculate len if xdp.data or xdp.data_end were
865 * adjusted
866 */
Nikita V. Shirokovaaa64522018-04-22 21:16:48 -0700867 len = xdp.data_end - xdp.data + vi->hdr_len;
Jason Wang1830f892016-12-23 22:37:27 +0800868 /* We can only create skb based on xdp_page. */
869 if (unlikely(xdp_page != page)) {
870 rcu_read_unlock();
871 put_page(page);
872 head_skb = page_to_skb(vi, rq, xdp_page,
John Fastabend2de2f7f2017-02-02 19:16:29 -0800873 offset, len, PAGE_SIZE);
Jason Wang1830f892016-12-23 22:37:27 +0800874 return head_skb;
875 }
John Fastabend56434a02016-12-15 12:14:13 -0800876 break;
877 case XDP_TX:
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900878 stats->rx.xdp_tx++;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200879 xdpf = convert_to_xdp_frame(&xdp);
880 if (unlikely(!xdpf))
881 goto err_xdp;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900882 stats->tx.xdp_tx++;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200883 err = __virtnet_xdp_tx_xmit(vi, xdpf);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200884 if (unlikely(err)) {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900885 stats->tx.xdp_tx_drops++;
John Fastabend0354e4d2017-02-02 19:15:01 -0800886 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100887 if (unlikely(xdp_page != page))
888 put_page(xdp_page);
889 goto err_xdp;
890 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200891 *xdp_xmit |= VIRTIO_XDP_TX;
John Fastabend72979a62016-12-15 12:14:36 -0800892 if (unlikely(xdp_page != page))
Jason Wang5d458a12018-05-22 11:44:29 +0800893 put_page(page);
John Fastabend56434a02016-12-15 12:14:13 -0800894 rcu_read_unlock();
895 goto xdp_xmit;
Jason Wang3cc81a92018-03-02 17:29:14 +0800896 case XDP_REDIRECT:
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900897 stats->rx.xdp_redirects++;
Jason Wang3cc81a92018-03-02 17:29:14 +0800898 err = xdp_do_redirect(dev, &xdp, xdp_prog);
899 if (err) {
900 if (unlikely(xdp_page != page))
901 put_page(xdp_page);
902 goto err_xdp;
903 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200904 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang3cc81a92018-03-02 17:29:14 +0800905 if (unlikely(xdp_page != page))
Jason Wang68904182018-05-22 11:44:28 +0800906 put_page(page);
Jason Wang3cc81a92018-03-02 17:29:14 +0800907 rcu_read_unlock();
908 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -0800909 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800910 bpf_warn_invalid_xdp_action(act);
911 case XDP_ABORTED:
912 trace_xdp_exception(vi->dev, xdp_prog, act);
913 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -0800914 if (unlikely(xdp_page != page))
915 __free_pages(xdp_page, 0);
John Fastabendf600b692016-12-15 12:13:24 -0800916 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800917 }
John Fastabendf600b692016-12-15 12:13:24 -0800918 }
919 rcu_read_unlock();
920
Jason Wang28b39bc2017-07-19 16:54:46 +0800921 truesize = mergeable_ctx_to_truesize(ctx);
922 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +0300923 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200924 dev->name, len, (unsigned long)ctx);
925 dev->stats.rx_length_errors++;
926 goto err_skb;
927 }
Jason Wang28b39bc2017-07-19 16:54:46 +0800928
John Fastabendf600b692016-12-15 12:13:24 -0800929 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
930 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000931
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200932 if (unlikely(!curr_skb))
933 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000934 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200935 int num_skb_frags;
936
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200937 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
Yunjian Wang03e9f8a2017-12-04 14:02:19 +0800938 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200939 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200940 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300941 virtio16_to_cpu(vi->vdev,
942 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200943 dev->stats.rx_length_errors++;
944 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000945 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200946
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900947 stats->rx.bytes += len;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200948 page = virt_to_head_page(buf);
Jason Wang28b39bc2017-07-19 16:54:46 +0800949
950 truesize = mergeable_ctx_to_truesize(ctx);
951 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +0300952 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200953 dev->name, len, (unsigned long)ctx);
954 dev->stats.rx_length_errors++;
955 goto err_skb;
956 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200957
958 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700959 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
960 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200961
962 if (unlikely(!nskb))
963 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700964 if (curr_skb == head_skb)
965 skb_shinfo(curr_skb)->frag_list = nskb;
966 else
967 curr_skb->next = nskb;
968 curr_skb = nskb;
969 head_skb->truesize += nskb->truesize;
970 num_skb_frags = 0;
971 }
972 if (curr_skb != head_skb) {
973 head_skb->data_len += len;
974 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800975 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700976 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200977 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800978 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
979 put_page(page);
980 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800981 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800982 } else {
983 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800984 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800985 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200986 }
987
Johannes Berg5377d7582015-08-19 09:48:40 +0200988 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200989 return head_skb;
990
John Fastabendf600b692016-12-15 12:13:24 -0800991err_xdp:
992 rcu_read_unlock();
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900993 stats->rx.xdp_drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200994err_skb:
995 put_page(page);
Jason Wang850e0882018-05-22 11:44:30 +0800996 while (num_buf-- > 1) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200997 buf = virtqueue_get_buf(rq->vq, &len);
998 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200999 pr_debug("%s: rx error: %d buffers missing\n",
1000 dev->name, num_buf);
1001 dev->stats.rx_length_errors++;
1002 break;
1003 }
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001004 stats->rx.bytes += len;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001005 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001006 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001007 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001008err_buf:
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001009 stats->rx.drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001010 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -08001011xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001012 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001013}
1014
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001015static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1016 void *buf, unsigned int len, void **ctx,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001017 unsigned int *xdp_xmit,
1018 struct virtnet_rx_stats *stats)
Rusty Russell296f96f2007-10-22 11:03:37 +10001019{
Jason Wange9d74172012-12-07 07:04:55 +00001020 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001021 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001022 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001023
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +03001024 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001025 pr_debug("%s: short packet %i\n", dev->name, len);
1026 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -08001027 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001028 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001029 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -08001030 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001031 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08001032 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001033 }
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001034 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001035 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001036
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001037 if (vi->mergeable_rx_bufs)
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001038 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001039 stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001040 else if (vi->big_packets)
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001041 skb = receive_big(dev, vi, rq, buf, len, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001042 else
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001043 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001044
1045 if (unlikely(!skb))
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001046 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001047
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001048 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001049
Mike Rapoporte858fae2016-06-08 16:09:21 +03001050 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +00001051 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +10001052
Mike Rapoporte858fae2016-06-08 16:09:21 +03001053 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1054 virtio_is_little_endian(vi->vdev))) {
1055 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1056 dev->name, hdr->hdr.gso_type,
1057 hdr->hdr.gso_size);
1058 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001059 }
1060
Mike Rapoportd1dc06d2016-06-14 08:29:38 +03001061 skb->protocol = eth_type_trans(skb, dev);
1062 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1063 ntohs(skb->protocol), skb->len, skb->pkt_type);
1064
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001065 napi_gro_receive(&rq->napi, skb);
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001066 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001067
1068frame_err:
1069 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +10001070 dev_kfree_skb(skb);
1071}
1072
Jason Wang192f68c2017-07-19 16:54:47 +08001073/* Unlike mergeable buffers, all buffers are allocated to the
1074 * same size, except for the headroom. For this reason we do
1075 * not need to use mergeable_len_to_ctx here - it is enough
1076 * to store the headroom as the context ignoring the truesize.
1077 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001078static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1079 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +10001080{
Jason Wangf6b10202017-02-21 16:46:28 +08001081 struct page_frag *alloc_frag = &rq->alloc_frag;
1082 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001083 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wang192f68c2017-07-19 16:54:47 +08001084 void *ctx = (void *)(unsigned long)xdp_headroom;
Jason Wangf6b10202017-02-21 16:46:28 +08001085 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001086 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001087
Jason Wangf6b10202017-02-21 16:46:28 +08001088 len = SKB_DATA_ALIGN(len) +
1089 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1090 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001091 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001092
Jason Wangf6b10202017-02-21 16:46:28 +08001093 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1094 get_page(alloc_frag->page);
1095 alloc_frag->offset += len;
1096 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1097 vi->hdr_len + GOOD_PACKET_LEN);
Jason Wang192f68c2017-07-19 16:54:47 +08001098 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001099 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +08001100 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001101 return err;
1102}
1103
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001104static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1105 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001106{
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001107 struct page *first, *list = NULL;
1108 char *p;
1109 int i, err, offset;
1110
Rusty Russella5835442014-09-11 10:17:36 +09301111 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1112
Jason Wange9d74172012-12-07 07:04:55 +00001113 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001114 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +00001115 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001116 if (!first) {
1117 if (list)
Jason Wange9d74172012-12-07 07:04:55 +00001118 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001119 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -07001120 }
Jason Wange9d74172012-12-07 07:04:55 +00001121 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +10001122
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001123 /* chain new page in list head to match sg */
1124 first->private = (unsigned long)list;
1125 list = first;
1126 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001127
Jason Wange9d74172012-12-07 07:04:55 +00001128 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001129 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +00001130 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001131 return -ENOMEM;
1132 }
1133 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +08001134
Jason Wange9d74172012-12-07 07:04:55 +00001135 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001136 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1137 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +08001138
Jason Wange9d74172012-12-07 07:04:55 +00001139 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001140 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +00001141 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +08001142
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001143 /* chain first in list head */
1144 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301145 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1146 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001147 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +00001148 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +08001149
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001150 return err;
1151}
Herbert Xu97402b92008-04-18 11:24:27 +08001152
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02001153static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
Jason Wang3cc81a92018-03-02 17:29:14 +08001154 struct ewma_pkt_len *avg_pkt_len,
1155 unsigned int room)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001156{
Michael Daltonab7db912014-01-16 22:23:27 -08001157 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001158 unsigned int len;
1159
Jason Wang3cc81a92018-03-02 17:29:14 +08001160 if (room)
1161 return PAGE_SIZE - room;
1162
1163 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03001164 rq->min_buf_len, PAGE_SIZE - hdr_len);
Jason Wang3cc81a92018-03-02 17:29:14 +08001165
Michael S. Tsirkine377fcc2017-03-06 22:21:35 +02001166 return ALIGN(len, L1_CACHE_BYTES);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001167}
1168
John Fastabend2de2f7f2017-02-02 19:16:29 -08001169static int add_recvbuf_mergeable(struct virtnet_info *vi,
1170 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -08001171{
Michael Daltonfb518792014-01-16 22:23:26 -08001172 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001173 unsigned int headroom = virtnet_get_headroom(vi);
Jason Wang3cc81a92018-03-02 17:29:14 +08001174 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1175 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
Michael Daltonfb518792014-01-16 22:23:26 -08001176 char *buf;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001177 void *ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001178 int err;
Michael Daltonfb518792014-01-16 22:23:26 -08001179 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +10001180
Jason Wang3cc81a92018-03-02 17:29:14 +08001181 /* Extra tailroom is needed to satisfy XDP's assumption. This
1182 * means rx frags coalescing won't work, but consider we've
1183 * disabled GSO for XDP, it won't be a big issue.
1184 */
1185 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1186 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001187 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -08001188
Michael Daltonfb518792014-01-16 22:23:26 -08001189 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001190 buf += headroom; /* advance address leaving hole at front of pkt */
Michael Daltonfb518792014-01-16 22:23:26 -08001191 get_page(alloc_frag->page);
Jason Wang3cc81a92018-03-02 17:29:14 +08001192 alloc_frag->offset += len + room;
Michael Daltonfb518792014-01-16 22:23:26 -08001193 hole = alloc_frag->size - alloc_frag->offset;
Jason Wang3cc81a92018-03-02 17:29:14 +08001194 if (hole < len + room) {
Michael Daltonab7db912014-01-16 22:23:27 -08001195 /* To avoid internal fragmentation, if there is very likely not
1196 * enough space for another buffer, add the remaining space to
Michael S. Tsirkin1daa8792017-07-31 21:49:49 +03001197 * the current buffer.
Michael Daltonab7db912014-01-16 22:23:27 -08001198 */
Michael Daltonfb518792014-01-16 22:23:26 -08001199 len += hole;
1200 alloc_frag->offset += hole;
1201 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001202
Michael Daltonfb518792014-01-16 22:23:26 -08001203 sg_init_one(rq->sg, buf, len);
David S. Miller29fda252017-08-01 10:07:50 -07001204 ctx = mergeable_len_to_ctx(len, headroom);
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001205 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001206 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -07001207 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001208
1209 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001210}
1211
Rusty Russellb2baed62011-12-29 00:42:38 +00001212/*
1213 * Returns false if we couldn't fill entirely (OOM).
1214 *
1215 * Normally run in the receive path, but can also be run from ndo_open
1216 * before we're receiving packets, or from refill_work which is
1217 * careful to disable receiving (using napi_disable).
1218 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001219static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1220 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001221{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001222 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001223 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001224
Amit Shah0aea51c2009-08-26 14:58:28 +05301225 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001226 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001227 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001228 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001229 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001230 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001231 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001232
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001233 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301234 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001235 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001236 } while (rq->vq->num_free);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001237 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1238 u64_stats_update_begin(&rq->stats.syncp);
1239 rq->stats.items.kicks++;
1240 u64_stats_update_end(&rq->stats.syncp);
1241 }
1242
Rusty Russell3161e452009-08-26 12:22:32 -07001243 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001244}
1245
Rusty Russell18445c42008-02-04 23:49:57 -05001246static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001247{
1248 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001249 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +00001250
Willem de Bruijne4e84522017-04-24 13:49:26 -04001251 virtqueue_napi_schedule(&rq->napi, rvq);
Rusty Russell296f96f2007-10-22 11:03:37 +10001252}
1253
Willem de Bruijne4e84522017-04-24 13:49:26 -04001254static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001255{
Willem de Bruijne4e84522017-04-24 13:49:26 -04001256 napi_enable(napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001257
1258 /* If all buffers were filled by other side before we napi_enabled, we
Willem de Bruijne4e84522017-04-24 13:49:26 -04001259 * won't get another interrupt, so process any outstanding packets now.
1260 * Call local_bh_enable after to trigger softIRQ processing.
1261 */
1262 local_bh_disable();
1263 virtqueue_napi_schedule(napi, vq);
1264 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001265}
1266
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001267static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1268 struct virtqueue *vq,
1269 struct napi_struct *napi)
1270{
1271 if (!napi->weight)
1272 return;
1273
1274 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1275 * enable the feature if this is likely affine with the transmit path.
1276 */
1277 if (!vi->affinity_hint_set) {
1278 napi->weight = 0;
1279 return;
1280 }
1281
1282 return virtnet_napi_enable(vq, napi);
1283}
1284
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001285static void virtnet_napi_tx_disable(struct napi_struct *napi)
1286{
1287 if (napi->weight)
1288 napi_disable(napi);
1289}
1290
Rusty Russell3161e452009-08-26 12:22:32 -07001291static void refill_work(struct work_struct *work)
1292{
Jason Wange9d74172012-12-07 07:04:55 +00001293 struct virtnet_info *vi =
1294 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -07001295 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +00001296 int i;
Rusty Russell3161e452009-08-26 12:22:32 -07001297
Sasha Levin55257d72013-04-29 12:00:08 +09301298 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001299 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -07001300
Jason Wang986a4f42012-12-07 07:04:56 +00001301 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001302 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001303 virtnet_napi_enable(rq->vq, &rq->napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001304
1305 /* In theory, this can happen: if we don't get any buffers in
1306 * we will *never* try to fill again.
1307 */
1308 if (still_empty)
1309 schedule_delayed_work(&vi->refill, HZ/2);
1310 }
Rusty Russell3161e452009-08-26 12:22:32 -07001311}
1312
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001313static int virtnet_receive(struct receive_queue *rq, int budget,
1314 unsigned int *xdp_xmit)
Rusty Russell296f96f2007-10-22 11:03:37 +10001315{
Jason Wange9d74172012-12-07 07:04:55 +00001316 struct virtnet_info *vi = rq->vq->vdev->priv;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001317 struct virtnet_rx_stats stats = {};
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +09001318 struct send_queue *sq;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001319 unsigned int len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001320 void *buf;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001321 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001322
Jason Wang192f68c2017-07-19 16:54:47 +08001323 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001324 void *ctx;
1325
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001326 while (stats.rx.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001327 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001328 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1329 stats.rx.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001330 }
1331 } else {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001332 while (stats.rx.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001333 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001334 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1335 stats.rx.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001336 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001337 }
1338
Jason Wangbe121f42014-01-16 14:45:24 +08001339 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001340 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001341 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -07001342 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001343
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001344 u64_stats_update_begin(&rq->stats.syncp);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001345 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1346 size_t offset = virtnet_rq_stats_desc[i].offset;
1347 u64 *item;
1348
1349 item = (u64 *)((u8 *)&rq->stats.items + offset);
1350 *item += *(u64 *)((u8 *)&stats.rx + offset);
1351 }
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001352 u64_stats_update_end(&rq->stats.syncp);
Jason Wang61845d22017-02-17 11:33:09 +08001353
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +09001354 sq = virtnet_xdp_sq(vi);
1355 u64_stats_update_begin(&sq->stats.syncp);
1356 sq->stats.xdp_tx += stats.tx.xdp_tx;
1357 sq->stats.xdp_tx_drops += stats.tx.xdp_tx_drops;
1358 u64_stats_update_end(&sq->stats.syncp);
1359
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001360 return stats.rx.packets;
Jason Wang2ffa7592014-07-23 16:33:54 +08001361}
1362
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001363static void free_old_xmit_skbs(struct send_queue *sq)
1364{
1365 struct sk_buff *skb;
1366 unsigned int len;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001367 unsigned int packets = 0;
1368 unsigned int bytes = 0;
1369
1370 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1371 pr_debug("Sent skb %p\n", skb);
1372
1373 bytes += skb->len;
1374 packets++;
1375
Eric Dumazetdadc0732017-08-24 09:02:49 -07001376 dev_consume_skb_any(skb);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001377 }
1378
1379 /* Avoid overhead when no packets have been processed
1380 * happens when called speculatively from start_xmit.
1381 */
1382 if (!packets)
1383 return;
1384
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001385 u64_stats_update_begin(&sq->stats.syncp);
1386 sq->stats.bytes += bytes;
1387 sq->stats.packets += packets;
1388 u64_stats_update_end(&sq->stats.syncp);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001389}
1390
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001391static void virtnet_poll_cleantx(struct receive_queue *rq)
1392{
1393 struct virtnet_info *vi = rq->vq->vdev->priv;
1394 unsigned int index = vq2rxq(rq->vq);
1395 struct send_queue *sq = &vi->sq[index];
1396 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1397
1398 if (!sq->napi.weight)
1399 return;
1400
1401 if (__netif_tx_trylock(txq)) {
1402 free_old_xmit_skbs(sq);
1403 __netif_tx_unlock(txq);
1404 }
1405
1406 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1407 netif_tx_wake_queue(txq);
1408}
1409
Jason Wang2ffa7592014-07-23 16:33:54 +08001410static int virtnet_poll(struct napi_struct *napi, int budget)
1411{
1412 struct receive_queue *rq =
1413 container_of(napi, struct receive_queue, napi);
Jason Wang9267c432018-04-13 14:58:25 +08001414 struct virtnet_info *vi = rq->vq->vdev->priv;
1415 struct send_queue *sq;
Toshiaki Makita2a435652018-07-23 23:36:07 +09001416 unsigned int received;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001417 unsigned int xdp_xmit = 0;
Jason Wang2ffa7592014-07-23 16:33:54 +08001418
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001419 virtnet_poll_cleantx(rq);
1420
Jason Wang186b3c92017-09-19 17:42:43 +08001421 received = virtnet_receive(rq, budget, &xdp_xmit);
Jason Wang2ffa7592014-07-23 16:33:54 +08001422
Rusty Russell8329d982007-11-19 11:20:43 -05001423 /* Out of packets? */
Willem de Bruijne4e84522017-04-24 13:49:26 -04001424 if (received < budget)
1425 virtqueue_napi_complete(napi, rq->vq, received);
Rusty Russell296f96f2007-10-22 11:03:37 +10001426
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001427 if (xdp_xmit & VIRTIO_XDP_REDIR)
1428 xdp_do_flush_map();
1429
1430 if (xdp_xmit & VIRTIO_XDP_TX) {
Toshiaki Makita2a435652018-07-23 23:36:07 +09001431 sq = virtnet_xdp_sq(vi);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001432 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1433 u64_stats_update_begin(&sq->stats.syncp);
1434 sq->stats.kicks++;
1435 u64_stats_update_end(&sq->stats.syncp);
1436 }
Jason Wang9267c432018-04-13 14:58:25 +08001437 }
Jason Wang186b3c92017-09-19 17:42:43 +08001438
Rusty Russell296f96f2007-10-22 11:03:37 +10001439 return received;
1440}
1441
Jason Wang986a4f42012-12-07 07:04:56 +00001442static int virtnet_open(struct net_device *dev)
1443{
1444 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001445 int i, err;
Jason Wang986a4f42012-12-07 07:04:56 +00001446
Jason Wange4166622013-05-21 20:03:58 +00001447 for (i = 0; i < vi->max_queue_pairs; i++) {
1448 if (i < vi->curr_queue_pairs)
1449 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001450 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001451 schedule_delayed_work(&vi->refill, 0);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001452
1453 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i);
1454 if (err < 0)
1455 return err;
1456
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +02001457 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1458 MEM_TYPE_PAGE_SHARED, NULL);
1459 if (err < 0) {
1460 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1461 return err;
1462 }
1463
Willem de Bruijne4e84522017-04-24 13:49:26 -04001464 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001465 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001466 }
1467
1468 return 0;
1469}
1470
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001471static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1472{
1473 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1474 struct virtnet_info *vi = sq->vq->vdev->priv;
1475 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
1476
1477 __netif_tx_lock(txq, raw_smp_processor_id());
1478 free_old_xmit_skbs(sq);
1479 __netif_tx_unlock(txq);
1480
1481 virtqueue_napi_complete(napi, sq->vq, 0);
1482
1483 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1484 netif_tx_wake_queue(txq);
1485
1486 return 0;
1487}
1488
Jason Wange9d74172012-12-07 07:04:55 +00001489static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001490{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001491 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001492 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001493 struct virtnet_info *vi = sq->vq->vdev->priv;
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001494 int num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001495 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301496 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001497
Johannes Berge1749612008-10-27 15:59:26 -07001498 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301499
1500 can_push = vi->any_header_sg &&
1501 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1502 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1503 /* Even if we can, don't push here yet as this would skew
1504 * csum_start offset below. */
1505 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001506 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301507 else
1508 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001509
Mike Rapoporte858fae2016-06-08 16:09:21 +03001510 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04001511 virtio_is_little_endian(vi->vdev), false,
1512 0))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001513 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001514
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001515 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001516 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001517
Jason Wang547c8902015-08-27 14:53:06 +08001518 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301519 if (can_push) {
1520 __skb_push(skb, hdr_len);
1521 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001522 if (unlikely(num_sg < 0))
1523 return num_sg;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301524 /* Pull header back to avoid skew in tx bytes calculations. */
1525 __skb_pull(skb, hdr_len);
1526 } else {
1527 sg_set_buf(sq->sg, hdr, hdr_len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001528 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1529 if (unlikely(num_sg < 0))
1530 return num_sg;
1531 num_sg++;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301532 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301533 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001534}
1535
Stephen Hemminger424efe92009-08-31 19:50:51 +00001536static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001537{
1538 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001539 int qnum = skb_get_queue_mapping(skb);
1540 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301541 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001542 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1543 bool kick = !skb->xmit_more;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001544 bool use_napi = sq->napi.weight;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001545
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001546 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001547 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001548
Willem de Bruijnbdb12e02017-04-24 13:49:30 -04001549 if (use_napi && kick)
1550 virtqueue_enable_cb_delayed(sq->vq);
1551
Jacob Keller074c3582014-06-25 02:37:13 +00001552 /* timestamp packet in software */
1553 skb_tx_timestamp(skb);
1554
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001555 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001556 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001557
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301558 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001559 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301560 dev->stats.tx_fifo_errors++;
1561 if (net_ratelimit())
1562 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001563 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001564 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001565 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001566 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001567 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001568
Rusty Russell48925e32009-09-24 09:59:20 -06001569 /* Don't wait up for transmitted skbs to be freed. */
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001570 if (!use_napi) {
1571 skb_orphan(skb);
1572 nf_reset(skb);
1573 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001574
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001575 /* If running out of space, stop queue to avoid getting packets that we
1576 * are then unable to transmit.
1577 * An alternative would be to force queuing layer to requeue the skb by
1578 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1579 * returned in a normal path of operation: it means that driver is not
1580 * maintaining the TX queue stop/start state properly, and causes
1581 * the stack to do a non-trivial amount of useless work.
1582 * Since most packets only take 1 or 2 ring slots, stopping the queue
1583 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001584 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001585 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001586 netif_stop_subqueue(dev, qnum);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001587 if (!use_napi &&
1588 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001589 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001590 free_old_xmit_skbs(sq);
1591 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001592 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001593 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001594 }
1595 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001596 }
Rusty Russell48925e32009-09-24 09:59:20 -06001597
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001598 if (kick || netif_xmit_stopped(txq)) {
1599 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1600 u64_stats_update_begin(&sq->stats.syncp);
1601 sq->stats.kicks++;
1602 u64_stats_update_end(&sq->stats.syncp);
1603 }
1604 }
David S. Miller0b725a22014-08-25 15:51:53 -07001605
Rusty Russell48925e32009-09-24 09:59:20 -06001606 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001607}
1608
Amos Kong40cbfc32013-01-21 01:17:21 +00001609/*
1610 * Send command via the control virtqueue and check status. Commands
1611 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001612 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001613 */
1614static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001615 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001616{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301617 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001618 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001619
1620 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301621 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001622
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001623 vi->ctrl->status = ~0;
1624 vi->ctrl->hdr.class = class;
1625 vi->ctrl->hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301626 /* Add header */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001627 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301628 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001629
Rusty Russellf7bc9592013-03-20 15:44:28 +10301630 if (out)
1631 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001632
Rusty Russellf7bc9592013-03-20 15:44:28 +10301633 /* Add return status. */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001634 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001635 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001636
stephen hemmingerd24bae32013-12-09 16:17:40 -08001637 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301638 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001639
Heinz Graalfs67975902013-10-29 09:40:02 +10301640 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001641 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001642
1643 /* Spin for a response, the kick causes an ioport write, trapping
1644 * into the hypervisor, so the request should be handled immediately.
1645 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301646 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1647 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001648 cpu_relax();
1649
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001650 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001651}
1652
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001653static int virtnet_set_mac_address(struct net_device *dev, void *p)
1654{
1655 struct virtnet_info *vi = netdev_priv(dev);
1656 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001657 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001658 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001659 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001660
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07001661 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1662 return -EOPNOTSUPP;
1663
Shyam Saini801822d2016-12-24 00:44:58 +05301664 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001665 if (!addr)
1666 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001667
1668 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001669 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001670 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001671
Amos Kong7e58d5a2013-01-21 01:17:23 +00001672 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1673 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1674 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001675 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001676 dev_warn(&vdev->dev,
1677 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001678 ret = -EINVAL;
1679 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001680 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001681 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1682 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301683 unsigned int i;
1684
1685 /* Naturally, this has an atomicity problem. */
1686 for (i = 0; i < dev->addr_len; i++)
1687 virtio_cwrite8(vdev,
1688 offsetof(struct virtio_net_config, mac) +
1689 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001690 }
1691
1692 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001693 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001694
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001695out:
1696 kfree(addr);
1697 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001698}
1699
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001700static void virtnet_stats(struct net_device *dev,
1701 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001702{
1703 struct virtnet_info *vi = netdev_priv(dev);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001704 unsigned int start;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001705 int i;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001706
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001707 for (i = 0; i < vi->max_queue_pairs; i++) {
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001708 u64 tpackets, tbytes, rpackets, rbytes, rdrops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001709 struct receive_queue *rq = &vi->rq[i];
1710 struct send_queue *sq = &vi->sq[i];
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001711
1712 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001713 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1714 tpackets = sq->stats.packets;
1715 tbytes = sq->stats.bytes;
1716 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001717
1718 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001719 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001720 rpackets = rq->stats.items.packets;
1721 rbytes = rq->stats.items.bytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001722 rdrops = rq->stats.items.drops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001723 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001724
1725 tot->rx_packets += rpackets;
1726 tot->tx_packets += tpackets;
1727 tot->rx_bytes += rbytes;
1728 tot->tx_bytes += tbytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001729 tot->rx_dropped += rdrops;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001730 }
1731
1732 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001733 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001734 tot->rx_length_errors = dev->stats.rx_length_errors;
1735 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001736}
1737
Amit Shahda74e892008-02-29 16:24:50 +05301738#ifdef CONFIG_NET_POLL_CONTROLLER
1739static void virtnet_netpoll(struct net_device *dev)
1740{
1741 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001742 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301743
Jason Wang986a4f42012-12-07 07:04:56 +00001744 for (i = 0; i < vi->curr_queue_pairs; i++)
1745 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301746}
1747#endif
1748
Jason Wang586d17c2012-04-11 20:43:52 +00001749static void virtnet_ack_link_announce(struct virtnet_info *vi)
1750{
1751 rtnl_lock();
1752 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001753 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001754 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1755 rtnl_unlock();
1756}
1757
John Fastabend473153292017-02-02 19:14:32 -08001758static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001759{
1760 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001761 struct net_device *dev = vi->dev;
1762
1763 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1764 return 0;
1765
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001766 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1767 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001768
1769 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001770 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001771 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1772 queue_pairs);
1773 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301774 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001775 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001776 /* virtnet_open() will refill when device is going to up. */
1777 if (dev->flags & IFF_UP)
1778 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301779 }
Jason Wang986a4f42012-12-07 07:04:56 +00001780
1781 return 0;
1782}
1783
John Fastabend473153292017-02-02 19:14:32 -08001784static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1785{
1786 int err;
1787
1788 rtnl_lock();
1789 err = _virtnet_set_queues(vi, queue_pairs);
1790 rtnl_unlock();
1791 return err;
1792}
1793
Rusty Russell296f96f2007-10-22 11:03:37 +10001794static int virtnet_close(struct net_device *dev)
1795{
1796 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001797 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001798
Rusty Russellb2baed62011-12-29 00:42:38 +00001799 /* Make sure refill_work doesn't re-enable napi! */
1800 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001801
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001802 for (i = 0; i < vi->max_queue_pairs; i++) {
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001803 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
Jason Wang986a4f42012-12-07 07:04:56 +00001804 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001805 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001806 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001807
Rusty Russell296f96f2007-10-22 11:03:37 +10001808 return 0;
1809}
1810
Alex Williamson2af76982009-02-04 09:02:40 +00001811static void virtnet_set_rx_mode(struct net_device *dev)
1812{
1813 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001814 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001815 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001816 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001817 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001818 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001819 void *buf;
1820 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001821
stephen hemminger788a8b62013-12-09 16:18:45 -08001822 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001823 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1824 return;
1825
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001826 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1827 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001828
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001829 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001830
1831 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001832 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001833 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001834 vi->ctrl->promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001835
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001836 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001837
1838 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001839 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001840 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001841 vi->ctrl->allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001842
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001843 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001844 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001845 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001846 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1847 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1848 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001849 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001850 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001851
Alex Williamson23e258e2009-05-01 17:27:56 +00001852 sg_init_table(sg, 2);
1853
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001854 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001855 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001856 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001857 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001858 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001859
1860 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001861 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001862
1863 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001864 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001865
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001866 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001867 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001868 netdev_for_each_mc_addr(ha, dev)
1869 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001870
1871 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001872 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001873
1874 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001875 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001876 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001877
1878 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001879}
1880
Patrick McHardy80d5c362013-04-19 02:04:28 +00001881static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1882 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001883{
1884 struct virtnet_info *vi = netdev_priv(dev);
1885 struct scatterlist sg;
1886
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03001887 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001888 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001889
1890 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001891 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001892 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001893 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001894}
1895
Patrick McHardy80d5c362013-04-19 02:04:28 +00001896static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1897 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001898{
1899 struct virtnet_info *vi = netdev_priv(dev);
1900 struct scatterlist sg;
1901
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03001902 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001903 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001904
1905 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001906 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001907 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001908 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001909}
1910
Wanlong Gao8898c212013-01-24 23:51:30 +00001911static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001912{
1913 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001914
1915 if (vi->affinity_hint_set) {
1916 for (i = 0; i < vi->max_queue_pairs; i++) {
1917 virtqueue_set_affinity(vi->rq[i].vq, -1);
1918 virtqueue_set_affinity(vi->sq[i].vq, -1);
1919 }
1920
1921 vi->affinity_hint_set = false;
1922 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001923}
1924
1925static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001926{
1927 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001928 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001929
1930 /* In multiqueue mode, when the number of cpu is equal to the number of
1931 * queue pairs, we let the queue pairs to be private to one cpu by
1932 * setting the affinity hint to eliminate the contention.
1933 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001934 if (vi->curr_queue_pairs == 1 ||
1935 vi->max_queue_pairs != num_online_cpus()) {
1936 virtnet_clean_affinity(vi, -1);
1937 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001938 }
1939
Wanlong Gao8898c212013-01-24 23:51:30 +00001940 i = 0;
1941 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001942 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1943 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001944 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001945 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001946 }
1947
Wanlong Gao8898c212013-01-24 23:51:30 +00001948 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001949}
1950
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001951static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001952{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001953 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1954 node);
1955 virtnet_set_affinity(vi);
1956 return 0;
1957}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001958
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001959static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1960{
1961 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1962 node_dead);
1963 virtnet_set_affinity(vi);
1964 return 0;
1965}
Jason Wang3ab098d2013-10-15 11:18:58 +08001966
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001967static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1968{
1969 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1970 node);
1971
1972 virtnet_clean_affinity(vi, cpu);
1973 return 0;
1974}
1975
1976static enum cpuhp_state virtionet_online;
1977
1978static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1979{
1980 int ret;
1981
1982 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1983 if (ret)
1984 return ret;
1985 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1986 &vi->node_dead);
1987 if (!ret)
1988 return ret;
1989 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1990 return ret;
1991}
1992
1993static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1994{
1995 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1996 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1997 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001998}
1999
Rick Jones8f9f4662011-10-19 08:10:59 +00002000static void virtnet_get_ringparam(struct net_device *dev,
2001 struct ethtool_ringparam *ring)
2002{
2003 struct virtnet_info *vi = netdev_priv(dev);
2004
Jason Wang986a4f42012-12-07 07:04:56 +00002005 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2006 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00002007 ring->rx_pending = ring->rx_max_pending;
2008 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00002009}
2010
Rick Jones66846042011-11-14 14:17:08 +00002011
2012static void virtnet_get_drvinfo(struct net_device *dev,
2013 struct ethtool_drvinfo *info)
2014{
2015 struct virtnet_info *vi = netdev_priv(dev);
2016 struct virtio_device *vdev = vi->vdev;
2017
2018 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2019 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2020 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2021
2022}
2023
Jason Wangd73bcd22012-12-07 07:04:57 +00002024/* TODO: Eliminate OOO packets during switching */
2025static int virtnet_set_channels(struct net_device *dev,
2026 struct ethtool_channels *channels)
2027{
2028 struct virtnet_info *vi = netdev_priv(dev);
2029 u16 queue_pairs = channels->combined_count;
2030 int err;
2031
2032 /* We don't support separate rx/tx channels.
2033 * We don't allow setting 'other' channels.
2034 */
2035 if (channels->rx_count || channels->tx_count || channels->other_count)
2036 return -EINVAL;
2037
Amos Kongc18e9cd2014-04-18 13:45:41 +08002038 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00002039 return -EINVAL;
2040
John Fastabendf600b692016-12-15 12:13:24 -08002041 /* For now we don't support modifying channels while XDP is loaded
2042 * also when XDP is loaded all RX queues have XDP programs so we only
2043 * need to check a single RX queue.
2044 */
2045 if (vi->rq[0].xdp_prog)
2046 return -EINVAL;
2047
Wanlong Gao47be2472013-01-24 23:51:29 +00002048 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08002049 err = _virtnet_set_queues(vi, queue_pairs);
Jason Wangd73bcd22012-12-07 07:04:57 +00002050 if (!err) {
2051 netif_set_real_num_tx_queues(dev, queue_pairs);
2052 netif_set_real_num_rx_queues(dev, queue_pairs);
2053
Wanlong Gao8898c212013-01-24 23:51:30 +00002054 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00002055 }
Wanlong Gao47be2472013-01-24 23:51:29 +00002056 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00002057
2058 return err;
2059}
2060
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002061static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2062{
2063 struct virtnet_info *vi = netdev_priv(dev);
2064 char *p = (char *)data;
2065 unsigned int i, j;
2066
2067 switch (stringset) {
2068 case ETH_SS_STATS:
2069 for (i = 0; i < vi->curr_queue_pairs; i++) {
2070 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2071 snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s",
2072 i, virtnet_rq_stats_desc[j].desc);
2073 p += ETH_GSTRING_LEN;
2074 }
2075 }
2076
2077 for (i = 0; i < vi->curr_queue_pairs; i++) {
2078 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2079 snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s",
2080 i, virtnet_sq_stats_desc[j].desc);
2081 p += ETH_GSTRING_LEN;
2082 }
2083 }
2084 break;
2085 }
2086}
2087
2088static int virtnet_get_sset_count(struct net_device *dev, int sset)
2089{
2090 struct virtnet_info *vi = netdev_priv(dev);
2091
2092 switch (sset) {
2093 case ETH_SS_STATS:
2094 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2095 VIRTNET_SQ_STATS_LEN);
2096 default:
2097 return -EOPNOTSUPP;
2098 }
2099}
2100
2101static void virtnet_get_ethtool_stats(struct net_device *dev,
2102 struct ethtool_stats *stats, u64 *data)
2103{
2104 struct virtnet_info *vi = netdev_priv(dev);
2105 unsigned int idx = 0, start, i, j;
2106 const u8 *stats_base;
2107 size_t offset;
2108
2109 for (i = 0; i < vi->curr_queue_pairs; i++) {
2110 struct receive_queue *rq = &vi->rq[i];
2111
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09002112 stats_base = (u8 *)&rq->stats.items;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002113 do {
2114 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2115 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2116 offset = virtnet_rq_stats_desc[j].offset;
2117 data[idx + j] = *(u64 *)(stats_base + offset);
2118 }
2119 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2120 idx += VIRTNET_RQ_STATS_LEN;
2121 }
2122
2123 for (i = 0; i < vi->curr_queue_pairs; i++) {
2124 struct send_queue *sq = &vi->sq[i];
2125
2126 stats_base = (u8 *)&sq->stats;
2127 do {
2128 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2129 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2130 offset = virtnet_sq_stats_desc[j].offset;
2131 data[idx + j] = *(u64 *)(stats_base + offset);
2132 }
2133 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2134 idx += VIRTNET_SQ_STATS_LEN;
2135 }
2136}
2137
Jason Wangd73bcd22012-12-07 07:04:57 +00002138static void virtnet_get_channels(struct net_device *dev,
2139 struct ethtool_channels *channels)
2140{
2141 struct virtnet_info *vi = netdev_priv(dev);
2142
2143 channels->combined_count = vi->curr_queue_pairs;
2144 channels->max_combined = vi->max_queue_pairs;
2145 channels->max_other = 0;
2146 channels->rx_count = 0;
2147 channels->tx_count = 0;
2148 channels->other_count = 0;
2149}
2150
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002151/* Check if the user is trying to change anything besides speed/duplex */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002152static bool
2153virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002154{
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002155 struct ethtool_link_ksettings diff1 = *cmd;
2156 struct ethtool_link_ksettings diff2 = {};
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002157
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01002158 /* cmd is always set so we need to clear it, validate the port type
2159 * and also without autonegotiation we can ignore advertising
2160 */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002161 diff1.base.speed = 0;
2162 diff2.base.port = PORT_OTHER;
2163 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
2164 diff1.base.duplex = 0;
2165 diff1.base.cmd = 0;
2166 diff1.base.link_mode_masks_nwords = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002167
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002168 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
2169 bitmap_empty(diff1.link_modes.supported,
2170 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2171 bitmap_empty(diff1.link_modes.advertising,
2172 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2173 bitmap_empty(diff1.link_modes.lp_advertising,
2174 __ETHTOOL_LINK_MODE_MASK_NBITS);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002175}
2176
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002177static int virtnet_set_link_ksettings(struct net_device *dev,
2178 const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002179{
2180 struct virtnet_info *vi = netdev_priv(dev);
2181 u32 speed;
2182
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002183 speed = cmd->base.speed;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002184 /* don't allow custom speed and duplex */
2185 if (!ethtool_validate_speed(speed) ||
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002186 !ethtool_validate_duplex(cmd->base.duplex) ||
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002187 !virtnet_validate_ethtool_cmd(cmd))
2188 return -EINVAL;
2189 vi->speed = speed;
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002190 vi->duplex = cmd->base.duplex;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002191
2192 return 0;
2193}
2194
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002195static int virtnet_get_link_ksettings(struct net_device *dev,
2196 struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002197{
2198 struct virtnet_info *vi = netdev_priv(dev);
2199
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002200 cmd->base.speed = vi->speed;
2201 cmd->base.duplex = vi->duplex;
2202 cmd->base.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002203
2204 return 0;
2205}
2206
2207static void virtnet_init_settings(struct net_device *dev)
2208{
2209 struct virtnet_info *vi = netdev_priv(dev);
2210
2211 vi->speed = SPEED_UNKNOWN;
2212 vi->duplex = DUPLEX_UNKNOWN;
2213}
2214
Jason Baronfaa9b392018-01-05 17:44:54 -05002215static void virtnet_update_settings(struct virtnet_info *vi)
2216{
2217 u32 speed;
2218 u8 duplex;
2219
2220 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2221 return;
2222
2223 speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config,
2224 speed));
2225 if (ethtool_validate_speed(speed))
2226 vi->speed = speed;
2227 duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config,
2228 duplex));
2229 if (ethtool_validate_duplex(duplex))
2230 vi->duplex = duplex;
2231}
2232
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002233static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00002234 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002235 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00002236 .get_ringparam = virtnet_get_ringparam,
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002237 .get_strings = virtnet_get_strings,
2238 .get_sset_count = virtnet_get_sset_count,
2239 .get_ethtool_stats = virtnet_get_ethtool_stats,
Jason Wangd73bcd22012-12-07 07:04:57 +00002240 .set_channels = virtnet_set_channels,
2241 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00002242 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002243 .get_link_ksettings = virtnet_get_link_ksettings,
2244 .set_link_ksettings = virtnet_set_link_ksettings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08002245};
2246
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002247static void virtnet_freeze_down(struct virtio_device *vdev)
2248{
2249 struct virtnet_info *vi = vdev->priv;
2250 int i;
2251
2252 /* Make sure no work handler is accessing the device */
2253 flush_work(&vi->config_work);
2254
2255 netif_device_detach(vi->dev);
Jason Wang713a98d2017-06-28 09:51:03 +08002256 netif_tx_disable(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002257 cancel_delayed_work_sync(&vi->refill);
2258
2259 if (netif_running(vi->dev)) {
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002260 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002261 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04002262 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002263 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002264 }
2265}
2266
2267static int init_vqs(struct virtnet_info *vi);
2268
2269static int virtnet_restore_up(struct virtio_device *vdev)
2270{
2271 struct virtnet_info *vi = vdev->priv;
2272 int err, i;
2273
2274 err = init_vqs(vi);
2275 if (err)
2276 return err;
2277
2278 virtio_device_ready(vdev);
2279
2280 if (netif_running(vi->dev)) {
2281 for (i = 0; i < vi->curr_queue_pairs; i++)
2282 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2283 schedule_delayed_work(&vi->refill, 0);
2284
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002285 for (i = 0; i < vi->max_queue_pairs; i++) {
Willem de Bruijne4e84522017-04-24 13:49:26 -04002286 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002287 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2288 &vi->sq[i].napi);
2289 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002290 }
2291
2292 netif_device_attach(vi->dev);
2293 return err;
2294}
2295
Jason Wang3f935222017-07-19 16:54:49 +08002296static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2297{
2298 struct scatterlist sg;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002299 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
Jason Wang3f935222017-07-19 16:54:49 +08002300
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002301 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
Jason Wang3f935222017-07-19 16:54:49 +08002302
2303 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2304 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2305 dev_warn(&vi->dev->dev, "Fail to set guest offload. \n");
2306 return -EINVAL;
2307 }
2308
2309 return 0;
2310}
2311
2312static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2313{
2314 u64 offloads = 0;
2315
2316 if (!vi->guest_offloads)
2317 return 0;
2318
2319 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
2320 offloads = 1ULL << VIRTIO_NET_F_GUEST_CSUM;
2321
2322 return virtnet_set_guest_offloads(vi, offloads);
2323}
2324
2325static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2326{
2327 u64 offloads = vi->guest_offloads;
2328
2329 if (!vi->guest_offloads)
2330 return 0;
2331 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
2332 offloads |= 1ULL << VIRTIO_NET_F_GUEST_CSUM;
2333
2334 return virtnet_set_guest_offloads(vi, offloads);
2335}
2336
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002337static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2338 struct netlink_ext_ack *extack)
John Fastabendf600b692016-12-15 12:13:24 -08002339{
2340 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2341 struct virtnet_info *vi = netdev_priv(dev);
2342 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08002343 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002344 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08002345
Jason Wang3f935222017-07-19 16:54:49 +08002346 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2347 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2348 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2349 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2350 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO))) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002351 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 -08002352 return -EOPNOTSUPP;
2353 }
2354
2355 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002356 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
John Fastabendf600b692016-12-15 12:13:24 -08002357 return -EINVAL;
2358 }
2359
2360 if (dev->mtu > max_sz) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002361 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
John Fastabendf600b692016-12-15 12:13:24 -08002362 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2363 return -EINVAL;
2364 }
2365
John Fastabend672aafd2016-12-15 12:13:49 -08002366 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2367 if (prog)
2368 xdp_qp = nr_cpu_ids;
2369
2370 /* XDP requires extra queues for XDP_TX */
2371 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002372 NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
John Fastabend672aafd2016-12-15 12:13:49 -08002373 netdev_warn(dev, "request %i queues but max is %i\n",
2374 curr_qp + xdp_qp, vi->max_queue_pairs);
2375 return -ENOMEM;
2376 }
2377
John Fastabend2de2f7f2017-02-02 19:16:29 -08002378 if (prog) {
2379 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
2380 if (IS_ERR(prog))
2381 return PTR_ERR(prog);
2382 }
2383
Jason Wang4941d472017-07-19 16:54:48 +08002384 /* Make sure NAPI is not using any XDP TX queues for RX. */
Jason Wang4e09ff52018-02-28 18:20:04 +08002385 if (netif_running(dev))
2386 for (i = 0; i < vi->max_queue_pairs; i++)
2387 napi_disable(&vi->rq[i].napi);
John Fastabendf600b692016-12-15 12:13:24 -08002388
John Fastabend672aafd2016-12-15 12:13:49 -08002389 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
Jason Wang4941d472017-07-19 16:54:48 +08002390 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2391 if (err)
2392 goto err;
2393 vi->xdp_queue_pairs = xdp_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002394
John Fastabendf600b692016-12-15 12:13:24 -08002395 for (i = 0; i < vi->max_queue_pairs; i++) {
2396 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2397 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
Jason Wang3f935222017-07-19 16:54:49 +08002398 if (i == 0) {
2399 if (!old_prog)
2400 virtnet_clear_guest_offloads(vi);
2401 if (!prog)
2402 virtnet_restore_guest_offloads(vi);
2403 }
John Fastabendf600b692016-12-15 12:13:24 -08002404 if (old_prog)
2405 bpf_prog_put(old_prog);
Jason Wang4e09ff52018-02-28 18:20:04 +08002406 if (netif_running(dev))
2407 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
John Fastabendf600b692016-12-15 12:13:24 -08002408 }
2409
2410 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08002411
Jason Wang4941d472017-07-19 16:54:48 +08002412err:
2413 for (i = 0; i < vi->max_queue_pairs; i++)
2414 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
John Fastabend2de2f7f2017-02-02 19:16:29 -08002415 if (prog)
2416 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2417 return err;
John Fastabendf600b692016-12-15 12:13:24 -08002418}
2419
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002420static u32 virtnet_xdp_query(struct net_device *dev)
John Fastabendf600b692016-12-15 12:13:24 -08002421{
2422 struct virtnet_info *vi = netdev_priv(dev);
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002423 const struct bpf_prog *xdp_prog;
John Fastabendf600b692016-12-15 12:13:24 -08002424 int i;
2425
2426 for (i = 0; i < vi->max_queue_pairs; i++) {
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002427 xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2428 if (xdp_prog)
2429 return xdp_prog->aux->id;
John Fastabendf600b692016-12-15 12:13:24 -08002430 }
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002431 return 0;
John Fastabendf600b692016-12-15 12:13:24 -08002432}
2433
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002434static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
John Fastabendf600b692016-12-15 12:13:24 -08002435{
2436 switch (xdp->command) {
2437 case XDP_SETUP_PROG:
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002438 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
John Fastabendf600b692016-12-15 12:13:24 -08002439 case XDP_QUERY_PROG:
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002440 xdp->prog_id = virtnet_xdp_query(dev);
John Fastabendf600b692016-12-15 12:13:24 -08002441 return 0;
2442 default:
2443 return -EINVAL;
2444 }
2445}
2446
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002447static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2448 size_t len)
2449{
2450 struct virtnet_info *vi = netdev_priv(dev);
2451 int ret;
2452
2453 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2454 return -EOPNOTSUPP;
2455
2456 ret = snprintf(buf, len, "sby");
2457 if (ret >= len)
2458 return -EOPNOTSUPP;
2459
2460 return 0;
2461}
2462
Stephen Hemminger76288b42009-01-06 10:44:22 -08002463static const struct net_device_ops virtnet_netdev = {
2464 .ndo_open = virtnet_open,
2465 .ndo_stop = virtnet_close,
2466 .ndo_start_xmit = start_xmit,
2467 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08002468 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00002469 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002470 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00002471 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2472 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002473#ifdef CONFIG_NET_POLL_CONTROLLER
2474 .ndo_poll_controller = virtnet_netpoll,
2475#endif
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002476 .ndo_bpf = virtnet_xdp,
Jason Wang186b3c92017-09-19 17:42:43 +08002477 .ndo_xdp_xmit = virtnet_xdp_xmit,
Vlad Yasevich2836b4f2017-05-23 13:38:43 -04002478 .ndo_features_check = passthru_features_check,
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002479 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002480};
2481
Jason Wang586d17c2012-04-11 20:43:52 +00002482static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002483{
Jason Wang586d17c2012-04-11 20:43:52 +00002484 struct virtnet_info *vi =
2485 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002486 u16 v;
2487
Rusty Russell855e0c52013-10-14 18:11:51 +10302488 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2489 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302490 return;
Jason Wang586d17c2012-04-11 20:43:52 +00002491
2492 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00002493 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00002494 virtnet_ack_link_announce(vi);
2495 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002496
2497 /* Ignore unknown (future) status bits */
2498 v &= VIRTIO_NET_S_LINK_UP;
2499
2500 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302501 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002502
2503 vi->status = v;
2504
2505 if (vi->status & VIRTIO_NET_S_LINK_UP) {
Jason Baronfaa9b392018-01-05 17:44:54 -05002506 virtnet_update_settings(vi);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002507 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002508 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002509 } else {
2510 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002511 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002512 }
2513}
2514
2515static void virtnet_config_changed(struct virtio_device *vdev)
2516{
2517 struct virtnet_info *vi = vdev->priv;
2518
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002519 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002520}
2521
Jason Wang986a4f42012-12-07 07:04:56 +00002522static void virtnet_free_queues(struct virtnet_info *vi)
2523{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002524 int i;
2525
Jason Wangab3971b2015-03-12 13:57:44 +08002526 for (i = 0; i < vi->max_queue_pairs; i++) {
2527 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002528 netif_napi_del(&vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002529 netif_napi_del(&vi->sq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08002530 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002531
Eric Dumazet963abe52016-11-15 22:24:12 -08002532 /* We called napi_hash_del() before netif_napi_del(),
2533 * we need to respect an RCU grace period before freeing vi->rq
2534 */
2535 synchronize_net();
2536
Jason Wang986a4f42012-12-07 07:04:56 +00002537 kfree(vi->rq);
2538 kfree(vi->sq);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002539 kfree(vi->ctrl);
Jason Wang986a4f42012-12-07 07:04:56 +00002540}
2541
John Fastabend473153292017-02-02 19:14:32 -08002542static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002543{
John Fastabendf600b692016-12-15 12:13:24 -08002544 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00002545 int i;
2546
2547 for (i = 0; i < vi->max_queue_pairs; i++) {
2548 while (vi->rq[i].pages)
2549 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08002550
2551 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2552 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2553 if (old_prog)
2554 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00002555 }
John Fastabend473153292017-02-02 19:14:32 -08002556}
2557
2558static void free_receive_bufs(struct virtnet_info *vi)
2559{
2560 rtnl_lock();
2561 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08002562 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002563}
2564
Michael Daltonfb518792014-01-16 22:23:26 -08002565static void free_receive_page_frags(struct virtnet_info *vi)
2566{
2567 int i;
2568 for (i = 0; i < vi->max_queue_pairs; i++)
2569 if (vi->rq[i].alloc_frag.page)
2570 put_page(vi->rq[i].alloc_frag.page);
2571}
2572
John Fastabendb68df012017-01-25 18:22:48 -08002573static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08002574{
2575 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
2576 return false;
2577 else if (q < vi->curr_queue_pairs)
2578 return true;
2579 else
2580 return false;
2581}
2582
Jason Wang986a4f42012-12-07 07:04:56 +00002583static void free_unused_bufs(struct virtnet_info *vi)
2584{
2585 void *buf;
2586 int i;
2587
2588 for (i = 0; i < vi->max_queue_pairs; i++) {
2589 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002590 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08002591 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08002592 dev_kfree_skb(buf);
2593 else
2594 put_page(virt_to_head_page(buf));
2595 }
Jason Wang986a4f42012-12-07 07:04:56 +00002596 }
2597
2598 for (i = 0; i < vi->max_queue_pairs; i++) {
2599 struct virtqueue *vq = vi->rq[i].vq;
2600
2601 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002602 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02002603 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002604 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002605 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002606 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002607 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002608 }
Jason Wang986a4f42012-12-07 07:04:56 +00002609 }
Jason Wang986a4f42012-12-07 07:04:56 +00002610 }
2611}
2612
Jason Wange9d74172012-12-07 07:04:55 +00002613static void virtnet_del_vqs(struct virtnet_info *vi)
2614{
2615 struct virtio_device *vdev = vi->vdev;
2616
Wanlong Gao8898c212013-01-24 23:51:30 +00002617 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00002618
Jason Wange9d74172012-12-07 07:04:55 +00002619 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002620
2621 virtnet_free_queues(vi);
2622}
2623
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002624/* How large should a single buffer be so a queue full of these can fit at
2625 * least one full packet?
2626 * Logic below assumes the mergeable buffer header is used.
2627 */
2628static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2629{
2630 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2631 unsigned int rq_size = virtqueue_get_vring_size(vq);
2632 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2633 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2634 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2635
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03002636 return max(max(min_buf_len, hdr_len) - hdr_len,
2637 (unsigned int)GOOD_PACKET_LEN);
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002638}
2639
Jason Wang986a4f42012-12-07 07:04:56 +00002640static int virtnet_find_vqs(struct virtnet_info *vi)
2641{
2642 vq_callback_t **callbacks;
2643 struct virtqueue **vqs;
2644 int ret = -ENOMEM;
2645 int i, total_vqs;
2646 const char **names;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002647 bool *ctx;
Jason Wang986a4f42012-12-07 07:04:56 +00002648
2649 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2650 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2651 * possible control vq.
2652 */
2653 total_vqs = vi->max_queue_pairs * 2 +
2654 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2655
2656 /* Allocate space for find_vqs parameters */
Kees Cook6396bb22018-06-12 14:03:40 -07002657 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002658 if (!vqs)
2659 goto err_vq;
Kees Cook6da2ec52018-06-12 13:55:00 -07002660 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002661 if (!callbacks)
2662 goto err_callback;
Kees Cook6da2ec52018-06-12 13:55:00 -07002663 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002664 if (!names)
2665 goto err_names;
Jason Wang192f68c2017-07-19 16:54:47 +08002666 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Kees Cook6396bb22018-06-12 14:03:40 -07002667 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002668 if (!ctx)
2669 goto err_ctx;
2670 } else {
2671 ctx = NULL;
2672 }
Jason Wang986a4f42012-12-07 07:04:56 +00002673
2674 /* Parameters for control virtqueue, if any */
2675 if (vi->has_cvq) {
2676 callbacks[total_vqs - 1] = NULL;
2677 names[total_vqs - 1] = "control";
2678 }
2679
2680 /* Allocate/initialize parameters for send/receive virtqueues */
2681 for (i = 0; i < vi->max_queue_pairs; i++) {
2682 callbacks[rxq2vq(i)] = skb_recv_done;
2683 callbacks[txq2vq(i)] = skb_xmit_done;
2684 sprintf(vi->rq[i].name, "input.%d", i);
2685 sprintf(vi->sq[i].name, "output.%d", i);
2686 names[rxq2vq(i)] = vi->rq[i].name;
2687 names[txq2vq(i)] = vi->sq[i].name;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002688 if (ctx)
2689 ctx[rxq2vq(i)] = true;
Jason Wang986a4f42012-12-07 07:04:56 +00002690 }
2691
2692 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002693 names, ctx, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002694 if (ret)
2695 goto err_find;
2696
2697 if (vi->has_cvq) {
2698 vi->cvq = vqs[total_vqs - 1];
2699 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002700 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002701 }
2702
2703 for (i = 0; i < vi->max_queue_pairs; i++) {
2704 vi->rq[i].vq = vqs[rxq2vq(i)];
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002705 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
Jason Wang986a4f42012-12-07 07:04:56 +00002706 vi->sq[i].vq = vqs[txq2vq(i)];
2707 }
2708
Tonghao Zhang2fa3c8a2018-05-31 07:16:32 -07002709 /* run here: ret == 0. */
Jason Wang986a4f42012-12-07 07:04:56 +00002710
Jason Wang986a4f42012-12-07 07:04:56 +00002711
2712err_find:
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002713 kfree(ctx);
2714err_ctx:
Jason Wang986a4f42012-12-07 07:04:56 +00002715 kfree(names);
2716err_names:
2717 kfree(callbacks);
2718err_callback:
2719 kfree(vqs);
2720err_vq:
2721 return ret;
2722}
2723
2724static int virtnet_alloc_queues(struct virtnet_info *vi)
2725{
2726 int i;
2727
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002728 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2729 if (!vi->ctrl)
2730 goto err_ctrl;
Kees Cook6396bb22018-06-12 14:03:40 -07002731 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002732 if (!vi->sq)
2733 goto err_sq;
Kees Cook6396bb22018-06-12 14:03:40 -07002734 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002735 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002736 goto err_rq;
2737
2738 INIT_DELAYED_WORK(&vi->refill, refill_work);
2739 for (i = 0; i < vi->max_queue_pairs; i++) {
2740 vi->rq[i].pages = NULL;
2741 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2742 napi_weight);
Willem de Bruijn1d11e732017-04-27 20:37:58 -04002743 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2744 napi_tx ? napi_weight : 0);
Jason Wang986a4f42012-12-07 07:04:56 +00002745
2746 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002747 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002748 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002749
2750 u64_stats_init(&vi->rq[i].stats.syncp);
2751 u64_stats_init(&vi->sq[i].stats.syncp);
Jason Wang986a4f42012-12-07 07:04:56 +00002752 }
2753
2754 return 0;
2755
2756err_rq:
2757 kfree(vi->sq);
2758err_sq:
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002759 kfree(vi->ctrl);
2760err_ctrl:
Jason Wang986a4f42012-12-07 07:04:56 +00002761 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002762}
2763
Amit Shah3f9c10b2011-12-22 16:58:31 +05302764static int init_vqs(struct virtnet_info *vi)
2765{
Jason Wang986a4f42012-12-07 07:04:56 +00002766 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302767
Jason Wang986a4f42012-12-07 07:04:56 +00002768 /* Allocate send & receive queues */
2769 ret = virtnet_alloc_queues(vi);
2770 if (ret)
2771 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302772
Jason Wang986a4f42012-12-07 07:04:56 +00002773 ret = virtnet_find_vqs(vi);
2774 if (ret)
2775 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302776
Wanlong Gao47be2472013-01-24 23:51:29 +00002777 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002778 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002779 put_online_cpus();
2780
Amit Shah3f9c10b2011-12-22 16:58:31 +05302781 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002782
2783err_free:
2784 virtnet_free_queues(vi);
2785err:
2786 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302787}
2788
Michael Daltonfbf28d72014-01-16 22:23:30 -08002789#ifdef CONFIG_SYSFS
2790static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -07002791 char *buf)
Michael Daltonfbf28d72014-01-16 22:23:30 -08002792{
2793 struct virtnet_info *vi = netdev_priv(queue->dev);
2794 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Jason Wang3cc81a92018-03-02 17:29:14 +08002795 unsigned int headroom = virtnet_get_headroom(vi);
2796 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
Johannes Berg5377d7582015-08-19 09:48:40 +02002797 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002798
2799 BUG_ON(queue_index >= vi->max_queue_pairs);
2800 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002801 return sprintf(buf, "%u\n",
Jason Wang3cc81a92018-03-02 17:29:14 +08002802 get_mergeable_buf_len(&vi->rq[queue_index], avg,
2803 SKB_DATA_ALIGN(headroom + tailroom)));
Michael Daltonfbf28d72014-01-16 22:23:30 -08002804}
2805
2806static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2807 __ATTR_RO(mergeable_rx_buffer_size);
2808
2809static struct attribute *virtio_net_mrg_rx_attrs[] = {
2810 &mergeable_rx_buffer_size_attribute.attr,
2811 NULL
2812};
2813
2814static const struct attribute_group virtio_net_mrg_rx_group = {
2815 .name = "virtio_net",
2816 .attrs = virtio_net_mrg_rx_attrs
2817};
2818#endif
2819
Jason Wang892d6eb2014-11-20 17:03:05 +08002820static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2821 unsigned int fbit,
2822 const char *fname, const char *dname)
2823{
2824 if (!virtio_has_feature(vdev, fbit))
2825 return false;
2826
2827 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2828 fname, dname);
2829
2830 return true;
2831}
2832
2833#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2834 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2835
2836static bool virtnet_validate_features(struct virtio_device *vdev)
2837{
2838 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2839 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2840 "VIRTIO_NET_F_CTRL_VQ") ||
2841 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2842 "VIRTIO_NET_F_CTRL_VQ") ||
2843 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2844 "VIRTIO_NET_F_CTRL_VQ") ||
2845 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2846 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2847 "VIRTIO_NET_F_CTRL_VQ"))) {
2848 return false;
2849 }
2850
2851 return true;
2852}
2853
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002854#define MIN_MTU ETH_MIN_MTU
2855#define MAX_MTU ETH_MAX_MTU
2856
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002857static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10002858{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002859 if (!vdev->config->get) {
2860 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2861 __func__);
2862 return -EINVAL;
2863 }
2864
Jason Wang892d6eb2014-11-20 17:03:05 +08002865 if (!virtnet_validate_features(vdev))
2866 return -EINVAL;
2867
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002868 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2869 int mtu = virtio_cread16(vdev,
2870 offsetof(struct virtio_net_config,
2871 mtu));
2872 if (mtu < MIN_MTU)
2873 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2874 }
2875
2876 return 0;
2877}
2878
2879static int virtnet_probe(struct virtio_device *vdev)
2880{
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002881 int i, err = -ENOMEM;
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002882 struct net_device *dev;
2883 struct virtnet_info *vi;
2884 u16 max_queue_pairs;
2885 int mtu;
2886
Jason Wang986a4f42012-12-07 07:04:56 +00002887 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302888 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2889 struct virtio_net_config,
2890 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002891
2892 /* We need at least 2 queue's */
2893 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2894 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2895 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2896 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002897
2898 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002899 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002900 if (!dev)
2901 return -ENOMEM;
2902
2903 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002904 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002905 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002906 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002907
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002908 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002909 SET_NETDEV_DEV(dev, &vdev->dev);
2910
2911 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002912 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002913 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002914 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002915 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002916 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002917
2918 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
David S. Millere078de02017-07-03 06:37:32 -07002919 dev->hw_features |= NETIF_F_TSO
Rusty Russell34a48572008-02-04 23:50:02 -05002920 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2921 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002922 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002923 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2924 dev->hw_features |= NETIF_F_TSO;
2925 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2926 dev->hw_features |= NETIF_F_TSO6;
2927 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2928 dev->hw_features |= NETIF_F_TSO_ECN;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002929
Jason Wang41f2f122014-12-24 11:03:52 +08002930 dev->features |= NETIF_F_GSO_ROBUST;
2931
Michał Mirosław98e778c2011-03-31 01:01:35 +00002932 if (gso)
David S. Millere078de02017-07-03 06:37:32 -07002933 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002934 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002935 }
Thomas Huth4f491292013-08-27 17:09:02 +02002936 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2937 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002938
Jason Wang4fda8302013-04-10 23:32:21 +00002939 dev->vlan_features = dev->features;
2940
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002941 /* MTU range: 68 - 65535 */
2942 dev->min_mtu = MIN_MTU;
2943 dev->max_mtu = MAX_MTU;
2944
Rusty Russell296f96f2007-10-22 11:03:37 +10002945 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302946 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2947 virtio_cread_bytes(vdev,
2948 offsetof(struct virtio_net_config, mac),
2949 dev->dev_addr, dev->addr_len);
2950 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002951 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002952
2953 /* Set up our device-specific information */
2954 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002955 vi->dev = dev;
2956 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002957 vdev->priv = vi;
John Stultz827da442013-10-07 15:51:58 -07002958
Jason Wang586d17c2012-04-11 20:43:52 +00002959 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002960
Herbert Xu97402b92008-04-18 11:24:27 +08002961 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002962 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2963 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002964 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2965 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002966 vi->big_packets = true;
2967
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002968 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2969 vi->mergeable_rx_bufs = true;
2970
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002971 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2972 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002973 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2974 else
2975 vi->hdr_len = sizeof(struct virtio_net_hdr);
2976
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002977 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2978 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302979 vi->any_header_sg = true;
2980
Jason Wang986a4f42012-12-07 07:04:56 +00002981 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2982 vi->has_cvq = true;
2983
Aaron Conole14de9d12016-06-03 16:57:12 -04002984 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2985 mtu = virtio_cread16(vdev,
2986 offsetof(struct virtio_net_config,
2987 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002988 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002989 /* Should never trigger: MTU was previously validated
2990 * in virtnet_validate.
2991 */
2992 dev_err(&vdev->dev, "device MTU appears to have changed "
2993 "it is now %d < %d", mtu, dev->min_mtu);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002994 goto free;
Aaron Conole93a205e2016-10-25 16:12:12 -04002995 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02002996
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002997 dev->mtu = mtu;
2998 dev->max_mtu = mtu;
2999
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02003000 /* TODO: size buffers correctly in this case. */
3001 if (dev->mtu > ETH_DATA_LEN)
3002 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04003003 }
3004
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03003005 if (vi->any_header_sg)
3006 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08003007
Jason Wang44900012016-11-25 12:37:26 +08003008 /* Enable multiqueue by default */
3009 if (num_online_cpus() >= max_queue_pairs)
3010 vi->curr_queue_pairs = max_queue_pairs;
3011 else
3012 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00003013 vi->max_queue_pairs = max_queue_pairs;
3014
3015 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05303016 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003017 if (err)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003018 goto free;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003019
Michael Daltonfbf28d72014-01-16 22:23:30 -08003020#ifdef CONFIG_SYSFS
3021 if (vi->mergeable_rx_bufs)
3022 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3023#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08003024 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3025 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00003026
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01003027 virtnet_init_settings(dev);
3028
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003029 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3030 vi->failover = net_failover_create(vi->dev);
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00003031 if (IS_ERR(vi->failover)) {
3032 err = PTR_ERR(vi->failover);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003033 goto free_vqs;
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00003034 }
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003035 }
3036
Rusty Russell296f96f2007-10-22 11:03:37 +10003037 err = register_netdev(dev);
3038 if (err) {
3039 pr_debug("virtio_net: registering device failed\n");
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003040 goto free_failover;
Rusty Russell296f96f2007-10-22 11:03:37 +10003041 }
Rusty Russellb3369c12008-02-04 23:50:02 -05003042
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10303043 virtio_device_ready(vdev);
3044
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003045 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003046 if (err) {
3047 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08003048 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003049 }
3050
Jason Wanga2208712016-12-13 14:23:05 +08003051 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08003052
Jason Wang167c25e2010-11-10 14:45:41 +00003053 /* Assume link up if device can't report link status,
3054 otherwise get link status from config. */
Jay Vosburghbda7fab2018-03-22 14:42:41 +00003055 netif_carrier_off(dev);
Jason Wang167c25e2010-11-10 14:45:41 +00003056 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
Tejun Heo3b07e9c2012-08-20 14:51:24 -07003057 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00003058 } else {
3059 vi->status = VIRTIO_NET_S_LINK_UP;
Jason Baronfaa9b392018-01-05 17:44:54 -05003060 virtnet_update_settings(vi);
Jason Wang167c25e2010-11-10 14:45:41 +00003061 netif_carrier_on(dev);
3062 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003063
Jason Wang3f935222017-07-19 16:54:49 +08003064 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3065 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3066 set_bit(guest_offloads[i], &vi->guest_offloads);
3067
Jason Wang986a4f42012-12-07 07:04:56 +00003068 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3069 dev->name, max_queue_pairs);
3070
Rusty Russell296f96f2007-10-22 11:03:37 +10003071 return 0;
3072
wangyunjianf00e35e2016-05-31 11:52:43 +08003073free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10303074 vi->vdev->config->reset(vdev);
3075
Rusty Russellb3369c12008-02-04 23:50:02 -05003076 unregister_netdev(dev);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003077free_failover:
3078 net_failover_destroy(vi->failover);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003079free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00003080 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08003081 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00003082 virtnet_del_vqs(vi);
Rusty Russell296f96f2007-10-22 11:03:37 +10003083free:
3084 free_netdev(dev);
3085 return err;
3086}
3087
Amit Shah04486ed2011-12-22 16:58:32 +05303088static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10003089{
Amit Shah04486ed2011-12-22 16:58:32 +05303090 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00003091
3092 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00003093 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003094
Jason Wang986a4f42012-12-07 07:04:56 +00003095 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003096
Michael Daltonfb518792014-01-16 22:23:26 -08003097 free_receive_page_frags(vi);
3098
Jason Wang986a4f42012-12-07 07:04:56 +00003099 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05303100}
3101
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003102static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05303103{
3104 struct virtnet_info *vi = vdev->priv;
3105
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003106 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003107
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10303108 /* Make sure no work handler is accessing the device. */
3109 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00003110
Amit Shah04486ed2011-12-22 16:58:32 +05303111 unregister_netdev(vi->dev);
3112
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003113 net_failover_destroy(vi->failover);
3114
Amit Shah04486ed2011-12-22 16:58:32 +05303115 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003116
Rusty Russell74b25532007-11-19 11:20:42 -05003117 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003118}
3119
Arnd Bergmann67a75192017-07-25 17:35:50 +02003120static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303121{
3122 struct virtnet_info *vi = vdev->priv;
3123
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003124 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003125 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303126 remove_vq_common(vi);
3127
3128 return 0;
3129}
3130
Arnd Bergmann67a75192017-07-25 17:35:50 +02003131static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303132{
3133 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003134 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05303135
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003136 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303137 if (err)
3138 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00003139 virtnet_set_queues(vi, vi->curr_queue_pairs);
3140
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003141 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08003142 if (err)
3143 return err;
3144
Amit Shah0741bcb2011-12-22 16:58:33 +05303145 return 0;
3146}
Amit Shah0741bcb2011-12-22 16:58:33 +05303147
Rusty Russell296f96f2007-10-22 11:03:37 +10003148static struct virtio_device_id id_table[] = {
3149 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3150 { 0 },
3151};
3152
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003153#define VIRTNET_FEATURES \
3154 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3155 VIRTIO_NET_F_MAC, \
3156 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3157 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3158 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3159 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3160 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3161 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3162 VIRTIO_NET_F_CTRL_MAC_ADDR, \
Jason Baronfaa9b392018-01-05 17:44:54 -05003163 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
Sridhar Samudrala98050692018-05-24 09:55:16 -07003164 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003165
Rusty Russellc45a6812008-05-02 21:50:50 -05003166static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003167 VIRTNET_FEATURES,
3168};
3169
3170static unsigned int features_legacy[] = {
3171 VIRTNET_FEATURES,
3172 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09303173 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05003174};
3175
Uwe Kleine-König22402522009-11-05 01:32:44 -08003176static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05003177 .feature_table = features,
3178 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003179 .feature_table_legacy = features_legacy,
3180 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10003181 .driver.name = KBUILD_MODNAME,
3182 .driver.owner = THIS_MODULE,
3183 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003184 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10003185 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003186 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003187 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09303188#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05303189 .freeze = virtnet_freeze,
3190 .restore = virtnet_restore,
3191#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10003192};
3193
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003194static __init int virtio_net_driver_init(void)
3195{
3196 int ret;
3197
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003198 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003199 virtnet_cpu_online,
3200 virtnet_cpu_down_prep);
3201 if (ret < 0)
3202 goto out;
3203 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003204 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003205 NULL, virtnet_cpu_dead);
3206 if (ret)
3207 goto err_dead;
3208
3209 ret = register_virtio_driver(&virtio_net_driver);
3210 if (ret)
3211 goto err_virtio;
3212 return 0;
3213err_virtio:
3214 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3215err_dead:
3216 cpuhp_remove_multi_state(virtionet_online);
3217out:
3218 return ret;
3219}
3220module_init(virtio_net_driver_init);
3221
3222static __exit void virtio_net_driver_exit(void)
3223{
Andrew Jonescfa0ebc2017-07-24 15:38:32 +02003224 unregister_virtio_driver(&virtio_net_driver);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003225 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3226 cpuhp_remove_multi_state(virtionet_online);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003227}
3228module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10003229
3230MODULE_DEVICE_TABLE(virtio, id_table);
3231MODULE_DESCRIPTION("Virtio network driver");
3232MODULE_LICENSE("GPL");