blob: a801ea40908ffc9fa78df5f3bd6cb9e6821332f0 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rusty Russell48925e32009-09-24 09:59:20 -06002/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10003 *
4 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
Rusty Russell296f96f2007-10-22 11:03:37 +10005 */
6//#define DEBUG
7#include <linux/netdevice.h>
8#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +08009#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100010#include <linux/module.h>
11#include <linux/virtio.h>
12#include <linux/virtio_net.h>
John Fastabendf600b692016-12-15 12:13:24 -080013#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010014#include <linux/bpf_trace.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100015#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080016#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000018#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080019#include <linux/average.h>
Jason Wang186b3c92017-09-19 17:42:43 +080020#include <linux/filter.h>
Caleb Raitto2ca653d2018-08-09 17:28:40 -070021#include <linux/kernel.h>
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +020022#include <net/route.h>
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +010023#include <net/xdp.h>
Sridhar Samudralaba5e4422018-05-24 09:55:17 -070024#include <net/net_failover.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100025
Amerigo Wangd34710e2013-05-09 19:50:51 +000026static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020027module_param(napi_weight, int, 0444);
28
Willem de Bruijn31c03ae2019-06-13 12:24:57 -040029static bool csum = true, gso = true, napi_tx = true;
Rusty Russell34a48572008-02-04 23:50:02 -050030module_param(csum, bool, 0444);
31module_param(gso, bool, 0444);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -040032module_param(napi_tx, bool, 0644);
Rusty Russell34a48572008-02-04 23:50:02 -050033
Rusty Russell296f96f2007-10-22 11:03:37 +100034/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080035#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080036#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100037
Jason Wangf6b10202017-02-21 16:46:28 +080038#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
39
John Fastabend2de2f7f2017-02-02 19:16:29 -080040/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
41#define VIRTIO_XDP_HEADROOM 256
42
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +020043/* Separating two types of XDP xmit */
44#define VIRTIO_XDP_TX BIT(0)
45#define VIRTIO_XDP_REDIR BIT(1)
46
Toshiaki Makita50504712019-01-29 09:45:59 +090047#define VIRTIO_XDP_FLAG BIT(0)
48
Johannes Berg5377d7582015-08-19 09:48:40 +020049/* RX packet size EWMA. The average packet size is used to determine the packet
50 * buffer size when refilling RX rings. As the entire RX ring may be refilled
51 * at once, the weight is chosen so that the EWMA will be insensitive to short-
52 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080053 */
Johannes Bergeb1e0112017-02-15 09:49:26 +010054DECLARE_EWMA(pkt_len, 0, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080055
Rick Jones66846042011-11-14 14:17:08 +000056#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000057
Colin Ian King7acd4322017-08-12 22:45:53 +010058static const unsigned long guest_offloads[] = {
59 VIRTIO_NET_F_GUEST_TSO4,
60 VIRTIO_NET_F_GUEST_TSO6,
61 VIRTIO_NET_F_GUEST_ECN,
Jason Wange59ff2c2018-11-22 14:36:30 +080062 VIRTIO_NET_F_GUEST_UFO,
63 VIRTIO_NET_F_GUEST_CSUM
Colin Ian King7acd4322017-08-12 22:45:53 +010064};
Jason Wang3f935222017-07-19 16:54:49 +080065
Jason Wangdbcf24d2021-08-17 16:06:59 +080066#define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
Tonghao Zhang1a03b8a2020-09-29 09:58:06 +080067 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
68 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
69 (1ULL << VIRTIO_NET_F_GUEST_UFO))
70
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090071struct virtnet_stat_desc {
72 char desc[ETH_GSTRING_LEN];
73 size_t offset;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000074};
75
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090076struct virtnet_sq_stats {
77 struct u64_stats_sync syncp;
78 u64 packets;
79 u64 bytes;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090080 u64 xdp_tx;
81 u64 xdp_tx_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +090082 u64 kicks;
Tony Lua5207942021-09-17 16:40:06 +080083 u64 tx_timeouts;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090084};
85
Jason Wangd46eeea2018-07-31 17:43:39 +080086struct virtnet_rq_stats {
87 struct u64_stats_sync syncp;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090088 u64 packets;
89 u64 bytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +090090 u64 drops;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +090091 u64 xdp_packets;
92 u64 xdp_tx;
93 u64 xdp_redirects;
94 u64 xdp_drops;
Toshiaki Makita461f03d2018-07-23 23:36:09 +090095 u64 kicks;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +090096};
97
98#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
Jason Wangd46eeea2018-07-31 17:43:39 +080099#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900100
101static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900102 { "packets", VIRTNET_SQ_STAT(packets) },
103 { "bytes", VIRTNET_SQ_STAT(bytes) },
104 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
105 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900106 { "kicks", VIRTNET_SQ_STAT(kicks) },
Tony Lua5207942021-09-17 16:40:06 +0800107 { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900108};
109
110static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900111 { "packets", VIRTNET_RQ_STAT(packets) },
112 { "bytes", VIRTNET_RQ_STAT(bytes) },
113 { "drops", VIRTNET_RQ_STAT(drops) },
114 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
115 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
116 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
117 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900118 { "kicks", VIRTNET_RQ_STAT(kicks) },
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900119};
120
121#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
122#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
123
Jason Wange9d74172012-12-07 07:04:55 +0000124/* Internal representation of a send virtqueue */
125struct send_queue {
126 /* Virtqueue associated with this send _queue */
127 struct virtqueue *vq;
128
129 /* TX: fragments + linear part + virtio header */
130 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000131
132 /* Name of the send queue: output.$index */
133 char name[40];
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400134
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900135 struct virtnet_sq_stats stats;
136
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400137 struct napi_struct napi;
Jason Wange9d74172012-12-07 07:04:55 +0000138};
139
140/* Internal representation of a receive virtqueue */
141struct receive_queue {
142 /* Virtqueue associated with this receive_queue */
143 struct virtqueue *vq;
144
Rusty Russell296f96f2007-10-22 11:03:37 +1000145 struct napi_struct napi;
146
John Fastabendf600b692016-12-15 12:13:24 -0800147 struct bpf_prog __rcu *xdp_prog;
148
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +0900149 struct virtnet_rq_stats stats;
150
Jason Wange9d74172012-12-07 07:04:55 +0000151 /* Chain pages by the private ptr. */
152 struct page *pages;
153
Michael Daltonab7db912014-01-16 22:23:27 -0800154 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +0200155 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800156
Michael Daltonfb518792014-01-16 22:23:26 -0800157 /* Page frag for packet buffer allocation. */
158 struct page_frag alloc_frag;
159
Jason Wange9d74172012-12-07 07:04:55 +0000160 /* RX: fragments + linear part + virtio header */
161 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000162
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +0200163 /* Min single buffer size for mergeable buffers case. */
164 unsigned int min_buf_len;
165
Jason Wang986a4f42012-12-07 07:04:56 +0000166 /* Name of this receive queue: input.$index */
167 char name[40];
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100168
169 struct xdp_rxq_info xdp_rxq;
Jason Wange9d74172012-12-07 07:04:55 +0000170};
171
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300172/* Control VQ buffers: protected by the rtnl lock */
173struct control_buf {
174 struct virtio_net_ctrl_hdr hdr;
175 virtio_net_ctrl_ack status;
176 struct virtio_net_ctrl_mq mq;
177 u8 promisc;
178 u8 allmulti;
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +0300179 __virtio16 vid;
Michael S. Tsirkinf4ee7032018-04-19 08:30:50 +0300180 __virtio64 offloads;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300181};
182
Jason Wange9d74172012-12-07 07:04:55 +0000183struct virtnet_info {
184 struct virtio_device *vdev;
185 struct virtqueue *cvq;
186 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000187 struct send_queue *sq;
188 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000189 unsigned int status;
190
Jason Wang986a4f42012-12-07 07:04:56 +0000191 /* Max # of queue pairs supported by the device */
192 u16 max_queue_pairs;
193
194 /* # of queue pairs currently used by the driver */
195 u16 curr_queue_pairs;
196
John Fastabend672aafd2016-12-15 12:13:49 -0800197 /* # of XDP queue pairs currently used by the driver */
198 u16 xdp_queue_pairs;
199
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800200 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
201 bool xdp_enabled;
202
Herbert Xu97402b92008-04-18 11:24:27 +0800203 /* I like... big packets and I cannot lie! */
204 bool big_packets;
205
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800206 /* Host will merge rx buffers for big packets (shake it! shake it!) */
207 bool mergeable_rx_bufs;
208
Jason Wang986a4f42012-12-07 07:04:56 +0000209 /* Has control virtqueue */
210 bool has_cvq;
211
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930212 /* Host can handle any s/g split between our header and packet data */
213 bool any_header_sg;
214
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300215 /* Packet virtio header size */
216 u8 hdr_len;
217
Rusty Russell3161e452009-08-26 12:22:32 -0700218 /* Work struct for refilling if we run low on memory. */
219 struct delayed_work refill;
220
Jason Wang586d17c2012-04-11 20:43:52 +0000221 /* Work struct for config space updates */
222 struct work_struct config_work;
223
Jason Wang986a4f42012-12-07 07:04:56 +0000224 /* Does the affinity hint is set for virtqueues? */
225 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000226
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200227 /* CPU hotplug instances for online & dead */
228 struct hlist_node node;
229 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200230
Michael S. Tsirkin12e57162018-04-19 08:30:48 +0300231 struct control_buf *ctrl;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100232
233 /* Ethtool settings */
234 u8 duplex;
235 u32 speed;
Jason Wang3f935222017-07-19 16:54:49 +0800236
237 unsigned long guest_offloads;
Willem de Bruijna02e8962018-12-20 17:14:54 -0500238 unsigned long guest_offloads_capable;
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
Toshiaki Makita50504712019-01-29 09:45:59 +0900254static bool is_xdp_frame(void *ptr)
255{
256 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
257}
258
259static void *xdp_to_ptr(struct xdp_frame *ptr)
260{
261 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
262}
263
264static struct xdp_frame *ptr_to_xdp(void *ptr)
265{
266 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
267}
268
Jason Wang986a4f42012-12-07 07:04:56 +0000269/* Converting between virtqueue no. and kernel tx/rx queue no.
270 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
271 */
272static int vq2txq(struct virtqueue *vq)
273{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000274 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000275}
276
277static int txq2vq(int txq)
278{
279 return txq * 2 + 1;
280}
281
282static int vq2rxq(struct virtqueue *vq)
283{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000284 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000285}
286
287static int rxq2vq(int rxq)
288{
289 return rxq * 2;
290}
291
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300292static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000293{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300294 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000295}
296
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000297/*
298 * private is used to chain pages for big packets, put the whole
299 * most recent used list in the beginning for reuse
300 */
Jason Wange9d74172012-12-07 07:04:55 +0000301static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500302{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000303 struct page *end;
304
Jason Wange9d74172012-12-07 07:04:55 +0000305 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000306 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000307 end->private = (unsigned long)rq->pages;
308 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500309}
310
Jason Wange9d74172012-12-07 07:04:55 +0000311static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500312{
Jason Wange9d74172012-12-07 07:04:55 +0000313 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500314
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000315 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000316 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000317 /* clear private here, it is used to chain pages */
318 p->private = 0;
319 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500320 p = alloc_page(gfp_mask);
321 return p;
322}
323
Willem de Bruijne4e84522017-04-24 13:49:26 -0400324static void virtqueue_napi_schedule(struct napi_struct *napi,
325 struct virtqueue *vq)
326{
327 if (napi_schedule_prep(napi)) {
328 virtqueue_disable_cb(vq);
329 __napi_schedule(napi);
330 }
331}
332
333static void virtqueue_napi_complete(struct napi_struct *napi,
334 struct virtqueue *vq, int processed)
335{
336 int opaque;
337
338 opaque = virtqueue_enable_cb_prepare(vq);
Toshiaki Makitafdaa7672017-12-07 13:15:15 +0900339 if (napi_complete_done(napi, processed)) {
340 if (unlikely(virtqueue_poll(vq, opaque)))
341 virtqueue_napi_schedule(napi, vq);
342 } else {
343 virtqueue_disable_cb(vq);
344 }
Willem de Bruijne4e84522017-04-24 13:49:26 -0400345}
346
Jason Wange9d74172012-12-07 07:04:55 +0000347static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000348{
Jason Wange9d74172012-12-07 07:04:55 +0000349 struct virtnet_info *vi = vq->vdev->priv;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400350 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000351
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500352 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000353 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000354
Willem de Bruijnb92f1e62017-04-24 13:49:27 -0400355 if (napi->weight)
356 virtqueue_napi_schedule(napi, vq);
357 else
358 /* We were probably waiting for more output buffers. */
359 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000360}
361
Jason Wang28b39bc2017-07-19 16:54:46 +0800362#define MRG_CTX_HEADER_SHIFT 22
363static void *mergeable_len_to_ctx(unsigned int truesize,
364 unsigned int headroom)
365{
366 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
367}
368
369static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
370{
371 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
372}
373
374static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
375{
376 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
377}
378
Mike Waychison34646452012-01-04 12:52:32 +0000379/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300380static struct sk_buff *page_to_skb(struct virtnet_info *vi,
381 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700382 struct page *page, unsigned int offset,
Jason Wang436c9452018-11-29 13:53:16 +0800383 unsigned int len, unsigned int truesize,
Xuan Zhuofb328562021-04-16 17:16:15 +0800384 bool hdr_valid, unsigned int metasize,
Jakub Kicinskic32325b2021-08-02 10:57:29 -0700385 unsigned int headroom)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000386{
387 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300388 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700389 unsigned int copy, hdr_len, hdr_padded_len;
Eric Dumazetaf39c8f2021-04-20 02:43:41 -0700390 struct page *page_to_free = NULL;
Xuan Zhuofb328562021-04-16 17:16:15 +0800391 int tailroom, shinfo_size;
Xuan Zhuof80bd742021-04-22 23:16:20 +0800392 char *p, *hdr_p, *buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000393
Michael Dalton2613af02013-10-28 15:44:18 -0700394 p = page_address(page) + offset;
Xuan Zhuofb328562021-04-16 17:16:15 +0800395 hdr_p = p;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000396
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300397 hdr_len = vi->hdr_len;
398 if (vi->mergeable_rx_bufs)
stephen hemmingera4a76502017-08-15 10:29:17 -0700399 hdr_padded_len = sizeof(*hdr);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300400 else
Michael Dalton2613af02013-10-28 15:44:18 -0700401 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000402
Jakub Kicinskic32325b2021-08-02 10:57:29 -0700403 /* If headroom is not 0, there is an offset between the beginning of the
Xuan Zhuofb328562021-04-16 17:16:15 +0800404 * data and the allocated space, otherwise the data and the allocated
405 * space are aligned.
Xuan Zhuo8fb7da92021-06-01 14:40:00 +0800406 *
407 * Buffers with headroom use PAGE_SIZE as alloc size, see
408 * add_recvbuf_mergeable() + get_mergeable_buf_len()
Xuan Zhuofb328562021-04-16 17:16:15 +0800409 */
Jakub Kicinskic32325b2021-08-02 10:57:29 -0700410 truesize = headroom ? PAGE_SIZE : truesize;
Michael S. Tsirkinfc02e8c2021-10-09 10:52:50 -0400411 tailroom = truesize - headroom;
Jakub Kicinskic32325b2021-08-02 10:57:29 -0700412 buf = p - headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000413
414 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700415 offset += hdr_padded_len;
416 p += hdr_padded_len;
Michael S. Tsirkinfc02e8c2021-10-09 10:52:50 -0400417 tailroom -= hdr_padded_len + len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000418
Xuan Zhuofb328562021-04-16 17:16:15 +0800419 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
420
Xuan Zhuof80bd742021-04-22 23:16:20 +0800421 /* copy small packet so we can reuse these pages */
Eric Dumazetf5d78722021-04-20 13:01:44 -0700422 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
Xuan Zhuof80bd742021-04-22 23:16:20 +0800423 skb = build_skb(buf, truesize);
Xuan Zhuofb328562021-04-16 17:16:15 +0800424 if (unlikely(!skb))
425 return NULL;
426
Xuan Zhuof80bd742021-04-22 23:16:20 +0800427 skb_reserve(skb, p - buf);
Xuan Zhuofb328562021-04-16 17:16:15 +0800428 skb_put(skb, len);
Jason Wangafd92d82021-09-17 16:34:06 +0800429
430 page = (struct page *)page->private;
431 if (page)
432 give_pages(rq, page);
Xuan Zhuofb328562021-04-16 17:16:15 +0800433 goto ok;
434 }
435
436 /* copy small packet so we can reuse these pages for small data */
437 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
438 if (unlikely(!skb))
439 return NULL;
440
Eric Dumazet0f6925b2021-04-02 06:26:02 -0700441 /* Copy all frame if it fits skb->head, otherwise
442 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
443 */
444 if (len <= skb_tailroom(skb))
445 copy = len;
446 else
447 copy = ETH_HLEN + metasize;
Johannes Berg59ae1d12017-06-16 14:29:20 +0200448 skb_put_data(skb, p, copy);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000449
450 len -= copy;
451 offset += copy;
452
Michael Dalton2613af02013-10-28 15:44:18 -0700453 if (vi->mergeable_rx_bufs) {
454 if (len)
455 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
456 else
Eric Dumazetaf39c8f2021-04-20 02:43:41 -0700457 page_to_free = page;
Xuan Zhuofb328562021-04-16 17:16:15 +0800458 goto ok;
Michael Dalton2613af02013-10-28 15:44:18 -0700459 }
460
Sasha Levine878d782011-09-28 04:40:54 +0000461 /*
462 * Verify that we can indeed put this data into a skb.
463 * This is here to handle cases when the device erroneously
464 * tries to receive more than is possible. This is usually
465 * the case of a broken device.
466 */
467 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000468 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000469 dev_kfree_skb(skb);
470 return NULL;
471 }
Michael Dalton2613af02013-10-28 15:44:18 -0700472 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000473 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700474 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
475 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
476 frag_size, truesize);
477 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000478 page = (struct page *)page->private;
479 offset = 0;
480 }
481
482 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000483 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000484
Xuan Zhuofb328562021-04-16 17:16:15 +0800485ok:
486 /* hdr_valid means no XDP, so we can copy the vnet header */
487 if (hdr_valid) {
488 hdr = skb_vnet_hdr(skb);
489 memcpy(hdr, hdr_p, hdr_len);
490 }
Eric Dumazetaf39c8f2021-04-20 02:43:41 -0700491 if (page_to_free)
492 put_page(page_to_free);
Xuan Zhuofb328562021-04-16 17:16:15 +0800493
494 if (metasize) {
495 __skb_pull(skb, metasize);
496 skb_metadata_set(skb, metasize);
497 }
498
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000499 return skb;
500}
501
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200502static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
503 struct send_queue *sq,
504 struct xdp_frame *xdpf)
John Fastabend56434a02016-12-15 12:14:13 -0800505{
John Fastabend56434a02016-12-15 12:14:13 -0800506 struct virtio_net_hdr_mrg_rxbuf *hdr;
John Fastabend56434a02016-12-15 12:14:13 -0800507 int err;
508
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200509 if (unlikely(xdpf->headroom < vi->hdr_len))
510 return -EOVERFLOW;
511
512 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
513 xdpf->data -= vi->hdr_len;
Jason Wangf6b10202017-02-21 16:46:28 +0800514 /* Zero header and leave csum up to XDP layers */
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200515 hdr = xdpf->data;
Jason Wangf6b10202017-02-21 16:46:28 +0800516 memset(hdr, 0, vi->hdr_len);
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200517 xdpf->len += vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800518
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200519 sg_init_one(sq->sg, xdpf->data, xdpf->len);
Jason Wangbb91acc2016-12-23 22:37:32 +0800520
Toshiaki Makita50504712019-01-29 09:45:59 +0900521 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
522 GFP_ATOMIC);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100523 if (unlikely(err))
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200524 return -ENOSPC; /* Caller handle free/refcnt */
John Fastabend56434a02016-12-15 12:14:13 -0800525
Jesper Dangaard Brouercac320c2018-04-17 16:45:52 +0200526 return 0;
John Fastabend56434a02016-12-15 12:14:13 -0800527}
528
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800529/* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
530 * the current cpu, so it does not need to be locked.
531 *
532 * Here we use marco instead of inline functions because we have to deal with
533 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
534 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
535 * functions to perfectly solve these three problems at the same time.
536 */
537#define virtnet_xdp_get_sq(vi) ({ \
Li RongQing3dcc1ed2021-08-26 16:21:35 +0800538 int cpu = smp_processor_id(); \
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800539 struct netdev_queue *txq; \
540 typeof(vi) v = (vi); \
541 unsigned int qp; \
542 \
543 if (v->curr_queue_pairs > nr_cpu_ids) { \
544 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
Li RongQing3dcc1ed2021-08-26 16:21:35 +0800545 qp += cpu; \
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800546 txq = netdev_get_tx_queue(v->dev, qp); \
547 __netif_tx_acquire(txq); \
548 } else { \
Li RongQing3dcc1ed2021-08-26 16:21:35 +0800549 qp = cpu % v->curr_queue_pairs; \
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800550 txq = netdev_get_tx_queue(v->dev, qp); \
Li RongQing3dcc1ed2021-08-26 16:21:35 +0800551 __netif_tx_lock(txq, cpu); \
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800552 } \
553 v->sq + qp; \
554})
Toshiaki Makita2a435652018-07-23 23:36:07 +0900555
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800556#define virtnet_xdp_put_sq(vi, q) { \
557 struct netdev_queue *txq; \
558 typeof(vi) v = (vi); \
559 \
560 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
561 if (v->curr_queue_pairs > nr_cpu_ids) \
562 __netif_tx_release(txq); \
563 else \
564 __netif_tx_unlock(txq); \
Toshiaki Makita2a435652018-07-23 23:36:07 +0900565}
566
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200567static int virtnet_xdp_xmit(struct net_device *dev,
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +0200568 int n, struct xdp_frame **frames, u32 flags)
Jason Wang186b3c92017-09-19 17:42:43 +0800569{
570 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100571 struct receive_queue *rq = vi->rq;
572 struct bpf_prog *xdp_prog;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200573 struct send_queue *sq;
574 unsigned int len;
Toshiaki Makita546f28972019-01-31 20:40:30 +0900575 int packets = 0;
576 int bytes = 0;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100577 int nxmit = 0;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900578 int kicks = 0;
Toshiaki Makita50504712019-01-29 09:45:59 +0900579 void *ptr;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100580 int ret;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200581 int i;
582
Jesper Dangaard Brouer8dcc5b02018-02-20 14:32:20 +0100583 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
584 * indicate XDP resources have been successfully allocated.
585 */
John Fastabend9719c6b2020-01-26 16:14:01 -0800586 xdp_prog = rcu_access_pointer(rq->xdp_prog);
Toshiaki Makita1667c082019-01-29 09:45:56 +0900587 if (!xdp_prog)
588 return -ENXIO;
589
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800590 sq = virtnet_xdp_get_sq(vi);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000591
592 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
Jason Wang186b3c92017-09-19 17:42:43 +0800593 ret = -EINVAL;
Jason Wang186b3c92017-09-19 17:42:43 +0800594 goto out;
595 }
596
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200597 /* Free up any pending old buffers before queueing new ones. */
Toshiaki Makita50504712019-01-29 09:45:59 +0900598 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Toshiaki Makita546f28972019-01-31 20:40:30 +0900599 if (likely(is_xdp_frame(ptr))) {
600 struct xdp_frame *frame = ptr_to_xdp(ptr);
601
602 bytes += frame->len;
603 xdp_return_frame(frame);
604 } else {
605 struct sk_buff *skb = ptr;
606
607 bytes += skb->len;
608 napi_consume_skb(skb, false);
609 }
610 packets++;
Toshiaki Makita50504712019-01-29 09:45:59 +0900611 }
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200612
613 for (i = 0; i < n; i++) {
614 struct xdp_frame *xdpf = frames[i];
615
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100616 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
617 break;
618 nxmit++;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200619 }
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100620 ret = nxmit;
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200621
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900622 if (flags & XDP_XMIT_FLUSH) {
623 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
624 kicks = 1;
625 }
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900626out:
627 u64_stats_update_begin(&sq->stats.syncp);
Toshiaki Makita546f28972019-01-31 20:40:30 +0900628 sq->stats.bytes += bytes;
629 sq->stats.packets += packets;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900630 sq->stats.xdp_tx += n;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100631 sq->stats.xdp_tx_drops += n - nxmit;
Toshiaki Makita461f03d2018-07-23 23:36:09 +0900632 sq->stats.kicks += kicks;
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900633 u64_stats_update_end(&sq->stats.syncp);
Jesper Dangaard Brouer5d274cb2018-05-31 11:00:08 +0200634
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800635 virtnet_xdp_put_sq(vi, sq);
Toshiaki Makita5b8f3c82018-07-23 23:36:08 +0900636 return ret;
Jason Wang186b3c92017-09-19 17:42:43 +0800637}
638
Jason Wangf6b10202017-02-21 16:46:28 +0800639static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
640{
Xuan Zhuo97c2c692021-03-10 10:24:45 +0800641 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
Jason Wangf6b10202017-02-21 16:46:28 +0800642}
643
Jason Wang4941d472017-07-19 16:54:48 +0800644/* We copy the packet for XDP in the following cases:
645 *
646 * 1) Packet is scattered across multiple rx buffers.
647 * 2) Headroom space is insufficient.
648 *
649 * This is inefficient but it's a temporary condition that
650 * we hit right after XDP is enabled and until queue is refilled
651 * with large buffers with sufficient headroom - so it should affect
652 * at most queue size packets.
653 * Afterwards, the conditions to enable
654 * XDP should preclude the underlying device from sending packets
655 * across multiple buffers (num_buf > 1), and we make sure buffers
656 * have enough headroom.
John Fastabend72979a62016-12-15 12:14:36 -0800657 */
658static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800659 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800660 struct page *p,
661 int offset,
Jason Wang4941d472017-07-19 16:54:48 +0800662 int page_off,
John Fastabend72979a62016-12-15 12:14:36 -0800663 unsigned int *len)
664{
665 struct page *page = alloc_page(GFP_ATOMIC);
John Fastabend72979a62016-12-15 12:14:36 -0800666
667 if (!page)
668 return NULL;
669
670 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
671 page_off += *len;
672
Jason Wang56a86f82016-12-23 22:37:26 +0800673 while (--*num_buf) {
Jason Wang3cc81a92018-03-02 17:29:14 +0800674 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
John Fastabend72979a62016-12-15 12:14:36 -0800675 unsigned int buflen;
John Fastabend72979a62016-12-15 12:14:36 -0800676 void *buf;
677 int off;
678
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200679 buf = virtqueue_get_buf(rq->vq, &buflen);
680 if (unlikely(!buf))
John Fastabend72979a62016-12-15 12:14:36 -0800681 goto err_buf;
682
John Fastabend72979a62016-12-15 12:14:36 -0800683 p = virt_to_head_page(buf);
684 off = buf - page_address(p);
685
Jason Wang56a86f82016-12-23 22:37:26 +0800686 /* guard against a misconfigured or uncooperative backend that
687 * is sending packet larger than the MTU.
688 */
Jason Wang3cc81a92018-03-02 17:29:14 +0800689 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
Jason Wang56a86f82016-12-23 22:37:26 +0800690 put_page(p);
691 goto err_buf;
692 }
693
John Fastabend72979a62016-12-15 12:14:36 -0800694 memcpy(page_address(page) + page_off,
695 page_address(p) + off, buflen);
696 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800697 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800698 }
699
John Fastabend2de2f7f2017-02-02 19:16:29 -0800700 /* Headroom does not contribute to packet length */
701 *len = page_off - VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800702 return page;
703err_buf:
704 __free_pages(page, 0);
705 return NULL;
706}
707
Jason Wang4941d472017-07-19 16:54:48 +0800708static struct sk_buff *receive_small(struct net_device *dev,
709 struct virtnet_info *vi,
710 struct receive_queue *rq,
711 void *buf, void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800712 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900713 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800714 struct virtnet_rq_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800715{
716 struct sk_buff *skb;
717 struct bpf_prog *xdp_prog;
718 unsigned int xdp_headroom = (unsigned long)ctx;
719 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
720 unsigned int headroom = vi->hdr_len + header_offset;
721 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
722 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
723 struct page *page = virt_to_head_page(buf);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100724 unsigned int delta = 0;
Jason Wang4941d472017-07-19 16:54:48 +0800725 struct page *xdp_page;
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100726 int err;
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900727 unsigned int metasize = 0;
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100728
Jason Wang4941d472017-07-19 16:54:48 +0800729 len -= vi->hdr_len;
Jason Wangd46eeea2018-07-31 17:43:39 +0800730 stats->bytes += len;
Jason Wang4941d472017-07-19 16:54:48 +0800731
Xie Yongjiad993a92021-05-31 21:58:52 +0800732 if (unlikely(len > GOOD_PACKET_LEN)) {
733 pr_debug("%s: rx error: len %u exceeds max size %d\n",
734 dev->name, len, GOOD_PACKET_LEN);
735 dev->stats.rx_length_errors++;
Wenliang Wang053c9e12021-12-16 11:11:35 +0800736 goto err;
Xie Yongjiad993a92021-05-31 21:58:52 +0800737 }
Li RongQing6213f072021-10-09 17:32:43 +0800738
739 if (likely(!vi->xdp_enabled)) {
740 xdp_prog = NULL;
741 goto skip_xdp;
742 }
743
Jason Wang4941d472017-07-19 16:54:48 +0800744 rcu_read_lock();
745 xdp_prog = rcu_dereference(rq->xdp_prog);
746 if (xdp_prog) {
747 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200748 struct xdp_frame *xdpf;
Jason Wang4941d472017-07-19 16:54:48 +0800749 struct xdp_buff xdp;
750 void *orig_data;
751 u32 act;
752
Jesper Dangaard Brouer95dbe9e2018-02-20 14:32:10 +0100753 if (unlikely(hdr->hdr.gso_type))
Jason Wang4941d472017-07-19 16:54:48 +0800754 goto err_xdp;
755
756 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
757 int offset = buf - page_address(page) + header_offset;
758 unsigned int tlen = len + vi->hdr_len;
759 u16 num_buf = 1;
760
761 xdp_headroom = virtnet_get_headroom(vi);
762 header_offset = VIRTNET_RX_PAD + xdp_headroom;
763 headroom = vi->hdr_len + header_offset;
764 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
765 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
766 xdp_page = xdp_linearize_page(rq, &num_buf, page,
767 offset, header_offset,
768 &tlen);
769 if (!xdp_page)
770 goto err_xdp;
771
772 buf = page_address(xdp_page);
773 put_page(page);
774 page = xdp_page;
775 }
776
Lorenzo Bianconi43b51692020-12-22 22:09:28 +0100777 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100778 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
779 xdp_headroom, len, true);
Jason Wang4941d472017-07-19 16:54:48 +0800780 orig_data = xdp.data;
781 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Jason Wangd46eeea2018-07-31 17:43:39 +0800782 stats->xdp_packets++;
Jason Wang4941d472017-07-19 16:54:48 +0800783
784 switch (act) {
785 case XDP_PASS:
786 /* Recalculate length in case bpf program changed it */
787 delta = orig_data - xdp.data;
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700788 len = xdp.data_end - xdp.data;
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900789 metasize = xdp.data - xdp.data_meta;
Jason Wang4941d472017-07-19 16:54:48 +0800790 break;
791 case XDP_TX:
Jason Wangd46eeea2018-07-31 17:43:39 +0800792 stats->xdp_tx++;
Lorenzo Bianconi1b698fa2020-05-28 22:47:29 +0200793 xdpf = xdp_convert_buff_to_frame(&xdp);
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200794 if (unlikely(!xdpf))
795 goto err_xdp;
Jason Wangca9e83b2018-07-31 17:43:38 +0800796 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100797 if (unlikely(!err)) {
798 xdp_return_frame_rx_napi(xdpf);
799 } else if (unlikely(err < 0)) {
Jason Wang4941d472017-07-19 16:54:48 +0800800 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100801 goto err_xdp;
802 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200803 *xdp_xmit |= VIRTIO_XDP_TX;
Jason Wang186b3c92017-09-19 17:42:43 +0800804 rcu_read_unlock();
805 goto xdp_xmit;
806 case XDP_REDIRECT:
Jason Wangd46eeea2018-07-31 17:43:39 +0800807 stats->xdp_redirects++;
Jason Wang186b3c92017-09-19 17:42:43 +0800808 err = xdp_do_redirect(dev, &xdp, xdp_prog);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +0100809 if (err)
810 goto err_xdp;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +0200811 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang4941d472017-07-19 16:54:48 +0800812 rcu_read_unlock();
813 goto xdp_xmit;
814 default:
Paolo Abenic8064e52021-11-30 11:08:07 +0100815 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500816 fallthrough;
Jason Wang4941d472017-07-19 16:54:48 +0800817 case XDP_ABORTED:
818 trace_xdp_exception(vi->dev, xdp_prog, act);
Gustavo A. R. Silva95efabf2020-11-20 12:40:44 -0600819 goto err_xdp;
Jason Wang4941d472017-07-19 16:54:48 +0800820 case XDP_DROP:
821 goto err_xdp;
822 }
823 }
824 rcu_read_unlock();
825
Li RongQing6213f072021-10-09 17:32:43 +0800826skip_xdp:
Jason Wang4941d472017-07-19 16:54:48 +0800827 skb = build_skb(buf, buflen);
Wenliang Wang053c9e12021-12-16 11:11:35 +0800828 if (!skb)
Jason Wang4941d472017-07-19 16:54:48 +0800829 goto err;
Jason Wang4941d472017-07-19 16:54:48 +0800830 skb_reserve(skb, headroom - delta);
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700831 skb_put(skb, len);
Yuya Kusakabef1d48842020-02-25 12:32:11 +0900832 if (!xdp_prog) {
Jason Wang4941d472017-07-19 16:54:48 +0800833 buf += header_offset;
834 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
Yuya Kusakabef1d48842020-02-25 12:32:11 +0900835 } /* keep zeroed vnet hdr since XDP is loaded */
Jason Wang4941d472017-07-19 16:54:48 +0800836
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900837 if (metasize)
838 skb_metadata_set(skb, metasize);
839
Jason Wang4941d472017-07-19 16:54:48 +0800840 return skb;
841
842err_xdp:
843 rcu_read_unlock();
Jason Wangd46eeea2018-07-31 17:43:39 +0800844 stats->xdp_drops++;
Wenliang Wang053c9e12021-12-16 11:11:35 +0800845err:
Jason Wangd46eeea2018-07-31 17:43:39 +0800846 stats->drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800847 put_page(page);
848xdp_xmit:
849 return NULL;
850}
851
852static struct sk_buff *receive_big(struct net_device *dev,
853 struct virtnet_info *vi,
854 struct receive_queue *rq,
855 void *buf,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900856 unsigned int len,
Jason Wangd46eeea2018-07-31 17:43:39 +0800857 struct virtnet_rq_stats *stats)
Jason Wang4941d472017-07-19 16:54:48 +0800858{
859 struct page *page = buf;
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900860 struct sk_buff *skb =
Xuan Zhuofb328562021-04-16 17:16:15 +0800861 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
Jason Wang4941d472017-07-19 16:54:48 +0800862
Jason Wangd46eeea2018-07-31 17:43:39 +0800863 stats->bytes += len - vi->hdr_len;
Jason Wang4941d472017-07-19 16:54:48 +0800864 if (unlikely(!skb))
865 goto err;
866
867 return skb;
868
869err:
Jason Wangd46eeea2018-07-31 17:43:39 +0800870 stats->drops++;
Jason Wang4941d472017-07-19 16:54:48 +0800871 give_pages(rq, page);
872 return NULL;
873}
874
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200875static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200876 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200877 struct receive_queue *rq,
Michael S. Tsirkin680557c2017-03-06 21:29:47 +0200878 void *buf,
879 void *ctx,
Jason Wang186b3c92017-09-19 17:42:43 +0800880 unsigned int len,
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +0900881 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +0800882 struct virtnet_rq_stats *stats)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000883{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300884 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
885 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200886 struct page *page = virt_to_head_page(buf);
887 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800888 struct sk_buff *head_skb, *curr_skb;
889 struct bpf_prog *xdp_prog;
Jesper Dangaard Brouer9ce61462020-05-14 12:50:44 +0200890 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
Jason Wang4941d472017-07-19 16:54:48 +0800891 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900892 unsigned int metasize = 0;
Jesper Dangaard Brouer9ce61462020-05-14 12:50:44 +0200893 unsigned int frame_sz;
894 int err;
Michael Daltonab7db912014-01-16 22:23:27 -0800895
John Fastabend56434a02016-12-15 12:14:13 -0800896 head_skb = NULL;
Jason Wangd46eeea2018-07-31 17:43:39 +0800897 stats->bytes += len - vi->hdr_len;
John Fastabend56434a02016-12-15 12:14:13 -0800898
Xie Yongjiad993a92021-05-31 21:58:52 +0800899 if (unlikely(len > truesize)) {
900 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
901 dev->name, len, (unsigned long)ctx);
902 dev->stats.rx_length_errors++;
903 goto err_skb;
904 }
Li RongQing6213f072021-10-09 17:32:43 +0800905
906 if (likely(!vi->xdp_enabled)) {
907 xdp_prog = NULL;
908 goto skip_xdp;
909 }
910
John Fastabendf600b692016-12-15 12:13:24 -0800911 rcu_read_lock();
912 xdp_prog = rcu_dereference(rq->xdp_prog);
913 if (xdp_prog) {
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200914 struct xdp_frame *xdpf;
John Fastabend72979a62016-12-15 12:14:36 -0800915 struct page *xdp_page;
John Fastabend0354e4d2017-02-02 19:15:01 -0800916 struct xdp_buff xdp;
John Fastabend0354e4d2017-02-02 19:15:01 -0800917 void *data;
John Fastabendf600b692016-12-15 12:13:24 -0800918 u32 act;
919
Jason Wang3d62b2a2018-05-22 11:44:31 +0800920 /* Transient failure which in theory could occur if
921 * in-flight packets from before XDP was enabled reach
922 * the receive path after XDP is loaded.
923 */
924 if (unlikely(hdr->hdr.gso_type))
925 goto err_xdp;
926
Jesper Dangaard Brouer9ce61462020-05-14 12:50:44 +0200927 /* Buffers with headroom use PAGE_SIZE as alloc size,
928 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
929 */
930 frame_sz = headroom ? PAGE_SIZE : truesize;
931
Jason Wang3cc81a92018-03-02 17:29:14 +0800932 /* This happens when rx buffer size is underestimated
933 * or headroom is not enough because of the buffer
934 * was refilled before XDP is set. This should only
935 * happen for the first several packets, so we don't
936 * care much about its performance.
937 */
Jason Wang4941d472017-07-19 16:54:48 +0800938 if (unlikely(num_buf > 1 ||
939 headroom < virtnet_get_headroom(vi))) {
John Fastabend72979a62016-12-15 12:14:36 -0800940 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800941 xdp_page = xdp_linearize_page(rq, &num_buf,
Jason Wang4941d472017-07-19 16:54:48 +0800942 page, offset,
943 VIRTIO_XDP_HEADROOM,
944 &len);
Jesper Dangaard Brouer9ce61462020-05-14 12:50:44 +0200945 frame_sz = PAGE_SIZE;
946
John Fastabend72979a62016-12-15 12:14:36 -0800947 if (!xdp_page)
948 goto err_xdp;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800949 offset = VIRTIO_XDP_HEADROOM;
John Fastabend72979a62016-12-15 12:14:36 -0800950 } else {
951 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800952 }
953
John Fastabend2de2f7f2017-02-02 19:16:29 -0800954 /* Allow consuming headroom but reserve enough space to push
955 * the descriptor on if we get an XDP_TX return code.
956 */
John Fastabend0354e4d2017-02-02 19:15:01 -0800957 data = page_address(xdp_page) + offset;
Lorenzo Bianconi43b51692020-12-22 22:09:28 +0100958 xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100959 xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
960 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +0100961
John Fastabend0354e4d2017-02-02 19:15:01 -0800962 act = bpf_prog_run_xdp(xdp_prog, &xdp);
Jason Wangd46eeea2018-07-31 17:43:39 +0800963 stats->xdp_packets++;
John Fastabend0354e4d2017-02-02 19:15:01 -0800964
John Fastabend56434a02016-12-15 12:14:13 -0800965 switch (act) {
966 case XDP_PASS:
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900967 metasize = xdp.data - xdp.data_meta;
John Fastabend2de2f7f2017-02-02 19:16:29 -0800968
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900969 /* recalculate offset to account for any header
970 * adjustments and minus the metasize to copy the
971 * metadata in page_to_skb(). Note other cases do not
972 * build an skb and avoid using offset
Nikita V. Shirokov6870de42018-04-17 21:42:20 -0700973 */
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900974 offset = xdp.data - page_address(xdp_page) -
975 vi->hdr_len - metasize;
976
977 /* recalculate len if xdp.data, xdp.data_end or
978 * xdp.data_meta were adjusted
979 */
980 len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
Jason Wang1830f892016-12-23 22:37:27 +0800981 /* We can only create skb based on xdp_page. */
982 if (unlikely(xdp_page != page)) {
983 rcu_read_unlock();
984 put_page(page);
Yuya Kusakabe503d5392020-02-25 12:32:12 +0900985 head_skb = page_to_skb(vi, rq, xdp_page, offset,
986 len, PAGE_SIZE, false,
Jakub Kicinskic32325b2021-08-02 10:57:29 -0700987 metasize,
988 VIRTIO_XDP_HEADROOM);
Jason Wang1830f892016-12-23 22:37:27 +0800989 return head_skb;
990 }
John Fastabend56434a02016-12-15 12:14:13 -0800991 break;
992 case XDP_TX:
Jason Wangd46eeea2018-07-31 17:43:39 +0800993 stats->xdp_tx++;
Lorenzo Bianconi1b698fa2020-05-28 22:47:29 +0200994 xdpf = xdp_convert_buff_to_frame(&xdp);
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +0200995 if (unlikely(!xdpf))
996 goto err_xdp;
Jason Wangca9e83b2018-07-31 17:43:38 +0800997 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100998 if (unlikely(!err)) {
999 xdp_return_frame_rx_napi(xdpf);
1000 } else if (unlikely(err < 0)) {
John Fastabend0354e4d2017-02-02 19:15:01 -08001001 trace_xdp_exception(vi->dev, xdp_prog, act);
Jesper Dangaard Brouer11b7d8972018-02-20 14:32:15 +01001002 if (unlikely(xdp_page != page))
1003 put_page(xdp_page);
1004 goto err_xdp;
1005 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001006 *xdp_xmit |= VIRTIO_XDP_TX;
John Fastabend72979a62016-12-15 12:14:36 -08001007 if (unlikely(xdp_page != page))
Jason Wang5d458a12018-05-22 11:44:29 +08001008 put_page(page);
John Fastabend56434a02016-12-15 12:14:13 -08001009 rcu_read_unlock();
1010 goto xdp_xmit;
Jason Wang3cc81a92018-03-02 17:29:14 +08001011 case XDP_REDIRECT:
Jason Wangd46eeea2018-07-31 17:43:39 +08001012 stats->xdp_redirects++;
Jason Wang3cc81a92018-03-02 17:29:14 +08001013 err = xdp_do_redirect(dev, &xdp, xdp_prog);
1014 if (err) {
1015 if (unlikely(xdp_page != page))
1016 put_page(xdp_page);
1017 goto err_xdp;
1018 }
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001019 *xdp_xmit |= VIRTIO_XDP_REDIR;
Jason Wang3cc81a92018-03-02 17:29:14 +08001020 if (unlikely(xdp_page != page))
Jason Wang68904182018-05-22 11:44:28 +08001021 put_page(page);
Jason Wang3cc81a92018-03-02 17:29:14 +08001022 rcu_read_unlock();
1023 goto xdp_xmit;
John Fastabend56434a02016-12-15 12:14:13 -08001024 default:
Paolo Abenic8064e52021-11-30 11:08:07 +01001025 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001026 fallthrough;
John Fastabend0354e4d2017-02-02 19:15:01 -08001027 case XDP_ABORTED:
1028 trace_xdp_exception(vi->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001029 fallthrough;
John Fastabend0354e4d2017-02-02 19:15:01 -08001030 case XDP_DROP:
John Fastabend72979a62016-12-15 12:14:36 -08001031 if (unlikely(xdp_page != page))
1032 __free_pages(xdp_page, 0);
John Fastabendf600b692016-12-15 12:13:24 -08001033 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -08001034 }
John Fastabendf600b692016-12-15 12:13:24 -08001035 }
1036 rcu_read_unlock();
1037
Li RongQing6213f072021-10-09 17:32:43 +08001038skip_xdp:
Yuya Kusakabe503d5392020-02-25 12:32:12 +09001039 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
Jakub Kicinskic32325b2021-08-02 10:57:29 -07001040 metasize, headroom);
John Fastabendf600b692016-12-15 12:13:24 -08001041 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001042
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001043 if (unlikely(!curr_skb))
1044 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001045 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001046 int num_skb_frags;
1047
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001048 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
Yunjian Wang03e9f8a2017-12-04 14:02:19 +08001049 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001050 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001051 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001052 virtio16_to_cpu(vi->vdev,
1053 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001054 dev->stats.rx_length_errors++;
1055 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001056 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001057
Jason Wangd46eeea2018-07-31 17:43:39 +08001058 stats->bytes += len;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001059 page = virt_to_head_page(buf);
Jason Wang28b39bc2017-07-19 16:54:46 +08001060
1061 truesize = mergeable_ctx_to_truesize(ctx);
1062 if (unlikely(len > truesize)) {
Dan Carpenter56da5fd2017-04-06 12:04:47 +03001063 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001064 dev->name, len, (unsigned long)ctx);
1065 dev->stats.rx_length_errors++;
1066 goto err_skb;
1067 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001068
1069 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -07001070 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1071 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001072
1073 if (unlikely(!nskb))
1074 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -07001075 if (curr_skb == head_skb)
1076 skb_shinfo(curr_skb)->frag_list = nskb;
1077 else
1078 curr_skb->next = nskb;
1079 curr_skb = nskb;
1080 head_skb->truesize += nskb->truesize;
1081 num_skb_frags = 0;
1082 }
1083 if (curr_skb != head_skb) {
1084 head_skb->data_len += len;
1085 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -08001086 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -07001087 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001088 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +08001089 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1090 put_page(page);
1091 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -08001092 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +08001093 } else {
1094 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -08001095 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +08001096 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001097 }
1098
Johannes Berg5377d7582015-08-19 09:48:40 +02001099 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001100 return head_skb;
1101
John Fastabendf600b692016-12-15 12:13:24 -08001102err_xdp:
1103 rcu_read_unlock();
Jason Wangd46eeea2018-07-31 17:43:39 +08001104 stats->xdp_drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001105err_skb:
1106 put_page(page);
Jason Wang850e0882018-05-22 11:44:30 +08001107 while (num_buf-- > 1) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001108 buf = virtqueue_get_buf(rq->vq, &len);
1109 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001110 pr_debug("%s: rx error: %d buffers missing\n",
1111 dev->name, num_buf);
1112 dev->stats.rx_length_errors++;
1113 break;
1114 }
Jason Wangd46eeea2018-07-31 17:43:39 +08001115 stats->bytes += len;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001116 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001117 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001118 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001119err_buf:
Jason Wangd46eeea2018-07-31 17:43:39 +08001120 stats->drops++;
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001121 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -08001122xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +02001123 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001124}
1125
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001126static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1127 void *buf, unsigned int len, void **ctx,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001128 unsigned int *xdp_xmit,
Jason Wangd46eeea2018-07-31 17:43:39 +08001129 struct virtnet_rq_stats *stats)
Rusty Russell296f96f2007-10-22 11:03:37 +10001130{
Jason Wange9d74172012-12-07 07:04:55 +00001131 struct net_device *dev = vi->dev;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001132 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001133 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001134
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +03001135 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001136 pr_debug("%s: short packet %i\n", dev->name, len);
1137 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -08001138 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001139 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001140 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -08001141 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001142 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08001143 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08001144 }
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001145 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001146 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001147
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001148 if (vi->mergeable_rx_bufs)
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001149 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001150 stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001151 else if (vi->big_packets)
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001152 skb = receive_big(dev, vi, rq, buf, len, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001153 else
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001154 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +02001155
1156 if (unlikely(!skb))
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001157 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001158
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001159 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001160
Mike Rapoporte858fae2016-06-08 16:09:21 +03001161 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +00001162 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +10001163
Mike Rapoporte858fae2016-06-08 16:09:21 +03001164 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1165 virtio_is_little_endian(vi->vdev))) {
1166 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1167 dev->name, hdr->hdr.gso_type,
1168 hdr->hdr.gso_size);
1169 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001170 }
1171
Willem de Bruijn133bbb12019-01-17 20:08:53 -05001172 skb_record_rx_queue(skb, vq2rxq(rq->vq));
Mike Rapoportd1dc06d2016-06-14 08:29:38 +03001173 skb->protocol = eth_type_trans(skb, dev);
1174 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1175 ntohs(skb->protocol), skb->len, skb->pkt_type);
1176
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001177 napi_gro_receive(&rq->napi, skb);
Toshiaki Makita7d9d60f2018-07-23 23:36:04 +09001178 return;
Rusty Russell296f96f2007-10-22 11:03:37 +10001179
1180frame_err:
1181 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +10001182 dev_kfree_skb(skb);
1183}
1184
Jason Wang192f68c2017-07-19 16:54:47 +08001185/* Unlike mergeable buffers, all buffers are allocated to the
1186 * same size, except for the headroom. For this reason we do
1187 * not need to use mergeable_len_to_ctx here - it is enough
1188 * to store the headroom as the context ignoring the truesize.
1189 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001190static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1191 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +10001192{
Jason Wangf6b10202017-02-21 16:46:28 +08001193 struct page_frag *alloc_frag = &rq->alloc_frag;
1194 char *buf;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001195 unsigned int xdp_headroom = virtnet_get_headroom(vi);
Jason Wang192f68c2017-07-19 16:54:47 +08001196 void *ctx = (void *)(unsigned long)xdp_headroom;
Jason Wangf6b10202017-02-21 16:46:28 +08001197 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001198 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001199
Jason Wangf6b10202017-02-21 16:46:28 +08001200 len = SKB_DATA_ALIGN(len) +
1201 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1202 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001203 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001204
Jason Wangf6b10202017-02-21 16:46:28 +08001205 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1206 get_page(alloc_frag->page);
1207 alloc_frag->offset += len;
1208 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1209 vi->hdr_len + GOOD_PACKET_LEN);
Jason Wang192f68c2017-07-19 16:54:47 +08001210 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001211 if (err < 0)
Jason Wangf6b10202017-02-21 16:46:28 +08001212 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001213 return err;
1214}
1215
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001216static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1217 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001218{
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001219 struct page *first, *list = NULL;
1220 char *p;
1221 int i, err, offset;
1222
Rusty Russella5835442014-09-11 10:17:36 +09301223 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1224
Jason Wange9d74172012-12-07 07:04:55 +00001225 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001226 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +00001227 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001228 if (!first) {
1229 if (list)
Jason Wange9d74172012-12-07 07:04:55 +00001230 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001231 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -07001232 }
Jason Wange9d74172012-12-07 07:04:55 +00001233 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +10001234
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001235 /* chain new page in list head to match sg */
1236 first->private = (unsigned long)list;
1237 list = first;
1238 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001239
Jason Wange9d74172012-12-07 07:04:55 +00001240 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001241 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +00001242 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001243 return -ENOMEM;
1244 }
1245 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +08001246
Jason Wange9d74172012-12-07 07:04:55 +00001247 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001248 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1249 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +08001250
Jason Wange9d74172012-12-07 07:04:55 +00001251 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001252 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +00001253 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +08001254
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001255 /* chain first in list head */
1256 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301257 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1258 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001259 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +00001260 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +08001261
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001262 return err;
1263}
Herbert Xu97402b92008-04-18 11:24:27 +08001264
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02001265static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
Jason Wang3cc81a92018-03-02 17:29:14 +08001266 struct ewma_pkt_len *avg_pkt_len,
1267 unsigned int room)
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001268{
Michael Daltonab7db912014-01-16 22:23:27 -08001269 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001270 unsigned int len;
1271
Jason Wang3cc81a92018-03-02 17:29:14 +08001272 if (room)
1273 return PAGE_SIZE - room;
1274
1275 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03001276 rq->min_buf_len, PAGE_SIZE - hdr_len);
Jason Wang3cc81a92018-03-02 17:29:14 +08001277
Michael S. Tsirkine377fcc2017-03-06 22:21:35 +02001278 return ALIGN(len, L1_CACHE_BYTES);
Michael Daltonfbf28d72014-01-16 22:23:30 -08001279}
1280
John Fastabend2de2f7f2017-02-02 19:16:29 -08001281static int add_recvbuf_mergeable(struct virtnet_info *vi,
1282 struct receive_queue *rq, gfp_t gfp)
Michael Daltonfbf28d72014-01-16 22:23:30 -08001283{
Michael Daltonfb518792014-01-16 22:23:26 -08001284 struct page_frag *alloc_frag = &rq->alloc_frag;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001285 unsigned int headroom = virtnet_get_headroom(vi);
Jason Wang3cc81a92018-03-02 17:29:14 +08001286 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1287 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
Michael Daltonfb518792014-01-16 22:23:26 -08001288 char *buf;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001289 void *ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001290 int err;
Michael Daltonfb518792014-01-16 22:23:26 -08001291 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +10001292
Jason Wang3cc81a92018-03-02 17:29:14 +08001293 /* Extra tailroom is needed to satisfy XDP's assumption. This
1294 * means rx frags coalescing won't work, but consider we've
1295 * disabled GSO for XDP, it won't be a big issue.
1296 */
1297 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1298 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001299 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -08001300
Michael Daltonfb518792014-01-16 22:23:26 -08001301 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
John Fastabend2de2f7f2017-02-02 19:16:29 -08001302 buf += headroom; /* advance address leaving hole at front of pkt */
Michael Daltonfb518792014-01-16 22:23:26 -08001303 get_page(alloc_frag->page);
Jason Wang3cc81a92018-03-02 17:29:14 +08001304 alloc_frag->offset += len + room;
Michael Daltonfb518792014-01-16 22:23:26 -08001305 hole = alloc_frag->size - alloc_frag->offset;
Jason Wang3cc81a92018-03-02 17:29:14 +08001306 if (hole < len + room) {
Michael Daltonab7db912014-01-16 22:23:27 -08001307 /* To avoid internal fragmentation, if there is very likely not
1308 * enough space for another buffer, add the remaining space to
Michael S. Tsirkin1daa8792017-07-31 21:49:49 +03001309 * the current buffer.
Michael Daltonab7db912014-01-16 22:23:27 -08001310 */
Michael Daltonfb518792014-01-16 22:23:26 -08001311 len += hole;
1312 alloc_frag->offset += hole;
1313 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001314
Michael Daltonfb518792014-01-16 22:23:26 -08001315 sg_init_one(rq->sg, buf, len);
David S. Miller29fda252017-08-01 10:07:50 -07001316 ctx = mergeable_len_to_ctx(len, headroom);
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001317 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001318 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -07001319 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001320
1321 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001322}
1323
Rusty Russellb2baed62011-12-29 00:42:38 +00001324/*
1325 * Returns false if we couldn't fill entirely (OOM).
1326 *
1327 * Normally run in the receive path, but can also be run from ndo_open
1328 * before we're receiving packets, or from refill_work which is
1329 * careful to disable receiving (using napi_disable).
1330 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001331static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1332 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001333{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001334 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001335 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001336
Amit Shah0aea51c2009-08-26 14:58:28 +05301337 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001338 if (vi->mergeable_rx_bufs)
John Fastabend2de2f7f2017-02-02 19:16:29 -08001339 err = add_recvbuf_mergeable(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001340 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001341 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001342 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001343 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001344
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +00001345 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301346 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001347 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001348 } while (rq->vq->num_free);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001349 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
Michael S. Tsirkin01c32592020-05-07 03:25:56 -04001350 unsigned long flags;
1351
1352 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
Jason Wangd46eeea2018-07-31 17:43:39 +08001353 rq->stats.kicks++;
Michael S. Tsirkin01c32592020-05-07 03:25:56 -04001354 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001355 }
1356
Rusty Russell3161e452009-08-26 12:22:32 -07001357 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001358}
1359
Rusty Russell18445c42008-02-04 23:49:57 -05001360static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001361{
1362 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001363 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +00001364
Willem de Bruijne4e84522017-04-24 13:49:26 -04001365 virtqueue_napi_schedule(&rq->napi, rvq);
Rusty Russell296f96f2007-10-22 11:03:37 +10001366}
1367
Willem de Bruijne4e84522017-04-24 13:49:26 -04001368static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001369{
Willem de Bruijne4e84522017-04-24 13:49:26 -04001370 napi_enable(napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001371
1372 /* If all buffers were filled by other side before we napi_enabled, we
Willem de Bruijne4e84522017-04-24 13:49:26 -04001373 * won't get another interrupt, so process any outstanding packets now.
1374 * Call local_bh_enable after to trigger softIRQ processing.
1375 */
1376 local_bh_disable();
1377 virtqueue_napi_schedule(napi, vq);
1378 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -08001379}
1380
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001381static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1382 struct virtqueue *vq,
1383 struct napi_struct *napi)
1384{
1385 if (!napi->weight)
1386 return;
1387
1388 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1389 * enable the feature if this is likely affine with the transmit path.
1390 */
1391 if (!vi->affinity_hint_set) {
1392 napi->weight = 0;
1393 return;
1394 }
1395
1396 return virtnet_napi_enable(vq, napi);
1397}
1398
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001399static void virtnet_napi_tx_disable(struct napi_struct *napi)
1400{
1401 if (napi->weight)
1402 napi_disable(napi);
1403}
1404
Rusty Russell3161e452009-08-26 12:22:32 -07001405static void refill_work(struct work_struct *work)
1406{
Jason Wange9d74172012-12-07 07:04:55 +00001407 struct virtnet_info *vi =
1408 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -07001409 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +00001410 int i;
Rusty Russell3161e452009-08-26 12:22:32 -07001411
Sasha Levin55257d72013-04-29 12:00:08 +09301412 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001413 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -07001414
Jason Wang986a4f42012-12-07 07:04:56 +00001415 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001416 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Willem de Bruijne4e84522017-04-24 13:49:26 -04001417 virtnet_napi_enable(rq->vq, &rq->napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001418
1419 /* In theory, this can happen: if we don't get any buffers in
1420 * we will *never* try to fill again.
1421 */
1422 if (still_empty)
1423 schedule_delayed_work(&vi->refill, HZ/2);
1424 }
Rusty Russell3161e452009-08-26 12:22:32 -07001425}
1426
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001427static int virtnet_receive(struct receive_queue *rq, int budget,
1428 unsigned int *xdp_xmit)
Rusty Russell296f96f2007-10-22 11:03:37 +10001429{
Jason Wange9d74172012-12-07 07:04:55 +00001430 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wangd46eeea2018-07-31 17:43:39 +08001431 struct virtnet_rq_stats stats = {};
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001432 unsigned int len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001433 void *buf;
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001434 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001435
Jason Wang192f68c2017-07-19 16:54:47 +08001436 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001437 void *ctx;
1438
Jason Wangd46eeea2018-07-31 17:43:39 +08001439 while (stats.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001440 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001441 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
Jason Wangd46eeea2018-07-31 17:43:39 +08001442 stats.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001443 }
1444 } else {
Jason Wangd46eeea2018-07-31 17:43:39 +08001445 while (stats.packets < budget &&
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001446 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001447 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
Jason Wangd46eeea2018-07-31 17:43:39 +08001448 stats.packets++;
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02001449 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001450 }
1451
? jiang718be6b2019-08-20 02:51:23 +00001452 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001453 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001454 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -07001455 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001456
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001457 u64_stats_update_begin(&rq->stats.syncp);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001458 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1459 size_t offset = virtnet_rq_stats_desc[i].offset;
1460 u64 *item;
1461
Jason Wangd46eeea2018-07-31 17:43:39 +08001462 item = (u64 *)((u8 *)&rq->stats + offset);
1463 *item += *(u64 *)((u8 *)&stats + offset);
Toshiaki Makitaa0929a42018-07-23 23:36:05 +09001464 }
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001465 u64_stats_update_end(&rq->stats.syncp);
Jason Wang61845d22017-02-17 11:33:09 +08001466
Jason Wangd46eeea2018-07-31 17:43:39 +08001467 return stats.packets;
Jason Wang2ffa7592014-07-23 16:33:54 +08001468}
1469
Michael S. Tsirkindf133f32019-01-17 23:20:07 -05001470static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001471{
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001472 unsigned int len;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001473 unsigned int packets = 0;
1474 unsigned int bytes = 0;
Toshiaki Makita50504712019-01-29 09:45:59 +09001475 void *ptr;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001476
Toshiaki Makita50504712019-01-29 09:45:59 +09001477 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1478 if (likely(!is_xdp_frame(ptr))) {
1479 struct sk_buff *skb = ptr;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001480
Toshiaki Makita50504712019-01-29 09:45:59 +09001481 pr_debug("Sent skb %p\n", skb);
1482
1483 bytes += skb->len;
1484 napi_consume_skb(skb, in_napi);
1485 } else {
1486 struct xdp_frame *frame = ptr_to_xdp(ptr);
1487
1488 bytes += frame->len;
1489 xdp_return_frame(frame);
1490 }
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001491 packets++;
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001492 }
1493
1494 /* Avoid overhead when no packets have been processed
1495 * happens when called speculatively from start_xmit.
1496 */
1497 if (!packets)
1498 return;
1499
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001500 u64_stats_update_begin(&sq->stats.syncp);
1501 sq->stats.bytes += bytes;
1502 sq->stats.packets += packets;
1503 u64_stats_update_end(&sq->stats.syncp);
Willem de Bruijnea7735d2017-04-24 13:49:28 -04001504}
1505
Toshiaki Makita534da5e2019-01-29 09:45:54 +09001506static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1507{
1508 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1509 return false;
1510 else if (q < vi->curr_queue_pairs)
1511 return true;
1512 else
1513 return false;
1514}
1515
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001516static void virtnet_poll_cleantx(struct receive_queue *rq)
1517{
1518 struct virtnet_info *vi = rq->vq->vdev->priv;
1519 unsigned int index = vq2rxq(rq->vq);
1520 struct send_queue *sq = &vi->sq[index];
1521 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1522
Toshiaki Makita534da5e2019-01-29 09:45:54 +09001523 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001524 return;
1525
1526 if (__netif_tx_trylock(txq)) {
Michael S. Tsirkina7766ef2021-04-13 01:30:45 -04001527 do {
1528 virtqueue_disable_cb(sq->vq);
1529 free_old_xmit_skbs(sq, true);
1530 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
Michael S. Tsirkin22bc63c2021-04-13 01:38:08 -04001531
1532 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1533 netif_tx_wake_queue(txq);
1534
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001535 __netif_tx_unlock(txq);
1536 }
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001537}
1538
Jason Wang2ffa7592014-07-23 16:33:54 +08001539static int virtnet_poll(struct napi_struct *napi, int budget)
1540{
1541 struct receive_queue *rq =
1542 container_of(napi, struct receive_queue, napi);
Jason Wang9267c432018-04-13 14:58:25 +08001543 struct virtnet_info *vi = rq->vq->vdev->priv;
1544 struct send_queue *sq;
Toshiaki Makita2a435652018-07-23 23:36:07 +09001545 unsigned int received;
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001546 unsigned int xdp_xmit = 0;
Jason Wang2ffa7592014-07-23 16:33:54 +08001547
Willem de Bruijn7b0411e2017-04-24 13:49:29 -04001548 virtnet_poll_cleantx(rq);
1549
Jason Wang186b3c92017-09-19 17:42:43 +08001550 received = virtnet_receive(rq, budget, &xdp_xmit);
Jason Wang2ffa7592014-07-23 16:33:54 +08001551
Rusty Russell8329d982007-11-19 11:20:43 -05001552 /* Out of packets? */
Willem de Bruijne4e84522017-04-24 13:49:26 -04001553 if (received < budget)
1554 virtqueue_napi_complete(napi, rq->vq, received);
Rusty Russell296f96f2007-10-22 11:03:37 +10001555
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001556 if (xdp_xmit & VIRTIO_XDP_REDIR)
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001557 xdp_do_flush();
Jesper Dangaard Brouer2471c752018-06-26 17:39:58 +02001558
1559 if (xdp_xmit & VIRTIO_XDP_TX) {
Xuan Zhuo97c2c692021-03-10 10:24:45 +08001560 sq = virtnet_xdp_get_sq(vi);
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001561 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1562 u64_stats_update_begin(&sq->stats.syncp);
1563 sq->stats.kicks++;
1564 u64_stats_update_end(&sq->stats.syncp);
1565 }
Xuan Zhuo97c2c692021-03-10 10:24:45 +08001566 virtnet_xdp_put_sq(vi, sq);
Jason Wang9267c432018-04-13 14:58:25 +08001567 }
Jason Wang186b3c92017-09-19 17:42:43 +08001568
Rusty Russell296f96f2007-10-22 11:03:37 +10001569 return received;
1570}
1571
Jason Wang986a4f42012-12-07 07:04:56 +00001572static int virtnet_open(struct net_device *dev)
1573{
1574 struct virtnet_info *vi = netdev_priv(dev);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001575 int i, err;
Jason Wang986a4f42012-12-07 07:04:56 +00001576
Jason Wange4166622013-05-21 20:03:58 +00001577 for (i = 0; i < vi->max_queue_pairs; i++) {
1578 if (i < vi->curr_queue_pairs)
1579 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001580 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001581 schedule_delayed_work(&vi->refill, 0);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001582
Björn Töpelb02e5a02020-11-30 19:52:01 +01001583 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001584 if (err < 0)
1585 return err;
1586
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +02001587 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1588 MEM_TYPE_PAGE_SHARED, NULL);
1589 if (err < 0) {
1590 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1591 return err;
1592 }
1593
Willem de Bruijne4e84522017-04-24 13:49:26 -04001594 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001595 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
Jason Wang986a4f42012-12-07 07:04:56 +00001596 }
1597
1598 return 0;
1599}
1600
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001601static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1602{
1603 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1604 struct virtnet_info *vi = sq->vq->vdev->priv;
Toshiaki Makita534da5e2019-01-29 09:45:54 +09001605 unsigned int index = vq2txq(sq->vq);
1606 struct netdev_queue *txq;
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001607 int opaque;
1608 bool done;
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001609
Toshiaki Makita534da5e2019-01-29 09:45:54 +09001610 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1611 /* We don't need to enable cb for XDP */
1612 napi_complete_done(napi, 0);
1613 return 0;
1614 }
1615
1616 txq = netdev_get_tx_queue(vi->dev, index);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001617 __netif_tx_lock(txq, raw_smp_processor_id());
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001618 virtqueue_disable_cb(sq->vq);
Michael S. Tsirkindf133f32019-01-17 23:20:07 -05001619 free_old_xmit_skbs(sq, true);
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001620
Michael S. Tsirkin22bc63c2021-04-13 01:38:08 -04001621 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1622 netif_tx_wake_queue(txq);
1623
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001624 opaque = virtqueue_enable_cb_prepare(sq->vq);
1625
1626 done = napi_complete_done(napi, 0);
1627
1628 if (!done)
1629 virtqueue_disable_cb(sq->vq);
1630
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001631 __netif_tx_unlock(txq);
1632
Michael S. Tsirkin5a2f9662021-04-13 01:35:26 -04001633 if (done) {
1634 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
1635 if (napi_schedule_prep(napi)) {
1636 __netif_tx_lock(txq, raw_smp_processor_id());
1637 virtqueue_disable_cb(sq->vq);
1638 __netif_tx_unlock(txq);
1639 __napi_schedule(napi);
1640 }
1641 }
1642 }
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001643
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001644 return 0;
1645}
1646
Jason Wange9d74172012-12-07 07:04:55 +00001647static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001648{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001649 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001650 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001651 struct virtnet_info *vi = sq->vq->vdev->priv;
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001652 int num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001653 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301654 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001655
Johannes Berge1749612008-10-27 15:59:26 -07001656 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301657
1658 can_push = vi->any_header_sg &&
1659 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1660 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1661 /* Even if we can, don't push here yet as this would skew
1662 * csum_start offset below. */
1663 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001664 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301665 else
1666 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001667
Mike Rapoporte858fae2016-06-08 16:09:21 +03001668 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04001669 virtio_is_little_endian(vi->vdev), false,
1670 0))
Xianting Tian85eb1382021-06-05 11:31:00 -04001671 return -EPROTO;
Rusty Russell296f96f2007-10-22 11:03:37 +10001672
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001673 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001674 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001675
Jason Wang547c8902015-08-27 14:53:06 +08001676 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301677 if (can_push) {
1678 __skb_push(skb, hdr_len);
1679 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001680 if (unlikely(num_sg < 0))
1681 return num_sg;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301682 /* Pull header back to avoid skew in tx bytes calculations. */
1683 __skb_pull(skb, hdr_len);
1684 } else {
1685 sg_set_buf(sq->sg, hdr, hdr_len);
Jason A. Donenfelde2fcad52017-06-04 04:16:26 +02001686 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1687 if (unlikely(num_sg < 0))
1688 return num_sg;
1689 num_sg++;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301690 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301691 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001692}
1693
Stephen Hemminger424efe92009-08-31 19:50:51 +00001694static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001695{
1696 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001697 int qnum = skb_get_queue_mapping(skb);
1698 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301699 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001700 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
Florian Westphal6b16f9e2019-04-01 16:42:14 +02001701 bool kick = !netdev_xmit_more();
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001702 bool use_napi = sq->napi.weight;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001703
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001704 /* Free up any pending old buffers before queueing new ones. */
Michael S. Tsirkina7766ef2021-04-13 01:30:45 -04001705 do {
1706 if (use_napi)
1707 virtqueue_disable_cb(sq->vq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001708
Michael S. Tsirkina7766ef2021-04-13 01:30:45 -04001709 free_old_xmit_skbs(sq, false);
1710
1711 } while (use_napi && kick &&
1712 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
Willem de Bruijnbdb12e02017-04-24 13:49:30 -04001713
Jacob Keller074c3582014-06-25 02:37:13 +00001714 /* timestamp packet in software */
1715 skb_tx_timestamp(skb);
1716
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001717 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001718 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001719
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301720 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001721 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301722 dev->stats.tx_fifo_errors++;
1723 if (net_ratelimit())
1724 dev_warn(&dev->dev,
Yuval Shaia7934b482019-04-03 12:10:13 +03001725 "Unexpected TXQ (%d) queue failure: %d\n",
1726 qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001727 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001728 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001729 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001730 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001731
Rusty Russell48925e32009-09-24 09:59:20 -06001732 /* Don't wait up for transmitted skbs to be freed. */
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001733 if (!use_napi) {
1734 skb_orphan(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +02001735 nf_reset_ct(skb);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001736 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001737
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001738 /* If running out of space, stop queue to avoid getting packets that we
1739 * are then unable to transmit.
1740 * An alternative would be to force queuing layer to requeue the skb by
1741 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1742 * returned in a normal path of operation: it means that driver is not
1743 * maintaining the TX queue stop/start state properly, and causes
1744 * the stack to do a non-trivial amount of useless work.
1745 * Since most packets only take 1 or 2 ring slots, stopping the queue
1746 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001747 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001748 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001749 netif_stop_subqueue(dev, qnum);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001750 if (!use_napi &&
1751 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001752 /* More just got used, free them then recheck. */
Michael S. Tsirkindf133f32019-01-17 23:20:07 -05001753 free_old_xmit_skbs(sq, false);
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001754 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001755 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001756 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001757 }
1758 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001759 }
Rusty Russell48925e32009-09-24 09:59:20 -06001760
Toshiaki Makita461f03d2018-07-23 23:36:09 +09001761 if (kick || netif_xmit_stopped(txq)) {
1762 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1763 u64_stats_update_begin(&sq->stats.syncp);
1764 sq->stats.kicks++;
1765 u64_stats_update_end(&sq->stats.syncp);
1766 }
1767 }
David S. Miller0b725a22014-08-25 15:51:53 -07001768
Rusty Russell48925e32009-09-24 09:59:20 -06001769 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001770}
1771
Amos Kong40cbfc32013-01-21 01:17:21 +00001772/*
1773 * Send command via the control virtqueue and check status. Commands
1774 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001775 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001776 */
1777static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001778 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001779{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301780 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001781 unsigned out_num = 0, tmp;
Yunjian Wang222722b2021-07-10 11:32:49 +08001782 int ret;
Amos Kong40cbfc32013-01-21 01:17:21 +00001783
1784 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301785 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001786
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001787 vi->ctrl->status = ~0;
1788 vi->ctrl->hdr.class = class;
1789 vi->ctrl->hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301790 /* Add header */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001791 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301792 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001793
Rusty Russellf7bc9592013-03-20 15:44:28 +10301794 if (out)
1795 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001796
Rusty Russellf7bc9592013-03-20 15:44:28 +10301797 /* Add return status. */
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001798 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001799 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001800
stephen hemmingerd24bae32013-12-09 16:17:40 -08001801 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Yunjian Wang222722b2021-07-10 11:32:49 +08001802 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1803 if (ret < 0) {
1804 dev_warn(&vi->vdev->dev,
1805 "Failed to add sgs for command vq: %d\n.", ret);
1806 return false;
1807 }
Amos Kong40cbfc32013-01-21 01:17:21 +00001808
Heinz Graalfs67975902013-10-29 09:40:02 +10301809 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001810 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001811
1812 /* Spin for a response, the kick causes an ioport write, trapping
1813 * into the hypervisor, so the request should be handled immediately.
1814 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301815 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1816 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001817 cpu_relax();
1818
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001819 return vi->ctrl->status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001820}
1821
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001822static int virtnet_set_mac_address(struct net_device *dev, void *p)
1823{
1824 struct virtnet_info *vi = netdev_priv(dev);
1825 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001826 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001827 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001828 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001829
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07001830 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1831 return -EOPNOTSUPP;
1832
Shyam Saini801822d2016-12-24 00:44:58 +05301833 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001834 if (!addr)
1835 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001836
1837 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001838 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001839 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001840
Amos Kong7e58d5a2013-01-21 01:17:23 +00001841 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1842 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1843 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001844 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001845 dev_warn(&vdev->dev,
1846 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001847 ret = -EINVAL;
1848 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001849 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001850 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1851 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301852 unsigned int i;
1853
1854 /* Naturally, this has an atomicity problem. */
1855 for (i = 0; i < dev->addr_len; i++)
1856 virtio_cwrite8(vdev,
1857 offsetof(struct virtio_net_config, mac) +
1858 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001859 }
1860
1861 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001862 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001863
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001864out:
1865 kfree(addr);
1866 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001867}
1868
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001869static void virtnet_stats(struct net_device *dev,
1870 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001871{
1872 struct virtnet_info *vi = netdev_priv(dev);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001873 unsigned int start;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001874 int i;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001875
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001876 for (i = 0; i < vi->max_queue_pairs; i++) {
Tony Lua5207942021-09-17 16:40:06 +08001877 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001878 struct receive_queue *rq = &vi->rq[i];
1879 struct send_queue *sq = &vi->sq[i];
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001880
1881 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001882 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1883 tpackets = sq->stats.packets;
1884 tbytes = sq->stats.bytes;
Tony Lua5207942021-09-17 16:40:06 +08001885 terrors = sq->stats.tx_timeouts;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001886 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001887
1888 do {
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001889 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
Jason Wangd46eeea2018-07-31 17:43:39 +08001890 rpackets = rq->stats.packets;
1891 rbytes = rq->stats.bytes;
1892 rdrops = rq->stats.drops;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09001893 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001894
1895 tot->rx_packets += rpackets;
1896 tot->tx_packets += tpackets;
1897 tot->rx_bytes += rbytes;
1898 tot->tx_bytes += tbytes;
Toshiaki Makita2c4a2f72018-07-23 23:36:06 +09001899 tot->rx_dropped += rdrops;
Tony Lua5207942021-09-17 16:40:06 +08001900 tot->tx_errors += terrors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001901 }
1902
1903 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001904 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001905 tot->rx_length_errors = dev->stats.rx_length_errors;
1906 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001907}
1908
Jason Wang586d17c2012-04-11 20:43:52 +00001909static void virtnet_ack_link_announce(struct virtnet_info *vi)
1910{
1911 rtnl_lock();
1912 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001913 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001914 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1915 rtnl_unlock();
1916}
1917
John Fastabend473153292017-02-02 19:14:32 -08001918static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
Jason Wang986a4f42012-12-07 07:04:56 +00001919{
1920 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001921 struct net_device *dev = vi->dev;
1922
1923 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1924 return 0;
1925
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001926 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1927 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001928
1929 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001930 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001931 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1932 queue_pairs);
1933 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301934 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001935 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001936 /* virtnet_open() will refill when device is going to up. */
1937 if (dev->flags & IFF_UP)
1938 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301939 }
Jason Wang986a4f42012-12-07 07:04:56 +00001940
1941 return 0;
1942}
1943
John Fastabend473153292017-02-02 19:14:32 -08001944static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1945{
1946 int err;
1947
1948 rtnl_lock();
1949 err = _virtnet_set_queues(vi, queue_pairs);
1950 rtnl_unlock();
1951 return err;
1952}
1953
Rusty Russell296f96f2007-10-22 11:03:37 +10001954static int virtnet_close(struct net_device *dev)
1955{
1956 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001957 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001958
Rusty Russellb2baed62011-12-29 00:42:38 +00001959 /* Make sure refill_work doesn't re-enable napi! */
1960 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001961
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001962 for (i = 0; i < vi->max_queue_pairs; i++) {
Jesper Dangaard Brouer754b8a22018-01-03 11:26:04 +01001963 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
Jason Wang986a4f42012-12-07 07:04:56 +00001964 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04001965 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04001966 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001967
Rusty Russell296f96f2007-10-22 11:03:37 +10001968 return 0;
1969}
1970
Alex Williamson2af76982009-02-04 09:02:40 +00001971static void virtnet_set_rx_mode(struct net_device *dev)
1972{
1973 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001974 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001975 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001976 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001977 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001978 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001979 void *buf;
1980 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001981
stephen hemminger788a8b62013-12-09 16:18:45 -08001982 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001983 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1984 return;
1985
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001986 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1987 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001988
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001989 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001990
1991 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001992 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001993 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001994 vi->ctrl->promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001995
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03001996 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001997
1998 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001999 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00002000 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002001 vi->ctrl->allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002002
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002003 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002004 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002005 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002006 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2007 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2008 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00002009 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002010 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002011
Alex Williamson23e258e2009-05-01 17:27:56 +00002012 sg_init_table(sg, 2);
2013
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002014 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02002015 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00002016 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002017 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00002018 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002019
2020 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002021 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002022
2023 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002024 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002025
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02002026 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00002027 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002028 netdev_for_each_mc_addr(ha, dev)
2029 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002030
2031 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002032 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002033
2034 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08002035 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01002036 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00002037
2038 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00002039}
2040
Patrick McHardy80d5c362013-04-19 02:04:28 +00002041static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2042 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00002043{
2044 struct virtnet_info *vi = netdev_priv(dev);
2045 struct scatterlist sg;
2046
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03002047 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002048 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00002049
2050 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08002051 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00002052 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05002053 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00002054}
2055
Patrick McHardy80d5c362013-04-19 02:04:28 +00002056static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2057 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00002058{
2059 struct virtnet_info *vi = netdev_priv(dev);
2060 struct scatterlist sg;
2061
Michael S. Tsirkind7fad4c2018-04-19 08:30:49 +03002062 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002063 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00002064
2065 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08002066 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00002067 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05002068 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00002069}
2070
Peter Xu310974f2019-03-18 14:56:06 +08002071static void virtnet_clean_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002072{
2073 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00002074
2075 if (vi->affinity_hint_set) {
2076 for (i = 0; i < vi->max_queue_pairs; i++) {
Caleb Raitto19e226e2018-08-09 18:18:28 -07002077 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2078 virtqueue_set_affinity(vi->sq[i].vq, NULL);
Wanlong Gao8898c212013-01-24 23:51:30 +00002079 }
2080
2081 vi->affinity_hint_set = false;
2082 }
Wanlong Gao8898c212013-01-24 23:51:30 +00002083}
2084
2085static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002086{
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002087 cpumask_var_t mask;
2088 int stragglers;
2089 int group_size;
2090 int i, j, cpu;
2091 int num_cpu;
2092 int stride;
Jason Wang986a4f42012-12-07 07:04:56 +00002093
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002094 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
Peter Xu310974f2019-03-18 14:56:06 +08002095 virtnet_clean_affinity(vi);
Wanlong Gao8898c212013-01-24 23:51:30 +00002096 return;
Jason Wang986a4f42012-12-07 07:04:56 +00002097 }
2098
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002099 num_cpu = num_online_cpus();
2100 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2101 stragglers = num_cpu >= vi->curr_queue_pairs ?
2102 num_cpu % vi->curr_queue_pairs :
2103 0;
Yury Norov9b51d9d2021-08-14 14:17:05 -07002104 cpu = cpumask_first(cpu_online_mask);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002105
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002106 for (i = 0; i < vi->curr_queue_pairs; i++) {
2107 group_size = stride + (i < stragglers ? 1 : 0);
2108
2109 for (j = 0; j < group_size; j++) {
2110 cpumask_set_cpu(cpu, mask);
2111 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2112 nr_cpu_ids, false);
2113 }
2114 virtqueue_set_affinity(vi->rq[i].vq, mask);
2115 virtqueue_set_affinity(vi->sq[i].vq, mask);
Antoine Tenart044ab862021-03-18 19:37:46 +01002116 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002117 cpumask_clear(mask);
Jason Wang986a4f42012-12-07 07:04:56 +00002118 }
2119
Wanlong Gao8898c212013-01-24 23:51:30 +00002120 vi->affinity_hint_set = true;
Caleb Raitto2ca653d2018-08-09 17:28:40 -07002121 free_cpumask_var(mask);
Jason Wang986a4f42012-12-07 07:04:56 +00002122}
2123
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002124static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002125{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002126 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2127 node);
2128 virtnet_set_affinity(vi);
2129 return 0;
2130}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002131
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002132static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2133{
2134 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2135 node_dead);
2136 virtnet_set_affinity(vi);
2137 return 0;
2138}
Jason Wang3ab098d2013-10-15 11:18:58 +08002139
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002140static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2141{
2142 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2143 node);
2144
Peter Xu310974f2019-03-18 14:56:06 +08002145 virtnet_clean_affinity(vi);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002146 return 0;
2147}
2148
2149static enum cpuhp_state virtionet_online;
2150
2151static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2152{
2153 int ret;
2154
2155 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2156 if (ret)
2157 return ret;
2158 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2159 &vi->node_dead);
2160 if (!ret)
2161 return ret;
2162 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2163 return ret;
2164}
2165
2166static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2167{
2168 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2169 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2170 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08002171}
2172
Rick Jones8f9f4662011-10-19 08:10:59 +00002173static void virtnet_get_ringparam(struct net_device *dev,
Hao Chen74624942021-11-18 20:12:43 +08002174 struct ethtool_ringparam *ring,
2175 struct kernel_ethtool_ringparam *kernel_ring,
2176 struct netlink_ext_ack *extack)
Rick Jones8f9f4662011-10-19 08:10:59 +00002177{
2178 struct virtnet_info *vi = netdev_priv(dev);
2179
Jason Wang986a4f42012-12-07 07:04:56 +00002180 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2181 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00002182 ring->rx_pending = ring->rx_max_pending;
2183 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00002184}
2185
Rick Jones66846042011-11-14 14:17:08 +00002186
2187static void virtnet_get_drvinfo(struct net_device *dev,
2188 struct ethtool_drvinfo *info)
2189{
2190 struct virtnet_info *vi = netdev_priv(dev);
2191 struct virtio_device *vdev = vi->vdev;
2192
2193 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2194 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2195 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2196
2197}
2198
Jason Wangd73bcd22012-12-07 07:04:57 +00002199/* TODO: Eliminate OOO packets during switching */
2200static int virtnet_set_channels(struct net_device *dev,
2201 struct ethtool_channels *channels)
2202{
2203 struct virtnet_info *vi = netdev_priv(dev);
2204 u16 queue_pairs = channels->combined_count;
2205 int err;
2206
2207 /* We don't support separate rx/tx channels.
2208 * We don't allow setting 'other' channels.
2209 */
2210 if (channels->rx_count || channels->tx_count || channels->other_count)
2211 return -EINVAL;
2212
Amos Kongc18e9cd2014-04-18 13:45:41 +08002213 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00002214 return -EINVAL;
2215
John Fastabendf600b692016-12-15 12:13:24 -08002216 /* For now we don't support modifying channels while XDP is loaded
2217 * also when XDP is loaded all RX queues have XDP programs so we only
2218 * need to check a single RX queue.
2219 */
2220 if (vi->rq[0].xdp_prog)
2221 return -EINVAL;
2222
Sebastian Andrzej Siewiora0d1d0f2021-08-03 16:16:04 +02002223 cpus_read_lock();
John Fastabend473153292017-02-02 19:14:32 -08002224 err = _virtnet_set_queues(vi, queue_pairs);
Jeff Dikede332122020-12-22 21:54:21 -05002225 if (err) {
Sebastian Andrzej Siewiora0d1d0f2021-08-03 16:16:04 +02002226 cpus_read_unlock();
Jeff Dikede332122020-12-22 21:54:21 -05002227 goto err;
Jason Wangd73bcd22012-12-07 07:04:57 +00002228 }
Jeff Dikede332122020-12-22 21:54:21 -05002229 virtnet_set_affinity(vi);
Sebastian Andrzej Siewiora0d1d0f2021-08-03 16:16:04 +02002230 cpus_read_unlock();
Jason Wangd73bcd22012-12-07 07:04:57 +00002231
Jeff Dikede332122020-12-22 21:54:21 -05002232 netif_set_real_num_tx_queues(dev, queue_pairs);
2233 netif_set_real_num_rx_queues(dev, queue_pairs);
2234 err:
Jason Wangd73bcd22012-12-07 07:04:57 +00002235 return err;
2236}
2237
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002238static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2239{
2240 struct virtnet_info *vi = netdev_priv(dev);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002241 unsigned int i, j;
Alexander Duyckd7a9a012021-03-16 17:31:29 -07002242 u8 *p = data;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002243
2244 switch (stringset) {
2245 case ETH_SS_STATS:
2246 for (i = 0; i < vi->curr_queue_pairs; i++) {
Alexander Duyckd7a9a012021-03-16 17:31:29 -07002247 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2248 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2249 virtnet_rq_stats_desc[j].desc);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002250 }
2251
2252 for (i = 0; i < vi->curr_queue_pairs; i++) {
Alexander Duyckd7a9a012021-03-16 17:31:29 -07002253 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2254 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2255 virtnet_sq_stats_desc[j].desc);
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002256 }
2257 break;
2258 }
2259}
2260
2261static int virtnet_get_sset_count(struct net_device *dev, int sset)
2262{
2263 struct virtnet_info *vi = netdev_priv(dev);
2264
2265 switch (sset) {
2266 case ETH_SS_STATS:
2267 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2268 VIRTNET_SQ_STATS_LEN);
2269 default:
2270 return -EOPNOTSUPP;
2271 }
2272}
2273
2274static void virtnet_get_ethtool_stats(struct net_device *dev,
2275 struct ethtool_stats *stats, u64 *data)
2276{
2277 struct virtnet_info *vi = netdev_priv(dev);
2278 unsigned int idx = 0, start, i, j;
2279 const u8 *stats_base;
2280 size_t offset;
2281
2282 for (i = 0; i < vi->curr_queue_pairs; i++) {
2283 struct receive_queue *rq = &vi->rq[i];
2284
Jason Wangd46eeea2018-07-31 17:43:39 +08002285 stats_base = (u8 *)&rq->stats;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002286 do {
2287 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2288 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2289 offset = virtnet_rq_stats_desc[j].offset;
2290 data[idx + j] = *(u64 *)(stats_base + offset);
2291 }
2292 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2293 idx += VIRTNET_RQ_STATS_LEN;
2294 }
2295
2296 for (i = 0; i < vi->curr_queue_pairs; i++) {
2297 struct send_queue *sq = &vi->sq[i];
2298
2299 stats_base = (u8 *)&sq->stats;
2300 do {
2301 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2302 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2303 offset = virtnet_sq_stats_desc[j].offset;
2304 data[idx + j] = *(u64 *)(stats_base + offset);
2305 }
2306 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2307 idx += VIRTNET_SQ_STATS_LEN;
2308 }
2309}
2310
Jason Wangd73bcd22012-12-07 07:04:57 +00002311static void virtnet_get_channels(struct net_device *dev,
2312 struct ethtool_channels *channels)
2313{
2314 struct virtnet_info *vi = netdev_priv(dev);
2315
2316 channels->combined_count = vi->curr_queue_pairs;
2317 channels->max_combined = vi->max_queue_pairs;
2318 channels->max_other = 0;
2319 channels->rx_count = 0;
2320 channels->tx_count = 0;
2321 channels->other_count = 0;
2322}
2323
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002324static int virtnet_set_link_ksettings(struct net_device *dev,
2325 const struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002326{
2327 struct virtnet_info *vi = netdev_priv(dev);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002328
Cris Forno9aedc6e22020-02-28 14:12:05 -06002329 return ethtool_virtdev_set_link_ksettings(dev, cmd,
2330 &vi->speed, &vi->duplex);
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002331}
2332
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002333static int virtnet_get_link_ksettings(struct net_device *dev,
2334 struct ethtool_link_ksettings *cmd)
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002335{
2336 struct virtnet_info *vi = netdev_priv(dev);
2337
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002338 cmd->base.speed = vi->speed;
2339 cmd->base.duplex = vi->duplex;
2340 cmd->base.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002341
2342 return 0;
2343}
2344
Jason Wang0c465be2018-10-09 10:06:26 +08002345static int virtnet_set_coalesce(struct net_device *dev,
Yufeng Mof3ccfda12021-08-20 15:35:18 +08002346 struct ethtool_coalesce *ec,
2347 struct kernel_ethtool_coalesce *kernel_coal,
2348 struct netlink_ext_ack *extack)
Jason Wang0c465be2018-10-09 10:06:26 +08002349{
Jason Wang0c465be2018-10-09 10:06:26 +08002350 struct virtnet_info *vi = netdev_priv(dev);
2351 int i, napi_weight;
2352
Jakub Kicinskia51e5202020-03-04 21:15:42 -08002353 if (ec->tx_max_coalesced_frames > 1 ||
2354 ec->rx_max_coalesced_frames != 1)
Jason Wang0c465be2018-10-09 10:06:26 +08002355 return -EINVAL;
2356
Jason Wang0c465be2018-10-09 10:06:26 +08002357 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
Jason Wang0c465be2018-10-09 10:06:26 +08002358 if (napi_weight ^ vi->sq[0].napi.weight) {
2359 if (dev->flags & IFF_UP)
2360 return -EBUSY;
2361 for (i = 0; i < vi->max_queue_pairs; i++)
2362 vi->sq[i].napi.weight = napi_weight;
2363 }
2364
2365 return 0;
2366}
2367
2368static int virtnet_get_coalesce(struct net_device *dev,
Yufeng Mof3ccfda12021-08-20 15:35:18 +08002369 struct ethtool_coalesce *ec,
2370 struct kernel_ethtool_coalesce *kernel_coal,
2371 struct netlink_ext_ack *extack)
Jason Wang0c465be2018-10-09 10:06:26 +08002372{
2373 struct ethtool_coalesce ec_default = {
2374 .cmd = ETHTOOL_GCOALESCE,
2375 .rx_max_coalesced_frames = 1,
2376 };
2377 struct virtnet_info *vi = netdev_priv(dev);
2378
2379 memcpy(ec, &ec_default, sizeof(ec_default));
2380
2381 if (vi->sq[0].napi.weight)
2382 ec->tx_max_coalesced_frames = 1;
2383
2384 return 0;
2385}
2386
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002387static void virtnet_init_settings(struct net_device *dev)
2388{
2389 struct virtnet_info *vi = netdev_priv(dev);
2390
2391 vi->speed = SPEED_UNKNOWN;
2392 vi->duplex = DUPLEX_UNKNOWN;
2393}
2394
Jason Baronfaa9b392018-01-05 17:44:54 -05002395static void virtnet_update_settings(struct virtnet_info *vi)
2396{
2397 u32 speed;
2398 u8 duplex;
2399
2400 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2401 return;
2402
Michael S. Tsirkin64ffa392020-08-05 05:39:36 -04002403 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
2404
Jason Baronfaa9b392018-01-05 17:44:54 -05002405 if (ethtool_validate_speed(speed))
2406 vi->speed = speed;
Michael S. Tsirkin64ffa392020-08-05 05:39:36 -04002407
2408 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
2409
Jason Baronfaa9b392018-01-05 17:44:54 -05002410 if (ethtool_validate_duplex(duplex))
2411 vi->duplex = duplex;
2412}
2413
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002414static const struct ethtool_ops virtnet_ethtool_ops = {
Jakub Kicinskia51e5202020-03-04 21:15:42 -08002415 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
Rick Jones66846042011-11-14 14:17:08 +00002416 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002417 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00002418 .get_ringparam = virtnet_get_ringparam,
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002419 .get_strings = virtnet_get_strings,
2420 .get_sset_count = virtnet_get_sset_count,
2421 .get_ethtool_stats = virtnet_get_ethtool_stats,
Jason Wangd73bcd22012-12-07 07:04:57 +00002422 .set_channels = virtnet_set_channels,
2423 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00002424 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynesebb6b4b2017-03-21 23:24:24 +01002425 .get_link_ksettings = virtnet_get_link_ksettings,
2426 .set_link_ksettings = virtnet_set_link_ksettings,
Jason Wang0c465be2018-10-09 10:06:26 +08002427 .set_coalesce = virtnet_set_coalesce,
2428 .get_coalesce = virtnet_get_coalesce,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08002429};
2430
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002431static void virtnet_freeze_down(struct virtio_device *vdev)
2432{
2433 struct virtnet_info *vi = vdev->priv;
2434 int i;
2435
2436 /* Make sure no work handler is accessing the device */
2437 flush_work(&vi->config_work);
2438
Ake Koomsin05c998b2018-10-17 19:44:12 +09002439 netif_tx_lock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002440 netif_device_detach(vi->dev);
Ake Koomsin05c998b2018-10-17 19:44:12 +09002441 netif_tx_unlock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002442 cancel_delayed_work_sync(&vi->refill);
2443
2444 if (netif_running(vi->dev)) {
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002445 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002446 napi_disable(&vi->rq[i].napi);
Willem de Bruijn78a57b42017-04-25 15:59:17 -04002447 virtnet_napi_tx_disable(&vi->sq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002448 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002449 }
2450}
2451
2452static int init_vqs(struct virtnet_info *vi);
2453
2454static int virtnet_restore_up(struct virtio_device *vdev)
2455{
2456 struct virtnet_info *vi = vdev->priv;
2457 int err, i;
2458
2459 err = init_vqs(vi);
2460 if (err)
2461 return err;
2462
2463 virtio_device_ready(vdev);
2464
2465 if (netif_running(vi->dev)) {
2466 for (i = 0; i < vi->curr_queue_pairs; i++)
2467 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2468 schedule_delayed_work(&vi->refill, 0);
2469
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002470 for (i = 0; i < vi->max_queue_pairs; i++) {
Willem de Bruijne4e84522017-04-24 13:49:26 -04002471 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Willem de Bruijnb92f1e62017-04-24 13:49:27 -04002472 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2473 &vi->sq[i].napi);
2474 }
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002475 }
2476
Ake Koomsin05c998b2018-10-17 19:44:12 +09002477 netif_tx_lock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002478 netif_device_attach(vi->dev);
Ake Koomsin05c998b2018-10-17 19:44:12 +09002479 netif_tx_unlock_bh(vi->dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08002480 return err;
2481}
2482
Jason Wang3f935222017-07-19 16:54:49 +08002483static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2484{
2485 struct scatterlist sg;
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002486 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
Jason Wang3f935222017-07-19 16:54:49 +08002487
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002488 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
Jason Wang3f935222017-07-19 16:54:49 +08002489
2490 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2491 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
Yuval Shaia7934b482019-04-03 12:10:13 +03002492 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
Jason Wang3f935222017-07-19 16:54:49 +08002493 return -EINVAL;
2494 }
2495
2496 return 0;
2497}
2498
2499static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2500{
2501 u64 offloads = 0;
2502
2503 if (!vi->guest_offloads)
2504 return 0;
2505
Jason Wang3f935222017-07-19 16:54:49 +08002506 return virtnet_set_guest_offloads(vi, offloads);
2507}
2508
2509static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2510{
2511 u64 offloads = vi->guest_offloads;
2512
2513 if (!vi->guest_offloads)
2514 return 0;
Jason Wang3f935222017-07-19 16:54:49 +08002515
2516 return virtnet_set_guest_offloads(vi, offloads);
2517}
2518
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002519static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2520 struct netlink_ext_ack *extack)
John Fastabendf600b692016-12-15 12:13:24 -08002521{
2522 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2523 struct virtnet_info *vi = netdev_priv(dev);
2524 struct bpf_prog *old_prog;
Jason Wang017b29c2017-02-20 11:50:20 +08002525 u16 xdp_qp = 0, curr_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002526 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08002527
Jason Wang3f935222017-07-19 16:54:49 +08002528 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2529 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2530 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2531 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
Jason Wang18ba58e2018-11-22 14:36:31 +08002532 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2533 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
Jason Wangdbcf24d2021-08-17 16:06:59 +08002534 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
John Fastabendf600b692016-12-15 12:13:24 -08002535 return -EOPNOTSUPP;
2536 }
2537
2538 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002539 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
John Fastabendf600b692016-12-15 12:13:24 -08002540 return -EINVAL;
2541 }
2542
2543 if (dev->mtu > max_sz) {
Daniel Borkmann4d463c42017-05-03 00:39:17 +02002544 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
John Fastabendf600b692016-12-15 12:13:24 -08002545 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2546 return -EINVAL;
2547 }
2548
John Fastabend672aafd2016-12-15 12:13:49 -08002549 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2550 if (prog)
2551 xdp_qp = nr_cpu_ids;
2552
2553 /* XDP requires extra queues for XDP_TX */
2554 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
Xuan Zhuo9ce4e3d2021-09-18 14:06:15 +08002555 netdev_warn_once(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
2556 curr_qp + xdp_qp, vi->max_queue_pairs);
Xuan Zhuo97c2c692021-03-10 10:24:45 +08002557 xdp_qp = 0;
John Fastabend672aafd2016-12-15 12:13:49 -08002558 }
2559
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002560 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
2561 if (!prog && !old_prog)
2562 return 0;
2563
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002564 if (prog)
2565 bpf_prog_add(prog, vi->max_queue_pairs - 1);
John Fastabend2de2f7f2017-02-02 19:16:29 -08002566
Jason Wang4941d472017-07-19 16:54:48 +08002567 /* Make sure NAPI is not using any XDP TX queues for RX. */
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002568 if (netif_running(dev)) {
2569 for (i = 0; i < vi->max_queue_pairs; i++) {
Jason Wang4e09ff52018-02-28 18:20:04 +08002570 napi_disable(&vi->rq[i].napi);
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002571 virtnet_napi_tx_disable(&vi->sq[i].napi);
2572 }
2573 }
John Fastabendf600b692016-12-15 12:13:24 -08002574
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002575 if (!prog) {
2576 for (i = 0; i < vi->max_queue_pairs; i++) {
2577 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2578 if (i == 0)
2579 virtnet_restore_guest_offloads(vi);
2580 }
2581 synchronize_net();
2582 }
2583
Jason Wang4941d472017-07-19 16:54:48 +08002584 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2585 if (err)
2586 goto err;
Toshiaki Makita188313c2019-01-29 09:45:55 +09002587 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
Jason Wang4941d472017-07-19 16:54:48 +08002588 vi->xdp_queue_pairs = xdp_qp;
John Fastabend672aafd2016-12-15 12:13:49 -08002589
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002590 if (prog) {
Xuan Zhuo97c2c692021-03-10 10:24:45 +08002591 vi->xdp_enabled = true;
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002592 for (i = 0; i < vi->max_queue_pairs; i++) {
2593 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2594 if (i == 0 && !old_prog)
Jason Wang3f935222017-07-19 16:54:49 +08002595 virtnet_clear_guest_offloads(vi);
Jason Wang3f935222017-07-19 16:54:49 +08002596 }
Xuan Zhuo97c2c692021-03-10 10:24:45 +08002597 } else {
2598 vi->xdp_enabled = false;
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002599 }
2600
2601 for (i = 0; i < vi->max_queue_pairs; i++) {
John Fastabendf600b692016-12-15 12:13:24 -08002602 if (old_prog)
2603 bpf_prog_put(old_prog);
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002604 if (netif_running(dev)) {
Jason Wang4e09ff52018-02-28 18:20:04 +08002605 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002606 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2607 &vi->sq[i].napi);
2608 }
John Fastabendf600b692016-12-15 12:13:24 -08002609 }
2610
2611 return 0;
John Fastabend2de2f7f2017-02-02 19:16:29 -08002612
Jason Wang4941d472017-07-19 16:54:48 +08002613err:
Toshiaki Makita03aa6d32019-01-29 09:45:57 +09002614 if (!prog) {
2615 virtnet_clear_guest_offloads(vi);
2616 for (i = 0; i < vi->max_queue_pairs; i++)
2617 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
2618 }
2619
Toshiaki Makita8be4d9a2019-01-29 09:45:53 +09002620 if (netif_running(dev)) {
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002621 for (i = 0; i < vi->max_queue_pairs; i++) {
Toshiaki Makita8be4d9a2019-01-29 09:45:53 +09002622 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
Toshiaki Makita534da5e2019-01-29 09:45:54 +09002623 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2624 &vi->sq[i].napi);
2625 }
Toshiaki Makita8be4d9a2019-01-29 09:45:53 +09002626 }
John Fastabend2de2f7f2017-02-02 19:16:29 -08002627 if (prog)
2628 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2629 return err;
John Fastabendf600b692016-12-15 12:13:24 -08002630}
2631
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002632static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
John Fastabendf600b692016-12-15 12:13:24 -08002633{
2634 switch (xdp->command) {
2635 case XDP_SETUP_PROG:
Jakub Kicinski9861ce02017-04-30 21:46:48 -07002636 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
John Fastabendf600b692016-12-15 12:13:24 -08002637 default:
2638 return -EINVAL;
2639 }
2640}
2641
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002642static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2643 size_t len)
2644{
2645 struct virtnet_info *vi = netdev_priv(dev);
2646 int ret;
2647
2648 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2649 return -EOPNOTSUPP;
2650
2651 ret = snprintf(buf, len, "sby");
2652 if (ret >= len)
2653 return -EOPNOTSUPP;
2654
2655 return 0;
2656}
2657
Willem de Bruijna02e8962018-12-20 17:14:54 -05002658static int virtnet_set_features(struct net_device *dev,
2659 netdev_features_t features)
2660{
2661 struct virtnet_info *vi = netdev_priv(dev);
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002662 u64 offloads;
Willem de Bruijna02e8962018-12-20 17:14:54 -05002663 int err;
2664
Jason Wangdbcf24d2021-08-17 16:06:59 +08002665 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
Xuan Zhuo97c2c692021-03-10 10:24:45 +08002666 if (vi->xdp_enabled)
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002667 return -EBUSY;
2668
Jason Wangdbcf24d2021-08-17 16:06:59 +08002669 if (features & NETIF_F_GRO_HW)
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002670 offloads = vi->guest_offloads_capable;
Willem de Bruijna02e8962018-12-20 17:14:54 -05002671 else
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002672 offloads = vi->guest_offloads_capable &
Jason Wangdbcf24d2021-08-17 16:06:59 +08002673 ~GUEST_OFFLOAD_GRO_HW_MASK;
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04002674
2675 err = virtnet_set_guest_offloads(vi, offloads);
2676 if (err)
2677 return err;
2678 vi->guest_offloads = offloads;
Willem de Bruijna02e8962018-12-20 17:14:54 -05002679 }
2680
2681 return 0;
2682}
2683
Tony Lua5207942021-09-17 16:40:06 +08002684static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
2685{
2686 struct virtnet_info *priv = netdev_priv(dev);
2687 struct send_queue *sq = &priv->sq[txqueue];
2688 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
2689
2690 u64_stats_update_begin(&sq->stats.syncp);
2691 sq->stats.tx_timeouts++;
2692 u64_stats_update_end(&sq->stats.syncp);
2693
2694 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
2695 txqueue, sq->name, sq->vq->index, sq->vq->name,
Eric Dumazet53378242021-11-16 19:29:22 -08002696 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
Tony Lua5207942021-09-17 16:40:06 +08002697}
2698
Stephen Hemminger76288b42009-01-06 10:44:22 -08002699static const struct net_device_ops virtnet_netdev = {
2700 .ndo_open = virtnet_open,
2701 .ndo_stop = virtnet_close,
2702 .ndo_start_xmit = start_xmit,
2703 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08002704 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00002705 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002706 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00002707 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2708 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07002709 .ndo_bpf = virtnet_xdp,
Jason Wang186b3c92017-09-19 17:42:43 +08002710 .ndo_xdp_xmit = virtnet_xdp_xmit,
Vlad Yasevich2836b4f2017-05-23 13:38:43 -04002711 .ndo_features_check = passthru_features_check,
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07002712 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
Willem de Bruijna02e8962018-12-20 17:14:54 -05002713 .ndo_set_features = virtnet_set_features,
Tony Lua5207942021-09-17 16:40:06 +08002714 .ndo_tx_timeout = virtnet_tx_timeout,
Stephen Hemminger76288b42009-01-06 10:44:22 -08002715};
2716
Jason Wang586d17c2012-04-11 20:43:52 +00002717static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002718{
Jason Wang586d17c2012-04-11 20:43:52 +00002719 struct virtnet_info *vi =
2720 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002721 u16 v;
2722
Rusty Russell855e0c52013-10-14 18:11:51 +10302723 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2724 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302725 return;
Jason Wang586d17c2012-04-11 20:43:52 +00002726
2727 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00002728 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00002729 virtnet_ack_link_announce(vi);
2730 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002731
2732 /* Ignore unknown (future) status bits */
2733 v &= VIRTIO_NET_S_LINK_UP;
2734
2735 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10302736 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002737
2738 vi->status = v;
2739
2740 if (vi->status & VIRTIO_NET_S_LINK_UP) {
Jason Baronfaa9b392018-01-05 17:44:54 -05002741 virtnet_update_settings(vi);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002742 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002743 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002744 } else {
2745 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00002746 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002747 }
2748}
2749
2750static void virtnet_config_changed(struct virtio_device *vdev)
2751{
2752 struct virtnet_info *vi = vdev->priv;
2753
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002754 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002755}
2756
Jason Wang986a4f42012-12-07 07:04:56 +00002757static void virtnet_free_queues(struct virtnet_info *vi)
2758{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002759 int i;
2760
Jason Wangab3971b2015-03-12 13:57:44 +08002761 for (i = 0; i < vi->max_queue_pairs; i++) {
Jakub Kicinski5198d5452020-09-09 10:37:51 -07002762 __netif_napi_del(&vi->rq[i].napi);
2763 __netif_napi_del(&vi->sq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08002764 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04002765
Jakub Kicinski5198d5452020-09-09 10:37:51 -07002766 /* We called __netif_napi_del(),
Eric Dumazet963abe52016-11-15 22:24:12 -08002767 * we need to respect an RCU grace period before freeing vi->rq
2768 */
2769 synchronize_net();
2770
Jason Wang986a4f42012-12-07 07:04:56 +00002771 kfree(vi->rq);
2772 kfree(vi->sq);
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002773 kfree(vi->ctrl);
Jason Wang986a4f42012-12-07 07:04:56 +00002774}
2775
John Fastabend473153292017-02-02 19:14:32 -08002776static void _free_receive_bufs(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00002777{
John Fastabendf600b692016-12-15 12:13:24 -08002778 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00002779 int i;
2780
2781 for (i = 0; i < vi->max_queue_pairs; i++) {
2782 while (vi->rq[i].pages)
2783 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08002784
2785 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2786 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2787 if (old_prog)
2788 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00002789 }
John Fastabend473153292017-02-02 19:14:32 -08002790}
2791
2792static void free_receive_bufs(struct virtnet_info *vi)
2793{
2794 rtnl_lock();
2795 _free_receive_bufs(vi);
John Fastabendf600b692016-12-15 12:13:24 -08002796 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002797}
2798
Michael Daltonfb518792014-01-16 22:23:26 -08002799static void free_receive_page_frags(struct virtnet_info *vi)
2800{
2801 int i;
2802 for (i = 0; i < vi->max_queue_pairs; i++)
2803 if (vi->rq[i].alloc_frag.page)
2804 put_page(vi->rq[i].alloc_frag.page);
2805}
2806
Jason Wang986a4f42012-12-07 07:04:56 +00002807static void free_unused_bufs(struct virtnet_info *vi)
2808{
2809 void *buf;
2810 int i;
2811
2812 for (i = 0; i < vi->max_queue_pairs; i++) {
2813 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08002814 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Toshiaki Makita50504712019-01-29 09:45:59 +09002815 if (!is_xdp_frame(buf))
John Fastabend56434a02016-12-15 12:14:13 -08002816 dev_kfree_skb(buf);
2817 else
Toshiaki Makita50504712019-01-29 09:45:59 +09002818 xdp_return_frame(ptr_to_xdp(buf));
John Fastabend56434a02016-12-15 12:14:13 -08002819 }
Jason Wang986a4f42012-12-07 07:04:56 +00002820 }
2821
2822 for (i = 0; i < vi->max_queue_pairs; i++) {
2823 struct virtqueue *vq = vi->rq[i].vq;
2824
2825 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08002826 if (vi->mergeable_rx_bufs) {
Michael S. Tsirkin680557c2017-03-06 21:29:47 +02002827 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002828 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04002829 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08002830 } else {
Jason Wangf6b10202017-02-21 16:46:28 +08002831 put_page(virt_to_head_page(buf));
Michael Daltonab7db912014-01-16 22:23:27 -08002832 }
Jason Wang986a4f42012-12-07 07:04:56 +00002833 }
Jason Wang986a4f42012-12-07 07:04:56 +00002834 }
2835}
2836
Jason Wange9d74172012-12-07 07:04:55 +00002837static void virtnet_del_vqs(struct virtnet_info *vi)
2838{
2839 struct virtio_device *vdev = vi->vdev;
2840
Peter Xu310974f2019-03-18 14:56:06 +08002841 virtnet_clean_affinity(vi);
Jason Wang986a4f42012-12-07 07:04:56 +00002842
Jason Wange9d74172012-12-07 07:04:55 +00002843 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00002844
2845 virtnet_free_queues(vi);
2846}
2847
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002848/* How large should a single buffer be so a queue full of these can fit at
2849 * least one full packet?
2850 * Logic below assumes the mergeable buffer header is used.
2851 */
2852static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2853{
2854 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2855 unsigned int rq_size = virtqueue_get_vring_size(vq);
2856 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2857 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2858 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2859
Michael S. Tsirkinf0c31922017-06-02 17:54:33 +03002860 return max(max(min_buf_len, hdr_len) - hdr_len,
2861 (unsigned int)GOOD_PACKET_LEN);
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002862}
2863
Jason Wang986a4f42012-12-07 07:04:56 +00002864static int virtnet_find_vqs(struct virtnet_info *vi)
2865{
2866 vq_callback_t **callbacks;
2867 struct virtqueue **vqs;
2868 int ret = -ENOMEM;
2869 int i, total_vqs;
2870 const char **names;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002871 bool *ctx;
Jason Wang986a4f42012-12-07 07:04:56 +00002872
2873 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2874 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2875 * possible control vq.
2876 */
2877 total_vqs = vi->max_queue_pairs * 2 +
2878 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2879
2880 /* Allocate space for find_vqs parameters */
Kees Cook6396bb22018-06-12 14:03:40 -07002881 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002882 if (!vqs)
2883 goto err_vq;
Kees Cook6da2ec52018-06-12 13:55:00 -07002884 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002885 if (!callbacks)
2886 goto err_callback;
Kees Cook6da2ec52018-06-12 13:55:00 -07002887 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002888 if (!names)
2889 goto err_names;
Jason Wang192f68c2017-07-19 16:54:47 +08002890 if (!vi->big_packets || vi->mergeable_rx_bufs) {
Kees Cook6396bb22018-06-12 14:03:40 -07002891 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002892 if (!ctx)
2893 goto err_ctx;
2894 } else {
2895 ctx = NULL;
2896 }
Jason Wang986a4f42012-12-07 07:04:56 +00002897
2898 /* Parameters for control virtqueue, if any */
2899 if (vi->has_cvq) {
2900 callbacks[total_vqs - 1] = NULL;
2901 names[total_vqs - 1] = "control";
2902 }
2903
2904 /* Allocate/initialize parameters for send/receive virtqueues */
2905 for (i = 0; i < vi->max_queue_pairs; i++) {
2906 callbacks[rxq2vq(i)] = skb_recv_done;
2907 callbacks[txq2vq(i)] = skb_xmit_done;
2908 sprintf(vi->rq[i].name, "input.%d", i);
2909 sprintf(vi->sq[i].name, "output.%d", i);
2910 names[rxq2vq(i)] = vi->rq[i].name;
2911 names[txq2vq(i)] = vi->sq[i].name;
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002912 if (ctx)
2913 ctx[rxq2vq(i)] = true;
Jason Wang986a4f42012-12-07 07:04:56 +00002914 }
2915
Xianting Tiana2f7dc02021-06-23 11:16:22 -04002916 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
2917 names, ctx, NULL);
Jason Wang986a4f42012-12-07 07:04:56 +00002918 if (ret)
2919 goto err_find;
2920
2921 if (vi->has_cvq) {
2922 vi->cvq = vqs[total_vqs - 1];
2923 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00002924 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002925 }
2926
2927 for (i = 0; i < vi->max_queue_pairs; i++) {
2928 vi->rq[i].vq = vqs[rxq2vq(i)];
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02002929 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
Jason Wang986a4f42012-12-07 07:04:56 +00002930 vi->sq[i].vq = vqs[txq2vq(i)];
2931 }
2932
Tonghao Zhang2fa3c8a2018-05-31 07:16:32 -07002933 /* run here: ret == 0. */
Jason Wang986a4f42012-12-07 07:04:56 +00002934
Jason Wang986a4f42012-12-07 07:04:56 +00002935
2936err_find:
Michael S. Tsirkind45b8972017-03-06 20:31:21 +02002937 kfree(ctx);
2938err_ctx:
Jason Wang986a4f42012-12-07 07:04:56 +00002939 kfree(names);
2940err_names:
2941 kfree(callbacks);
2942err_callback:
2943 kfree(vqs);
2944err_vq:
2945 return ret;
2946}
2947
2948static int virtnet_alloc_queues(struct virtnet_info *vi)
2949{
2950 int i;
2951
Max Gurtovoy122b84a2021-05-02 12:33:19 +03002952 if (vi->has_cvq) {
2953 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2954 if (!vi->ctrl)
2955 goto err_ctrl;
2956 } else {
2957 vi->ctrl = NULL;
2958 }
Kees Cook6396bb22018-06-12 14:03:40 -07002959 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +00002960 if (!vi->sq)
2961 goto err_sq;
Kees Cook6396bb22018-06-12 14:03:40 -07002962 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002963 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002964 goto err_rq;
2965
2966 INIT_DELAYED_WORK(&vi->refill, refill_work);
2967 for (i = 0; i < vi->max_queue_pairs; i++) {
2968 vi->rq[i].pages = NULL;
2969 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2970 napi_weight);
Willem de Bruijn1d11e732017-04-27 20:37:58 -04002971 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2972 napi_tx ? napi_weight : 0);
Jason Wang986a4f42012-12-07 07:04:56 +00002973
2974 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002975 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002976 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09002977
2978 u64_stats_init(&vi->rq[i].stats.syncp);
2979 u64_stats_init(&vi->sq[i].stats.syncp);
Jason Wang986a4f42012-12-07 07:04:56 +00002980 }
2981
2982 return 0;
2983
2984err_rq:
2985 kfree(vi->sq);
2986err_sq:
Michael S. Tsirkin12e57162018-04-19 08:30:48 +03002987 kfree(vi->ctrl);
2988err_ctrl:
Jason Wang986a4f42012-12-07 07:04:56 +00002989 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002990}
2991
Amit Shah3f9c10b2011-12-22 16:58:31 +05302992static int init_vqs(struct virtnet_info *vi)
2993{
Jason Wang986a4f42012-12-07 07:04:56 +00002994 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302995
Jason Wang986a4f42012-12-07 07:04:56 +00002996 /* Allocate send & receive queues */
2997 ret = virtnet_alloc_queues(vi);
2998 if (ret)
2999 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05303000
Jason Wang986a4f42012-12-07 07:04:56 +00003001 ret = virtnet_find_vqs(vi);
3002 if (ret)
3003 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05303004
Sebastian Andrzej Siewiora0d1d0f2021-08-03 16:16:04 +02003005 cpus_read_lock();
Wanlong Gao8898c212013-01-24 23:51:30 +00003006 virtnet_set_affinity(vi);
Sebastian Andrzej Siewiora0d1d0f2021-08-03 16:16:04 +02003007 cpus_read_unlock();
Wanlong Gao47be2472013-01-24 23:51:29 +00003008
Amit Shah3f9c10b2011-12-22 16:58:31 +05303009 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00003010
3011err_free:
3012 virtnet_free_queues(vi);
3013err:
3014 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05303015}
3016
Michael Daltonfbf28d72014-01-16 22:23:30 -08003017#ifdef CONFIG_SYSFS
3018static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -07003019 char *buf)
Michael Daltonfbf28d72014-01-16 22:23:30 -08003020{
3021 struct virtnet_info *vi = netdev_priv(queue->dev);
3022 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Jason Wang3cc81a92018-03-02 17:29:14 +08003023 unsigned int headroom = virtnet_get_headroom(vi);
3024 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
Johannes Berg5377d7582015-08-19 09:48:40 +02003025 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08003026
3027 BUG_ON(queue_index >= vi->max_queue_pairs);
3028 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
Michael S. Tsirkind85b758f72017-03-09 02:21:21 +02003029 return sprintf(buf, "%u\n",
Jason Wang3cc81a92018-03-02 17:29:14 +08003030 get_mergeable_buf_len(&vi->rq[queue_index], avg,
3031 SKB_DATA_ALIGN(headroom + tailroom)));
Michael Daltonfbf28d72014-01-16 22:23:30 -08003032}
3033
3034static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3035 __ATTR_RO(mergeable_rx_buffer_size);
3036
3037static struct attribute *virtio_net_mrg_rx_attrs[] = {
3038 &mergeable_rx_buffer_size_attribute.attr,
3039 NULL
3040};
3041
3042static const struct attribute_group virtio_net_mrg_rx_group = {
3043 .name = "virtio_net",
3044 .attrs = virtio_net_mrg_rx_attrs
3045};
3046#endif
3047
Jason Wang892d6eb2014-11-20 17:03:05 +08003048static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3049 unsigned int fbit,
3050 const char *fname, const char *dname)
3051{
3052 if (!virtio_has_feature(vdev, fbit))
3053 return false;
3054
3055 dev_err(&vdev->dev, "device advertises feature %s but not %s",
3056 fname, dname);
3057
3058 return true;
3059}
3060
3061#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
3062 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3063
3064static bool virtnet_validate_features(struct virtio_device *vdev)
3065{
3066 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3067 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3068 "VIRTIO_NET_F_CTRL_VQ") ||
3069 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3070 "VIRTIO_NET_F_CTRL_VQ") ||
3071 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3072 "VIRTIO_NET_F_CTRL_VQ") ||
3073 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3074 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3075 "VIRTIO_NET_F_CTRL_VQ"))) {
3076 return false;
3077 }
3078
3079 return true;
3080}
3081
Jarod Wilsond0c2c992016-10-20 13:55:21 -04003082#define MIN_MTU ETH_MIN_MTU
3083#define MAX_MTU ETH_MAX_MTU
3084
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003085static int virtnet_validate(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +10003086{
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02003087 if (!vdev->config->get) {
3088 dev_err(&vdev->dev, "%s failure: config access disabled\n",
3089 __func__);
3090 return -EINVAL;
3091 }
3092
Jason Wang892d6eb2014-11-20 17:03:05 +08003093 if (!virtnet_validate_features(vdev))
3094 return -EINVAL;
3095
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003096 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3097 int mtu = virtio_cread16(vdev,
3098 offsetof(struct virtio_net_config,
3099 mtu));
3100 if (mtu < MIN_MTU)
3101 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3102 }
3103
3104 return 0;
3105}
3106
3107static int virtnet_probe(struct virtio_device *vdev)
3108{
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003109 int i, err = -ENOMEM;
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003110 struct net_device *dev;
3111 struct virtnet_info *vi;
3112 u16 max_queue_pairs;
3113 int mtu;
3114
Jason Wang986a4f42012-12-07 07:04:56 +00003115 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10303116 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
3117 struct virtio_net_config,
3118 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00003119
3120 /* We need at least 2 queue's */
3121 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3122 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3123 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3124 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10003125
3126 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00003127 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10003128 if (!dev)
3129 return -ENOMEM;
3130
3131 /* Set up network device as normal. */
Xuan Zhuoab5bd582021-02-18 20:50:17 +00003132 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3133 IFF_TX_SKB_NO_LINEAR;
Stephen Hemminger76288b42009-01-06 10:44:22 -08003134 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10003135 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00003136
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003137 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10003138 SET_NETDEV_DEV(dev, &vdev->dev);
3139
3140 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00003141 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10003142 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08003143 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00003144 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08003145 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00003146
3147 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
David S. Millere078de02017-07-03 06:37:32 -07003148 dev->hw_features |= NETIF_F_TSO
Rusty Russell34a48572008-02-04 23:50:02 -05003149 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
3150 }
Rusty Russell5539ae962008-05-02 21:50:46 -05003151 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00003152 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
3153 dev->hw_features |= NETIF_F_TSO;
3154 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
3155 dev->hw_features |= NETIF_F_TSO6;
3156 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
3157 dev->hw_features |= NETIF_F_TSO_ECN;
Michał Mirosław98e778c2011-03-31 01:01:35 +00003158
Jason Wang41f2f122014-12-24 11:03:52 +08003159 dev->features |= NETIF_F_GSO_ROBUST;
3160
Michał Mirosław98e778c2011-03-31 01:01:35 +00003161 if (gso)
David S. Millere078de02017-07-03 06:37:32 -07003162 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00003163 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10003164 }
Thomas Huth4f491292013-08-27 17:09:02 +02003165 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
3166 dev->features |= NETIF_F_RXCSUM;
Willem de Bruijna02e8962018-12-20 17:14:54 -05003167 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3168 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
Jason Wangdbcf24d2021-08-17 16:06:59 +08003169 dev->features |= NETIF_F_GRO_HW;
Michael S. Tsirkincf8691cb2020-10-21 10:30:43 -04003170 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
Jason Wangdbcf24d2021-08-17 16:06:59 +08003171 dev->hw_features |= NETIF_F_GRO_HW;
Rusty Russell296f96f2007-10-22 11:03:37 +10003172
Jason Wang4fda8302013-04-10 23:32:21 +00003173 dev->vlan_features = dev->features;
3174
Jarod Wilsond0c2c992016-10-20 13:55:21 -04003175 /* MTU range: 68 - 65535 */
3176 dev->min_mtu = MIN_MTU;
3177 dev->max_mtu = MAX_MTU;
3178
Rusty Russell296f96f2007-10-22 11:03:37 +10003179 /* Configuration may specify what MAC to use. Otherwise random. */
Jakub Kicinskif2edaa42021-10-27 08:20:12 -07003180 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3181 u8 addr[ETH_ALEN];
3182
Rusty Russell855e0c52013-10-14 18:11:51 +10303183 virtio_cread_bytes(vdev,
3184 offsetof(struct virtio_net_config, mac),
Jakub Kicinskif2edaa42021-10-27 08:20:12 -07003185 addr, ETH_ALEN);
3186 eth_hw_addr_set(dev, addr);
3187 } else {
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00003188 eth_hw_addr_random(dev);
Jakub Kicinskif2edaa42021-10-27 08:20:12 -07003189 }
Rusty Russell296f96f2007-10-22 11:03:37 +10003190
3191 /* Set up our device-specific information */
3192 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003193 vi->dev = dev;
3194 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01003195 vdev->priv = vi;
John Stultz827da442013-10-07 15:51:58 -07003196
Jason Wang586d17c2012-04-11 20:43:52 +00003197 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10003198
Herbert Xu97402b92008-04-18 11:24:27 +08003199 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00003200 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3201 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05003202 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
3203 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08003204 vi->big_packets = true;
3205
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08003206 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
3207 vi->mergeable_rx_bufs = true;
3208
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03003209 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3210 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03003211 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3212 else
3213 vi->hdr_len = sizeof(struct virtio_net_hdr);
3214
Michael S. Tsirkin75993302015-07-15 15:26:19 +03003215 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
3216 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09303217 vi->any_header_sg = true;
3218
Jason Wang986a4f42012-12-07 07:04:56 +00003219 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3220 vi->has_cvq = true;
3221
Aaron Conole14de9d12016-06-03 16:57:12 -04003222 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3223 mtu = virtio_cread16(vdev,
3224 offsetof(struct virtio_net_config,
3225 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04003226 if (mtu < dev->min_mtu) {
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003227 /* Should never trigger: MTU was previously validated
3228 * in virtnet_validate.
3229 */
Yuval Shaia7934b482019-04-03 12:10:13 +03003230 dev_err(&vdev->dev,
3231 "device MTU appears to have changed it is now %d < %d",
3232 mtu, dev->min_mtu);
Dan Carpenter411ea232020-12-04 17:23:16 +03003233 err = -EINVAL;
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003234 goto free;
Aaron Conole93a205e2016-10-25 16:12:12 -04003235 }
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02003236
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003237 dev->mtu = mtu;
3238 dev->max_mtu = mtu;
3239
Michael S. Tsirkin2e123b42017-03-08 02:14:25 +02003240 /* TODO: size buffers correctly in this case. */
3241 if (dev->mtu > ETH_DATA_LEN)
3242 vi->big_packets = true;
Aaron Conole14de9d12016-06-03 16:57:12 -04003243 }
3244
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03003245 if (vi->any_header_sg)
3246 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08003247
Jason Wang44900012016-11-25 12:37:26 +08003248 /* Enable multiqueue by default */
3249 if (num_online_cpus() >= max_queue_pairs)
3250 vi->curr_queue_pairs = max_queue_pairs;
3251 else
3252 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00003253 vi->max_queue_pairs = max_queue_pairs;
3254
3255 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05303256 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003257 if (err)
Toshiaki Makitad7dfc5c2018-01-17 15:38:25 +09003258 goto free;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003259
Michael Daltonfbf28d72014-01-16 22:23:30 -08003260#ifdef CONFIG_SYSFS
3261 if (vi->mergeable_rx_bufs)
3262 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3263#endif
Zhi Yong Wu0f13b66b2013-11-18 21:19:27 +08003264 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3265 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00003266
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01003267 virtnet_init_settings(dev);
3268
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003269 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3270 vi->failover = net_failover_create(vi->dev);
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00003271 if (IS_ERR(vi->failover)) {
3272 err = PTR_ERR(vi->failover);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003273 goto free_vqs;
Wei Yongjun4b8e6ac2018-05-31 02:05:07 +00003274 }
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003275 }
3276
Rusty Russell296f96f2007-10-22 11:03:37 +10003277 err = register_netdev(dev);
3278 if (err) {
3279 pr_debug("virtio_net: registering device failed\n");
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003280 goto free_failover;
Rusty Russell296f96f2007-10-22 11:03:37 +10003281 }
Rusty Russellb3369c12008-02-04 23:50:02 -05003282
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10303283 virtio_device_ready(vdev);
3284
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003285 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003286 if (err) {
3287 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08003288 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003289 }
3290
Jason Wanga2208712016-12-13 14:23:05 +08003291 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang44900012016-11-25 12:37:26 +08003292
Jason Wang167c25e2010-11-10 14:45:41 +00003293 /* Assume link up if device can't report link status,
3294 otherwise get link status from config. */
Jay Vosburghbda7fab2018-03-22 14:42:41 +00003295 netif_carrier_off(dev);
Jason Wang167c25e2010-11-10 14:45:41 +00003296 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
Tejun Heo3b07e9c2012-08-20 14:51:24 -07003297 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00003298 } else {
3299 vi->status = VIRTIO_NET_S_LINK_UP;
Jason Baronfaa9b392018-01-05 17:44:54 -05003300 virtnet_update_settings(vi);
Jason Wang167c25e2010-11-10 14:45:41 +00003301 netif_carrier_on(dev);
3302 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003303
Jason Wang3f935222017-07-19 16:54:49 +08003304 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3305 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3306 set_bit(guest_offloads[i], &vi->guest_offloads);
Willem de Bruijna02e8962018-12-20 17:14:54 -05003307 vi->guest_offloads_capable = vi->guest_offloads;
Jason Wang3f935222017-07-19 16:54:49 +08003308
Jason Wang986a4f42012-12-07 07:04:56 +00003309 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3310 dev->name, max_queue_pairs);
3311
Rusty Russell296f96f2007-10-22 11:03:37 +10003312 return 0;
3313
wangyunjianf00e35e2016-05-31 11:52:43 +08003314free_unregister_netdev:
Michael S. Tsirkind9679d02021-10-13 06:55:44 -04003315 virtio_reset_device(vdev);
Michael S. Tsirkin02465552014-10-15 10:22:31 +10303316
Rusty Russellb3369c12008-02-04 23:50:02 -05003317 unregister_netdev(dev);
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003318free_failover:
3319 net_failover_destroy(vi->failover);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003320free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00003321 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08003322 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00003323 virtnet_del_vqs(vi);
Rusty Russell296f96f2007-10-22 11:03:37 +10003324free:
3325 free_netdev(dev);
3326 return err;
3327}
3328
Amit Shah04486ed2011-12-22 16:58:32 +05303329static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10003330{
Michael S. Tsirkind9679d02021-10-13 06:55:44 -04003331 virtio_reset_device(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00003332
3333 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00003334 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003335
Jason Wang986a4f42012-12-07 07:04:56 +00003336 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06003337
Michael Daltonfb518792014-01-16 22:23:26 -08003338 free_receive_page_frags(vi);
3339
Jason Wang986a4f42012-12-07 07:04:56 +00003340 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05303341}
3342
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003343static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05303344{
3345 struct virtnet_info *vi = vdev->priv;
3346
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003347 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00003348
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10303349 /* Make sure no work handler is accessing the device. */
3350 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00003351
Amit Shah04486ed2011-12-22 16:58:32 +05303352 unregister_netdev(vi->dev);
3353
Sridhar Samudralaba5e4422018-05-24 09:55:17 -07003354 net_failover_destroy(vi->failover);
3355
Amit Shah04486ed2011-12-22 16:58:32 +05303356 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05003357
Rusty Russell74b25532007-11-19 11:20:42 -05003358 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10003359}
3360
Arnd Bergmann67a75192017-07-25 17:35:50 +02003361static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303362{
3363 struct virtnet_info *vi = vdev->priv;
3364
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003365 virtnet_cpu_notif_remove(vi);
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003366 virtnet_freeze_down(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303367 remove_vq_common(vi);
3368
3369 return 0;
3370}
3371
Arnd Bergmann67a75192017-07-25 17:35:50 +02003372static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
Amit Shah0741bcb2011-12-22 16:58:33 +05303373{
3374 struct virtnet_info *vi = vdev->priv;
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003375 int err;
Amit Shah0741bcb2011-12-22 16:58:33 +05303376
John Fastabend9fe7bfc2017-02-02 19:16:01 -08003377 err = virtnet_restore_up(vdev);
Amit Shah0741bcb2011-12-22 16:58:33 +05303378 if (err)
3379 return err;
Jason Wang986a4f42012-12-07 07:04:56 +00003380 virtnet_set_queues(vi, vi->curr_queue_pairs);
3381
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003382 err = virtnet_cpu_notif_add(vi);
Xie Yongji3f2869c2021-05-17 16:45:16 +08003383 if (err) {
3384 virtnet_freeze_down(vdev);
3385 remove_vq_common(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08003386 return err;
Xie Yongji3f2869c2021-05-17 16:45:16 +08003387 }
Jason Wangec9debb2013-10-29 15:11:07 +08003388
Amit Shah0741bcb2011-12-22 16:58:33 +05303389 return 0;
3390}
Amit Shah0741bcb2011-12-22 16:58:33 +05303391
Rusty Russell296f96f2007-10-22 11:03:37 +10003392static struct virtio_device_id id_table[] = {
3393 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3394 { 0 },
3395};
3396
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003397#define VIRTNET_FEATURES \
3398 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3399 VIRTIO_NET_F_MAC, \
3400 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3401 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3402 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3403 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3404 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3405 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3406 VIRTIO_NET_F_CTRL_MAC_ADDR, \
Jason Baronfaa9b392018-01-05 17:44:54 -05003407 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
Sridhar Samudrala98050692018-05-24 09:55:16 -07003408 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003409
Rusty Russellc45a6812008-05-02 21:50:50 -05003410static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003411 VIRTNET_FEATURES,
3412};
3413
3414static unsigned int features_legacy[] = {
3415 VIRTNET_FEATURES,
3416 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09303417 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05003418};
3419
Uwe Kleine-König22402522009-11-05 01:32:44 -08003420static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05003421 .feature_table = features,
3422 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02003423 .feature_table_legacy = features_legacy,
3424 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10003425 .driver.name = KBUILD_MODNAME,
3426 .driver.owner = THIS_MODULE,
3427 .id_table = id_table,
Michael S. Tsirkinfe36cbe2017-03-29 19:09:14 +03003428 .validate = virtnet_validate,
Rusty Russell296f96f2007-10-22 11:03:37 +10003429 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05003430 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08003431 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09303432#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05303433 .freeze = virtnet_freeze,
3434 .restore = virtnet_restore,
3435#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10003436};
3437
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003438static __init int virtio_net_driver_init(void)
3439{
3440 int ret;
3441
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003442 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003443 virtnet_cpu_online,
3444 virtnet_cpu_down_prep);
3445 if (ret < 0)
3446 goto out;
3447 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01003448 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003449 NULL, virtnet_cpu_dead);
3450 if (ret)
3451 goto err_dead;
3452
3453 ret = register_virtio_driver(&virtio_net_driver);
3454 if (ret)
3455 goto err_virtio;
3456 return 0;
3457err_virtio:
3458 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3459err_dead:
3460 cpuhp_remove_multi_state(virtionet_online);
3461out:
3462 return ret;
3463}
3464module_init(virtio_net_driver_init);
3465
3466static __exit void virtio_net_driver_exit(void)
3467{
Andrew Jonescfa0ebc2017-07-24 15:38:32 +02003468 unregister_virtio_driver(&virtio_net_driver);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003469 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3470 cpuhp_remove_multi_state(virtionet_online);
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02003471}
3472module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10003473
3474MODULE_DEVICE_TABLE(virtio, id_table);
3475MODULE_DESCRIPTION("Virtio network driver");
3476MODULE_LICENSE("GPL");