]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/xen-netfront.c
xen/netfront: fix leaking data in shared pages
[mirror_ubuntu-jammy-kernel.git] / drivers / net / xen-netfront.c
CommitLineData
0d160211
JF
1/*
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
383eda32
JP
32#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
0d160211
JF
34#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>
9ecd1a75 41#include <net/tcp.h>
0d160211
JF
42#include <linux/udp.h>
43#include <linux/moduleparam.h>
44#include <linux/mm.h>
5a0e3ad6 45#include <linux/slab.h>
0d160211 46#include <net/ip.h>
6c5aa6fc
DK
47#include <linux/bpf.h>
48#include <net/page_pool.h>
49#include <linux/bpf_trace.h>
0d160211 50
1ccbf534 51#include <xen/xen.h>
0d160211
JF
52#include <xen/xenbus.h>
53#include <xen/events.h>
54#include <xen/page.h>
b9136d20 55#include <xen/platform_pci.h>
0d160211
JF
56#include <xen/grant_table.h>
57
58#include <xen/interface/io/netif.h>
59#include <xen/interface/memory.h>
60#include <xen/interface/grant_table.h>
61
50ee6061 62/* Module parameters */
034702a6 63#define MAX_QUEUES_DEFAULT 8
50ee6061
AB
64static unsigned int xennet_max_queues;
65module_param_named(max_queues, xennet_max_queues, uint, 0644);
66MODULE_PARM_DESC(max_queues,
67 "Maximum number of queues per virtual interface");
68
c2c63310
AR
69#define XENNET_TIMEOUT (5 * HZ)
70
0fc0b732 71static const struct ethtool_ops xennet_ethtool_ops;
0d160211
JF
72
73struct netfront_cb {
3683243b 74 int pull_to;
0d160211
JF
75};
76
77#define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
78
79#define RX_COPY_THRESHOLD 256
80
81#define GRANT_INVALID_REF 0
82
30c5d7f0
JG
83#define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
84#define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
1f3c2eba
DV
85
86/* Minimum number of Rx slots (includes slot for GSO metadata). */
87#define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
0d160211 88
2688fcb7
AB
89/* Queue name is interface name with "-qNNN" appended */
90#define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
91
92/* IRQ name is queue name with "-tx" or "-rx" appended */
93#define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
94
8edfe2e9 95static DECLARE_WAIT_QUEUE_HEAD(module_wq);
5b5971df 96
e00f85be 97struct netfront_stats {
900e1833
DV
98 u64 packets;
99 u64 bytes;
e00f85be 100 struct u64_stats_sync syncp;
101};
102
2688fcb7
AB
103struct netfront_info;
104
105struct netfront_queue {
106 unsigned int id; /* Queue ID, 0-based */
107 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
108 struct netfront_info *info;
0d160211 109
6c5aa6fc
DK
110 struct bpf_prog __rcu *xdp_prog;
111
bea3348e 112 struct napi_struct napi;
0d160211 113
d634bf2c
WL
114 /* Split event channels support, tx_* == rx_* when using
115 * single event channel.
116 */
117 unsigned int tx_evtchn, rx_evtchn;
118 unsigned int tx_irq, rx_irq;
119 /* Only used when split event channels support is enabled */
2688fcb7
AB
120 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
121 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
0d160211 122
84284d3c
JF
123 spinlock_t tx_lock;
124 struct xen_netif_tx_front_ring tx;
125 int tx_ring_ref;
0d160211
JF
126
127 /*
128 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
21631d2d 129 * are linked from tx_skb_freelist through tx_link.
0d160211 130 */
21631d2d
JG
131 struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
132 unsigned short tx_link[NET_TX_RING_SIZE];
133#define TX_LINK_NONE 0xffff
a884daa6 134#define TX_PENDING 0xfffe
0d160211
JF
135 grant_ref_t gref_tx_head;
136 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
cefe0078 137 struct page *grant_tx_page[NET_TX_RING_SIZE];
0d160211 138 unsigned tx_skb_freelist;
a884daa6 139 unsigned int tx_pend_queue;
0d160211 140
84284d3c
JF
141 spinlock_t rx_lock ____cacheline_aligned_in_smp;
142 struct xen_netif_rx_front_ring rx;
143 int rx_ring_ref;
144
84284d3c
JF
145 struct timer_list rx_refill_timer;
146
0d160211
JF
147 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
148 grant_ref_t gref_rx_head;
149 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
6c5aa6fc 150
900e0ac2
JG
151 unsigned int rx_rsp_unconsumed;
152 spinlock_t rx_cons_lock;
153
6c5aa6fc
DK
154 struct page_pool *page_pool;
155 struct xdp_rxq_info xdp_rxq;
2688fcb7
AB
156};
157
158struct netfront_info {
159 struct list_head list;
160 struct net_device *netdev;
161
162 struct xenbus_device *xbdev;
163
164 /* Multi-queue support */
165 struct netfront_queue *queues;
e0ce4af9
IC
166
167 /* Statistics */
900e1833
DV
168 struct netfront_stats __percpu *rx_stats;
169 struct netfront_stats __percpu *tx_stats;
e00f85be 170
6c5aa6fc
DK
171 /* XDP state */
172 bool netback_has_xdp_headroom;
173 bool netfront_xdp_enabled;
174
a884daa6
JG
175 /* Is device behaving sane? */
176 bool broken;
177
2688fcb7 178 atomic_t rx_gso_checksum_fixup;
0d160211
JF
179};
180
181struct netfront_rx_info {
182 struct xen_netif_rx_response rx;
183 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
184};
185
186/*
187 * Access macros for acquiring freeing slots in tx_skbs[].
188 */
189
21631d2d
JG
190static void add_id_to_list(unsigned *head, unsigned short *list,
191 unsigned short id)
0d160211 192{
21631d2d 193 list[id] = *head;
0d160211
JF
194 *head = id;
195}
196
21631d2d 197static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
0d160211
JF
198{
199 unsigned int id = *head;
21631d2d
JG
200
201 if (id != TX_LINK_NONE) {
202 *head = list[id];
203 list[id] = TX_LINK_NONE;
204 }
0d160211
JF
205 return id;
206}
207
208static int xennet_rxidx(RING_IDX idx)
209{
210 return idx & (NET_RX_RING_SIZE - 1);
211}
212
2688fcb7 213static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
0d160211
JF
214 RING_IDX ri)
215{
216 int i = xennet_rxidx(ri);
2688fcb7
AB
217 struct sk_buff *skb = queue->rx_skbs[i];
218 queue->rx_skbs[i] = NULL;
0d160211
JF
219 return skb;
220}
221
2688fcb7 222static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
0d160211
JF
223 RING_IDX ri)
224{
225 int i = xennet_rxidx(ri);
2688fcb7
AB
226 grant_ref_t ref = queue->grant_rx_ref[i];
227 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
0d160211
JF
228 return ref;
229}
230
231#ifdef CONFIG_SYSFS
27b917e5 232static const struct attribute_group xennet_dev_group;
0d160211
JF
233#endif
234
3ad9b358 235static bool xennet_can_sg(struct net_device *dev)
0d160211 236{
3ad9b358 237 return dev->features & NETIF_F_SG;
0d160211
JF
238}
239
240
e99e88a9 241static void rx_refill_timeout(struct timer_list *t)
0d160211 242{
e99e88a9 243 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
2688fcb7 244 napi_schedule(&queue->napi);
0d160211
JF
245}
246
2688fcb7 247static int netfront_tx_slot_available(struct netfront_queue *queue)
0d160211 248{
2688fcb7 249 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
57f230ab 250 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
0d160211
JF
251}
252
2688fcb7 253static void xennet_maybe_wake_tx(struct netfront_queue *queue)
0d160211 254{
2688fcb7
AB
255 struct net_device *dev = queue->info->netdev;
256 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
0d160211 257
2688fcb7
AB
258 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
259 netfront_tx_slot_available(queue) &&
0d160211 260 likely(netif_running(dev)))
2688fcb7 261 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
0d160211
JF
262}
263
1f3c2eba
DV
264
265static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
0d160211 266{
0d160211
JF
267 struct sk_buff *skb;
268 struct page *page;
0d160211 269
1f3c2eba
DV
270 skb = __netdev_alloc_skb(queue->info->netdev,
271 RX_COPY_THRESHOLD + NET_IP_ALIGN,
272 GFP_ATOMIC | __GFP_NOWARN);
273 if (unlikely(!skb))
274 return NULL;
0d160211 275
6240a8a6
RPM
276 page = page_pool_alloc_pages(queue->page_pool,
277 GFP_ATOMIC | __GFP_NOWARN | __GFP_ZERO);
6c5aa6fc 278 if (unlikely(!page)) {
1f3c2eba
DV
279 kfree_skb(skb);
280 return NULL;
0d160211 281 }
1f3c2eba
DV
282 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
283
284 /* Align ip header to a 16 bytes boundary */
285 skb_reserve(skb, NET_IP_ALIGN);
286 skb->dev = queue->info->netdev;
287
288 return skb;
289}
0d160211 290
1f3c2eba
DV
291
292static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
293{
294 RING_IDX req_prod = queue->rx.req_prod_pvt;
295 int notify;
538d9291 296 int err = 0;
1f3c2eba
DV
297
298 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
0d160211 299 return;
0d160211 300
1f3c2eba
DV
301 for (req_prod = queue->rx.req_prod_pvt;
302 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
303 req_prod++) {
304 struct sk_buff *skb;
305 unsigned short id;
306 grant_ref_t ref;
30c5d7f0 307 struct page *page;
1f3c2eba 308 struct xen_netif_rx_request *req;
0d160211 309
1f3c2eba 310 skb = xennet_alloc_one_rx_buffer(queue);
538d9291
VRP
311 if (!skb) {
312 err = -ENOMEM;
0d160211 313 break;
538d9291 314 }
0d160211 315
1f3c2eba 316 id = xennet_rxidx(req_prod);
0d160211 317
2688fcb7
AB
318 BUG_ON(queue->rx_skbs[id]);
319 queue->rx_skbs[id] = skb;
0d160211 320
2688fcb7 321 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
269ebce4 322 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
2688fcb7 323 queue->grant_rx_ref[id] = ref;
0d160211 324
30c5d7f0 325 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
0d160211 326
1f3c2eba 327 req = RING_GET_REQUEST(&queue->rx, req_prod);
30c5d7f0
JG
328 gnttab_page_grant_foreign_access_ref_one(ref,
329 queue->info->xbdev->otherend_id,
330 page,
331 0);
0d160211
JF
332 req->id = id;
333 req->gref = ref;
334 }
335
1f3c2eba
DV
336 queue->rx.req_prod_pvt = req_prod;
337
538d9291
VRP
338 /* Try again later if there are not enough requests or skb allocation
339 * failed.
340 * Enough requests is quantified as the sum of newly created slots and
341 * the unconsumed slots at the backend.
342 */
343 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
344 unlikely(err)) {
1f3c2eba
DV
345 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
346 return;
347 }
348
2688fcb7 349 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
0d160211 350 if (notify)
2688fcb7 351 notify_remote_via_irq(queue->rx_irq);
0d160211
JF
352}
353
354static int xennet_open(struct net_device *dev)
355{
356 struct netfront_info *np = netdev_priv(dev);
2688fcb7
AB
357 unsigned int num_queues = dev->real_num_tx_queues;
358 unsigned int i = 0;
359 struct netfront_queue *queue = NULL;
360
a884daa6 361 if (!np->queues || np->broken)
f599c64f
RL
362 return -ENODEV;
363
2688fcb7
AB
364 for (i = 0; i < num_queues; ++i) {
365 queue = &np->queues[i];
366 napi_enable(&queue->napi);
367
368 spin_lock_bh(&queue->rx_lock);
369 if (netif_carrier_ok(dev)) {
370 xennet_alloc_rx_buffers(queue);
371 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
372 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
373 napi_schedule(&queue->napi);
374 }
375 spin_unlock_bh(&queue->rx_lock);
0d160211 376 }
0d160211 377
2688fcb7 378 netif_tx_start_all_queues(dev);
0d160211
JF
379
380 return 0;
381}
382
900e0ac2 383static bool xennet_tx_buf_gc(struct netfront_queue *queue)
0d160211
JF
384{
385 RING_IDX cons, prod;
386 unsigned short id;
0d160211 387 struct sk_buff *skb;
7d0105b5 388 bool more_to_do;
900e0ac2 389 bool work_done = false;
a884daa6 390 const struct device *dev = &queue->info->netdev->dev;
0d160211 391
2688fcb7 392 BUG_ON(!netif_carrier_ok(queue->info->netdev));
0d160211
JF
393
394 do {
2688fcb7 395 prod = queue->tx.sring->rsp_prod;
a884daa6
JG
396 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
397 dev_alert(dev, "Illegal number of responses %u\n",
398 prod - queue->tx.rsp_cons);
399 goto err;
400 }
0d160211
JF
401 rmb(); /* Ensure we see responses up to 'rp'. */
402
2688fcb7 403 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
8446066b 404 struct xen_netif_tx_response txrsp;
0d160211 405
900e0ac2
JG
406 work_done = true;
407
8446066b
JG
408 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
409 if (txrsp.status == XEN_NETIF_RSP_NULL)
0d160211
JF
410 continue;
411
a884daa6
JG
412 id = txrsp.id;
413 if (id >= RING_SIZE(&queue->tx)) {
414 dev_alert(dev,
415 "Response has incorrect id (%u)\n",
416 id);
417 goto err;
418 }
419 if (queue->tx_link[id] != TX_PENDING) {
420 dev_alert(dev,
421 "Response for inactive request\n");
422 goto err;
423 }
424
425 queue->tx_link[id] = TX_LINK_NONE;
21631d2d
JG
426 skb = queue->tx_skbs[id];
427 queue->tx_skbs[id] = NULL;
b602c2e0
JG
428 if (unlikely(!gnttab_end_foreign_access_ref(
429 queue->grant_tx_ref[id], GNTMAP_readonly))) {
a884daa6
JG
430 dev_alert(dev,
431 "Grant still in use by backend domain\n");
432 goto err;
0d160211 433 }
0d160211 434 gnttab_release_grant_reference(
2688fcb7
AB
435 &queue->gref_tx_head, queue->grant_tx_ref[id]);
436 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
437 queue->grant_tx_page[id] = NULL;
21631d2d 438 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
0d160211
JF
439 dev_kfree_skb_irq(skb);
440 }
441
2688fcb7 442 queue->tx.rsp_cons = prod;
0d160211 443
7d0105b5
MC
444 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
445 } while (more_to_do);
0d160211 446
2688fcb7 447 xennet_maybe_wake_tx(queue);
a884daa6 448
900e0ac2 449 return work_done;
a884daa6
JG
450
451 err:
452 queue->info->broken = true;
453 dev_alert(dev, "Disabled for further use\n");
900e0ac2
JG
454
455 return work_done;
0d160211
JF
456}
457
30c5d7f0
JG
458struct xennet_gnttab_make_txreq {
459 struct netfront_queue *queue;
460 struct sk_buff *skb;
461 struct page *page;
162081ec
JG
462 struct xen_netif_tx_request *tx; /* Last request on ring page */
463 struct xen_netif_tx_request tx_local; /* Last request local copy*/
30c5d7f0
JG
464 unsigned int size;
465};
466
467static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
468 unsigned int len, void *data)
0d160211 469{
30c5d7f0 470 struct xennet_gnttab_make_txreq *info = data;
0d160211 471 unsigned int id;
a55e8bb8 472 struct xen_netif_tx_request *tx;
0d160211 473 grant_ref_t ref;
30c5d7f0
JG
474 /* convenient aliases */
475 struct page *page = info->page;
476 struct netfront_queue *queue = info->queue;
477 struct sk_buff *skb = info->skb;
0d160211 478
21631d2d 479 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
a55e8bb8
DV
480 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
481 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
269ebce4 482 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
0d160211 483
30c5d7f0
JG
484 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
485 gfn, GNTMAP_readonly);
0d160211 486
21631d2d 487 queue->tx_skbs[id] = skb;
a55e8bb8
DV
488 queue->grant_tx_page[id] = page;
489 queue->grant_tx_ref[id] = ref;
0d160211 490
162081ec
JG
491 info->tx_local.id = id;
492 info->tx_local.gref = ref;
493 info->tx_local.offset = offset;
494 info->tx_local.size = len;
495 info->tx_local.flags = 0;
496
497 *tx = info->tx_local;
0d160211 498
a884daa6
JG
499 /*
500 * Put the request in the pending queue, it will be set to be pending
501 * when the producer index is about to be raised.
502 */
503 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
504
30c5d7f0 505 info->tx = tx;
162081ec 506 info->size += info->tx_local.size;
30c5d7f0
JG
507}
508
509static struct xen_netif_tx_request *xennet_make_first_txreq(
162081ec
JG
510 struct xennet_gnttab_make_txreq *info,
511 unsigned int offset, unsigned int len)
30c5d7f0 512{
162081ec 513 info->size = 0;
30c5d7f0 514
162081ec 515 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
30c5d7f0 516
162081ec 517 return info->tx;
30c5d7f0
JG
518}
519
520static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
521 unsigned int len, void *data)
522{
523 struct xennet_gnttab_make_txreq *info = data;
524
525 info->tx->flags |= XEN_NETTXF_more_data;
526 skb_get(info->skb);
527 xennet_tx_setup_grant(gfn, offset, len, data);
a55e8bb8 528}
0d160211 529
162081ec
JG
530static void xennet_make_txreqs(
531 struct xennet_gnttab_make_txreq *info,
532 struct page *page,
a55e8bb8
DV
533 unsigned int offset, unsigned int len)
534{
535 /* Skip unused frames from start of page */
536 page += offset >> PAGE_SHIFT;
537 offset &= ~PAGE_MASK;
0d160211 538
a55e8bb8 539 while (len) {
162081ec
JG
540 info->page = page;
541 info->size = 0;
30c5d7f0
JG
542
543 gnttab_foreach_grant_in_range(page, offset, len,
544 xennet_make_one_txreq,
162081ec 545 info);
30c5d7f0 546
a55e8bb8
DV
547 page++;
548 offset = 0;
162081ec 549 len -= info->size;
0d160211 550 }
0d160211
JF
551}
552
f36c3747 553/*
e84448d5
DV
554 * Count how many ring slots are required to send this skb. Each frag
555 * might be a compound page.
f36c3747 556 */
e84448d5 557static int xennet_count_skb_slots(struct sk_buff *skb)
f36c3747
IC
558{
559 int i, frags = skb_shinfo(skb)->nr_frags;
30c5d7f0 560 int slots;
e84448d5 561
30c5d7f0
JG
562 slots = gnttab_count_grant(offset_in_page(skb->data),
563 skb_headlen(skb));
f36c3747
IC
564
565 for (i = 0; i < frags; i++) {
566 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
567 unsigned long size = skb_frag_size(frag);
b54c9d5b 568 unsigned long offset = skb_frag_off(frag);
f36c3747
IC
569
570 /* Skip unused frames from start of page */
571 offset &= ~PAGE_MASK;
572
30c5d7f0 573 slots += gnttab_count_grant(offset, size);
f36c3747
IC
574 }
575
30c5d7f0 576 return slots;
f36c3747
IC
577}
578
50ee6061 579static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
a350ecce 580 struct net_device *sb_dev)
2688fcb7 581{
50ee6061
AB
582 unsigned int num_queues = dev->real_num_tx_queues;
583 u32 hash;
584 u16 queue_idx;
585
586 /* First, check if there is only one queue */
587 if (num_queues == 1) {
588 queue_idx = 0;
589 } else {
590 hash = skb_get_hash(skb);
591 queue_idx = hash % num_queues;
592 }
593
594 return queue_idx;
2688fcb7
AB
595}
596
a884daa6
JG
597static void xennet_mark_tx_pending(struct netfront_queue *queue)
598{
599 unsigned int i;
600
601 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
602 TX_LINK_NONE)
603 queue->tx_link[i] = TX_PENDING;
604}
605
6c5aa6fc
DK
606static int xennet_xdp_xmit_one(struct net_device *dev,
607 struct netfront_queue *queue,
608 struct xdp_frame *xdpf)
609{
610 struct netfront_info *np = netdev_priv(dev);
611 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
162081ec
JG
612 struct xennet_gnttab_make_txreq info = {
613 .queue = queue,
614 .skb = NULL,
615 .page = virt_to_page(xdpf->data),
616 };
6c5aa6fc
DK
617 int notify;
618
162081ec 619 xennet_make_first_txreq(&info,
6c5aa6fc
DK
620 offset_in_page(xdpf->data),
621 xdpf->len);
622
a884daa6
JG
623 xennet_mark_tx_pending(queue);
624
6c5aa6fc
DK
625 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
626 if (notify)
627 notify_remote_via_irq(queue->tx_irq);
628
629 u64_stats_update_begin(&tx_stats->syncp);
630 tx_stats->bytes += xdpf->len;
631 tx_stats->packets++;
632 u64_stats_update_end(&tx_stats->syncp);
633
634 xennet_tx_buf_gc(queue);
635
636 return 0;
637}
638
639static int xennet_xdp_xmit(struct net_device *dev, int n,
640 struct xdp_frame **frames, u32 flags)
641{
642 unsigned int num_queues = dev->real_num_tx_queues;
643 struct netfront_info *np = netdev_priv(dev);
644 struct netfront_queue *queue = NULL;
645 unsigned long irq_flags;
fdc13979
LB
646 int nxmit = 0;
647 int i;
6c5aa6fc 648
a884daa6
JG
649 if (unlikely(np->broken))
650 return -ENODEV;
6c5aa6fc
DK
651 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
652 return -EINVAL;
653
654 queue = &np->queues[smp_processor_id() % num_queues];
655
656 spin_lock_irqsave(&queue->tx_lock, irq_flags);
657 for (i = 0; i < n; i++) {
658 struct xdp_frame *xdpf = frames[i];
659
660 if (!xdpf)
661 continue;
fdc13979
LB
662 if (xennet_xdp_xmit_one(dev, queue, xdpf))
663 break;
664 nxmit++;
6c5aa6fc
DK
665 }
666 spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
667
fdc13979 668 return nxmit;
6c5aa6fc
DK
669}
670
671
30c5d7f0
JG
672#define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
673
24a94b3c 674static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
0d160211 675{
0d160211 676 struct netfront_info *np = netdev_priv(dev);
900e1833 677 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
162081ec 678 struct xen_netif_tx_request *first_tx;
a55e8bb8 679 unsigned int i;
0d160211 680 int notify;
f36c3747 681 int slots;
a55e8bb8
DV
682 struct page *page;
683 unsigned int offset;
684 unsigned int len;
cf66f9d4 685 unsigned long flags;
2688fcb7 686 struct netfront_queue *queue = NULL;
162081ec 687 struct xennet_gnttab_make_txreq info = { };
2688fcb7
AB
688 unsigned int num_queues = dev->real_num_tx_queues;
689 u16 queue_index;
fd07160b 690 struct sk_buff *nskb;
2688fcb7
AB
691
692 /* Drop the packet if no queues are set up */
693 if (num_queues < 1)
694 goto drop;
a884daa6
JG
695 if (unlikely(np->broken))
696 goto drop;
2688fcb7
AB
697 /* Determine which queue to transmit this SKB on */
698 queue_index = skb_get_queue_mapping(skb);
699 queue = &np->queues[queue_index];
0d160211 700
9ecd1a75
WL
701 /* If skb->len is too big for wire format, drop skb and alert
702 * user about misconfiguration.
703 */
704 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
705 net_alert_ratelimited(
706 "xennet: skb->len = %u, too big for wire format\n",
707 skb->len);
708 goto drop;
709 }
710
e84448d5 711 slots = xennet_count_skb_slots(skb);
30c5d7f0 712 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
97a6d1bb
ZK
713 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
714 slots, skb->len);
715 if (skb_linearize(skb))
716 goto drop;
0d160211
JF
717 }
718
a55e8bb8
DV
719 page = virt_to_page(skb->data);
720 offset = offset_in_page(skb->data);
fd07160b
VK
721
722 /* The first req should be at least ETH_HLEN size or the packet will be
723 * dropped by netback.
724 */
725 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
726 nskb = skb_copy(skb, GFP_ATOMIC);
727 if (!nskb)
728 goto drop;
62f3250f 729 dev_consume_skb_any(skb);
fd07160b
VK
730 skb = nskb;
731 page = virt_to_page(skb->data);
732 offset = offset_in_page(skb->data);
733 }
734
a55e8bb8
DV
735 len = skb_headlen(skb);
736
2688fcb7 737 spin_lock_irqsave(&queue->tx_lock, flags);
0d160211
JF
738
739 if (unlikely(!netif_carrier_ok(dev) ||
f36c3747 740 (slots > 1 && !xennet_can_sg(dev)) ||
8b86a61d 741 netif_needs_gso(skb, netif_skb_features(skb)))) {
2688fcb7 742 spin_unlock_irqrestore(&queue->tx_lock, flags);
0d160211
JF
743 goto drop;
744 }
745
a55e8bb8 746 /* First request for the linear area. */
162081ec
JG
747 info.queue = queue;
748 info.skb = skb;
749 info.page = page;
750 first_tx = xennet_make_first_txreq(&info, offset, len);
751 offset += info.tx_local.size;
30c5d7f0
JG
752 if (offset == PAGE_SIZE) {
753 page++;
754 offset = 0;
755 }
162081ec 756 len -= info.tx_local.size;
0d160211 757
0d160211
JF
758 if (skb->ip_summed == CHECKSUM_PARTIAL)
759 /* local packet? */
162081ec
JG
760 first_tx->flags |= XEN_NETTXF_csum_blank |
761 XEN_NETTXF_data_validated;
0d160211
JF
762 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
763 /* remote but checksummed. */
162081ec 764 first_tx->flags |= XEN_NETTXF_data_validated;
0d160211 765
a55e8bb8 766 /* Optional extra info after the first request. */
0d160211
JF
767 if (skb_shinfo(skb)->gso_size) {
768 struct xen_netif_extra_info *gso;
769
770 gso = (struct xen_netif_extra_info *)
a55e8bb8 771 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
0d160211 772
162081ec 773 first_tx->flags |= XEN_NETTXF_extra_info;
0d160211
JF
774
775 gso->u.gso.size = skb_shinfo(skb)->gso_size;
2c0057de
PD
776 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
777 XEN_NETIF_GSO_TYPE_TCPV6 :
778 XEN_NETIF_GSO_TYPE_TCPV4;
0d160211
JF
779 gso->u.gso.pad = 0;
780 gso->u.gso.features = 0;
781
782 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
783 gso->flags = 0;
0d160211
JF
784 }
785
a55e8bb8 786 /* Requests for the rest of the linear area. */
162081ec 787 xennet_make_txreqs(&info, page, offset, len);
a55e8bb8
DV
788
789 /* Requests for all the frags. */
790 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
791 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
162081ec 792 xennet_make_txreqs(&info, skb_frag_page(frag),
b54c9d5b 793 skb_frag_off(frag),
a55e8bb8
DV
794 skb_frag_size(frag));
795 }
0d160211 796
a55e8bb8
DV
797 /* First request has the packet length. */
798 first_tx->size = skb->len;
0d160211 799
91ffb9d3
DD
800 /* timestamp packet in software */
801 skb_tx_timestamp(skb);
802
a884daa6
JG
803 xennet_mark_tx_pending(queue);
804
2688fcb7 805 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
0d160211 806 if (notify)
2688fcb7 807 notify_remote_via_irq(queue->tx_irq);
0d160211 808
900e1833
DV
809 u64_stats_update_begin(&tx_stats->syncp);
810 tx_stats->bytes += skb->len;
811 tx_stats->packets++;
812 u64_stats_update_end(&tx_stats->syncp);
10a273a6
JF
813
814 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
2688fcb7 815 xennet_tx_buf_gc(queue);
0d160211 816
2688fcb7
AB
817 if (!netfront_tx_slot_available(queue))
818 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
0d160211 819
2688fcb7 820 spin_unlock_irqrestore(&queue->tx_lock, flags);
0d160211 821
6ed10654 822 return NETDEV_TX_OK;
0d160211
JF
823
824 drop:
09f75cd7 825 dev->stats.tx_dropped++;
979de8a0 826 dev_kfree_skb_any(skb);
6ed10654 827 return NETDEV_TX_OK;
0d160211
JF
828}
829
830static int xennet_close(struct net_device *dev)
831{
832 struct netfront_info *np = netdev_priv(dev);
2688fcb7
AB
833 unsigned int num_queues = dev->real_num_tx_queues;
834 unsigned int i;
835 struct netfront_queue *queue;
836 netif_tx_stop_all_queues(np->netdev);
837 for (i = 0; i < num_queues; ++i) {
838 queue = &np->queues[i];
839 napi_disable(&queue->napi);
840 }
0d160211
JF
841 return 0;
842}
843
1d0ea124
MMG
844static void xennet_destroy_queues(struct netfront_info *info)
845{
846 unsigned int i;
847
848 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
849 struct netfront_queue *queue = &info->queues[i];
850
851 if (netif_running(info->netdev))
852 napi_disable(&queue->napi);
853 netif_napi_del(&queue->napi);
854 }
855
856 kfree(info->queues);
857 info->queues = NULL;
858}
859
860static void xennet_uninit(struct net_device *dev)
861{
862 struct netfront_info *np = netdev_priv(dev);
863 xennet_destroy_queues(np);
864}
865
900e0ac2
JG
866static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
867{
868 unsigned long flags;
869
870 spin_lock_irqsave(&queue->rx_cons_lock, flags);
871 queue->rx.rsp_cons = val;
872 queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
873 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
874}
875
2688fcb7 876static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
0d160211
JF
877 grant_ref_t ref)
878{
2688fcb7
AB
879 int new = xennet_rxidx(queue->rx.req_prod_pvt);
880
881 BUG_ON(queue->rx_skbs[new]);
882 queue->rx_skbs[new] = skb;
883 queue->grant_rx_ref[new] = ref;
884 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
885 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
886 queue->rx.req_prod_pvt++;
0d160211
JF
887}
888
2688fcb7 889static int xennet_get_extras(struct netfront_queue *queue,
0d160211
JF
890 struct xen_netif_extra_info *extras,
891 RING_IDX rp)
892
893{
8446066b 894 struct xen_netif_extra_info extra;
2688fcb7
AB
895 struct device *dev = &queue->info->netdev->dev;
896 RING_IDX cons = queue->rx.rsp_cons;
0d160211
JF
897 int err = 0;
898
899 do {
900 struct sk_buff *skb;
901 grant_ref_t ref;
902
903 if (unlikely(cons + 1 == rp)) {
904 if (net_ratelimit())
905 dev_warn(dev, "Missing extra info\n");
906 err = -EBADR;
907 break;
908 }
909
8446066b 910 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
0d160211 911
8446066b
JG
912 if (unlikely(!extra.type ||
913 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
0d160211
JF
914 if (net_ratelimit())
915 dev_warn(dev, "Invalid extra type: %d\n",
8446066b 916 extra.type);
0d160211
JF
917 err = -EINVAL;
918 } else {
8446066b 919 extras[extra.type - 1] = extra;
0d160211
JF
920 }
921
2688fcb7
AB
922 skb = xennet_get_rx_skb(queue, cons);
923 ref = xennet_get_rx_ref(queue, cons);
924 xennet_move_rx_slot(queue, skb, ref);
8446066b 925 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
0d160211 926
900e0ac2 927 xennet_set_rx_rsp_cons(queue, cons);
0d160211
JF
928 return err;
929}
930
6c5aa6fc
DK
931static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
932 struct xen_netif_rx_response *rx, struct bpf_prog *prog,
933 struct xdp_buff *xdp, bool *need_xdp_flush)
934{
935 struct xdp_frame *xdpf;
936 u32 len = rx->status;
e44f65fd 937 u32 act;
6c5aa6fc
DK
938 int err;
939
43b5169d
LB
940 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
941 &queue->xdp_rxq);
be9df4af
LB
942 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
943 len, false);
6c5aa6fc
DK
944
945 act = bpf_prog_run_xdp(prog, xdp);
946 switch (act) {
947 case XDP_TX:
948 get_page(pdata);
949 xdpf = xdp_convert_buff_to_frame(xdp);
950 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
fdc13979
LB
951 if (unlikely(!err))
952 xdp_return_frame_rx_napi(xdpf);
953 else if (unlikely(err < 0))
6c5aa6fc
DK
954 trace_xdp_exception(queue->info->netdev, prog, act);
955 break;
956 case XDP_REDIRECT:
957 get_page(pdata);
958 err = xdp_do_redirect(queue->info->netdev, xdp, prog);
959 *need_xdp_flush = true;
960 if (unlikely(err))
961 trace_xdp_exception(queue->info->netdev, prog, act);
962 break;
963 case XDP_PASS:
964 case XDP_DROP:
965 break;
966
967 case XDP_ABORTED:
968 trace_xdp_exception(queue->info->netdev, prog, act);
969 break;
970
971 default:
972 bpf_warn_invalid_xdp_action(act);
973 }
974
975 return act;
976}
977
2688fcb7 978static int xennet_get_responses(struct netfront_queue *queue,
0d160211 979 struct netfront_rx_info *rinfo, RING_IDX rp,
6c5aa6fc
DK
980 struct sk_buff_head *list,
981 bool *need_xdp_flush)
0d160211 982{
8446066b 983 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
6c5aa6fc 984 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
2688fcb7
AB
985 RING_IDX cons = queue->rx.rsp_cons;
986 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
6c5aa6fc 987 struct xen_netif_extra_info *extras = rinfo->extras;
2688fcb7 988 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
6c5aa6fc
DK
989 struct device *dev = &queue->info->netdev->dev;
990 struct bpf_prog *xdp_prog;
991 struct xdp_buff xdp;
7158ff6d 992 int slots = 1;
0d160211 993 int err = 0;
6c5aa6fc 994 u32 verdict;
0d160211 995
f942dc25 996 if (rx->flags & XEN_NETRXF_extra_info) {
2688fcb7 997 err = xennet_get_extras(queue, extras, rp);
6c5aa6fc
DK
998 if (!err) {
999 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
1000 struct xen_netif_extra_info *xdp;
1001
1002 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
1003 rx->offset = xdp->u.xdp.headroom;
1004 }
1005 }
2688fcb7 1006 cons = queue->rx.rsp_cons;
0d160211
JF
1007 }
1008
1009 for (;;) {
1010 if (unlikely(rx->status < 0 ||
30c5d7f0 1011 rx->offset + rx->status > XEN_PAGE_SIZE)) {
0d160211 1012 if (net_ratelimit())
6c10127d 1013 dev_warn(dev, "rx->offset: %u, size: %d\n",
0d160211 1014 rx->offset, rx->status);
2688fcb7 1015 xennet_move_rx_slot(queue, skb, ref);
0d160211
JF
1016 err = -EINVAL;
1017 goto next;
1018 }
1019
1020 /*
1021 * This definitely indicates a bug, either in this driver or in
1022 * the backend driver. In future this should flag the bad
697089dc 1023 * situation to the system controller to reboot the backend.
0d160211
JF
1024 */
1025 if (ref == GRANT_INVALID_REF) {
1026 if (net_ratelimit())
1027 dev_warn(dev, "Bad rx response id %d.\n",
1028 rx->id);
1029 err = -EINVAL;
1030 goto next;
1031 }
1032
60a69176
JG
1033 if (!gnttab_end_foreign_access_ref(ref, 0)) {
1034 dev_alert(dev,
1035 "Grant still in use by backend domain\n");
1036 queue->info->broken = true;
1037 dev_alert(dev, "Disabled for further use\n");
1038 return -EINVAL;
1039 }
0d160211 1040
2688fcb7 1041 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
0d160211 1042
6c5aa6fc
DK
1043 rcu_read_lock();
1044 xdp_prog = rcu_dereference(queue->xdp_prog);
1045 if (xdp_prog) {
1046 if (!(rx->flags & XEN_NETRXF_more_data)) {
1047 /* currently only a single page contains data */
1048 verdict = xennet_run_xdp(queue,
1049 skb_frag_page(&skb_shinfo(skb)->frags[0]),
1050 rx, xdp_prog, &xdp, need_xdp_flush);
1051 if (verdict != XDP_PASS)
1052 err = -EINVAL;
1053 } else {
1054 /* drop the frame */
1055 err = -EINVAL;
1056 }
1057 }
1058 rcu_read_unlock();
0d160211 1059next:
6c5aa6fc 1060 __skb_queue_tail(list, skb);
f942dc25 1061 if (!(rx->flags & XEN_NETRXF_more_data))
0d160211
JF
1062 break;
1063
7158ff6d 1064 if (cons + slots == rp) {
0d160211 1065 if (net_ratelimit())
7158ff6d 1066 dev_warn(dev, "Need more slots\n");
0d160211
JF
1067 err = -ENOENT;
1068 break;
1069 }
1070
8446066b
JG
1071 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
1072 rx = &rx_local;
2688fcb7
AB
1073 skb = xennet_get_rx_skb(queue, cons + slots);
1074 ref = xennet_get_rx_ref(queue, cons + slots);
7158ff6d 1075 slots++;
0d160211
JF
1076 }
1077
7158ff6d 1078 if (unlikely(slots > max)) {
0d160211 1079 if (net_ratelimit())
697089dc 1080 dev_warn(dev, "Too many slots\n");
0d160211
JF
1081 err = -E2BIG;
1082 }
1083
1084 if (unlikely(err))
900e0ac2 1085 xennet_set_rx_rsp_cons(queue, cons + slots);
0d160211
JF
1086
1087 return err;
1088}
1089
1090static int xennet_set_skb_gso(struct sk_buff *skb,
1091 struct xen_netif_extra_info *gso)
1092{
1093 if (!gso->u.gso.size) {
1094 if (net_ratelimit())
383eda32 1095 pr_warn("GSO size must not be zero\n");
0d160211
JF
1096 return -EINVAL;
1097 }
1098
2c0057de
PD
1099 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
1100 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
0d160211 1101 if (net_ratelimit())
383eda32 1102 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
0d160211
JF
1103 return -EINVAL;
1104 }
1105
1106 skb_shinfo(skb)->gso_size = gso->u.gso.size;
2c0057de
PD
1107 skb_shinfo(skb)->gso_type =
1108 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
1109 SKB_GSO_TCPV4 :
1110 SKB_GSO_TCPV6;
0d160211
JF
1111
1112 /* Header must be checked, and gso_segs computed. */
1113 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1114 skb_shinfo(skb)->gso_segs = 0;
1115
1116 return 0;
1117}
1118
a761129e
DZ
1119static int xennet_fill_frags(struct netfront_queue *queue,
1120 struct sk_buff *skb,
1121 struct sk_buff_head *list)
0d160211 1122{
2688fcb7 1123 RING_IDX cons = queue->rx.rsp_cons;
0d160211
JF
1124 struct sk_buff *nskb;
1125
1126 while ((nskb = __skb_dequeue(list))) {
8446066b 1127 struct xen_netif_rx_response rx;
01c68026 1128 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
0d160211 1129
8446066b
JG
1130 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1131
d472b3a6 1132 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
093b9c71 1133 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
0d160211 1134
d81c5054 1135 BUG_ON(pull_to < skb_headlen(skb));
093b9c71
JB
1136 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1137 }
ad4f15dc 1138 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
900e0ac2
JG
1139 xennet_set_rx_rsp_cons(queue,
1140 ++cons + skb_queue_len(list));
ad4f15dc 1141 kfree_skb(nskb);
a761129e 1142 return -ENOENT;
ad4f15dc 1143 }
093b9c71 1144
d472b3a6
JG
1145 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1146 skb_frag_page(nfrag),
8446066b 1147 rx.offset, rx.status, PAGE_SIZE);
0d160211
JF
1148
1149 skb_shinfo(nskb)->nr_frags = 0;
1150 kfree_skb(nskb);
0d160211
JF
1151 }
1152
900e0ac2 1153 xennet_set_rx_rsp_cons(queue, cons);
a761129e
DZ
1154
1155 return 0;
0d160211
JF
1156}
1157
e0ce4af9 1158static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
0d160211 1159{
b5cf66cd 1160 bool recalculate_partial_csum = false;
e0ce4af9
IC
1161
1162 /*
1163 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1164 * peers can fail to set NETRXF_csum_blank when sending a GSO
1165 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1166 * recalculate the partial checksum.
1167 */
1168 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1169 struct netfront_info *np = netdev_priv(dev);
2688fcb7 1170 atomic_inc(&np->rx_gso_checksum_fixup);
e0ce4af9 1171 skb->ip_summed = CHECKSUM_PARTIAL;
b5cf66cd 1172 recalculate_partial_csum = true;
e0ce4af9
IC
1173 }
1174
1175 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1176 if (skb->ip_summed != CHECKSUM_PARTIAL)
1177 return 0;
0d160211 1178
b5cf66cd 1179 return skb_checksum_setup(skb, recalculate_partial_csum);
0d160211
JF
1180}
1181
2688fcb7 1182static int handle_incoming_queue(struct netfront_queue *queue,
09f75cd7 1183 struct sk_buff_head *rxq)
0d160211 1184{
900e1833 1185 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
0d160211
JF
1186 int packets_dropped = 0;
1187 struct sk_buff *skb;
1188
1189 while ((skb = __skb_dequeue(rxq)) != NULL) {
3683243b 1190 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
0d160211 1191
093b9c71
JB
1192 if (pull_to > skb_headlen(skb))
1193 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
0d160211
JF
1194
1195 /* Ethernet work: Delayed to here as it peeks the header. */
2688fcb7 1196 skb->protocol = eth_type_trans(skb, queue->info->netdev);
d554f73d 1197 skb_reset_network_header(skb);
0d160211 1198
2688fcb7 1199 if (checksum_setup(queue->info->netdev, skb)) {
e0ce4af9
IC
1200 kfree_skb(skb);
1201 packets_dropped++;
2688fcb7 1202 queue->info->netdev->stats.rx_errors++;
e0ce4af9 1203 continue;
0d160211
JF
1204 }
1205
900e1833
DV
1206 u64_stats_update_begin(&rx_stats->syncp);
1207 rx_stats->packets++;
1208 rx_stats->bytes += skb->len;
1209 u64_stats_update_end(&rx_stats->syncp);
0d160211
JF
1210
1211 /* Pass it up. */
2688fcb7 1212 napi_gro_receive(&queue->napi, skb);
0d160211
JF
1213 }
1214
1215 return packets_dropped;
1216}
1217
bea3348e 1218static int xennet_poll(struct napi_struct *napi, int budget)
0d160211 1219{
2688fcb7
AB
1220 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1221 struct net_device *dev = queue->info->netdev;
0d160211
JF
1222 struct sk_buff *skb;
1223 struct netfront_rx_info rinfo;
1224 struct xen_netif_rx_response *rx = &rinfo.rx;
1225 struct xen_netif_extra_info *extras = rinfo.extras;
1226 RING_IDX i, rp;
bea3348e 1227 int work_done;
0d160211
JF
1228 struct sk_buff_head rxq;
1229 struct sk_buff_head errq;
1230 struct sk_buff_head tmpq;
0d160211 1231 int err;
6c5aa6fc 1232 bool need_xdp_flush = false;
0d160211 1233
2688fcb7 1234 spin_lock(&queue->rx_lock);
0d160211 1235
0d160211
JF
1236 skb_queue_head_init(&rxq);
1237 skb_queue_head_init(&errq);
1238 skb_queue_head_init(&tmpq);
1239
2688fcb7 1240 rp = queue->rx.sring->rsp_prod;
a884daa6
JG
1241 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1242 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1243 rp - queue->rx.rsp_cons);
1244 queue->info->broken = true;
1245 spin_unlock(&queue->rx_lock);
1246 return 0;
1247 }
0d160211
JF
1248 rmb(); /* Ensure we see queued responses up to 'rp'. */
1249
2688fcb7 1250 i = queue->rx.rsp_cons;
0d160211
JF
1251 work_done = 0;
1252 while ((i != rp) && (work_done < budget)) {
8446066b 1253 RING_COPY_RESPONSE(&queue->rx, i, rx);
0d160211
JF
1254 memset(extras, 0, sizeof(rinfo.extras));
1255
6c5aa6fc
DK
1256 err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1257 &need_xdp_flush);
0d160211
JF
1258
1259 if (unlikely(err)) {
60a69176
JG
1260 if (queue->info->broken) {
1261 spin_unlock(&queue->rx_lock);
1262 return 0;
1263 }
0d160211
JF
1264err:
1265 while ((skb = __skb_dequeue(&tmpq)))
1266 __skb_queue_tail(&errq, skb);
09f75cd7 1267 dev->stats.rx_errors++;
2688fcb7 1268 i = queue->rx.rsp_cons;
0d160211
JF
1269 continue;
1270 }
1271
1272 skb = __skb_dequeue(&tmpq);
1273
1274 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1275 struct xen_netif_extra_info *gso;
1276 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1277
1278 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1279 __skb_queue_head(&tmpq, skb);
900e0ac2
JG
1280 xennet_set_rx_rsp_cons(queue,
1281 queue->rx.rsp_cons +
1282 skb_queue_len(&tmpq));
0d160211
JF
1283 goto err;
1284 }
1285 }
1286
3683243b
IC
1287 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1288 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1289 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
0d160211 1290
b54c9d5b 1291 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
3683243b
IC
1292 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1293 skb->data_len = rx->status;
093b9c71 1294 skb->len += rx->status;
0d160211 1295
a761129e 1296 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
ad4f15dc 1297 goto err;
0d160211 1298
f942dc25 1299 if (rx->flags & XEN_NETRXF_csum_blank)
0d160211 1300 skb->ip_summed = CHECKSUM_PARTIAL;
f942dc25 1301 else if (rx->flags & XEN_NETRXF_data_validated)
0d160211
JF
1302 skb->ip_summed = CHECKSUM_UNNECESSARY;
1303
1304 __skb_queue_tail(&rxq, skb);
1305
900e0ac2
JG
1306 i = queue->rx.rsp_cons + 1;
1307 xennet_set_rx_rsp_cons(queue, i);
0d160211
JF
1308 work_done++;
1309 }
6c5aa6fc
DK
1310 if (need_xdp_flush)
1311 xdp_do_flush();
0d160211 1312
56cfe5d0 1313 __skb_queue_purge(&errq);
0d160211 1314
2688fcb7 1315 work_done -= handle_incoming_queue(queue, &rxq);
0d160211 1316
2688fcb7 1317 xennet_alloc_rx_buffers(queue);
0d160211 1318
0d160211 1319 if (work_done < budget) {
bea3348e
SH
1320 int more_to_do = 0;
1321
6ad20165 1322 napi_complete_done(napi, work_done);
0d160211 1323
2688fcb7 1324 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
6a6dc08f
DV
1325 if (more_to_do)
1326 napi_schedule(napi);
0d160211
JF
1327 }
1328
2688fcb7 1329 spin_unlock(&queue->rx_lock);
0d160211 1330
bea3348e 1331 return work_done;
0d160211
JF
1332}
1333
1334static int xennet_change_mtu(struct net_device *dev, int mtu)
1335{
0c36820e 1336 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
0d160211
JF
1337
1338 if (mtu > max)
1339 return -EINVAL;
1340 dev->mtu = mtu;
1341 return 0;
1342}
1343
bc1f4470 1344static void xennet_get_stats64(struct net_device *dev,
1345 struct rtnl_link_stats64 *tot)
e00f85be 1346{
1347 struct netfront_info *np = netdev_priv(dev);
1348 int cpu;
1349
1350 for_each_possible_cpu(cpu) {
900e1833
DV
1351 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1352 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
e00f85be 1353 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1354 unsigned int start;
1355
1356 do {
900e1833
DV
1357 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1358 tx_packets = tx_stats->packets;
1359 tx_bytes = tx_stats->bytes;
1360 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
e00f85be 1361
900e1833
DV
1362 do {
1363 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1364 rx_packets = rx_stats->packets;
1365 rx_bytes = rx_stats->bytes;
1366 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
e00f85be 1367
1368 tot->rx_packets += rx_packets;
1369 tot->tx_packets += tx_packets;
1370 tot->rx_bytes += rx_bytes;
1371 tot->tx_bytes += tx_bytes;
1372 }
1373
1374 tot->rx_errors = dev->stats.rx_errors;
1375 tot->tx_dropped = dev->stats.tx_dropped;
e00f85be 1376}
1377
2688fcb7 1378static void xennet_release_tx_bufs(struct netfront_queue *queue)
0d160211
JF
1379{
1380 struct sk_buff *skb;
1381 int i;
1382
1383 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1384 /* Skip over entries which are actually freelist references */
21631d2d 1385 if (!queue->tx_skbs[i])
0d160211
JF
1386 continue;
1387
21631d2d
JG
1388 skb = queue->tx_skbs[i];
1389 queue->tx_skbs[i] = NULL;
2688fcb7
AB
1390 get_page(queue->grant_tx_page[i]);
1391 gnttab_end_foreign_access(queue->grant_tx_ref[i],
cefe0078 1392 GNTMAP_readonly,
2688fcb7
AB
1393 (unsigned long)page_address(queue->grant_tx_page[i]));
1394 queue->grant_tx_page[i] = NULL;
1395 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
21631d2d 1396 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
0d160211
JF
1397 dev_kfree_skb_irq(skb);
1398 }
1399}
1400
2688fcb7 1401static void xennet_release_rx_bufs(struct netfront_queue *queue)
0d160211 1402{
0d160211
JF
1403 int id, ref;
1404
2688fcb7 1405 spin_lock_bh(&queue->rx_lock);
0d160211
JF
1406
1407 for (id = 0; id < NET_RX_RING_SIZE; id++) {
cefe0078
AL
1408 struct sk_buff *skb;
1409 struct page *page;
0d160211 1410
2688fcb7 1411 skb = queue->rx_skbs[id];
cefe0078 1412 if (!skb)
0d160211 1413 continue;
0d160211 1414
2688fcb7 1415 ref = queue->grant_rx_ref[id];
cefe0078
AL
1416 if (ref == GRANT_INVALID_REF)
1417 continue;
0d160211 1418
cefe0078 1419 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
0d160211 1420
cefe0078
AL
1421 /* gnttab_end_foreign_access() needs a page ref until
1422 * foreign access is ended (which may be deferred).
1423 */
1424 get_page(page);
1425 gnttab_end_foreign_access(ref, 0,
1426 (unsigned long)page_address(page));
2688fcb7 1427 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
0d160211 1428
cefe0078 1429 kfree_skb(skb);
0d160211
JF
1430 }
1431
2688fcb7 1432 spin_unlock_bh(&queue->rx_lock);
0d160211
JF
1433}
1434
c8f44aff
MM
1435static netdev_features_t xennet_fix_features(struct net_device *dev,
1436 netdev_features_t features)
8f7b01a1
ED
1437{
1438 struct netfront_info *np = netdev_priv(dev);
8f7b01a1 1439
2890ea5c
JG
1440 if (features & NETIF_F_SG &&
1441 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1442 features &= ~NETIF_F_SG;
8f7b01a1 1443
2890ea5c
JG
1444 if (features & NETIF_F_IPV6_CSUM &&
1445 !xenbus_read_unsigned(np->xbdev->otherend,
1446 "feature-ipv6-csum-offload", 0))
1447 features &= ~NETIF_F_IPV6_CSUM;
8f7b01a1 1448
2890ea5c
JG
1449 if (features & NETIF_F_TSO &&
1450 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1451 features &= ~NETIF_F_TSO;
8f7b01a1 1452
2890ea5c
JG
1453 if (features & NETIF_F_TSO6 &&
1454 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1455 features &= ~NETIF_F_TSO6;
2c0057de 1456
8f7b01a1
ED
1457 return features;
1458}
1459
c8f44aff
MM
1460static int xennet_set_features(struct net_device *dev,
1461 netdev_features_t features)
8f7b01a1
ED
1462{
1463 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1464 netdev_info(dev, "Reducing MTU because no SG offload");
1465 dev->mtu = ETH_DATA_LEN;
1466 }
1467
1468 return 0;
1469}
1470
900e0ac2 1471static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
cf66f9d4 1472{
cf66f9d4
KRW
1473 unsigned long flags;
1474
900e0ac2
JG
1475 if (unlikely(queue->info->broken))
1476 return false;
a884daa6 1477
2688fcb7 1478 spin_lock_irqsave(&queue->tx_lock, flags);
900e0ac2
JG
1479 if (xennet_tx_buf_gc(queue))
1480 *eoi = 0;
2688fcb7 1481 spin_unlock_irqrestore(&queue->tx_lock, flags);
cf66f9d4 1482
900e0ac2
JG
1483 return true;
1484}
1485
1486static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1487{
1488 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1489
1490 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1491 xen_irq_lateeoi(irq, eoiflag);
1492
d634bf2c
WL
1493 return IRQ_HANDLED;
1494}
1495
900e0ac2 1496static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
d634bf2c 1497{
900e0ac2
JG
1498 unsigned int work_queued;
1499 unsigned long flags;
d634bf2c 1500
900e0ac2
JG
1501 if (unlikely(queue->info->broken))
1502 return false;
1503
1504 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1505 work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1506 if (work_queued > queue->rx_rsp_unconsumed) {
1507 queue->rx_rsp_unconsumed = work_queued;
1508 *eoi = 0;
1509 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1510 const struct device *dev = &queue->info->netdev->dev;
1511
1512 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1513 dev_alert(dev, "RX producer index going backwards\n");
1514 dev_alert(dev, "Disabled for further use\n");
1515 queue->info->broken = true;
1516 return false;
1517 }
1518 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
a884daa6 1519
900e0ac2 1520 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
76541869 1521 napi_schedule(&queue->napi);
cf66f9d4 1522
900e0ac2
JG
1523 return true;
1524}
1525
1526static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1527{
1528 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1529
1530 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1531 xen_irq_lateeoi(irq, eoiflag);
1532
d634bf2c
WL
1533 return IRQ_HANDLED;
1534}
cf66f9d4 1535
d634bf2c
WL
1536static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1537{
900e0ac2
JG
1538 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1539
1540 if (xennet_handle_tx(dev_id, &eoiflag) &&
1541 xennet_handle_rx(dev_id, &eoiflag))
1542 xen_irq_lateeoi(irq, eoiflag);
1543
cf66f9d4
KRW
1544 return IRQ_HANDLED;
1545}
1546
1547#ifdef CONFIG_NET_POLL_CONTROLLER
1548static void xennet_poll_controller(struct net_device *dev)
1549{
2688fcb7
AB
1550 /* Poll each queue */
1551 struct netfront_info *info = netdev_priv(dev);
1552 unsigned int num_queues = dev->real_num_tx_queues;
1553 unsigned int i;
a884daa6
JG
1554
1555 if (info->broken)
1556 return;
1557
2688fcb7
AB
1558 for (i = 0; i < num_queues; ++i)
1559 xennet_interrupt(0, &info->queues[i]);
cf66f9d4
KRW
1560}
1561#endif
1562
6c5aa6fc
DK
1563#define NETBACK_XDP_HEADROOM_DISABLE 0
1564#define NETBACK_XDP_HEADROOM_ENABLE 1
1565
1566static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1567{
1568 int err;
1569 unsigned short headroom;
1570
1571 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1572 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1573 "xdp-headroom", "%hu",
1574 headroom);
1575 if (err)
1576 pr_warn("Error writing xdp-headroom\n");
1577
1578 return err;
1579}
1580
1581static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1582 struct netlink_ext_ack *extack)
1583{
1584 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1585 struct netfront_info *np = netdev_priv(dev);
1586 struct bpf_prog *old_prog;
1587 unsigned int i, err;
1588
1589 if (dev->mtu > max_mtu) {
1590 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1591 return -EINVAL;
1592 }
1593
1594 if (!np->netback_has_xdp_headroom)
1595 return 0;
1596
1597 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1598
1599 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1600 NETBACK_XDP_HEADROOM_DISABLE);
1601 if (err)
1602 return err;
1603
1604 /* avoid the race with XDP headroom adjustment */
1605 wait_event(module_wq,
1606 xenbus_read_driver_state(np->xbdev->otherend) ==
1607 XenbusStateReconfigured);
1608 np->netfront_xdp_enabled = true;
1609
1610 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1611
1612 if (prog)
1613 bpf_prog_add(prog, dev->real_num_tx_queues);
1614
1615 for (i = 0; i < dev->real_num_tx_queues; ++i)
1616 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1617
1618 if (old_prog)
1619 for (i = 0; i < dev->real_num_tx_queues; ++i)
1620 bpf_prog_put(old_prog);
1621
1622 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1623
1624 return 0;
1625}
1626
6c5aa6fc
DK
1627static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1628{
a884daa6
JG
1629 struct netfront_info *np = netdev_priv(dev);
1630
1631 if (np->broken)
1632 return -ENODEV;
1633
6c5aa6fc
DK
1634 switch (xdp->command) {
1635 case XDP_SETUP_PROG:
1636 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
6c5aa6fc
DK
1637 default:
1638 return -EINVAL;
1639 }
1640}
1641
0a0b9d2e 1642static const struct net_device_ops xennet_netdev_ops = {
1d0ea124 1643 .ndo_uninit = xennet_uninit,
0a0b9d2e 1644 .ndo_open = xennet_open,
0a0b9d2e
SH
1645 .ndo_stop = xennet_close,
1646 .ndo_start_xmit = xennet_start_xmit,
1647 .ndo_change_mtu = xennet_change_mtu,
e00f85be 1648 .ndo_get_stats64 = xennet_get_stats64,
0a0b9d2e
SH
1649 .ndo_set_mac_address = eth_mac_addr,
1650 .ndo_validate_addr = eth_validate_addr,
fb507934
MM
1651 .ndo_fix_features = xennet_fix_features,
1652 .ndo_set_features = xennet_set_features,
2688fcb7 1653 .ndo_select_queue = xennet_select_queue,
6c5aa6fc
DK
1654 .ndo_bpf = xennet_xdp,
1655 .ndo_xdp_xmit = xennet_xdp_xmit,
cf66f9d4
KRW
1656#ifdef CONFIG_NET_POLL_CONTROLLER
1657 .ndo_poll_controller = xennet_poll_controller,
1658#endif
0a0b9d2e
SH
1659};
1660
900e1833
DV
1661static void xennet_free_netdev(struct net_device *netdev)
1662{
1663 struct netfront_info *np = netdev_priv(netdev);
1664
1665 free_percpu(np->rx_stats);
1666 free_percpu(np->tx_stats);
1667 free_netdev(netdev);
1668}
1669
8e0e46bb 1670static struct net_device *xennet_create_dev(struct xenbus_device *dev)
0d160211 1671{
2688fcb7 1672 int err;
0d160211
JF
1673 struct net_device *netdev;
1674 struct netfront_info *np;
1675
50ee6061 1676 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
41de8d4c 1677 if (!netdev)
0d160211 1678 return ERR_PTR(-ENOMEM);
0d160211
JF
1679
1680 np = netdev_priv(netdev);
1681 np->xbdev = dev;
1682
2688fcb7 1683 np->queues = NULL;
0d160211 1684
e00f85be 1685 err = -ENOMEM;
900e1833
DV
1686 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1687 if (np->rx_stats == NULL)
1688 goto exit;
1689 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1690 if (np->tx_stats == NULL)
e00f85be 1691 goto exit;
1692
0a0b9d2e
SH
1693 netdev->netdev_ops = &xennet_netdev_ops;
1694
fb507934
MM
1695 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1696 NETIF_F_GSO_ROBUST;
2c0057de
PD
1697 netdev->hw_features = NETIF_F_SG |
1698 NETIF_F_IPV6_CSUM |
1699 NETIF_F_TSO | NETIF_F_TSO6;
0d160211 1700
fc3e5941
IC
1701 /*
1702 * Assume that all hw features are available for now. This set
1703 * will be adjusted by the call to netdev_update_features() in
1704 * xennet_connect() which is the earliest point where we can
1705 * negotiate with the backend regarding supported features.
1706 */
1707 netdev->features |= netdev->hw_features;
1708
7ad24ea4 1709 netdev->ethtool_ops = &xennet_ethtool_ops;
e1043a4b 1710 netdev->min_mtu = ETH_MIN_MTU;
d0c2c997 1711 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
0d160211
JF
1712 SET_NETDEV_DEV(netdev, &dev->dev);
1713
1714 np->netdev = netdev;
6c5aa6fc 1715 np->netfront_xdp_enabled = false;
0d160211
JF
1716
1717 netif_carrier_off(netdev);
1718
c2c63310
AR
1719 do {
1720 xenbus_switch_state(dev, XenbusStateInitialising);
1721 err = wait_event_timeout(module_wq,
1722 xenbus_read_driver_state(dev->otherend) !=
1723 XenbusStateClosed &&
1724 xenbus_read_driver_state(dev->otherend) !=
1725 XenbusStateUnknown, XENNET_TIMEOUT);
1726 } while (!err);
1727
0d160211
JF
1728 return netdev;
1729
0d160211 1730 exit:
900e1833 1731 xennet_free_netdev(netdev);
0d160211
JF
1732 return ERR_PTR(err);
1733}
1734
80708602 1735/*
0d160211
JF
1736 * Entry point to this code when a new device is created. Allocate the basic
1737 * structures and the ring buffers for communication with the backend, and
1738 * inform the backend of the appropriate details for those.
1739 */
8e0e46bb 1740static int netfront_probe(struct xenbus_device *dev,
1dd06ae8 1741 const struct xenbus_device_id *id)
0d160211
JF
1742{
1743 int err;
1744 struct net_device *netdev;
1745 struct netfront_info *info;
1746
1747 netdev = xennet_create_dev(dev);
1748 if (IS_ERR(netdev)) {
1749 err = PTR_ERR(netdev);
1750 xenbus_dev_fatal(dev, err, "creating netdev");
1751 return err;
1752 }
1753
1754 info = netdev_priv(netdev);
1b713e00 1755 dev_set_drvdata(&dev->dev, info);
27b917e5
TI
1756#ifdef CONFIG_SYSFS
1757 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1758#endif
0d160211 1759
0d160211 1760 return 0;
0d160211
JF
1761}
1762
1763static void xennet_end_access(int ref, void *page)
1764{
1765 /* This frees the page as a side-effect */
1766 if (ref != GRANT_INVALID_REF)
1767 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1768}
1769
1770static void xennet_disconnect_backend(struct netfront_info *info)
1771{
2688fcb7 1772 unsigned int i = 0;
2688fcb7
AB
1773 unsigned int num_queues = info->netdev->real_num_tx_queues;
1774
f9feb1e6
DV
1775 netif_carrier_off(info->netdev);
1776
9a873c71 1777 for (i = 0; i < num_queues && info->queues; ++i) {
76541869
DV
1778 struct netfront_queue *queue = &info->queues[i];
1779
74470954
BO
1780 del_timer_sync(&queue->rx_refill_timer);
1781
2688fcb7
AB
1782 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1783 unbind_from_irqhandler(queue->tx_irq, queue);
1784 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1785 unbind_from_irqhandler(queue->tx_irq, queue);
1786 unbind_from_irqhandler(queue->rx_irq, queue);
1787 }
1788 queue->tx_evtchn = queue->rx_evtchn = 0;
1789 queue->tx_irq = queue->rx_irq = 0;
0d160211 1790
274b0455
CW
1791 if (netif_running(info->netdev))
1792 napi_synchronize(&queue->napi);
f9feb1e6 1793
a5b5dc3c
DV
1794 xennet_release_tx_bufs(queue);
1795 xennet_release_rx_bufs(queue);
1796 gnttab_free_grant_references(queue->gref_tx_head);
1797 gnttab_free_grant_references(queue->gref_rx_head);
1798
2688fcb7
AB
1799 /* End access and free the pages */
1800 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1801 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
0d160211 1802
2688fcb7
AB
1803 queue->tx_ring_ref = GRANT_INVALID_REF;
1804 queue->rx_ring_ref = GRANT_INVALID_REF;
1805 queue->tx.sring = NULL;
1806 queue->rx.sring = NULL;
6c5aa6fc
DK
1807
1808 page_pool_destroy(queue->page_pool);
2688fcb7 1809 }
0d160211
JF
1810}
1811
80708602 1812/*
0d160211
JF
1813 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1814 * driver restart. We tear down our netif structure and recreate it, but
1815 * leave the device-layer structures intact so that this is transparent to the
1816 * rest of the kernel.
1817 */
1818static int netfront_resume(struct xenbus_device *dev)
1819{
1b713e00 1820 struct netfront_info *info = dev_get_drvdata(&dev->dev);
0d160211
JF
1821
1822 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1823
042b2046
DZ
1824 netif_tx_lock_bh(info->netdev);
1825 netif_device_detach(info->netdev);
1826 netif_tx_unlock_bh(info->netdev);
1827
0d160211
JF
1828 xennet_disconnect_backend(info);
1829 return 0;
1830}
1831
1832static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1833{
1834 char *s, *e, *macstr;
1835 int i;
1836
1837 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1838 if (IS_ERR(macstr))
1839 return PTR_ERR(macstr);
1840
1841 for (i = 0; i < ETH_ALEN; i++) {
1842 mac[i] = simple_strtoul(s, &e, 16);
1843 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1844 kfree(macstr);
1845 return -ENOENT;
1846 }
1847 s = e+1;
1848 }
1849
1850 kfree(macstr);
1851 return 0;
1852}
1853
2688fcb7 1854static int setup_netfront_single(struct netfront_queue *queue)
d634bf2c
WL
1855{
1856 int err;
1857
2688fcb7 1858 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
d634bf2c
WL
1859 if (err < 0)
1860 goto fail;
1861
900e0ac2
JG
1862 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1863 xennet_interrupt, 0,
1864 queue->info->netdev->name,
1865 queue);
d634bf2c
WL
1866 if (err < 0)
1867 goto bind_fail;
2688fcb7
AB
1868 queue->rx_evtchn = queue->tx_evtchn;
1869 queue->rx_irq = queue->tx_irq = err;
d634bf2c
WL
1870
1871 return 0;
1872
1873bind_fail:
2688fcb7
AB
1874 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1875 queue->tx_evtchn = 0;
d634bf2c
WL
1876fail:
1877 return err;
1878}
1879
2688fcb7 1880static int setup_netfront_split(struct netfront_queue *queue)
d634bf2c
WL
1881{
1882 int err;
1883
2688fcb7 1884 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
d634bf2c
WL
1885 if (err < 0)
1886 goto fail;
2688fcb7 1887 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
d634bf2c
WL
1888 if (err < 0)
1889 goto alloc_rx_evtchn_fail;
1890
2688fcb7
AB
1891 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1892 "%s-tx", queue->name);
900e0ac2
JG
1893 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1894 xennet_tx_interrupt, 0,
1895 queue->tx_irq_name, queue);
d634bf2c
WL
1896 if (err < 0)
1897 goto bind_tx_fail;
2688fcb7 1898 queue->tx_irq = err;
d634bf2c 1899
2688fcb7
AB
1900 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1901 "%s-rx", queue->name);
900e0ac2
JG
1902 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1903 xennet_rx_interrupt, 0,
1904 queue->rx_irq_name, queue);
d634bf2c
WL
1905 if (err < 0)
1906 goto bind_rx_fail;
2688fcb7 1907 queue->rx_irq = err;
d634bf2c
WL
1908
1909 return 0;
1910
1911bind_rx_fail:
2688fcb7
AB
1912 unbind_from_irqhandler(queue->tx_irq, queue);
1913 queue->tx_irq = 0;
d634bf2c 1914bind_tx_fail:
2688fcb7
AB
1915 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1916 queue->rx_evtchn = 0;
d634bf2c 1917alloc_rx_evtchn_fail:
2688fcb7
AB
1918 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1919 queue->tx_evtchn = 0;
d634bf2c
WL
1920fail:
1921 return err;
1922}
1923
2688fcb7
AB
1924static int setup_netfront(struct xenbus_device *dev,
1925 struct netfront_queue *queue, unsigned int feature_split_evtchn)
0d160211
JF
1926{
1927 struct xen_netif_tx_sring *txs;
60a69176 1928 struct xen_netif_rx_sring *rxs = NULL;
ccc9d90a 1929 grant_ref_t gref;
0d160211 1930 int err;
0d160211 1931
2688fcb7
AB
1932 queue->tx_ring_ref = GRANT_INVALID_REF;
1933 queue->rx_ring_ref = GRANT_INVALID_REF;
1934 queue->rx.sring = NULL;
1935 queue->tx.sring = NULL;
0d160211 1936
a144ff09 1937 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
0d160211
JF
1938 if (!txs) {
1939 err = -ENOMEM;
1940 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1941 goto fail;
1942 }
1943 SHARED_RING_INIT(txs);
30c5d7f0 1944 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
0d160211 1945
ccc9d90a 1946 err = xenbus_grant_ring(dev, txs, 1, &gref);
1ca2983a 1947 if (err < 0)
60a69176 1948 goto fail;
ccc9d90a 1949 queue->tx_ring_ref = gref;
0d160211 1950
a144ff09 1951 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
0d160211
JF
1952 if (!rxs) {
1953 err = -ENOMEM;
1954 xenbus_dev_fatal(dev, err, "allocating rx ring page");
60a69176 1955 goto fail;
0d160211
JF
1956 }
1957 SHARED_RING_INIT(rxs);
30c5d7f0 1958 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
0d160211 1959
ccc9d90a 1960 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1ca2983a 1961 if (err < 0)
60a69176 1962 goto fail;
ccc9d90a 1963 queue->rx_ring_ref = gref;
0d160211 1964
d634bf2c 1965 if (feature_split_evtchn)
2688fcb7 1966 err = setup_netfront_split(queue);
d634bf2c
WL
1967 /* setup single event channel if
1968 * a) feature-split-event-channels == 0
1969 * b) feature-split-event-channels == 1 but failed to setup
1970 */
e93fac3b 1971 if (!feature_split_evtchn || err)
2688fcb7 1972 err = setup_netfront_single(queue);
d634bf2c 1973
0d160211 1974 if (err)
60a69176 1975 goto fail;
0d160211 1976
0d160211
JF
1977 return 0;
1978
1ca2983a
WL
1979 /* If we fail to setup netfront, it is safe to just revoke access to
1980 * granted pages because backend is not accessing it at this point.
1981 */
60a69176
JG
1982 fail:
1983 if (queue->rx_ring_ref != GRANT_INVALID_REF) {
1984 gnttab_end_foreign_access(queue->rx_ring_ref, 0,
1985 (unsigned long)rxs);
1986 queue->rx_ring_ref = GRANT_INVALID_REF;
1987 } else {
1988 free_page((unsigned long)rxs);
1989 }
1990 if (queue->tx_ring_ref != GRANT_INVALID_REF) {
1991 gnttab_end_foreign_access(queue->tx_ring_ref, 0,
1992 (unsigned long)txs);
1993 queue->tx_ring_ref = GRANT_INVALID_REF;
1994 } else {
1995 free_page((unsigned long)txs);
1996 }
0d160211
JF
1997 return err;
1998}
1999
2688fcb7
AB
2000/* Queue-specific initialisation
2001 * This used to be done in xennet_create_dev() but must now
2002 * be run per-queue.
2003 */
2004static int xennet_init_queue(struct netfront_queue *queue)
2005{
2006 unsigned short i;
2007 int err = 0;
21f2706b 2008 char *devid;
2688fcb7
AB
2009
2010 spin_lock_init(&queue->tx_lock);
2011 spin_lock_init(&queue->rx_lock);
900e0ac2 2012 spin_lock_init(&queue->rx_cons_lock);
2688fcb7 2013
e99e88a9 2014 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
2688fcb7 2015
21f2706b
XL
2016 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
2017 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
2018 devid, queue->id);
8b715010 2019
21631d2d 2020 /* Initialise tx_skb_freelist as a free chain containing every entry. */
2688fcb7 2021 queue->tx_skb_freelist = 0;
a884daa6 2022 queue->tx_pend_queue = TX_LINK_NONE;
2688fcb7 2023 for (i = 0; i < NET_TX_RING_SIZE; i++) {
21631d2d 2024 queue->tx_link[i] = i + 1;
2688fcb7
AB
2025 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
2026 queue->grant_tx_page[i] = NULL;
2027 }
21631d2d 2028 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
2688fcb7
AB
2029
2030 /* Clear out rx_skbs */
2031 for (i = 0; i < NET_RX_RING_SIZE; i++) {
2032 queue->rx_skbs[i] = NULL;
2033 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
2034 }
2035
2036 /* A grant for every tx ring slot */
1f3c2eba 2037 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2688fcb7
AB
2038 &queue->gref_tx_head) < 0) {
2039 pr_alert("can't alloc tx grant refs\n");
2040 err = -ENOMEM;
2041 goto exit;
2042 }
2043
2044 /* A grant for every rx ring slot */
1f3c2eba 2045 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
2688fcb7
AB
2046 &queue->gref_rx_head) < 0) {
2047 pr_alert("can't alloc rx grant refs\n");
2048 err = -ENOMEM;
2049 goto exit_free_tx;
2050 }
2051
2688fcb7
AB
2052 return 0;
2053
2054 exit_free_tx:
2055 gnttab_free_grant_references(queue->gref_tx_head);
2056 exit:
2057 return err;
2058}
2059
50ee6061
AB
2060static int write_queue_xenstore_keys(struct netfront_queue *queue,
2061 struct xenbus_transaction *xbt, int write_hierarchical)
2062{
2063 /* Write the queue-specific keys into XenStore in the traditional
2064 * way for a single queue, or in a queue subkeys for multiple
2065 * queues.
2066 */
2067 struct xenbus_device *dev = queue->info->xbdev;
2068 int err;
2069 const char *message;
2070 char *path;
2071 size_t pathsize;
2072
2073 /* Choose the correct place to write the keys */
2074 if (write_hierarchical) {
2075 pathsize = strlen(dev->nodename) + 10;
2076 path = kzalloc(pathsize, GFP_KERNEL);
2077 if (!path) {
2078 err = -ENOMEM;
2079 message = "out of memory while writing ring references";
2080 goto error;
2081 }
2082 snprintf(path, pathsize, "%s/queue-%u",
2083 dev->nodename, queue->id);
2084 } else {
2085 path = (char *)dev->nodename;
2086 }
2087
2088 /* Write ring references */
2089 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
2090 queue->tx_ring_ref);
2091 if (err) {
2092 message = "writing tx-ring-ref";
2093 goto error;
2094 }
2095
2096 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
2097 queue->rx_ring_ref);
2098 if (err) {
2099 message = "writing rx-ring-ref";
2100 goto error;
2101 }
2102
2103 /* Write event channels; taking into account both shared
2104 * and split event channel scenarios.
2105 */
2106 if (queue->tx_evtchn == queue->rx_evtchn) {
2107 /* Shared event channel */
2108 err = xenbus_printf(*xbt, path,
2109 "event-channel", "%u", queue->tx_evtchn);
2110 if (err) {
2111 message = "writing event-channel";
2112 goto error;
2113 }
2114 } else {
2115 /* Split event channels */
2116 err = xenbus_printf(*xbt, path,
2117 "event-channel-tx", "%u", queue->tx_evtchn);
2118 if (err) {
2119 message = "writing event-channel-tx";
2120 goto error;
2121 }
2122
2123 err = xenbus_printf(*xbt, path,
2124 "event-channel-rx", "%u", queue->rx_evtchn);
2125 if (err) {
2126 message = "writing event-channel-rx";
2127 goto error;
2128 }
2129 }
2130
2131 if (write_hierarchical)
2132 kfree(path);
2133 return 0;
2134
2135error:
2136 if (write_hierarchical)
2137 kfree(path);
2138 xenbus_dev_fatal(dev, err, "%s", message);
2139 return err;
2140}
2141
6c5aa6fc
DK
2142
2143
2144static int xennet_create_page_pool(struct netfront_queue *queue)
2145{
2146 int err;
2147 struct page_pool_params pp_params = {
2148 .order = 0,
2149 .flags = 0,
2150 .pool_size = NET_RX_RING_SIZE,
2151 .nid = NUMA_NO_NODE,
2152 .dev = &queue->info->netdev->dev,
2153 .offset = XDP_PACKET_HEADROOM,
2154 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2155 };
2156
2157 queue->page_pool = page_pool_create(&pp_params);
2158 if (IS_ERR(queue->page_pool)) {
2159 err = PTR_ERR(queue->page_pool);
2160 queue->page_pool = NULL;
2161 return err;
2162 }
2163
2164 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
b02e5a0e 2165 queue->id, 0);
6c5aa6fc
DK
2166 if (err) {
2167 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2168 goto err_free_pp;
2169 }
2170
2171 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2172 MEM_TYPE_PAGE_POOL, queue->page_pool);
2173 if (err) {
2174 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2175 goto err_unregister_rxq;
2176 }
2177 return 0;
2178
2179err_unregister_rxq:
2180 xdp_rxq_info_unreg(&queue->xdp_rxq);
2181err_free_pp:
2182 page_pool_destroy(queue->page_pool);
2183 queue->page_pool = NULL;
2184 return err;
2185}
2186
ce58725f 2187static int xennet_create_queues(struct netfront_info *info,
ca88ea12 2188 unsigned int *num_queues)
ce58725f
DV
2189{
2190 unsigned int i;
2191 int ret;
2192
ca88ea12 2193 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
ce58725f
DV
2194 GFP_KERNEL);
2195 if (!info->queues)
2196 return -ENOMEM;
2197
ca88ea12 2198 for (i = 0; i < *num_queues; i++) {
ce58725f
DV
2199 struct netfront_queue *queue = &info->queues[i];
2200
2201 queue->id = i;
2202 queue->info = info;
2203
2204 ret = xennet_init_queue(queue);
2205 if (ret < 0) {
f599c64f 2206 dev_warn(&info->xbdev->dev,
69cb8524 2207 "only created %d queues\n", i);
ca88ea12 2208 *num_queues = i;
ce58725f
DV
2209 break;
2210 }
2211
6c5aa6fc
DK
2212 /* use page pool recycling instead of buddy allocator */
2213 ret = xennet_create_page_pool(queue);
2214 if (ret < 0) {
2215 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2216 *num_queues = i;
2217 return ret;
2218 }
2219
ce58725f
DV
2220 netif_napi_add(queue->info->netdev, &queue->napi,
2221 xennet_poll, 64);
2222 if (netif_running(info->netdev))
2223 napi_enable(&queue->napi);
2224 }
2225
ca88ea12 2226 netif_set_real_num_tx_queues(info->netdev, *num_queues);
ce58725f 2227
ca88ea12 2228 if (*num_queues == 0) {
f599c64f 2229 dev_err(&info->xbdev->dev, "no queues\n");
ce58725f
DV
2230 return -EINVAL;
2231 }
2232 return 0;
2233}
2234
0d160211 2235/* Common code used when first setting up, and when resuming. */
f502bf2b 2236static int talk_to_netback(struct xenbus_device *dev,
0d160211
JF
2237 struct netfront_info *info)
2238{
2239 const char *message;
2240 struct xenbus_transaction xbt;
2241 int err;
2688fcb7
AB
2242 unsigned int feature_split_evtchn;
2243 unsigned int i = 0;
50ee6061 2244 unsigned int max_queues = 0;
2688fcb7
AB
2245 struct netfront_queue *queue = NULL;
2246 unsigned int num_queues = 1;
0d160211 2247
2688fcb7
AB
2248 info->netdev->irq = 0;
2249
50ee6061 2250 /* Check if backend supports multiple queues */
2890ea5c
JG
2251 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2252 "multi-queue-max-queues", 1);
50ee6061
AB
2253 num_queues = min(max_queues, xennet_max_queues);
2254
2688fcb7 2255 /* Check feature-split-event-channels */
2890ea5c
JG
2256 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2257 "feature-split-event-channels", 0);
2688fcb7
AB
2258
2259 /* Read mac addr. */
2260 err = xen_net_read_mac(dev, info->netdev->dev_addr);
2261 if (err) {
2262 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
cb257783 2263 goto out_unlocked;
2688fcb7
AB
2264 }
2265
6c5aa6fc
DK
2266 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2267 "feature-xdp-headroom", 0);
2268 if (info->netback_has_xdp_headroom) {
2269 /* set the current xen-netfront xdp state */
2270 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2271 NETBACK_XDP_HEADROOM_ENABLE :
2272 NETBACK_XDP_HEADROOM_DISABLE);
2273 if (err)
2274 goto out_unlocked;
2275 }
2276
f599c64f 2277 rtnl_lock();
ce58725f
DV
2278 if (info->queues)
2279 xennet_destroy_queues(info);
2280
a884daa6
JG
2281 /* For the case of a reconnect reset the "broken" indicator. */
2282 info->broken = false;
2283
ca88ea12 2284 err = xennet_create_queues(info, &num_queues);
e2e004ac
RL
2285 if (err < 0) {
2286 xenbus_dev_fatal(dev, err, "creating queues");
2287 kfree(info->queues);
2288 info->queues = NULL;
2289 goto out;
2290 }
f599c64f 2291 rtnl_unlock();
2688fcb7
AB
2292
2293 /* Create shared ring, alloc event channel -- for each queue */
2294 for (i = 0; i < num_queues; ++i) {
2295 queue = &info->queues[i];
2688fcb7 2296 err = setup_netfront(dev, queue, feature_split_evtchn);
e2e004ac
RL
2297 if (err)
2298 goto destroy_ring;
2688fcb7 2299 }
0d160211
JF
2300
2301again:
2302 err = xenbus_transaction_start(&xbt);
2303 if (err) {
2304 xenbus_dev_fatal(dev, err, "starting transaction");
2305 goto destroy_ring;
2306 }
2307
812494d9 2308 if (xenbus_exists(XBT_NIL,
2309 info->xbdev->otherend, "multi-queue-max-queues")) {
50ee6061 2310 /* Write the number of queues */
812494d9 2311 err = xenbus_printf(xbt, dev->nodename,
2312 "multi-queue-num-queues", "%u", num_queues);
d634bf2c 2313 if (err) {
50ee6061
AB
2314 message = "writing multi-queue-num-queues";
2315 goto abort_transaction_no_dev_fatal;
d634bf2c 2316 }
812494d9 2317 }
50ee6061 2318
812494d9 2319 if (num_queues == 1) {
2320 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2321 if (err)
2322 goto abort_transaction_no_dev_fatal;
2323 } else {
50ee6061
AB
2324 /* Write the keys for each queue */
2325 for (i = 0; i < num_queues; ++i) {
2326 queue = &info->queues[i];
2327 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2328 if (err)
2329 goto abort_transaction_no_dev_fatal;
d634bf2c 2330 }
0d160211
JF
2331 }
2332
50ee6061 2333 /* The remaining keys are not queue-specific */
0d160211
JF
2334 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2335 1);
2336 if (err) {
2337 message = "writing request-rx-copy";
2338 goto abort_transaction;
2339 }
2340
2341 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2342 if (err) {
2343 message = "writing feature-rx-notify";
2344 goto abort_transaction;
2345 }
2346
2347 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2348 if (err) {
2349 message = "writing feature-sg";
2350 goto abort_transaction;
2351 }
2352
2353 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2354 if (err) {
2355 message = "writing feature-gso-tcpv4";
2356 goto abort_transaction;
2357 }
2358
2c0057de
PD
2359 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2360 if (err) {
2361 message = "writing feature-gso-tcpv6";
2362 goto abort_transaction;
2363 }
2364
2365 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2366 "1");
2367 if (err) {
2368 message = "writing feature-ipv6-csum-offload";
2369 goto abort_transaction;
2370 }
2371
0d160211
JF
2372 err = xenbus_transaction_end(xbt, 0);
2373 if (err) {
2374 if (err == -EAGAIN)
2375 goto again;
2376 xenbus_dev_fatal(dev, err, "completing transaction");
2377 goto destroy_ring;
2378 }
2379
2380 return 0;
2381
2382 abort_transaction:
0d160211 2383 xenbus_dev_fatal(dev, err, "%s", message);
50ee6061
AB
2384abort_transaction_no_dev_fatal:
2385 xenbus_transaction_end(xbt, 1);
0d160211
JF
2386 destroy_ring:
2387 xennet_disconnect_backend(info);
f599c64f 2388 rtnl_lock();
e2e004ac 2389 xennet_destroy_queues(info);
0d160211 2390 out:
f599c64f 2391 rtnl_unlock();
cb257783 2392out_unlocked:
d86b5672 2393 device_unregister(&dev->dev);
0d160211
JF
2394 return err;
2395}
2396
0d160211
JF
2397static int xennet_connect(struct net_device *dev)
2398{
2399 struct netfront_info *np = netdev_priv(dev);
2688fcb7 2400 unsigned int num_queues = 0;
a5b5dc3c 2401 int err;
2688fcb7
AB
2402 unsigned int j = 0;
2403 struct netfront_queue *queue = NULL;
0d160211 2404
2890ea5c 2405 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
0d160211 2406 dev_info(&dev->dev,
898eb71c 2407 "backend does not support copying receive path\n");
0d160211
JF
2408 return -ENODEV;
2409 }
2410
f502bf2b 2411 err = talk_to_netback(np->xbdev, np);
0d160211
JF
2412 if (err)
2413 return err;
6c5aa6fc
DK
2414 if (np->netback_has_xdp_headroom)
2415 pr_info("backend supports XDP headroom\n");
0d160211 2416
2688fcb7
AB
2417 /* talk_to_netback() sets the correct number of queues */
2418 num_queues = dev->real_num_tx_queues;
2419
f599c64f
RL
2420 if (dev->reg_state == NETREG_UNINITIALIZED) {
2421 err = register_netdev(dev);
2422 if (err) {
2423 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2424 device_unregister(&np->xbdev->dev);
2425 return err;
2426 }
2427 }
2428
45c8184c
RL
2429 rtnl_lock();
2430 netdev_update_features(dev);
2431 rtnl_unlock();
2432
0d160211 2433 /*
a5b5dc3c 2434 * All public and private state should now be sane. Get
0d160211
JF
2435 * ready to start sending and receiving packets and give the driver
2436 * domain a kick because we've probably just requeued some
2437 * packets.
2438 */
042b2046
DZ
2439 netif_tx_lock_bh(np->netdev);
2440 netif_device_attach(np->netdev);
2441 netif_tx_unlock_bh(np->netdev);
2442
0d160211 2443 netif_carrier_on(np->netdev);
2688fcb7
AB
2444 for (j = 0; j < num_queues; ++j) {
2445 queue = &np->queues[j];
f50b4076 2446
2688fcb7
AB
2447 notify_remote_via_irq(queue->tx_irq);
2448 if (queue->tx_irq != queue->rx_irq)
2449 notify_remote_via_irq(queue->rx_irq);
2688fcb7 2450
f50b4076
DV
2451 spin_lock_irq(&queue->tx_lock);
2452 xennet_tx_buf_gc(queue);
2688fcb7 2453 spin_unlock_irq(&queue->tx_lock);
f50b4076
DV
2454
2455 spin_lock_bh(&queue->rx_lock);
2456 xennet_alloc_rx_buffers(queue);
2688fcb7
AB
2457 spin_unlock_bh(&queue->rx_lock);
2458 }
0d160211
JF
2459
2460 return 0;
2461}
2462
80708602 2463/*
0d160211
JF
2464 * Callback received when the backend's state changes.
2465 */
f502bf2b 2466static void netback_changed(struct xenbus_device *dev,
0d160211
JF
2467 enum xenbus_state backend_state)
2468{
1b713e00 2469 struct netfront_info *np = dev_get_drvdata(&dev->dev);
0d160211
JF
2470 struct net_device *netdev = np->netdev;
2471
2472 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2473
8edfe2e9
JG
2474 wake_up_all(&module_wq);
2475
0d160211
JF
2476 switch (backend_state) {
2477 case XenbusStateInitialising:
2478 case XenbusStateInitialised:
b78c9512
NI
2479 case XenbusStateReconfiguring:
2480 case XenbusStateReconfigured:
0d160211 2481 case XenbusStateUnknown:
0d160211
JF
2482 break;
2483
2484 case XenbusStateInitWait:
2485 if (dev->state != XenbusStateInitialising)
2486 break;
2487 if (xennet_connect(netdev) != 0)
2488 break;
2489 xenbus_switch_state(dev, XenbusStateConnected);
08e34eb1
LE
2490 break;
2491
2492 case XenbusStateConnected:
ee89bab1 2493 netdev_notify_peers(netdev);
0d160211
JF
2494 break;
2495
bce3ea81
DV
2496 case XenbusStateClosed:
2497 if (dev->state == XenbusStateClosed)
2498 break;
df561f66 2499 fallthrough; /* Missed the backend's CLOSING state */
0d160211
JF
2500 case XenbusStateClosing:
2501 xenbus_frontend_closed(dev);
2502 break;
2503 }
2504}
2505
e0ce4af9
IC
2506static const struct xennet_stat {
2507 char name[ETH_GSTRING_LEN];
2508 u16 offset;
2509} xennet_stats[] = {
2510 {
2511 "rx_gso_checksum_fixup",
2512 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2513 },
2514};
2515
2516static int xennet_get_sset_count(struct net_device *dev, int string_set)
2517{
2518 switch (string_set) {
2519 case ETH_SS_STATS:
2520 return ARRAY_SIZE(xennet_stats);
2521 default:
2522 return -EINVAL;
2523 }
2524}
2525
2526static void xennet_get_ethtool_stats(struct net_device *dev,
2527 struct ethtool_stats *stats, u64 * data)
2528{
2529 void *np = netdev_priv(dev);
2530 int i;
2531
2532 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2688fcb7 2533 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
e0ce4af9
IC
2534}
2535
2536static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2537{
2538 int i;
2539
2540 switch (stringset) {
2541 case ETH_SS_STATS:
2542 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2543 memcpy(data + i * ETH_GSTRING_LEN,
2544 xennet_stats[i].name, ETH_GSTRING_LEN);
2545 break;
2546 }
2547}
2548
0fc0b732 2549static const struct ethtool_ops xennet_ethtool_ops =
0d160211 2550{
0d160211 2551 .get_link = ethtool_op_get_link,
e0ce4af9
IC
2552
2553 .get_sset_count = xennet_get_sset_count,
2554 .get_ethtool_stats = xennet_get_ethtool_stats,
2555 .get_strings = xennet_get_strings,
91ffb9d3 2556 .get_ts_info = ethtool_op_get_ts_info,
0d160211
JF
2557};
2558
2559#ifdef CONFIG_SYSFS
1f3c2eba
DV
2560static ssize_t show_rxbuf(struct device *dev,
2561 struct device_attribute *attr, char *buf)
0d160211 2562{
1f3c2eba 2563 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
0d160211
JF
2564}
2565
1f3c2eba
DV
2566static ssize_t store_rxbuf(struct device *dev,
2567 struct device_attribute *attr,
2568 const char *buf, size_t len)
0d160211 2569{
0d160211 2570 char *endp;
0d160211
JF
2571
2572 if (!capable(CAP_NET_ADMIN))
2573 return -EPERM;
2574
8ed7ec13 2575 simple_strtoul(buf, &endp, 0);
0d160211
JF
2576 if (endp == buf)
2577 return -EBADMSG;
2578
1f3c2eba 2579 /* rxbuf_min and rxbuf_max are no longer configurable. */
0d160211 2580
0d160211
JF
2581 return len;
2582}
2583
d61e4038
JP
2584static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2585static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2586static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
0d160211 2587
27b917e5
TI
2588static struct attribute *xennet_dev_attrs[] = {
2589 &dev_attr_rxbuf_min.attr,
2590 &dev_attr_rxbuf_max.attr,
2591 &dev_attr_rxbuf_cur.attr,
2592 NULL
2593};
0d160211 2594
27b917e5
TI
2595static const struct attribute_group xennet_dev_group = {
2596 .attrs = xennet_dev_attrs
2597};
0d160211
JF
2598#endif /* CONFIG_SYSFS */
2599
c2c63310 2600static void xennet_bus_close(struct xenbus_device *dev)
0d160211 2601{
c2c63310 2602 int ret;
0d160211 2603
c2c63310
AR
2604 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2605 return;
2606 do {
5b5971df 2607 xenbus_switch_state(dev, XenbusStateClosing);
c2c63310
AR
2608 ret = wait_event_timeout(module_wq,
2609 xenbus_read_driver_state(dev->otherend) ==
2610 XenbusStateClosing ||
2611 xenbus_read_driver_state(dev->otherend) ==
2612 XenbusStateClosed ||
2613 xenbus_read_driver_state(dev->otherend) ==
2614 XenbusStateUnknown,
2615 XENNET_TIMEOUT);
2616 } while (!ret);
2617
2618 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2619 return;
5b5971df 2620
c2c63310 2621 do {
5b5971df 2622 xenbus_switch_state(dev, XenbusStateClosed);
c2c63310
AR
2623 ret = wait_event_timeout(module_wq,
2624 xenbus_read_driver_state(dev->otherend) ==
2625 XenbusStateClosed ||
2626 xenbus_read_driver_state(dev->otherend) ==
2627 XenbusStateUnknown,
2628 XENNET_TIMEOUT);
2629 } while (!ret);
2630}
2631
2632static int xennet_remove(struct xenbus_device *dev)
2633{
2634 struct netfront_info *info = dev_get_drvdata(&dev->dev);
5b5971df 2635
c2c63310 2636 xennet_bus_close(dev);
0d160211
JF
2637 xennet_disconnect_backend(info);
2638
f599c64f
RL
2639 if (info->netdev->reg_state == NETREG_REGISTERED)
2640 unregister_netdev(info->netdev);
6bc96d04 2641
f599c64f
RL
2642 if (info->queues) {
2643 rtnl_lock();
9a873c71 2644 xennet_destroy_queues(info);
f599c64f
RL
2645 rtnl_unlock();
2646 }
900e1833 2647 xennet_free_netdev(info->netdev);
0d160211
JF
2648
2649 return 0;
2650}
2651
95afae48
DV
2652static const struct xenbus_device_id netfront_ids[] = {
2653 { "vif" },
2654 { "" }
2655};
2656
2657static struct xenbus_driver netfront_driver = {
2658 .ids = netfront_ids,
0d160211 2659 .probe = netfront_probe,
8e0e46bb 2660 .remove = xennet_remove,
0d160211 2661 .resume = netfront_resume,
f502bf2b 2662 .otherend_changed = netback_changed,
95afae48 2663};
0d160211
JF
2664
2665static int __init netif_init(void)
2666{
6e833587 2667 if (!xen_domain())
0d160211
JF
2668 return -ENODEV;
2669
51c71a3b 2670 if (!xen_has_pv_nic_devices())
b9136d20
IM
2671 return -ENODEV;
2672
383eda32 2673 pr_info("Initialising Xen virtual ethernet driver\n");
0d160211 2674
034702a6 2675 /* Allow as many queues as there are CPUs inut max. 8 if user has not
32a84405
WL
2676 * specified a value.
2677 */
2678 if (xennet_max_queues == 0)
034702a6
JG
2679 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2680 num_online_cpus());
50ee6061 2681
ffb78a26 2682 return xenbus_register_frontend(&netfront_driver);
0d160211
JF
2683}
2684module_init(netif_init);
2685
2686
2687static void __exit netif_exit(void)
2688{
ffb78a26 2689 xenbus_unregister_driver(&netfront_driver);
0d160211
JF
2690}
2691module_exit(netif_exit);
2692
2693MODULE_DESCRIPTION("Xen virtual network device frontend");
2694MODULE_LICENSE("GPL");
d2f0c52b 2695MODULE_ALIAS("xen:vif");
4f93f09b 2696MODULE_ALIAS("xennet");