blob: 88280057e032175c125f2073c89cc8e940ff97fd [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>
47
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070048#include <xen/xen.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070049#include <xen/xenbus.h>
50#include <xen/events.h>
51#include <xen/page.h>
Igor Mammedovb9136d22012-03-21 15:08:38 +010052#include <xen/platform_pci.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070053#include <xen/grant_table.h>
54
55#include <xen/interface/io/netif.h>
56#include <xen/interface/memory.h>
57#include <xen/interface/grant_table.h>
58
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +010059/* Module parameters */
Juergen Gross034702a2017-01-10 14:32:51 +010060#define MAX_QUEUES_DEFAULT 8
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +010061static unsigned int xennet_max_queues;
62module_param_named(max_queues, xennet_max_queues, uint, 0644);
63MODULE_PARM_DESC(max_queues,
64 "Maximum number of queues per virtual interface");
65
Andrea Righic2c63312020-07-24 10:59:10 +020066#define XENNET_TIMEOUT (5 * HZ)
67
Stephen Hemminger0fc0b732009-09-02 01:03:33 -070068static const struct ethtool_ops xennet_ethtool_ops;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070069
70struct netfront_cb {
Ian Campbell36832432012-08-22 00:26:47 +000071 int pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070072};
73
74#define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
75
76#define RX_COPY_THRESHOLD 256
77
78#define GRANT_INVALID_REF 0
79
Julien Grall30c5d7f2015-04-10 14:42:21 +010080#define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
81#define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
David Vrabel1f3c2eb2014-10-22 11:17:06 +010082
83/* Minimum number of Rx slots (includes slot for GSO metadata). */
84#define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070085
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +010086/* Queue name is interface name with "-qNNN" appended */
87#define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
88
89/* IRQ name is queue name with "-tx" or "-rx" appended */
90#define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
91
Juergen Gross8edfe2e2018-09-07 14:21:30 +020092static DECLARE_WAIT_QUEUE_HEAD(module_wq);
Eduardo Otubo5b5971d2017-11-23 15:18:35 +010093
stephen hemmingere00f85b2011-06-21 05:35:31 +000094struct netfront_stats {
David Vrabel900e1832015-01-13 16:42:42 +000095 u64 packets;
96 u64 bytes;
stephen hemmingere00f85b2011-06-21 05:35:31 +000097 struct u64_stats_sync syncp;
98};
99
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100100struct netfront_info;
101
102struct netfront_queue {
103 unsigned int id; /* Queue ID, 0-based */
104 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
105 struct netfront_info *info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700106
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700107 struct napi_struct napi;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700108
Wei Liud634bf22013-05-22 06:34:46 +0000109 /* Split event channels support, tx_* == rx_* when using
110 * single event channel.
111 */
112 unsigned int tx_evtchn, rx_evtchn;
113 unsigned int tx_irq, rx_irq;
114 /* Only used when split event channels support is enabled */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100115 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
116 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700117
118 spinlock_t tx_lock;
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700119 struct xen_netif_tx_front_ring tx;
120 int tx_ring_ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700121
122 /*
123 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
124 * are linked from tx_skb_freelist through skb_entry.link.
125 *
126 * NB. Freelist index entries are always going to be less than
127 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
128 * greater than PAGE_OFFSET: we use this property to distinguish
129 * them.
130 */
131 union skb_entry {
132 struct sk_buff *skb;
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700133 unsigned long link;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700134 } tx_skbs[NET_TX_RING_SIZE];
135 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;
139
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700140 spinlock_t rx_lock ____cacheline_aligned_in_smp;
141 struct xen_netif_rx_front_ring rx;
142 int rx_ring_ref;
143
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700144 struct timer_list rx_refill_timer;
145
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700146 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
147 grant_ref_t gref_rx_head;
148 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100149};
150
151struct netfront_info {
152 struct list_head list;
153 struct net_device *netdev;
154
155 struct xenbus_device *xbdev;
156
157 /* Multi-queue support */
158 struct netfront_queue *queues;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000159
160 /* Statistics */
David Vrabel900e1832015-01-13 16:42:42 +0000161 struct netfront_stats __percpu *rx_stats;
162 struct netfront_stats __percpu *tx_stats;
stephen hemmingere00f85b2011-06-21 05:35:31 +0000163
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100164 atomic_t rx_gso_checksum_fixup;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700165};
166
167struct netfront_rx_info {
168 struct xen_netif_rx_response rx;
169 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
170};
171
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700172static void skb_entry_set_link(union skb_entry *list, unsigned short id)
173{
174 list->link = id;
175}
176
177static int skb_entry_is_link(const union skb_entry *list)
178{
179 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
Eric Dumazet807540b2010-09-23 05:40:09 +0000180 return (unsigned long)list->skb < PAGE_OFFSET;
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700181}
182
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700183/*
184 * Access macros for acquiring freeing slots in tx_skbs[].
185 */
186
187static void add_id_to_freelist(unsigned *head, union skb_entry *list,
188 unsigned short id)
189{
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700190 skb_entry_set_link(&list[id], *head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700191 *head = id;
192}
193
194static unsigned short get_id_from_freelist(unsigned *head,
195 union skb_entry *list)
196{
197 unsigned int id = *head;
198 *head = list[id].link;
199 return id;
200}
201
202static int xennet_rxidx(RING_IDX idx)
203{
204 return idx & (NET_RX_RING_SIZE - 1);
205}
206
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100207static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700208 RING_IDX ri)
209{
210 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100211 struct sk_buff *skb = queue->rx_skbs[i];
212 queue->rx_skbs[i] = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700213 return skb;
214}
215
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100216static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700217 RING_IDX ri)
218{
219 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100220 grant_ref_t ref = queue->grant_rx_ref[i];
221 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700222 return ref;
223}
224
225#ifdef CONFIG_SYSFS
Takashi Iwai27b917e2015-02-04 14:38:55 +0100226static const struct attribute_group xennet_dev_group;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700227#endif
228
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000229static bool xennet_can_sg(struct net_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700230{
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000231 return dev->features & NETIF_F_SG;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700232}
233
234
Kees Cooke99e88a2017-10-16 14:43:17 -0700235static void rx_refill_timeout(struct timer_list *t)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700236{
Kees Cooke99e88a2017-10-16 14:43:17 -0700237 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100238 napi_schedule(&queue->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700239}
240
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100241static int netfront_tx_slot_available(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700242{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100243 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
Juergen Gross57f230a2018-06-12 08:57:53 +0200244 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700245}
246
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100247static void xennet_maybe_wake_tx(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700248{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100249 struct net_device *dev = queue->info->netdev;
250 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700251
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100252 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
253 netfront_tx_slot_available(queue) &&
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700254 likely(netif_running(dev)))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100255 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700256}
257
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100258
259static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700260{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700261 struct sk_buff *skb;
262 struct page *page;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100263
264 skb = __netdev_alloc_skb(queue->info->netdev,
265 RX_COPY_THRESHOLD + NET_IP_ALIGN,
266 GFP_ATOMIC | __GFP_NOWARN);
267 if (unlikely(!skb))
268 return NULL;
269
270 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
271 if (!page) {
272 kfree_skb(skb);
273 return NULL;
274 }
275 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
276
277 /* Align ip header to a 16 bytes boundary */
278 skb_reserve(skb, NET_IP_ALIGN);
279 skb->dev = queue->info->netdev;
280
281 return skb;
282}
283
284
285static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
286{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100287 RING_IDX req_prod = queue->rx.req_prod_pvt;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100288 int notify;
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000289 int err = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700290
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100291 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700292 return;
293
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100294 for (req_prod = queue->rx.req_prod_pvt;
295 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
296 req_prod++) {
297 struct sk_buff *skb;
298 unsigned short id;
299 grant_ref_t ref;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100300 struct page *page;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100301 struct xen_netif_rx_request *req;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700302
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100303 skb = xennet_alloc_one_rx_buffer(queue);
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000304 if (!skb) {
305 err = -ENOMEM;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700306 break;
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000307 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700308
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100309 id = xennet_rxidx(req_prod);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700310
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100311 BUG_ON(queue->rx_skbs[id]);
312 queue->rx_skbs[id] = skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700313
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100314 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
Dongli Zhang269ebce2016-11-02 09:04:33 +0800315 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100316 queue->grant_rx_ref[id] = ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700317
Julien Grall30c5d7f2015-04-10 14:42:21 +0100318 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700319
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100320 req = RING_GET_REQUEST(&queue->rx, req_prod);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100321 gnttab_page_grant_foreign_access_ref_one(ref,
322 queue->info->xbdev->otherend_id,
323 page,
324 0);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700325 req->id = id;
326 req->gref = ref;
327 }
328
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100329 queue->rx.req_prod_pvt = req_prod;
330
Vineeth Remanan Pillai538d9292017-02-07 18:59:01 +0000331 /* Try again later if there are not enough requests or skb allocation
332 * failed.
333 * Enough requests is quantified as the sum of newly created slots and
334 * the unconsumed slots at the backend.
335 */
336 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
337 unlikely(err)) {
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100338 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
339 return;
340 }
341
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100342 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700343 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100344 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700345}
346
347static int xennet_open(struct net_device *dev)
348{
349 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100350 unsigned int num_queues = dev->real_num_tx_queues;
351 unsigned int i = 0;
352 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700353
Ross Lagerwallf599c642018-01-11 09:36:38 +0000354 if (!np->queues)
355 return -ENODEV;
356
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100357 for (i = 0; i < num_queues; ++i) {
358 queue = &np->queues[i];
359 napi_enable(&queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700360
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100361 spin_lock_bh(&queue->rx_lock);
362 if (netif_carrier_ok(dev)) {
363 xennet_alloc_rx_buffers(queue);
364 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
365 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
366 napi_schedule(&queue->napi);
367 }
368 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700369 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700370
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100371 netif_tx_start_all_queues(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700372
373 return 0;
374}
375
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100376static void xennet_tx_buf_gc(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700377{
378 RING_IDX cons, prod;
379 unsigned short id;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700380 struct sk_buff *skb;
Malcolm Crossley7d0105b2016-01-26 17:12:44 +0000381 bool more_to_do;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700382
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100383 BUG_ON(!netif_carrier_ok(queue->info->netdev));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700384
385 do {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100386 prod = queue->tx.sring->rsp_prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700387 rmb(); /* Ensure we see responses up to 'rp'. */
388
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100389 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700390 struct xen_netif_tx_response *txrsp;
391
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100392 txrsp = RING_GET_RESPONSE(&queue->tx, cons);
Ian Campbellf942dc22011-03-15 00:06:18 +0000393 if (txrsp->status == XEN_NETIF_RSP_NULL)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700394 continue;
395
396 id = txrsp->id;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100397 skb = queue->tx_skbs[id].skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700398 if (unlikely(gnttab_query_foreign_access(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100399 queue->grant_tx_ref[id]) != 0)) {
Joe Perches383eda32013-06-27 21:57:49 -0700400 pr_alert("%s: warning -- grant still in use by backend domain\n",
401 __func__);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700402 BUG();
403 }
404 gnttab_end_foreign_access_ref(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100405 queue->grant_tx_ref[id], GNTMAP_readonly);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700406 gnttab_release_grant_reference(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100407 &queue->gref_tx_head, queue->grant_tx_ref[id]);
408 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
409 queue->grant_tx_page[id] = NULL;
410 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700411 dev_kfree_skb_irq(skb);
412 }
413
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100414 queue->tx.rsp_cons = prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700415
Malcolm Crossley7d0105b2016-01-26 17:12:44 +0000416 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
417 } while (more_to_do);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700418
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100419 xennet_maybe_wake_tx(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700420}
421
Julien Grall30c5d7f2015-04-10 14:42:21 +0100422struct xennet_gnttab_make_txreq {
423 struct netfront_queue *queue;
424 struct sk_buff *skb;
425 struct page *page;
426 struct xen_netif_tx_request *tx; /* Last request */
427 unsigned int size;
428};
429
430static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
431 unsigned int len, void *data)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700432{
Julien Grall30c5d7f2015-04-10 14:42:21 +0100433 struct xennet_gnttab_make_txreq *info = data;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700434 unsigned int id;
David Vrabela55e8bb2015-01-13 17:16:44 +0000435 struct xen_netif_tx_request *tx;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700436 grant_ref_t ref;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100437 /* convenient aliases */
438 struct page *page = info->page;
439 struct netfront_queue *queue = info->queue;
440 struct sk_buff *skb = info->skb;
David Vrabela55e8bb2015-01-13 17:16:44 +0000441
442 id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
443 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
444 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
Dongli Zhang269ebce2016-11-02 09:04:33 +0800445 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
David Vrabela55e8bb2015-01-13 17:16:44 +0000446
Julien Grall30c5d7f2015-04-10 14:42:21 +0100447 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
448 gfn, GNTMAP_readonly);
David Vrabela55e8bb2015-01-13 17:16:44 +0000449
450 queue->tx_skbs[id].skb = skb;
451 queue->grant_tx_page[id] = page;
452 queue->grant_tx_ref[id] = ref;
453
454 tx->id = id;
455 tx->gref = ref;
456 tx->offset = offset;
457 tx->size = len;
458 tx->flags = 0;
459
Julien Grall30c5d7f2015-04-10 14:42:21 +0100460 info->tx = tx;
461 info->size += tx->size;
462}
463
464static struct xen_netif_tx_request *xennet_make_first_txreq(
465 struct netfront_queue *queue, struct sk_buff *skb,
466 struct page *page, unsigned int offset, unsigned int len)
467{
468 struct xennet_gnttab_make_txreq info = {
469 .queue = queue,
470 .skb = skb,
471 .page = page,
472 .size = 0,
473 };
474
475 gnttab_for_one_grant(page, offset, len, xennet_tx_setup_grant, &info);
476
477 return info.tx;
478}
479
480static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
481 unsigned int len, void *data)
482{
483 struct xennet_gnttab_make_txreq *info = data;
484
485 info->tx->flags |= XEN_NETTXF_more_data;
486 skb_get(info->skb);
487 xennet_tx_setup_grant(gfn, offset, len, data);
David Vrabela55e8bb2015-01-13 17:16:44 +0000488}
489
490static struct xen_netif_tx_request *xennet_make_txreqs(
491 struct netfront_queue *queue, struct xen_netif_tx_request *tx,
492 struct sk_buff *skb, struct page *page,
493 unsigned int offset, unsigned int len)
494{
Julien Grall30c5d7f2015-04-10 14:42:21 +0100495 struct xennet_gnttab_make_txreq info = {
496 .queue = queue,
497 .skb = skb,
498 .tx = tx,
499 };
500
David Vrabela55e8bb2015-01-13 17:16:44 +0000501 /* Skip unused frames from start of page */
502 page += offset >> PAGE_SHIFT;
503 offset &= ~PAGE_MASK;
504
505 while (len) {
Julien Grall30c5d7f2015-04-10 14:42:21 +0100506 info.page = page;
507 info.size = 0;
508
509 gnttab_foreach_grant_in_range(page, offset, len,
510 xennet_make_one_txreq,
511 &info);
512
David Vrabela55e8bb2015-01-13 17:16:44 +0000513 page++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700514 offset = 0;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100515 len -= info.size;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700516 }
517
Julien Grall30c5d7f2015-04-10 14:42:21 +0100518 return info.tx;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700519}
520
Ian Campbellf36c3742012-11-21 02:02:16 +0000521/*
David Vrabele84448d2015-01-13 17:16:43 +0000522 * Count how many ring slots are required to send this skb. Each frag
523 * might be a compound page.
Ian Campbellf36c3742012-11-21 02:02:16 +0000524 */
David Vrabele84448d2015-01-13 17:16:43 +0000525static int xennet_count_skb_slots(struct sk_buff *skb)
Ian Campbellf36c3742012-11-21 02:02:16 +0000526{
527 int i, frags = skb_shinfo(skb)->nr_frags;
Julien Grall30c5d7f2015-04-10 14:42:21 +0100528 int slots;
David Vrabele84448d2015-01-13 17:16:43 +0000529
Julien Grall30c5d7f2015-04-10 14:42:21 +0100530 slots = gnttab_count_grant(offset_in_page(skb->data),
531 skb_headlen(skb));
Ian Campbellf36c3742012-11-21 02:02:16 +0000532
533 for (i = 0; i < frags; i++) {
534 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
535 unsigned long size = skb_frag_size(frag);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700536 unsigned long offset = skb_frag_off(frag);
Ian Campbellf36c3742012-11-21 02:02:16 +0000537
538 /* Skip unused frames from start of page */
539 offset &= ~PAGE_MASK;
540
Julien Grall30c5d7f2015-04-10 14:42:21 +0100541 slots += gnttab_count_grant(offset, size);
Ian Campbellf36c3742012-11-21 02:02:16 +0000542 }
543
Julien Grall30c5d7f2015-04-10 14:42:21 +0100544 return slots;
Ian Campbellf36c3742012-11-21 02:02:16 +0000545}
546
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100547static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +0100548 struct net_device *sb_dev)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100549{
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100550 unsigned int num_queues = dev->real_num_tx_queues;
551 u32 hash;
552 u16 queue_idx;
553
554 /* First, check if there is only one queue */
555 if (num_queues == 1) {
556 queue_idx = 0;
557 } else {
558 hash = skb_get_hash(skb);
559 queue_idx = hash % num_queues;
560 }
561
562 return queue_idx;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100563}
564
Julien Grall30c5d7f2015-04-10 14:42:21 +0100565#define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
566
Luc Van Oostenryck24a94b32018-04-24 15:18:14 +0200567static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700568{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700569 struct netfront_info *np = netdev_priv(dev);
David Vrabel900e1832015-01-13 16:42:42 +0000570 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
David Vrabela55e8bb2015-01-13 17:16:44 +0000571 struct xen_netif_tx_request *tx, *first_tx;
572 unsigned int i;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700573 int notify;
Ian Campbellf36c3742012-11-21 02:02:16 +0000574 int slots;
David Vrabela55e8bb2015-01-13 17:16:44 +0000575 struct page *page;
576 unsigned int offset;
577 unsigned int len;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +0000578 unsigned long flags;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100579 struct netfront_queue *queue = NULL;
580 unsigned int num_queues = dev->real_num_tx_queues;
581 u16 queue_index;
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200582 struct sk_buff *nskb;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100583
584 /* Drop the packet if no queues are set up */
585 if (num_queues < 1)
586 goto drop;
587 /* Determine which queue to transmit this SKB on */
588 queue_index = skb_get_queue_mapping(skb);
589 queue = &np->queues[queue_index];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700590
Wei Liu9ecd1a72013-04-22 02:20:41 +0000591 /* If skb->len is too big for wire format, drop skb and alert
592 * user about misconfiguration.
593 */
594 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
595 net_alert_ratelimited(
596 "xennet: skb->len = %u, too big for wire format\n",
597 skb->len);
598 goto drop;
599 }
600
David Vrabele84448d2015-01-13 17:16:43 +0000601 slots = xennet_count_skb_slots(skb);
Julien Grall30c5d7f2015-04-10 14:42:21 +0100602 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
Zoltan Kiss97a6d1b2014-08-11 18:32:23 +0100603 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
604 slots, skb->len);
605 if (skb_linearize(skb))
606 goto drop;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700607 }
608
David Vrabela55e8bb2015-01-13 17:16:44 +0000609 page = virt_to_page(skb->data);
610 offset = offset_in_page(skb->data);
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200611
612 /* The first req should be at least ETH_HLEN size or the packet will be
613 * dropped by netback.
614 */
615 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
616 nskb = skb_copy(skb, GFP_ATOMIC);
617 if (!nskb)
618 goto drop;
Eric Dumazet62f32502017-08-30 10:32:58 -0700619 dev_consume_skb_any(skb);
Vitaly Kuznetsovfd071602016-09-19 12:53:40 +0200620 skb = nskb;
621 page = virt_to_page(skb->data);
622 offset = offset_in_page(skb->data);
623 }
624
David Vrabela55e8bb2015-01-13 17:16:44 +0000625 len = skb_headlen(skb);
626
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100627 spin_lock_irqsave(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700628
629 if (unlikely(!netif_carrier_ok(dev) ||
Ian Campbellf36c3742012-11-21 02:02:16 +0000630 (slots > 1 && !xennet_can_sg(dev)) ||
Johannes Berg8b86a612015-04-17 15:45:04 +0200631 netif_needs_gso(skb, netif_skb_features(skb)))) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100632 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700633 goto drop;
634 }
635
David Vrabela55e8bb2015-01-13 17:16:44 +0000636 /* First request for the linear area. */
Julien Grall30c5d7f2015-04-10 14:42:21 +0100637 first_tx = tx = xennet_make_first_txreq(queue, skb,
638 page, offset, len);
639 offset += tx->size;
640 if (offset == PAGE_SIZE) {
641 page++;
642 offset = 0;
643 }
David Vrabela55e8bb2015-01-13 17:16:44 +0000644 len -= tx->size;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700645
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700646 if (skb->ip_summed == CHECKSUM_PARTIAL)
647 /* local packet? */
Ian Campbellf942dc22011-03-15 00:06:18 +0000648 tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700649 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
650 /* remote but checksummed. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000651 tx->flags |= XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700652
David Vrabela55e8bb2015-01-13 17:16:44 +0000653 /* Optional extra info after the first request. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700654 if (skb_shinfo(skb)->gso_size) {
655 struct xen_netif_extra_info *gso;
656
657 gso = (struct xen_netif_extra_info *)
David Vrabela55e8bb2015-01-13 17:16:44 +0000658 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700659
Wei Liue2d617c2013-03-25 01:08:17 +0000660 tx->flags |= XEN_NETTXF_extra_info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700661
662 gso->u.gso.size = skb_shinfo(skb)->gso_size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000663 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
664 XEN_NETIF_GSO_TYPE_TCPV6 :
665 XEN_NETIF_GSO_TYPE_TCPV4;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700666 gso->u.gso.pad = 0;
667 gso->u.gso.features = 0;
668
669 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
670 gso->flags = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700671 }
672
David Vrabela55e8bb2015-01-13 17:16:44 +0000673 /* Requests for the rest of the linear area. */
674 tx = xennet_make_txreqs(queue, tx, skb, page, offset, len);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700675
David Vrabela55e8bb2015-01-13 17:16:44 +0000676 /* Requests for all the frags. */
677 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
678 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700679 tx = xennet_make_txreqs(queue, tx, skb, skb_frag_page(frag),
680 skb_frag_off(frag),
David Vrabela55e8bb2015-01-13 17:16:44 +0000681 skb_frag_size(frag));
682 }
683
684 /* First request has the packet length. */
685 first_tx->size = skb->len;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700686
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100687 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700688 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100689 notify_remote_via_irq(queue->tx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700690
David Vrabel900e1832015-01-13 16:42:42 +0000691 u64_stats_update_begin(&tx_stats->syncp);
692 tx_stats->bytes += skb->len;
693 tx_stats->packets++;
694 u64_stats_update_end(&tx_stats->syncp);
Jeremy Fitzhardinge10a273a2007-08-13 12:54:37 -0700695
696 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100697 xennet_tx_buf_gc(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700698
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100699 if (!netfront_tx_slot_available(queue))
700 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700701
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100702 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700703
Patrick McHardy6ed10652009-06-23 06:03:08 +0000704 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700705
706 drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700707 dev->stats.tx_dropped++;
Eric W. Biederman979de8a2014-03-15 18:33:04 -0700708 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000709 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700710}
711
712static int xennet_close(struct net_device *dev)
713{
714 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100715 unsigned int num_queues = dev->real_num_tx_queues;
716 unsigned int i;
717 struct netfront_queue *queue;
718 netif_tx_stop_all_queues(np->netdev);
719 for (i = 0; i < num_queues; ++i) {
720 queue = &np->queues[i];
721 napi_disable(&queue->napi);
722 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700723 return 0;
724}
725
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100726static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700727 grant_ref_t ref)
728{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100729 int new = xennet_rxidx(queue->rx.req_prod_pvt);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700730
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100731 BUG_ON(queue->rx_skbs[new]);
732 queue->rx_skbs[new] = skb;
733 queue->grant_rx_ref[new] = ref;
734 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
735 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
736 queue->rx.req_prod_pvt++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700737}
738
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100739static int xennet_get_extras(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700740 struct xen_netif_extra_info *extras,
741 RING_IDX rp)
742
743{
744 struct xen_netif_extra_info *extra;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100745 struct device *dev = &queue->info->netdev->dev;
746 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700747 int err = 0;
748
749 do {
750 struct sk_buff *skb;
751 grant_ref_t ref;
752
753 if (unlikely(cons + 1 == rp)) {
754 if (net_ratelimit())
755 dev_warn(dev, "Missing extra info\n");
756 err = -EBADR;
757 break;
758 }
759
760 extra = (struct xen_netif_extra_info *)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100761 RING_GET_RESPONSE(&queue->rx, ++cons);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700762
763 if (unlikely(!extra->type ||
764 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
765 if (net_ratelimit())
766 dev_warn(dev, "Invalid extra type: %d\n",
767 extra->type);
768 err = -EINVAL;
769 } else {
770 memcpy(&extras[extra->type - 1], extra,
771 sizeof(*extra));
772 }
773
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100774 skb = xennet_get_rx_skb(queue, cons);
775 ref = xennet_get_rx_ref(queue, cons);
776 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700777 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
778
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100779 queue->rx.rsp_cons = cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700780 return err;
781}
782
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100783static int xennet_get_responses(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700784 struct netfront_rx_info *rinfo, RING_IDX rp,
785 struct sk_buff_head *list)
786{
787 struct xen_netif_rx_response *rx = &rinfo->rx;
788 struct xen_netif_extra_info *extras = rinfo->extras;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100789 struct device *dev = &queue->info->netdev->dev;
790 RING_IDX cons = queue->rx.rsp_cons;
791 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
792 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
Juergen Gross57f230a2018-06-12 08:57:53 +0200793 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
Wei Liu7158ff62013-03-25 01:08:19 +0000794 int slots = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700795 int err = 0;
796 unsigned long ret;
797
Ian Campbellf942dc22011-03-15 00:06:18 +0000798 if (rx->flags & XEN_NETRXF_extra_info) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100799 err = xennet_get_extras(queue, extras, rp);
800 cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700801 }
802
803 for (;;) {
804 if (unlikely(rx->status < 0 ||
Julien Grall30c5d7f2015-04-10 14:42:21 +0100805 rx->offset + rx->status > XEN_PAGE_SIZE)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700806 if (net_ratelimit())
Julien Grall6c101272015-06-16 20:10:46 +0100807 dev_warn(dev, "rx->offset: %u, size: %d\n",
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700808 rx->offset, rx->status);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100809 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700810 err = -EINVAL;
811 goto next;
812 }
813
814 /*
815 * This definitely indicates a bug, either in this driver or in
816 * the backend driver. In future this should flag the bad
Wei Liu697089d2013-04-22 02:20:40 +0000817 * situation to the system controller to reboot the backend.
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700818 */
819 if (ref == GRANT_INVALID_REF) {
820 if (net_ratelimit())
821 dev_warn(dev, "Bad rx response id %d.\n",
822 rx->id);
823 err = -EINVAL;
824 goto next;
825 }
826
827 ret = gnttab_end_foreign_access_ref(ref, 0);
828 BUG_ON(!ret);
829
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100830 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700831
832 __skb_queue_tail(list, skb);
833
834next:
Ian Campbellf942dc22011-03-15 00:06:18 +0000835 if (!(rx->flags & XEN_NETRXF_more_data))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700836 break;
837
Wei Liu7158ff62013-03-25 01:08:19 +0000838 if (cons + slots == rp) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700839 if (net_ratelimit())
Wei Liu7158ff62013-03-25 01:08:19 +0000840 dev_warn(dev, "Need more slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700841 err = -ENOENT;
842 break;
843 }
844
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100845 rx = RING_GET_RESPONSE(&queue->rx, cons + slots);
846 skb = xennet_get_rx_skb(queue, cons + slots);
847 ref = xennet_get_rx_ref(queue, cons + slots);
Wei Liu7158ff62013-03-25 01:08:19 +0000848 slots++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700849 }
850
Wei Liu7158ff62013-03-25 01:08:19 +0000851 if (unlikely(slots > max)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700852 if (net_ratelimit())
Wei Liu697089d2013-04-22 02:20:40 +0000853 dev_warn(dev, "Too many slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700854 err = -E2BIG;
855 }
856
857 if (unlikely(err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100858 queue->rx.rsp_cons = cons + slots;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700859
860 return err;
861}
862
863static int xennet_set_skb_gso(struct sk_buff *skb,
864 struct xen_netif_extra_info *gso)
865{
866 if (!gso->u.gso.size) {
867 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -0700868 pr_warn("GSO size must not be zero\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700869 return -EINVAL;
870 }
871
Paul Durrant2c0057d2014-01-15 17:30:33 +0000872 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
873 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700874 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -0700875 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700876 return -EINVAL;
877 }
878
879 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000880 skb_shinfo(skb)->gso_type =
881 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
882 SKB_GSO_TCPV4 :
883 SKB_GSO_TCPV6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700884
885 /* Header must be checked, and gso_segs computed. */
886 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
887 skb_shinfo(skb)->gso_segs = 0;
888
889 return 0;
890}
891
Dongli Zhanga7611292019-10-01 21:56:41 +0800892static int xennet_fill_frags(struct netfront_queue *queue,
893 struct sk_buff *skb,
894 struct sk_buff_head *list)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700895{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100896 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700897 struct sk_buff *nskb;
898
899 while ((nskb = __skb_dequeue(list))) {
900 struct xen_netif_rx_response *rx =
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100901 RING_GET_RESPONSE(&queue->rx, ++cons);
Ian Campbell01c68022011-10-05 00:28:47 +0000902 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700903
Juergen Grossd472b3a2018-08-09 16:42:16 +0200904 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
Jan Beulich093b9c72013-07-17 08:09:37 +0100905 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700906
Juergen Grossd81c5052018-12-18 16:06:19 +0100907 BUG_ON(pull_to < skb_headlen(skb));
Jan Beulich093b9c72013-07-17 08:09:37 +0100908 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
909 }
Juergen Grossad4f15d2018-09-11 09:04:48 +0200910 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
Dongli Zhang00b36852019-09-16 11:46:59 +0800911 queue->rx.rsp_cons = ++cons + skb_queue_len(list);
Juergen Grossad4f15d2018-09-11 09:04:48 +0200912 kfree_skb(nskb);
Dongli Zhanga7611292019-10-01 21:56:41 +0800913 return -ENOENT;
Juergen Grossad4f15d2018-09-11 09:04:48 +0200914 }
Jan Beulich093b9c72013-07-17 08:09:37 +0100915
Juergen Grossd472b3a2018-08-09 16:42:16 +0200916 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
917 skb_frag_page(nfrag),
Jan Beulich093b9c72013-07-17 08:09:37 +0100918 rx->offset, rx->status, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700919
920 skb_shinfo(nskb)->nr_frags = 0;
921 kfree_skb(nskb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700922 }
923
Dongli Zhanga7611292019-10-01 21:56:41 +0800924 queue->rx.rsp_cons = cons;
925
926 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700927}
928
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000929static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700930{
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000931 bool recalculate_partial_csum = false;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000932
933 /*
934 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
935 * peers can fail to set NETRXF_csum_blank when sending a GSO
936 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
937 * recalculate the partial checksum.
938 */
939 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
940 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100941 atomic_inc(&np->rx_gso_checksum_fixup);
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000942 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000943 recalculate_partial_csum = true;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000944 }
945
946 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
947 if (skb->ip_summed != CHECKSUM_PARTIAL)
948 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700949
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000950 return skb_checksum_setup(skb, recalculate_partial_csum);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700951}
952
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100953static int handle_incoming_queue(struct netfront_queue *queue,
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700954 struct sk_buff_head *rxq)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700955{
David Vrabel900e1832015-01-13 16:42:42 +0000956 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700957 int packets_dropped = 0;
958 struct sk_buff *skb;
959
960 while ((skb = __skb_dequeue(rxq)) != NULL) {
Ian Campbell36832432012-08-22 00:26:47 +0000961 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700962
Jan Beulich093b9c72013-07-17 08:09:37 +0100963 if (pull_to > skb_headlen(skb))
964 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700965
966 /* Ethernet work: Delayed to here as it peeks the header. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100967 skb->protocol = eth_type_trans(skb, queue->info->netdev);
Wei Liud554f732014-02-19 18:48:34 +0000968 skb_reset_network_header(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700969
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100970 if (checksum_setup(queue->info->netdev, skb)) {
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000971 kfree_skb(skb);
972 packets_dropped++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100973 queue->info->netdev->stats.rx_errors++;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000974 continue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700975 }
976
David Vrabel900e1832015-01-13 16:42:42 +0000977 u64_stats_update_begin(&rx_stats->syncp);
978 rx_stats->packets++;
979 rx_stats->bytes += skb->len;
980 u64_stats_update_end(&rx_stats->syncp);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700981
982 /* Pass it up. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100983 napi_gro_receive(&queue->napi, skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700984 }
985
986 return packets_dropped;
987}
988
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700989static int xennet_poll(struct napi_struct *napi, int budget)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700990{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100991 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
992 struct net_device *dev = queue->info->netdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700993 struct sk_buff *skb;
994 struct netfront_rx_info rinfo;
995 struct xen_netif_rx_response *rx = &rinfo.rx;
996 struct xen_netif_extra_info *extras = rinfo.extras;
997 RING_IDX i, rp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700998 int work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700999 struct sk_buff_head rxq;
1000 struct sk_buff_head errq;
1001 struct sk_buff_head tmpq;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001002 int err;
1003
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001004 spin_lock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001005
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001006 skb_queue_head_init(&rxq);
1007 skb_queue_head_init(&errq);
1008 skb_queue_head_init(&tmpq);
1009
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001010 rp = queue->rx.sring->rsp_prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001011 rmb(); /* Ensure we see queued responses up to 'rp'. */
1012
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001013 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001014 work_done = 0;
1015 while ((i != rp) && (work_done < budget)) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001016 memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001017 memset(extras, 0, sizeof(rinfo.extras));
1018
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001019 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001020
1021 if (unlikely(err)) {
1022err:
1023 while ((skb = __skb_dequeue(&tmpq)))
1024 __skb_queue_tail(&errq, skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001025 dev->stats.rx_errors++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001026 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001027 continue;
1028 }
1029
1030 skb = __skb_dequeue(&tmpq);
1031
1032 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1033 struct xen_netif_extra_info *gso;
1034 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1035
1036 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1037 __skb_queue_head(&tmpq, skb);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001038 queue->rx.rsp_cons += skb_queue_len(&tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001039 goto err;
1040 }
1041 }
1042
Ian Campbell36832432012-08-22 00:26:47 +00001043 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1044 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1045 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001046
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001047 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
Ian Campbell36832432012-08-22 00:26:47 +00001048 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1049 skb->data_len = rx->status;
Jan Beulich093b9c72013-07-17 08:09:37 +01001050 skb->len += rx->status;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001051
Dongli Zhanga7611292019-10-01 21:56:41 +08001052 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
Juergen Grossad4f15d2018-09-11 09:04:48 +02001053 goto err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001054
Ian Campbellf942dc22011-03-15 00:06:18 +00001055 if (rx->flags & XEN_NETRXF_csum_blank)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001056 skb->ip_summed = CHECKSUM_PARTIAL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001057 else if (rx->flags & XEN_NETRXF_data_validated)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001058 skb->ip_summed = CHECKSUM_UNNECESSARY;
1059
1060 __skb_queue_tail(&rxq, skb);
1061
Dongli Zhanga7611292019-10-01 21:56:41 +08001062 i = ++queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001063 work_done++;
1064 }
1065
Wang Chen56cfe5d2008-05-22 18:09:06 +08001066 __skb_queue_purge(&errq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001067
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001068 work_done -= handle_incoming_queue(queue, &rxq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001069
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001070 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001071
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001072 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001073 int more_to_do = 0;
1074
Eric Dumazet6ad20162017-01-30 08:22:01 -08001075 napi_complete_done(napi, work_done);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001076
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001077 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
David Vrabel6a6dc082014-12-16 18:59:46 +00001078 if (more_to_do)
1079 napi_schedule(napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001080 }
1081
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001082 spin_unlock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001083
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001084 return work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001085}
1086
1087static int xennet_change_mtu(struct net_device *dev, int mtu)
1088{
Jonathan Davies0c368202015-03-31 11:05:15 +01001089 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001090
1091 if (mtu > max)
1092 return -EINVAL;
1093 dev->mtu = mtu;
1094 return 0;
1095}
1096
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001097static void xennet_get_stats64(struct net_device *dev,
1098 struct rtnl_link_stats64 *tot)
stephen hemmingere00f85b2011-06-21 05:35:31 +00001099{
1100 struct netfront_info *np = netdev_priv(dev);
1101 int cpu;
1102
1103 for_each_possible_cpu(cpu) {
David Vrabel900e1832015-01-13 16:42:42 +00001104 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1105 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
stephen hemmingere00f85b2011-06-21 05:35:31 +00001106 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1107 unsigned int start;
1108
1109 do {
David Vrabel900e1832015-01-13 16:42:42 +00001110 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1111 tx_packets = tx_stats->packets;
1112 tx_bytes = tx_stats->bytes;
1113 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001114
David Vrabel900e1832015-01-13 16:42:42 +00001115 do {
1116 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1117 rx_packets = rx_stats->packets;
1118 rx_bytes = rx_stats->bytes;
1119 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001120
1121 tot->rx_packets += rx_packets;
1122 tot->tx_packets += tx_packets;
1123 tot->rx_bytes += rx_bytes;
1124 tot->tx_bytes += tx_bytes;
1125 }
1126
1127 tot->rx_errors = dev->stats.rx_errors;
1128 tot->tx_dropped = dev->stats.tx_dropped;
stephen hemmingere00f85b2011-06-21 05:35:31 +00001129}
1130
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001131static void xennet_release_tx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001132{
1133 struct sk_buff *skb;
1134 int i;
1135
1136 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1137 /* Skip over entries which are actually freelist references */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001138 if (skb_entry_is_link(&queue->tx_skbs[i]))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001139 continue;
1140
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001141 skb = queue->tx_skbs[i].skb;
1142 get_page(queue->grant_tx_page[i]);
1143 gnttab_end_foreign_access(queue->grant_tx_ref[i],
Annie Licefe0072014-01-28 11:35:42 +08001144 GNTMAP_readonly,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001145 (unsigned long)page_address(queue->grant_tx_page[i]));
1146 queue->grant_tx_page[i] = NULL;
1147 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1148 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001149 dev_kfree_skb_irq(skb);
1150 }
1151}
1152
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001153static void xennet_release_rx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001154{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001155 int id, ref;
1156
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001157 spin_lock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001158
1159 for (id = 0; id < NET_RX_RING_SIZE; id++) {
Annie Licefe0072014-01-28 11:35:42 +08001160 struct sk_buff *skb;
1161 struct page *page;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001162
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001163 skb = queue->rx_skbs[id];
Annie Licefe0072014-01-28 11:35:42 +08001164 if (!skb)
1165 continue;
1166
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001167 ref = queue->grant_rx_ref[id];
Annie Licefe0072014-01-28 11:35:42 +08001168 if (ref == GRANT_INVALID_REF)
1169 continue;
1170
1171 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1172
1173 /* gnttab_end_foreign_access() needs a page ref until
1174 * foreign access is ended (which may be deferred).
1175 */
1176 get_page(page);
1177 gnttab_end_foreign_access(ref, 0,
1178 (unsigned long)page_address(page));
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001179 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001180
Annie Licefe0072014-01-28 11:35:42 +08001181 kfree_skb(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001182 }
1183
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001184 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001185}
1186
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001187static netdev_features_t xennet_fix_features(struct net_device *dev,
1188 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001189{
1190 struct netfront_info *np = netdev_priv(dev);
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001191
Juergen Gross2890ea52016-10-31 14:58:41 +01001192 if (features & NETIF_F_SG &&
1193 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1194 features &= ~NETIF_F_SG;
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001195
Juergen Gross2890ea52016-10-31 14:58:41 +01001196 if (features & NETIF_F_IPV6_CSUM &&
1197 !xenbus_read_unsigned(np->xbdev->otherend,
1198 "feature-ipv6-csum-offload", 0))
1199 features &= ~NETIF_F_IPV6_CSUM;
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001200
Juergen Gross2890ea52016-10-31 14:58:41 +01001201 if (features & NETIF_F_TSO &&
1202 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1203 features &= ~NETIF_F_TSO;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001204
Juergen Gross2890ea52016-10-31 14:58:41 +01001205 if (features & NETIF_F_TSO6 &&
1206 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1207 features &= ~NETIF_F_TSO6;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001208
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001209 return features;
1210}
1211
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001212static int xennet_set_features(struct net_device *dev,
1213 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001214{
1215 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1216 netdev_info(dev, "Reducing MTU because no SG offload");
1217 dev->mtu = ETH_DATA_LEN;
1218 }
1219
1220 return 0;
1221}
1222
Wei Liud634bf22013-05-22 06:34:46 +00001223static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001224{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001225 struct netfront_queue *queue = dev_id;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001226 unsigned long flags;
1227
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001228 spin_lock_irqsave(&queue->tx_lock, flags);
1229 xennet_tx_buf_gc(queue);
1230 spin_unlock_irqrestore(&queue->tx_lock, flags);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001231
1232 return IRQ_HANDLED;
1233}
1234
Wei Liud634bf22013-05-22 06:34:46 +00001235static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1236{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001237 struct netfront_queue *queue = dev_id;
1238 struct net_device *dev = queue->info->netdev;
Wei Liud634bf22013-05-22 06:34:46 +00001239
1240 if (likely(netif_carrier_ok(dev) &&
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001241 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
David Vrabel76541862014-06-18 10:47:27 +01001242 napi_schedule(&queue->napi);
Wei Liud634bf22013-05-22 06:34:46 +00001243
1244 return IRQ_HANDLED;
1245}
1246
1247static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1248{
1249 xennet_tx_interrupt(irq, dev_id);
1250 xennet_rx_interrupt(irq, dev_id);
1251 return IRQ_HANDLED;
1252}
1253
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001254#ifdef CONFIG_NET_POLL_CONTROLLER
1255static void xennet_poll_controller(struct net_device *dev)
1256{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001257 /* Poll each queue */
1258 struct netfront_info *info = netdev_priv(dev);
1259 unsigned int num_queues = dev->real_num_tx_queues;
1260 unsigned int i;
1261 for (i = 0; i < num_queues; ++i)
1262 xennet_interrupt(0, &info->queues[i]);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001263}
1264#endif
1265
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001266static const struct net_device_ops xennet_netdev_ops = {
1267 .ndo_open = xennet_open,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001268 .ndo_stop = xennet_close,
1269 .ndo_start_xmit = xennet_start_xmit,
1270 .ndo_change_mtu = xennet_change_mtu,
stephen hemmingere00f85b2011-06-21 05:35:31 +00001271 .ndo_get_stats64 = xennet_get_stats64,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001272 .ndo_set_mac_address = eth_mac_addr,
1273 .ndo_validate_addr = eth_validate_addr,
Michał Mirosławfb507932011-03-31 01:01:35 +00001274 .ndo_fix_features = xennet_fix_features,
1275 .ndo_set_features = xennet_set_features,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001276 .ndo_select_queue = xennet_select_queue,
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001277#ifdef CONFIG_NET_POLL_CONTROLLER
1278 .ndo_poll_controller = xennet_poll_controller,
1279#endif
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001280};
1281
David Vrabel900e1832015-01-13 16:42:42 +00001282static void xennet_free_netdev(struct net_device *netdev)
1283{
1284 struct netfront_info *np = netdev_priv(netdev);
1285
1286 free_percpu(np->rx_stats);
1287 free_percpu(np->tx_stats);
1288 free_netdev(netdev);
1289}
1290
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001291static struct net_device *xennet_create_dev(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001292{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001293 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001294 struct net_device *netdev;
1295 struct netfront_info *np;
1296
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001297 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
Joe Perches41de8d42012-01-29 13:47:52 +00001298 if (!netdev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001299 return ERR_PTR(-ENOMEM);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001300
1301 np = netdev_priv(netdev);
1302 np->xbdev = dev;
1303
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001304 np->queues = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001305
stephen hemmingere00f85b2011-06-21 05:35:31 +00001306 err = -ENOMEM;
David Vrabel900e1832015-01-13 16:42:42 +00001307 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1308 if (np->rx_stats == NULL)
1309 goto exit;
1310 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1311 if (np->tx_stats == NULL)
stephen hemmingere00f85b2011-06-21 05:35:31 +00001312 goto exit;
1313
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001314 netdev->netdev_ops = &xennet_netdev_ops;
1315
Michał Mirosławfb507932011-03-31 01:01:35 +00001316 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1317 NETIF_F_GSO_ROBUST;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001318 netdev->hw_features = NETIF_F_SG |
1319 NETIF_F_IPV6_CSUM |
1320 NETIF_F_TSO | NETIF_F_TSO6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001321
Ian Campbellfc3e5942011-04-04 11:07:57 -07001322 /*
1323 * Assume that all hw features are available for now. This set
1324 * will be adjusted by the call to netdev_update_features() in
1325 * xennet_connect() which is the earliest point where we can
1326 * negotiate with the backend regarding supported features.
1327 */
1328 netdev->features |= netdev->hw_features;
1329
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001330 netdev->ethtool_ops = &xennet_ethtool_ops;
Mohammed Gamale1043a42017-10-16 15:20:32 +02001331 netdev->min_mtu = ETH_MIN_MTU;
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001332 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001333 SET_NETDEV_DEV(netdev, &dev->dev);
1334
1335 np->netdev = netdev;
1336
1337 netif_carrier_off(netdev);
1338
Andrea Righic2c63312020-07-24 10:59:10 +02001339 do {
1340 xenbus_switch_state(dev, XenbusStateInitialising);
1341 err = wait_event_timeout(module_wq,
1342 xenbus_read_driver_state(dev->otherend) !=
1343 XenbusStateClosed &&
1344 xenbus_read_driver_state(dev->otherend) !=
1345 XenbusStateUnknown, XENNET_TIMEOUT);
1346 } while (!err);
1347
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001348 return netdev;
1349
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001350 exit:
David Vrabel900e1832015-01-13 16:42:42 +00001351 xennet_free_netdev(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001352 return ERR_PTR(err);
1353}
1354
1355/**
1356 * Entry point to this code when a new device is created. Allocate the basic
1357 * structures and the ring buffers for communication with the backend, and
1358 * inform the backend of the appropriate details for those.
1359 */
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001360static int netfront_probe(struct xenbus_device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001361 const struct xenbus_device_id *id)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001362{
1363 int err;
1364 struct net_device *netdev;
1365 struct netfront_info *info;
1366
1367 netdev = xennet_create_dev(dev);
1368 if (IS_ERR(netdev)) {
1369 err = PTR_ERR(netdev);
1370 xenbus_dev_fatal(dev, err, "creating netdev");
1371 return err;
1372 }
1373
1374 info = netdev_priv(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001375 dev_set_drvdata(&dev->dev, info);
Takashi Iwai27b917e2015-02-04 14:38:55 +01001376#ifdef CONFIG_SYSFS
1377 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1378#endif
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001379
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001380 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001381}
1382
1383static void xennet_end_access(int ref, void *page)
1384{
1385 /* This frees the page as a side-effect */
1386 if (ref != GRANT_INVALID_REF)
1387 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1388}
1389
1390static void xennet_disconnect_backend(struct netfront_info *info)
1391{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001392 unsigned int i = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001393 unsigned int num_queues = info->netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001394
David Vrabelf9feb1e2014-07-02 16:09:15 +01001395 netif_carrier_off(info->netdev);
1396
Chas Williams9a873c72015-08-19 19:14:20 -04001397 for (i = 0; i < num_queues && info->queues; ++i) {
David Vrabel76541862014-06-18 10:47:27 +01001398 struct netfront_queue *queue = &info->queues[i];
1399
Boris Ostrovsky74470952017-01-30 12:45:46 -05001400 del_timer_sync(&queue->rx_refill_timer);
1401
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001402 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1403 unbind_from_irqhandler(queue->tx_irq, queue);
1404 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1405 unbind_from_irqhandler(queue->tx_irq, queue);
1406 unbind_from_irqhandler(queue->rx_irq, queue);
1407 }
1408 queue->tx_evtchn = queue->rx_evtchn = 0;
1409 queue->tx_irq = queue->rx_irq = 0;
1410
Chas Williams274b0452015-08-27 12:28:46 -04001411 if (netif_running(info->netdev))
1412 napi_synchronize(&queue->napi);
David Vrabelf9feb1e2014-07-02 16:09:15 +01001413
David Vrabela5b5dc32014-07-31 17:38:23 +01001414 xennet_release_tx_bufs(queue);
1415 xennet_release_rx_bufs(queue);
1416 gnttab_free_grant_references(queue->gref_tx_head);
1417 gnttab_free_grant_references(queue->gref_rx_head);
1418
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001419 /* End access and free the pages */
1420 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1421 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1422
1423 queue->tx_ring_ref = GRANT_INVALID_REF;
1424 queue->rx_ring_ref = GRANT_INVALID_REF;
1425 queue->tx.sring = NULL;
1426 queue->rx.sring = NULL;
Wei Liud634bf22013-05-22 06:34:46 +00001427 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001428}
1429
1430/**
1431 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1432 * driver restart. We tear down our netif structure and recreate it, but
1433 * leave the device-layer structures intact so that this is transparent to the
1434 * rest of the kernel.
1435 */
1436static int netfront_resume(struct xenbus_device *dev)
1437{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001438 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001439
1440 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1441
1442 xennet_disconnect_backend(info);
1443 return 0;
1444}
1445
1446static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1447{
1448 char *s, *e, *macstr;
1449 int i;
1450
1451 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1452 if (IS_ERR(macstr))
1453 return PTR_ERR(macstr);
1454
1455 for (i = 0; i < ETH_ALEN; i++) {
1456 mac[i] = simple_strtoul(s, &e, 16);
1457 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1458 kfree(macstr);
1459 return -ENOENT;
1460 }
1461 s = e+1;
1462 }
1463
1464 kfree(macstr);
1465 return 0;
1466}
1467
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001468static int setup_netfront_single(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001469{
1470 int err;
1471
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001472 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001473 if (err < 0)
1474 goto fail;
1475
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001476 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001477 xennet_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001478 0, queue->info->netdev->name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001479 if (err < 0)
1480 goto bind_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001481 queue->rx_evtchn = queue->tx_evtchn;
1482 queue->rx_irq = queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001483
1484 return 0;
1485
1486bind_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001487 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1488 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001489fail:
1490 return err;
1491}
1492
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001493static int setup_netfront_split(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001494{
1495 int err;
1496
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001497 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001498 if (err < 0)
1499 goto fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001500 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001501 if (err < 0)
1502 goto alloc_rx_evtchn_fail;
1503
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001504 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1505 "%s-tx", queue->name);
1506 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001507 xennet_tx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001508 0, queue->tx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001509 if (err < 0)
1510 goto bind_tx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001511 queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001512
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001513 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1514 "%s-rx", queue->name);
1515 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001516 xennet_rx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001517 0, queue->rx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001518 if (err < 0)
1519 goto bind_rx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001520 queue->rx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001521
1522 return 0;
1523
1524bind_rx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001525 unbind_from_irqhandler(queue->tx_irq, queue);
1526 queue->tx_irq = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001527bind_tx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001528 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1529 queue->rx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001530alloc_rx_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001531 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1532 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001533fail:
1534 return err;
1535}
1536
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001537static int setup_netfront(struct xenbus_device *dev,
1538 struct netfront_queue *queue, unsigned int feature_split_evtchn)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001539{
1540 struct xen_netif_tx_sring *txs;
1541 struct xen_netif_rx_sring *rxs;
Wei Liuccc9d902015-04-03 14:44:59 +08001542 grant_ref_t gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001543 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001544
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001545 queue->tx_ring_ref = GRANT_INVALID_REF;
1546 queue->rx_ring_ref = GRANT_INVALID_REF;
1547 queue->rx.sring = NULL;
1548 queue->tx.sring = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001549
Ian Campbella144ff02008-06-17 10:47:08 +02001550 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001551 if (!txs) {
1552 err = -ENOMEM;
1553 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1554 goto fail;
1555 }
1556 SHARED_RING_INIT(txs);
Julien Grall30c5d7f2015-04-10 14:42:21 +01001557 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001558
Wei Liuccc9d902015-04-03 14:44:59 +08001559 err = xenbus_grant_ring(dev, txs, 1, &gref);
Wei Liu1ca29832013-05-20 01:05:12 +00001560 if (err < 0)
1561 goto grant_tx_ring_fail;
Wei Liuccc9d902015-04-03 14:44:59 +08001562 queue->tx_ring_ref = gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001563
Ian Campbella144ff02008-06-17 10:47:08 +02001564 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001565 if (!rxs) {
1566 err = -ENOMEM;
1567 xenbus_dev_fatal(dev, err, "allocating rx ring page");
Wei Liu1ca29832013-05-20 01:05:12 +00001568 goto alloc_rx_ring_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001569 }
1570 SHARED_RING_INIT(rxs);
Julien Grall30c5d7f2015-04-10 14:42:21 +01001571 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001572
Wei Liuccc9d902015-04-03 14:44:59 +08001573 err = xenbus_grant_ring(dev, rxs, 1, &gref);
Wei Liu1ca29832013-05-20 01:05:12 +00001574 if (err < 0)
1575 goto grant_rx_ring_fail;
Wei Liuccc9d902015-04-03 14:44:59 +08001576 queue->rx_ring_ref = gref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001577
Wei Liud634bf22013-05-22 06:34:46 +00001578 if (feature_split_evtchn)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001579 err = setup_netfront_split(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001580 /* setup single event channel if
1581 * a) feature-split-event-channels == 0
1582 * b) feature-split-event-channels == 1 but failed to setup
1583 */
1584 if (!feature_split_evtchn || (feature_split_evtchn && err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001585 err = setup_netfront_single(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001586
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001587 if (err)
Wei Liu1ca29832013-05-20 01:05:12 +00001588 goto alloc_evtchn_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001589
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001590 return 0;
1591
Wei Liu1ca29832013-05-20 01:05:12 +00001592 /* If we fail to setup netfront, it is safe to just revoke access to
1593 * granted pages because backend is not accessing it at this point.
1594 */
Wei Liu1ca29832013-05-20 01:05:12 +00001595alloc_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001596 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001597grant_rx_ring_fail:
1598 free_page((unsigned long)rxs);
1599alloc_rx_ring_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001600 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001601grant_tx_ring_fail:
1602 free_page((unsigned long)txs);
1603fail:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001604 return err;
1605}
1606
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001607/* Queue-specific initialisation
1608 * This used to be done in xennet_create_dev() but must now
1609 * be run per-queue.
1610 */
1611static int xennet_init_queue(struct netfront_queue *queue)
1612{
1613 unsigned short i;
1614 int err = 0;
Xiao Liang21f27062018-08-14 23:21:28 +08001615 char *devid;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001616
1617 spin_lock_init(&queue->tx_lock);
1618 spin_lock_init(&queue->rx_lock);
1619
Kees Cooke99e88a2017-10-16 14:43:17 -07001620 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001621
Xiao Liang21f27062018-08-14 23:21:28 +08001622 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1623 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1624 devid, queue->id);
Wei Liu8b715012014-06-04 10:30:47 +01001625
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001626 /* Initialise tx_skbs as a free chain containing every entry. */
1627 queue->tx_skb_freelist = 0;
1628 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1629 skb_entry_set_link(&queue->tx_skbs[i], i+1);
1630 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1631 queue->grant_tx_page[i] = NULL;
1632 }
1633
1634 /* Clear out rx_skbs */
1635 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1636 queue->rx_skbs[i] = NULL;
1637 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1638 }
1639
1640 /* A grant for every tx ring slot */
David Vrabel1f3c2eb2014-10-22 11:17:06 +01001641 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001642 &queue->gref_tx_head) < 0) {
1643 pr_alert("can't alloc tx grant refs\n");
1644 err = -ENOMEM;
1645 goto exit;
1646 }
1647
1648 /* A grant for every rx ring slot */
David Vrabel1f3c2eb2014-10-22 11:17:06 +01001649 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001650 &queue->gref_rx_head) < 0) {
1651 pr_alert("can't alloc rx grant refs\n");
1652 err = -ENOMEM;
1653 goto exit_free_tx;
1654 }
1655
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001656 return 0;
1657
1658 exit_free_tx:
1659 gnttab_free_grant_references(queue->gref_tx_head);
1660 exit:
1661 return err;
1662}
1663
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001664static int write_queue_xenstore_keys(struct netfront_queue *queue,
1665 struct xenbus_transaction *xbt, int write_hierarchical)
1666{
1667 /* Write the queue-specific keys into XenStore in the traditional
1668 * way for a single queue, or in a queue subkeys for multiple
1669 * queues.
1670 */
1671 struct xenbus_device *dev = queue->info->xbdev;
1672 int err;
1673 const char *message;
1674 char *path;
1675 size_t pathsize;
1676
1677 /* Choose the correct place to write the keys */
1678 if (write_hierarchical) {
1679 pathsize = strlen(dev->nodename) + 10;
1680 path = kzalloc(pathsize, GFP_KERNEL);
1681 if (!path) {
1682 err = -ENOMEM;
1683 message = "out of memory while writing ring references";
1684 goto error;
1685 }
1686 snprintf(path, pathsize, "%s/queue-%u",
1687 dev->nodename, queue->id);
1688 } else {
1689 path = (char *)dev->nodename;
1690 }
1691
1692 /* Write ring references */
1693 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1694 queue->tx_ring_ref);
1695 if (err) {
1696 message = "writing tx-ring-ref";
1697 goto error;
1698 }
1699
1700 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1701 queue->rx_ring_ref);
1702 if (err) {
1703 message = "writing rx-ring-ref";
1704 goto error;
1705 }
1706
1707 /* Write event channels; taking into account both shared
1708 * and split event channel scenarios.
1709 */
1710 if (queue->tx_evtchn == queue->rx_evtchn) {
1711 /* Shared event channel */
1712 err = xenbus_printf(*xbt, path,
1713 "event-channel", "%u", queue->tx_evtchn);
1714 if (err) {
1715 message = "writing event-channel";
1716 goto error;
1717 }
1718 } else {
1719 /* Split event channels */
1720 err = xenbus_printf(*xbt, path,
1721 "event-channel-tx", "%u", queue->tx_evtchn);
1722 if (err) {
1723 message = "writing event-channel-tx";
1724 goto error;
1725 }
1726
1727 err = xenbus_printf(*xbt, path,
1728 "event-channel-rx", "%u", queue->rx_evtchn);
1729 if (err) {
1730 message = "writing event-channel-rx";
1731 goto error;
1732 }
1733 }
1734
1735 if (write_hierarchical)
1736 kfree(path);
1737 return 0;
1738
1739error:
1740 if (write_hierarchical)
1741 kfree(path);
1742 xenbus_dev_fatal(dev, err, "%s", message);
1743 return err;
1744}
1745
David Vrabelce587252014-06-18 10:47:28 +01001746static void xennet_destroy_queues(struct netfront_info *info)
1747{
1748 unsigned int i;
1749
David Vrabelce587252014-06-18 10:47:28 +01001750 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1751 struct netfront_queue *queue = &info->queues[i];
1752
1753 if (netif_running(info->netdev))
1754 napi_disable(&queue->napi);
1755 netif_napi_del(&queue->napi);
1756 }
1757
David Vrabelce587252014-06-18 10:47:28 +01001758 kfree(info->queues);
1759 info->queues = NULL;
1760}
1761
1762static int xennet_create_queues(struct netfront_info *info,
Joe Jinca88ea12015-10-19 13:37:17 +08001763 unsigned int *num_queues)
David Vrabelce587252014-06-18 10:47:28 +01001764{
1765 unsigned int i;
1766 int ret;
1767
Joe Jinca88ea12015-10-19 13:37:17 +08001768 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
David Vrabelce587252014-06-18 10:47:28 +01001769 GFP_KERNEL);
1770 if (!info->queues)
1771 return -ENOMEM;
1772
Joe Jinca88ea12015-10-19 13:37:17 +08001773 for (i = 0; i < *num_queues; i++) {
David Vrabelce587252014-06-18 10:47:28 +01001774 struct netfront_queue *queue = &info->queues[i];
1775
1776 queue->id = i;
1777 queue->info = info;
1778
1779 ret = xennet_init_queue(queue);
1780 if (ret < 0) {
Ross Lagerwallf599c642018-01-11 09:36:38 +00001781 dev_warn(&info->xbdev->dev,
David Vrabel69cb8522014-07-31 17:38:24 +01001782 "only created %d queues\n", i);
Joe Jinca88ea12015-10-19 13:37:17 +08001783 *num_queues = i;
David Vrabelce587252014-06-18 10:47:28 +01001784 break;
1785 }
1786
1787 netif_napi_add(queue->info->netdev, &queue->napi,
1788 xennet_poll, 64);
1789 if (netif_running(info->netdev))
1790 napi_enable(&queue->napi);
1791 }
1792
Joe Jinca88ea12015-10-19 13:37:17 +08001793 netif_set_real_num_tx_queues(info->netdev, *num_queues);
David Vrabelce587252014-06-18 10:47:28 +01001794
Joe Jinca88ea12015-10-19 13:37:17 +08001795 if (*num_queues == 0) {
Ross Lagerwallf599c642018-01-11 09:36:38 +00001796 dev_err(&info->xbdev->dev, "no queues\n");
David Vrabelce587252014-06-18 10:47:28 +01001797 return -EINVAL;
1798 }
1799 return 0;
1800}
1801
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001802/* Common code used when first setting up, and when resuming. */
Ian Campbellf502bf22010-08-18 23:27:49 +00001803static int talk_to_netback(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001804 struct netfront_info *info)
1805{
1806 const char *message;
1807 struct xenbus_transaction xbt;
1808 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001809 unsigned int feature_split_evtchn;
1810 unsigned int i = 0;
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001811 unsigned int max_queues = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001812 struct netfront_queue *queue = NULL;
1813 unsigned int num_queues = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001814
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001815 info->netdev->irq = 0;
1816
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001817 /* Check if backend supports multiple queues */
Juergen Gross2890ea52016-10-31 14:58:41 +01001818 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1819 "multi-queue-max-queues", 1);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001820 num_queues = min(max_queues, xennet_max_queues);
1821
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001822 /* Check feature-split-event-channels */
Juergen Gross2890ea52016-10-31 14:58:41 +01001823 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
1824 "feature-split-event-channels", 0);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001825
1826 /* Read mac addr. */
1827 err = xen_net_read_mac(dev, info->netdev->dev_addr);
1828 if (err) {
1829 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
Ross Lagerwallcb257782018-06-21 14:00:20 +01001830 goto out_unlocked;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001831 }
1832
Ross Lagerwallf599c642018-01-11 09:36:38 +00001833 rtnl_lock();
David Vrabelce587252014-06-18 10:47:28 +01001834 if (info->queues)
1835 xennet_destroy_queues(info);
1836
Joe Jinca88ea12015-10-19 13:37:17 +08001837 err = xennet_create_queues(info, &num_queues);
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00001838 if (err < 0) {
1839 xenbus_dev_fatal(dev, err, "creating queues");
1840 kfree(info->queues);
1841 info->queues = NULL;
1842 goto out;
1843 }
Ross Lagerwallf599c642018-01-11 09:36:38 +00001844 rtnl_unlock();
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001845
1846 /* Create shared ring, alloc event channel -- for each queue */
1847 for (i = 0; i < num_queues; ++i) {
1848 queue = &info->queues[i];
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001849 err = setup_netfront(dev, queue, feature_split_evtchn);
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00001850 if (err)
1851 goto destroy_ring;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001852 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001853
1854again:
1855 err = xenbus_transaction_start(&xbt);
1856 if (err) {
1857 xenbus_dev_fatal(dev, err, "starting transaction");
1858 goto destroy_ring;
1859 }
1860
chas williams812494d2015-09-16 16:28:25 -04001861 if (xenbus_exists(XBT_NIL,
1862 info->xbdev->otherend, "multi-queue-max-queues")) {
1863 /* Write the number of queues */
1864 err = xenbus_printf(xbt, dev->nodename,
1865 "multi-queue-num-queues", "%u", num_queues);
1866 if (err) {
1867 message = "writing multi-queue-num-queues";
1868 goto abort_transaction_no_dev_fatal;
1869 }
1870 }
1871
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001872 if (num_queues == 1) {
1873 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
1874 if (err)
1875 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001876 } else {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001877 /* Write the keys for each queue */
1878 for (i = 0; i < num_queues; ++i) {
1879 queue = &info->queues[i];
1880 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
1881 if (err)
1882 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001883 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001884 }
1885
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001886 /* The remaining keys are not queue-specific */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001887 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1888 1);
1889 if (err) {
1890 message = "writing request-rx-copy";
1891 goto abort_transaction;
1892 }
1893
1894 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1895 if (err) {
1896 message = "writing feature-rx-notify";
1897 goto abort_transaction;
1898 }
1899
1900 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1901 if (err) {
1902 message = "writing feature-sg";
1903 goto abort_transaction;
1904 }
1905
1906 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1907 if (err) {
1908 message = "writing feature-gso-tcpv4";
1909 goto abort_transaction;
1910 }
1911
Paul Durrant2c0057d2014-01-15 17:30:33 +00001912 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
1913 if (err) {
1914 message = "writing feature-gso-tcpv6";
1915 goto abort_transaction;
1916 }
1917
1918 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
1919 "1");
1920 if (err) {
1921 message = "writing feature-ipv6-csum-offload";
1922 goto abort_transaction;
1923 }
1924
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001925 err = xenbus_transaction_end(xbt, 0);
1926 if (err) {
1927 if (err == -EAGAIN)
1928 goto again;
1929 xenbus_dev_fatal(dev, err, "completing transaction");
1930 goto destroy_ring;
1931 }
1932
1933 return 0;
1934
1935 abort_transaction:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001936 xenbus_dev_fatal(dev, err, "%s", message);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001937abort_transaction_no_dev_fatal:
1938 xenbus_transaction_end(xbt, 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001939 destroy_ring:
1940 xennet_disconnect_backend(info);
Ross Lagerwallf599c642018-01-11 09:36:38 +00001941 rtnl_lock();
Ross Lagerwalle2e004a2017-02-08 10:57:37 +00001942 xennet_destroy_queues(info);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001943 out:
Ross Lagerwallf599c642018-01-11 09:36:38 +00001944 rtnl_unlock();
Ross Lagerwallcb257782018-06-21 14:00:20 +01001945out_unlocked:
Vitaly Kuznetsovd86b5672017-05-11 13:58:06 +02001946 device_unregister(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001947 return err;
1948}
1949
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001950static int xennet_connect(struct net_device *dev)
1951{
1952 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001953 unsigned int num_queues = 0;
David Vrabela5b5dc32014-07-31 17:38:23 +01001954 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001955 unsigned int j = 0;
1956 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001957
Juergen Gross2890ea52016-10-31 14:58:41 +01001958 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001959 dev_info(&dev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07001960 "backend does not support copying receive path\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001961 return -ENODEV;
1962 }
1963
Ian Campbellf502bf22010-08-18 23:27:49 +00001964 err = talk_to_netback(np->xbdev, np);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001965 if (err)
1966 return err;
1967
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001968 /* talk_to_netback() sets the correct number of queues */
1969 num_queues = dev->real_num_tx_queues;
1970
Ross Lagerwallf599c642018-01-11 09:36:38 +00001971 if (dev->reg_state == NETREG_UNINITIALIZED) {
1972 err = register_netdev(dev);
1973 if (err) {
1974 pr_warn("%s: register_netdev err=%d\n", __func__, err);
1975 device_unregister(&np->xbdev->dev);
1976 return err;
1977 }
1978 }
1979
Ross Lagerwall45c81842018-06-21 14:00:21 +01001980 rtnl_lock();
1981 netdev_update_features(dev);
1982 rtnl_unlock();
1983
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001984 /*
David Vrabela5b5dc32014-07-31 17:38:23 +01001985 * All public and private state should now be sane. Get
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001986 * ready to start sending and receiving packets and give the driver
1987 * domain a kick because we've probably just requeued some
1988 * packets.
1989 */
1990 netif_carrier_on(np->netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001991 for (j = 0; j < num_queues; ++j) {
1992 queue = &np->queues[j];
David Vrabelf50b4072014-07-02 16:09:14 +01001993
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001994 notify_remote_via_irq(queue->tx_irq);
1995 if (queue->tx_irq != queue->rx_irq)
1996 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001997
David Vrabelf50b4072014-07-02 16:09:14 +01001998 spin_lock_irq(&queue->tx_lock);
1999 xennet_tx_buf_gc(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002000 spin_unlock_irq(&queue->tx_lock);
David Vrabelf50b4072014-07-02 16:09:14 +01002001
2002 spin_lock_bh(&queue->rx_lock);
2003 xennet_alloc_rx_buffers(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002004 spin_unlock_bh(&queue->rx_lock);
2005 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002006
2007 return 0;
2008}
2009
2010/**
2011 * Callback received when the backend's state changes.
2012 */
Ian Campbellf502bf22010-08-18 23:27:49 +00002013static void netback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002014 enum xenbus_state backend_state)
2015{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002016 struct netfront_info *np = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002017 struct net_device *netdev = np->netdev;
2018
2019 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2020
Juergen Gross8edfe2e2018-09-07 14:21:30 +02002021 wake_up_all(&module_wq);
2022
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002023 switch (backend_state) {
2024 case XenbusStateInitialising:
2025 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002026 case XenbusStateReconfiguring:
2027 case XenbusStateReconfigured:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002028 case XenbusStateUnknown:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002029 break;
2030
2031 case XenbusStateInitWait:
2032 if (dev->state != XenbusStateInitialising)
2033 break;
2034 if (xennet_connect(netdev) != 0)
2035 break;
2036 xenbus_switch_state(dev, XenbusStateConnected);
Laszlo Ersek08e34eb2011-12-11 01:48:59 +00002037 break;
2038
2039 case XenbusStateConnected:
Amerigo Wangee89bab2012-08-09 22:14:56 +00002040 netdev_notify_peers(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002041 break;
2042
David Vrabelbce3ea82014-02-04 18:50:26 +00002043 case XenbusStateClosed:
2044 if (dev->state == XenbusStateClosed)
2045 break;
Gustavo A. R. Silvaa32b9d92019-04-15 16:11:41 -05002046 /* Fall through - Missed the backend's CLOSING state. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002047 case XenbusStateClosing:
2048 xenbus_frontend_closed(dev);
2049 break;
2050 }
2051}
2052
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002053static const struct xennet_stat {
2054 char name[ETH_GSTRING_LEN];
2055 u16 offset;
2056} xennet_stats[] = {
2057 {
2058 "rx_gso_checksum_fixup",
2059 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2060 },
2061};
2062
2063static int xennet_get_sset_count(struct net_device *dev, int string_set)
2064{
2065 switch (string_set) {
2066 case ETH_SS_STATS:
2067 return ARRAY_SIZE(xennet_stats);
2068 default:
2069 return -EINVAL;
2070 }
2071}
2072
2073static void xennet_get_ethtool_stats(struct net_device *dev,
2074 struct ethtool_stats *stats, u64 * data)
2075{
2076 void *np = netdev_priv(dev);
2077 int i;
2078
2079 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002080 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002081}
2082
2083static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2084{
2085 int i;
2086
2087 switch (stringset) {
2088 case ETH_SS_STATS:
2089 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2090 memcpy(data + i * ETH_GSTRING_LEN,
2091 xennet_stats[i].name, ETH_GSTRING_LEN);
2092 break;
2093 }
2094}
2095
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002096static const struct ethtool_ops xennet_ethtool_ops =
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002097{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002098 .get_link = ethtool_op_get_link,
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002099
2100 .get_sset_count = xennet_get_sset_count,
2101 .get_ethtool_stats = xennet_get_ethtool_stats,
2102 .get_strings = xennet_get_strings,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002103};
2104
2105#ifdef CONFIG_SYSFS
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002106static ssize_t show_rxbuf(struct device *dev,
2107 struct device_attribute *attr, char *buf)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002108{
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002109 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002110}
2111
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002112static ssize_t store_rxbuf(struct device *dev,
2113 struct device_attribute *attr,
2114 const char *buf, size_t len)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002115{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002116 char *endp;
2117 unsigned long target;
2118
2119 if (!capable(CAP_NET_ADMIN))
2120 return -EPERM;
2121
2122 target = simple_strtoul(buf, &endp, 0);
2123 if (endp == buf)
2124 return -EBADMSG;
2125
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002126 /* rxbuf_min and rxbuf_max are no longer configurable. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002127
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002128 return len;
2129}
2130
Joe Perchesd61e4032018-03-23 15:54:39 -07002131static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2132static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2133static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
Takashi Iwai27b917e2015-02-04 14:38:55 +01002134
2135static struct attribute *xennet_dev_attrs[] = {
2136 &dev_attr_rxbuf_min.attr,
2137 &dev_attr_rxbuf_max.attr,
2138 &dev_attr_rxbuf_cur.attr,
2139 NULL
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002140};
2141
Takashi Iwai27b917e2015-02-04 14:38:55 +01002142static const struct attribute_group xennet_dev_group = {
2143 .attrs = xennet_dev_attrs
2144};
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002145#endif /* CONFIG_SYSFS */
2146
Andrea Righic2c63312020-07-24 10:59:10 +02002147static void xennet_bus_close(struct xenbus_device *dev)
2148{
2149 int ret;
2150
2151 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2152 return;
2153 do {
2154 xenbus_switch_state(dev, XenbusStateClosing);
2155 ret = wait_event_timeout(module_wq,
2156 xenbus_read_driver_state(dev->otherend) ==
2157 XenbusStateClosing ||
2158 xenbus_read_driver_state(dev->otherend) ==
2159 XenbusStateClosed ||
2160 xenbus_read_driver_state(dev->otherend) ==
2161 XenbusStateUnknown,
2162 XENNET_TIMEOUT);
2163 } while (!ret);
2164
2165 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2166 return;
2167
2168 do {
2169 xenbus_switch_state(dev, XenbusStateClosed);
2170 ret = wait_event_timeout(module_wq,
2171 xenbus_read_driver_state(dev->otherend) ==
2172 XenbusStateClosed ||
2173 xenbus_read_driver_state(dev->otherend) ==
2174 XenbusStateUnknown,
2175 XENNET_TIMEOUT);
2176 } while (!ret);
2177}
2178
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002179static int xennet_remove(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002180{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002181 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002182
Andrea Righic2c63312020-07-24 10:59:10 +02002183 xennet_bus_close(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002184 xennet_disconnect_backend(info);
2185
Ross Lagerwallf599c642018-01-11 09:36:38 +00002186 if (info->netdev->reg_state == NETREG_REGISTERED)
2187 unregister_netdev(info->netdev);
Ian Campbell6bc96d02012-06-25 22:48:41 +00002188
Ross Lagerwallf599c642018-01-11 09:36:38 +00002189 if (info->queues) {
2190 rtnl_lock();
Chas Williams9a873c72015-08-19 19:14:20 -04002191 xennet_destroy_queues(info);
Ross Lagerwallf599c642018-01-11 09:36:38 +00002192 rtnl_unlock();
2193 }
David Vrabel900e1832015-01-13 16:42:42 +00002194 xennet_free_netdev(info->netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002195
2196 return 0;
2197}
2198
David Vrabel95afae42014-09-08 17:30:41 +01002199static const struct xenbus_device_id netfront_ids[] = {
2200 { "vif" },
2201 { "" }
2202};
2203
2204static struct xenbus_driver netfront_driver = {
2205 .ids = netfront_ids,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002206 .probe = netfront_probe,
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002207 .remove = xennet_remove,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002208 .resume = netfront_resume,
Ian Campbellf502bf22010-08-18 23:27:49 +00002209 .otherend_changed = netback_changed,
David Vrabel95afae42014-09-08 17:30:41 +01002210};
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002211
2212static int __init netif_init(void)
2213{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002214 if (!xen_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002215 return -ENODEV;
2216
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002217 if (!xen_has_pv_nic_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002218 return -ENODEV;
2219
Joe Perches383eda32013-06-27 21:57:49 -07002220 pr_info("Initialising Xen virtual ethernet driver\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002221
Juergen Gross034702a2017-01-10 14:32:51 +01002222 /* Allow as many queues as there are CPUs inut max. 8 if user has not
Wei Liu32a84402015-09-10 11:18:58 +01002223 * specified a value.
2224 */
2225 if (xennet_max_queues == 0)
Juergen Gross034702a2017-01-10 14:32:51 +01002226 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2227 num_online_cpus());
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002228
Al Viroffb78a22008-11-22 17:38:14 +00002229 return xenbus_register_frontend(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002230}
2231module_init(netif_init);
2232
2233
2234static void __exit netif_exit(void)
2235{
Al Viroffb78a22008-11-22 17:38:14 +00002236 xenbus_unregister_driver(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002237}
2238module_exit(netif_exit);
2239
2240MODULE_DESCRIPTION("Xen virtual network device frontend");
2241MODULE_LICENSE("GPL");
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002242MODULE_ALIAS("xen:vif");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002243MODULE_ALIAS("xennet");