blob: fc41ba95f81d09feb89c2f2eaff7349c9d924c16 [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
151 struct page_pool *page_pool;
152 struct xdp_rxq_info xdp_rxq;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100153};
154
155struct netfront_info {
156 struct list_head list;
157 struct net_device *netdev;
158
159 struct xenbus_device *xbdev;
160
161 /* Multi-queue support */
162 struct netfront_queue *queues;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000163
164 /* Statistics */
David Vrabel900e1832015-01-13 16:42:42 +0000165 struct netfront_stats __percpu *rx_stats;
166 struct netfront_stats __percpu *tx_stats;
stephen hemmingere00f85b2011-06-21 05:35:31 +0000167
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300168 /* XDP state */
169 bool netback_has_xdp_headroom;
170 bool netfront_xdp_enabled;
171
Juergen Grossa884daa2021-08-24 12:28:09 +0200172 /* Is device behaving sane? */
173 bool broken;
174
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100175 atomic_t rx_gso_checksum_fixup;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700176};
177
178struct netfront_rx_info {
179 struct xen_netif_rx_response rx;
180 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
181};
182
183/*
184 * Access macros for acquiring freeing slots in tx_skbs[].
185 */
186
Juergen Gross21631d22021-08-24 12:28:08 +0200187static void add_id_to_list(unsigned *head, unsigned short *list,
188 unsigned short id)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700189{
Juergen Gross21631d22021-08-24 12:28:08 +0200190 list[id] = *head;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700191 *head = id;
192}
193
Juergen Gross21631d22021-08-24 12:28:08 +0200194static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700195{
196 unsigned int id = *head;
Juergen Gross21631d22021-08-24 12:28:08 +0200197
198 if (id != TX_LINK_NONE) {
199 *head = list[id];
200 list[id] = TX_LINK_NONE;
201 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700202 return id;
203}
204
205static int xennet_rxidx(RING_IDX idx)
206{
207 return idx & (NET_RX_RING_SIZE - 1);
208}
209
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100210static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700211 RING_IDX ri)
212{
213 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100214 struct sk_buff *skb = queue->rx_skbs[i];
215 queue->rx_skbs[i] = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700216 return skb;
217}
218
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100219static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700220 RING_IDX ri)
221{
222 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100223 grant_ref_t ref = queue->grant_rx_ref[i];
224 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700225 return ref;
226}
227
228#ifdef CONFIG_SYSFS
Takashi Iwai27b917e2015-02-04 14:38:55 +0100229static const struct attribute_group xennet_dev_group;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700230#endif
231
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000232static bool xennet_can_sg(struct net_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700233{
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000234 return dev->features & NETIF_F_SG;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700235}
236
237
Kees Cooke99e88a2017-10-16 14:43:17 -0700238static void rx_refill_timeout(struct timer_list *t)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700239{
Kees Cooke99e88a2017-10-16 14:43:17 -0700240 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100241 napi_schedule(&queue->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700242}
243
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100244static int netfront_tx_slot_available(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700245{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100246 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
Juergen Gross57f230a2018-06-12 08:57:53 +0200247 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700248}
249
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100250static void xennet_maybe_wake_tx(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700251{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100252 struct net_device *dev = queue->info->netdev;
253 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700254
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100255 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
256 netfront_tx_slot_available(queue) &&
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700257 likely(netif_running(dev)))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100258 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700259}
260
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100261
262static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700263{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700264 struct sk_buff *skb;
265 struct page *page;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100266
267 skb = __netdev_alloc_skb(queue->info->netdev,
268 RX_COPY_THRESHOLD + NET_IP_ALIGN,
269 GFP_ATOMIC | __GFP_NOWARN);
270 if (unlikely(!skb))
271 return NULL;
272
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300273 page = page_pool_dev_alloc_pages(queue->page_pool);
274 if (unlikely(!page)) {
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100275 kfree_skb(skb);
276 return NULL;
277 }
278 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
279
280 /* Align ip header to a 16 bytes boundary */
281 skb_reserve(skb, NET_IP_ALIGN);
282 skb->dev = queue->info->netdev;
283
284 return skb;
285}
286
287
288static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
289{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100290 RING_IDX req_prod = queue->rx.req_prod_pvt;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100291 int notify;
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000292 int err = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700293
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100294 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700295 return;
296
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100297 for (req_prod = queue->rx.req_prod_pvt;
298 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
299 req_prod++) {
300 struct sk_buff *skb;
301 unsigned short id;
302 grant_ref_t ref;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100303 struct page *page;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100304 struct xen_netif_rx_request *req;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700305
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100306 skb = xennet_alloc_one_rx_buffer(queue);
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000307 if (!skb) {
308 err = -ENOMEM;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700309 break;
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000310 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700311
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100312 id = xennet_rxidx(req_prod);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700313
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100314 BUG_ON(queue->rx_skbs[id]);
315 queue->rx_skbs[id] = skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700316
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100317 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
Dongli Zhang269ebce2016-11-02 09:04:33 +0800318 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100319 queue->grant_rx_ref[id] = ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700320
Julien Grall30c5d7f2015-04-10 14:42:21 +0100321 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700322
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100323 req = RING_GET_REQUEST(&queue->rx, req_prod);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100324 gnttab_page_grant_foreign_access_ref_one(ref,
325 queue->info->xbdev->otherend_id,
326 page,
327 0);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700328 req->id = id;
329 req->gref = ref;
330 }
331
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100332 queue->rx.req_prod_pvt = req_prod;
333
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000334 /* Try again later if there are not enough requests or skb allocation
335 * failed.
336 * Enough requests is quantified as the sum of newly created slots and
337 * the unconsumed slots at the backend.
338 */
339 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
340 unlikely(err)) {
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100341 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
342 return;
343 }
344
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100345 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700346 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100347 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700348}
349
350static int xennet_open(struct net_device *dev)
351{
352 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100353 unsigned int num_queues = dev->real_num_tx_queues;
354 unsigned int i = 0;
355 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700356
Juergen Grossa884daa2021-08-24 12:28:09 +0200357 if (!np->queues || np->broken)
Ross Lagerwallf599c642018-01-11 09:36:38 +0000358 return -ENODEV;
359
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100360 for (i = 0; i < num_queues; ++i) {
361 queue = &np->queues[i];
362 napi_enable(&queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700363
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100364 spin_lock_bh(&queue->rx_lock);
365 if (netif_carrier_ok(dev)) {
366 xennet_alloc_rx_buffers(queue);
367 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
368 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
369 napi_schedule(&queue->napi);
370 }
371 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700372 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700373
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100374 netif_tx_start_all_queues(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700375
376 return 0;
377}
378
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100379static void xennet_tx_buf_gc(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700380{
381 RING_IDX cons, prod;
382 unsigned short id;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700383 struct sk_buff *skb;
Malcolm Crossley7d0105b2016-01-26 17:12:44 +0000384 bool more_to_do;
Juergen Grossa884daa2021-08-24 12:28:09 +0200385 const struct device *dev = &queue->info->netdev->dev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700386
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100387 BUG_ON(!netif_carrier_ok(queue->info->netdev));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700388
389 do {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100390 prod = queue->tx.sring->rsp_prod;
Juergen Grossa884daa2021-08-24 12:28:09 +0200391 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
392 dev_alert(dev, "Illegal number of responses %u\n",
393 prod - queue->tx.rsp_cons);
394 goto err;
395 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700396 rmb(); /* Ensure we see responses up to 'rp'. */
397
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100398 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
Juergen Gross84460662021-08-24 12:28:06 +0200399 struct xen_netif_tx_response txrsp;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700400
Juergen Gross84460662021-08-24 12:28:06 +0200401 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
402 if (txrsp.status == XEN_NETIF_RSP_NULL)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700403 continue;
404
Juergen Grossa884daa2021-08-24 12:28:09 +0200405 id = txrsp.id;
406 if (id >= RING_SIZE(&queue->tx)) {
407 dev_alert(dev,
408 "Response has incorrect id (%u)\n",
409 id);
410 goto err;
411 }
412 if (queue->tx_link[id] != TX_PENDING) {
413 dev_alert(dev,
414 "Response for inactive request\n");
415 goto err;
416 }
417
418 queue->tx_link[id] = TX_LINK_NONE;
Juergen Gross21631d22021-08-24 12:28:08 +0200419 skb = queue->tx_skbs[id];
420 queue->tx_skbs[id] = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700421 if (unlikely(gnttab_query_foreign_access(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100422 queue->grant_tx_ref[id]) != 0)) {
Juergen Grossa884daa2021-08-24 12:28:09 +0200423 dev_alert(dev,
424 "Grant still in use by backend domain\n");
425 goto err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700426 }
427 gnttab_end_foreign_access_ref(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100428 queue->grant_tx_ref[id], GNTMAP_readonly);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700429 gnttab_release_grant_reference(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100430 &queue->gref_tx_head, queue->grant_tx_ref[id]);
431 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
432 queue->grant_tx_page[id] = NULL;
Juergen Gross21631d22021-08-24 12:28:08 +0200433 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700434 dev_kfree_skb_irq(skb);
435 }
436
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100437 queue->tx.rsp_cons = prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700438
Malcolm Crossley7d0105b2016-01-26 17:12:44 +0000439 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
440 } while (more_to_do);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700441
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100442 xennet_maybe_wake_tx(queue);
Juergen Grossa884daa2021-08-24 12:28:09 +0200443
444 return;
445
446 err:
447 queue->info->broken = true;
448 dev_alert(dev, "Disabled for further use\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700449}
450
Julien Grall30c5d7f2015-04-10 14:42:21 +0100451struct xennet_gnttab_make_txreq {
452 struct netfront_queue *queue;
453 struct sk_buff *skb;
454 struct page *page;
Juergen Gross162081ec2021-08-24 12:28:07 +0200455 struct xen_netif_tx_request *tx; /* Last request on ring page */
456 struct xen_netif_tx_request tx_local; /* Last request local copy*/
Julien Grall30c5d7f2015-04-10 14:42:21 +0100457 unsigned int size;
458};
459
460static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
461 unsigned int len, void *data)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700462{
Julien Grall30c5d7f2015-04-10 14:42:21 +0100463 struct xennet_gnttab_make_txreq *info = data;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700464 unsigned int id;
David Vrabela55e8bb2015-01-13 17:16:44 +0000465 struct xen_netif_tx_request *tx;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700466 grant_ref_t ref;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100467 /* convenient aliases */
468 struct page *page = info->page;
469 struct netfront_queue *queue = info->queue;
470 struct sk_buff *skb = info->skb;
David Vrabela55e8bb2015-01-13 17:16:44 +0000471
Juergen Gross21631d22021-08-24 12:28:08 +0200472 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
David Vrabela55e8bb2015-01-13 17:16:44 +0000473 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
474 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
Dongli Zhang269ebce2016-11-02 09:04:33 +0800475 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
David Vrabela55e8bb2015-01-13 17:16:44 +0000476
Julien Grall30c5d7f2015-04-10 14:42:21 +0100477 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
478 gfn, GNTMAP_readonly);
David Vrabela55e8bb2015-01-13 17:16:44 +0000479
Juergen Gross21631d22021-08-24 12:28:08 +0200480 queue->tx_skbs[id] = skb;
David Vrabela55e8bb2015-01-13 17:16:44 +0000481 queue->grant_tx_page[id] = page;
482 queue->grant_tx_ref[id] = ref;
483
Juergen Gross162081ec2021-08-24 12:28:07 +0200484 info->tx_local.id = id;
485 info->tx_local.gref = ref;
486 info->tx_local.offset = offset;
487 info->tx_local.size = len;
488 info->tx_local.flags = 0;
489
490 *tx = info->tx_local;
David Vrabela55e8bb2015-01-13 17:16:44 +0000491
Juergen Grossa884daa2021-08-24 12:28:09 +0200492 /*
493 * Put the request in the pending queue, it will be set to be pending
494 * when the producer index is about to be raised.
495 */
496 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
497
Julien Grall30c5d7f2015-04-10 14:42:21 +0100498 info->tx = tx;
Juergen Gross162081ec2021-08-24 12:28:07 +0200499 info->size += info->tx_local.size;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100500}
501
502static struct xen_netif_tx_request *xennet_make_first_txreq(
Juergen Gross162081ec2021-08-24 12:28:07 +0200503 struct xennet_gnttab_make_txreq *info,
504 unsigned int offset, unsigned int len)
Julien Grall30c5d7f2015-04-10 14:42:21 +0100505{
Juergen Gross162081ec2021-08-24 12:28:07 +0200506 info->size = 0;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100507
Juergen Gross162081ec2021-08-24 12:28:07 +0200508 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100509
Juergen Gross162081ec2021-08-24 12:28:07 +0200510 return info->tx;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100511}
512
513static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
514 unsigned int len, void *data)
515{
516 struct xennet_gnttab_make_txreq *info = data;
517
518 info->tx->flags |= XEN_NETTXF_more_data;
519 skb_get(info->skb);
520 xennet_tx_setup_grant(gfn, offset, len, data);
David Vrabela55e8bb2015-01-13 17:16:44 +0000521}
522
Juergen Gross162081ec2021-08-24 12:28:07 +0200523static void xennet_make_txreqs(
524 struct xennet_gnttab_make_txreq *info,
525 struct page *page,
David Vrabela55e8bb2015-01-13 17:16:44 +0000526 unsigned int offset, unsigned int len)
527{
528 /* Skip unused frames from start of page */
529 page += offset >> PAGE_SHIFT;
530 offset &= ~PAGE_MASK;
531
532 while (len) {
Juergen Gross162081ec2021-08-24 12:28:07 +0200533 info->page = page;
534 info->size = 0;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100535
536 gnttab_foreach_grant_in_range(page, offset, len,
537 xennet_make_one_txreq,
Juergen Gross162081ec2021-08-24 12:28:07 +0200538 info);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100539
David Vrabela55e8bb2015-01-13 17:16:44 +0000540 page++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700541 offset = 0;
Juergen Gross162081ec2021-08-24 12:28:07 +0200542 len -= info->size;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700543 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700544}
545
Ian Campbellf36c3742012-11-21 02:02:16 +0000546/*
David Vrabele84448d2015-01-13 17:16:43 +0000547 * Count how many ring slots are required to send this skb. Each frag
548 * might be a compound page.
Ian Campbellf36c3742012-11-21 02:02:16 +0000549 */
David Vrabele84448d2015-01-13 17:16:43 +0000550static int xennet_count_skb_slots(struct sk_buff *skb)
Ian Campbellf36c3742012-11-21 02:02:16 +0000551{
552 int i, frags = skb_shinfo(skb)->nr_frags;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100553 int slots;
David Vrabele84448d2015-01-13 17:16:43 +0000554
Julien Grall30c5d7f2015-04-10 14:42:21 +0100555 slots = gnttab_count_grant(offset_in_page(skb->data),
556 skb_headlen(skb));
Ian Campbellf36c3742012-11-21 02:02:16 +0000557
558 for (i = 0; i < frags; i++) {
559 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
560 unsigned long size = skb_frag_size(frag);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700561 unsigned long offset = skb_frag_off(frag);
Ian Campbellf36c3742012-11-21 02:02:16 +0000562
563 /* Skip unused frames from start of page */
564 offset &= ~PAGE_MASK;
565
Julien Grall30c5d7f2015-04-10 14:42:21 +0100566 slots += gnttab_count_grant(offset, size);
Ian Campbellf36c3742012-11-21 02:02:16 +0000567 }
568
Julien Grall30c5d7f2015-04-10 14:42:21 +0100569 return slots;
Ian Campbellf36c3742012-11-21 02:02:16 +0000570}
571
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100572static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +0100573 struct net_device *sb_dev)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100574{
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100575 unsigned int num_queues = dev->real_num_tx_queues;
576 u32 hash;
577 u16 queue_idx;
578
579 /* First, check if there is only one queue */
580 if (num_queues == 1) {
581 queue_idx = 0;
582 } else {
583 hash = skb_get_hash(skb);
584 queue_idx = hash % num_queues;
585 }
586
587 return queue_idx;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100588}
589
Juergen Grossa884daa2021-08-24 12:28:09 +0200590static void xennet_mark_tx_pending(struct netfront_queue *queue)
591{
592 unsigned int i;
593
594 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
595 TX_LINK_NONE)
596 queue->tx_link[i] = TX_PENDING;
597}
598
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300599static int xennet_xdp_xmit_one(struct net_device *dev,
600 struct netfront_queue *queue,
601 struct xdp_frame *xdpf)
602{
603 struct netfront_info *np = netdev_priv(dev);
604 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
Juergen Gross162081ec2021-08-24 12:28:07 +0200605 struct xennet_gnttab_make_txreq info = {
606 .queue = queue,
607 .skb = NULL,
608 .page = virt_to_page(xdpf->data),
609 };
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300610 int notify;
611
Juergen Gross162081ec2021-08-24 12:28:07 +0200612 xennet_make_first_txreq(&info,
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300613 offset_in_page(xdpf->data),
614 xdpf->len);
615
Juergen Grossa884daa2021-08-24 12:28:09 +0200616 xennet_mark_tx_pending(queue);
617
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300618 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
619 if (notify)
620 notify_remote_via_irq(queue->tx_irq);
621
622 u64_stats_update_begin(&tx_stats->syncp);
623 tx_stats->bytes += xdpf->len;
624 tx_stats->packets++;
625 u64_stats_update_end(&tx_stats->syncp);
626
627 xennet_tx_buf_gc(queue);
628
629 return 0;
630}
631
632static int xennet_xdp_xmit(struct net_device *dev, int n,
633 struct xdp_frame **frames, u32 flags)
634{
635 unsigned int num_queues = dev->real_num_tx_queues;
636 struct netfront_info *np = netdev_priv(dev);
637 struct netfront_queue *queue = NULL;
638 unsigned long irq_flags;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100639 int nxmit = 0;
640 int i;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300641
Juergen Grossa884daa2021-08-24 12:28:09 +0200642 if (unlikely(np->broken))
643 return -ENODEV;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300644 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
645 return -EINVAL;
646
647 queue = &np->queues[smp_processor_id() % num_queues];
648
649 spin_lock_irqsave(&queue->tx_lock, irq_flags);
650 for (i = 0; i < n; i++) {
651 struct xdp_frame *xdpf = frames[i];
652
653 if (!xdpf)
654 continue;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100655 if (xennet_xdp_xmit_one(dev, queue, xdpf))
656 break;
657 nxmit++;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300658 }
659 spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
660
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100661 return nxmit;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300662}
663
664
Julien Grall30c5d7f2015-04-10 14:42:21 +0100665#define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
666
Luc Van Oostenryck24a94b32018-04-24 15:18:14 +0200667static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700668{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700669 struct netfront_info *np = netdev_priv(dev);
David Vrabel900e1832015-01-13 16:42:42 +0000670 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
Juergen Gross162081ec2021-08-24 12:28:07 +0200671 struct xen_netif_tx_request *first_tx;
David Vrabela55e8bb2015-01-13 17:16:44 +0000672 unsigned int i;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700673 int notify;
Ian Campbellf36c3742012-11-21 02:02:16 +0000674 int slots;
David Vrabela55e8bb2015-01-13 17:16:44 +0000675 struct page *page;
676 unsigned int offset;
677 unsigned int len;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +0000678 unsigned long flags;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100679 struct netfront_queue *queue = NULL;
Juergen Gross162081ec2021-08-24 12:28:07 +0200680 struct xennet_gnttab_make_txreq info = { };
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100681 unsigned int num_queues = dev->real_num_tx_queues;
682 u16 queue_index;
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200683 struct sk_buff *nskb;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100684
685 /* Drop the packet if no queues are set up */
686 if (num_queues < 1)
687 goto drop;
Juergen Grossa884daa2021-08-24 12:28:09 +0200688 if (unlikely(np->broken))
689 goto drop;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100690 /* Determine which queue to transmit this SKB on */
691 queue_index = skb_get_queue_mapping(skb);
692 queue = &np->queues[queue_index];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700693
Wei Liu9ecd1a72013-04-22 02:20:41 +0000694 /* If skb->len is too big for wire format, drop skb and alert
695 * user about misconfiguration.
696 */
697 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
698 net_alert_ratelimited(
699 "xennet: skb->len = %u, too big for wire format\n",
700 skb->len);
701 goto drop;
702 }
703
David Vrabele84448d2015-01-13 17:16:43 +0000704 slots = xennet_count_skb_slots(skb);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100705 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
Zoltan Kiss97a6d1b2014-08-11 18:32:23 +0100706 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
707 slots, skb->len);
708 if (skb_linearize(skb))
709 goto drop;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700710 }
711
David Vrabela55e8bb2015-01-13 17:16:44 +0000712 page = virt_to_page(skb->data);
713 offset = offset_in_page(skb->data);
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200714
715 /* The first req should be at least ETH_HLEN size or the packet will be
716 * dropped by netback.
717 */
718 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
719 nskb = skb_copy(skb, GFP_ATOMIC);
720 if (!nskb)
721 goto drop;
Eric Dumazet62f32502017-08-30 10:32:58 -0700722 dev_consume_skb_any(skb);
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200723 skb = nskb;
724 page = virt_to_page(skb->data);
725 offset = offset_in_page(skb->data);
726 }
727
David Vrabela55e8bb2015-01-13 17:16:44 +0000728 len = skb_headlen(skb);
729
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100730 spin_lock_irqsave(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700731
732 if (unlikely(!netif_carrier_ok(dev) ||
Ian Campbellf36c3742012-11-21 02:02:16 +0000733 (slots > 1 && !xennet_can_sg(dev)) ||
Johannes Berg8b86a612015-04-17 15:45:04 +0200734 netif_needs_gso(skb, netif_skb_features(skb)))) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100735 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700736 goto drop;
737 }
738
David Vrabela55e8bb2015-01-13 17:16:44 +0000739 /* First request for the linear area. */
Juergen Gross162081ec2021-08-24 12:28:07 +0200740 info.queue = queue;
741 info.skb = skb;
742 info.page = page;
743 first_tx = xennet_make_first_txreq(&info, offset, len);
744 offset += info.tx_local.size;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100745 if (offset == PAGE_SIZE) {
746 page++;
747 offset = 0;
748 }
Juergen Gross162081ec2021-08-24 12:28:07 +0200749 len -= info.tx_local.size;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700750
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700751 if (skb->ip_summed == CHECKSUM_PARTIAL)
752 /* local packet? */
Juergen Gross162081ec2021-08-24 12:28:07 +0200753 first_tx->flags |= XEN_NETTXF_csum_blank |
754 XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700755 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
756 /* remote but checksummed. */
Juergen Gross162081ec2021-08-24 12:28:07 +0200757 first_tx->flags |= XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700758
David Vrabela55e8bb2015-01-13 17:16:44 +0000759 /* Optional extra info after the first request. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700760 if (skb_shinfo(skb)->gso_size) {
761 struct xen_netif_extra_info *gso;
762
763 gso = (struct xen_netif_extra_info *)
David Vrabela55e8bb2015-01-13 17:16:44 +0000764 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700765
Juergen Gross162081ec2021-08-24 12:28:07 +0200766 first_tx->flags |= XEN_NETTXF_extra_info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700767
768 gso->u.gso.size = skb_shinfo(skb)->gso_size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000769 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
770 XEN_NETIF_GSO_TYPE_TCPV6 :
771 XEN_NETIF_GSO_TYPE_TCPV4;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700772 gso->u.gso.pad = 0;
773 gso->u.gso.features = 0;
774
775 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
776 gso->flags = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700777 }
778
David Vrabela55e8bb2015-01-13 17:16:44 +0000779 /* Requests for the rest of the linear area. */
Juergen Gross162081ec2021-08-24 12:28:07 +0200780 xennet_make_txreqs(&info, page, offset, len);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700781
David Vrabela55e8bb2015-01-13 17:16:44 +0000782 /* Requests for all the frags. */
783 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
784 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Juergen Gross162081ec2021-08-24 12:28:07 +0200785 xennet_make_txreqs(&info, skb_frag_page(frag),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700786 skb_frag_off(frag),
David Vrabela55e8bb2015-01-13 17:16:44 +0000787 skb_frag_size(frag));
788 }
789
790 /* First request has the packet length. */
791 first_tx->size = skb->len;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700792
Daniel Drown91ffb9d2020-07-03 01:22:34 -0500793 /* timestamp packet in software */
794 skb_tx_timestamp(skb);
795
Juergen Grossa884daa2021-08-24 12:28:09 +0200796 xennet_mark_tx_pending(queue);
797
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100798 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700799 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100800 notify_remote_via_irq(queue->tx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700801
David Vrabel900e1832015-01-13 16:42:42 +0000802 u64_stats_update_begin(&tx_stats->syncp);
803 tx_stats->bytes += skb->len;
804 tx_stats->packets++;
805 u64_stats_update_end(&tx_stats->syncp);
Jeremy Fitzhardinge10a273a2007-08-13 12:54:37 -0700806
807 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100808 xennet_tx_buf_gc(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700809
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100810 if (!netfront_tx_slot_available(queue))
811 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700812
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100813 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700814
Patrick McHardy6ed10652009-06-23 06:03:08 +0000815 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700816
817 drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700818 dev->stats.tx_dropped++;
Eric W. Biederman979de8a2014-03-15 18:33:04 -0700819 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000820 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700821}
822
823static int xennet_close(struct net_device *dev)
824{
825 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100826 unsigned int num_queues = dev->real_num_tx_queues;
827 unsigned int i;
828 struct netfront_queue *queue;
829 netif_tx_stop_all_queues(np->netdev);
830 for (i = 0; i < num_queues; ++i) {
831 queue = &np->queues[i];
832 napi_disable(&queue->napi);
833 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700834 return 0;
835}
836
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100837static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700838 grant_ref_t ref)
839{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100840 int new = xennet_rxidx(queue->rx.req_prod_pvt);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700841
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100842 BUG_ON(queue->rx_skbs[new]);
843 queue->rx_skbs[new] = skb;
844 queue->grant_rx_ref[new] = ref;
845 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
846 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
847 queue->rx.req_prod_pvt++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700848}
849
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100850static int xennet_get_extras(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700851 struct xen_netif_extra_info *extras,
852 RING_IDX rp)
853
854{
Juergen Gross84460662021-08-24 12:28:06 +0200855 struct xen_netif_extra_info extra;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100856 struct device *dev = &queue->info->netdev->dev;
857 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700858 int err = 0;
859
860 do {
861 struct sk_buff *skb;
862 grant_ref_t ref;
863
864 if (unlikely(cons + 1 == rp)) {
865 if (net_ratelimit())
866 dev_warn(dev, "Missing extra info\n");
867 err = -EBADR;
868 break;
869 }
870
Juergen Gross84460662021-08-24 12:28:06 +0200871 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700872
Juergen Gross84460662021-08-24 12:28:06 +0200873 if (unlikely(!extra.type ||
874 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700875 if (net_ratelimit())
876 dev_warn(dev, "Invalid extra type: %d\n",
Juergen Gross84460662021-08-24 12:28:06 +0200877 extra.type);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700878 err = -EINVAL;
879 } else {
Juergen Gross84460662021-08-24 12:28:06 +0200880 extras[extra.type - 1] = extra;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700881 }
882
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100883 skb = xennet_get_rx_skb(queue, cons);
884 ref = xennet_get_rx_ref(queue, cons);
885 xennet_move_rx_slot(queue, skb, ref);
Juergen Gross84460662021-08-24 12:28:06 +0200886 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700887
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100888 queue->rx.rsp_cons = cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700889 return err;
890}
891
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300892static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
893 struct xen_netif_rx_response *rx, struct bpf_prog *prog,
894 struct xdp_buff *xdp, bool *need_xdp_flush)
895{
896 struct xdp_frame *xdpf;
897 u32 len = rx->status;
Colin Ian Kinge44f65f2020-07-02 15:22:23 +0100898 u32 act;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300899 int err;
900
Lorenzo Bianconi43b51692020-12-22 22:09:28 +0100901 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
902 &queue->xdp_rxq);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100903 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
904 len, false);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300905
906 act = bpf_prog_run_xdp(prog, xdp);
907 switch (act) {
908 case XDP_TX:
909 get_page(pdata);
910 xdpf = xdp_convert_buff_to_frame(xdp);
911 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
Lorenzo Bianconifdc13972021-03-08 12:06:58 +0100912 if (unlikely(!err))
913 xdp_return_frame_rx_napi(xdpf);
914 else if (unlikely(err < 0))
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300915 trace_xdp_exception(queue->info->netdev, prog, act);
916 break;
917 case XDP_REDIRECT:
918 get_page(pdata);
919 err = xdp_do_redirect(queue->info->netdev, xdp, prog);
920 *need_xdp_flush = true;
921 if (unlikely(err))
922 trace_xdp_exception(queue->info->netdev, prog, act);
923 break;
924 case XDP_PASS:
925 case XDP_DROP:
926 break;
927
928 case XDP_ABORTED:
929 trace_xdp_exception(queue->info->netdev, prog, act);
930 break;
931
932 default:
933 bpf_warn_invalid_xdp_action(act);
934 }
935
936 return act;
937}
938
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100939static int xennet_get_responses(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700940 struct netfront_rx_info *rinfo, RING_IDX rp,
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300941 struct sk_buff_head *list,
942 bool *need_xdp_flush)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700943{
Juergen Gross84460662021-08-24 12:28:06 +0200944 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300945 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100946 RING_IDX cons = queue->rx.rsp_cons;
947 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300948 struct xen_netif_extra_info *extras = rinfo->extras;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100949 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300950 struct device *dev = &queue->info->netdev->dev;
951 struct bpf_prog *xdp_prog;
952 struct xdp_buff xdp;
953 unsigned long ret;
Wei Liu7158ff62013-03-25 01:08:19 +0000954 int slots = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700955 int err = 0;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300956 u32 verdict;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700957
Ian Campbellf942dc22011-03-15 00:06:18 +0000958 if (rx->flags & XEN_NETRXF_extra_info) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100959 err = xennet_get_extras(queue, extras, rp);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +0300960 if (!err) {
961 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
962 struct xen_netif_extra_info *xdp;
963
964 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
965 rx->offset = xdp->u.xdp.headroom;
966 }
967 }
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100968 cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700969 }
970
971 for (;;) {
972 if (unlikely(rx->status < 0 ||
Julien Grall30c5d7f2015-04-10 14:42:21 +0100973 rx->offset + rx->status > XEN_PAGE_SIZE)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700974 if (net_ratelimit())
Julien Grall6c101272015-06-16 20:10:46 +0100975 dev_warn(dev, "rx->offset: %u, size: %d\n",
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700976 rx->offset, rx->status);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100977 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700978 err = -EINVAL;
979 goto next;
980 }
981
982 /*
983 * This definitely indicates a bug, either in this driver or in
984 * the backend driver. In future this should flag the bad
Wei Liu697089d2013-04-22 02:20:40 +0000985 * situation to the system controller to reboot the backend.
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700986 */
987 if (ref == GRANT_INVALID_REF) {
988 if (net_ratelimit())
989 dev_warn(dev, "Bad rx response id %d.\n",
990 rx->id);
991 err = -EINVAL;
992 goto next;
993 }
994
995 ret = gnttab_end_foreign_access_ref(ref, 0);
996 BUG_ON(!ret);
997
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100998 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700999
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001000 rcu_read_lock();
1001 xdp_prog = rcu_dereference(queue->xdp_prog);
1002 if (xdp_prog) {
1003 if (!(rx->flags & XEN_NETRXF_more_data)) {
1004 /* currently only a single page contains data */
1005 verdict = xennet_run_xdp(queue,
1006 skb_frag_page(&skb_shinfo(skb)->frags[0]),
1007 rx, xdp_prog, &xdp, need_xdp_flush);
1008 if (verdict != XDP_PASS)
1009 err = -EINVAL;
1010 } else {
1011 /* drop the frame */
1012 err = -EINVAL;
1013 }
1014 }
1015 rcu_read_unlock();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001016next:
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001017 __skb_queue_tail(list, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001018 if (!(rx->flags & XEN_NETRXF_more_data))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001019 break;
1020
Wei Liu7158ff62013-03-25 01:08:19 +00001021 if (cons + slots == rp) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001022 if (net_ratelimit())
Wei Liu7158ff62013-03-25 01:08:19 +00001023 dev_warn(dev, "Need more slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001024 err = -ENOENT;
1025 break;
1026 }
1027
Juergen Gross84460662021-08-24 12:28:06 +02001028 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
1029 rx = &rx_local;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001030 skb = xennet_get_rx_skb(queue, cons + slots);
1031 ref = xennet_get_rx_ref(queue, cons + slots);
Wei Liu7158ff62013-03-25 01:08:19 +00001032 slots++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001033 }
1034
Wei Liu7158ff62013-03-25 01:08:19 +00001035 if (unlikely(slots > max)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001036 if (net_ratelimit())
Wei Liu697089d2013-04-22 02:20:40 +00001037 dev_warn(dev, "Too many slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001038 err = -E2BIG;
1039 }
1040
1041 if (unlikely(err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001042 queue->rx.rsp_cons = cons + slots;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001043
1044 return err;
1045}
1046
1047static int xennet_set_skb_gso(struct sk_buff *skb,
1048 struct xen_netif_extra_info *gso)
1049{
1050 if (!gso->u.gso.size) {
1051 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -07001052 pr_warn("GSO size must not be zero\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001053 return -EINVAL;
1054 }
1055
Paul Durrant2c0057d2014-01-15 17:30:33 +00001056 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
1057 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001058 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -07001059 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001060 return -EINVAL;
1061 }
1062
1063 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001064 skb_shinfo(skb)->gso_type =
1065 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
1066 SKB_GSO_TCPV4 :
1067 SKB_GSO_TCPV6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001068
1069 /* Header must be checked, and gso_segs computed. */
1070 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1071 skb_shinfo(skb)->gso_segs = 0;
1072
1073 return 0;
1074}
1075
Dongli Zhanga7611292019-10-01 21:56:41 +08001076static int xennet_fill_frags(struct netfront_queue *queue,
1077 struct sk_buff *skb,
1078 struct sk_buff_head *list)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001079{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001080 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001081 struct sk_buff *nskb;
1082
1083 while ((nskb = __skb_dequeue(list))) {
Juergen Gross84460662021-08-24 12:28:06 +02001084 struct xen_netif_rx_response rx;
Ian Campbell01c68022011-10-05 00:28:47 +00001085 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001086
Juergen Gross84460662021-08-24 12:28:06 +02001087 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1088
Juergen Grossd472b3a2018-08-09 16:42:16 +02001089 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
Jan Beulich093b9c72013-07-17 08:09:37 +01001090 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001091
Juergen Grossd81c5052018-12-18 16:06:19 +01001092 BUG_ON(pull_to < skb_headlen(skb));
Jan Beulich093b9c72013-07-17 08:09:37 +01001093 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1094 }
Juergen Grossad4f15d2018-09-11 09:04:48 +02001095 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
Dongli Zhang00b36852019-09-16 11:46:59 +08001096 queue->rx.rsp_cons = ++cons + skb_queue_len(list);
Juergen Grossad4f15d2018-09-11 09:04:48 +02001097 kfree_skb(nskb);
Dongli Zhanga7611292019-10-01 21:56:41 +08001098 return -ENOENT;
Juergen Grossad4f15d2018-09-11 09:04:48 +02001099 }
Jan Beulich093b9c72013-07-17 08:09:37 +01001100
Juergen Grossd472b3a2018-08-09 16:42:16 +02001101 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1102 skb_frag_page(nfrag),
Juergen Gross84460662021-08-24 12:28:06 +02001103 rx.offset, rx.status, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001104
1105 skb_shinfo(nskb)->nr_frags = 0;
1106 kfree_skb(nskb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001107 }
1108
Dongli Zhanga7611292019-10-01 21:56:41 +08001109 queue->rx.rsp_cons = cons;
1110
1111 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001112}
1113
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001114static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001115{
Paul Durrantb5cf66c2014-01-09 10:02:48 +00001116 bool recalculate_partial_csum = false;
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001117
1118 /*
1119 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1120 * peers can fail to set NETRXF_csum_blank when sending a GSO
1121 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1122 * recalculate the partial checksum.
1123 */
1124 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1125 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001126 atomic_inc(&np->rx_gso_checksum_fixup);
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001127 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrantb5cf66c2014-01-09 10:02:48 +00001128 recalculate_partial_csum = true;
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001129 }
1130
1131 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1132 if (skb->ip_summed != CHECKSUM_PARTIAL)
1133 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001134
Paul Durrantb5cf66c2014-01-09 10:02:48 +00001135 return skb_checksum_setup(skb, recalculate_partial_csum);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001136}
1137
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001138static int handle_incoming_queue(struct netfront_queue *queue,
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001139 struct sk_buff_head *rxq)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001140{
David Vrabel900e1832015-01-13 16:42:42 +00001141 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001142 int packets_dropped = 0;
1143 struct sk_buff *skb;
1144
1145 while ((skb = __skb_dequeue(rxq)) != NULL) {
Ian Campbell36832432012-08-22 00:26:47 +00001146 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001147
Jan Beulich093b9c72013-07-17 08:09:37 +01001148 if (pull_to > skb_headlen(skb))
1149 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001150
1151 /* Ethernet work: Delayed to here as it peeks the header. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001152 skb->protocol = eth_type_trans(skb, queue->info->netdev);
Wei Liud554f732014-02-19 18:48:34 +00001153 skb_reset_network_header(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001154
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001155 if (checksum_setup(queue->info->netdev, skb)) {
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001156 kfree_skb(skb);
1157 packets_dropped++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001158 queue->info->netdev->stats.rx_errors++;
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001159 continue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001160 }
1161
David Vrabel900e1832015-01-13 16:42:42 +00001162 u64_stats_update_begin(&rx_stats->syncp);
1163 rx_stats->packets++;
1164 rx_stats->bytes += skb->len;
1165 u64_stats_update_end(&rx_stats->syncp);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001166
1167 /* Pass it up. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001168 napi_gro_receive(&queue->napi, skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001169 }
1170
1171 return packets_dropped;
1172}
1173
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001174static int xennet_poll(struct napi_struct *napi, int budget)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001175{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001176 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1177 struct net_device *dev = queue->info->netdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001178 struct sk_buff *skb;
1179 struct netfront_rx_info rinfo;
1180 struct xen_netif_rx_response *rx = &rinfo.rx;
1181 struct xen_netif_extra_info *extras = rinfo.extras;
1182 RING_IDX i, rp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001183 int work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001184 struct sk_buff_head rxq;
1185 struct sk_buff_head errq;
1186 struct sk_buff_head tmpq;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001187 int err;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001188 bool need_xdp_flush = false;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001189
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001190 spin_lock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001191
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001192 skb_queue_head_init(&rxq);
1193 skb_queue_head_init(&errq);
1194 skb_queue_head_init(&tmpq);
1195
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001196 rp = queue->rx.sring->rsp_prod;
Juergen Grossa884daa2021-08-24 12:28:09 +02001197 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1198 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1199 rp - queue->rx.rsp_cons);
1200 queue->info->broken = true;
1201 spin_unlock(&queue->rx_lock);
1202 return 0;
1203 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001204 rmb(); /* Ensure we see queued responses up to 'rp'. */
1205
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001206 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001207 work_done = 0;
1208 while ((i != rp) && (work_done < budget)) {
Juergen Gross84460662021-08-24 12:28:06 +02001209 RING_COPY_RESPONSE(&queue->rx, i, rx);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001210 memset(extras, 0, sizeof(rinfo.extras));
1211
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001212 err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1213 &need_xdp_flush);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001214
1215 if (unlikely(err)) {
1216err:
1217 while ((skb = __skb_dequeue(&tmpq)))
1218 __skb_queue_tail(&errq, skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001219 dev->stats.rx_errors++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001220 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001221 continue;
1222 }
1223
1224 skb = __skb_dequeue(&tmpq);
1225
1226 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1227 struct xen_netif_extra_info *gso;
1228 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1229
1230 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1231 __skb_queue_head(&tmpq, skb);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001232 queue->rx.rsp_cons += skb_queue_len(&tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001233 goto err;
1234 }
1235 }
1236
Ian Campbell36832432012-08-22 00:26:47 +00001237 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1238 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1239 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001240
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001241 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
Ian Campbell36832432012-08-22 00:26:47 +00001242 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1243 skb->data_len = rx->status;
Jan Beulich093b9c72013-07-17 08:09:37 +01001244 skb->len += rx->status;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001245
Dongli Zhanga7611292019-10-01 21:56:41 +08001246 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
Juergen Grossad4f15d2018-09-11 09:04:48 +02001247 goto err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001248
Ian Campbellf942dc22011-03-15 00:06:18 +00001249 if (rx->flags & XEN_NETRXF_csum_blank)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001250 skb->ip_summed = CHECKSUM_PARTIAL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001251 else if (rx->flags & XEN_NETRXF_data_validated)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001252 skb->ip_summed = CHECKSUM_UNNECESSARY;
1253
1254 __skb_queue_tail(&rxq, skb);
1255
Dongli Zhanga7611292019-10-01 21:56:41 +08001256 i = ++queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001257 work_done++;
1258 }
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001259 if (need_xdp_flush)
1260 xdp_do_flush();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001261
Wang Chen56cfe5d2008-05-22 18:09:06 +08001262 __skb_queue_purge(&errq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001263
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001264 work_done -= handle_incoming_queue(queue, &rxq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001265
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001266 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001267
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001268 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001269 int more_to_do = 0;
1270
Eric Dumazet6ad20162017-01-30 08:22:01 -08001271 napi_complete_done(napi, work_done);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001272
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001273 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
David Vrabel6a6dc082014-12-16 18:59:46 +00001274 if (more_to_do)
1275 napi_schedule(napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001276 }
1277
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001278 spin_unlock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001279
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001280 return work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001281}
1282
1283static int xennet_change_mtu(struct net_device *dev, int mtu)
1284{
Jonathan Davies0c368202015-03-31 11:05:15 +01001285 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001286
1287 if (mtu > max)
1288 return -EINVAL;
1289 dev->mtu = mtu;
1290 return 0;
1291}
1292
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001293static void xennet_get_stats64(struct net_device *dev,
1294 struct rtnl_link_stats64 *tot)
stephen hemmingere00f85b2011-06-21 05:35:31 +00001295{
1296 struct netfront_info *np = netdev_priv(dev);
1297 int cpu;
1298
1299 for_each_possible_cpu(cpu) {
David Vrabel900e1832015-01-13 16:42:42 +00001300 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1301 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
stephen hemmingere00f85b2011-06-21 05:35:31 +00001302 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1303 unsigned int start;
1304
1305 do {
David Vrabel900e1832015-01-13 16:42:42 +00001306 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1307 tx_packets = tx_stats->packets;
1308 tx_bytes = tx_stats->bytes;
1309 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001310
David Vrabel900e1832015-01-13 16:42:42 +00001311 do {
1312 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1313 rx_packets = rx_stats->packets;
1314 rx_bytes = rx_stats->bytes;
1315 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001316
1317 tot->rx_packets += rx_packets;
1318 tot->tx_packets += tx_packets;
1319 tot->rx_bytes += rx_bytes;
1320 tot->tx_bytes += tx_bytes;
1321 }
1322
1323 tot->rx_errors = dev->stats.rx_errors;
1324 tot->tx_dropped = dev->stats.tx_dropped;
stephen hemmingere00f85b2011-06-21 05:35:31 +00001325}
1326
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001327static void xennet_release_tx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001328{
1329 struct sk_buff *skb;
1330 int i;
1331
1332 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1333 /* Skip over entries which are actually freelist references */
Juergen Gross21631d22021-08-24 12:28:08 +02001334 if (!queue->tx_skbs[i])
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001335 continue;
1336
Juergen Gross21631d22021-08-24 12:28:08 +02001337 skb = queue->tx_skbs[i];
1338 queue->tx_skbs[i] = NULL;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001339 get_page(queue->grant_tx_page[i]);
1340 gnttab_end_foreign_access(queue->grant_tx_ref[i],
Annie Licefe0072014-01-28 11:35:42 +08001341 GNTMAP_readonly,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001342 (unsigned long)page_address(queue->grant_tx_page[i]));
1343 queue->grant_tx_page[i] = NULL;
1344 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
Juergen Gross21631d22021-08-24 12:28:08 +02001345 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001346 dev_kfree_skb_irq(skb);
1347 }
1348}
1349
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001350static void xennet_release_rx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001351{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001352 int id, ref;
1353
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001354 spin_lock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001355
1356 for (id = 0; id < NET_RX_RING_SIZE; id++) {
Annie Licefe0072014-01-28 11:35:42 +08001357 struct sk_buff *skb;
1358 struct page *page;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001359
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001360 skb = queue->rx_skbs[id];
Annie Licefe0072014-01-28 11:35:42 +08001361 if (!skb)
1362 continue;
1363
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001364 ref = queue->grant_rx_ref[id];
Annie Licefe0072014-01-28 11:35:42 +08001365 if (ref == GRANT_INVALID_REF)
1366 continue;
1367
1368 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1369
1370 /* gnttab_end_foreign_access() needs a page ref until
1371 * foreign access is ended (which may be deferred).
1372 */
1373 get_page(page);
1374 gnttab_end_foreign_access(ref, 0,
1375 (unsigned long)page_address(page));
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001376 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001377
Annie Licefe0072014-01-28 11:35:42 +08001378 kfree_skb(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001379 }
1380
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001381 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001382}
1383
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001384static netdev_features_t xennet_fix_features(struct net_device *dev,
1385 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001386{
1387 struct netfront_info *np = netdev_priv(dev);
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001388
Juergen Gross2890ea52016-10-31 14:58:41 +01001389 if (features & NETIF_F_SG &&
1390 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1391 features &= ~NETIF_F_SG;
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001392
Juergen Gross2890ea52016-10-31 14:58:41 +01001393 if (features & NETIF_F_IPV6_CSUM &&
1394 !xenbus_read_unsigned(np->xbdev->otherend,
1395 "feature-ipv6-csum-offload", 0))
1396 features &= ~NETIF_F_IPV6_CSUM;
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001397
Juergen Gross2890ea52016-10-31 14:58:41 +01001398 if (features & NETIF_F_TSO &&
1399 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1400 features &= ~NETIF_F_TSO;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001401
Juergen Gross2890ea52016-10-31 14:58:41 +01001402 if (features & NETIF_F_TSO6 &&
1403 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1404 features &= ~NETIF_F_TSO6;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001405
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001406 return features;
1407}
1408
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001409static int xennet_set_features(struct net_device *dev,
1410 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001411{
1412 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1413 netdev_info(dev, "Reducing MTU because no SG offload");
1414 dev->mtu = ETH_DATA_LEN;
1415 }
1416
1417 return 0;
1418}
1419
Wei Liud634bf22013-05-22 06:34:46 +00001420static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001421{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001422 struct netfront_queue *queue = dev_id;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001423 unsigned long flags;
1424
Juergen Grossa884daa2021-08-24 12:28:09 +02001425 if (queue->info->broken)
1426 return IRQ_HANDLED;
1427
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001428 spin_lock_irqsave(&queue->tx_lock, flags);
1429 xennet_tx_buf_gc(queue);
1430 spin_unlock_irqrestore(&queue->tx_lock, flags);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001431
1432 return IRQ_HANDLED;
1433}
1434
Wei Liud634bf22013-05-22 06:34:46 +00001435static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1436{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001437 struct netfront_queue *queue = dev_id;
1438 struct net_device *dev = queue->info->netdev;
Wei Liud634bf22013-05-22 06:34:46 +00001439
Juergen Grossa884daa2021-08-24 12:28:09 +02001440 if (queue->info->broken)
1441 return IRQ_HANDLED;
1442
Wei Liud634bf22013-05-22 06:34:46 +00001443 if (likely(netif_carrier_ok(dev) &&
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001444 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
David Vrabel76541862014-06-18 10:47:27 +01001445 napi_schedule(&queue->napi);
Wei Liud634bf22013-05-22 06:34:46 +00001446
1447 return IRQ_HANDLED;
1448}
1449
1450static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1451{
1452 xennet_tx_interrupt(irq, dev_id);
1453 xennet_rx_interrupt(irq, dev_id);
1454 return IRQ_HANDLED;
1455}
1456
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001457#ifdef CONFIG_NET_POLL_CONTROLLER
1458static void xennet_poll_controller(struct net_device *dev)
1459{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001460 /* Poll each queue */
1461 struct netfront_info *info = netdev_priv(dev);
1462 unsigned int num_queues = dev->real_num_tx_queues;
1463 unsigned int i;
Juergen Grossa884daa2021-08-24 12:28:09 +02001464
1465 if (info->broken)
1466 return;
1467
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001468 for (i = 0; i < num_queues; ++i)
1469 xennet_interrupt(0, &info->queues[i]);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001470}
1471#endif
1472
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001473#define NETBACK_XDP_HEADROOM_DISABLE 0
1474#define NETBACK_XDP_HEADROOM_ENABLE 1
1475
1476static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1477{
1478 int err;
1479 unsigned short headroom;
1480
1481 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1482 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1483 "xdp-headroom", "%hu",
1484 headroom);
1485 if (err)
1486 pr_warn("Error writing xdp-headroom\n");
1487
1488 return err;
1489}
1490
1491static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1492 struct netlink_ext_ack *extack)
1493{
1494 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1495 struct netfront_info *np = netdev_priv(dev);
1496 struct bpf_prog *old_prog;
1497 unsigned int i, err;
1498
1499 if (dev->mtu > max_mtu) {
1500 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1501 return -EINVAL;
1502 }
1503
1504 if (!np->netback_has_xdp_headroom)
1505 return 0;
1506
1507 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1508
1509 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1510 NETBACK_XDP_HEADROOM_DISABLE);
1511 if (err)
1512 return err;
1513
1514 /* avoid the race with XDP headroom adjustment */
1515 wait_event(module_wq,
1516 xenbus_read_driver_state(np->xbdev->otherend) ==
1517 XenbusStateReconfigured);
1518 np->netfront_xdp_enabled = true;
1519
1520 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1521
1522 if (prog)
1523 bpf_prog_add(prog, dev->real_num_tx_queues);
1524
1525 for (i = 0; i < dev->real_num_tx_queues; ++i)
1526 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1527
1528 if (old_prog)
1529 for (i = 0; i < dev->real_num_tx_queues; ++i)
1530 bpf_prog_put(old_prog);
1531
1532 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1533
1534 return 0;
1535}
1536
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001537static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1538{
Juergen Grossa884daa2021-08-24 12:28:09 +02001539 struct netfront_info *np = netdev_priv(dev);
1540
1541 if (np->broken)
1542 return -ENODEV;
1543
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001544 switch (xdp->command) {
1545 case XDP_SETUP_PROG:
1546 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001547 default:
1548 return -EINVAL;
1549 }
1550}
1551
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001552static const struct net_device_ops xennet_netdev_ops = {
1553 .ndo_open = xennet_open,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001554 .ndo_stop = xennet_close,
1555 .ndo_start_xmit = xennet_start_xmit,
1556 .ndo_change_mtu = xennet_change_mtu,
stephen hemmingere00f85b2011-06-21 05:35:31 +00001557 .ndo_get_stats64 = xennet_get_stats64,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001558 .ndo_set_mac_address = eth_mac_addr,
1559 .ndo_validate_addr = eth_validate_addr,
Michał Mirosławfb507932011-03-31 01:01:35 +00001560 .ndo_fix_features = xennet_fix_features,
1561 .ndo_set_features = xennet_set_features,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001562 .ndo_select_queue = xennet_select_queue,
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001563 .ndo_bpf = xennet_xdp,
1564 .ndo_xdp_xmit = xennet_xdp_xmit,
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001565#ifdef CONFIG_NET_POLL_CONTROLLER
1566 .ndo_poll_controller = xennet_poll_controller,
1567#endif
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001568};
1569
David Vrabel900e1832015-01-13 16:42:42 +00001570static void xennet_free_netdev(struct net_device *netdev)
1571{
1572 struct netfront_info *np = netdev_priv(netdev);
1573
1574 free_percpu(np->rx_stats);
1575 free_percpu(np->tx_stats);
1576 free_netdev(netdev);
1577}
1578
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001579static struct net_device *xennet_create_dev(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001580{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001581 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001582 struct net_device *netdev;
1583 struct netfront_info *np;
1584
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001585 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
Joe Perches41de8d42012-01-29 13:47:52 +00001586 if (!netdev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001587 return ERR_PTR(-ENOMEM);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001588
1589 np = netdev_priv(netdev);
1590 np->xbdev = dev;
1591
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001592 np->queues = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001593
stephen hemmingere00f85b2011-06-21 05:35:31 +00001594 err = -ENOMEM;
David Vrabel900e1832015-01-13 16:42:42 +00001595 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1596 if (np->rx_stats == NULL)
1597 goto exit;
1598 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1599 if (np->tx_stats == NULL)
stephen hemmingere00f85b2011-06-21 05:35:31 +00001600 goto exit;
1601
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001602 netdev->netdev_ops = &xennet_netdev_ops;
1603
Michał Mirosławfb507932011-03-31 01:01:35 +00001604 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1605 NETIF_F_GSO_ROBUST;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001606 netdev->hw_features = NETIF_F_SG |
1607 NETIF_F_IPV6_CSUM |
1608 NETIF_F_TSO | NETIF_F_TSO6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001609
Ian Campbellfc3e5942011-04-04 11:07:57 -07001610 /*
1611 * Assume that all hw features are available for now. This set
1612 * will be adjusted by the call to netdev_update_features() in
1613 * xennet_connect() which is the earliest point where we can
1614 * negotiate with the backend regarding supported features.
1615 */
1616 netdev->features |= netdev->hw_features;
1617
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001618 netdev->ethtool_ops = &xennet_ethtool_ops;
Mohammed Gamale1043a42017-10-16 15:20:32 +02001619 netdev->min_mtu = ETH_MIN_MTU;
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001620 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001621 SET_NETDEV_DEV(netdev, &dev->dev);
1622
1623 np->netdev = netdev;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001624 np->netfront_xdp_enabled = false;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001625
1626 netif_carrier_off(netdev);
1627
Andrea Righic2c63312020-07-24 10:59:10 +02001628 do {
1629 xenbus_switch_state(dev, XenbusStateInitialising);
1630 err = wait_event_timeout(module_wq,
1631 xenbus_read_driver_state(dev->otherend) !=
1632 XenbusStateClosed &&
1633 xenbus_read_driver_state(dev->otherend) !=
1634 XenbusStateUnknown, XENNET_TIMEOUT);
1635 } while (!err);
1636
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001637 return netdev;
1638
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001639 exit:
David Vrabel900e1832015-01-13 16:42:42 +00001640 xennet_free_netdev(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001641 return ERR_PTR(err);
1642}
1643
Lee Jones80708602021-01-15 20:09:03 +00001644/*
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001645 * Entry point to this code when a new device is created. Allocate the basic
1646 * structures and the ring buffers for communication with the backend, and
1647 * inform the backend of the appropriate details for those.
1648 */
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001649static int netfront_probe(struct xenbus_device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001650 const struct xenbus_device_id *id)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001651{
1652 int err;
1653 struct net_device *netdev;
1654 struct netfront_info *info;
1655
1656 netdev = xennet_create_dev(dev);
1657 if (IS_ERR(netdev)) {
1658 err = PTR_ERR(netdev);
1659 xenbus_dev_fatal(dev, err, "creating netdev");
1660 return err;
1661 }
1662
1663 info = netdev_priv(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001664 dev_set_drvdata(&dev->dev, info);
Takashi Iwai27b917e2015-02-04 14:38:55 +01001665#ifdef CONFIG_SYSFS
1666 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1667#endif
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001668
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001669 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001670}
1671
1672static void xennet_end_access(int ref, void *page)
1673{
1674 /* This frees the page as a side-effect */
1675 if (ref != GRANT_INVALID_REF)
1676 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1677}
1678
1679static void xennet_disconnect_backend(struct netfront_info *info)
1680{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001681 unsigned int i = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001682 unsigned int num_queues = info->netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001683
David Vrabelf9feb1e2014-07-02 16:09:15 +01001684 netif_carrier_off(info->netdev);
1685
Chas Williams9a873c72015-08-19 19:14:20 -04001686 for (i = 0; i < num_queues && info->queues; ++i) {
David Vrabel76541862014-06-18 10:47:27 +01001687 struct netfront_queue *queue = &info->queues[i];
1688
Boris Ostrovsky74470952017-01-30 12:45:46 -05001689 del_timer_sync(&queue->rx_refill_timer);
1690
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001691 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1692 unbind_from_irqhandler(queue->tx_irq, queue);
1693 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1694 unbind_from_irqhandler(queue->tx_irq, queue);
1695 unbind_from_irqhandler(queue->rx_irq, queue);
1696 }
1697 queue->tx_evtchn = queue->rx_evtchn = 0;
1698 queue->tx_irq = queue->rx_irq = 0;
1699
Chas Williams274b0452015-08-27 12:28:46 -04001700 if (netif_running(info->netdev))
1701 napi_synchronize(&queue->napi);
David Vrabelf9feb1e2014-07-02 16:09:15 +01001702
David Vrabela5b5dc32014-07-31 17:38:23 +01001703 xennet_release_tx_bufs(queue);
1704 xennet_release_rx_bufs(queue);
1705 gnttab_free_grant_references(queue->gref_tx_head);
1706 gnttab_free_grant_references(queue->gref_rx_head);
1707
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001708 /* End access and free the pages */
1709 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1710 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1711
1712 queue->tx_ring_ref = GRANT_INVALID_REF;
1713 queue->rx_ring_ref = GRANT_INVALID_REF;
1714 queue->tx.sring = NULL;
1715 queue->rx.sring = NULL;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03001716
1717 page_pool_destroy(queue->page_pool);
Wei Liud634bf22013-05-22 06:34:46 +00001718 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001719}
1720
Lee Jones80708602021-01-15 20:09:03 +00001721/*
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001722 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1723 * driver restart. We tear down our netif structure and recreate it, but
1724 * leave the device-layer structures intact so that this is transparent to the
1725 * rest of the kernel.
1726 */
1727static int netfront_resume(struct xenbus_device *dev)
1728{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001729 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001730
1731 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1732
Dongli Zhang042b2042021-10-22 16:31:39 -07001733 netif_tx_lock_bh(info->netdev);
1734 netif_device_detach(info->netdev);
1735 netif_tx_unlock_bh(info->netdev);
1736
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001737 xennet_disconnect_backend(info);
1738 return 0;
1739}
1740
1741static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1742{
1743 char *s, *e, *macstr;
1744 int i;
1745
1746 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1747 if (IS_ERR(macstr))
1748 return PTR_ERR(macstr);
1749
1750 for (i = 0; i < ETH_ALEN; i++) {
1751 mac[i] = simple_strtoul(s, &e, 16);
1752 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1753 kfree(macstr);
1754 return -ENOENT;
1755 }
1756 s = e+1;
1757 }
1758
1759 kfree(macstr);
1760 return 0;
1761}
1762
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001763static int setup_netfront_single(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001764{
1765 int err;
1766
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001767 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001768 if (err < 0)
1769 goto fail;
1770
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001771 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001772 xennet_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001773 0, queue->info->netdev->name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001774 if (err < 0)
1775 goto bind_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001776 queue->rx_evtchn = queue->tx_evtchn;
1777 queue->rx_irq = queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001778
1779 return 0;
1780
1781bind_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001782 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1783 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001784fail:
1785 return err;
1786}
1787
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001788static int setup_netfront_split(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001789{
1790 int err;
1791
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001792 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001793 if (err < 0)
1794 goto fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001795 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001796 if (err < 0)
1797 goto alloc_rx_evtchn_fail;
1798
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001799 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1800 "%s-tx", queue->name);
1801 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001802 xennet_tx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001803 0, queue->tx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001804 if (err < 0)
1805 goto bind_tx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001806 queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001807
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001808 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1809 "%s-rx", queue->name);
1810 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001811 xennet_rx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001812 0, queue->rx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001813 if (err < 0)
1814 goto bind_rx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001815 queue->rx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001816
1817 return 0;
1818
1819bind_rx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001820 unbind_from_irqhandler(queue->tx_irq, queue);
1821 queue->tx_irq = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001822bind_tx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001823 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1824 queue->rx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001825alloc_rx_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001826 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1827 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001828fail:
1829 return err;
1830}
1831
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001832static int setup_netfront(struct xenbus_device *dev,
1833 struct netfront_queue *queue, unsigned int feature_split_evtchn)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001834{
1835 struct xen_netif_tx_sring *txs;
1836 struct xen_netif_rx_sring *rxs;
Wei Liuccc9d902015-04-03 14:44:59 +08001837 grant_ref_t gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001838 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001839
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001840 queue->tx_ring_ref = GRANT_INVALID_REF;
1841 queue->rx_ring_ref = GRANT_INVALID_REF;
1842 queue->rx.sring = NULL;
1843 queue->tx.sring = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001844
Ian Campbella144ff02008-06-17 10:47:08 +02001845 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001846 if (!txs) {
1847 err = -ENOMEM;
1848 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1849 goto fail;
1850 }
1851 SHARED_RING_INIT(txs);
Julien Grall30c5d7f2015-04-10 14:42:21 +01001852 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001853
Wei Liuccc9d902015-04-03 14:44:59 +08001854 err = xenbus_grant_ring(dev, txs, 1, &gref);
Wei Liu1ca29832013-05-20 01:05:12 +00001855 if (err < 0)
1856 goto grant_tx_ring_fail;
Wei Liuccc9d902015-04-03 14:44:59 +08001857 queue->tx_ring_ref = gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001858
Ian Campbella144ff02008-06-17 10:47:08 +02001859 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001860 if (!rxs) {
1861 err = -ENOMEM;
1862 xenbus_dev_fatal(dev, err, "allocating rx ring page");
Wei Liu1ca29832013-05-20 01:05:12 +00001863 goto alloc_rx_ring_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001864 }
1865 SHARED_RING_INIT(rxs);
Julien Grall30c5d7f2015-04-10 14:42:21 +01001866 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001867
Wei Liuccc9d902015-04-03 14:44:59 +08001868 err = xenbus_grant_ring(dev, rxs, 1, &gref);
Wei Liu1ca29832013-05-20 01:05:12 +00001869 if (err < 0)
1870 goto grant_rx_ring_fail;
Wei Liuccc9d902015-04-03 14:44:59 +08001871 queue->rx_ring_ref = gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001872
Wei Liud634bf22013-05-22 06:34:46 +00001873 if (feature_split_evtchn)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001874 err = setup_netfront_split(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001875 /* setup single event channel if
1876 * a) feature-split-event-channels == 0
1877 * b) feature-split-event-channels == 1 but failed to setup
1878 */
Jiapeng Chonge93fac32021-02-02 18:17:49 +08001879 if (!feature_split_evtchn || err)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001880 err = setup_netfront_single(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001881
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001882 if (err)
Wei Liu1ca29832013-05-20 01:05:12 +00001883 goto alloc_evtchn_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001884
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001885 return 0;
1886
Wei Liu1ca29832013-05-20 01:05:12 +00001887 /* If we fail to setup netfront, it is safe to just revoke access to
1888 * granted pages because backend is not accessing it at this point.
1889 */
Wei Liu1ca29832013-05-20 01:05:12 +00001890alloc_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001891 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001892grant_rx_ring_fail:
1893 free_page((unsigned long)rxs);
1894alloc_rx_ring_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001895 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001896grant_tx_ring_fail:
1897 free_page((unsigned long)txs);
1898fail:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001899 return err;
1900}
1901
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001902/* Queue-specific initialisation
1903 * This used to be done in xennet_create_dev() but must now
1904 * be run per-queue.
1905 */
1906static int xennet_init_queue(struct netfront_queue *queue)
1907{
1908 unsigned short i;
1909 int err = 0;
Xiao Liang21f27062018-08-14 23:21:28 +08001910 char *devid;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001911
1912 spin_lock_init(&queue->tx_lock);
1913 spin_lock_init(&queue->rx_lock);
1914
Kees Cooke99e88a2017-10-16 14:43:17 -07001915 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001916
Xiao Liang21f27062018-08-14 23:21:28 +08001917 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1918 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1919 devid, queue->id);
Wei Liu8b715012014-06-04 10:30:47 +01001920
Juergen Gross21631d22021-08-24 12:28:08 +02001921 /* Initialise tx_skb_freelist as a free chain containing every entry. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001922 queue->tx_skb_freelist = 0;
Juergen Grossa884daa2021-08-24 12:28:09 +02001923 queue->tx_pend_queue = TX_LINK_NONE;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001924 for (i = 0; i < NET_TX_RING_SIZE; i++) {
Juergen Gross21631d22021-08-24 12:28:08 +02001925 queue->tx_link[i] = i + 1;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001926 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1927 queue->grant_tx_page[i] = NULL;
1928 }
Juergen Gross21631d22021-08-24 12:28:08 +02001929 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001930
1931 /* Clear out rx_skbs */
1932 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1933 queue->rx_skbs[i] = NULL;
1934 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1935 }
1936
1937 /* A grant for every tx ring slot */
David Vrabel1f3c2eb2014-10-22 11:17:06 +01001938 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001939 &queue->gref_tx_head) < 0) {
1940 pr_alert("can't alloc tx grant refs\n");
1941 err = -ENOMEM;
1942 goto exit;
1943 }
1944
1945 /* A grant for every rx ring slot */
David Vrabel1f3c2eb2014-10-22 11:17:06 +01001946 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001947 &queue->gref_rx_head) < 0) {
1948 pr_alert("can't alloc rx grant refs\n");
1949 err = -ENOMEM;
1950 goto exit_free_tx;
1951 }
1952
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001953 return 0;
1954
1955 exit_free_tx:
1956 gnttab_free_grant_references(queue->gref_tx_head);
1957 exit:
1958 return err;
1959}
1960
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001961static int write_queue_xenstore_keys(struct netfront_queue *queue,
1962 struct xenbus_transaction *xbt, int write_hierarchical)
1963{
1964 /* Write the queue-specific keys into XenStore in the traditional
1965 * way for a single queue, or in a queue subkeys for multiple
1966 * queues.
1967 */
1968 struct xenbus_device *dev = queue->info->xbdev;
1969 int err;
1970 const char *message;
1971 char *path;
1972 size_t pathsize;
1973
1974 /* Choose the correct place to write the keys */
1975 if (write_hierarchical) {
1976 pathsize = strlen(dev->nodename) + 10;
1977 path = kzalloc(pathsize, GFP_KERNEL);
1978 if (!path) {
1979 err = -ENOMEM;
1980 message = "out of memory while writing ring references";
1981 goto error;
1982 }
1983 snprintf(path, pathsize, "%s/queue-%u",
1984 dev->nodename, queue->id);
1985 } else {
1986 path = (char *)dev->nodename;
1987 }
1988
1989 /* Write ring references */
1990 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1991 queue->tx_ring_ref);
1992 if (err) {
1993 message = "writing tx-ring-ref";
1994 goto error;
1995 }
1996
1997 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1998 queue->rx_ring_ref);
1999 if (err) {
2000 message = "writing rx-ring-ref";
2001 goto error;
2002 }
2003
2004 /* Write event channels; taking into account both shared
2005 * and split event channel scenarios.
2006 */
2007 if (queue->tx_evtchn == queue->rx_evtchn) {
2008 /* Shared event channel */
2009 err = xenbus_printf(*xbt, path,
2010 "event-channel", "%u", queue->tx_evtchn);
2011 if (err) {
2012 message = "writing event-channel";
2013 goto error;
2014 }
2015 } else {
2016 /* Split event channels */
2017 err = xenbus_printf(*xbt, path,
2018 "event-channel-tx", "%u", queue->tx_evtchn);
2019 if (err) {
2020 message = "writing event-channel-tx";
2021 goto error;
2022 }
2023
2024 err = xenbus_printf(*xbt, path,
2025 "event-channel-rx", "%u", queue->rx_evtchn);
2026 if (err) {
2027 message = "writing event-channel-rx";
2028 goto error;
2029 }
2030 }
2031
2032 if (write_hierarchical)
2033 kfree(path);
2034 return 0;
2035
2036error:
2037 if (write_hierarchical)
2038 kfree(path);
2039 xenbus_dev_fatal(dev, err, "%s", message);
2040 return err;
2041}
2042
David Vrabelce587252014-06-18 10:47:28 +01002043static void xennet_destroy_queues(struct netfront_info *info)
2044{
2045 unsigned int i;
2046
David Vrabelce587252014-06-18 10:47:28 +01002047 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
2048 struct netfront_queue *queue = &info->queues[i];
2049
2050 if (netif_running(info->netdev))
2051 napi_disable(&queue->napi);
2052 netif_napi_del(&queue->napi);
2053 }
2054
David Vrabelce587252014-06-18 10:47:28 +01002055 kfree(info->queues);
2056 info->queues = NULL;
2057}
2058
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002059
2060
2061static int xennet_create_page_pool(struct netfront_queue *queue)
2062{
2063 int err;
2064 struct page_pool_params pp_params = {
2065 .order = 0,
2066 .flags = 0,
2067 .pool_size = NET_RX_RING_SIZE,
2068 .nid = NUMA_NO_NODE,
2069 .dev = &queue->info->netdev->dev,
2070 .offset = XDP_PACKET_HEADROOM,
2071 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2072 };
2073
2074 queue->page_pool = page_pool_create(&pp_params);
2075 if (IS_ERR(queue->page_pool)) {
2076 err = PTR_ERR(queue->page_pool);
2077 queue->page_pool = NULL;
2078 return err;
2079 }
2080
2081 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
Björn Töpelb02e5a02020-11-30 19:52:01 +01002082 queue->id, 0);
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002083 if (err) {
2084 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2085 goto err_free_pp;
2086 }
2087
2088 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2089 MEM_TYPE_PAGE_POOL, queue->page_pool);
2090 if (err) {
2091 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2092 goto err_unregister_rxq;
2093 }
2094 return 0;
2095
2096err_unregister_rxq:
2097 xdp_rxq_info_unreg(&queue->xdp_rxq);
2098err_free_pp:
2099 page_pool_destroy(queue->page_pool);
2100 queue->page_pool = NULL;
2101 return err;
2102}
2103
David Vrabelce587252014-06-18 10:47:28 +01002104static int xennet_create_queues(struct netfront_info *info,
Joe Jinca88ea12015-10-19 13:37:17 +08002105 unsigned int *num_queues)
David Vrabelce587252014-06-18 10:47:28 +01002106{
2107 unsigned int i;
2108 int ret;
2109
Joe Jinca88ea12015-10-19 13:37:17 +08002110 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
David Vrabelce587252014-06-18 10:47:28 +01002111 GFP_KERNEL);
2112 if (!info->queues)
2113 return -ENOMEM;
2114
Joe Jinca88ea12015-10-19 13:37:17 +08002115 for (i = 0; i < *num_queues; i++) {
David Vrabelce587252014-06-18 10:47:28 +01002116 struct netfront_queue *queue = &info->queues[i];
2117
2118 queue->id = i;
2119 queue->info = info;
2120
2121 ret = xennet_init_queue(queue);
2122 if (ret < 0) {
Ross Lagerwallf599c642018-01-11 09:36:38 +00002123 dev_warn(&info->xbdev->dev,
David Vrabel69cb8522014-07-31 17:38:24 +01002124 "only created %d queues\n", i);
Joe Jinca88ea12015-10-19 13:37:17 +08002125 *num_queues = i;
David Vrabelce587252014-06-18 10:47:28 +01002126 break;
2127 }
2128
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002129 /* use page pool recycling instead of buddy allocator */
2130 ret = xennet_create_page_pool(queue);
2131 if (ret < 0) {
2132 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2133 *num_queues = i;
2134 return ret;
2135 }
2136
David Vrabelce587252014-06-18 10:47:28 +01002137 netif_napi_add(queue->info->netdev, &queue->napi,
2138 xennet_poll, 64);
2139 if (netif_running(info->netdev))
2140 napi_enable(&queue->napi);
2141 }
2142
Joe Jinca88ea12015-10-19 13:37:17 +08002143 netif_set_real_num_tx_queues(info->netdev, *num_queues);
David Vrabelce587252014-06-18 10:47:28 +01002144
Joe Jinca88ea12015-10-19 13:37:17 +08002145 if (*num_queues == 0) {
Ross Lagerwallf599c642018-01-11 09:36:38 +00002146 dev_err(&info->xbdev->dev, "no queues\n");
David Vrabelce587252014-06-18 10:47:28 +01002147 return -EINVAL;
2148 }
2149 return 0;
2150}
2151
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002152/* Common code used when first setting up, and when resuming. */
Ian Campbellf502bf22010-08-18 23:27:49 +00002153static int talk_to_netback(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002154 struct netfront_info *info)
2155{
2156 const char *message;
2157 struct xenbus_transaction xbt;
2158 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002159 unsigned int feature_split_evtchn;
2160 unsigned int i = 0;
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002161 unsigned int max_queues = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002162 struct netfront_queue *queue = NULL;
2163 unsigned int num_queues = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002164
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002165 info->netdev->irq = 0;
2166
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002167 /* Check if backend supports multiple queues */
Juergen Gross2890ea52016-10-31 14:58:41 +01002168 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2169 "multi-queue-max-queues", 1);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002170 num_queues = min(max_queues, xennet_max_queues);
2171
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002172 /* Check feature-split-event-channels */
Juergen Gross2890ea52016-10-31 14:58:41 +01002173 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2174 "feature-split-event-channels", 0);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002175
2176 /* Read mac addr. */
2177 err = xen_net_read_mac(dev, info->netdev->dev_addr);
2178 if (err) {
2179 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
Ross Lagerwallcb257782018-06-21 14:00:20 +01002180 goto out_unlocked;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002181 }
2182
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002183 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2184 "feature-xdp-headroom", 0);
2185 if (info->netback_has_xdp_headroom) {
2186 /* set the current xen-netfront xdp state */
2187 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2188 NETBACK_XDP_HEADROOM_ENABLE :
2189 NETBACK_XDP_HEADROOM_DISABLE);
2190 if (err)
2191 goto out_unlocked;
2192 }
2193
Ross Lagerwallf599c642018-01-11 09:36:38 +00002194 rtnl_lock();
David Vrabelce587252014-06-18 10:47:28 +01002195 if (info->queues)
2196 xennet_destroy_queues(info);
2197
Juergen Grossa884daa2021-08-24 12:28:09 +02002198 /* For the case of a reconnect reset the "broken" indicator. */
2199 info->broken = false;
2200
Joe Jinca88ea12015-10-19 13:37:17 +08002201 err = xennet_create_queues(info, &num_queues);
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00002202 if (err < 0) {
2203 xenbus_dev_fatal(dev, err, "creating queues");
2204 kfree(info->queues);
2205 info->queues = NULL;
2206 goto out;
2207 }
Ross Lagerwallf599c642018-01-11 09:36:38 +00002208 rtnl_unlock();
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002209
2210 /* Create shared ring, alloc event channel -- for each queue */
2211 for (i = 0; i < num_queues; ++i) {
2212 queue = &info->queues[i];
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002213 err = setup_netfront(dev, queue, feature_split_evtchn);
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00002214 if (err)
2215 goto destroy_ring;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002216 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002217
2218again:
2219 err = xenbus_transaction_start(&xbt);
2220 if (err) {
2221 xenbus_dev_fatal(dev, err, "starting transaction");
2222 goto destroy_ring;
2223 }
2224
chas williams812494d2015-09-16 16:28:25 -04002225 if (xenbus_exists(XBT_NIL,
2226 info->xbdev->otherend, "multi-queue-max-queues")) {
2227 /* Write the number of queues */
2228 err = xenbus_printf(xbt, dev->nodename,
2229 "multi-queue-num-queues", "%u", num_queues);
2230 if (err) {
2231 message = "writing multi-queue-num-queues";
2232 goto abort_transaction_no_dev_fatal;
2233 }
2234 }
2235
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002236 if (num_queues == 1) {
2237 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2238 if (err)
2239 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00002240 } else {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002241 /* Write the keys for each queue */
2242 for (i = 0; i < num_queues; ++i) {
2243 queue = &info->queues[i];
2244 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2245 if (err)
2246 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00002247 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002248 }
2249
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002250 /* The remaining keys are not queue-specific */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002251 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2252 1);
2253 if (err) {
2254 message = "writing request-rx-copy";
2255 goto abort_transaction;
2256 }
2257
2258 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2259 if (err) {
2260 message = "writing feature-rx-notify";
2261 goto abort_transaction;
2262 }
2263
2264 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2265 if (err) {
2266 message = "writing feature-sg";
2267 goto abort_transaction;
2268 }
2269
2270 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2271 if (err) {
2272 message = "writing feature-gso-tcpv4";
2273 goto abort_transaction;
2274 }
2275
Paul Durrant2c0057d2014-01-15 17:30:33 +00002276 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2277 if (err) {
2278 message = "writing feature-gso-tcpv6";
2279 goto abort_transaction;
2280 }
2281
2282 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2283 "1");
2284 if (err) {
2285 message = "writing feature-ipv6-csum-offload";
2286 goto abort_transaction;
2287 }
2288
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002289 err = xenbus_transaction_end(xbt, 0);
2290 if (err) {
2291 if (err == -EAGAIN)
2292 goto again;
2293 xenbus_dev_fatal(dev, err, "completing transaction");
2294 goto destroy_ring;
2295 }
2296
2297 return 0;
2298
2299 abort_transaction:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002300 xenbus_dev_fatal(dev, err, "%s", message);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002301abort_transaction_no_dev_fatal:
2302 xenbus_transaction_end(xbt, 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002303 destroy_ring:
2304 xennet_disconnect_backend(info);
Ross Lagerwallf599c642018-01-11 09:36:38 +00002305 rtnl_lock();
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00002306 xennet_destroy_queues(info);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002307 out:
Ross Lagerwallf599c642018-01-11 09:36:38 +00002308 rtnl_unlock();
Ross Lagerwallcb257782018-06-21 14:00:20 +01002309out_unlocked:
Vitaly Kuznetsovd86b5672017-05-11 13:58:06 +02002310 device_unregister(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002311 return err;
2312}
2313
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002314static int xennet_connect(struct net_device *dev)
2315{
2316 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002317 unsigned int num_queues = 0;
David Vrabela5b5dc32014-07-31 17:38:23 +01002318 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002319 unsigned int j = 0;
2320 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002321
Juergen Gross2890ea52016-10-31 14:58:41 +01002322 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002323 dev_info(&dev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07002324 "backend does not support copying receive path\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002325 return -ENODEV;
2326 }
2327
Ian Campbellf502bf22010-08-18 23:27:49 +00002328 err = talk_to_netback(np->xbdev, np);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002329 if (err)
2330 return err;
Denis Kirjanov6c5aa6f2020-06-29 16:13:28 +03002331 if (np->netback_has_xdp_headroom)
2332 pr_info("backend supports XDP headroom\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002333
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002334 /* talk_to_netback() sets the correct number of queues */
2335 num_queues = dev->real_num_tx_queues;
2336
Ross Lagerwallf599c642018-01-11 09:36:38 +00002337 if (dev->reg_state == NETREG_UNINITIALIZED) {
2338 err = register_netdev(dev);
2339 if (err) {
2340 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2341 device_unregister(&np->xbdev->dev);
2342 return err;
2343 }
2344 }
2345
Ross Lagerwall45c81842018-06-21 14:00:21 +01002346 rtnl_lock();
2347 netdev_update_features(dev);
2348 rtnl_unlock();
2349
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002350 /*
David Vrabela5b5dc32014-07-31 17:38:23 +01002351 * All public and private state should now be sane. Get
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002352 * ready to start sending and receiving packets and give the driver
2353 * domain a kick because we've probably just requeued some
2354 * packets.
2355 */
Dongli Zhang042b2042021-10-22 16:31:39 -07002356 netif_tx_lock_bh(np->netdev);
2357 netif_device_attach(np->netdev);
2358 netif_tx_unlock_bh(np->netdev);
2359
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002360 netif_carrier_on(np->netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002361 for (j = 0; j < num_queues; ++j) {
2362 queue = &np->queues[j];
David Vrabelf50b4072014-07-02 16:09:14 +01002363
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002364 notify_remote_via_irq(queue->tx_irq);
2365 if (queue->tx_irq != queue->rx_irq)
2366 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002367
David Vrabelf50b4072014-07-02 16:09:14 +01002368 spin_lock_irq(&queue->tx_lock);
2369 xennet_tx_buf_gc(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002370 spin_unlock_irq(&queue->tx_lock);
David Vrabelf50b4072014-07-02 16:09:14 +01002371
2372 spin_lock_bh(&queue->rx_lock);
2373 xennet_alloc_rx_buffers(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002374 spin_unlock_bh(&queue->rx_lock);
2375 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002376
2377 return 0;
2378}
2379
Lee Jones80708602021-01-15 20:09:03 +00002380/*
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002381 * Callback received when the backend's state changes.
2382 */
Ian Campbellf502bf22010-08-18 23:27:49 +00002383static void netback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002384 enum xenbus_state backend_state)
2385{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002386 struct netfront_info *np = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002387 struct net_device *netdev = np->netdev;
2388
2389 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2390
Juergen Gross8edfe2e2018-09-07 14:21:30 +02002391 wake_up_all(&module_wq);
2392
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002393 switch (backend_state) {
2394 case XenbusStateInitialising:
2395 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002396 case XenbusStateReconfiguring:
2397 case XenbusStateReconfigured:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002398 case XenbusStateUnknown:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002399 break;
2400
2401 case XenbusStateInitWait:
2402 if (dev->state != XenbusStateInitialising)
2403 break;
2404 if (xennet_connect(netdev) != 0)
2405 break;
2406 xenbus_switch_state(dev, XenbusStateConnected);
Laszlo Ersek08e34eb2011-12-11 01:48:59 +00002407 break;
2408
2409 case XenbusStateConnected:
Amerigo Wangee89bab2012-08-09 22:14:56 +00002410 netdev_notify_peers(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002411 break;
2412
David Vrabelbce3ea82014-02-04 18:50:26 +00002413 case XenbusStateClosed:
2414 if (dev->state == XenbusStateClosed)
2415 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002416 fallthrough; /* Missed the backend's CLOSING state */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002417 case XenbusStateClosing:
2418 xenbus_frontend_closed(dev);
2419 break;
2420 }
2421}
2422
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002423static const struct xennet_stat {
2424 char name[ETH_GSTRING_LEN];
2425 u16 offset;
2426} xennet_stats[] = {
2427 {
2428 "rx_gso_checksum_fixup",
2429 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2430 },
2431};
2432
2433static int xennet_get_sset_count(struct net_device *dev, int string_set)
2434{
2435 switch (string_set) {
2436 case ETH_SS_STATS:
2437 return ARRAY_SIZE(xennet_stats);
2438 default:
2439 return -EINVAL;
2440 }
2441}
2442
2443static void xennet_get_ethtool_stats(struct net_device *dev,
2444 struct ethtool_stats *stats, u64 * data)
2445{
2446 void *np = netdev_priv(dev);
2447 int i;
2448
2449 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002450 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002451}
2452
2453static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2454{
2455 int i;
2456
2457 switch (stringset) {
2458 case ETH_SS_STATS:
2459 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2460 memcpy(data + i * ETH_GSTRING_LEN,
2461 xennet_stats[i].name, ETH_GSTRING_LEN);
2462 break;
2463 }
2464}
2465
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002466static const struct ethtool_ops xennet_ethtool_ops =
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002467{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002468 .get_link = ethtool_op_get_link,
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002469
2470 .get_sset_count = xennet_get_sset_count,
2471 .get_ethtool_stats = xennet_get_ethtool_stats,
2472 .get_strings = xennet_get_strings,
Daniel Drown91ffb9d2020-07-03 01:22:34 -05002473 .get_ts_info = ethtool_op_get_ts_info,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002474};
2475
2476#ifdef CONFIG_SYSFS
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002477static ssize_t show_rxbuf(struct device *dev,
2478 struct device_attribute *attr, char *buf)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002479{
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002480 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002481}
2482
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002483static ssize_t store_rxbuf(struct device *dev,
2484 struct device_attribute *attr,
2485 const char *buf, size_t len)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002486{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002487 char *endp;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002488
2489 if (!capable(CAP_NET_ADMIN))
2490 return -EPERM;
2491
Andrew Lunn8ed7ec12020-10-31 19:04:35 +01002492 simple_strtoul(buf, &endp, 0);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002493 if (endp == buf)
2494 return -EBADMSG;
2495
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002496 /* rxbuf_min and rxbuf_max are no longer configurable. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002497
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002498 return len;
2499}
2500
Joe Perchesd61e4032018-03-23 15:54:39 -07002501static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2502static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2503static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
Takashi Iwai27b917e2015-02-04 14:38:55 +01002504
2505static struct attribute *xennet_dev_attrs[] = {
2506 &dev_attr_rxbuf_min.attr,
2507 &dev_attr_rxbuf_max.attr,
2508 &dev_attr_rxbuf_cur.attr,
2509 NULL
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002510};
2511
Takashi Iwai27b917e2015-02-04 14:38:55 +01002512static const struct attribute_group xennet_dev_group = {
2513 .attrs = xennet_dev_attrs
2514};
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002515#endif /* CONFIG_SYSFS */
2516
Andrea Righic2c63312020-07-24 10:59:10 +02002517static void xennet_bus_close(struct xenbus_device *dev)
2518{
2519 int ret;
2520
2521 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2522 return;
2523 do {
2524 xenbus_switch_state(dev, XenbusStateClosing);
2525 ret = wait_event_timeout(module_wq,
2526 xenbus_read_driver_state(dev->otherend) ==
2527 XenbusStateClosing ||
2528 xenbus_read_driver_state(dev->otherend) ==
2529 XenbusStateClosed ||
2530 xenbus_read_driver_state(dev->otherend) ==
2531 XenbusStateUnknown,
2532 XENNET_TIMEOUT);
2533 } while (!ret);
2534
2535 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2536 return;
2537
2538 do {
2539 xenbus_switch_state(dev, XenbusStateClosed);
2540 ret = wait_event_timeout(module_wq,
2541 xenbus_read_driver_state(dev->otherend) ==
2542 XenbusStateClosed ||
2543 xenbus_read_driver_state(dev->otherend) ==
2544 XenbusStateUnknown,
2545 XENNET_TIMEOUT);
2546 } while (!ret);
2547}
2548
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002549static int xennet_remove(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002550{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002551 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002552
Andrea Righic2c63312020-07-24 10:59:10 +02002553 xennet_bus_close(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002554 xennet_disconnect_backend(info);
2555
Ross Lagerwallf599c642018-01-11 09:36:38 +00002556 if (info->netdev->reg_state == NETREG_REGISTERED)
2557 unregister_netdev(info->netdev);
Ian Campbell6bc96d02012-06-25 22:48:41 +00002558
Ross Lagerwallf599c642018-01-11 09:36:38 +00002559 if (info->queues) {
2560 rtnl_lock();
Chas Williams9a873c72015-08-19 19:14:20 -04002561 xennet_destroy_queues(info);
Ross Lagerwallf599c642018-01-11 09:36:38 +00002562 rtnl_unlock();
2563 }
David Vrabel900e1832015-01-13 16:42:42 +00002564 xennet_free_netdev(info->netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002565
2566 return 0;
2567}
2568
David Vrabel95afae42014-09-08 17:30:41 +01002569static const struct xenbus_device_id netfront_ids[] = {
2570 { "vif" },
2571 { "" }
2572};
2573
2574static struct xenbus_driver netfront_driver = {
2575 .ids = netfront_ids,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002576 .probe = netfront_probe,
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002577 .remove = xennet_remove,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002578 .resume = netfront_resume,
Ian Campbellf502bf22010-08-18 23:27:49 +00002579 .otherend_changed = netback_changed,
David Vrabel95afae42014-09-08 17:30:41 +01002580};
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002581
2582static int __init netif_init(void)
2583{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002584 if (!xen_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002585 return -ENODEV;
2586
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002587 if (!xen_has_pv_nic_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002588 return -ENODEV;
2589
Joe Perches383eda32013-06-27 21:57:49 -07002590 pr_info("Initialising Xen virtual ethernet driver\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002591
Juergen Gross034702a2017-01-10 14:32:51 +01002592 /* Allow as many queues as there are CPUs inut max. 8 if user has not
Wei Liu32a84402015-09-10 11:18:58 +01002593 * specified a value.
2594 */
2595 if (xennet_max_queues == 0)
Juergen Gross034702a2017-01-10 14:32:51 +01002596 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2597 num_online_cpus());
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002598
Al Viroffb78a22008-11-22 17:38:14 +00002599 return xenbus_register_frontend(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002600}
2601module_init(netif_init);
2602
2603
2604static void __exit netif_exit(void)
2605{
Al Viroffb78a22008-11-22 17:38:14 +00002606 xenbus_unregister_driver(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002607}
2608module_exit(netif_exit);
2609
2610MODULE_DESCRIPTION("Xen virtual network device frontend");
2611MODULE_LICENSE("GPL");
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002612MODULE_ALIAS("xen:vif");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002613MODULE_ALIAS("xennet");