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