blob: ea672145f6a66b97ec3572f213939a64323fcdf3 [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>
Caleb Raitto2ca653d2018-08-09 17:28:40 -070033#include <linux/kernel.h>
Sridhar Samudralaba5e4422018-05-24 09:55:17 -070034#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,
Jason Wange59ff2c2018-11-22 14:36:30 +080073 VIRTIO_NET_F_GUEST_UFO,
74 VIRTIO_NET_F_GUEST_CSUM
Colin Ian King7acd4322017-08-12 22:45:53 +010075};
Jason Wang3f935222017-07-19 16:54:49 +080076
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090077struct virtnet_stat_desc {
78 char desc[ETH_GSTRING_LEN];
79 size_t offset;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000080};
81
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090082struct virtnet_sq_stats {
83 struct u64_stats_sync syncp;
84 u64 packets;
85 u64 bytes;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090086 u64 xdp_tx;
87 u64 xdp_tx_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +090088 u64 kicks;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090089};
90
Jason Wangd46eeea2018-07-31 17:43:39 +080091struct virtnet_rq_stats {
92 struct u64_stats_sync syncp;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090093 u64 packets;
94 u64 bytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +090095 u64 drops;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090096 u64 xdp_packets;
97 u64 xdp_tx;
98 u64 xdp_redirects;
99 u64 xdp_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900100 u64 kicks;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900101};
102
103#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
Jason Wangd46eeea2018-07-31 17:43:39 +0800104#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900105
106static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900107 { "packets", VIRTNET_SQ_STAT(packets) },
108 { "bytes", VIRTNET_SQ_STAT(bytes) },
109 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
110 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900111 { "kicks", VIRTNET_SQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900112};
113
114static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900115 { "packets", VIRTNET_RQ_STAT(packets) },
116 { "bytes", VIRTNET_RQ_STAT(bytes) },
117 { "drops", VIRTNET_RQ_STAT(drops) },
118 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
119 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
120 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
121 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900122 { "kicks", VIRTNET_RQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900123};
124
125#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
126#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
127
Jason Wange9d74172012-12-07 07:04:55 +0000128/* Internal representation of a send virtqueue */
129struct send_queue {
130 /* Virtqueue associated with this send _queue */
131 struct virtqueue *vq;
132
133 /* TX: fragments + linear part + virtio header */
134 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000135
136 /* Name of the send queue: output.$index */
137 char name[40];
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400138
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900139 struct virtnet_sq_stats stats;
140
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400141 struct napi_struct napi;
Jason Wange9d74172012-12-07 07:04:55 +0000142};
143
144/* Internal representation of a receive virtqueue */
145struct receive_queue {
146 /* Virtqueue associated with this receive_queue */
147 struct virtqueue *vq;
148
Rusty Russell296f96f2007-10-22 11:03:37 +1000149 struct napi_struct napi;
150
John Fastabendf600b692016-12-15 12:13:24 -0800151 struct bpf_prog __rcu *xdp_prog;
152
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900153 struct virtnet_rq_stats stats;
154
Jason Wange9d74172012-12-07 07:04:55 +0000155 /* Chain pages by the private ptr. */
156 struct page *pages;
157
Michael Daltonab7db912014-01-16 22:23:27 -0800158 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200159 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800160
Michael Daltonfb518792014-01-16 22:23:26 -0800161 /* Page frag for packet buffer allocation. */
162 struct page_frag alloc_frag;
163
Jason Wange9d74172012-12-07 07:04:55 +0000164 /* RX: fragments + linear part + virtio header */
165 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000166
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +0200167 /* Min single buffer size for mergeable buffers case. */
168 unsigned int min_buf_len;
169
Jason Wang986a4f42012-12-07 07:04:56 +0000170 /* Name of this receive queue: input.$index */
171 char name[40];
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100172
173 struct xdp_rxq_info xdp_rxq;
Jason Wange9d74172012-12-07 07:04:55 +0000174};
175
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300176/* Control VQ buffers: protected by the rtnl lock */
177struct control_buf {
178 struct virtio_net_ctrl_hdr hdr;
179 virtio_net_ctrl_ack status;
180 struct virtio_net_ctrl_mq mq;
181 u8 promisc;
182 u8 allmulti;
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +0300183 __virtio16 vid;
Michael S. Tsirkinf4ee7032018-04-19 08:30:50 +0300184 __virtio64 offloads;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300185};
186
Jason Wange9d74172012-12-07 07:04:55 +0000187struct virtnet_info {
188 struct virtio_device *vdev;
189 struct virtqueue *cvq;
190 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000191 struct send_queue *sq;
192 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000193 unsigned int status;
194
Jason Wang986a4f42012-12-07 07:04:56 +0000195 /* Max # of queue pairs supported by the device */
196 u16 max_queue_pairs;
197
198 /* # of queue pairs currently used by the driver */
199 u16 curr_queue_pairs;
200
John Fastabend672aafd2016-12-15 12:13:49 -0800201 /* # of XDP queue pairs currently used by the driver */
202 u16 xdp_queue_pairs;
203
Herbert Xu97402b92008-04-18 11:24:27 +0800204 /* I like... big packets and I cannot lie! */
205 bool big_packets;
206
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800207 /* Host will merge rx buffers for big packets (shake it! shake it!) */
208 bool mergeable_rx_bufs;
209
Jason Wang986a4f42012-12-07 07:04:56 +0000210 /* Has control virtqueue */
211 bool has_cvq;
212
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930213 /* Host can handle any s/g split between our header and packet data */
214 bool any_header_sg;
215
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300216 /* Packet virtio header size */
217 u8 hdr_len;
218
Rusty Russell3161e452009-08-26 12:22:32 -0700219 /* Work struct for refilling if we run low on memory. */
220 struct delayed_work refill;
221
Jason Wang586d17c2012-04-11 20:43:52 +0000222 /* Work struct for config space updates */
223 struct work_struct config_work;
224
Jason Wang986a4f42012-12-07 07:04:56 +0000225 /* Does the affinity hint is set for virtqueues? */
226 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000227
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200228 /* CPU hotplug instances for online & dead */
229 struct hlist_node node;
230 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200231
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300232 struct control_buf *ctrl;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100233
234 /* Ethtool settings */
235 u8 duplex;
236 u32 speed;
Jason Wang3f935222017-07-19 16:54:49 +0800237
238 unsigned long guest_offloads;
Sridhar Samudralaba5e4422018-05-24 09:55:17 -0700239
240 /* failover when STANDBY feature enabled */
241 struct failover *failover;
Rusty Russell296f96f2007-10-22 11:03:37 +1000242};
243
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000244struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300245 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000246 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300247 * hdr is in a separate sg buffer, and data sg buffer shares same page
248 * with this header sg. This padding makes next sg 16 byte aligned
249 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000250 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300251 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000252};
253
Jason Wang986a4f42012-12-07 07:04:56 +0000254/* Converting between virtqueue no. and kernel tx/rx queue no.
255 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
256 */
257static int vq2txq(struct virtqueue *vq)
258{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000259 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000260}
261
262static int txq2vq(int txq)
263{
264 return txq * 2 + 1;
265}
266
267static int vq2rxq(struct virtqueue *vq)
268{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000269 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000270}
271
272static int rxq2vq(int rxq)
273{
274 return rxq * 2;
275}
276
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300277static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000278{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300279 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000280}
281
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000282/*
283 * private is used to chain pages for big packets, put the whole
284 * most recent used list in the beginning for reuse
285 */
Jason Wange9d74172012-12-07 07:04:55 +0000286static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500287{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000288 struct page *end;
289
Jason Wange9d74172012-12-07 07:04:55 +0000290 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000291 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000292 end->private = (unsigned long)rq->pages;
293 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500294}
295
Jason Wange9d74172012-12-07 07:04:55 +0000296static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500297{
Jason Wange9d74172012-12-07 07:04:55 +0000298 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500299
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000300 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000301 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000302 /* clear private here, it is used to chain pages */
303 p->private = 0;
304 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500305 p = alloc_page(gfp_mask);
306 return p;
307}
308
Willem de Bruijne4e84522017-04-24 13:49:26 -0400309static void virtqueue_napi_schedule(struct napi_struct *napi,
310 struct virtqueue *vq)
311{
312 if (napi_schedule_prep(napi)) {
313 virtqueue_disable_cb(vq);
314 __napi_schedule(napi);
315 }
316}
317
318static void virtqueue_napi_complete(struct napi_struct *napi,
319 struct virtqueue *vq, int processed)
320{
321 int opaque;
322
323 opaque = virtqueue_enable_cb_prepare(vq);
Toshiaki Makitafdaa7672017-12-07 13:15:15 +0900324 if (napi_complete_done(napi, processed)) {
325 if (unlikely(virtqueue_poll(vq, opaque)))
326 virtqueue_napi_schedule(napi, vq);
327 } else {
328 virtqueue_disable_cb(vq);
329 }
Willem de Bruijne4e84522017-04-24 13:49:26 -0400330}
331
Jason Wange9d74172012-12-07 07:04:55 +0000332static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000333{
Jason Wange9d74172012-12-07 07:04:55 +0000334 struct virtnet_info *vi = vq->vdev->priv;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400335 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000336
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500337 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000338 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000339
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400340 if (napi->weight)
341 virtqueue_napi_schedule(napi, vq);
342 else
343 /* We were probably waiting for more output buffers. */
344 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000345}
346
Jason Wang28b39bc2017-07-19 16:54:46 +0800347#define MRG_CTX_HEADER_SHIFT 22
348static void *mergeable_len_to_ctx(unsigned int truesize,
349 unsigned int headroom)
350{
351 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
352}
353
354static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
355{
356 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
357}
358
359static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
360{
361 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
362}
363
Mike Waychison34646452012-01-04 12:52:32 +0000364/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300365static struct sk_buff *page_to_skb(struct virtnet_info *vi,
366 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700367 struct page *page, unsigned int offset,
Jason Wang436c9452018-11-29 13:53:16 +0800368 unsigned int len, unsigned int truesize,
369 bool hdr_valid)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000370{
371 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300372 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700373 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000374 char *p;
375
Michael Dalton2613af02013-10-28 15:44:18 -0700376 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000377
378 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100379 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000380 if (unlikely(!skb))
381 return NULL;
382
383 hdr = skb_vnet_hdr(skb);
384
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300385 hdr_len = vi->hdr_len;
386 if (vi->mergeable_rx_bufs)
stephen hemmingera4a76502017-08-15 10:29:17 -0700387 hdr_padded_len = sizeof(*hdr);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300388 else
Michael Dalton2613af02013-10-28 15:44:18 -0700389 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000390
Jason Wang436c9452018-11-29 13:53:16 +0800391 if (hdr_valid)
392 memcpy(hdr, p, hdr_len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000393
394 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700395 offset += hdr_padded_len;
396 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000397
398 copy = len;
399 if (copy > skb_tailroom(skb))
400 copy = skb_tailroom(skb);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200401 skb_put_data(skb, p, copy);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000402
403 len -= copy;
404 offset += copy;
405
Michael Dalton2613af02013-10-28 15:44:18 -0700406 if (vi->mergeable_rx_bufs) {
407 if (len)
408 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
409 else
410 put_page(page);
411 return skb;
412 }
413
Sasha Levine878d782011-09-28 04:40:54 +0000414 /*
415 * Verify that we can indeed put this data into a skb.
416 * This is here to handle cases when the device erroneously
417 * tries to receive more than is possible. This is usually
418 * the case of a broken device.
419 */
420 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000421 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000422 dev_kfree_skb(skb);
423 return NULL;
424 }
Michael Dalton2613af02013-10-28 15:44:18 -0700425 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000426 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700427 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
428 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
429 frag_size, truesize);
430 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000431 page = (struct page *)page->private;
432 offset = 0;
433 }
434
435 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000436 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000437
438 return skb;
439}
440
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200441static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
442 struct send_queue *sq,
443 struct xdp_frame *xdpf)
John Fastabend56434a02016-12-15 12:14:13 -0800444{
John Fastabend56434a02016-12-15 12:14:13 -0800445 struct virtio_net_hdr_mrg_rxbuf *hdr;
John Fastabend56434a02016-12-15 12:14:13 -0800446 int err;
447
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200448 /* virtqueue want to use data area in-front of packet */
449 if (unlikely(xdpf->metasize > 0))
450 return -EOPNOTSUPP;
451
452 if (unlikely(xdpf->headroom < vi->hdr_len))
453 return -EOVERFLOW;
454
455 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
456 xdpf->data -= vi->hdr_len;
Jason Wangf6b10202017-02-21 16:46:28 +0800457 /* Zero header and leave csum up to XDP layers */
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200458 hdr = xdpf->data;
Jason Wangf6b10202017-02-21 16:46:28 +0800459 memset(hdr, 0, vi->hdr_len);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200460 xdpf->len += vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800461
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200462 sg_init_one(sq->sg, xdpf->data, xdpf->len);
Jason Wangbb91acc2016-12-23 22:37:32 +0800463
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200464 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdpf, GFP_ATOMIC);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100465 if (unlikely(err))
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200466 return -ENOSPC; /* Caller handle free/refcnt */
John Fastabend56434a02016-12-15 12:14:13 -0800467
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200468 return 0;
John Fastabend56434a02016-12-15 12:14:13 -0800469}
470
Toshiaki Makita2a435652018-07-23 23:36:07 +0900471static struct send_queue *virtnet_xdp_sq(struct virtnet_info *vi)
472{
473 unsigned int qp;
474
475 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
476 return &vi->sq[qp];
477}
478
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200479static int virtnet_xdp_xmit(struct net_device *dev,
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +0200480 int n, struct xdp_frame **frames, u32 flags)
Jason Wang186b3c92017-09-19 17:42:43 +0800481{
482 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100483 struct receive_queue *rq = vi->rq;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200484 struct xdp_frame *xdpf_sent;
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100485 struct bpf_prog *xdp_prog;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200486 struct send_queue *sq;
487 unsigned int len;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200488 int drops = 0;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900489 int kicks = 0;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900490 int ret, err;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200491 int i;
492
Toshiaki Makita2a435652018-07-23 23:36:07 +0900493 sq = virtnet_xdp_sq(vi);
Jason Wang186b3c92017-09-19 17:42:43 +0800494
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900495 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
496 ret = -EINVAL;
497 drops = n;
498 goto out;
499 }
500
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100501 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
502 * indicate XDP resources have been successfully allocated.
503 */
504 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900505 if (!xdp_prog) {
506 ret = -ENXIO;
507 drops = n;
508 goto out;
509 }
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100510
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200511 /* Free up any pending old buffers before queueing new ones. */
512 while ((xdpf_sent = virtqueue_get_buf(sq->vq, &len)) != NULL)
513 xdp_return_frame(xdpf_sent);
514
515 for (i = 0; i < n; i++) {
516 struct xdp_frame *xdpf = frames[i];
517
518 err = __virtnet_xdp_xmit_one(vi, sq, xdpf);
519 if (err) {
520 xdp_return_frame_rx_napi(xdpf);
521 drops++;
522 }
523 }
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900524 ret = n - drops;
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200525
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900526 if (flags & XDP_XMIT_FLUSH) {
527 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
528 kicks = 1;
529 }
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900530out:
531 u64_stats_update_begin(&sq->stats.syncp);
532 sq->stats.xdp_tx += n;
533 sq->stats.xdp_tx_drops += drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900534 sq->stats.kicks += kicks;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900535 u64_stats_update_end(&sq->stats.syncp);
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200536
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900537 return ret;
Jason Wang186b3c92017-09-19 17:42:43 +0800538}
539
Jason Wangf6b10202017-02-21 16:46:28 +0800540static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
541{
542 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
543}
544
Jason Wang4941d472017-07-19 16:54:48 +0800545/* We copy the packet for XDP in the following cases:
546 *
547 * 1) Packet is scattered across multiple rx buffers.
548 * 2) Headroom space is insufficient.
549 *
550 * This is inefficient but it's a temporary condition that
551 * we hit right after XDP is enabled and until queue is refilled
552 * with large buffers with sufficient headroom - so it should affect
553 * at most queue size packets.
554 * Afterwards, the conditions to enable
555 * XDP should preclude the underlying device from sending packets
556 * across multiple buffers (num_buf > 1), and we make sure buffers
557 * have enough headroom.
John Fastabend72979a62016-12-15 12:14:36 -0800558 */
559static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800560 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800561 struct page *p,
562 int offset,
Jason Wang4941d472017-07-19 16:54:48 +0800563 int page_off,
John Fastabend72979a62016-12-15 12:14:36 -0800564 unsigned int *len)
565{
566 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend72979a62016-12-15 12:14:36 -0800567
568 if (!page)
569 return NULL;
570
571 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
572 page_off += *len;
573
Jason Wang56a86f82016-12-23 22:37:26 +0800574 while (--*num_buf) {
Jason Wang3cc81a92018-03-02 17:29:14 +0800575 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
John Fastabend72979a62016-12-15 12:14:36 -0800576 unsigned int buflen;
John Fastabend72979a62016-12-15 12:14:36 -0800577 void *buf;
578 int off;
579
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200580 buf = virtqueue_get_buf(rq->vq, &buflen);
581 if (unlikely(!buf))
John Fastabend72979a62016-12-15 12:14:36 -0800582 goto err_buf;
583
John Fastabend72979a62016-12-15 12:14:36 -0800584 p = virt_to_head_page(buf);
585 off = buf - page_address(p);
586
Jason Wang56a86f82016-12-23 22:37:26 +0800587 /* guard against a misconfigured or uncooperative backend that
588 * is sending packet larger than the MTU.
589 */
Jason Wang3cc81a92018-03-02 17:29:14 +0800590 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
Jason Wang56a86f82016-12-23 22:37:26 +0800591 put_page(p);
592 goto err_buf;
593 }
594
John Fastabend72979a62016-12-15 12:14:36 -0800595 memcpy(page_address(page) + page_off,
596 page_address(p) + off, buflen);
597 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800598 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800599 }
600
John Fastabend2de2f7f2017-02-02 19:16:29 -0800601 /* Headroom does not contribute to packet length */
602 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800603 return page;
604err_buf:
605 __free_pages(page, 0);
606 return NULL;
607}
608
Jason Wang4941d472017-07-19 16:54:48 +0800609static struct sk_buff *receive_small(struct net_device *dev,
610 struct virtnet_info *vi,
611 struct receive_queue *rq,
612 void *buf, void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800613 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900614 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800615 struct virtnet_rq_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800616{
617 struct sk_buff *skb;
618 struct bpf_prog *xdp_prog;
619 unsigned int xdp_headroom = (unsigned long)ctx;
620 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
621 unsigned int headroom = vi->hdr_len + header_offset;
622 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
623 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
624 struct page *page = virt_to_head_page(buf);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100625 unsigned int delta = 0;
Jason Wang4941d472017-07-19 16:54:48 +0800626 struct page *xdp_page;
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100627 int err;
628
Jason Wang4941d472017-07-19 16:54:48 +0800629 len -= vi->hdr_len;
Jason Wangd46eeea2018-07-31 17:43:39 +0800630 stats->bytes += len;
Jason Wang4941d472017-07-19 16:54:48 +0800631
632 rcu_read_lock();
633 xdp_prog = rcu_dereference(rq->xdp_prog);
634 if (xdp_prog) {
635 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200636 struct xdp_frame *xdpf;
Jason Wang4941d472017-07-19 16:54:48 +0800637 struct xdp_buff xdp;
638 void *orig_data;
639 u32 act;
640
Jesper Dangaard Brouer95dbe9e2018-02-20 14:32:10 +0100641 if (unlikely(hdr->hdr.gso_type))
Jason Wang4941d472017-07-19 16:54:48 +0800642 goto err_xdp;
643
644 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
645 int offset = buf - page_address(page) + header_offset;
646 unsigned int tlen = len + vi->hdr_len;
647 u16 num_buf = 1;
648
649 xdp_headroom = virtnet_get_headroom(vi);
650 header_offset = VIRTNET_RX_PAD + xdp_headroom;
651 headroom = vi->hdr_len + header_offset;
652 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
653 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
654 xdp_page = xdp_linearize_page(rq, &num_buf, page,
655 offset, header_offset,
656 &tlen);
657 if (!xdp_page)
658 goto err_xdp;
659
660 buf = page_address(xdp_page);
661 put_page(page);
662 page = xdp_page;
663 }
664
665 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
666 xdp.data = xdp.data_hard_start + xdp_headroom;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200667 xdp_set_data_meta_invalid(&xdp);
Jason Wang4941d472017-07-19 16:54:48 +0800668 xdp.data_end = xdp.data + len;
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100669 xdp.rxq = &rq->xdp_rxq;
Jason Wang4941d472017-07-19 16:54:48 +0800670 orig_data = xdp.data;
671 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Jason Wangd46eeea2018-07-31 17:43:39 +0800672 stats->xdp_packets++;
Jason Wang4941d472017-07-19 16:54:48 +0800673
674 switch (act) {
675 case XDP_PASS:
676 /* Recalculate length in case bpf program changed it */
677 delta = orig_data - xdp.data;
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700678 len = xdp.data_end - xdp.data;
Jason Wang4941d472017-07-19 16:54:48 +0800679 break;
680 case XDP_TX:
Jason Wangd46eeea2018-07-31 17:43:39 +0800681 stats->xdp_tx++;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200682 xdpf = convert_to_xdp_frame(&xdp);
683 if (unlikely(!xdpf))
684 goto err_xdp;
Jason Wangca9e83b2018-07-31 17:43:38 +0800685 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
686 if (unlikely(err < 0)) {
Jason Wang4941d472017-07-19 16:54:48 +0800687 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100688 goto err_xdp;
689 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200690 *xdp_xmit |= VIRTIO_XDP_TX;
Jason Wang186b3c92017-09-19 17:42:43 +0800691 rcu_read_unlock();
692 goto xdp_xmit;
693 case XDP_REDIRECT:
Jason Wangd46eeea2018-07-31 17:43:39 +0800694 stats->xdp_redirects++;
Jason Wang186b3c92017-09-19 17:42:43 +0800695 err = xdp_do_redirect(dev, &xdp, xdp_prog);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100696 if (err)
697 goto err_xdp;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200698 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang4941d472017-07-19 16:54:48 +0800699 rcu_read_unlock();
700 goto xdp_xmit;
701 default:
702 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvab633d442018-08-04 21:42:05 -0500703 /* fall through */
Jason Wang4941d472017-07-19 16:54:48 +0800704 case XDP_ABORTED:
705 trace_xdp_exception(vi->dev, xdp_prog, act);
706 case XDP_DROP:
707 goto err_xdp;
708 }
709 }
710 rcu_read_unlock();
711
712 skb = build_skb(buf, buflen);
713 if (!skb) {
714 put_page(page);
715 goto err;
716 }
717 skb_reserve(skb, headroom - delta);
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700718 skb_put(skb, len);
Jason Wang4941d472017-07-19 16:54:48 +0800719 if (!delta) {
720 buf += header_offset;
721 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
722 } /* keep zeroed vnet hdr since packet was changed by bpf */
723
724err:
725 return skb;
726
727err_xdp:
728 rcu_read_unlock();
Jason Wangd46eeea2018-07-31 17:43:39 +0800729 stats->xdp_drops++;
730 stats->drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800731 put_page(page);
732xdp_xmit:
733 return NULL;
734}
735
736static struct sk_buff *receive_big(struct net_device *dev,
737 struct virtnet_info *vi,
738 struct receive_queue *rq,
739 void *buf,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900740 unsigned int len,
Jason Wangd46eeea2018-07-31 17:43:39 +0800741 struct virtnet_rq_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800742{
743 struct page *page = buf;
Jason Wang436c9452018-11-29 13:53:16 +0800744 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len,
745 PAGE_SIZE, true);
Jason Wang4941d472017-07-19 16:54:48 +0800746
Jason Wangd46eeea2018-07-31 17:43:39 +0800747 stats->bytes += len - vi->hdr_len;
Jason Wang4941d472017-07-19 16:54:48 +0800748 if (unlikely(!skb))
749 goto err;
750
751 return skb;
752
753err:
Jason Wangd46eeea2018-07-31 17:43:39 +0800754 stats->drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800755 give_pages(rq, page);
756 return NULL;
757}
758
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200759static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200760 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200761 struct receive_queue *rq,
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200762 void *buf,
763 void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800764 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900765 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800766 struct virtnet_rq_stats *stats)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000767{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300768 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
769 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200770 struct page *page = virt_to_head_page(buf);
771 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800772 struct sk_buff *head_skb, *curr_skb;
773 struct bpf_prog *xdp_prog;
774 unsigned int truesize;
Jason Wang4941d472017-07-19 16:54:48 +0800775 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
Jason Wang3cc81a92018-03-02 17:29:14 +0800776 int err;
Michael Daltonab7db912014-01-16 22:23:27 -0800777
John Fastabend56434a02016-12-15 12:14:13 -0800778 head_skb = NULL;
Jason Wangd46eeea2018-07-31 17:43:39 +0800779 stats->bytes += len - vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800780
John Fastabendf600b692016-12-15 12:13:24 -0800781 rcu_read_lock();
782 xdp_prog = rcu_dereference(rq->xdp_prog);
783 if (xdp_prog) {
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200784 struct xdp_frame *xdpf;
John Fastabend72979a62016-12-15 12:14:36 -0800785 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800786 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800787 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800788 u32 act;
789
Jason Wang3d62b2a2018-05-22 11:44:31 +0800790 /* Transient failure which in theory could occur if
791 * in-flight packets from before XDP was enabled reach
792 * the receive path after XDP is loaded.
793 */
794 if (unlikely(hdr->hdr.gso_type))
795 goto err_xdp;
796
Jason Wang3cc81a92018-03-02 17:29:14 +0800797 /* This happens when rx buffer size is underestimated
798 * or headroom is not enough because of the buffer
799 * was refilled before XDP is set. This should only
800 * happen for the first several packets, so we don't
801 * care much about its performance.
802 */
Jason Wang4941d472017-07-19 16:54:48 +0800803 if (unlikely(num_buf > 1 ||
804 headroom < virtnet_get_headroom(vi))) {
John Fastabend72979a62016-12-15 12:14:36 -0800805 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800806 xdp_page = xdp_linearize_page(rq, &num_buf,
Jason Wang4941d472017-07-19 16:54:48 +0800807 page, offset,
808 VIRTIO_XDP_HEADROOM,
809 &len);
John Fastabend72979a62016-12-15 12:14:36 -0800810 if (!xdp_page)
811 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800812 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800813 } else {
814 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800815 }
816
John Fastabend2de2f7f2017-02-02 19:16:29 -0800817 /* Allow consuming headroom but reserve enough space to push
818 * the descriptor on if we get an XDP_TX return code.
819 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800820 data = page_address(xdp_page) + offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800821 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
John Fastabend0354e4d2017-02-02 19:15:01 -0800822 xdp.data = data + vi->hdr_len;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200823 xdp_set_data_meta_invalid(&xdp);
John Fastabend0354e4d2017-02-02 19:15:01 -0800824 xdp.data_end = xdp.data + (len - vi->hdr_len);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100825 xdp.rxq = &rq->xdp_rxq;
826
John Fastabend0354e4d2017-02-02 19:15:01 -0800827 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Jason Wangd46eeea2018-07-31 17:43:39 +0800828 stats->xdp_packets++;
John Fastabend0354e4d2017-02-02 19:15:01 -0800829
John Fastabend56434a02016-12-15 12:14:13 -0800830 switch (act) {
831 case XDP_PASS:
John Fastabend2de2f7f2017-02-02 19:16:29 -0800832 /* recalculate offset to account for any header
833 * adjustments. Note other cases do not build an
834 * skb and avoid using offset
835 */
836 offset = xdp.data -
837 page_address(xdp_page) - vi->hdr_len;
838
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700839 /* recalculate len if xdp.data or xdp.data_end were
840 * adjusted
841 */
Nikita V. Shirokovaaa64522018-04-22 21:16:48 -0700842 len = xdp.data_end - xdp.data + vi->hdr_len;
Jason Wang1830f892016-12-23 22:37:27 +0800843 /* We can only create skb based on xdp_page. */
844 if (unlikely(xdp_page != page)) {
845 rcu_read_unlock();
846 put_page(page);
847 head_skb = page_to_skb(vi, rq, xdp_page,
Jason Wang436c9452018-11-29 13:53:16 +0800848 offset, len,
849 PAGE_SIZE, false);
Jason Wang1830f892016-12-23 22:37:27 +0800850 return head_skb;
851 }
John Fastabend56434a02016-12-15 12:14:13 -0800852 break;
853 case XDP_TX:
Jason Wangd46eeea2018-07-31 17:43:39 +0800854 stats->xdp_tx++;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200855 xdpf = convert_to_xdp_frame(&xdp);
856 if (unlikely(!xdpf))
857 goto err_xdp;
Jason Wangca9e83b2018-07-31 17:43:38 +0800858 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
859 if (unlikely(err < 0)) {
John Fastabend0354e4d2017-02-02 19:15:01 -0800860 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100861 if (unlikely(xdp_page != page))
862 put_page(xdp_page);
863 goto err_xdp;
864 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200865 *xdp_xmit |= VIRTIO_XDP_TX;
John Fastabend72979a62016-12-15 12:14:36 -0800866 if (unlikely(xdp_page != page))
Jason Wang5d458a12018-05-22 11:44:29 +0800867 put_page(page);
John Fastabend56434a02016-12-15 12:14:13 -0800868 rcu_read_unlock();
869 goto xdp_xmit;
Jason Wang3cc81a92018-03-02 17:29:14 +0800870 case XDP_REDIRECT:
Jason Wangd46eeea2018-07-31 17:43:39 +0800871 stats->xdp_redirects++;
Jason Wang3cc81a92018-03-02 17:29:14 +0800872 err = xdp_do_redirect(dev, &xdp, xdp_prog);
873 if (err) {
874 if (unlikely(xdp_page != page))
875 put_page(xdp_page);
876 goto err_xdp;
877 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200878 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang3cc81a92018-03-02 17:29:14 +0800879 if (unlikely(xdp_page != page))
Jason Wang68904182018-05-22 11:44:28 +0800880 put_page(page);
Jason Wang3cc81a92018-03-02 17:29:14 +0800881 rcu_read_unlock();
882 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -0800883 default:
John Fastabend0354e4d2017-02-02 19:15:01 -0800884 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvab633d442018-08-04 21:42:05 -0500885 /* fall through */
John Fastabend0354e4d2017-02-02 19:15:01 -0800886 case XDP_ABORTED:
887 trace_xdp_exception(vi->dev, xdp_prog, act);
Gustavo A. R. Silvab633d442018-08-04 21:42:05 -0500888 /* fall through */
John Fastabend0354e4d2017-02-02 19:15:01 -0800889 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -0800890 if (unlikely(xdp_page != page))
891 __free_pages(xdp_page, 0);
John Fastabendf600b692016-12-15 12:13:24 -0800892 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800893 }
John Fastabendf600b692016-12-15 12:13:24 -0800894 }
895 rcu_read_unlock();
896
Jason Wang28b39bc2017-07-19 16:54:46 +0800897 truesize = mergeable_ctx_to_truesize(ctx);
898 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +0300899 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200900 dev->name, len, (unsigned long)ctx);
901 dev->stats.rx_length_errors++;
902 goto err_skb;
903 }
Jason Wang28b39bc2017-07-19 16:54:46 +0800904
Jason Wang436c9452018-11-29 13:53:16 +0800905 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog);
John Fastabendf600b692016-12-15 12:13:24 -0800906 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000907
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200908 if (unlikely(!curr_skb))
909 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000910 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200911 int num_skb_frags;
912
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200913 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
Yunjian Wang03e9f8a2017-12-04 14:02:19 +0800914 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200915 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200916 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300917 virtio16_to_cpu(vi->vdev,
918 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200919 dev->stats.rx_length_errors++;
920 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000921 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200922
Jason Wangd46eeea2018-07-31 17:43:39 +0800923 stats->bytes += len;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200924 page = virt_to_head_page(buf);
Jason Wang28b39bc2017-07-19 16:54:46 +0800925
926 truesize = mergeable_ctx_to_truesize(ctx);
927 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +0300928 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200929 dev->name, len, (unsigned long)ctx);
930 dev->stats.rx_length_errors++;
931 goto err_skb;
932 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200933
934 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700935 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
936 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200937
938 if (unlikely(!nskb))
939 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700940 if (curr_skb == head_skb)
941 skb_shinfo(curr_skb)->frag_list = nskb;
942 else
943 curr_skb->next = nskb;
944 curr_skb = nskb;
945 head_skb->truesize += nskb->truesize;
946 num_skb_frags = 0;
947 }
948 if (curr_skb != head_skb) {
949 head_skb->data_len += len;
950 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800951 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700952 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200953 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800954 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
955 put_page(page);
956 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800957 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800958 } else {
959 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800960 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800961 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200962 }
963
Johannes Berg5377d7582015-08-19 09:48:40 +0200964 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200965 return head_skb;
966
John Fastabendf600b692016-12-15 12:13:24 -0800967err_xdp:
968 rcu_read_unlock();
Jason Wangd46eeea2018-07-31 17:43:39 +0800969 stats->xdp_drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200970err_skb:
971 put_page(page);
Jason Wang850e0882018-05-22 11:44:30 +0800972 while (num_buf-- > 1) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200973 buf = virtqueue_get_buf(rq->vq, &len);
974 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200975 pr_debug("%s: rx error: %d buffers missing\n",
976 dev->name, num_buf);
977 dev->stats.rx_length_errors++;
978 break;
979 }
Jason Wangd46eeea2018-07-31 17:43:39 +0800980 stats->bytes += len;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200981 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200982 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000983 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200984err_buf:
Jason Wangd46eeea2018-07-31 17:43:39 +0800985 stats->drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200986 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800987xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200988 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000989}
990
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900991static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
992 void *buf, unsigned int len, void **ctx,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +0900993 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800994 struct virtnet_rq_stats *stats)
Rusty Russell296f96f2007-10-22 11:03:37 +1000995{
Jason Wange9d74172012-12-07 07:04:55 +0000996 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000997 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300998 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000999
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +03001000 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001001 pr_debug("%s: short packet %i\n", dev->name, len);
1002 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -08001003 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001004 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001005 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -08001006 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001007 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08001008 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001009 }
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001010 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001011 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001012
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001013 if (vi->mergeable_rx_bufs)
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001014 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001015 stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001016 else if (vi->big_packets)
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001017 skb = receive_big(dev, vi, rq, buf, len, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001018 else
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001019 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001020
1021 if (unlikely(!skb))
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001022 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001023
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001024 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001025
Mike Rapoporte858fae2016-06-08 16:09:21 +03001026 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +00001027 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +10001028
Mike Rapoporte858fae2016-06-08 16:09:21 +03001029 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1030 virtio_is_little_endian(vi->vdev))) {
1031 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1032 dev->name, hdr->hdr.gso_type,
1033 hdr->hdr.gso_size);
1034 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001035 }
1036
Mike Rapoportd1dc06d2016-06-14 08:29:38 +03001037 skb->protocol = eth_type_trans(skb, dev);
1038 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1039 ntohs(skb->protocol), skb->len, skb->pkt_type);
1040
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001041 napi_gro_receive(&rq->napi, skb);
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001042 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001043
1044frame_err:
1045 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +10001046 dev_kfree_skb(skb);
1047}
1048
Jason Wang192f68c2017-07-19 16:54:47 +08001049/* Unlike mergeable buffers, all buffers are allocated to the
1050 * same size, except for the headroom. For this reason we do
1051 * not need to use mergeable_len_to_ctx here - it is enough
1052 * to store the headroom as the context ignoring the truesize.
1053 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001054static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1055 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +10001056{
Jason Wangf6b10202017-02-21 16:46:28 +08001057 struct page_frag *alloc_frag = &rq->alloc_frag;
1058 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001059 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wang192f68c2017-07-19 16:54:47 +08001060 void *ctx = (void *)(unsigned long)xdp_headroom;
Jason Wangf6b10202017-02-21 16:46:28 +08001061 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001062 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001063
Jason Wangf6b10202017-02-21 16:46:28 +08001064 len = SKB_DATA_ALIGN(len) +
1065 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1066 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001067 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001068
Jason Wangf6b10202017-02-21 16:46:28 +08001069 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1070 get_page(alloc_frag->page);
1071 alloc_frag->offset += len;
1072 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1073 vi->hdr_len + GOOD_PACKET_LEN);
Jason Wang192f68c2017-07-19 16:54:47 +08001074 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001075 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +08001076 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001077 return err;
1078}
1079
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001080static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1081 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001082{
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001083 struct page *first, *list = NULL;
1084 char *p;
1085 int i, err, offset;
1086
Rusty Russella5835442014-09-11 10:17:36 +09301087 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1088
Jason Wange9d74172012-12-07 07:04:55 +00001089 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001090 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +00001091 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001092 if (!first) {
1093 if (list)
Jason Wange9d74172012-12-07 07:04:55 +00001094 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001095 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -07001096 }
Jason Wange9d74172012-12-07 07:04:55 +00001097 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +10001098
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001099 /* chain new page in list head to match sg */
1100 first->private = (unsigned long)list;
1101 list = first;
1102 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001103
Jason Wange9d74172012-12-07 07:04:55 +00001104 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001105 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +00001106 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001107 return -ENOMEM;
1108 }
1109 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +08001110
Jason Wange9d74172012-12-07 07:04:55 +00001111 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001112 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1113 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +08001114
Jason Wange9d74172012-12-07 07:04:55 +00001115 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001116 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +00001117 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +08001118
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001119 /* chain first in list head */
1120 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301121 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1122 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001123 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +00001124 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +08001125
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001126 return err;
1127}
Herbert Xu97402b92008-04-18 11:24:27 +08001128
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02001129static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
Jason Wang3cc81a92018-03-02 17:29:14 +08001130 struct ewma_pkt_len *avg_pkt_len,
1131 unsigned int room)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001132{
Michael Daltonab7db912014-01-16 22:23:27 -08001133 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001134 unsigned int len;
1135
Jason Wang3cc81a92018-03-02 17:29:14 +08001136 if (room)
1137 return PAGE_SIZE - room;
1138
1139 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03001140 rq->min_buf_len, PAGE_SIZE - hdr_len);
Jason Wang3cc81a92018-03-02 17:29:14 +08001141
Michael S. Tsirkine377fcc2017-03-06 22:21:35 +02001142 return ALIGN(len, L1_CACHE_BYTES);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001143}
1144
John Fastabend2de2f7f2017-02-02 19:16:29 -08001145static int add_recvbuf_mergeable(struct virtnet_info *vi,
1146 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -08001147{
Michael Daltonfb518792014-01-16 22:23:26 -08001148 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001149 unsigned int headroom = virtnet_get_headroom(vi);
Jason Wang3cc81a92018-03-02 17:29:14 +08001150 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1151 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
Michael Daltonfb518792014-01-16 22:23:26 -08001152 char *buf;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001153 void *ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001154 int err;
Michael Daltonfb518792014-01-16 22:23:26 -08001155 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +10001156
Jason Wang3cc81a92018-03-02 17:29:14 +08001157 /* Extra tailroom is needed to satisfy XDP's assumption. This
1158 * means rx frags coalescing won't work, but consider we've
1159 * disabled GSO for XDP, it won't be a big issue.
1160 */
1161 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1162 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001163 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -08001164
Michael Daltonfb518792014-01-16 22:23:26 -08001165 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001166 buf += headroom; /* advance address leaving hole at front of pkt */
Michael Daltonfb518792014-01-16 22:23:26 -08001167 get_page(alloc_frag->page);
Jason Wang3cc81a92018-03-02 17:29:14 +08001168 alloc_frag->offset += len + room;
Michael Daltonfb518792014-01-16 22:23:26 -08001169 hole = alloc_frag->size - alloc_frag->offset;
Jason Wang3cc81a92018-03-02 17:29:14 +08001170 if (hole < len + room) {
Michael Daltonab7db912014-01-16 22:23:27 -08001171 /* To avoid internal fragmentation, if there is very likely not
1172 * enough space for another buffer, add the remaining space to
Michael S. Tsirkin1daa8792017-07-31 21:49:49 +03001173 * the current buffer.
Michael Daltonab7db912014-01-16 22:23:27 -08001174 */
Michael Daltonfb518792014-01-16 22:23:26 -08001175 len += hole;
1176 alloc_frag->offset += hole;
1177 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001178
Michael Daltonfb518792014-01-16 22:23:26 -08001179 sg_init_one(rq->sg, buf, len);
David S. Miller29fda252017-08-01 10:07:50 -07001180 ctx = mergeable_len_to_ctx(len, headroom);
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001181 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001182 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -07001183 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001184
1185 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001186}
1187
Rusty Russellb2baed62011-12-29 00:42:38 +00001188/*
1189 * Returns false if we couldn't fill entirely (OOM).
1190 *
1191 * Normally run in the receive path, but can also be run from ndo_open
1192 * before we're receiving packets, or from refill_work which is
1193 * careful to disable receiving (using napi_disable).
1194 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001195static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1196 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001197{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001198 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001199 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001200
Amit Shah0aea51c2009-08-26 14:58:28 +05301201 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001202 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001203 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001204 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001205 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001206 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001207 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001208
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001209 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301210 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001211 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001212 } while (rq->vq->num_free);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001213 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1214 u64_stats_update_begin(&rq->stats.syncp);
Jason Wangd46eeea2018-07-31 17:43:39 +08001215 rq->stats.kicks++;
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001216 u64_stats_update_end(&rq->stats.syncp);
1217 }
1218
Rusty Russell3161e452009-08-26 12:22:32 -07001219 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001220}
1221
Rusty Russell18445c42008-02-04 23:49:57 -05001222static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001223{
1224 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001225 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +00001226
Willem de Bruijne4e84522017-04-24 13:49:26 -04001227 virtqueue_napi_schedule(&rq->napi, rvq);
Rusty Russell296f96f2007-10-22 11:03:37 +10001228}
1229
Willem de Bruijne4e84522017-04-24 13:49:26 -04001230static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001231{
Willem de Bruijne4e84522017-04-24 13:49:26 -04001232 napi_enable(napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001233
1234 /* If all buffers were filled by other side before we napi_enabled, we
Willem de Bruijne4e84522017-04-24 13:49:26 -04001235 * won't get another interrupt, so process any outstanding packets now.
1236 * Call local_bh_enable after to trigger softIRQ processing.
1237 */
1238 local_bh_disable();
1239 virtqueue_napi_schedule(napi, vq);
1240 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001241}
1242
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001243static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1244 struct virtqueue *vq,
1245 struct napi_struct *napi)
1246{
1247 if (!napi->weight)
1248 return;
1249
1250 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1251 * enable the feature if this is likely affine with the transmit path.
1252 */
1253 if (!vi->affinity_hint_set) {
1254 napi->weight = 0;
1255 return;
1256 }
1257
1258 return virtnet_napi_enable(vq, napi);
1259}
1260
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001261static void virtnet_napi_tx_disable(struct napi_struct *napi)
1262{
1263 if (napi->weight)
1264 napi_disable(napi);
1265}
1266
Rusty Russell3161e452009-08-26 12:22:32 -07001267static void refill_work(struct work_struct *work)
1268{
Jason Wange9d74172012-12-07 07:04:55 +00001269 struct virtnet_info *vi =
1270 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -07001271 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +00001272 int i;
Rusty Russell3161e452009-08-26 12:22:32 -07001273
Sasha Levin55257d72013-04-29 12:00:08 +09301274 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001275 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -07001276
Jason Wang986a4f42012-12-07 07:04:56 +00001277 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001278 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001279 virtnet_napi_enable(rq->vq, &rq->napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001280
1281 /* In theory, this can happen: if we don't get any buffers in
1282 * we will *never* try to fill again.
1283 */
1284 if (still_empty)
1285 schedule_delayed_work(&vi->refill, HZ/2);
1286 }
Rusty Russell3161e452009-08-26 12:22:32 -07001287}
1288
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001289static int virtnet_receive(struct receive_queue *rq, int budget,
1290 unsigned int *xdp_xmit)
Rusty Russell296f96f2007-10-22 11:03:37 +10001291{
Jason Wange9d74172012-12-07 07:04:55 +00001292 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wangd46eeea2018-07-31 17:43:39 +08001293 struct virtnet_rq_stats stats = {};
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001294 unsigned int len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001295 void *buf;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001296 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001297
Jason Wang192f68c2017-07-19 16:54:47 +08001298 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001299 void *ctx;
1300
Jason Wangd46eeea2018-07-31 17:43:39 +08001301 while (stats.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001302 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001303 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
Jason Wangd46eeea2018-07-31 17:43:39 +08001304 stats.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001305 }
1306 } else {
Jason Wangd46eeea2018-07-31 17:43:39 +08001307 while (stats.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001308 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001309 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
Jason Wangd46eeea2018-07-31 17:43:39 +08001310 stats.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001311 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001312 }
1313
Jason Wangbe121f42014-01-16 14:45:24 +08001314 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001315 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001316 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -07001317 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001318
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001319 u64_stats_update_begin(&rq->stats.syncp);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001320 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1321 size_t offset = virtnet_rq_stats_desc[i].offset;
1322 u64 *item;
1323
Jason Wangd46eeea2018-07-31 17:43:39 +08001324 item = (u64 *)((u8 *)&rq->stats + offset);
1325 *item += *(u64 *)((u8 *)&stats + offset);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001326 }
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001327 u64_stats_update_end(&rq->stats.syncp);
Jason Wang61845d22017-02-17 11:33:09 +08001328
Jason Wangd46eeea2018-07-31 17:43:39 +08001329 return stats.packets;
Jason Wang2ffa7592014-07-23 16:33:54 +08001330}
1331
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001332static void free_old_xmit_skbs(struct send_queue *sq)
1333{
1334 struct sk_buff *skb;
1335 unsigned int len;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001336 unsigned int packets = 0;
1337 unsigned int bytes = 0;
1338
1339 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1340 pr_debug("Sent skb %p\n", skb);
1341
1342 bytes += skb->len;
1343 packets++;
1344
Eric Dumazetdadc0732017-08-24 09:02:49 -07001345 dev_consume_skb_any(skb);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001346 }
1347
1348 /* Avoid overhead when no packets have been processed
1349 * happens when called speculatively from start_xmit.
1350 */
1351 if (!packets)
1352 return;
1353
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001354 u64_stats_update_begin(&sq->stats.syncp);
1355 sq->stats.bytes += bytes;
1356 sq->stats.packets += packets;
1357 u64_stats_update_end(&sq->stats.syncp);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001358}
1359
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001360static void virtnet_poll_cleantx(struct receive_queue *rq)
1361{
1362 struct virtnet_info *vi = rq->vq->vdev->priv;
1363 unsigned int index = vq2rxq(rq->vq);
1364 struct send_queue *sq = &vi->sq[index];
1365 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1366
1367 if (!sq->napi.weight)
1368 return;
1369
1370 if (__netif_tx_trylock(txq)) {
1371 free_old_xmit_skbs(sq);
1372 __netif_tx_unlock(txq);
1373 }
1374
1375 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1376 netif_tx_wake_queue(txq);
1377}
1378
Jason Wang2ffa7592014-07-23 16:33:54 +08001379static int virtnet_poll(struct napi_struct *napi, int budget)
1380{
1381 struct receive_queue *rq =
1382 container_of(napi, struct receive_queue, napi);
Jason Wang9267c432018-04-13 14:58:25 +08001383 struct virtnet_info *vi = rq->vq->vdev->priv;
1384 struct send_queue *sq;
Toshiaki Makita2a435652018-07-23 23:36:07 +09001385 unsigned int received;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001386 unsigned int xdp_xmit = 0;
Jason Wang2ffa7592014-07-23 16:33:54 +08001387
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001388 virtnet_poll_cleantx(rq);
1389
Jason Wang186b3c92017-09-19 17:42:43 +08001390 received = virtnet_receive(rq, budget, &xdp_xmit);
Jason Wang2ffa7592014-07-23 16:33:54 +08001391
Rusty Russell8329d982007-11-19 11:20:43 -05001392 /* Out of packets? */
Willem de Bruijne4e84522017-04-24 13:49:26 -04001393 if (received < budget)
1394 virtqueue_napi_complete(napi, rq->vq, received);
Rusty Russell296f96f2007-10-22 11:03:37 +10001395
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001396 if (xdp_xmit & VIRTIO_XDP_REDIR)
1397 xdp_do_flush_map();
1398
1399 if (xdp_xmit & VIRTIO_XDP_TX) {
Toshiaki Makita2a435652018-07-23 23:36:07 +09001400 sq = virtnet_xdp_sq(vi);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001401 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1402 u64_stats_update_begin(&sq->stats.syncp);
1403 sq->stats.kicks++;
1404 u64_stats_update_end(&sq->stats.syncp);
1405 }
Jason Wang9267c432018-04-13 14:58:25 +08001406 }
Jason Wang186b3c92017-09-19 17:42:43 +08001407
Rusty Russell296f96f2007-10-22 11:03:37 +10001408 return received;
1409}
1410
Jason Wang986a4f42012-12-07 07:04:56 +00001411static int virtnet_open(struct net_device *dev)
1412{
1413 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001414 int i, err;
Jason Wang986a4f42012-12-07 07:04:56 +00001415
Jason Wange4166622013-05-21 20:03:58 +00001416 for (i = 0; i < vi->max_queue_pairs; i++) {
1417 if (i < vi->curr_queue_pairs)
1418 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001419 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001420 schedule_delayed_work(&vi->refill, 0);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001421
1422 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i);
1423 if (err < 0)
1424 return err;
1425
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +02001426 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1427 MEM_TYPE_PAGE_SHARED, NULL);
1428 if (err < 0) {
1429 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1430 return err;
1431 }
1432
Willem de Bruijne4e84522017-04-24 13:49:26 -04001433 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001434 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001435 }
1436
1437 return 0;
1438}
1439
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001440static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1441{
1442 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1443 struct virtnet_info *vi = sq->vq->vdev->priv;
1444 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
1445
1446 __netif_tx_lock(txq, raw_smp_processor_id());
1447 free_old_xmit_skbs(sq);
1448 __netif_tx_unlock(txq);
1449
1450 virtqueue_napi_complete(napi, sq->vq, 0);
1451
1452 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1453 netif_tx_wake_queue(txq);
1454
1455 return 0;
1456}
1457
Jason Wange9d74172012-12-07 07:04:55 +00001458static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001459{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001460 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001461 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001462 struct virtnet_info *vi = sq->vq->vdev->priv;
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001463 int num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001464 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301465 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001466
Johannes Berge1749612008-10-27 15:59:26 -07001467 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301468
1469 can_push = vi->any_header_sg &&
1470 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1471 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1472 /* Even if we can, don't push here yet as this would skew
1473 * csum_start offset below. */
1474 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001475 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301476 else
1477 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001478
Mike Rapoporte858fae2016-06-08 16:09:21 +03001479 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04001480 virtio_is_little_endian(vi->vdev), false,
1481 0))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001482 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001483
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001484 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001485 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001486
Jason Wang547c8902015-08-27 14:53:06 +08001487 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301488 if (can_push) {
1489 __skb_push(skb, hdr_len);
1490 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001491 if (unlikely(num_sg < 0))
1492 return num_sg;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301493 /* Pull header back to avoid skew in tx bytes calculations. */
1494 __skb_pull(skb, hdr_len);
1495 } else {
1496 sg_set_buf(sq->sg, hdr, hdr_len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001497 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1498 if (unlikely(num_sg < 0))
1499 return num_sg;
1500 num_sg++;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301501 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301502 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001503}
1504
Stephen Hemminger424efe92009-08-31 19:50:51 +00001505static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001506{
1507 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001508 int qnum = skb_get_queue_mapping(skb);
1509 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301510 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001511 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1512 bool kick = !skb->xmit_more;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001513 bool use_napi = sq->napi.weight;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001514
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001515 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001516 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001517
Willem de Bruijnbdb12e02017-04-24 13:49:30 -04001518 if (use_napi && kick)
1519 virtqueue_enable_cb_delayed(sq->vq);
1520
Jacob Keller074c3582014-06-25 02:37:13 +00001521 /* timestamp packet in software */
1522 skb_tx_timestamp(skb);
1523
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001524 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001525 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001526
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301527 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001528 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301529 dev->stats.tx_fifo_errors++;
1530 if (net_ratelimit())
1531 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001532 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001533 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001534 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001535 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001536 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001537
Rusty Russell48925e32009-09-24 09:59:20 -06001538 /* Don't wait up for transmitted skbs to be freed. */
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001539 if (!use_napi) {
1540 skb_orphan(skb);
1541 nf_reset(skb);
1542 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001543
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001544 /* If running out of space, stop queue to avoid getting packets that we
1545 * are then unable to transmit.
1546 * An alternative would be to force queuing layer to requeue the skb by
1547 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1548 * returned in a normal path of operation: it means that driver is not
1549 * maintaining the TX queue stop/start state properly, and causes
1550 * the stack to do a non-trivial amount of useless work.
1551 * Since most packets only take 1 or 2 ring slots, stopping the queue
1552 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001553 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001554 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001555 netif_stop_subqueue(dev, qnum);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001556 if (!use_napi &&
1557 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001558 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001559 free_old_xmit_skbs(sq);
1560 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001561 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001562 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001563 }
1564 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001565 }
Rusty Russell48925e32009-09-24 09:59:20 -06001566
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001567 if (kick || netif_xmit_stopped(txq)) {
1568 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1569 u64_stats_update_begin(&sq->stats.syncp);
1570 sq->stats.kicks++;
1571 u64_stats_update_end(&sq->stats.syncp);
1572 }
1573 }
David S. Miller0b725a22014-08-25 15:51:53 -07001574
Rusty Russell48925e32009-09-24 09:59:20 -06001575 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001576}
1577
Amos Kong40cbfc32013-01-21 01:17:21 +00001578/*
1579 * Send command via the control virtqueue and check status. Commands
1580 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001581 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001582 */
1583static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001584 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001585{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301586 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001587 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001588
1589 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301590 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001591
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001592 vi->ctrl->status = ~0;
1593 vi->ctrl->hdr.class = class;
1594 vi->ctrl->hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301595 /* Add header */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001596 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301597 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001598
Rusty Russellf7bc9592013-03-20 15:44:28 +10301599 if (out)
1600 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001601
Rusty Russellf7bc9592013-03-20 15:44:28 +10301602 /* Add return status. */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001603 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001604 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001605
stephen hemmingerd24bae32013-12-09 16:17:40 -08001606 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301607 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001608
Heinz Graalfs67975902013-10-29 09:40:02 +10301609 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001610 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001611
1612 /* Spin for a response, the kick causes an ioport write, trapping
1613 * into the hypervisor, so the request should be handled immediately.
1614 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301615 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1616 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001617 cpu_relax();
1618
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001619 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001620}
1621
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001622static int virtnet_set_mac_address(struct net_device *dev, void *p)
1623{
1624 struct virtnet_info *vi = netdev_priv(dev);
1625 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001626 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001627 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001628 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001629
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07001630 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1631 return -EOPNOTSUPP;
1632
Shyam Saini801822d2016-12-24 00:44:58 +05301633 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001634 if (!addr)
1635 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001636
1637 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001638 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001639 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001640
Amos Kong7e58d5a2013-01-21 01:17:23 +00001641 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1642 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1643 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001644 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001645 dev_warn(&vdev->dev,
1646 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001647 ret = -EINVAL;
1648 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001649 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001650 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1651 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301652 unsigned int i;
1653
1654 /* Naturally, this has an atomicity problem. */
1655 for (i = 0; i < dev->addr_len; i++)
1656 virtio_cwrite8(vdev,
1657 offsetof(struct virtio_net_config, mac) +
1658 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001659 }
1660
1661 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001662 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001663
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001664out:
1665 kfree(addr);
1666 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001667}
1668
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001669static void virtnet_stats(struct net_device *dev,
1670 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001671{
1672 struct virtnet_info *vi = netdev_priv(dev);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001673 unsigned int start;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001674 int i;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001675
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001676 for (i = 0; i < vi->max_queue_pairs; i++) {
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001677 u64 tpackets, tbytes, rpackets, rbytes, rdrops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001678 struct receive_queue *rq = &vi->rq[i];
1679 struct send_queue *sq = &vi->sq[i];
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001680
1681 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001682 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1683 tpackets = sq->stats.packets;
1684 tbytes = sq->stats.bytes;
1685 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001686
1687 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001688 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
Jason Wangd46eeea2018-07-31 17:43:39 +08001689 rpackets = rq->stats.packets;
1690 rbytes = rq->stats.bytes;
1691 rdrops = rq->stats.drops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001692 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001693
1694 tot->rx_packets += rpackets;
1695 tot->tx_packets += tpackets;
1696 tot->rx_bytes += rbytes;
1697 tot->tx_bytes += tbytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001698 tot->rx_dropped += rdrops;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001699 }
1700
1701 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001702 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001703 tot->rx_length_errors = dev->stats.rx_length_errors;
1704 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001705}
1706
Jason Wang586d17c2012-04-11 20:43:52 +00001707static void virtnet_ack_link_announce(struct virtnet_info *vi)
1708{
1709 rtnl_lock();
1710 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001711 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001712 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1713 rtnl_unlock();
1714}
1715
John Fastabend473153292017-02-02 19:14:32 -08001716static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001717{
1718 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001719 struct net_device *dev = vi->dev;
1720
1721 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1722 return 0;
1723
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001724 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1725 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001726
1727 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001728 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001729 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1730 queue_pairs);
1731 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301732 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001733 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001734 /* virtnet_open() will refill when device is going to up. */
1735 if (dev->flags & IFF_UP)
1736 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301737 }
Jason Wang986a4f42012-12-07 07:04:56 +00001738
1739 return 0;
1740}
1741
John Fastabend473153292017-02-02 19:14:32 -08001742static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1743{
1744 int err;
1745
1746 rtnl_lock();
1747 err = _virtnet_set_queues(vi, queue_pairs);
1748 rtnl_unlock();
1749 return err;
1750}
1751
Rusty Russell296f96f2007-10-22 11:03:37 +10001752static int virtnet_close(struct net_device *dev)
1753{
1754 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001755 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001756
Rusty Russellb2baed62011-12-29 00:42:38 +00001757 /* Make sure refill_work doesn't re-enable napi! */
1758 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001759
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001760 for (i = 0; i < vi->max_queue_pairs; i++) {
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001761 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
Jason Wang986a4f42012-12-07 07:04:56 +00001762 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001763 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001764 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001765
Rusty Russell296f96f2007-10-22 11:03:37 +10001766 return 0;
1767}
1768
Alex Williamson2af76982009-02-04 09:02:40 +00001769static void virtnet_set_rx_mode(struct net_device *dev)
1770{
1771 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001772 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001773 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001774 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001775 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001776 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001777 void *buf;
1778 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001779
stephen hemminger788a8b62013-12-09 16:18:45 -08001780 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001781 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1782 return;
1783
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001784 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1785 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001786
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001787 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001788
1789 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001790 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001791 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001792 vi->ctrl->promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001793
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001794 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001795
1796 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001797 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001798 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001799 vi->ctrl->allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001800
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001801 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001802 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001803 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001804 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1805 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1806 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001807 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001808 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001809
Alex Williamson23e258e2009-05-01 17:27:56 +00001810 sg_init_table(sg, 2);
1811
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001812 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001813 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001814 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001815 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001816 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001817
1818 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001819 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001820
1821 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001822 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001823
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001824 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001825 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001826 netdev_for_each_mc_addr(ha, dev)
1827 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001828
1829 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001830 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001831
1832 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001833 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001834 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001835
1836 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001837}
1838
Patrick McHardy80d5c362013-04-19 02:04:28 +00001839static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1840 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001841{
1842 struct virtnet_info *vi = netdev_priv(dev);
1843 struct scatterlist sg;
1844
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03001845 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001846 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001847
1848 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001849 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001850 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001851 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001852}
1853
Patrick McHardy80d5c362013-04-19 02:04:28 +00001854static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1855 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001856{
1857 struct virtnet_info *vi = netdev_priv(dev);
1858 struct scatterlist sg;
1859
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03001860 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001861 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001862
1863 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001864 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001865 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001866 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001867}
1868
Wanlong Gao8898c212013-01-24 23:51:30 +00001869static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001870{
1871 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001872
1873 if (vi->affinity_hint_set) {
1874 for (i = 0; i < vi->max_queue_pairs; i++) {
Caleb Raitto19e226e2018-08-09 18:18:28 -07001875 virtqueue_set_affinity(vi->rq[i].vq, NULL);
1876 virtqueue_set_affinity(vi->sq[i].vq, NULL);
Wanlong Gao8898c212013-01-24 23:51:30 +00001877 }
1878
1879 vi->affinity_hint_set = false;
1880 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001881}
1882
1883static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001884{
Caleb Raitto2ca653d2018-08-09 17:28:40 -07001885 cpumask_var_t mask;
1886 int stragglers;
1887 int group_size;
1888 int i, j, cpu;
1889 int num_cpu;
1890 int stride;
Jason Wang986a4f42012-12-07 07:04:56 +00001891
Caleb Raitto2ca653d2018-08-09 17:28:40 -07001892 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
Wanlong Gao8898c212013-01-24 23:51:30 +00001893 virtnet_clean_affinity(vi, -1);
1894 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001895 }
1896
Caleb Raitto2ca653d2018-08-09 17:28:40 -07001897 num_cpu = num_online_cpus();
1898 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
1899 stragglers = num_cpu >= vi->curr_queue_pairs ?
1900 num_cpu % vi->curr_queue_pairs :
1901 0;
1902 cpu = cpumask_next(-1, cpu_online_mask);
Andrei Vagin4d99f662018-08-08 20:07:35 -07001903
Caleb Raitto2ca653d2018-08-09 17:28:40 -07001904 for (i = 0; i < vi->curr_queue_pairs; i++) {
1905 group_size = stride + (i < stragglers ? 1 : 0);
1906
1907 for (j = 0; j < group_size; j++) {
1908 cpumask_set_cpu(cpu, mask);
1909 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
1910 nr_cpu_ids, false);
1911 }
1912 virtqueue_set_affinity(vi->rq[i].vq, mask);
1913 virtqueue_set_affinity(vi->sq[i].vq, mask);
1914 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, false);
1915 cpumask_clear(mask);
Jason Wang986a4f42012-12-07 07:04:56 +00001916 }
1917
Wanlong Gao8898c212013-01-24 23:51:30 +00001918 vi->affinity_hint_set = true;
Caleb Raitto2ca653d2018-08-09 17:28:40 -07001919 free_cpumask_var(mask);
Jason Wang986a4f42012-12-07 07:04:56 +00001920}
1921
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001922static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001923{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001924 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1925 node);
1926 virtnet_set_affinity(vi);
1927 return 0;
1928}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001929
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001930static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1931{
1932 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1933 node_dead);
1934 virtnet_set_affinity(vi);
1935 return 0;
1936}
Jason Wang3ab098d2013-10-15 11:18:58 +08001937
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001938static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1939{
1940 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1941 node);
1942
1943 virtnet_clean_affinity(vi, cpu);
1944 return 0;
1945}
1946
1947static enum cpuhp_state virtionet_online;
1948
1949static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1950{
1951 int ret;
1952
1953 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1954 if (ret)
1955 return ret;
1956 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1957 &vi->node_dead);
1958 if (!ret)
1959 return ret;
1960 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1961 return ret;
1962}
1963
1964static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1965{
1966 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1967 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1968 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001969}
1970
Rick Jones8f9f4662011-10-19 08:10:59 +00001971static void virtnet_get_ringparam(struct net_device *dev,
1972 struct ethtool_ringparam *ring)
1973{
1974 struct virtnet_info *vi = netdev_priv(dev);
1975
Jason Wang986a4f42012-12-07 07:04:56 +00001976 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1977 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001978 ring->rx_pending = ring->rx_max_pending;
1979 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001980}
1981
Rick Jones66846042011-11-14 14:17:08 +00001982
1983static void virtnet_get_drvinfo(struct net_device *dev,
1984 struct ethtool_drvinfo *info)
1985{
1986 struct virtnet_info *vi = netdev_priv(dev);
1987 struct virtio_device *vdev = vi->vdev;
1988
1989 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1990 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1991 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1992
1993}
1994
Jason Wangd73bcd22012-12-07 07:04:57 +00001995/* TODO: Eliminate OOO packets during switching */
1996static int virtnet_set_channels(struct net_device *dev,
1997 struct ethtool_channels *channels)
1998{
1999 struct virtnet_info *vi = netdev_priv(dev);
2000 u16 queue_pairs = channels->combined_count;
2001 int err;
2002
2003 /* We don't support separate rx/tx channels.
2004 * We don't allow setting 'other' channels.
2005 */
2006 if (channels->rx_count || channels->tx_count || channels->other_count)
2007 return -EINVAL;
2008
Amos Kongc18e9cd2014-04-18 13:45:41 +08002009 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00002010 return -EINVAL;
2011
John Fastabendf600b692016-12-15 12:13:24 -08002012 /* For now we don't support modifying channels while XDP is loaded
2013 * also when XDP is loaded all RX queues have XDP programs so we only
2014 * need to check a single RX queue.
2015 */
2016 if (vi->rq[0].xdp_prog)
2017 return -EINVAL;
2018
Wanlong Gao47be2472013-01-24 23:51:29 +00002019 get_online_cpus();
John Fastabend473153292017-02-02 19:14:32 -08002020 err = _virtnet_set_queues(vi, queue_pairs);
Jason Wangd73bcd22012-12-07 07:04:57 +00002021 if (!err) {
2022 netif_set_real_num_tx_queues(dev, queue_pairs);
2023 netif_set_real_num_rx_queues(dev, queue_pairs);
2024
Wanlong Gao8898c212013-01-24 23:51:30 +00002025 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00002026 }
Wanlong Gao47be2472013-01-24 23:51:29 +00002027 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00002028
2029 return err;
2030}
2031
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002032static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2033{
2034 struct virtnet_info *vi = netdev_priv(dev);
2035 char *p = (char *)data;
2036 unsigned int i, j;
2037
2038 switch (stringset) {
2039 case ETH_SS_STATS:
2040 for (i = 0; i < vi->curr_queue_pairs; i++) {
2041 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2042 snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s",
2043 i, virtnet_rq_stats_desc[j].desc);
2044 p += ETH_GSTRING_LEN;
2045 }
2046 }
2047
2048 for (i = 0; i < vi->curr_queue_pairs; i++) {
2049 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2050 snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s",
2051 i, virtnet_sq_stats_desc[j].desc);
2052 p += ETH_GSTRING_LEN;
2053 }
2054 }
2055 break;
2056 }
2057}
2058
2059static int virtnet_get_sset_count(struct net_device *dev, int sset)
2060{
2061 struct virtnet_info *vi = netdev_priv(dev);
2062
2063 switch (sset) {
2064 case ETH_SS_STATS:
2065 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2066 VIRTNET_SQ_STATS_LEN);
2067 default:
2068 return -EOPNOTSUPP;
2069 }
2070}
2071
2072static void virtnet_get_ethtool_stats(struct net_device *dev,
2073 struct ethtool_stats *stats, u64 *data)
2074{
2075 struct virtnet_info *vi = netdev_priv(dev);
2076 unsigned int idx = 0, start, i, j;
2077 const u8 *stats_base;
2078 size_t offset;
2079
2080 for (i = 0; i < vi->curr_queue_pairs; i++) {
2081 struct receive_queue *rq = &vi->rq[i];
2082
Jason Wangd46eeea2018-07-31 17:43:39 +08002083 stats_base = (u8 *)&rq->stats;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002084 do {
2085 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2086 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2087 offset = virtnet_rq_stats_desc[j].offset;
2088 data[idx + j] = *(u64 *)(stats_base + offset);
2089 }
2090 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2091 idx += VIRTNET_RQ_STATS_LEN;
2092 }
2093
2094 for (i = 0; i < vi->curr_queue_pairs; i++) {
2095 struct send_queue *sq = &vi->sq[i];
2096
2097 stats_base = (u8 *)&sq->stats;
2098 do {
2099 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2100 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2101 offset = virtnet_sq_stats_desc[j].offset;
2102 data[idx + j] = *(u64 *)(stats_base + offset);
2103 }
2104 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2105 idx += VIRTNET_SQ_STATS_LEN;
2106 }
2107}
2108
Jason Wangd73bcd22012-12-07 07:04:57 +00002109static void virtnet_get_channels(struct net_device *dev,
2110 struct ethtool_channels *channels)
2111{
2112 struct virtnet_info *vi = netdev_priv(dev);
2113
2114 channels->combined_count = vi->curr_queue_pairs;
2115 channels->max_combined = vi->max_queue_pairs;
2116 channels->max_other = 0;
2117 channels->rx_count = 0;
2118 channels->tx_count = 0;
2119 channels->other_count = 0;
2120}
2121
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002122/* Check if the user is trying to change anything besides speed/duplex */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002123static bool
2124virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002125{
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002126 struct ethtool_link_ksettings diff1 = *cmd;
2127 struct ethtool_link_ksettings diff2 = {};
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002128
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01002129 /* cmd is always set so we need to clear it, validate the port type
2130 * and also without autonegotiation we can ignore advertising
2131 */
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002132 diff1.base.speed = 0;
2133 diff2.base.port = PORT_OTHER;
2134 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
2135 diff1.base.duplex = 0;
2136 diff1.base.cmd = 0;
2137 diff1.base.link_mode_masks_nwords = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002138
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002139 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
2140 bitmap_empty(diff1.link_modes.supported,
2141 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2142 bitmap_empty(diff1.link_modes.advertising,
2143 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2144 bitmap_empty(diff1.link_modes.lp_advertising,
2145 __ETHTOOL_LINK_MODE_MASK_NBITS);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002146}
2147
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002148static int virtnet_set_link_ksettings(struct net_device *dev,
2149 const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002150{
2151 struct virtnet_info *vi = netdev_priv(dev);
2152 u32 speed;
2153
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002154 speed = cmd->base.speed;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002155 /* don't allow custom speed and duplex */
2156 if (!ethtool_validate_speed(speed) ||
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002157 !ethtool_validate_duplex(cmd->base.duplex) ||
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002158 !virtnet_validate_ethtool_cmd(cmd))
2159 return -EINVAL;
2160 vi->speed = speed;
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002161 vi->duplex = cmd->base.duplex;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002162
2163 return 0;
2164}
2165
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002166static int virtnet_get_link_ksettings(struct net_device *dev,
2167 struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002168{
2169 struct virtnet_info *vi = netdev_priv(dev);
2170
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002171 cmd->base.speed = vi->speed;
2172 cmd->base.duplex = vi->duplex;
2173 cmd->base.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002174
2175 return 0;
2176}
2177
Jason Wang0c465be2018-10-09 10:06:26 +08002178static int virtnet_set_coalesce(struct net_device *dev,
2179 struct ethtool_coalesce *ec)
2180{
2181 struct ethtool_coalesce ec_default = {
2182 .cmd = ETHTOOL_SCOALESCE,
2183 .rx_max_coalesced_frames = 1,
2184 };
2185 struct virtnet_info *vi = netdev_priv(dev);
2186 int i, napi_weight;
2187
2188 if (ec->tx_max_coalesced_frames > 1)
2189 return -EINVAL;
2190
2191 ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
2192 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
2193
2194 /* disallow changes to fields not explicitly tested above */
2195 if (memcmp(ec, &ec_default, sizeof(ec_default)))
2196 return -EINVAL;
2197
2198 if (napi_weight ^ vi->sq[0].napi.weight) {
2199 if (dev->flags & IFF_UP)
2200 return -EBUSY;
2201 for (i = 0; i < vi->max_queue_pairs; i++)
2202 vi->sq[i].napi.weight = napi_weight;
2203 }
2204
2205 return 0;
2206}
2207
2208static int virtnet_get_coalesce(struct net_device *dev,
2209 struct ethtool_coalesce *ec)
2210{
2211 struct ethtool_coalesce ec_default = {
2212 .cmd = ETHTOOL_GCOALESCE,
2213 .rx_max_coalesced_frames = 1,
2214 };
2215 struct virtnet_info *vi = netdev_priv(dev);
2216
2217 memcpy(ec, &ec_default, sizeof(ec_default));
2218
2219 if (vi->sq[0].napi.weight)
2220 ec->tx_max_coalesced_frames = 1;
2221
2222 return 0;
2223}
2224
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002225static void virtnet_init_settings(struct net_device *dev)
2226{
2227 struct virtnet_info *vi = netdev_priv(dev);
2228
2229 vi->speed = SPEED_UNKNOWN;
2230 vi->duplex = DUPLEX_UNKNOWN;
2231}
2232
Jason Baronfaa9b392018-01-05 17:44:54 -05002233static void virtnet_update_settings(struct virtnet_info *vi)
2234{
2235 u32 speed;
2236 u8 duplex;
2237
2238 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2239 return;
2240
2241 speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config,
2242 speed));
2243 if (ethtool_validate_speed(speed))
2244 vi->speed = speed;
2245 duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config,
2246 duplex));
2247 if (ethtool_validate_duplex(duplex))
2248 vi->duplex = duplex;
2249}
2250
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002251static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00002252 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002253 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00002254 .get_ringparam = virtnet_get_ringparam,
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002255 .get_strings = virtnet_get_strings,
2256 .get_sset_count = virtnet_get_sset_count,
2257 .get_ethtool_stats = virtnet_get_ethtool_stats,
Jason Wangd73bcd22012-12-07 07:04:57 +00002258 .set_channels = virtnet_set_channels,
2259 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00002260 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002261 .get_link_ksettings = virtnet_get_link_ksettings,
2262 .set_link_ksettings = virtnet_set_link_ksettings,
Jason Wang0c465be2018-10-09 10:06:26 +08002263 .set_coalesce = virtnet_set_coalesce,
2264 .get_coalesce = virtnet_get_coalesce,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08002265};
2266
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002267static void virtnet_freeze_down(struct virtio_device *vdev)
2268{
2269 struct virtnet_info *vi = vdev->priv;
2270 int i;
2271
2272 /* Make sure no work handler is accessing the device */
2273 flush_work(&vi->config_work);
2274
Ake Koomsin05c998b2018-10-17 19:44:12 +09002275 netif_tx_lock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002276 netif_device_detach(vi->dev);
Ake Koomsin05c998b2018-10-17 19:44:12 +09002277 netif_tx_unlock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002278 cancel_delayed_work_sync(&vi->refill);
2279
2280 if (netif_running(vi->dev)) {
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002281 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002282 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04002283 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002284 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002285 }
2286}
2287
2288static int init_vqs(struct virtnet_info *vi);
2289
2290static int virtnet_restore_up(struct virtio_device *vdev)
2291{
2292 struct virtnet_info *vi = vdev->priv;
2293 int err, i;
2294
2295 err = init_vqs(vi);
2296 if (err)
2297 return err;
2298
2299 virtio_device_ready(vdev);
2300
2301 if (netif_running(vi->dev)) {
2302 for (i = 0; i < vi->curr_queue_pairs; i++)
2303 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2304 schedule_delayed_work(&vi->refill, 0);
2305
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002306 for (i = 0; i < vi->max_queue_pairs; i++) {
Willem de Bruijne4e84522017-04-24 13:49:26 -04002307 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002308 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2309 &vi->sq[i].napi);
2310 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002311 }
2312
Ake Koomsin05c998b2018-10-17 19:44:12 +09002313 netif_tx_lock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002314 netif_device_attach(vi->dev);
Ake Koomsin05c998b2018-10-17 19:44:12 +09002315 netif_tx_unlock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002316 return err;
2317}
2318
Jason Wang3f935222017-07-19 16:54:49 +08002319static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2320{
2321 struct scatterlist sg;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002322 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
Jason Wang3f935222017-07-19 16:54:49 +08002323
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002324 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
Jason Wang3f935222017-07-19 16:54:49 +08002325
2326 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2327 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2328 dev_warn(&vi->dev->dev, "Fail to set guest offload. \n");
2329 return -EINVAL;
2330 }
2331
2332 return 0;
2333}
2334
2335static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2336{
2337 u64 offloads = 0;
2338
2339 if (!vi->guest_offloads)
2340 return 0;
2341
Jason Wang3f935222017-07-19 16:54:49 +08002342 return virtnet_set_guest_offloads(vi, offloads);
2343}
2344
2345static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2346{
2347 u64 offloads = vi->guest_offloads;
2348
2349 if (!vi->guest_offloads)
2350 return 0;
Jason Wang3f935222017-07-19 16:54:49 +08002351
2352 return virtnet_set_guest_offloads(vi, offloads);
2353}
2354
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002355static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2356 struct netlink_ext_ack *extack)
John Fastabendf600b692016-12-15 12:13:24 -08002357{
2358 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2359 struct virtnet_info *vi = netdev_priv(dev);
2360 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08002361 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002362 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08002363
Jason Wang3f935222017-07-19 16:54:49 +08002364 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2365 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2366 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2367 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
Jason Wang18ba58e2018-11-22 14:36:31 +08002368 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2369 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
2370 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first");
John Fastabendf600b692016-12-15 12:13:24 -08002371 return -EOPNOTSUPP;
2372 }
2373
2374 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002375 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
John Fastabendf600b692016-12-15 12:13:24 -08002376 return -EINVAL;
2377 }
2378
2379 if (dev->mtu > max_sz) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002380 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
John Fastabendf600b692016-12-15 12:13:24 -08002381 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2382 return -EINVAL;
2383 }
2384
John Fastabend672aafd2016-12-15 12:13:49 -08002385 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2386 if (prog)
2387 xdp_qp = nr_cpu_ids;
2388
2389 /* XDP requires extra queues for XDP_TX */
2390 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002391 NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
John Fastabend672aafd2016-12-15 12:13:49 -08002392 netdev_warn(dev, "request %i queues but max is %i\n",
2393 curr_qp + xdp_qp, vi->max_queue_pairs);
2394 return -ENOMEM;
2395 }
2396
John Fastabend2de2f7f2017-02-02 19:16:29 -08002397 if (prog) {
2398 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
2399 if (IS_ERR(prog))
2400 return PTR_ERR(prog);
2401 }
2402
Jason Wang4941d472017-07-19 16:54:48 +08002403 /* Make sure NAPI is not using any XDP TX queues for RX. */
Jason Wang4e09ff52018-02-28 18:20:04 +08002404 if (netif_running(dev))
2405 for (i = 0; i < vi->max_queue_pairs; i++)
2406 napi_disable(&vi->rq[i].napi);
John Fastabendf600b692016-12-15 12:13:24 -08002407
John Fastabend672aafd2016-12-15 12:13:49 -08002408 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
Jason Wang4941d472017-07-19 16:54:48 +08002409 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2410 if (err)
2411 goto err;
2412 vi->xdp_queue_pairs = xdp_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002413
John Fastabendf600b692016-12-15 12:13:24 -08002414 for (i = 0; i < vi->max_queue_pairs; i++) {
2415 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2416 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
Jason Wang3f935222017-07-19 16:54:49 +08002417 if (i == 0) {
2418 if (!old_prog)
2419 virtnet_clear_guest_offloads(vi);
2420 if (!prog)
2421 virtnet_restore_guest_offloads(vi);
2422 }
John Fastabendf600b692016-12-15 12:13:24 -08002423 if (old_prog)
2424 bpf_prog_put(old_prog);
Jason Wang4e09ff52018-02-28 18:20:04 +08002425 if (netif_running(dev))
2426 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
John Fastabendf600b692016-12-15 12:13:24 -08002427 }
2428
2429 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08002430
Jason Wang4941d472017-07-19 16:54:48 +08002431err:
2432 for (i = 0; i < vi->max_queue_pairs; i++)
2433 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
John Fastabend2de2f7f2017-02-02 19:16:29 -08002434 if (prog)
2435 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2436 return err;
John Fastabendf600b692016-12-15 12:13:24 -08002437}
2438
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002439static u32 virtnet_xdp_query(struct net_device *dev)
John Fastabendf600b692016-12-15 12:13:24 -08002440{
2441 struct virtnet_info *vi = netdev_priv(dev);
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002442 const struct bpf_prog *xdp_prog;
John Fastabendf600b692016-12-15 12:13:24 -08002443 int i;
2444
2445 for (i = 0; i < vi->max_queue_pairs; i++) {
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002446 xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2447 if (xdp_prog)
2448 return xdp_prog->aux->id;
John Fastabendf600b692016-12-15 12:13:24 -08002449 }
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002450 return 0;
John Fastabendf600b692016-12-15 12:13:24 -08002451}
2452
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002453static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
John Fastabendf600b692016-12-15 12:13:24 -08002454{
2455 switch (xdp->command) {
2456 case XDP_SETUP_PROG:
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002457 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
John Fastabendf600b692016-12-15 12:13:24 -08002458 case XDP_QUERY_PROG:
Martin KaFai Lau5b0e6622017-06-15 17:29:12 -07002459 xdp->prog_id = virtnet_xdp_query(dev);
John Fastabendf600b692016-12-15 12:13:24 -08002460 return 0;
2461 default:
2462 return -EINVAL;
2463 }
2464}
2465
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002466static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2467 size_t len)
2468{
2469 struct virtnet_info *vi = netdev_priv(dev);
2470 int ret;
2471
2472 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2473 return -EOPNOTSUPP;
2474
2475 ret = snprintf(buf, len, "sby");
2476 if (ret >= len)
2477 return -EOPNOTSUPP;
2478
2479 return 0;
2480}
2481
Stephen Hemminger76288b42009-01-06 10:44:22 -08002482static const struct net_device_ops virtnet_netdev = {
2483 .ndo_open = virtnet_open,
2484 .ndo_stop = virtnet_close,
2485 .ndo_start_xmit = start_xmit,
2486 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08002487 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00002488 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002489 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00002490 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2491 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002492 .ndo_bpf = virtnet_xdp,
Jason Wang186b3c92017-09-19 17:42:43 +08002493 .ndo_xdp_xmit = virtnet_xdp_xmit,
Vlad Yasevich2836b4f2017-05-23 13:38:43 -04002494 .ndo_features_check = passthru_features_check,
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002495 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002496};
2497
Jason Wang586d17c2012-04-11 20:43:52 +00002498static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002499{
Jason Wang586d17c2012-04-11 20:43:52 +00002500 struct virtnet_info *vi =
2501 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002502 u16 v;
2503
Rusty Russell855e0c52013-10-14 18:11:51 +10302504 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2505 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302506 return;
Jason Wang586d17c2012-04-11 20:43:52 +00002507
2508 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00002509 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00002510 virtnet_ack_link_announce(vi);
2511 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002512
2513 /* Ignore unknown (future) status bits */
2514 v &= VIRTIO_NET_S_LINK_UP;
2515
2516 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302517 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002518
2519 vi->status = v;
2520
2521 if (vi->status & VIRTIO_NET_S_LINK_UP) {
Jason Baronfaa9b392018-01-05 17:44:54 -05002522 virtnet_update_settings(vi);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002523 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002524 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002525 } else {
2526 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002527 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002528 }
2529}
2530
2531static void virtnet_config_changed(struct virtio_device *vdev)
2532{
2533 struct virtnet_info *vi = vdev->priv;
2534
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002535 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002536}
2537
Jason Wang986a4f42012-12-07 07:04:56 +00002538static void virtnet_free_queues(struct virtnet_info *vi)
2539{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002540 int i;
2541
Jason Wangab3971b2015-03-12 13:57:44 +08002542 for (i = 0; i < vi->max_queue_pairs; i++) {
2543 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002544 netif_napi_del(&vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002545 netif_napi_del(&vi->sq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08002546 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002547
Eric Dumazet963abe52016-11-15 22:24:12 -08002548 /* We called napi_hash_del() before netif_napi_del(),
2549 * we need to respect an RCU grace period before freeing vi->rq
2550 */
2551 synchronize_net();
2552
Jason Wang986a4f42012-12-07 07:04:56 +00002553 kfree(vi->rq);
2554 kfree(vi->sq);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002555 kfree(vi->ctrl);
Jason Wang986a4f42012-12-07 07:04:56 +00002556}
2557
John Fastabend473153292017-02-02 19:14:32 -08002558static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002559{
John Fastabendf600b692016-12-15 12:13:24 -08002560 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00002561 int i;
2562
2563 for (i = 0; i < vi->max_queue_pairs; i++) {
2564 while (vi->rq[i].pages)
2565 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08002566
2567 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2568 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2569 if (old_prog)
2570 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00002571 }
John Fastabend473153292017-02-02 19:14:32 -08002572}
2573
2574static void free_receive_bufs(struct virtnet_info *vi)
2575{
2576 rtnl_lock();
2577 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08002578 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002579}
2580
Michael Daltonfb518792014-01-16 22:23:26 -08002581static void free_receive_page_frags(struct virtnet_info *vi)
2582{
2583 int i;
2584 for (i = 0; i < vi->max_queue_pairs; i++)
2585 if (vi->rq[i].alloc_frag.page)
2586 put_page(vi->rq[i].alloc_frag.page);
2587}
2588
John Fastabendb68df012017-01-25 18:22:48 -08002589static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08002590{
2591 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
2592 return false;
2593 else if (q < vi->curr_queue_pairs)
2594 return true;
2595 else
2596 return false;
2597}
2598
Jason Wang986a4f42012-12-07 07:04:56 +00002599static void free_unused_bufs(struct virtnet_info *vi)
2600{
2601 void *buf;
2602 int i;
2603
2604 for (i = 0; i < vi->max_queue_pairs; i++) {
2605 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002606 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08002607 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08002608 dev_kfree_skb(buf);
2609 else
2610 put_page(virt_to_head_page(buf));
2611 }
Jason Wang986a4f42012-12-07 07:04:56 +00002612 }
2613
2614 for (i = 0; i < vi->max_queue_pairs; i++) {
2615 struct virtqueue *vq = vi->rq[i].vq;
2616
2617 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002618 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02002619 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002620 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002621 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002622 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002623 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002624 }
Jason Wang986a4f42012-12-07 07:04:56 +00002625 }
Jason Wang986a4f42012-12-07 07:04:56 +00002626 }
2627}
2628
Jason Wange9d74172012-12-07 07:04:55 +00002629static void virtnet_del_vqs(struct virtnet_info *vi)
2630{
2631 struct virtio_device *vdev = vi->vdev;
2632
Wanlong Gao8898c212013-01-24 23:51:30 +00002633 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00002634
Jason Wange9d74172012-12-07 07:04:55 +00002635 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002636
2637 virtnet_free_queues(vi);
2638}
2639
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002640/* How large should a single buffer be so a queue full of these can fit at
2641 * least one full packet?
2642 * Logic below assumes the mergeable buffer header is used.
2643 */
2644static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2645{
2646 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2647 unsigned int rq_size = virtqueue_get_vring_size(vq);
2648 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2649 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2650 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2651
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03002652 return max(max(min_buf_len, hdr_len) - hdr_len,
2653 (unsigned int)GOOD_PACKET_LEN);
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002654}
2655
Jason Wang986a4f42012-12-07 07:04:56 +00002656static int virtnet_find_vqs(struct virtnet_info *vi)
2657{
2658 vq_callback_t **callbacks;
2659 struct virtqueue **vqs;
2660 int ret = -ENOMEM;
2661 int i, total_vqs;
2662 const char **names;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002663 bool *ctx;
Jason Wang986a4f42012-12-07 07:04:56 +00002664
2665 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2666 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2667 * possible control vq.
2668 */
2669 total_vqs = vi->max_queue_pairs * 2 +
2670 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2671
2672 /* Allocate space for find_vqs parameters */
Kees Cook6396bb22018-06-12 14:03:40 -07002673 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002674 if (!vqs)
2675 goto err_vq;
Kees Cook6da2ec52018-06-12 13:55:00 -07002676 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002677 if (!callbacks)
2678 goto err_callback;
Kees Cook6da2ec52018-06-12 13:55:00 -07002679 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002680 if (!names)
2681 goto err_names;
Jason Wang192f68c2017-07-19 16:54:47 +08002682 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Kees Cook6396bb22018-06-12 14:03:40 -07002683 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002684 if (!ctx)
2685 goto err_ctx;
2686 } else {
2687 ctx = NULL;
2688 }
Jason Wang986a4f42012-12-07 07:04:56 +00002689
2690 /* Parameters for control virtqueue, if any */
2691 if (vi->has_cvq) {
2692 callbacks[total_vqs - 1] = NULL;
2693 names[total_vqs - 1] = "control";
2694 }
2695
2696 /* Allocate/initialize parameters for send/receive virtqueues */
2697 for (i = 0; i < vi->max_queue_pairs; i++) {
2698 callbacks[rxq2vq(i)] = skb_recv_done;
2699 callbacks[txq2vq(i)] = skb_xmit_done;
2700 sprintf(vi->rq[i].name, "input.%d", i);
2701 sprintf(vi->sq[i].name, "output.%d", i);
2702 names[rxq2vq(i)] = vi->rq[i].name;
2703 names[txq2vq(i)] = vi->sq[i].name;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002704 if (ctx)
2705 ctx[rxq2vq(i)] = true;
Jason Wang986a4f42012-12-07 07:04:56 +00002706 }
2707
2708 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002709 names, ctx, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002710 if (ret)
2711 goto err_find;
2712
2713 if (vi->has_cvq) {
2714 vi->cvq = vqs[total_vqs - 1];
2715 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002716 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002717 }
2718
2719 for (i = 0; i < vi->max_queue_pairs; i++) {
2720 vi->rq[i].vq = vqs[rxq2vq(i)];
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002721 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
Jason Wang986a4f42012-12-07 07:04:56 +00002722 vi->sq[i].vq = vqs[txq2vq(i)];
2723 }
2724
Tonghao Zhang2fa3c8a2018-05-31 07:16:32 -07002725 /* run here: ret == 0. */
Jason Wang986a4f42012-12-07 07:04:56 +00002726
Jason Wang986a4f42012-12-07 07:04:56 +00002727
2728err_find:
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002729 kfree(ctx);
2730err_ctx:
Jason Wang986a4f42012-12-07 07:04:56 +00002731 kfree(names);
2732err_names:
2733 kfree(callbacks);
2734err_callback:
2735 kfree(vqs);
2736err_vq:
2737 return ret;
2738}
2739
2740static int virtnet_alloc_queues(struct virtnet_info *vi)
2741{
2742 int i;
2743
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002744 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2745 if (!vi->ctrl)
2746 goto err_ctrl;
Kees Cook6396bb22018-06-12 14:03:40 -07002747 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002748 if (!vi->sq)
2749 goto err_sq;
Kees Cook6396bb22018-06-12 14:03:40 -07002750 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002751 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002752 goto err_rq;
2753
2754 INIT_DELAYED_WORK(&vi->refill, refill_work);
2755 for (i = 0; i < vi->max_queue_pairs; i++) {
2756 vi->rq[i].pages = NULL;
2757 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2758 napi_weight);
Willem de Bruijn1d11e732017-04-27 20:37:58 -04002759 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2760 napi_tx ? napi_weight : 0);
Jason Wang986a4f42012-12-07 07:04:56 +00002761
2762 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002763 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002764 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002765
2766 u64_stats_init(&vi->rq[i].stats.syncp);
2767 u64_stats_init(&vi->sq[i].stats.syncp);
Jason Wang986a4f42012-12-07 07:04:56 +00002768 }
2769
2770 return 0;
2771
2772err_rq:
2773 kfree(vi->sq);
2774err_sq:
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002775 kfree(vi->ctrl);
2776err_ctrl:
Jason Wang986a4f42012-12-07 07:04:56 +00002777 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002778}
2779
Amit Shah3f9c10b2011-12-22 16:58:31 +05302780static int init_vqs(struct virtnet_info *vi)
2781{
Jason Wang986a4f42012-12-07 07:04:56 +00002782 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302783
Jason Wang986a4f42012-12-07 07:04:56 +00002784 /* Allocate send & receive queues */
2785 ret = virtnet_alloc_queues(vi);
2786 if (ret)
2787 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302788
Jason Wang986a4f42012-12-07 07:04:56 +00002789 ret = virtnet_find_vqs(vi);
2790 if (ret)
2791 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302792
Wanlong Gao47be2472013-01-24 23:51:29 +00002793 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002794 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002795 put_online_cpus();
2796
Amit Shah3f9c10b2011-12-22 16:58:31 +05302797 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002798
2799err_free:
2800 virtnet_free_queues(vi);
2801err:
2802 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302803}
2804
Michael Daltonfbf28d72014-01-16 22:23:30 -08002805#ifdef CONFIG_SYSFS
2806static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -07002807 char *buf)
Michael Daltonfbf28d72014-01-16 22:23:30 -08002808{
2809 struct virtnet_info *vi = netdev_priv(queue->dev);
2810 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Jason Wang3cc81a92018-03-02 17:29:14 +08002811 unsigned int headroom = virtnet_get_headroom(vi);
2812 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
Johannes Berg5377d7582015-08-19 09:48:40 +02002813 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002814
2815 BUG_ON(queue_index >= vi->max_queue_pairs);
2816 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002817 return sprintf(buf, "%u\n",
Jason Wang3cc81a92018-03-02 17:29:14 +08002818 get_mergeable_buf_len(&vi->rq[queue_index], avg,
2819 SKB_DATA_ALIGN(headroom + tailroom)));
Michael Daltonfbf28d72014-01-16 22:23:30 -08002820}
2821
2822static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2823 __ATTR_RO(mergeable_rx_buffer_size);
2824
2825static struct attribute *virtio_net_mrg_rx_attrs[] = {
2826 &mergeable_rx_buffer_size_attribute.attr,
2827 NULL
2828};
2829
2830static const struct attribute_group virtio_net_mrg_rx_group = {
2831 .name = "virtio_net",
2832 .attrs = virtio_net_mrg_rx_attrs
2833};
2834#endif
2835
Jason Wang892d6eb2014-11-20 17:03:05 +08002836static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2837 unsigned int fbit,
2838 const char *fname, const char *dname)
2839{
2840 if (!virtio_has_feature(vdev, fbit))
2841 return false;
2842
2843 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2844 fname, dname);
2845
2846 return true;
2847}
2848
2849#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2850 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2851
2852static bool virtnet_validate_features(struct virtio_device *vdev)
2853{
2854 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2855 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2856 "VIRTIO_NET_F_CTRL_VQ") ||
2857 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2858 "VIRTIO_NET_F_CTRL_VQ") ||
2859 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2860 "VIRTIO_NET_F_CTRL_VQ") ||
2861 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2862 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2863 "VIRTIO_NET_F_CTRL_VQ"))) {
2864 return false;
2865 }
2866
2867 return true;
2868}
2869
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002870#define MIN_MTU ETH_MIN_MTU
2871#define MAX_MTU ETH_MAX_MTU
2872
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002873static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10002874{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002875 if (!vdev->config->get) {
2876 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2877 __func__);
2878 return -EINVAL;
2879 }
2880
Jason Wang892d6eb2014-11-20 17:03:05 +08002881 if (!virtnet_validate_features(vdev))
2882 return -EINVAL;
2883
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002884 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2885 int mtu = virtio_cread16(vdev,
2886 offsetof(struct virtio_net_config,
2887 mtu));
2888 if (mtu < MIN_MTU)
2889 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2890 }
2891
2892 return 0;
2893}
2894
2895static int virtnet_probe(struct virtio_device *vdev)
2896{
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002897 int i, err = -ENOMEM;
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03002898 struct net_device *dev;
2899 struct virtnet_info *vi;
2900 u16 max_queue_pairs;
2901 int mtu;
2902
Jason Wang986a4f42012-12-07 07:04:56 +00002903 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302904 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2905 struct virtio_net_config,
2906 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002907
2908 /* We need at least 2 queue's */
2909 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2910 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2911 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2912 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002913
2914 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002915 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002916 if (!dev)
2917 return -ENOMEM;
2918
2919 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002920 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002921 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002922 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002923
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002924 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002925 SET_NETDEV_DEV(dev, &vdev->dev);
2926
2927 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002928 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002929 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002930 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002931 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002932 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002933
2934 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
David S. Millere078de02017-07-03 06:37:32 -07002935 dev->hw_features |= NETIF_F_TSO
Rusty Russell34a48572008-02-04 23:50:02 -05002936 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2937 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002938 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002939 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2940 dev->hw_features |= NETIF_F_TSO;
2941 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2942 dev->hw_features |= NETIF_F_TSO6;
2943 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2944 dev->hw_features |= NETIF_F_TSO_ECN;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002945
Jason Wang41f2f122014-12-24 11:03:52 +08002946 dev->features |= NETIF_F_GSO_ROBUST;
2947
Michał Mirosław98e778c2011-03-31 01:01:35 +00002948 if (gso)
David S. Millere078de02017-07-03 06:37:32 -07002949 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002950 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002951 }
Thomas Huth4f491292013-08-27 17:09:02 +02002952 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2953 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002954
Jason Wang4fda8302013-04-10 23:32:21 +00002955 dev->vlan_features = dev->features;
2956
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002957 /* MTU range: 68 - 65535 */
2958 dev->min_mtu = MIN_MTU;
2959 dev->max_mtu = MAX_MTU;
2960
Rusty Russell296f96f2007-10-22 11:03:37 +10002961 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302962 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2963 virtio_cread_bytes(vdev,
2964 offsetof(struct virtio_net_config, mac),
2965 dev->dev_addr, dev->addr_len);
2966 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002967 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002968
2969 /* Set up our device-specific information */
2970 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002971 vi->dev = dev;
2972 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002973 vdev->priv = vi;
John Stultz827da442013-10-07 15:51:58 -07002974
Jason Wang586d17c2012-04-11 20:43:52 +00002975 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002976
Herbert Xu97402b92008-04-18 11:24:27 +08002977 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002978 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2979 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002980 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2981 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002982 vi->big_packets = true;
2983
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002984 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2985 vi->mergeable_rx_bufs = true;
2986
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002987 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2988 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002989 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2990 else
2991 vi->hdr_len = sizeof(struct virtio_net_hdr);
2992
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002993 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2994 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302995 vi->any_header_sg = true;
2996
Jason Wang986a4f42012-12-07 07:04:56 +00002997 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2998 vi->has_cvq = true;
2999
Aaron Conole14de9d12016-06-03 16:57:12 -04003000 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3001 mtu = virtio_cread16(vdev,
3002 offsetof(struct virtio_net_config,
3003 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04003004 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003005 /* Should never trigger: MTU was previously validated
3006 * in virtnet_validate.
3007 */
3008 dev_err(&vdev->dev, "device MTU appears to have changed "
3009 "it is now %d < %d", mtu, dev->min_mtu);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003010 goto free;
Aaron Conole93a205e2016-10-25 16:12:12 -04003011 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02003012
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003013 dev->mtu = mtu;
3014 dev->max_mtu = mtu;
3015
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02003016 /* TODO: size buffers correctly in this case. */
3017 if (dev->mtu > ETH_DATA_LEN)
3018 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04003019 }
3020
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03003021 if (vi->any_header_sg)
3022 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08003023
Jason Wang44900012016-11-25 12:37:26 +08003024 /* Enable multiqueue by default */
3025 if (num_online_cpus() >= max_queue_pairs)
3026 vi->curr_queue_pairs = max_queue_pairs;
3027 else
3028 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00003029 vi->max_queue_pairs = max_queue_pairs;
3030
3031 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05303032 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003033 if (err)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003034 goto free;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003035
Michael Daltonfbf28d72014-01-16 22:23:30 -08003036#ifdef CONFIG_SYSFS
3037 if (vi->mergeable_rx_bufs)
3038 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3039#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08003040 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3041 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00003042
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01003043 virtnet_init_settings(dev);
3044
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003045 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3046 vi->failover = net_failover_create(vi->dev);
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00003047 if (IS_ERR(vi->failover)) {
3048 err = PTR_ERR(vi->failover);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003049 goto free_vqs;
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00003050 }
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003051 }
3052
Rusty Russell296f96f2007-10-22 11:03:37 +10003053 err = register_netdev(dev);
3054 if (err) {
3055 pr_debug("virtio_net: registering device failed\n");
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003056 goto free_failover;
Rusty Russell296f96f2007-10-22 11:03:37 +10003057 }
Rusty Russellb3369c12008-02-04 23:50:02 -05003058
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10303059 virtio_device_ready(vdev);
3060
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003061 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003062 if (err) {
3063 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08003064 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003065 }
3066
Jason Wanga2208712016-12-13 14:23:05 +08003067 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08003068
Jason Wang167c25e2010-11-10 14:45:41 +00003069 /* Assume link up if device can't report link status,
3070 otherwise get link status from config. */
Jay Vosburghbda7fab2018-03-22 14:42:41 +00003071 netif_carrier_off(dev);
Jason Wang167c25e2010-11-10 14:45:41 +00003072 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
Tejun Heo3b07e9c2012-08-20 14:51:24 -07003073 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00003074 } else {
3075 vi->status = VIRTIO_NET_S_LINK_UP;
Jason Baronfaa9b392018-01-05 17:44:54 -05003076 virtnet_update_settings(vi);
Jason Wang167c25e2010-11-10 14:45:41 +00003077 netif_carrier_on(dev);
3078 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003079
Jason Wang3f935222017-07-19 16:54:49 +08003080 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3081 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3082 set_bit(guest_offloads[i], &vi->guest_offloads);
3083
Jason Wang986a4f42012-12-07 07:04:56 +00003084 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3085 dev->name, max_queue_pairs);
3086
Rusty Russell296f96f2007-10-22 11:03:37 +10003087 return 0;
3088
wangyunjianf00e35e2016-05-31 11:52:43 +08003089free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10303090 vi->vdev->config->reset(vdev);
3091
Rusty Russellb3369c12008-02-04 23:50:02 -05003092 unregister_netdev(dev);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003093free_failover:
3094 net_failover_destroy(vi->failover);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003095free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00003096 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08003097 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00003098 virtnet_del_vqs(vi);
Rusty Russell296f96f2007-10-22 11:03:37 +10003099free:
3100 free_netdev(dev);
3101 return err;
3102}
3103
Amit Shah04486ed2011-12-22 16:58:32 +05303104static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10003105{
Amit Shah04486ed2011-12-22 16:58:32 +05303106 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00003107
3108 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00003109 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003110
Jason Wang986a4f42012-12-07 07:04:56 +00003111 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003112
Michael Daltonfb518792014-01-16 22:23:26 -08003113 free_receive_page_frags(vi);
3114
Jason Wang986a4f42012-12-07 07:04:56 +00003115 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05303116}
3117
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003118static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05303119{
3120 struct virtnet_info *vi = vdev->priv;
3121
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003122 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003123
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10303124 /* Make sure no work handler is accessing the device. */
3125 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00003126
Amit Shah04486ed2011-12-22 16:58:32 +05303127 unregister_netdev(vi->dev);
3128
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003129 net_failover_destroy(vi->failover);
3130
Amit Shah04486ed2011-12-22 16:58:32 +05303131 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003132
Rusty Russell74b25532007-11-19 11:20:42 -05003133 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003134}
3135
Arnd Bergmann67a75192017-07-25 17:35:50 +02003136static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303137{
3138 struct virtnet_info *vi = vdev->priv;
3139
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003140 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003141 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303142 remove_vq_common(vi);
3143
3144 return 0;
3145}
3146
Arnd Bergmann67a75192017-07-25 17:35:50 +02003147static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303148{
3149 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003150 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05303151
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003152 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303153 if (err)
3154 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00003155 virtnet_set_queues(vi, vi->curr_queue_pairs);
3156
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003157 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08003158 if (err)
3159 return err;
3160
Amit Shah0741bcb2011-12-22 16:58:33 +05303161 return 0;
3162}
Amit Shah0741bcb2011-12-22 16:58:33 +05303163
Rusty Russell296f96f2007-10-22 11:03:37 +10003164static struct virtio_device_id id_table[] = {
3165 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3166 { 0 },
3167};
3168
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003169#define VIRTNET_FEATURES \
3170 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3171 VIRTIO_NET_F_MAC, \
3172 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3173 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3174 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3175 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3176 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3177 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3178 VIRTIO_NET_F_CTRL_MAC_ADDR, \
Jason Baronfaa9b392018-01-05 17:44:54 -05003179 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
Sridhar Samudrala98050692018-05-24 09:55:16 -07003180 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003181
Rusty Russellc45a6812008-05-02 21:50:50 -05003182static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003183 VIRTNET_FEATURES,
3184};
3185
3186static unsigned int features_legacy[] = {
3187 VIRTNET_FEATURES,
3188 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09303189 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05003190};
3191
Uwe Kleine-König22402522009-11-05 01:32:44 -08003192static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05003193 .feature_table = features,
3194 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003195 .feature_table_legacy = features_legacy,
3196 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10003197 .driver.name = KBUILD_MODNAME,
3198 .driver.owner = THIS_MODULE,
3199 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003200 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10003201 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003202 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003203 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09303204#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05303205 .freeze = virtnet_freeze,
3206 .restore = virtnet_restore,
3207#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10003208};
3209
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003210static __init int virtio_net_driver_init(void)
3211{
3212 int ret;
3213
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003214 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003215 virtnet_cpu_online,
3216 virtnet_cpu_down_prep);
3217 if (ret < 0)
3218 goto out;
3219 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003220 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003221 NULL, virtnet_cpu_dead);
3222 if (ret)
3223 goto err_dead;
3224
3225 ret = register_virtio_driver(&virtio_net_driver);
3226 if (ret)
3227 goto err_virtio;
3228 return 0;
3229err_virtio:
3230 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3231err_dead:
3232 cpuhp_remove_multi_state(virtionet_online);
3233out:
3234 return ret;
3235}
3236module_init(virtio_net_driver_init);
3237
3238static __exit void virtio_net_driver_exit(void)
3239{
Andrew Jonescfa0ebc2017-07-24 15:38:32 +02003240 unregister_virtio_driver(&virtio_net_driver);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003241 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3242 cpuhp_remove_multi_state(virtionet_online);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003243}
3244module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10003245
3246MODULE_DEVICE_TABLE(virtio, id_table);
3247MODULE_DESCRIPTION("Virtio network driver");
3248MODULE_LICENSE("GPL");