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