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