blob: 8b18246ad9992483faf94ff16471e40b0d39da21 [file] [log] [blame]
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001/*
2 * Virtual network driver for conversing with remote driver backends.
3 *
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation; or, when distributed
10 * separately from the Linux kernel or incorporated into other
11 * software packages, subject to the following license:
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
30 */
31
Joe Perches383eda32013-06-27 21:57:49 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070034#include <linux/module.h>
35#include <linux/kernel.h>
36#include <linux/netdevice.h>
37#include <linux/etherdevice.h>
38#include <linux/skbuff.h>
39#include <linux/ethtool.h>
40#include <linux/if_ether.h>
Wei Liu9ecd1a72013-04-22 02:20:41 +000041#include <net/tcp.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070042#include <linux/udp.h>
43#include <linux/moduleparam.h>
44#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070046#include <net/ip.h>
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +030047#include <linux/bpf.h>
48#include <net/page_pool.h>
49#include <linux/bpf_trace.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070050
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070051#include <xen/xen.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070052#include <xen/xenbus.h>
53#include <xen/events.h>
54#include <xen/page.h>
Igor Mammedovb9136d22012-03-21 15:08:38 +010055#include <xen/platform_pci.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070056#include <xen/grant_table.h>
57
58#include <xen/interface/io/netif.h>
59#include <xen/interface/memory.h>
60#include <xen/interface/grant_table.h>
61
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +010062/* Module parameters */
Juergen Gross034702a2017-01-10 14:32:51 +010063#define MAX_QUEUES_DEFAULT 8
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +010064static unsigned int xennet_max_queues;
65module_param_named(max_queues, xennet_max_queues, uint, 0644);
66MODULE_PARM_DESC(max_queues,
67 "Maximum number of queues per virtual interface");
68
Andrea Righic2c63312020-07-24 10:59:10 +020069#define XENNET_TIMEOUT (5 * HZ)
70
Stephen Hemminger0fc0b732009-09-02 01:03:33 -070071static const struct ethtool_ops xennet_ethtool_ops;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070072
73struct netfront_cb {
Ian Campbell36832432012-08-22 00:26:47 +000074 int pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070075};
76
77#define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
78
79#define RX_COPY_THRESHOLD 256
80
81#define GRANT_INVALID_REF 0
82
Julien Grall30c5d7f2015-04-10 14:42:21 +010083#define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
84#define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
David Vrabel1f3c2eb2014-10-22 11:17:06 +010085
86/* Minimum number of Rx slots (includes slot for GSO metadata). */
87#define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070088
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +010089/* Queue name is interface name with "-qNNN" appended */
90#define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
91
92/* IRQ name is queue name with "-tx" or "-rx" appended */
93#define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
94
Juergen Gross8edfe2e2018-09-07 14:21:30 +020095static DECLARE_WAIT_QUEUE_HEAD(module_wq);
Eduardo Otubo5b5971d2017-11-23 15:18:35 +010096
stephen hemmingere00f85b2011-06-21 05:35:31 +000097struct netfront_stats {
David Vrabel900e1832015-01-13 16:42:42 +000098 u64 packets;
99 u64 bytes;
stephen hemmingere00f85b2011-06-21 05:35:31 +0000100 struct u64_stats_sync syncp;
101};
102
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100103struct netfront_info;
104
105struct netfront_queue {
106 unsigned int id; /* Queue ID, 0-based */
107 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
108 struct netfront_info *info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700109
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300110 struct bpf_prog __rcu *xdp_prog;
111
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700112 struct napi_struct napi;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700113
Wei Liud634bf22013-05-22 06:34:46 +0000114 /* Split event channels support, tx_* == rx_* when using
115 * single event channel.
116 */
117 unsigned int tx_evtchn, rx_evtchn;
118 unsigned int tx_irq, rx_irq;
119 /* Only used when split event channels support is enabled */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100120 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
121 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700122
123 spinlock_t tx_lock;
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700124 struct xen_netif_tx_front_ring tx;
125 int tx_ring_ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700126
127 /*
128 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
Juergen Gross21631d22021-08-24 12:28:08 +0200129 * are linked from tx_skb_freelist through tx_link.
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700130 */
Juergen Gross21631d22021-08-24 12:28:08 +0200131 struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
132 unsigned short tx_link[NET_TX_RING_SIZE];
133#define TX_LINK_NONE 0xffff
Juergen Grossa884daa2021-08-24 12:28:09 +0200134#define TX_PENDING 0xfffe
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700135 grant_ref_t gref_tx_head;
136 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
Annie Licefe0072014-01-28 11:35:42 +0800137 struct page *grant_tx_page[NET_TX_RING_SIZE];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700138 unsigned tx_skb_freelist;
Juergen Grossa884daa2021-08-24 12:28:09 +0200139 unsigned int tx_pend_queue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700140
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700141 spinlock_t rx_lock ____cacheline_aligned_in_smp;
142 struct xen_netif_rx_front_ring rx;
143 int rx_ring_ref;
144
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700145 struct timer_list rx_refill_timer;
146
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700147 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
148 grant_ref_t gref_rx_head;
149 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300150
Juergen Grossb27d4792021-12-16 08:24:08 +0100151 unsigned int rx_rsp_unconsumed;
152 spinlock_t rx_cons_lock;
153
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300154 struct page_pool *page_pool;
155 struct xdp_rxq_info xdp_rxq;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100156};
157
158struct netfront_info {
159 struct list_head list;
160 struct net_device *netdev;
161
162 struct xenbus_device *xbdev;
163
164 /* Multi-queue support */
165 struct netfront_queue *queues;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000166
167 /* Statistics */
David Vrabel900e1832015-01-13 16:42:42 +0000168 struct netfront_stats __percpu *rx_stats;
169 struct netfront_stats __percpu *tx_stats;
stephen hemmingere00f85b2011-06-21 05:35:31 +0000170
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300171 /* XDP state */
172 bool netback_has_xdp_headroom;
173 bool netfront_xdp_enabled;
174
Juergen Grossa884daa2021-08-24 12:28:09 +0200175 /* Is device behaving sane? */
176 bool broken;
177
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100178 atomic_t rx_gso_checksum_fixup;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700179};
180
181struct netfront_rx_info {
182 struct xen_netif_rx_response rx;
183 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
184};
185
186/*
187 * Access macros for acquiring freeing slots in tx_skbs[].
188 */
189
Juergen Gross21631d22021-08-24 12:28:08 +0200190static void add_id_to_list(unsigned *head, unsigned short *list,
191 unsigned short id)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700192{
Juergen Gross21631d22021-08-24 12:28:08 +0200193 list[id] = *head;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700194 *head = id;
195}
196
Juergen Gross21631d22021-08-24 12:28:08 +0200197static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700198{
199 unsigned int id = *head;
Juergen Gross21631d22021-08-24 12:28:08 +0200200
201 if (id != TX_LINK_NONE) {
202 *head = list[id];
203 list[id] = TX_LINK_NONE;
204 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700205 return id;
206}
207
208static int xennet_rxidx(RING_IDX idx)
209{
210 return idx & (NET_RX_RING_SIZE - 1);
211}
212
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100213static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700214 RING_IDX ri)
215{
216 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100217 struct sk_buff *skb = queue->rx_skbs[i];
218 queue->rx_skbs[i] = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700219 return skb;
220}
221
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100222static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700223 RING_IDX ri)
224{
225 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100226 grant_ref_t ref = queue->grant_rx_ref[i];
227 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700228 return ref;
229}
230
231#ifdef CONFIG_SYSFS
Takashi Iwai27b917e2015-02-04 14:38:55 +0100232static const struct attribute_group xennet_dev_group;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700233#endif
234
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000235static bool xennet_can_sg(struct net_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700236{
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000237 return dev->features & NETIF_F_SG;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700238}
239
240
Kees Cooke99e88a2017-10-16 14:43:17 -0700241static void rx_refill_timeout(struct timer_list *t)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700242{
Kees Cooke99e88a2017-10-16 14:43:17 -0700243 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100244 napi_schedule(&queue->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700245}
246
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100247static int netfront_tx_slot_available(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700248{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100249 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
Juergen Gross57f230a2018-06-12 08:57:53 +0200250 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700251}
252
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100253static void xennet_maybe_wake_tx(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700254{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100255 struct net_device *dev = queue->info->netdev;
256 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700257
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100258 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
259 netfront_tx_slot_available(queue) &&
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700260 likely(netif_running(dev)))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100261 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700262}
263
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100264
265static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700266{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700267 struct sk_buff *skb;
268 struct page *page;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100269
270 skb = __netdev_alloc_skb(queue->info->netdev,
271 RX_COPY_THRESHOLD + NET_IP_ALIGN,
272 GFP_ATOMIC | __GFP_NOWARN);
273 if (unlikely(!skb))
274 return NULL;
275
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300276 page = page_pool_dev_alloc_pages(queue->page_pool);
277 if (unlikely(!page)) {
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100278 kfree_skb(skb);
279 return NULL;
280 }
281 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
282
283 /* Align ip header to a 16 bytes boundary */
284 skb_reserve(skb, NET_IP_ALIGN);
285 skb->dev = queue->info->netdev;
286
287 return skb;
288}
289
290
291static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
292{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100293 RING_IDX req_prod = queue->rx.req_prod_pvt;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100294 int notify;
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000295 int err = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700296
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100297 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700298 return;
299
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100300 for (req_prod = queue->rx.req_prod_pvt;
301 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
302 req_prod++) {
303 struct sk_buff *skb;
304 unsigned short id;
305 grant_ref_t ref;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100306 struct page *page;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100307 struct xen_netif_rx_request *req;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700308
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100309 skb = xennet_alloc_one_rx_buffer(queue);
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000310 if (!skb) {
311 err = -ENOMEM;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700312 break;
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000313 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700314
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100315 id = xennet_rxidx(req_prod);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700316
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100317 BUG_ON(queue->rx_skbs[id]);
318 queue->rx_skbs[id] = skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700319
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100320 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
Dongli Zhang269ebce2016-11-02 09:04:33 +0800321 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100322 queue->grant_rx_ref[id] = ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700323
Julien Grall30c5d7f2015-04-10 14:42:21 +0100324 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700325
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100326 req = RING_GET_REQUEST(&queue->rx, req_prod);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100327 gnttab_page_grant_foreign_access_ref_one(ref,
328 queue->info->xbdev->otherend_id,
329 page,
330 0);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700331 req->id = id;
332 req->gref = ref;
333 }
334
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100335 queue->rx.req_prod_pvt = req_prod;
336
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000337 /* Try again later if there are not enough requests or skb allocation
338 * failed.
339 * Enough requests is quantified as the sum of newly created slots and
340 * the unconsumed slots at the backend.
341 */
342 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
343 unlikely(err)) {
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100344 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
345 return;
346 }
347
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100348 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700349 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100350 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700351}
352
353static int xennet_open(struct net_device *dev)
354{
355 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100356 unsigned int num_queues = dev->real_num_tx_queues;
357 unsigned int i = 0;
358 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700359
Juergen Grossa884daa2021-08-24 12:28:09 +0200360 if (!np->queues || np->broken)
Ross Lagerwallf599c642018-01-11 09:36:38 +0000361 return -ENODEV;
362
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100363 for (i = 0; i < num_queues; ++i) {
364 queue = &np->queues[i];
365 napi_enable(&queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700366
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100367 spin_lock_bh(&queue->rx_lock);
368 if (netif_carrier_ok(dev)) {
369 xennet_alloc_rx_buffers(queue);
370 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
371 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
372 napi_schedule(&queue->napi);
373 }
374 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700375 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700376
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100377 netif_tx_start_all_queues(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700378
379 return 0;
380}
381
Juergen Grossb27d4792021-12-16 08:24:08 +0100382static bool xennet_tx_buf_gc(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700383{
384 RING_IDX cons, prod;
385 unsigned short id;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700386 struct sk_buff *skb;
Malcolm Crossley7d0105b2016-01-26 17:12:44 +0000387 bool more_to_do;
Juergen Grossb27d4792021-12-16 08:24:08 +0100388 bool work_done = false;
Juergen Grossa884daa2021-08-24 12:28:09 +0200389 const struct device *dev = &queue->info->netdev->dev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700390
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100391 BUG_ON(!netif_carrier_ok(queue->info->netdev));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700392
393 do {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100394 prod = queue->tx.sring->rsp_prod;
Juergen Grossa884daa2021-08-24 12:28:09 +0200395 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
396 dev_alert(dev, "Illegal number of responses %u\n",
397 prod - queue->tx.rsp_cons);
398 goto err;
399 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700400 rmb(); /* Ensure we see responses up to 'rp'. */
401
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100402 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
Juergen Gross84460662021-08-24 12:28:06 +0200403 struct xen_netif_tx_response txrsp;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700404
Juergen Grossb27d4792021-12-16 08:24:08 +0100405 work_done = true;
406
Juergen Gross84460662021-08-24 12:28:06 +0200407 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
408 if (txrsp.status == XEN_NETIF_RSP_NULL)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700409 continue;
410
Juergen Grossa884daa2021-08-24 12:28:09 +0200411 id = txrsp.id;
412 if (id >= RING_SIZE(&queue->tx)) {
413 dev_alert(dev,
414 "Response has incorrect id (%u)\n",
415 id);
416 goto err;
417 }
418 if (queue->tx_link[id] != TX_PENDING) {
419 dev_alert(dev,
420 "Response for inactive request\n");
421 goto err;
422 }
423
424 queue->tx_link[id] = TX_LINK_NONE;
Juergen Gross21631d22021-08-24 12:28:08 +0200425 skb = queue->tx_skbs[id];
426 queue->tx_skbs[id] = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700427 if (unlikely(gnttab_query_foreign_access(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100428 queue->grant_tx_ref[id]) != 0)) {
Juergen Grossa884daa2021-08-24 12:28:09 +0200429 dev_alert(dev,
430 "Grant still in use by backend domain\n");
431 goto err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700432 }
433 gnttab_end_foreign_access_ref(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100434 queue->grant_tx_ref[id], GNTMAP_readonly);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700435 gnttab_release_grant_reference(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100436 &queue->gref_tx_head, queue->grant_tx_ref[id]);
437 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
438 queue->grant_tx_page[id] = NULL;
Juergen Gross21631d22021-08-24 12:28:08 +0200439 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700440 dev_kfree_skb_irq(skb);
441 }
442
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100443 queue->tx.rsp_cons = prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700444
Malcolm Crossley7d0105b2016-01-26 17:12:44 +0000445 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
446 } while (more_to_do);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700447
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100448 xennet_maybe_wake_tx(queue);
Juergen Grossa884daa2021-08-24 12:28:09 +0200449
Juergen Grossb27d4792021-12-16 08:24:08 +0100450 return work_done;
Juergen Grossa884daa2021-08-24 12:28:09 +0200451
452 err:
453 queue->info->broken = true;
454 dev_alert(dev, "Disabled for further use\n");
Juergen Grossb27d4792021-12-16 08:24:08 +0100455
456 return work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700457}
458
Julien Grall30c5d7f2015-04-10 14:42:21 +0100459struct xennet_gnttab_make_txreq {
460 struct netfront_queue *queue;
461 struct sk_buff *skb;
462 struct page *page;
Juergen Gross162081ec2021-08-24 12:28:07 +0200463 struct xen_netif_tx_request *tx; /* Last request on ring page */
464 struct xen_netif_tx_request tx_local; /* Last request local copy*/
Julien Grall30c5d7f2015-04-10 14:42:21 +0100465 unsigned int size;
466};
467
468static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
469 unsigned int len, void *data)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700470{
Julien Grall30c5d7f2015-04-10 14:42:21 +0100471 struct xennet_gnttab_make_txreq *info = data;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700472 unsigned int id;
David Vrabela55e8bb2015-01-13 17:16:44 +0000473 struct xen_netif_tx_request *tx;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700474 grant_ref_t ref;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100475 /* convenient aliases */
476 struct page *page = info->page;
477 struct netfront_queue *queue = info->queue;
478 struct sk_buff *skb = info->skb;
David Vrabela55e8bb2015-01-13 17:16:44 +0000479
Juergen Gross21631d22021-08-24 12:28:08 +0200480 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
David Vrabela55e8bb2015-01-13 17:16:44 +0000481 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
482 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
Dongli Zhang269ebce2016-11-02 09:04:33 +0800483 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
David Vrabela55e8bb2015-01-13 17:16:44 +0000484
Julien Grall30c5d7f2015-04-10 14:42:21 +0100485 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
486 gfn, GNTMAP_readonly);
David Vrabela55e8bb2015-01-13 17:16:44 +0000487
Juergen Gross21631d22021-08-24 12:28:08 +0200488 queue->tx_skbs[id] = skb;
David Vrabela55e8bb2015-01-13 17:16:44 +0000489 queue->grant_tx_page[id] = page;
490 queue->grant_tx_ref[id] = ref;
491
Juergen Gross162081ec2021-08-24 12:28:07 +0200492 info->tx_local.id = id;
493 info->tx_local.gref = ref;
494 info->tx_local.offset = offset;
495 info->tx_local.size = len;
496 info->tx_local.flags = 0;
497
498 *tx = info->tx_local;
David Vrabela55e8bb2015-01-13 17:16:44 +0000499
Juergen Grossa884daa2021-08-24 12:28:09 +0200500 /*
501 * Put the request in the pending queue, it will be set to be pending
502 * when the producer index is about to be raised.
503 */
504 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
505
Julien Grall30c5d7f2015-04-10 14:42:21 +0100506 info->tx = tx;
Juergen Gross162081ec2021-08-24 12:28:07 +0200507 info->size += info->tx_local.size;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100508}
509
510static struct xen_netif_tx_request *xennet_make_first_txreq(
Juergen Gross162081ec2021-08-24 12:28:07 +0200511 struct xennet_gnttab_make_txreq *info,
512 unsigned int offset, unsigned int len)
Julien Grall30c5d7f2015-04-10 14:42:21 +0100513{
Juergen Gross162081ec2021-08-24 12:28:07 +0200514 info->size = 0;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100515
Juergen Gross162081ec2021-08-24 12:28:07 +0200516 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100517
Juergen Gross162081ec2021-08-24 12:28:07 +0200518 return info->tx;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100519}
520
521static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
522 unsigned int len, void *data)
523{
524 struct xennet_gnttab_make_txreq *info = data;
525
526 info->tx->flags |= XEN_NETTXF_more_data;
527 skb_get(info->skb);
528 xennet_tx_setup_grant(gfn, offset, len, data);
David Vrabela55e8bb2015-01-13 17:16:44 +0000529}
530
Juergen Gross162081ec2021-08-24 12:28:07 +0200531static void xennet_make_txreqs(
532 struct xennet_gnttab_make_txreq *info,
533 struct page *page,
David Vrabela55e8bb2015-01-13 17:16:44 +0000534 unsigned int offset, unsigned int len)
535{
536 /* Skip unused frames from start of page */
537 page += offset >> PAGE_SHIFT;
538 offset &= ~PAGE_MASK;
539
540 while (len) {
Juergen Gross162081ec2021-08-24 12:28:07 +0200541 info->page = page;
542 info->size = 0;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100543
544 gnttab_foreach_grant_in_range(page, offset, len,
545 xennet_make_one_txreq,
Juergen Gross162081ec2021-08-24 12:28:07 +0200546 info);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100547
David Vrabela55e8bb2015-01-13 17:16:44 +0000548 page++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700549 offset = 0;
Juergen Gross162081ec2021-08-24 12:28:07 +0200550 len -= info->size;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700551 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700552}
553
Ian Campbellf36c3742012-11-21 02:02:16 +0000554/*
David Vrabele84448d2015-01-13 17:16:43 +0000555 * Count how many ring slots are required to send this skb. Each frag
556 * might be a compound page.
Ian Campbellf36c3742012-11-21 02:02:16 +0000557 */
David Vrabele84448d2015-01-13 17:16:43 +0000558static int xennet_count_skb_slots(struct sk_buff *skb)
Ian Campbellf36c3742012-11-21 02:02:16 +0000559{
560 int i, frags = skb_shinfo(skb)->nr_frags;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100561 int slots;
David Vrabele84448d2015-01-13 17:16:43 +0000562
Julien Grall30c5d7f2015-04-10 14:42:21 +0100563 slots = gnttab_count_grant(offset_in_page(skb->data),
564 skb_headlen(skb));
Ian Campbellf36c3742012-11-21 02:02:16 +0000565
566 for (i = 0; i < frags; i++) {
567 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
568 unsigned long size = skb_frag_size(frag);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700569 unsigned long offset = skb_frag_off(frag);
Ian Campbellf36c3742012-11-21 02:02:16 +0000570
571 /* Skip unused frames from start of page */
572 offset &= ~PAGE_MASK;
573
Julien Grall30c5d7f2015-04-10 14:42:21 +0100574 slots += gnttab_count_grant(offset, size);
Ian Campbellf36c3742012-11-21 02:02:16 +0000575 }
576
Julien Grall30c5d7f2015-04-10 14:42:21 +0100577 return slots;
Ian Campbellf36c3742012-11-21 02:02:16 +0000578}
579
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100580static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +0100581 struct net_device *sb_dev)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100582{
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100583 unsigned int num_queues = dev->real_num_tx_queues;
584 u32 hash;
585 u16 queue_idx;
586
587 /* First, check if there is only one queue */
588 if (num_queues == 1) {
589 queue_idx = 0;
590 } else {
591 hash = skb_get_hash(skb);
592 queue_idx = hash % num_queues;
593 }
594
595 return queue_idx;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100596}
597
Juergen Grossa884daa2021-08-24 12:28:09 +0200598static void xennet_mark_tx_pending(struct netfront_queue *queue)
599{
600 unsigned int i;
601
602 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
603 TX_LINK_NONE)
604 queue->tx_link[i] = TX_PENDING;
605}
606
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300607static int xennet_xdp_xmit_one(struct net_device *dev,
608 struct netfront_queue *queue,
609 struct xdp_frame *xdpf)
610{
611 struct netfront_info *np = netdev_priv(dev);
612 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
Juergen Gross162081ec2021-08-24 12:28:07 +0200613 struct xennet_gnttab_make_txreq info = {
614 .queue = queue,
615 .skb = NULL,
616 .page = virt_to_page(xdpf->data),
617 };
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300618 int notify;
619
Juergen Gross162081ec2021-08-24 12:28:07 +0200620 xennet_make_first_txreq(&info,
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300621 offset_in_page(xdpf->data),
622 xdpf->len);
623
Juergen Grossa884daa2021-08-24 12:28:09 +0200624 xennet_mark_tx_pending(queue);
625
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300626 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
627 if (notify)
628 notify_remote_via_irq(queue->tx_irq);
629
630 u64_stats_update_begin(&tx_stats->syncp);
631 tx_stats->bytes += xdpf->len;
632 tx_stats->packets++;
633 u64_stats_update_end(&tx_stats->syncp);
634
635 xennet_tx_buf_gc(queue);
636
637 return 0;
638}
639
640static int xennet_xdp_xmit(struct net_device *dev, int n,
641 struct xdp_frame **frames, u32 flags)
642{
643 unsigned int num_queues = dev->real_num_tx_queues;
644 struct netfront_info *np = netdev_priv(dev);
645 struct netfront_queue *queue = NULL;
646 unsigned long irq_flags;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100647 int nxmit = 0;
648 int i;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300649
Juergen Grossa884daa2021-08-24 12:28:09 +0200650 if (unlikely(np->broken))
651 return -ENODEV;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300652 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
653 return -EINVAL;
654
655 queue = &np->queues[smp_processor_id() % num_queues];
656
657 spin_lock_irqsave(&queue->tx_lock, irq_flags);
658 for (i = 0; i < n; i++) {
659 struct xdp_frame *xdpf = frames[i];
660
661 if (!xdpf)
662 continue;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100663 if (xennet_xdp_xmit_one(dev, queue, xdpf))
664 break;
665 nxmit++;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300666 }
667 spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
668
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100669 return nxmit;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300670}
671
672
Julien Grall30c5d7f2015-04-10 14:42:21 +0100673#define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
674
Luc Van Oostenryck24a94b32018-04-24 15:18:14 +0200675static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700676{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700677 struct netfront_info *np = netdev_priv(dev);
David Vrabel900e1832015-01-13 16:42:42 +0000678 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
Juergen Gross162081ec2021-08-24 12:28:07 +0200679 struct xen_netif_tx_request *first_tx;
David Vrabela55e8bb2015-01-13 17:16:44 +0000680 unsigned int i;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700681 int notify;
Ian Campbellf36c3742012-11-21 02:02:16 +0000682 int slots;
David Vrabela55e8bb2015-01-13 17:16:44 +0000683 struct page *page;
684 unsigned int offset;
685 unsigned int len;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +0000686 unsigned long flags;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100687 struct netfront_queue *queue = NULL;
Juergen Gross162081ec2021-08-24 12:28:07 +0200688 struct xennet_gnttab_make_txreq info = { };
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100689 unsigned int num_queues = dev->real_num_tx_queues;
690 u16 queue_index;
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200691 struct sk_buff *nskb;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100692
693 /* Drop the packet if no queues are set up */
694 if (num_queues < 1)
695 goto drop;
Juergen Grossa884daa2021-08-24 12:28:09 +0200696 if (unlikely(np->broken))
697 goto drop;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100698 /* Determine which queue to transmit this SKB on */
699 queue_index = skb_get_queue_mapping(skb);
700 queue = &np->queues[queue_index];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700701
Wei Liu9ecd1a72013-04-22 02:20:41 +0000702 /* If skb->len is too big for wire format, drop skb and alert
703 * user about misconfiguration.
704 */
705 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
706 net_alert_ratelimited(
707 "xennet: skb->len = %u, too big for wire format\n",
708 skb->len);
709 goto drop;
710 }
711
David Vrabele84448d2015-01-13 17:16:43 +0000712 slots = xennet_count_skb_slots(skb);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100713 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
Zoltan Kiss97a6d1b2014-08-11 18:32:23 +0100714 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
715 slots, skb->len);
716 if (skb_linearize(skb))
717 goto drop;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700718 }
719
David Vrabela55e8bb2015-01-13 17:16:44 +0000720 page = virt_to_page(skb->data);
721 offset = offset_in_page(skb->data);
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200722
723 /* The first req should be at least ETH_HLEN size or the packet will be
724 * dropped by netback.
725 */
726 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
727 nskb = skb_copy(skb, GFP_ATOMIC);
728 if (!nskb)
729 goto drop;
Eric Dumazet62f32502017-08-30 10:32:58 -0700730 dev_consume_skb_any(skb);
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200731 skb = nskb;
732 page = virt_to_page(skb->data);
733 offset = offset_in_page(skb->data);
734 }
735
David Vrabela55e8bb2015-01-13 17:16:44 +0000736 len = skb_headlen(skb);
737
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100738 spin_lock_irqsave(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700739
740 if (unlikely(!netif_carrier_ok(dev) ||
Ian Campbellf36c3742012-11-21 02:02:16 +0000741 (slots > 1 && !xennet_can_sg(dev)) ||
Johannes Berg8b86a612015-04-17 15:45:04 +0200742 netif_needs_gso(skb, netif_skb_features(skb)))) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100743 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700744 goto drop;
745 }
746
David Vrabela55e8bb2015-01-13 17:16:44 +0000747 /* First request for the linear area. */
Juergen Gross162081ec2021-08-24 12:28:07 +0200748 info.queue = queue;
749 info.skb = skb;
750 info.page = page;
751 first_tx = xennet_make_first_txreq(&info, offset, len);
752 offset += info.tx_local.size;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100753 if (offset == PAGE_SIZE) {
754 page++;
755 offset = 0;
756 }
Juergen Gross162081ec2021-08-24 12:28:07 +0200757 len -= info.tx_local.size;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700758
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700759 if (skb->ip_summed == CHECKSUM_PARTIAL)
760 /* local packet? */
Juergen Gross162081ec2021-08-24 12:28:07 +0200761 first_tx->flags |= XEN_NETTXF_csum_blank |
762 XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700763 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
764 /* remote but checksummed. */
Juergen Gross162081ec2021-08-24 12:28:07 +0200765 first_tx->flags |= XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700766
David Vrabela55e8bb2015-01-13 17:16:44 +0000767 /* Optional extra info after the first request. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700768 if (skb_shinfo(skb)->gso_size) {
769 struct xen_netif_extra_info *gso;
770
771 gso = (struct xen_netif_extra_info *)
David Vrabela55e8bb2015-01-13 17:16:44 +0000772 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700773
Juergen Gross162081ec2021-08-24 12:28:07 +0200774 first_tx->flags |= XEN_NETTXF_extra_info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700775
776 gso->u.gso.size = skb_shinfo(skb)->gso_size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000777 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
778 XEN_NETIF_GSO_TYPE_TCPV6 :
779 XEN_NETIF_GSO_TYPE_TCPV4;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700780 gso->u.gso.pad = 0;
781 gso->u.gso.features = 0;
782
783 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
784 gso->flags = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700785 }
786
David Vrabela55e8bb2015-01-13 17:16:44 +0000787 /* Requests for the rest of the linear area. */
Juergen Gross162081ec2021-08-24 12:28:07 +0200788 xennet_make_txreqs(&info, page, offset, len);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700789
David Vrabela55e8bb2015-01-13 17:16:44 +0000790 /* Requests for all the frags. */
791 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
792 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Juergen Gross162081ec2021-08-24 12:28:07 +0200793 xennet_make_txreqs(&info, skb_frag_page(frag),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700794 skb_frag_off(frag),
David Vrabela55e8bb2015-01-13 17:16:44 +0000795 skb_frag_size(frag));
796 }
797
798 /* First request has the packet length. */
799 first_tx->size = skb->len;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700800
Daniel Drown91ffb9d2020-07-03 01:22:34 -0500801 /* timestamp packet in software */
802 skb_tx_timestamp(skb);
803
Juergen Grossa884daa2021-08-24 12:28:09 +0200804 xennet_mark_tx_pending(queue);
805
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100806 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700807 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100808 notify_remote_via_irq(queue->tx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700809
David Vrabel900e1832015-01-13 16:42:42 +0000810 u64_stats_update_begin(&tx_stats->syncp);
811 tx_stats->bytes += skb->len;
812 tx_stats->packets++;
813 u64_stats_update_end(&tx_stats->syncp);
Jeremy Fitzhardinge10a273a2007-08-13 12:54:37 -0700814
815 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100816 xennet_tx_buf_gc(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700817
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100818 if (!netfront_tx_slot_available(queue))
819 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700820
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100821 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700822
Patrick McHardy6ed10652009-06-23 06:03:08 +0000823 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700824
825 drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700826 dev->stats.tx_dropped++;
Eric W. Biederman979de8a2014-03-15 18:33:04 -0700827 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000828 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700829}
830
831static int xennet_close(struct net_device *dev)
832{
833 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100834 unsigned int num_queues = dev->real_num_tx_queues;
835 unsigned int i;
836 struct netfront_queue *queue;
837 netif_tx_stop_all_queues(np->netdev);
838 for (i = 0; i < num_queues; ++i) {
839 queue = &np->queues[i];
840 napi_disable(&queue->napi);
841 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700842 return 0;
843}
844
Juergen Grossb27d4792021-12-16 08:24:08 +0100845static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
846{
847 unsigned long flags;
848
849 spin_lock_irqsave(&queue->rx_cons_lock, flags);
850 queue->rx.rsp_cons = val;
851 queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
852 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
853}
854
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100855static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700856 grant_ref_t ref)
857{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100858 int new = xennet_rxidx(queue->rx.req_prod_pvt);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700859
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100860 BUG_ON(queue->rx_skbs[new]);
861 queue->rx_skbs[new] = skb;
862 queue->grant_rx_ref[new] = ref;
863 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
864 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
865 queue->rx.req_prod_pvt++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700866}
867
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100868static int xennet_get_extras(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700869 struct xen_netif_extra_info *extras,
870 RING_IDX rp)
871
872{
Juergen Gross84460662021-08-24 12:28:06 +0200873 struct xen_netif_extra_info extra;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100874 struct device *dev = &queue->info->netdev->dev;
875 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700876 int err = 0;
877
878 do {
879 struct sk_buff *skb;
880 grant_ref_t ref;
881
882 if (unlikely(cons + 1 == rp)) {
883 if (net_ratelimit())
884 dev_warn(dev, "Missing extra info\n");
885 err = -EBADR;
886 break;
887 }
888
Juergen Gross84460662021-08-24 12:28:06 +0200889 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700890
Juergen Gross84460662021-08-24 12:28:06 +0200891 if (unlikely(!extra.type ||
892 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700893 if (net_ratelimit())
894 dev_warn(dev, "Invalid extra type: %d\n",
Juergen Gross84460662021-08-24 12:28:06 +0200895 extra.type);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700896 err = -EINVAL;
897 } else {
Juergen Gross84460662021-08-24 12:28:06 +0200898 extras[extra.type - 1] = extra;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700899 }
900
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100901 skb = xennet_get_rx_skb(queue, cons);
902 ref = xennet_get_rx_ref(queue, cons);
903 xennet_move_rx_slot(queue, skb, ref);
Juergen Gross84460662021-08-24 12:28:06 +0200904 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700905
Juergen Grossb27d4792021-12-16 08:24:08 +0100906 xennet_set_rx_rsp_cons(queue, cons);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700907 return err;
908}
909
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300910static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
911 struct xen_netif_rx_response *rx, struct bpf_prog *prog,
912 struct xdp_buff *xdp, bool *need_xdp_flush)
913{
914 struct xdp_frame *xdpf;
915 u32 len = rx->status;
Colin Ian Kinge44f65f2020-07-02 15:22:23 +0100916 u32 act;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300917 int err;
918
Lorenzo Bianconi43b51692020-12-22 22:09:28 +0100919 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
920 &queue->xdp_rxq);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100921 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
922 len, false);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300923
924 act = bpf_prog_run_xdp(prog, xdp);
925 switch (act) {
926 case XDP_TX:
927 get_page(pdata);
928 xdpf = xdp_convert_buff_to_frame(xdp);
929 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100930 if (unlikely(!err))
931 xdp_return_frame_rx_napi(xdpf);
932 else if (unlikely(err < 0))
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300933 trace_xdp_exception(queue->info->netdev, prog, act);
934 break;
935 case XDP_REDIRECT:
936 get_page(pdata);
937 err = xdp_do_redirect(queue->info->netdev, xdp, prog);
938 *need_xdp_flush = true;
939 if (unlikely(err))
940 trace_xdp_exception(queue->info->netdev, prog, act);
941 break;
942 case XDP_PASS:
943 case XDP_DROP:
944 break;
945
946 case XDP_ABORTED:
947 trace_xdp_exception(queue->info->netdev, prog, act);
948 break;
949
950 default:
Paolo Abenic8064e52021-11-30 11:08:07 +0100951 bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300952 }
953
954 return act;
955}
956
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100957static int xennet_get_responses(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700958 struct netfront_rx_info *rinfo, RING_IDX rp,
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300959 struct sk_buff_head *list,
960 bool *need_xdp_flush)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700961{
Juergen Gross84460662021-08-24 12:28:06 +0200962 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300963 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100964 RING_IDX cons = queue->rx.rsp_cons;
965 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300966 struct xen_netif_extra_info *extras = rinfo->extras;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100967 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300968 struct device *dev = &queue->info->netdev->dev;
969 struct bpf_prog *xdp_prog;
970 struct xdp_buff xdp;
971 unsigned long ret;
Wei Liu7158ff62013-03-25 01:08:19 +0000972 int slots = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700973 int err = 0;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300974 u32 verdict;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700975
Ian Campbellf942dc22011-03-15 00:06:18 +0000976 if (rx->flags & XEN_NETRXF_extra_info) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100977 err = xennet_get_extras(queue, extras, rp);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300978 if (!err) {
979 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
980 struct xen_netif_extra_info *xdp;
981
982 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
983 rx->offset = xdp->u.xdp.headroom;
984 }
985 }
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100986 cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700987 }
988
989 for (;;) {
990 if (unlikely(rx->status < 0 ||
Julien Grall30c5d7f2015-04-10 14:42:21 +0100991 rx->offset + rx->status > XEN_PAGE_SIZE)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700992 if (net_ratelimit())
Julien Grall6c101272015-06-16 20:10:46 +0100993 dev_warn(dev, "rx->offset: %u, size: %d\n",
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700994 rx->offset, rx->status);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100995 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700996 err = -EINVAL;
997 goto next;
998 }
999
1000 /*
1001 * This definitely indicates a bug, either in this driver or in
1002 * the backend driver. In future this should flag the bad
Wei Liu697089d2013-04-22 02:20:40 +00001003 * situation to the system controller to reboot the backend.
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001004 */
1005 if (ref == GRANT_INVALID_REF) {
1006 if (net_ratelimit())
1007 dev_warn(dev, "Bad rx response id %d.\n",
1008 rx->id);
1009 err = -EINVAL;
1010 goto next;
1011 }
1012
1013 ret = gnttab_end_foreign_access_ref(ref, 0);
1014 BUG_ON(!ret);
1015
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001016 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001017
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001018 rcu_read_lock();
1019 xdp_prog = rcu_dereference(queue->xdp_prog);
1020 if (xdp_prog) {
1021 if (!(rx->flags & XEN_NETRXF_more_data)) {
1022 /* currently only a single page contains data */
1023 verdict = xennet_run_xdp(queue,
1024 skb_frag_page(&skb_shinfo(skb)->frags[0]),
1025 rx, xdp_prog, &xdp, need_xdp_flush);
1026 if (verdict != XDP_PASS)
1027 err = -EINVAL;
1028 } else {
1029 /* drop the frame */
1030 err = -EINVAL;
1031 }
1032 }
1033 rcu_read_unlock();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001034next:
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001035 __skb_queue_tail(list, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001036 if (!(rx->flags & XEN_NETRXF_more_data))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001037 break;
1038
Wei Liu7158ff62013-03-25 01:08:19 +00001039 if (cons + slots == rp) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001040 if (net_ratelimit())
Wei Liu7158ff62013-03-25 01:08:19 +00001041 dev_warn(dev, "Need more slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001042 err = -ENOENT;
1043 break;
1044 }
1045
Juergen Gross84460662021-08-24 12:28:06 +02001046 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
1047 rx = &rx_local;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001048 skb = xennet_get_rx_skb(queue, cons + slots);
1049 ref = xennet_get_rx_ref(queue, cons + slots);
Wei Liu7158ff62013-03-25 01:08:19 +00001050 slots++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001051 }
1052
Wei Liu7158ff62013-03-25 01:08:19 +00001053 if (unlikely(slots > max)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001054 if (net_ratelimit())
Wei Liu697089d2013-04-22 02:20:40 +00001055 dev_warn(dev, "Too many slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001056 err = -E2BIG;
1057 }
1058
1059 if (unlikely(err))
Juergen Grossb27d4792021-12-16 08:24:08 +01001060 xennet_set_rx_rsp_cons(queue, cons + slots);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001061
1062 return err;
1063}
1064
1065static int xennet_set_skb_gso(struct sk_buff *skb,
1066 struct xen_netif_extra_info *gso)
1067{
1068 if (!gso->u.gso.size) {
1069 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -07001070 pr_warn("GSO size must not be zero\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001071 return -EINVAL;
1072 }
1073
Paul Durrant2c0057d2014-01-15 17:30:33 +00001074 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
1075 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001076 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -07001077 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001078 return -EINVAL;
1079 }
1080
1081 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001082 skb_shinfo(skb)->gso_type =
1083 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
1084 SKB_GSO_TCPV4 :
1085 SKB_GSO_TCPV6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001086
1087 /* Header must be checked, and gso_segs computed. */
1088 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1089 skb_shinfo(skb)->gso_segs = 0;
1090
1091 return 0;
1092}
1093
Dongli Zhanga7611292019-10-01 21:56:41 +08001094static int xennet_fill_frags(struct netfront_queue *queue,
1095 struct sk_buff *skb,
1096 struct sk_buff_head *list)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001097{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001098 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001099 struct sk_buff *nskb;
1100
1101 while ((nskb = __skb_dequeue(list))) {
Juergen Gross84460662021-08-24 12:28:06 +02001102 struct xen_netif_rx_response rx;
Ian Campbell01c68022011-10-05 00:28:47 +00001103 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001104
Juergen Gross84460662021-08-24 12:28:06 +02001105 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1106
Juergen Grossd472b3a2018-08-09 16:42:16 +02001107 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
Jan Beulich093b9c72013-07-17 08:09:37 +01001108 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001109
Juergen Grossd81c5052018-12-18 16:06:19 +01001110 BUG_ON(pull_to < skb_headlen(skb));
Jan Beulich093b9c72013-07-17 08:09:37 +01001111 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1112 }
Juergen Grossad4f15d2018-09-11 09:04:48 +02001113 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
Juergen Grossb27d4792021-12-16 08:24:08 +01001114 xennet_set_rx_rsp_cons(queue,
1115 ++cons + skb_queue_len(list));
Juergen Grossad4f15d2018-09-11 09:04:48 +02001116 kfree_skb(nskb);
Dongli Zhanga7611292019-10-01 21:56:41 +08001117 return -ENOENT;
Juergen Grossad4f15d2018-09-11 09:04:48 +02001118 }
Jan Beulich093b9c72013-07-17 08:09:37 +01001119
Juergen Grossd472b3a2018-08-09 16:42:16 +02001120 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1121 skb_frag_page(nfrag),
Juergen Gross84460662021-08-24 12:28:06 +02001122 rx.offset, rx.status, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001123
1124 skb_shinfo(nskb)->nr_frags = 0;
1125 kfree_skb(nskb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001126 }
1127
Juergen Grossb27d4792021-12-16 08:24:08 +01001128 xennet_set_rx_rsp_cons(queue, cons);
Dongli Zhanga7611292019-10-01 21:56:41 +08001129
1130 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001131}
1132
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001133static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001134{
Paul Durrantb5cf66c2014-01-09 10:02:48 +00001135 bool recalculate_partial_csum = false;
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001136
1137 /*
1138 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1139 * peers can fail to set NETRXF_csum_blank when sending a GSO
1140 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1141 * recalculate the partial checksum.
1142 */
1143 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1144 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001145 atomic_inc(&np->rx_gso_checksum_fixup);
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001146 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrantb5cf66c2014-01-09 10:02:48 +00001147 recalculate_partial_csum = true;
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001148 }
1149
1150 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1151 if (skb->ip_summed != CHECKSUM_PARTIAL)
1152 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001153
Paul Durrantb5cf66c2014-01-09 10:02:48 +00001154 return skb_checksum_setup(skb, recalculate_partial_csum);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001155}
1156
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001157static int handle_incoming_queue(struct netfront_queue *queue,
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001158 struct sk_buff_head *rxq)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001159{
David Vrabel900e1832015-01-13 16:42:42 +00001160 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001161 int packets_dropped = 0;
1162 struct sk_buff *skb;
1163
1164 while ((skb = __skb_dequeue(rxq)) != NULL) {
Ian Campbell36832432012-08-22 00:26:47 +00001165 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001166
Jan Beulich093b9c72013-07-17 08:09:37 +01001167 if (pull_to > skb_headlen(skb))
1168 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001169
1170 /* Ethernet work: Delayed to here as it peeks the header. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001171 skb->protocol = eth_type_trans(skb, queue->info->netdev);
Wei Liud554f732014-02-19 18:48:34 +00001172 skb_reset_network_header(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001173
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001174 if (checksum_setup(queue->info->netdev, skb)) {
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001175 kfree_skb(skb);
1176 packets_dropped++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001177 queue->info->netdev->stats.rx_errors++;
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001178 continue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001179 }
1180
David Vrabel900e1832015-01-13 16:42:42 +00001181 u64_stats_update_begin(&rx_stats->syncp);
1182 rx_stats->packets++;
1183 rx_stats->bytes += skb->len;
1184 u64_stats_update_end(&rx_stats->syncp);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001185
1186 /* Pass it up. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001187 napi_gro_receive(&queue->napi, skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001188 }
1189
1190 return packets_dropped;
1191}
1192
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001193static int xennet_poll(struct napi_struct *napi, int budget)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001194{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001195 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1196 struct net_device *dev = queue->info->netdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001197 struct sk_buff *skb;
1198 struct netfront_rx_info rinfo;
1199 struct xen_netif_rx_response *rx = &rinfo.rx;
1200 struct xen_netif_extra_info *extras = rinfo.extras;
1201 RING_IDX i, rp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001202 int work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001203 struct sk_buff_head rxq;
1204 struct sk_buff_head errq;
1205 struct sk_buff_head tmpq;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001206 int err;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001207 bool need_xdp_flush = false;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001208
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001209 spin_lock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001210
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001211 skb_queue_head_init(&rxq);
1212 skb_queue_head_init(&errq);
1213 skb_queue_head_init(&tmpq);
1214
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001215 rp = queue->rx.sring->rsp_prod;
Juergen Grossa884daa2021-08-24 12:28:09 +02001216 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1217 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1218 rp - queue->rx.rsp_cons);
1219 queue->info->broken = true;
1220 spin_unlock(&queue->rx_lock);
1221 return 0;
1222 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001223 rmb(); /* Ensure we see queued responses up to 'rp'. */
1224
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001225 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001226 work_done = 0;
1227 while ((i != rp) && (work_done < budget)) {
Juergen Gross84460662021-08-24 12:28:06 +02001228 RING_COPY_RESPONSE(&queue->rx, i, rx);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001229 memset(extras, 0, sizeof(rinfo.extras));
1230
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001231 err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1232 &need_xdp_flush);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001233
1234 if (unlikely(err)) {
1235err:
1236 while ((skb = __skb_dequeue(&tmpq)))
1237 __skb_queue_tail(&errq, skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001238 dev->stats.rx_errors++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001239 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001240 continue;
1241 }
1242
1243 skb = __skb_dequeue(&tmpq);
1244
1245 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1246 struct xen_netif_extra_info *gso;
1247 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1248
1249 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1250 __skb_queue_head(&tmpq, skb);
Juergen Grossb27d4792021-12-16 08:24:08 +01001251 xennet_set_rx_rsp_cons(queue,
1252 queue->rx.rsp_cons +
1253 skb_queue_len(&tmpq));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001254 goto err;
1255 }
1256 }
1257
Ian Campbell36832432012-08-22 00:26:47 +00001258 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1259 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1260 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001261
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001262 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
Ian Campbell36832432012-08-22 00:26:47 +00001263 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1264 skb->data_len = rx->status;
Jan Beulich093b9c72013-07-17 08:09:37 +01001265 skb->len += rx->status;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001266
Dongli Zhanga7611292019-10-01 21:56:41 +08001267 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
Juergen Grossad4f15d2018-09-11 09:04:48 +02001268 goto err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001269
Ian Campbellf942dc22011-03-15 00:06:18 +00001270 if (rx->flags & XEN_NETRXF_csum_blank)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001271 skb->ip_summed = CHECKSUM_PARTIAL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001272 else if (rx->flags & XEN_NETRXF_data_validated)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001273 skb->ip_summed = CHECKSUM_UNNECESSARY;
1274
1275 __skb_queue_tail(&rxq, skb);
1276
Juergen Grossb27d4792021-12-16 08:24:08 +01001277 i = queue->rx.rsp_cons + 1;
1278 xennet_set_rx_rsp_cons(queue, i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001279 work_done++;
1280 }
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001281 if (need_xdp_flush)
1282 xdp_do_flush();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001283
Wang Chen56cfe5d2008-05-22 18:09:06 +08001284 __skb_queue_purge(&errq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001285
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001286 work_done -= handle_incoming_queue(queue, &rxq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001287
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001288 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001289
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001290 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001291 int more_to_do = 0;
1292
Eric Dumazet6ad20162017-01-30 08:22:01 -08001293 napi_complete_done(napi, work_done);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001294
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001295 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
David Vrabel6a6dc082014-12-16 18:59:46 +00001296 if (more_to_do)
1297 napi_schedule(napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001298 }
1299
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001300 spin_unlock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001301
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001302 return work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001303}
1304
1305static int xennet_change_mtu(struct net_device *dev, int mtu)
1306{
Jonathan Davies0c368202015-03-31 11:05:15 +01001307 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001308
1309 if (mtu > max)
1310 return -EINVAL;
1311 dev->mtu = mtu;
1312 return 0;
1313}
1314
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001315static void xennet_get_stats64(struct net_device *dev,
1316 struct rtnl_link_stats64 *tot)
stephen hemmingere00f85b2011-06-21 05:35:31 +00001317{
1318 struct netfront_info *np = netdev_priv(dev);
1319 int cpu;
1320
1321 for_each_possible_cpu(cpu) {
David Vrabel900e1832015-01-13 16:42:42 +00001322 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1323 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
stephen hemmingere00f85b2011-06-21 05:35:31 +00001324 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1325 unsigned int start;
1326
1327 do {
David Vrabel900e1832015-01-13 16:42:42 +00001328 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1329 tx_packets = tx_stats->packets;
1330 tx_bytes = tx_stats->bytes;
1331 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001332
David Vrabel900e1832015-01-13 16:42:42 +00001333 do {
1334 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1335 rx_packets = rx_stats->packets;
1336 rx_bytes = rx_stats->bytes;
1337 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001338
1339 tot->rx_packets += rx_packets;
1340 tot->tx_packets += tx_packets;
1341 tot->rx_bytes += rx_bytes;
1342 tot->tx_bytes += tx_bytes;
1343 }
1344
1345 tot->rx_errors = dev->stats.rx_errors;
1346 tot->tx_dropped = dev->stats.tx_dropped;
stephen hemmingere00f85b2011-06-21 05:35:31 +00001347}
1348
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001349static void xennet_release_tx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001350{
1351 struct sk_buff *skb;
1352 int i;
1353
1354 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1355 /* Skip over entries which are actually freelist references */
Juergen Gross21631d22021-08-24 12:28:08 +02001356 if (!queue->tx_skbs[i])
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001357 continue;
1358
Juergen Gross21631d22021-08-24 12:28:08 +02001359 skb = queue->tx_skbs[i];
1360 queue->tx_skbs[i] = NULL;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001361 get_page(queue->grant_tx_page[i]);
1362 gnttab_end_foreign_access(queue->grant_tx_ref[i],
Annie Licefe0072014-01-28 11:35:42 +08001363 GNTMAP_readonly,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001364 (unsigned long)page_address(queue->grant_tx_page[i]));
1365 queue->grant_tx_page[i] = NULL;
1366 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
Juergen Gross21631d22021-08-24 12:28:08 +02001367 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001368 dev_kfree_skb_irq(skb);
1369 }
1370}
1371
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001372static void xennet_release_rx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001373{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001374 int id, ref;
1375
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001376 spin_lock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001377
1378 for (id = 0; id < NET_RX_RING_SIZE; id++) {
Annie Licefe0072014-01-28 11:35:42 +08001379 struct sk_buff *skb;
1380 struct page *page;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001381
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001382 skb = queue->rx_skbs[id];
Annie Licefe0072014-01-28 11:35:42 +08001383 if (!skb)
1384 continue;
1385
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001386 ref = queue->grant_rx_ref[id];
Annie Licefe0072014-01-28 11:35:42 +08001387 if (ref == GRANT_INVALID_REF)
1388 continue;
1389
1390 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1391
1392 /* gnttab_end_foreign_access() needs a page ref until
1393 * foreign access is ended (which may be deferred).
1394 */
1395 get_page(page);
1396 gnttab_end_foreign_access(ref, 0,
1397 (unsigned long)page_address(page));
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001398 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001399
Annie Licefe0072014-01-28 11:35:42 +08001400 kfree_skb(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001401 }
1402
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001403 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001404}
1405
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001406static netdev_features_t xennet_fix_features(struct net_device *dev,
1407 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001408{
1409 struct netfront_info *np = netdev_priv(dev);
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001410
Juergen Gross2890ea52016-10-31 14:58:41 +01001411 if (features & NETIF_F_SG &&
1412 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1413 features &= ~NETIF_F_SG;
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001414
Juergen Gross2890ea52016-10-31 14:58:41 +01001415 if (features & NETIF_F_IPV6_CSUM &&
1416 !xenbus_read_unsigned(np->xbdev->otherend,
1417 "feature-ipv6-csum-offload", 0))
1418 features &= ~NETIF_F_IPV6_CSUM;
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001419
Juergen Gross2890ea52016-10-31 14:58:41 +01001420 if (features & NETIF_F_TSO &&
1421 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1422 features &= ~NETIF_F_TSO;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001423
Juergen Gross2890ea52016-10-31 14:58:41 +01001424 if (features & NETIF_F_TSO6 &&
1425 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1426 features &= ~NETIF_F_TSO6;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001427
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001428 return features;
1429}
1430
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001431static int xennet_set_features(struct net_device *dev,
1432 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001433{
1434 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1435 netdev_info(dev, "Reducing MTU because no SG offload");
1436 dev->mtu = ETH_DATA_LEN;
1437 }
1438
1439 return 0;
1440}
1441
Juergen Grossb27d4792021-12-16 08:24:08 +01001442static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001443{
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001444 unsigned long flags;
1445
Juergen Grossb27d4792021-12-16 08:24:08 +01001446 if (unlikely(queue->info->broken))
1447 return false;
Juergen Grossa884daa2021-08-24 12:28:09 +02001448
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001449 spin_lock_irqsave(&queue->tx_lock, flags);
Juergen Grossb27d4792021-12-16 08:24:08 +01001450 if (xennet_tx_buf_gc(queue))
1451 *eoi = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001452 spin_unlock_irqrestore(&queue->tx_lock, flags);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001453
Juergen Grossb27d4792021-12-16 08:24:08 +01001454 return true;
1455}
1456
1457static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1458{
1459 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1460
1461 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1462 xen_irq_lateeoi(irq, eoiflag);
1463
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001464 return IRQ_HANDLED;
1465}
1466
Juergen Grossb27d4792021-12-16 08:24:08 +01001467static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1468{
1469 unsigned int work_queued;
1470 unsigned long flags;
1471
1472 if (unlikely(queue->info->broken))
1473 return false;
1474
1475 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1476 work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1477 if (work_queued > queue->rx_rsp_unconsumed) {
1478 queue->rx_rsp_unconsumed = work_queued;
1479 *eoi = 0;
1480 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1481 const struct device *dev = &queue->info->netdev->dev;
1482
1483 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1484 dev_alert(dev, "RX producer index going backwards\n");
1485 dev_alert(dev, "Disabled for further use\n");
1486 queue->info->broken = true;
1487 return false;
1488 }
1489 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1490
1491 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1492 napi_schedule(&queue->napi);
1493
1494 return true;
1495}
1496
Wei Liud634bf22013-05-22 06:34:46 +00001497static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1498{
Juergen Grossb27d4792021-12-16 08:24:08 +01001499 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
Wei Liud634bf22013-05-22 06:34:46 +00001500
Juergen Grossb27d4792021-12-16 08:24:08 +01001501 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1502 xen_irq_lateeoi(irq, eoiflag);
Wei Liud634bf22013-05-22 06:34:46 +00001503
1504 return IRQ_HANDLED;
1505}
1506
1507static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1508{
Juergen Grossb27d4792021-12-16 08:24:08 +01001509 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1510
1511 if (xennet_handle_tx(dev_id, &eoiflag) &&
1512 xennet_handle_rx(dev_id, &eoiflag))
1513 xen_irq_lateeoi(irq, eoiflag);
1514
Wei Liud634bf22013-05-22 06:34:46 +00001515 return IRQ_HANDLED;
1516}
1517
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001518#ifdef CONFIG_NET_POLL_CONTROLLER
1519static void xennet_poll_controller(struct net_device *dev)
1520{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001521 /* Poll each queue */
1522 struct netfront_info *info = netdev_priv(dev);
1523 unsigned int num_queues = dev->real_num_tx_queues;
1524 unsigned int i;
Juergen Grossa884daa2021-08-24 12:28:09 +02001525
1526 if (info->broken)
1527 return;
1528
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001529 for (i = 0; i < num_queues; ++i)
1530 xennet_interrupt(0, &info->queues[i]);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001531}
1532#endif
1533
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001534#define NETBACK_XDP_HEADROOM_DISABLE 0
1535#define NETBACK_XDP_HEADROOM_ENABLE 1
1536
1537static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1538{
1539 int err;
1540 unsigned short headroom;
1541
1542 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1543 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1544 "xdp-headroom", "%hu",
1545 headroom);
1546 if (err)
1547 pr_warn("Error writing xdp-headroom\n");
1548
1549 return err;
1550}
1551
1552static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1553 struct netlink_ext_ack *extack)
1554{
1555 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1556 struct netfront_info *np = netdev_priv(dev);
1557 struct bpf_prog *old_prog;
1558 unsigned int i, err;
1559
1560 if (dev->mtu > max_mtu) {
1561 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1562 return -EINVAL;
1563 }
1564
1565 if (!np->netback_has_xdp_headroom)
1566 return 0;
1567
1568 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1569
1570 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1571 NETBACK_XDP_HEADROOM_DISABLE);
1572 if (err)
1573 return err;
1574
1575 /* avoid the race with XDP headroom adjustment */
1576 wait_event(module_wq,
1577 xenbus_read_driver_state(np->xbdev->otherend) ==
1578 XenbusStateReconfigured);
1579 np->netfront_xdp_enabled = true;
1580
1581 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1582
1583 if (prog)
1584 bpf_prog_add(prog, dev->real_num_tx_queues);
1585
1586 for (i = 0; i < dev->real_num_tx_queues; ++i)
1587 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1588
1589 if (old_prog)
1590 for (i = 0; i < dev->real_num_tx_queues; ++i)
1591 bpf_prog_put(old_prog);
1592
1593 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1594
1595 return 0;
1596}
1597
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001598static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1599{
Juergen Grossa884daa2021-08-24 12:28:09 +02001600 struct netfront_info *np = netdev_priv(dev);
1601
1602 if (np->broken)
1603 return -ENODEV;
1604
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001605 switch (xdp->command) {
1606 case XDP_SETUP_PROG:
1607 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001608 default:
1609 return -EINVAL;
1610 }
1611}
1612
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001613static const struct net_device_ops xennet_netdev_ops = {
1614 .ndo_open = xennet_open,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001615 .ndo_stop = xennet_close,
1616 .ndo_start_xmit = xennet_start_xmit,
1617 .ndo_change_mtu = xennet_change_mtu,
stephen hemmingere00f85b2011-06-21 05:35:31 +00001618 .ndo_get_stats64 = xennet_get_stats64,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001619 .ndo_set_mac_address = eth_mac_addr,
1620 .ndo_validate_addr = eth_validate_addr,
Michał Mirosławfb507932011-03-31 01:01:35 +00001621 .ndo_fix_features = xennet_fix_features,
1622 .ndo_set_features = xennet_set_features,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001623 .ndo_select_queue = xennet_select_queue,
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001624 .ndo_bpf = xennet_xdp,
1625 .ndo_xdp_xmit = xennet_xdp_xmit,
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001626#ifdef CONFIG_NET_POLL_CONTROLLER
1627 .ndo_poll_controller = xennet_poll_controller,
1628#endif
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001629};
1630
David Vrabel900e1832015-01-13 16:42:42 +00001631static void xennet_free_netdev(struct net_device *netdev)
1632{
1633 struct netfront_info *np = netdev_priv(netdev);
1634
1635 free_percpu(np->rx_stats);
1636 free_percpu(np->tx_stats);
1637 free_netdev(netdev);
1638}
1639
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001640static struct net_device *xennet_create_dev(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001641{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001642 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001643 struct net_device *netdev;
1644 struct netfront_info *np;
1645
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001646 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
Joe Perches41de8d42012-01-29 13:47:52 +00001647 if (!netdev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001648 return ERR_PTR(-ENOMEM);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001649
1650 np = netdev_priv(netdev);
1651 np->xbdev = dev;
1652
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001653 np->queues = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001654
stephen hemmingere00f85b2011-06-21 05:35:31 +00001655 err = -ENOMEM;
David Vrabel900e1832015-01-13 16:42:42 +00001656 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1657 if (np->rx_stats == NULL)
1658 goto exit;
1659 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1660 if (np->tx_stats == NULL)
stephen hemmingere00f85b2011-06-21 05:35:31 +00001661 goto exit;
1662
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001663 netdev->netdev_ops = &xennet_netdev_ops;
1664
Michał Mirosławfb507932011-03-31 01:01:35 +00001665 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1666 NETIF_F_GSO_ROBUST;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001667 netdev->hw_features = NETIF_F_SG |
1668 NETIF_F_IPV6_CSUM |
1669 NETIF_F_TSO | NETIF_F_TSO6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001670
Ian Campbellfc3e5942011-04-04 11:07:57 -07001671 /*
1672 * Assume that all hw features are available for now. This set
1673 * will be adjusted by the call to netdev_update_features() in
1674 * xennet_connect() which is the earliest point where we can
1675 * negotiate with the backend regarding supported features.
1676 */
1677 netdev->features |= netdev->hw_features;
1678
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001679 netdev->ethtool_ops = &xennet_ethtool_ops;
Mohammed Gamale1043a42017-10-16 15:20:32 +02001680 netdev->min_mtu = ETH_MIN_MTU;
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001681 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001682 SET_NETDEV_DEV(netdev, &dev->dev);
1683
1684 np->netdev = netdev;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001685 np->netfront_xdp_enabled = false;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001686
1687 netif_carrier_off(netdev);
1688
Andrea Righic2c63312020-07-24 10:59:10 +02001689 do {
1690 xenbus_switch_state(dev, XenbusStateInitialising);
1691 err = wait_event_timeout(module_wq,
1692 xenbus_read_driver_state(dev->otherend) !=
1693 XenbusStateClosed &&
1694 xenbus_read_driver_state(dev->otherend) !=
1695 XenbusStateUnknown, XENNET_TIMEOUT);
1696 } while (!err);
1697
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001698 return netdev;
1699
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001700 exit:
David Vrabel900e1832015-01-13 16:42:42 +00001701 xennet_free_netdev(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001702 return ERR_PTR(err);
1703}
1704
Lee Jones80708602021-01-15 20:09:03 +00001705/*
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001706 * Entry point to this code when a new device is created. Allocate the basic
1707 * structures and the ring buffers for communication with the backend, and
1708 * inform the backend of the appropriate details for those.
1709 */
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001710static int netfront_probe(struct xenbus_device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001711 const struct xenbus_device_id *id)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001712{
1713 int err;
1714 struct net_device *netdev;
1715 struct netfront_info *info;
1716
1717 netdev = xennet_create_dev(dev);
1718 if (IS_ERR(netdev)) {
1719 err = PTR_ERR(netdev);
1720 xenbus_dev_fatal(dev, err, "creating netdev");
1721 return err;
1722 }
1723
1724 info = netdev_priv(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001725 dev_set_drvdata(&dev->dev, info);
Takashi Iwai27b917e2015-02-04 14:38:55 +01001726#ifdef CONFIG_SYSFS
1727 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1728#endif
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001729
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001730 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001731}
1732
1733static void xennet_end_access(int ref, void *page)
1734{
1735 /* This frees the page as a side-effect */
1736 if (ref != GRANT_INVALID_REF)
1737 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1738}
1739
1740static void xennet_disconnect_backend(struct netfront_info *info)
1741{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001742 unsigned int i = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001743 unsigned int num_queues = info->netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001744
David Vrabelf9feb1e2014-07-02 16:09:15 +01001745 netif_carrier_off(info->netdev);
1746
Chas Williams9a873c72015-08-19 19:14:20 -04001747 for (i = 0; i < num_queues && info->queues; ++i) {
David Vrabel76541862014-06-18 10:47:27 +01001748 struct netfront_queue *queue = &info->queues[i];
1749
Boris Ostrovsky74470952017-01-30 12:45:46 -05001750 del_timer_sync(&queue->rx_refill_timer);
1751
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001752 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1753 unbind_from_irqhandler(queue->tx_irq, queue);
1754 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1755 unbind_from_irqhandler(queue->tx_irq, queue);
1756 unbind_from_irqhandler(queue->rx_irq, queue);
1757 }
1758 queue->tx_evtchn = queue->rx_evtchn = 0;
1759 queue->tx_irq = queue->rx_irq = 0;
1760
Chas Williams274b0452015-08-27 12:28:46 -04001761 if (netif_running(info->netdev))
1762 napi_synchronize(&queue->napi);
David Vrabelf9feb1e2014-07-02 16:09:15 +01001763
David Vrabela5b5dc32014-07-31 17:38:23 +01001764 xennet_release_tx_bufs(queue);
1765 xennet_release_rx_bufs(queue);
1766 gnttab_free_grant_references(queue->gref_tx_head);
1767 gnttab_free_grant_references(queue->gref_rx_head);
1768
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001769 /* End access and free the pages */
1770 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1771 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1772
1773 queue->tx_ring_ref = GRANT_INVALID_REF;
1774 queue->rx_ring_ref = GRANT_INVALID_REF;
1775 queue->tx.sring = NULL;
1776 queue->rx.sring = NULL;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001777
1778 page_pool_destroy(queue->page_pool);
Wei Liud634bf22013-05-22 06:34:46 +00001779 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001780}
1781
Lee Jones80708602021-01-15 20:09:03 +00001782/*
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001783 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1784 * driver restart. We tear down our netif structure and recreate it, but
1785 * leave the device-layer structures intact so that this is transparent to the
1786 * rest of the kernel.
1787 */
1788static int netfront_resume(struct xenbus_device *dev)
1789{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001790 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001791
1792 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1793
Dongli Zhang042b2042021-10-22 16:31:39 -07001794 netif_tx_lock_bh(info->netdev);
1795 netif_device_detach(info->netdev);
1796 netif_tx_unlock_bh(info->netdev);
1797
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001798 xennet_disconnect_backend(info);
1799 return 0;
1800}
1801
1802static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1803{
1804 char *s, *e, *macstr;
1805 int i;
1806
1807 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1808 if (IS_ERR(macstr))
1809 return PTR_ERR(macstr);
1810
1811 for (i = 0; i < ETH_ALEN; i++) {
1812 mac[i] = simple_strtoul(s, &e, 16);
1813 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1814 kfree(macstr);
1815 return -ENOENT;
1816 }
1817 s = e+1;
1818 }
1819
1820 kfree(macstr);
1821 return 0;
1822}
1823
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001824static int setup_netfront_single(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001825{
1826 int err;
1827
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001828 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001829 if (err < 0)
1830 goto fail;
1831
Juergen Grossb27d4792021-12-16 08:24:08 +01001832 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1833 xennet_interrupt, 0,
1834 queue->info->netdev->name,
1835 queue);
Wei Liud634bf22013-05-22 06:34:46 +00001836 if (err < 0)
1837 goto bind_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001838 queue->rx_evtchn = queue->tx_evtchn;
1839 queue->rx_irq = queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001840
1841 return 0;
1842
1843bind_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001844 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1845 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001846fail:
1847 return err;
1848}
1849
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001850static int setup_netfront_split(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001851{
1852 int err;
1853
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001854 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001855 if (err < 0)
1856 goto fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001857 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001858 if (err < 0)
1859 goto alloc_rx_evtchn_fail;
1860
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001861 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1862 "%s-tx", queue->name);
Juergen Grossb27d4792021-12-16 08:24:08 +01001863 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1864 xennet_tx_interrupt, 0,
1865 queue->tx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001866 if (err < 0)
1867 goto bind_tx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001868 queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001869
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001870 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1871 "%s-rx", queue->name);
Juergen Grossb27d4792021-12-16 08:24:08 +01001872 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1873 xennet_rx_interrupt, 0,
1874 queue->rx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001875 if (err < 0)
1876 goto bind_rx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001877 queue->rx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001878
1879 return 0;
1880
1881bind_rx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001882 unbind_from_irqhandler(queue->tx_irq, queue);
1883 queue->tx_irq = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001884bind_tx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001885 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1886 queue->rx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001887alloc_rx_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001888 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1889 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001890fail:
1891 return err;
1892}
1893
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001894static int setup_netfront(struct xenbus_device *dev,
1895 struct netfront_queue *queue, unsigned int feature_split_evtchn)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001896{
1897 struct xen_netif_tx_sring *txs;
1898 struct xen_netif_rx_sring *rxs;
Wei Liuccc9d902015-04-03 14:44:59 +08001899 grant_ref_t gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001900 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001901
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001902 queue->tx_ring_ref = GRANT_INVALID_REF;
1903 queue->rx_ring_ref = GRANT_INVALID_REF;
1904 queue->rx.sring = NULL;
1905 queue->tx.sring = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001906
Ian Campbella144ff02008-06-17 10:47:08 +02001907 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001908 if (!txs) {
1909 err = -ENOMEM;
1910 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1911 goto fail;
1912 }
1913 SHARED_RING_INIT(txs);
Julien Grall30c5d7f2015-04-10 14:42:21 +01001914 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001915
Wei Liuccc9d902015-04-03 14:44:59 +08001916 err = xenbus_grant_ring(dev, txs, 1, &gref);
Wei Liu1ca29832013-05-20 01:05:12 +00001917 if (err < 0)
1918 goto grant_tx_ring_fail;
Wei Liuccc9d902015-04-03 14:44:59 +08001919 queue->tx_ring_ref = gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001920
Ian Campbella144ff02008-06-17 10:47:08 +02001921 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001922 if (!rxs) {
1923 err = -ENOMEM;
1924 xenbus_dev_fatal(dev, err, "allocating rx ring page");
Wei Liu1ca29832013-05-20 01:05:12 +00001925 goto alloc_rx_ring_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001926 }
1927 SHARED_RING_INIT(rxs);
Julien Grall30c5d7f2015-04-10 14:42:21 +01001928 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001929
Wei Liuccc9d902015-04-03 14:44:59 +08001930 err = xenbus_grant_ring(dev, rxs, 1, &gref);
Wei Liu1ca29832013-05-20 01:05:12 +00001931 if (err < 0)
1932 goto grant_rx_ring_fail;
Wei Liuccc9d902015-04-03 14:44:59 +08001933 queue->rx_ring_ref = gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001934
Wei Liud634bf22013-05-22 06:34:46 +00001935 if (feature_split_evtchn)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001936 err = setup_netfront_split(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001937 /* setup single event channel if
1938 * a) feature-split-event-channels == 0
1939 * b) feature-split-event-channels == 1 but failed to setup
1940 */
Jiapeng Chonge93fac32021-02-02 18:17:49 +08001941 if (!feature_split_evtchn || err)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001942 err = setup_netfront_single(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001943
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001944 if (err)
Wei Liu1ca29832013-05-20 01:05:12 +00001945 goto alloc_evtchn_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001946
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001947 return 0;
1948
Wei Liu1ca29832013-05-20 01:05:12 +00001949 /* If we fail to setup netfront, it is safe to just revoke access to
1950 * granted pages because backend is not accessing it at this point.
1951 */
Wei Liu1ca29832013-05-20 01:05:12 +00001952alloc_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001953 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001954grant_rx_ring_fail:
1955 free_page((unsigned long)rxs);
1956alloc_rx_ring_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001957 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001958grant_tx_ring_fail:
1959 free_page((unsigned long)txs);
1960fail:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001961 return err;
1962}
1963
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001964/* Queue-specific initialisation
1965 * This used to be done in xennet_create_dev() but must now
1966 * be run per-queue.
1967 */
1968static int xennet_init_queue(struct netfront_queue *queue)
1969{
1970 unsigned short i;
1971 int err = 0;
Xiao Liang21f27062018-08-14 23:21:28 +08001972 char *devid;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001973
1974 spin_lock_init(&queue->tx_lock);
1975 spin_lock_init(&queue->rx_lock);
Juergen Grossb27d4792021-12-16 08:24:08 +01001976 spin_lock_init(&queue->rx_cons_lock);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001977
Kees Cooke99e88a2017-10-16 14:43:17 -07001978 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001979
Xiao Liang21f27062018-08-14 23:21:28 +08001980 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1981 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1982 devid, queue->id);
Wei Liu8b715012014-06-04 10:30:47 +01001983
Juergen Gross21631d22021-08-24 12:28:08 +02001984 /* Initialise tx_skb_freelist as a free chain containing every entry. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001985 queue->tx_skb_freelist = 0;
Juergen Grossa884daa2021-08-24 12:28:09 +02001986 queue->tx_pend_queue = TX_LINK_NONE;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001987 for (i = 0; i < NET_TX_RING_SIZE; i++) {
Juergen Gross21631d22021-08-24 12:28:08 +02001988 queue->tx_link[i] = i + 1;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001989 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1990 queue->grant_tx_page[i] = NULL;
1991 }
Juergen Gross21631d22021-08-24 12:28:08 +02001992 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001993
1994 /* Clear out rx_skbs */
1995 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1996 queue->rx_skbs[i] = NULL;
1997 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1998 }
1999
2000 /* A grant for every tx ring slot */
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002001 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002002 &queue->gref_tx_head) < 0) {
2003 pr_alert("can't alloc tx grant refs\n");
2004 err = -ENOMEM;
2005 goto exit;
2006 }
2007
2008 /* A grant for every rx ring slot */
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002009 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002010 &queue->gref_rx_head) < 0) {
2011 pr_alert("can't alloc rx grant refs\n");
2012 err = -ENOMEM;
2013 goto exit_free_tx;
2014 }
2015
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002016 return 0;
2017
2018 exit_free_tx:
2019 gnttab_free_grant_references(queue->gref_tx_head);
2020 exit:
2021 return err;
2022}
2023
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002024static int write_queue_xenstore_keys(struct netfront_queue *queue,
2025 struct xenbus_transaction *xbt, int write_hierarchical)
2026{
2027 /* Write the queue-specific keys into XenStore in the traditional
2028 * way for a single queue, or in a queue subkeys for multiple
2029 * queues.
2030 */
2031 struct xenbus_device *dev = queue->info->xbdev;
2032 int err;
2033 const char *message;
2034 char *path;
2035 size_t pathsize;
2036
2037 /* Choose the correct place to write the keys */
2038 if (write_hierarchical) {
2039 pathsize = strlen(dev->nodename) + 10;
2040 path = kzalloc(pathsize, GFP_KERNEL);
2041 if (!path) {
2042 err = -ENOMEM;
2043 message = "out of memory while writing ring references";
2044 goto error;
2045 }
2046 snprintf(path, pathsize, "%s/queue-%u",
2047 dev->nodename, queue->id);
2048 } else {
2049 path = (char *)dev->nodename;
2050 }
2051
2052 /* Write ring references */
2053 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
2054 queue->tx_ring_ref);
2055 if (err) {
2056 message = "writing tx-ring-ref";
2057 goto error;
2058 }
2059
2060 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
2061 queue->rx_ring_ref);
2062 if (err) {
2063 message = "writing rx-ring-ref";
2064 goto error;
2065 }
2066
2067 /* Write event channels; taking into account both shared
2068 * and split event channel scenarios.
2069 */
2070 if (queue->tx_evtchn == queue->rx_evtchn) {
2071 /* Shared event channel */
2072 err = xenbus_printf(*xbt, path,
2073 "event-channel", "%u", queue->tx_evtchn);
2074 if (err) {
2075 message = "writing event-channel";
2076 goto error;
2077 }
2078 } else {
2079 /* Split event channels */
2080 err = xenbus_printf(*xbt, path,
2081 "event-channel-tx", "%u", queue->tx_evtchn);
2082 if (err) {
2083 message = "writing event-channel-tx";
2084 goto error;
2085 }
2086
2087 err = xenbus_printf(*xbt, path,
2088 "event-channel-rx", "%u", queue->rx_evtchn);
2089 if (err) {
2090 message = "writing event-channel-rx";
2091 goto error;
2092 }
2093 }
2094
2095 if (write_hierarchical)
2096 kfree(path);
2097 return 0;
2098
2099error:
2100 if (write_hierarchical)
2101 kfree(path);
2102 xenbus_dev_fatal(dev, err, "%s", message);
2103 return err;
2104}
2105
David Vrabelce587252014-06-18 10:47:28 +01002106static void xennet_destroy_queues(struct netfront_info *info)
2107{
2108 unsigned int i;
2109
David Vrabelce587252014-06-18 10:47:28 +01002110 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
2111 struct netfront_queue *queue = &info->queues[i];
2112
2113 if (netif_running(info->netdev))
2114 napi_disable(&queue->napi);
2115 netif_napi_del(&queue->napi);
2116 }
2117
David Vrabelce587252014-06-18 10:47:28 +01002118 kfree(info->queues);
2119 info->queues = NULL;
2120}
2121
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002122
2123
2124static int xennet_create_page_pool(struct netfront_queue *queue)
2125{
2126 int err;
2127 struct page_pool_params pp_params = {
2128 .order = 0,
2129 .flags = 0,
2130 .pool_size = NET_RX_RING_SIZE,
2131 .nid = NUMA_NO_NODE,
2132 .dev = &queue->info->netdev->dev,
2133 .offset = XDP_PACKET_HEADROOM,
2134 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2135 };
2136
2137 queue->page_pool = page_pool_create(&pp_params);
2138 if (IS_ERR(queue->page_pool)) {
2139 err = PTR_ERR(queue->page_pool);
2140 queue->page_pool = NULL;
2141 return err;
2142 }
2143
2144 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
Björn Töpelb02e5a02020-11-30 19:52:01 +01002145 queue->id, 0);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002146 if (err) {
2147 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2148 goto err_free_pp;
2149 }
2150
2151 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2152 MEM_TYPE_PAGE_POOL, queue->page_pool);
2153 if (err) {
2154 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2155 goto err_unregister_rxq;
2156 }
2157 return 0;
2158
2159err_unregister_rxq:
2160 xdp_rxq_info_unreg(&queue->xdp_rxq);
2161err_free_pp:
2162 page_pool_destroy(queue->page_pool);
2163 queue->page_pool = NULL;
2164 return err;
2165}
2166
David Vrabelce587252014-06-18 10:47:28 +01002167static int xennet_create_queues(struct netfront_info *info,
Joe Jinca88ea12015-10-19 13:37:17 +08002168 unsigned int *num_queues)
David Vrabelce587252014-06-18 10:47:28 +01002169{
2170 unsigned int i;
2171 int ret;
2172
Joe Jinca88ea12015-10-19 13:37:17 +08002173 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
David Vrabelce587252014-06-18 10:47:28 +01002174 GFP_KERNEL);
2175 if (!info->queues)
2176 return -ENOMEM;
2177
Joe Jinca88ea12015-10-19 13:37:17 +08002178 for (i = 0; i < *num_queues; i++) {
David Vrabelce587252014-06-18 10:47:28 +01002179 struct netfront_queue *queue = &info->queues[i];
2180
2181 queue->id = i;
2182 queue->info = info;
2183
2184 ret = xennet_init_queue(queue);
2185 if (ret < 0) {
Ross Lagerwallf599c642018-01-11 09:36:38 +00002186 dev_warn(&info->xbdev->dev,
David Vrabel69cb8522014-07-31 17:38:24 +01002187 "only created %d queues\n", i);
Joe Jinca88ea12015-10-19 13:37:17 +08002188 *num_queues = i;
David Vrabelce587252014-06-18 10:47:28 +01002189 break;
2190 }
2191
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002192 /* use page pool recycling instead of buddy allocator */
2193 ret = xennet_create_page_pool(queue);
2194 if (ret < 0) {
2195 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2196 *num_queues = i;
2197 return ret;
2198 }
2199
David Vrabelce587252014-06-18 10:47:28 +01002200 netif_napi_add(queue->info->netdev, &queue->napi,
2201 xennet_poll, 64);
2202 if (netif_running(info->netdev))
2203 napi_enable(&queue->napi);
2204 }
2205
Joe Jinca88ea12015-10-19 13:37:17 +08002206 netif_set_real_num_tx_queues(info->netdev, *num_queues);
David Vrabelce587252014-06-18 10:47:28 +01002207
Joe Jinca88ea12015-10-19 13:37:17 +08002208 if (*num_queues == 0) {
Ross Lagerwallf599c642018-01-11 09:36:38 +00002209 dev_err(&info->xbdev->dev, "no queues\n");
David Vrabelce587252014-06-18 10:47:28 +01002210 return -EINVAL;
2211 }
2212 return 0;
2213}
2214
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002215/* Common code used when first setting up, and when resuming. */
Ian Campbellf502bf22010-08-18 23:27:49 +00002216static int talk_to_netback(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002217 struct netfront_info *info)
2218{
2219 const char *message;
2220 struct xenbus_transaction xbt;
2221 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002222 unsigned int feature_split_evtchn;
2223 unsigned int i = 0;
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002224 unsigned int max_queues = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002225 struct netfront_queue *queue = NULL;
2226 unsigned int num_queues = 1;
Jakub Kicinski93772112021-10-21 06:12:03 -07002227 u8 addr[ETH_ALEN];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002228
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002229 info->netdev->irq = 0;
2230
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002231 /* Check if backend supports multiple queues */
Juergen Gross2890ea52016-10-31 14:58:41 +01002232 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2233 "multi-queue-max-queues", 1);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002234 num_queues = min(max_queues, xennet_max_queues);
2235
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002236 /* Check feature-split-event-channels */
Juergen Gross2890ea52016-10-31 14:58:41 +01002237 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2238 "feature-split-event-channels", 0);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002239
2240 /* Read mac addr. */
Jakub Kicinski93772112021-10-21 06:12:03 -07002241 err = xen_net_read_mac(dev, addr);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002242 if (err) {
2243 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
Ross Lagerwallcb257782018-06-21 14:00:20 +01002244 goto out_unlocked;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002245 }
Jakub Kicinski93772112021-10-21 06:12:03 -07002246 eth_hw_addr_set(info->netdev, addr);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002247
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002248 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2249 "feature-xdp-headroom", 0);
2250 if (info->netback_has_xdp_headroom) {
2251 /* set the current xen-netfront xdp state */
2252 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2253 NETBACK_XDP_HEADROOM_ENABLE :
2254 NETBACK_XDP_HEADROOM_DISABLE);
2255 if (err)
2256 goto out_unlocked;
2257 }
2258
Ross Lagerwallf599c642018-01-11 09:36:38 +00002259 rtnl_lock();
David Vrabelce587252014-06-18 10:47:28 +01002260 if (info->queues)
2261 xennet_destroy_queues(info);
2262
Juergen Grossa884daa2021-08-24 12:28:09 +02002263 /* For the case of a reconnect reset the "broken" indicator. */
2264 info->broken = false;
2265
Joe Jinca88ea12015-10-19 13:37:17 +08002266 err = xennet_create_queues(info, &num_queues);
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00002267 if (err < 0) {
2268 xenbus_dev_fatal(dev, err, "creating queues");
2269 kfree(info->queues);
2270 info->queues = NULL;
2271 goto out;
2272 }
Ross Lagerwallf599c642018-01-11 09:36:38 +00002273 rtnl_unlock();
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002274
2275 /* Create shared ring, alloc event channel -- for each queue */
2276 for (i = 0; i < num_queues; ++i) {
2277 queue = &info->queues[i];
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002278 err = setup_netfront(dev, queue, feature_split_evtchn);
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00002279 if (err)
2280 goto destroy_ring;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002281 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002282
2283again:
2284 err = xenbus_transaction_start(&xbt);
2285 if (err) {
2286 xenbus_dev_fatal(dev, err, "starting transaction");
2287 goto destroy_ring;
2288 }
2289
chas williams812494d2015-09-16 16:28:25 -04002290 if (xenbus_exists(XBT_NIL,
2291 info->xbdev->otherend, "multi-queue-max-queues")) {
2292 /* Write the number of queues */
2293 err = xenbus_printf(xbt, dev->nodename,
2294 "multi-queue-num-queues", "%u", num_queues);
2295 if (err) {
2296 message = "writing multi-queue-num-queues";
2297 goto abort_transaction_no_dev_fatal;
2298 }
2299 }
2300
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002301 if (num_queues == 1) {
2302 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2303 if (err)
2304 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00002305 } else {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002306 /* Write the keys for each queue */
2307 for (i = 0; i < num_queues; ++i) {
2308 queue = &info->queues[i];
2309 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2310 if (err)
2311 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00002312 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002313 }
2314
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002315 /* The remaining keys are not queue-specific */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002316 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2317 1);
2318 if (err) {
2319 message = "writing request-rx-copy";
2320 goto abort_transaction;
2321 }
2322
2323 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2324 if (err) {
2325 message = "writing feature-rx-notify";
2326 goto abort_transaction;
2327 }
2328
2329 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2330 if (err) {
2331 message = "writing feature-sg";
2332 goto abort_transaction;
2333 }
2334
2335 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2336 if (err) {
2337 message = "writing feature-gso-tcpv4";
2338 goto abort_transaction;
2339 }
2340
Paul Durrant2c0057d2014-01-15 17:30:33 +00002341 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2342 if (err) {
2343 message = "writing feature-gso-tcpv6";
2344 goto abort_transaction;
2345 }
2346
2347 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2348 "1");
2349 if (err) {
2350 message = "writing feature-ipv6-csum-offload";
2351 goto abort_transaction;
2352 }
2353
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002354 err = xenbus_transaction_end(xbt, 0);
2355 if (err) {
2356 if (err == -EAGAIN)
2357 goto again;
2358 xenbus_dev_fatal(dev, err, "completing transaction");
2359 goto destroy_ring;
2360 }
2361
2362 return 0;
2363
2364 abort_transaction:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002365 xenbus_dev_fatal(dev, err, "%s", message);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002366abort_transaction_no_dev_fatal:
2367 xenbus_transaction_end(xbt, 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002368 destroy_ring:
2369 xennet_disconnect_backend(info);
Ross Lagerwallf599c642018-01-11 09:36:38 +00002370 rtnl_lock();
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00002371 xennet_destroy_queues(info);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002372 out:
Ross Lagerwallf599c642018-01-11 09:36:38 +00002373 rtnl_unlock();
Ross Lagerwallcb257782018-06-21 14:00:20 +01002374out_unlocked:
Vitaly Kuznetsovd86b5672017-05-11 13:58:06 +02002375 device_unregister(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002376 return err;
2377}
2378
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002379static int xennet_connect(struct net_device *dev)
2380{
2381 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002382 unsigned int num_queues = 0;
David Vrabela5b5dc32014-07-31 17:38:23 +01002383 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002384 unsigned int j = 0;
2385 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002386
Juergen Gross2890ea52016-10-31 14:58:41 +01002387 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002388 dev_info(&dev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07002389 "backend does not support copying receive path\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002390 return -ENODEV;
2391 }
2392
Ian Campbellf502bf22010-08-18 23:27:49 +00002393 err = talk_to_netback(np->xbdev, np);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002394 if (err)
2395 return err;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002396 if (np->netback_has_xdp_headroom)
2397 pr_info("backend supports XDP headroom\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002398
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002399 /* talk_to_netback() sets the correct number of queues */
2400 num_queues = dev->real_num_tx_queues;
2401
Ross Lagerwallf599c642018-01-11 09:36:38 +00002402 if (dev->reg_state == NETREG_UNINITIALIZED) {
2403 err = register_netdev(dev);
2404 if (err) {
2405 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2406 device_unregister(&np->xbdev->dev);
2407 return err;
2408 }
2409 }
2410
Ross Lagerwall45c81842018-06-21 14:00:21 +01002411 rtnl_lock();
2412 netdev_update_features(dev);
2413 rtnl_unlock();
2414
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002415 /*
David Vrabela5b5dc32014-07-31 17:38:23 +01002416 * All public and private state should now be sane. Get
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002417 * ready to start sending and receiving packets and give the driver
2418 * domain a kick because we've probably just requeued some
2419 * packets.
2420 */
Dongli Zhang042b2042021-10-22 16:31:39 -07002421 netif_tx_lock_bh(np->netdev);
2422 netif_device_attach(np->netdev);
2423 netif_tx_unlock_bh(np->netdev);
2424
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002425 netif_carrier_on(np->netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002426 for (j = 0; j < num_queues; ++j) {
2427 queue = &np->queues[j];
David Vrabelf50b4072014-07-02 16:09:14 +01002428
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002429 notify_remote_via_irq(queue->tx_irq);
2430 if (queue->tx_irq != queue->rx_irq)
2431 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002432
David Vrabelf50b4072014-07-02 16:09:14 +01002433 spin_lock_irq(&queue->tx_lock);
2434 xennet_tx_buf_gc(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002435 spin_unlock_irq(&queue->tx_lock);
David Vrabelf50b4072014-07-02 16:09:14 +01002436
2437 spin_lock_bh(&queue->rx_lock);
2438 xennet_alloc_rx_buffers(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002439 spin_unlock_bh(&queue->rx_lock);
2440 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002441
2442 return 0;
2443}
2444
Lee Jones80708602021-01-15 20:09:03 +00002445/*
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002446 * Callback received when the backend's state changes.
2447 */
Ian Campbellf502bf22010-08-18 23:27:49 +00002448static void netback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002449 enum xenbus_state backend_state)
2450{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002451 struct netfront_info *np = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002452 struct net_device *netdev = np->netdev;
2453
2454 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2455
Juergen Gross8edfe2e2018-09-07 14:21:30 +02002456 wake_up_all(&module_wq);
2457
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002458 switch (backend_state) {
2459 case XenbusStateInitialising:
2460 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002461 case XenbusStateReconfiguring:
2462 case XenbusStateReconfigured:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002463 case XenbusStateUnknown:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002464 break;
2465
2466 case XenbusStateInitWait:
2467 if (dev->state != XenbusStateInitialising)
2468 break;
2469 if (xennet_connect(netdev) != 0)
2470 break;
2471 xenbus_switch_state(dev, XenbusStateConnected);
Laszlo Ersek08e34eb2011-12-11 01:48:59 +00002472 break;
2473
2474 case XenbusStateConnected:
Amerigo Wangee89bab2012-08-09 22:14:56 +00002475 netdev_notify_peers(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002476 break;
2477
David Vrabelbce3ea82014-02-04 18:50:26 +00002478 case XenbusStateClosed:
2479 if (dev->state == XenbusStateClosed)
2480 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002481 fallthrough; /* Missed the backend's CLOSING state */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002482 case XenbusStateClosing:
2483 xenbus_frontend_closed(dev);
2484 break;
2485 }
2486}
2487
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002488static const struct xennet_stat {
2489 char name[ETH_GSTRING_LEN];
2490 u16 offset;
2491} xennet_stats[] = {
2492 {
2493 "rx_gso_checksum_fixup",
2494 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2495 },
2496};
2497
2498static int xennet_get_sset_count(struct net_device *dev, int string_set)
2499{
2500 switch (string_set) {
2501 case ETH_SS_STATS:
2502 return ARRAY_SIZE(xennet_stats);
2503 default:
2504 return -EINVAL;
2505 }
2506}
2507
2508static void xennet_get_ethtool_stats(struct net_device *dev,
2509 struct ethtool_stats *stats, u64 * data)
2510{
2511 void *np = netdev_priv(dev);
2512 int i;
2513
2514 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002515 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002516}
2517
2518static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2519{
2520 int i;
2521
2522 switch (stringset) {
2523 case ETH_SS_STATS:
2524 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2525 memcpy(data + i * ETH_GSTRING_LEN,
2526 xennet_stats[i].name, ETH_GSTRING_LEN);
2527 break;
2528 }
2529}
2530
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002531static const struct ethtool_ops xennet_ethtool_ops =
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002532{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002533 .get_link = ethtool_op_get_link,
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002534
2535 .get_sset_count = xennet_get_sset_count,
2536 .get_ethtool_stats = xennet_get_ethtool_stats,
2537 .get_strings = xennet_get_strings,
Daniel Drown91ffb9d2020-07-03 01:22:34 -05002538 .get_ts_info = ethtool_op_get_ts_info,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002539};
2540
2541#ifdef CONFIG_SYSFS
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002542static ssize_t show_rxbuf(struct device *dev,
2543 struct device_attribute *attr, char *buf)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002544{
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002545 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002546}
2547
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002548static ssize_t store_rxbuf(struct device *dev,
2549 struct device_attribute *attr,
2550 const char *buf, size_t len)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002551{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002552 char *endp;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002553
2554 if (!capable(CAP_NET_ADMIN))
2555 return -EPERM;
2556
Andrew Lunn8ed7ec12020-10-31 19:04:35 +01002557 simple_strtoul(buf, &endp, 0);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002558 if (endp == buf)
2559 return -EBADMSG;
2560
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002561 /* rxbuf_min and rxbuf_max are no longer configurable. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002562
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002563 return len;
2564}
2565
Joe Perchesd61e4032018-03-23 15:54:39 -07002566static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2567static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2568static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
Takashi Iwai27b917e2015-02-04 14:38:55 +01002569
2570static struct attribute *xennet_dev_attrs[] = {
2571 &dev_attr_rxbuf_min.attr,
2572 &dev_attr_rxbuf_max.attr,
2573 &dev_attr_rxbuf_cur.attr,
2574 NULL
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002575};
2576
Takashi Iwai27b917e2015-02-04 14:38:55 +01002577static const struct attribute_group xennet_dev_group = {
2578 .attrs = xennet_dev_attrs
2579};
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002580#endif /* CONFIG_SYSFS */
2581
Andrea Righic2c63312020-07-24 10:59:10 +02002582static void xennet_bus_close(struct xenbus_device *dev)
2583{
2584 int ret;
2585
2586 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2587 return;
2588 do {
2589 xenbus_switch_state(dev, XenbusStateClosing);
2590 ret = wait_event_timeout(module_wq,
2591 xenbus_read_driver_state(dev->otherend) ==
2592 XenbusStateClosing ||
2593 xenbus_read_driver_state(dev->otherend) ==
2594 XenbusStateClosed ||
2595 xenbus_read_driver_state(dev->otherend) ==
2596 XenbusStateUnknown,
2597 XENNET_TIMEOUT);
2598 } while (!ret);
2599
2600 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2601 return;
2602
2603 do {
2604 xenbus_switch_state(dev, XenbusStateClosed);
2605 ret = wait_event_timeout(module_wq,
2606 xenbus_read_driver_state(dev->otherend) ==
2607 XenbusStateClosed ||
2608 xenbus_read_driver_state(dev->otherend) ==
2609 XenbusStateUnknown,
2610 XENNET_TIMEOUT);
2611 } while (!ret);
2612}
2613
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002614static int xennet_remove(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002615{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002616 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002617
Andrea Righic2c63312020-07-24 10:59:10 +02002618 xennet_bus_close(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002619 xennet_disconnect_backend(info);
2620
Ross Lagerwallf599c642018-01-11 09:36:38 +00002621 if (info->netdev->reg_state == NETREG_REGISTERED)
2622 unregister_netdev(info->netdev);
Ian Campbell6bc96d02012-06-25 22:48:41 +00002623
Ross Lagerwallf599c642018-01-11 09:36:38 +00002624 if (info->queues) {
2625 rtnl_lock();
Chas Williams9a873c72015-08-19 19:14:20 -04002626 xennet_destroy_queues(info);
Ross Lagerwallf599c642018-01-11 09:36:38 +00002627 rtnl_unlock();
2628 }
David Vrabel900e1832015-01-13 16:42:42 +00002629 xennet_free_netdev(info->netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002630
2631 return 0;
2632}
2633
David Vrabel95afae42014-09-08 17:30:41 +01002634static const struct xenbus_device_id netfront_ids[] = {
2635 { "vif" },
2636 { "" }
2637};
2638
2639static struct xenbus_driver netfront_driver = {
2640 .ids = netfront_ids,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002641 .probe = netfront_probe,
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002642 .remove = xennet_remove,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002643 .resume = netfront_resume,
Ian Campbellf502bf22010-08-18 23:27:49 +00002644 .otherend_changed = netback_changed,
David Vrabel95afae42014-09-08 17:30:41 +01002645};
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002646
2647static int __init netif_init(void)
2648{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002649 if (!xen_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002650 return -ENODEV;
2651
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002652 if (!xen_has_pv_nic_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002653 return -ENODEV;
2654
Joe Perches383eda32013-06-27 21:57:49 -07002655 pr_info("Initialising Xen virtual ethernet driver\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002656
Juergen Gross034702a2017-01-10 14:32:51 +01002657 /* Allow as many queues as there are CPUs inut max. 8 if user has not
Wei Liu32a84402015-09-10 11:18:58 +01002658 * specified a value.
2659 */
2660 if (xennet_max_queues == 0)
Juergen Gross034702a2017-01-10 14:32:51 +01002661 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2662 num_online_cpus());
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002663
Al Viroffb78a22008-11-22 17:38:14 +00002664 return xenbus_register_frontend(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002665}
2666module_init(netif_init);
2667
2668
2669static void __exit netif_exit(void)
2670{
Al Viroffb78a22008-11-22 17:38:14 +00002671 xenbus_unregister_driver(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002672}
2673module_exit(netif_exit);
2674
2675MODULE_DESCRIPTION("Xen virtual network device frontend");
2676MODULE_LICENSE("GPL");
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002677MODULE_ALIAS("xen:vif");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002678MODULE_ALIAS("xennet");