Commit | Line | Data |
---|---|---|
0d160211 JF |
1 | /* |
2 | * Virtual network driver for conversing with remote driver backends. | |
3 | * | |
4 | * Copyright (c) 2002-2005, K A Fraser | |
5 | * Copyright (c) 2005, XenSource Ltd | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License version 2 | |
9 | * as published by the Free Software Foundation; or, when distributed | |
10 | * separately from the Linux kernel or incorporated into other | |
11 | * software packages, subject to the following license: | |
12 | * | |
13 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
14 | * of this source file (the "Software"), to deal in the Software without | |
15 | * restriction, including without limitation the rights to use, copy, modify, | |
16 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
17 | * and to permit persons to whom the Software is furnished to do so, subject to | |
18 | * the following conditions: | |
19 | * | |
20 | * The above copyright notice and this permission notice shall be included in | |
21 | * all copies or substantial portions of the Software. | |
22 | * | |
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
29 | * IN THE SOFTWARE. | |
30 | */ | |
31 | ||
383eda32 JP |
32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
33 | ||
0d160211 JF |
34 | #include <linux/module.h> |
35 | #include <linux/kernel.h> | |
36 | #include <linux/netdevice.h> | |
37 | #include <linux/etherdevice.h> | |
38 | #include <linux/skbuff.h> | |
39 | #include <linux/ethtool.h> | |
40 | #include <linux/if_ether.h> | |
9ecd1a75 | 41 | #include <net/tcp.h> |
0d160211 JF |
42 | #include <linux/udp.h> |
43 | #include <linux/moduleparam.h> | |
44 | #include <linux/mm.h> | |
5a0e3ad6 | 45 | #include <linux/slab.h> |
0d160211 | 46 | #include <net/ip.h> |
6c5aa6fc | 47 | #include <linux/bpf.h> |
a9ca9f9c | 48 | #include <net/page_pool/types.h> |
6c5aa6fc | 49 | #include <linux/bpf_trace.h> |
0d160211 | 50 | |
1ccbf534 | 51 | #include <xen/xen.h> |
0d160211 JF |
52 | #include <xen/xenbus.h> |
53 | #include <xen/events.h> | |
54 | #include <xen/page.h> | |
b9136d20 | 55 | #include <xen/platform_pci.h> |
0d160211 JF |
56 | #include <xen/grant_table.h> |
57 | ||
58 | #include <xen/interface/io/netif.h> | |
59 | #include <xen/interface/memory.h> | |
60 | #include <xen/interface/grant_table.h> | |
61 | ||
50ee6061 | 62 | /* Module parameters */ |
034702a6 | 63 | #define MAX_QUEUES_DEFAULT 8 |
50ee6061 AB |
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 | ||
4491001c RPM |
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 | ||
c2c63310 AR |
73 | #define XENNET_TIMEOUT (5 * HZ) |
74 | ||
0fc0b732 | 75 | static const struct ethtool_ops xennet_ethtool_ops; |
0d160211 JF |
76 | |
77 | struct netfront_cb { | |
3683243b | 78 | int pull_to; |
0d160211 JF |
79 | }; |
80 | ||
81 | #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb)) | |
82 | ||
83 | #define RX_COPY_THRESHOLD 256 | |
84 | ||
30c5d7f0 JG |
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) | |
1f3c2eba DV |
87 | |
88 | /* Minimum number of Rx slots (includes slot for GSO metadata). */ | |
89 | #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1) | |
0d160211 | 90 | |
2688fcb7 AB |
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 | ||
8edfe2e9 | 97 | static DECLARE_WAIT_QUEUE_HEAD(module_wq); |
5b5971df | 98 | |
e00f85be | 99 | struct netfront_stats { |
900e1833 DV |
100 | u64 packets; |
101 | u64 bytes; | |
e00f85be | 102 | struct u64_stats_sync syncp; |
103 | }; | |
104 | ||
2688fcb7 AB |
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; | |
0d160211 | 111 | |
6c5aa6fc DK |
112 | struct bpf_prog __rcu *xdp_prog; |
113 | ||
bea3348e | 114 | struct napi_struct napi; |
0d160211 | 115 | |
d634bf2c WL |
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 */ | |
2688fcb7 AB |
122 | char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */ |
123 | char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */ | |
0d160211 | 124 | |
84284d3c JF |
125 | spinlock_t tx_lock; |
126 | struct xen_netif_tx_front_ring tx; | |
127 | int tx_ring_ref; | |
0d160211 JF |
128 | |
129 | /* | |
130 | * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries | |
21631d2d | 131 | * are linked from tx_skb_freelist through tx_link. |
0d160211 | 132 | */ |
21631d2d JG |
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 | |
a884daa6 | 136 | #define TX_PENDING 0xfffe |
0d160211 JF |
137 | grant_ref_t gref_tx_head; |
138 | grant_ref_t grant_tx_ref[NET_TX_RING_SIZE]; | |
cefe0078 | 139 | struct page *grant_tx_page[NET_TX_RING_SIZE]; |
0d160211 | 140 | unsigned tx_skb_freelist; |
a884daa6 | 141 | unsigned int tx_pend_queue; |
0d160211 | 142 | |
84284d3c JF |
143 | spinlock_t rx_lock ____cacheline_aligned_in_smp; |
144 | struct xen_netif_rx_front_ring rx; | |
145 | int rx_ring_ref; | |
146 | ||
84284d3c JF |
147 | struct timer_list rx_refill_timer; |
148 | ||
0d160211 JF |
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]; | |
6c5aa6fc | 152 | |
b27d4795 JG |
153 | unsigned int rx_rsp_unconsumed; |
154 | spinlock_t rx_cons_lock; | |
155 | ||
6c5aa6fc DK |
156 | struct page_pool *page_pool; |
157 | struct xdp_rxq_info xdp_rxq; | |
2688fcb7 AB |
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; | |
e0ce4af9 IC |
168 | |
169 | /* Statistics */ | |
900e1833 DV |
170 | struct netfront_stats __percpu *rx_stats; |
171 | struct netfront_stats __percpu *tx_stats; | |
e00f85be | 172 | |
6c5aa6fc DK |
173 | /* XDP state */ |
174 | bool netback_has_xdp_headroom; | |
175 | bool netfront_xdp_enabled; | |
176 | ||
a884daa6 JG |
177 | /* Is device behaving sane? */ |
178 | bool broken; | |
179 | ||
4491001c RPM |
180 | /* Should skbs be bounced into a zeroed buffer? */ |
181 | bool bounce; | |
182 | ||
2688fcb7 | 183 | atomic_t rx_gso_checksum_fixup; |
0d160211 JF |
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 | ||
21631d2d JG |
195 | static void add_id_to_list(unsigned *head, unsigned short *list, |
196 | unsigned short id) | |
0d160211 | 197 | { |
21631d2d | 198 | list[id] = *head; |
0d160211 JF |
199 | *head = id; |
200 | } | |
201 | ||
21631d2d | 202 | static unsigned short get_id_from_list(unsigned *head, unsigned short *list) |
0d160211 JF |
203 | { |
204 | unsigned int id = *head; | |
21631d2d JG |
205 | |
206 | if (id != TX_LINK_NONE) { | |
207 | *head = list[id]; | |
208 | list[id] = TX_LINK_NONE; | |
209 | } | |
0d160211 JF |
210 | return id; |
211 | } | |
212 | ||
213 | static int xennet_rxidx(RING_IDX idx) | |
214 | { | |
215 | return idx & (NET_RX_RING_SIZE - 1); | |
216 | } | |
217 | ||
2688fcb7 | 218 | static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue, |
0d160211 JF |
219 | RING_IDX ri) |
220 | { | |
221 | int i = xennet_rxidx(ri); | |
2688fcb7 AB |
222 | struct sk_buff *skb = queue->rx_skbs[i]; |
223 | queue->rx_skbs[i] = NULL; | |
0d160211 JF |
224 | return skb; |
225 | } | |
226 | ||
2688fcb7 | 227 | static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue, |
0d160211 JF |
228 | RING_IDX ri) |
229 | { | |
230 | int i = xennet_rxidx(ri); | |
2688fcb7 | 231 | grant_ref_t ref = queue->grant_rx_ref[i]; |
145daab2 | 232 | queue->grant_rx_ref[i] = INVALID_GRANT_REF; |
0d160211 JF |
233 | return ref; |
234 | } | |
235 | ||
236 | #ifdef CONFIG_SYSFS | |
27b917e5 | 237 | static const struct attribute_group xennet_dev_group; |
0d160211 JF |
238 | #endif |
239 | ||
3ad9b358 | 240 | static bool xennet_can_sg(struct net_device *dev) |
0d160211 | 241 | { |
3ad9b358 | 242 | return dev->features & NETIF_F_SG; |
0d160211 JF |
243 | } |
244 | ||
245 | ||
e99e88a9 | 246 | static void rx_refill_timeout(struct timer_list *t) |
0d160211 | 247 | { |
41cb0855 IM |
248 | struct netfront_queue *queue = timer_container_of(queue, t, |
249 | rx_refill_timer); | |
2688fcb7 | 250 | napi_schedule(&queue->napi); |
0d160211 JF |
251 | } |
252 | ||
2688fcb7 | 253 | static int netfront_tx_slot_available(struct netfront_queue *queue) |
0d160211 | 254 | { |
2688fcb7 | 255 | return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) < |
57f230ab | 256 | (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1); |
0d160211 JF |
257 | } |
258 | ||
2688fcb7 | 259 | static void xennet_maybe_wake_tx(struct netfront_queue *queue) |
0d160211 | 260 | { |
2688fcb7 AB |
261 | struct net_device *dev = queue->info->netdev; |
262 | struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id); | |
0d160211 | 263 | |
2688fcb7 AB |
264 | if (unlikely(netif_tx_queue_stopped(dev_queue)) && |
265 | netfront_tx_slot_available(queue) && | |
0d160211 | 266 | likely(netif_running(dev))) |
2688fcb7 | 267 | netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id)); |
0d160211 JF |
268 | } |
269 | ||
1f3c2eba DV |
270 | |
271 | static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue) | |
0d160211 | 272 | { |
0d160211 JF |
273 | struct sk_buff *skb; |
274 | struct page *page; | |
0d160211 | 275 | |
1f3c2eba DV |
276 | skb = __netdev_alloc_skb(queue->info->netdev, |
277 | RX_COPY_THRESHOLD + NET_IP_ALIGN, | |
278 | GFP_ATOMIC | __GFP_NOWARN); | |
279 | if (unlikely(!skb)) | |
280 | return NULL; | |
0d160211 | 281 | |
307c8de2 RPM |
282 | page = page_pool_alloc_pages(queue->page_pool, |
283 | GFP_ATOMIC | __GFP_NOWARN | __GFP_ZERO); | |
6c5aa6fc | 284 | if (unlikely(!page)) { |
1f3c2eba DV |
285 | kfree_skb(skb); |
286 | return NULL; | |
0d160211 | 287 | } |
1f3c2eba | 288 | skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE); |
03796540 | 289 | skb_mark_for_recycle(skb); |
1f3c2eba DV |
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 | } | |
0d160211 | 297 | |
1f3c2eba DV |
298 | |
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; | |
538d9291 | 303 | int err = 0; |
1f3c2eba DV |
304 | |
305 | if (unlikely(!netif_carrier_ok(queue->info->netdev))) | |
0d160211 | 306 | return; |
0d160211 | 307 | |
1f3c2eba DV |
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; | |
30c5d7f0 | 314 | struct page *page; |
1f3c2eba | 315 | struct xen_netif_rx_request *req; |
0d160211 | 316 | |
1f3c2eba | 317 | skb = xennet_alloc_one_rx_buffer(queue); |
538d9291 VRP |
318 | if (!skb) { |
319 | err = -ENOMEM; | |
0d160211 | 320 | break; |
538d9291 | 321 | } |
0d160211 | 322 | |
1f3c2eba | 323 | id = xennet_rxidx(req_prod); |
0d160211 | 324 | |
2688fcb7 AB |
325 | BUG_ON(queue->rx_skbs[id]); |
326 | queue->rx_skbs[id] = skb; | |
0d160211 | 327 | |
2688fcb7 | 328 | ref = gnttab_claim_grant_reference(&queue->gref_rx_head); |
269ebce4 | 329 | WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref)); |
2688fcb7 | 330 | queue->grant_rx_ref[id] = ref; |
0d160211 | 331 | |
30c5d7f0 | 332 | page = skb_frag_page(&skb_shinfo(skb)->frags[0]); |
0d160211 | 333 | |
1f3c2eba | 334 | req = RING_GET_REQUEST(&queue->rx, req_prod); |
30c5d7f0 JG |
335 | gnttab_page_grant_foreign_access_ref_one(ref, |
336 | queue->info->xbdev->otherend_id, | |
337 | page, | |
338 | 0); | |
0d160211 JF |
339 | req->id = id; |
340 | req->gref = ref; | |
341 | } | |
342 | ||
1f3c2eba DV |
343 | queue->rx.req_prod_pvt = req_prod; |
344 | ||
538d9291 VRP |
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)) { | |
1f3c2eba DV |
352 | mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10)); |
353 | return; | |
354 | } | |
355 | ||
2688fcb7 | 356 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify); |
0d160211 | 357 | if (notify) |
2688fcb7 | 358 | notify_remote_via_irq(queue->rx_irq); |
0d160211 JF |
359 | } |
360 | ||
361 | static int xennet_open(struct net_device *dev) | |
362 | { | |
363 | struct netfront_info *np = netdev_priv(dev); | |
2688fcb7 AB |
364 | unsigned int num_queues = dev->real_num_tx_queues; |
365 | unsigned int i = 0; | |
366 | struct netfront_queue *queue = NULL; | |
367 | ||
a884daa6 | 368 | if (!np->queues || np->broken) |
f599c64f RL |
369 | return -ENODEV; |
370 | ||
2688fcb7 AB |
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); | |
0d160211 | 383 | } |
0d160211 | 384 | |
2688fcb7 | 385 | netif_tx_start_all_queues(dev); |
0d160211 JF |
386 | |
387 | return 0; | |
388 | } | |
389 | ||
b27d4795 | 390 | static bool xennet_tx_buf_gc(struct netfront_queue *queue) |
0d160211 JF |
391 | { |
392 | RING_IDX cons, prod; | |
393 | unsigned short id; | |
0d160211 | 394 | struct sk_buff *skb; |
7d0105b5 | 395 | bool more_to_do; |
b27d4795 | 396 | bool work_done = false; |
a884daa6 | 397 | const struct device *dev = &queue->info->netdev->dev; |
0d160211 | 398 | |
2688fcb7 | 399 | BUG_ON(!netif_carrier_ok(queue->info->netdev)); |
0d160211 JF |
400 | |
401 | do { | |
2688fcb7 | 402 | prod = queue->tx.sring->rsp_prod; |
a884daa6 JG |
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 | } | |
0d160211 JF |
408 | rmb(); /* Ensure we see responses up to 'rp'. */ |
409 | ||
2688fcb7 | 410 | for (cons = queue->tx.rsp_cons; cons != prod; cons++) { |
8446066b | 411 | struct xen_netif_tx_response txrsp; |
0d160211 | 412 | |
b27d4795 JG |
413 | work_done = true; |
414 | ||
8446066b JG |
415 | RING_COPY_RESPONSE(&queue->tx, cons, &txrsp); |
416 | if (txrsp.status == XEN_NETIF_RSP_NULL) | |
0d160211 JF |
417 | continue; |
418 | ||
a884daa6 JG |
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; | |
21631d2d JG |
433 | skb = queue->tx_skbs[id]; |
434 | queue->tx_skbs[id] = NULL; | |
31185df7 | 435 | if (unlikely(!gnttab_end_foreign_access_ref( |
c94b731d | 436 | queue->grant_tx_ref[id]))) { |
a884daa6 JG |
437 | dev_alert(dev, |
438 | "Grant still in use by backend domain\n"); | |
439 | goto err; | |
0d160211 | 440 | } |
0d160211 | 441 | gnttab_release_grant_reference( |
2688fcb7 | 442 | &queue->gref_tx_head, queue->grant_tx_ref[id]); |
145daab2 | 443 | queue->grant_tx_ref[id] = INVALID_GRANT_REF; |
2688fcb7 | 444 | queue->grant_tx_page[id] = NULL; |
21631d2d | 445 | add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id); |
0d160211 JF |
446 | dev_kfree_skb_irq(skb); |
447 | } | |
448 | ||
2688fcb7 | 449 | queue->tx.rsp_cons = prod; |
0d160211 | 450 | |
7d0105b5 MC |
451 | RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do); |
452 | } while (more_to_do); | |
0d160211 | 453 | |
2688fcb7 | 454 | xennet_maybe_wake_tx(queue); |
a884daa6 | 455 | |
b27d4795 | 456 | return work_done; |
a884daa6 JG |
457 | |
458 | err: | |
459 | queue->info->broken = true; | |
460 | dev_alert(dev, "Disabled for further use\n"); | |
b27d4795 JG |
461 | |
462 | return work_done; | |
0d160211 JF |
463 | } |
464 | ||
30c5d7f0 JG |
465 | struct xennet_gnttab_make_txreq { |
466 | struct netfront_queue *queue; | |
467 | struct sk_buff *skb; | |
468 | struct page *page; | |
162081ec JG |
469 | struct xen_netif_tx_request *tx; /* Last request on ring page */ |
470 | struct xen_netif_tx_request tx_local; /* Last request local copy*/ | |
30c5d7f0 JG |
471 | unsigned int size; |
472 | }; | |
473 | ||
474 | static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset, | |
475 | unsigned int len, void *data) | |
0d160211 | 476 | { |
30c5d7f0 | 477 | struct xennet_gnttab_make_txreq *info = data; |
0d160211 | 478 | unsigned int id; |
a55e8bb8 | 479 | struct xen_netif_tx_request *tx; |
0d160211 | 480 | grant_ref_t ref; |
30c5d7f0 JG |
481 | /* convenient aliases */ |
482 | struct page *page = info->page; | |
483 | struct netfront_queue *queue = info->queue; | |
484 | struct sk_buff *skb = info->skb; | |
0d160211 | 485 | |
21631d2d | 486 | id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link); |
a55e8bb8 DV |
487 | tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); |
488 | ref = gnttab_claim_grant_reference(&queue->gref_tx_head); | |
269ebce4 | 489 | WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref)); |
0d160211 | 490 | |
30c5d7f0 JG |
491 | gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id, |
492 | gfn, GNTMAP_readonly); | |
0d160211 | 493 | |
21631d2d | 494 | queue->tx_skbs[id] = skb; |
a55e8bb8 DV |
495 | queue->grant_tx_page[id] = page; |
496 | queue->grant_tx_ref[id] = ref; | |
0d160211 | 497 | |
162081ec JG |
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; | |
0d160211 | 505 | |
a884daa6 JG |
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 | ||
30c5d7f0 | 512 | info->tx = tx; |
162081ec | 513 | info->size += info->tx_local.size; |
30c5d7f0 JG |
514 | } |
515 | ||
516 | static struct xen_netif_tx_request *xennet_make_first_txreq( | |
162081ec JG |
517 | struct xennet_gnttab_make_txreq *info, |
518 | unsigned int offset, unsigned int len) | |
30c5d7f0 | 519 | { |
162081ec | 520 | info->size = 0; |
30c5d7f0 | 521 | |
162081ec | 522 | gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info); |
30c5d7f0 | 523 | |
162081ec | 524 | return info->tx; |
30c5d7f0 JG |
525 | } |
526 | ||
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); | |
a55e8bb8 | 535 | } |
0d160211 | 536 | |
162081ec JG |
537 | static void xennet_make_txreqs( |
538 | struct xennet_gnttab_make_txreq *info, | |
539 | struct page *page, | |
a55e8bb8 DV |
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; | |
0d160211 | 545 | |
a55e8bb8 | 546 | while (len) { |
162081ec JG |
547 | info->page = page; |
548 | info->size = 0; | |
30c5d7f0 JG |
549 | |
550 | gnttab_foreach_grant_in_range(page, offset, len, | |
551 | xennet_make_one_txreq, | |
162081ec | 552 | info); |
30c5d7f0 | 553 | |
a55e8bb8 DV |
554 | page++; |
555 | offset = 0; | |
162081ec | 556 | len -= info->size; |
0d160211 | 557 | } |
0d160211 JF |
558 | } |
559 | ||
f36c3747 | 560 | /* |
e84448d5 DV |
561 | * Count how many ring slots are required to send this skb. Each frag |
562 | * might be a compound page. | |
f36c3747 | 563 | */ |
e84448d5 | 564 | static int xennet_count_skb_slots(struct sk_buff *skb) |
f36c3747 IC |
565 | { |
566 | int i, frags = skb_shinfo(skb)->nr_frags; | |
30c5d7f0 | 567 | int slots; |
e84448d5 | 568 | |
30c5d7f0 JG |
569 | slots = gnttab_count_grant(offset_in_page(skb->data), |
570 | skb_headlen(skb)); | |
f36c3747 IC |
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); | |
b54c9d5b | 575 | unsigned long offset = skb_frag_off(frag); |
f36c3747 IC |
576 | |
577 | /* Skip unused frames from start of page */ | |
578 | offset &= ~PAGE_MASK; | |
579 | ||
30c5d7f0 | 580 | slots += gnttab_count_grant(offset, size); |
f36c3747 IC |
581 | } |
582 | ||
30c5d7f0 | 583 | return slots; |
f36c3747 IC |
584 | } |
585 | ||
50ee6061 | 586 | static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb, |
a350ecce | 587 | struct net_device *sb_dev) |
2688fcb7 | 588 | { |
50ee6061 AB |
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; | |
2688fcb7 AB |
602 | } |
603 | ||
a884daa6 JG |
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 | ||
6c5aa6fc DK |
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); | |
162081ec JG |
619 | struct xennet_gnttab_make_txreq info = { |
620 | .queue = queue, | |
621 | .skb = NULL, | |
622 | .page = virt_to_page(xdpf->data), | |
623 | }; | |
6c5aa6fc DK |
624 | int notify; |
625 | ||
162081ec | 626 | xennet_make_first_txreq(&info, |
6c5aa6fc DK |
627 | offset_in_page(xdpf->data), |
628 | xdpf->len); | |
629 | ||
a884daa6 JG |
630 | xennet_mark_tx_pending(queue); |
631 | ||
6c5aa6fc DK |
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 | ||
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; | |
fdc13979 LB |
653 | int nxmit = 0; |
654 | int i; | |
6c5aa6fc | 655 | |
a884daa6 JG |
656 | if (unlikely(np->broken)) |
657 | return -ENODEV; | |
6c5aa6fc DK |
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; | |
fdc13979 LB |
669 | if (xennet_xdp_xmit_one(dev, queue, xdpf)) |
670 | break; | |
671 | nxmit++; | |
6c5aa6fc DK |
672 | } |
673 | spin_unlock_irqrestore(&queue->tx_lock, irq_flags); | |
674 | ||
fdc13979 | 675 | return nxmit; |
6c5aa6fc DK |
676 | } |
677 | ||
53ff2517 | 678 | static struct sk_buff *bounce_skb(const struct sk_buff *skb) |
4491001c RPM |
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 | } | |
6c5aa6fc | 705 | |
30c5d7f0 JG |
706 | #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1) |
707 | ||
24a94b3c | 708 | static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev) |
0d160211 | 709 | { |
0d160211 | 710 | struct netfront_info *np = netdev_priv(dev); |
900e1833 | 711 | struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats); |
162081ec | 712 | struct xen_netif_tx_request *first_tx; |
a55e8bb8 | 713 | unsigned int i; |
0d160211 | 714 | int notify; |
f36c3747 | 715 | int slots; |
a55e8bb8 DV |
716 | struct page *page; |
717 | unsigned int offset; | |
718 | unsigned int len; | |
cf66f9d4 | 719 | unsigned long flags; |
2688fcb7 | 720 | struct netfront_queue *queue = NULL; |
162081ec | 721 | struct xennet_gnttab_make_txreq info = { }; |
2688fcb7 AB |
722 | unsigned int num_queues = dev->real_num_tx_queues; |
723 | u16 queue_index; | |
fd07160b | 724 | struct sk_buff *nskb; |
2688fcb7 AB |
725 | |
726 | /* Drop the packet if no queues are set up */ | |
727 | if (num_queues < 1) | |
728 | goto drop; | |
a884daa6 JG |
729 | if (unlikely(np->broken)) |
730 | goto drop; | |
2688fcb7 AB |
731 | /* Determine which queue to transmit this SKB on */ |
732 | queue_index = skb_get_queue_mapping(skb); | |
733 | queue = &np->queues[queue_index]; | |
0d160211 | 734 | |
9ecd1a75 WL |
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 | ||
e84448d5 | 745 | slots = xennet_count_skb_slots(skb); |
30c5d7f0 | 746 | if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) { |
97a6d1bb ZK |
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; | |
0d160211 JF |
751 | } |
752 | ||
a55e8bb8 DV |
753 | page = virt_to_page(skb->data); |
754 | offset = offset_in_page(skb->data); | |
fd07160b VK |
755 | |
756 | /* The first req should be at least ETH_HLEN size or the packet will be | |
757 | * dropped by netback. | |
4491001c RPM |
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. | |
fd07160b | 762 | */ |
4491001c RPM |
763 | if (np->bounce || unlikely(PAGE_SIZE - offset < ETH_HLEN)) { |
764 | nskb = bounce_skb(skb); | |
fd07160b VK |
765 | if (!nskb) |
766 | goto drop; | |
62f3250f | 767 | dev_consume_skb_any(skb); |
fd07160b VK |
768 | skb = nskb; |
769 | page = virt_to_page(skb->data); | |
770 | offset = offset_in_page(skb->data); | |
771 | } | |
772 | ||
a55e8bb8 DV |
773 | len = skb_headlen(skb); |
774 | ||
2688fcb7 | 775 | spin_lock_irqsave(&queue->tx_lock, flags); |
0d160211 JF |
776 | |
777 | if (unlikely(!netif_carrier_ok(dev) || | |
f36c3747 | 778 | (slots > 1 && !xennet_can_sg(dev)) || |
8b86a61d | 779 | netif_needs_gso(skb, netif_skb_features(skb)))) { |
2688fcb7 | 780 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
0d160211 JF |
781 | goto drop; |
782 | } | |
783 | ||
a55e8bb8 | 784 | /* First request for the linear area. */ |
162081ec JG |
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; | |
30c5d7f0 JG |
790 | if (offset == PAGE_SIZE) { |
791 | page++; | |
792 | offset = 0; | |
793 | } | |
162081ec | 794 | len -= info.tx_local.size; |
0d160211 | 795 | |
0d160211 JF |
796 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
797 | /* local packet? */ | |
162081ec JG |
798 | first_tx->flags |= XEN_NETTXF_csum_blank | |
799 | XEN_NETTXF_data_validated; | |
0d160211 JF |
800 | else if (skb->ip_summed == CHECKSUM_UNNECESSARY) |
801 | /* remote but checksummed. */ | |
162081ec | 802 | first_tx->flags |= XEN_NETTXF_data_validated; |
0d160211 | 803 | |
a55e8bb8 | 804 | /* Optional extra info after the first request. */ |
0d160211 JF |
805 | if (skb_shinfo(skb)->gso_size) { |
806 | struct xen_netif_extra_info *gso; | |
807 | ||
808 | gso = (struct xen_netif_extra_info *) | |
a55e8bb8 | 809 | RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); |
0d160211 | 810 | |
162081ec | 811 | first_tx->flags |= XEN_NETTXF_extra_info; |
0d160211 JF |
812 | |
813 | gso->u.gso.size = skb_shinfo(skb)->gso_size; | |
2c0057de PD |
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; | |
0d160211 JF |
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; | |
0d160211 JF |
822 | } |
823 | ||
a55e8bb8 | 824 | /* Requests for the rest of the linear area. */ |
162081ec | 825 | xennet_make_txreqs(&info, page, offset, len); |
a55e8bb8 DV |
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]; | |
162081ec | 830 | xennet_make_txreqs(&info, skb_frag_page(frag), |
b54c9d5b | 831 | skb_frag_off(frag), |
a55e8bb8 DV |
832 | skb_frag_size(frag)); |
833 | } | |
0d160211 | 834 | |
a55e8bb8 DV |
835 | /* First request has the packet length. */ |
836 | first_tx->size = skb->len; | |
0d160211 | 837 | |
91ffb9d3 DD |
838 | /* timestamp packet in software */ |
839 | skb_tx_timestamp(skb); | |
840 | ||
a884daa6 JG |
841 | xennet_mark_tx_pending(queue); |
842 | ||
2688fcb7 | 843 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); |
0d160211 | 844 | if (notify) |
2688fcb7 | 845 | notify_remote_via_irq(queue->tx_irq); |
0d160211 | 846 | |
900e1833 DV |
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); | |
10a273a6 JF |
851 | |
852 | /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */ | |
2688fcb7 | 853 | xennet_tx_buf_gc(queue); |
0d160211 | 854 | |
2688fcb7 AB |
855 | if (!netfront_tx_slot_available(queue)) |
856 | netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id)); | |
0d160211 | 857 | |
2688fcb7 | 858 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
0d160211 | 859 | |
6ed10654 | 860 | return NETDEV_TX_OK; |
0d160211 JF |
861 | |
862 | drop: | |
09f75cd7 | 863 | dev->stats.tx_dropped++; |
979de8a0 | 864 | dev_kfree_skb_any(skb); |
6ed10654 | 865 | return NETDEV_TX_OK; |
0d160211 JF |
866 | } |
867 | ||
868 | static int xennet_close(struct net_device *dev) | |
869 | { | |
870 | struct netfront_info *np = netdev_priv(dev); | |
f9244fb5 | 871 | unsigned int num_queues = np->queues ? dev->real_num_tx_queues : 0; |
2688fcb7 AB |
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 | } | |
0d160211 JF |
879 | return 0; |
880 | } | |
881 | ||
dcf4ff7a MMG |
882 | static void xennet_destroy_queues(struct netfront_info *info) |
883 | { | |
884 | unsigned int i; | |
885 | ||
f9244fb5 JG |
886 | if (!info->queues) |
887 | return; | |
888 | ||
dcf4ff7a MMG |
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 | ||
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 | ||
b27d4795 JG |
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; | |
6fac592c | 913 | queue->rx_rsp_unconsumed = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx); |
b27d4795 JG |
914 | spin_unlock_irqrestore(&queue->rx_cons_lock, flags); |
915 | } | |
916 | ||
2688fcb7 | 917 | static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb, |
0d160211 JF |
918 | grant_ref_t ref) |
919 | { | |
2688fcb7 AB |
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++; | |
0d160211 JF |
928 | } |
929 | ||
2688fcb7 | 930 | static int xennet_get_extras(struct netfront_queue *queue, |
0d160211 JF |
931 | struct xen_netif_extra_info *extras, |
932 | RING_IDX rp) | |
933 | ||
934 | { | |
8446066b | 935 | struct xen_netif_extra_info extra; |
2688fcb7 AB |
936 | struct device *dev = &queue->info->netdev->dev; |
937 | RING_IDX cons = queue->rx.rsp_cons; | |
0d160211 JF |
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 | ||
8446066b | 951 | RING_COPY_RESPONSE(&queue->rx, ++cons, &extra); |
0d160211 | 952 | |
8446066b JG |
953 | if (unlikely(!extra.type || |
954 | extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) { | |
0d160211 JF |
955 | if (net_ratelimit()) |
956 | dev_warn(dev, "Invalid extra type: %d\n", | |
8446066b | 957 | extra.type); |
0d160211 JF |
958 | err = -EINVAL; |
959 | } else { | |
8446066b | 960 | extras[extra.type - 1] = extra; |
0d160211 JF |
961 | } |
962 | ||
2688fcb7 AB |
963 | skb = xennet_get_rx_skb(queue, cons); |
964 | ref = xennet_get_rx_ref(queue, cons); | |
965 | xennet_move_rx_slot(queue, skb, ref); | |
8446066b | 966 | } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); |
0d160211 | 967 | |
b27d4795 | 968 | xennet_set_rx_rsp_cons(queue, cons); |
0d160211 JF |
969 | return err; |
970 | } | |
971 | ||
6c5aa6fc DK |
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; | |
e44f65fd | 978 | u32 act; |
6c5aa6fc DK |
979 | int err; |
980 | ||
43b5169d LB |
981 | xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM, |
982 | &queue->xdp_rxq); | |
be9df4af LB |
983 | xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM, |
984 | len, false); | |
6c5aa6fc DK |
985 | |
986 | act = bpf_prog_run_xdp(prog, xdp); | |
987 | switch (act) { | |
988 | case XDP_TX: | |
6c5aa6fc | 989 | xdpf = xdp_convert_buff_to_frame(xdp); |
cc3628dc AN |
990 | if (unlikely(!xdpf)) { |
991 | trace_xdp_exception(queue->info->netdev, prog, act); | |
992 | break; | |
993 | } | |
994 | get_page(pdata); | |
6c5aa6fc | 995 | err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0); |
cc3628dc AN |
996 | if (unlikely(err <= 0)) { |
997 | if (err < 0) | |
998 | trace_xdp_exception(queue->info->netdev, prog, act); | |
fdc13979 | 999 | xdp_return_frame_rx_napi(xdpf); |
cc3628dc | 1000 | } |
6c5aa6fc DK |
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; | |
cc3628dc | 1006 | if (unlikely(err)) { |
6c5aa6fc | 1007 | trace_xdp_exception(queue->info->netdev, prog, act); |
cc3628dc AN |
1008 | xdp_return_buff(xdp); |
1009 | } | |
6c5aa6fc DK |
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: | |
c8064e5b | 1020 | bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act); |
6c5aa6fc DK |
1021 | } |
1022 | ||
1023 | return act; | |
1024 | } | |
1025 | ||
2688fcb7 | 1026 | static int xennet_get_responses(struct netfront_queue *queue, |
0d160211 | 1027 | struct netfront_rx_info *rinfo, RING_IDX rp, |
6c5aa6fc DK |
1028 | struct sk_buff_head *list, |
1029 | bool *need_xdp_flush) | |
0d160211 | 1030 | { |
8446066b | 1031 | struct xen_netif_rx_response *rx = &rinfo->rx, rx_local; |
6c5aa6fc | 1032 | int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD); |
2688fcb7 AB |
1033 | RING_IDX cons = queue->rx.rsp_cons; |
1034 | struct sk_buff *skb = xennet_get_rx_skb(queue, cons); | |
6c5aa6fc | 1035 | struct xen_netif_extra_info *extras = rinfo->extras; |
2688fcb7 | 1036 | grant_ref_t ref = xennet_get_rx_ref(queue, cons); |
6c5aa6fc DK |
1037 | struct device *dev = &queue->info->netdev->dev; |
1038 | struct bpf_prog *xdp_prog; | |
1039 | struct xdp_buff xdp; | |
7158ff6d | 1040 | int slots = 1; |
0d160211 | 1041 | int err = 0; |
6c5aa6fc | 1042 | u32 verdict; |
0d160211 | 1043 | |
f942dc25 | 1044 | if (rx->flags & XEN_NETRXF_extra_info) { |
2688fcb7 | 1045 | err = xennet_get_extras(queue, extras, rp); |
6c5aa6fc DK |
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 | } | |
2688fcb7 | 1054 | cons = queue->rx.rsp_cons; |
0d160211 JF |
1055 | } |
1056 | ||
1057 | for (;;) { | |
0d160211 JF |
1058 | /* |
1059 | * This definitely indicates a bug, either in this driver or in | |
1060 | * the backend driver. In future this should flag the bad | |
697089dc | 1061 | * situation to the system controller to reboot the backend. |
0d160211 | 1062 | */ |
145daab2 | 1063 | if (ref == INVALID_GRANT_REF) { |
0d160211 JF |
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 | ||
06673c21 JB |
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 | ||
c94b731d | 1081 | if (!gnttab_end_foreign_access_ref(ref)) { |
66e3531b JG |
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 | } | |
0d160211 | 1088 | |
2688fcb7 | 1089 | gnttab_release_grant_reference(&queue->gref_rx_head, ref); |
0d160211 | 1090 | |
6c5aa6fc DK |
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(); | |
f63c2c20 | 1107 | |
6c5aa6fc | 1108 | __skb_queue_tail(list, skb); |
f63c2c20 JB |
1109 | |
1110 | next: | |
f942dc25 | 1111 | if (!(rx->flags & XEN_NETRXF_more_data)) |
0d160211 JF |
1112 | break; |
1113 | ||
7158ff6d | 1114 | if (cons + slots == rp) { |
0d160211 | 1115 | if (net_ratelimit()) |
7158ff6d | 1116 | dev_warn(dev, "Need more slots\n"); |
0d160211 JF |
1117 | err = -ENOENT; |
1118 | break; | |
1119 | } | |
1120 | ||
8446066b JG |
1121 | RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local); |
1122 | rx = &rx_local; | |
2688fcb7 AB |
1123 | skb = xennet_get_rx_skb(queue, cons + slots); |
1124 | ref = xennet_get_rx_ref(queue, cons + slots); | |
7158ff6d | 1125 | slots++; |
0d160211 JF |
1126 | } |
1127 | ||
7158ff6d | 1128 | if (unlikely(slots > max)) { |
0d160211 | 1129 | if (net_ratelimit()) |
697089dc | 1130 | dev_warn(dev, "Too many slots\n"); |
0d160211 JF |
1131 | err = -E2BIG; |
1132 | } | |
1133 | ||
1134 | if (unlikely(err)) | |
b27d4795 | 1135 | xennet_set_rx_rsp_cons(queue, cons + slots); |
0d160211 JF |
1136 | |
1137 | return err; | |
1138 | } | |
1139 | ||
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()) | |
383eda32 | 1145 | pr_warn("GSO size must not be zero\n"); |
0d160211 JF |
1146 | return -EINVAL; |
1147 | } | |
1148 | ||
2c0057de PD |
1149 | if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 && |
1150 | gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) { | |
0d160211 | 1151 | if (net_ratelimit()) |
383eda32 | 1152 | pr_warn("Bad GSO type %d\n", gso->u.gso.type); |
0d160211 JF |
1153 | return -EINVAL; |
1154 | } | |
1155 | ||
1156 | skb_shinfo(skb)->gso_size = gso->u.gso.size; | |
2c0057de PD |
1157 | skb_shinfo(skb)->gso_type = |
1158 | (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ? | |
1159 | SKB_GSO_TCPV4 : | |
1160 | SKB_GSO_TCPV6; | |
0d160211 JF |
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 | ||
a761129e DZ |
1169 | static int xennet_fill_frags(struct netfront_queue *queue, |
1170 | struct sk_buff *skb, | |
1171 | struct sk_buff_head *list) | |
0d160211 | 1172 | { |
2688fcb7 | 1173 | RING_IDX cons = queue->rx.rsp_cons; |
0d160211 JF |
1174 | struct sk_buff *nskb; |
1175 | ||
1176 | while ((nskb = __skb_dequeue(list))) { | |
8446066b | 1177 | struct xen_netif_rx_response rx; |
01c68026 | 1178 | skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0]; |
0d160211 | 1179 | |
8446066b JG |
1180 | RING_COPY_RESPONSE(&queue->rx, ++cons, &rx); |
1181 | ||
d472b3a6 | 1182 | if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) { |
093b9c71 | 1183 | unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to; |
0d160211 | 1184 | |
d81c5054 | 1185 | BUG_ON(pull_to < skb_headlen(skb)); |
093b9c71 JB |
1186 | __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); |
1187 | } | |
ad4f15dc | 1188 | if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) { |
b27d4795 JG |
1189 | xennet_set_rx_rsp_cons(queue, |
1190 | ++cons + skb_queue_len(list)); | |
ad4f15dc | 1191 | kfree_skb(nskb); |
a761129e | 1192 | return -ENOENT; |
ad4f15dc | 1193 | } |
093b9c71 | 1194 | |
d472b3a6 JG |
1195 | skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, |
1196 | skb_frag_page(nfrag), | |
8446066b | 1197 | rx.offset, rx.status, PAGE_SIZE); |
0d160211 JF |
1198 | |
1199 | skb_shinfo(nskb)->nr_frags = 0; | |
1200 | kfree_skb(nskb); | |
0d160211 JF |
1201 | } |
1202 | ||
b27d4795 | 1203 | xennet_set_rx_rsp_cons(queue, cons); |
a761129e DZ |
1204 | |
1205 | return 0; | |
0d160211 JF |
1206 | } |
1207 | ||
e0ce4af9 | 1208 | static int checksum_setup(struct net_device *dev, struct sk_buff *skb) |
0d160211 | 1209 | { |
b5cf66cd | 1210 | bool recalculate_partial_csum = false; |
e0ce4af9 IC |
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); | |
2688fcb7 | 1220 | atomic_inc(&np->rx_gso_checksum_fixup); |
e0ce4af9 | 1221 | skb->ip_summed = CHECKSUM_PARTIAL; |
b5cf66cd | 1222 | recalculate_partial_csum = true; |
e0ce4af9 IC |
1223 | } |
1224 | ||
1225 | /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ | |
1226 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
1227 | return 0; | |
0d160211 | 1228 | |
b5cf66cd | 1229 | return skb_checksum_setup(skb, recalculate_partial_csum); |
0d160211 JF |
1230 | } |
1231 | ||
2688fcb7 | 1232 | static int handle_incoming_queue(struct netfront_queue *queue, |
09f75cd7 | 1233 | struct sk_buff_head *rxq) |
0d160211 | 1234 | { |
900e1833 | 1235 | struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats); |
0d160211 JF |
1236 | int packets_dropped = 0; |
1237 | struct sk_buff *skb; | |
1238 | ||
1239 | while ((skb = __skb_dequeue(rxq)) != NULL) { | |
3683243b | 1240 | int pull_to = NETFRONT_SKB_CB(skb)->pull_to; |
0d160211 | 1241 | |
093b9c71 JB |
1242 | if (pull_to > skb_headlen(skb)) |
1243 | __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); | |
0d160211 JF |
1244 | |
1245 | /* Ethernet work: Delayed to here as it peeks the header. */ | |
2688fcb7 | 1246 | skb->protocol = eth_type_trans(skb, queue->info->netdev); |
d554f73d | 1247 | skb_reset_network_header(skb); |
0d160211 | 1248 | |
2688fcb7 | 1249 | if (checksum_setup(queue->info->netdev, skb)) { |
e0ce4af9 IC |
1250 | kfree_skb(skb); |
1251 | packets_dropped++; | |
2688fcb7 | 1252 | queue->info->netdev->stats.rx_errors++; |
e0ce4af9 | 1253 | continue; |
0d160211 JF |
1254 | } |
1255 | ||
900e1833 DV |
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); | |
0d160211 JF |
1260 | |
1261 | /* Pass it up. */ | |
2688fcb7 | 1262 | napi_gro_receive(&queue->napi, skb); |
0d160211 JF |
1263 | } |
1264 | ||
1265 | return packets_dropped; | |
1266 | } | |
1267 | ||
bea3348e | 1268 | static int xennet_poll(struct napi_struct *napi, int budget) |
0d160211 | 1269 | { |
2688fcb7 AB |
1270 | struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi); |
1271 | struct net_device *dev = queue->info->netdev; | |
0d160211 JF |
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; | |
bea3348e | 1277 | int work_done; |
0d160211 JF |
1278 | struct sk_buff_head rxq; |
1279 | struct sk_buff_head errq; | |
1280 | struct sk_buff_head tmpq; | |
0d160211 | 1281 | int err; |
6c5aa6fc | 1282 | bool need_xdp_flush = false; |
0d160211 | 1283 | |
2688fcb7 | 1284 | spin_lock(&queue->rx_lock); |
0d160211 | 1285 | |
0d160211 JF |
1286 | skb_queue_head_init(&rxq); |
1287 | skb_queue_head_init(&errq); | |
1288 | skb_queue_head_init(&tmpq); | |
1289 | ||
2688fcb7 | 1290 | rp = queue->rx.sring->rsp_prod; |
a884daa6 JG |
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 | } | |
0d160211 JF |
1298 | rmb(); /* Ensure we see queued responses up to 'rp'. */ |
1299 | ||
2688fcb7 | 1300 | i = queue->rx.rsp_cons; |
0d160211 JF |
1301 | work_done = 0; |
1302 | while ((i != rp) && (work_done < budget)) { | |
8446066b | 1303 | RING_COPY_RESPONSE(&queue->rx, i, rx); |
0d160211 JF |
1304 | memset(extras, 0, sizeof(rinfo.extras)); |
1305 | ||
6c5aa6fc DK |
1306 | err = xennet_get_responses(queue, &rinfo, rp, &tmpq, |
1307 | &need_xdp_flush); | |
0d160211 JF |
1308 | |
1309 | if (unlikely(err)) { | |
66e3531b JG |
1310 | if (queue->info->broken) { |
1311 | spin_unlock(&queue->rx_lock); | |
1312 | return 0; | |
1313 | } | |
0d160211 JF |
1314 | err: |
1315 | while ((skb = __skb_dequeue(&tmpq))) | |
1316 | __skb_queue_tail(&errq, skb); | |
09f75cd7 | 1317 | dev->stats.rx_errors++; |
2688fcb7 | 1318 | i = queue->rx.rsp_cons; |
0d160211 JF |
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); | |
b27d4795 JG |
1330 | xennet_set_rx_rsp_cons(queue, |
1331 | queue->rx.rsp_cons + | |
1332 | skb_queue_len(&tmpq)); | |
0d160211 JF |
1333 | goto err; |
1334 | } | |
1335 | } | |
1336 | ||
3683243b IC |
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; | |
0d160211 | 1340 | |
b54c9d5b | 1341 | skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset); |
3683243b IC |
1342 | skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status); |
1343 | skb->data_len = rx->status; | |
093b9c71 | 1344 | skb->len += rx->status; |
0d160211 | 1345 | |
a761129e | 1346 | if (unlikely(xennet_fill_frags(queue, skb, &tmpq))) |
ad4f15dc | 1347 | goto err; |
0d160211 | 1348 | |
f942dc25 | 1349 | if (rx->flags & XEN_NETRXF_csum_blank) |
0d160211 | 1350 | skb->ip_summed = CHECKSUM_PARTIAL; |
f942dc25 | 1351 | else if (rx->flags & XEN_NETRXF_data_validated) |
0d160211 JF |
1352 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
1353 | ||
1354 | __skb_queue_tail(&rxq, skb); | |
1355 | ||
b27d4795 JG |
1356 | i = queue->rx.rsp_cons + 1; |
1357 | xennet_set_rx_rsp_cons(queue, i); | |
0d160211 JF |
1358 | work_done++; |
1359 | } | |
6c5aa6fc DK |
1360 | if (need_xdp_flush) |
1361 | xdp_do_flush(); | |
0d160211 | 1362 | |
56cfe5d0 | 1363 | __skb_queue_purge(&errq); |
0d160211 | 1364 | |
2688fcb7 | 1365 | work_done -= handle_incoming_queue(queue, &rxq); |
0d160211 | 1366 | |
2688fcb7 | 1367 | xennet_alloc_rx_buffers(queue); |
0d160211 | 1368 | |
0d160211 | 1369 | if (work_done < budget) { |
bea3348e SH |
1370 | int more_to_do = 0; |
1371 | ||
6ad20165 | 1372 | napi_complete_done(napi, work_done); |
0d160211 | 1373 | |
2688fcb7 | 1374 | RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do); |
6a6dc08f DV |
1375 | if (more_to_do) |
1376 | napi_schedule(napi); | |
0d160211 JF |
1377 | } |
1378 | ||
2688fcb7 | 1379 | spin_unlock(&queue->rx_lock); |
0d160211 | 1380 | |
bea3348e | 1381 | return work_done; |
0d160211 JF |
1382 | } |
1383 | ||
1384 | static int xennet_change_mtu(struct net_device *dev, int mtu) | |
1385 | { | |
0c36820e | 1386 | int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN; |
0d160211 JF |
1387 | |
1388 | if (mtu > max) | |
1389 | return -EINVAL; | |
1eb2cded | 1390 | WRITE_ONCE(dev->mtu, mtu); |
0d160211 JF |
1391 | return 0; |
1392 | } | |
1393 | ||
bc1f4470 | 1394 | static void xennet_get_stats64(struct net_device *dev, |
1395 | struct rtnl_link_stats64 *tot) | |
e00f85be | 1396 | { |
1397 | struct netfront_info *np = netdev_priv(dev); | |
1398 | int cpu; | |
1399 | ||
1400 | for_each_possible_cpu(cpu) { | |
900e1833 DV |
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); | |
e00f85be | 1403 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
1404 | unsigned int start; | |
1405 | ||
1406 | do { | |
068c38ad | 1407 | start = u64_stats_fetch_begin(&tx_stats->syncp); |
900e1833 DV |
1408 | tx_packets = tx_stats->packets; |
1409 | tx_bytes = tx_stats->bytes; | |
068c38ad | 1410 | } while (u64_stats_fetch_retry(&tx_stats->syncp, start)); |
e00f85be | 1411 | |
900e1833 | 1412 | do { |
068c38ad | 1413 | start = u64_stats_fetch_begin(&rx_stats->syncp); |
900e1833 DV |
1414 | rx_packets = rx_stats->packets; |
1415 | rx_bytes = rx_stats->bytes; | |
068c38ad | 1416 | } while (u64_stats_fetch_retry(&rx_stats->syncp, start)); |
e00f85be | 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; | |
e00f85be | 1426 | } |
1427 | ||
2688fcb7 | 1428 | static void xennet_release_tx_bufs(struct netfront_queue *queue) |
0d160211 JF |
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 */ | |
21631d2d | 1435 | if (!queue->tx_skbs[i]) |
0d160211 JF |
1436 | continue; |
1437 | ||
21631d2d JG |
1438 | skb = queue->tx_skbs[i]; |
1439 | queue->tx_skbs[i] = NULL; | |
2688fcb7 AB |
1440 | get_page(queue->grant_tx_page[i]); |
1441 | gnttab_end_foreign_access(queue->grant_tx_ref[i], | |
49f8b459 | 1442 | queue->grant_tx_page[i]); |
2688fcb7 | 1443 | queue->grant_tx_page[i] = NULL; |
145daab2 | 1444 | queue->grant_tx_ref[i] = INVALID_GRANT_REF; |
21631d2d | 1445 | add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i); |
0d160211 JF |
1446 | dev_kfree_skb_irq(skb); |
1447 | } | |
1448 | } | |
1449 | ||
2688fcb7 | 1450 | static void xennet_release_rx_bufs(struct netfront_queue *queue) |
0d160211 | 1451 | { |
0d160211 JF |
1452 | int id, ref; |
1453 | ||
2688fcb7 | 1454 | spin_lock_bh(&queue->rx_lock); |
0d160211 JF |
1455 | |
1456 | for (id = 0; id < NET_RX_RING_SIZE; id++) { | |
cefe0078 AL |
1457 | struct sk_buff *skb; |
1458 | struct page *page; | |
0d160211 | 1459 | |
2688fcb7 | 1460 | skb = queue->rx_skbs[id]; |
cefe0078 | 1461 | if (!skb) |
0d160211 | 1462 | continue; |
0d160211 | 1463 | |
2688fcb7 | 1464 | ref = queue->grant_rx_ref[id]; |
145daab2 | 1465 | if (ref == INVALID_GRANT_REF) |
cefe0078 | 1466 | continue; |
0d160211 | 1467 | |
cefe0078 | 1468 | page = skb_frag_page(&skb_shinfo(skb)->frags[0]); |
0d160211 | 1469 | |
cefe0078 AL |
1470 | /* gnttab_end_foreign_access() needs a page ref until |
1471 | * foreign access is ended (which may be deferred). | |
1472 | */ | |
1473 | get_page(page); | |
49f8b459 | 1474 | gnttab_end_foreign_access(ref, page); |
145daab2 | 1475 | queue->grant_rx_ref[id] = INVALID_GRANT_REF; |
0d160211 | 1476 | |
cefe0078 | 1477 | kfree_skb(skb); |
0d160211 JF |
1478 | } |
1479 | ||
2688fcb7 | 1480 | spin_unlock_bh(&queue->rx_lock); |
0d160211 JF |
1481 | } |
1482 | ||
c8f44aff MM |
1483 | static netdev_features_t xennet_fix_features(struct net_device *dev, |
1484 | netdev_features_t features) | |
8f7b01a1 ED |
1485 | { |
1486 | struct netfront_info *np = netdev_priv(dev); | |
8f7b01a1 | 1487 | |
2890ea5c JG |
1488 | if (features & NETIF_F_SG && |
1489 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0)) | |
1490 | features &= ~NETIF_F_SG; | |
8f7b01a1 | 1491 | |
2890ea5c JG |
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; | |
8f7b01a1 | 1496 | |
2890ea5c JG |
1497 | if (features & NETIF_F_TSO && |
1498 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0)) | |
1499 | features &= ~NETIF_F_TSO; | |
8f7b01a1 | 1500 | |
2890ea5c JG |
1501 | if (features & NETIF_F_TSO6 && |
1502 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0)) | |
1503 | features &= ~NETIF_F_TSO6; | |
2c0057de | 1504 | |
8f7b01a1 ED |
1505 | return features; |
1506 | } | |
1507 | ||
c8f44aff MM |
1508 | static int xennet_set_features(struct net_device *dev, |
1509 | netdev_features_t features) | |
8f7b01a1 ED |
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 | ||
b27d4795 | 1519 | static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi) |
cf66f9d4 | 1520 | { |
cf66f9d4 KRW |
1521 | unsigned long flags; |
1522 | ||
b27d4795 JG |
1523 | if (unlikely(queue->info->broken)) |
1524 | return false; | |
a884daa6 | 1525 | |
2688fcb7 | 1526 | spin_lock_irqsave(&queue->tx_lock, flags); |
b27d4795 JG |
1527 | if (xennet_tx_buf_gc(queue)) |
1528 | *eoi = 0; | |
2688fcb7 | 1529 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
cf66f9d4 | 1530 | |
b27d4795 JG |
1531 | return true; |
1532 | } | |
1533 | ||
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 | ||
d634bf2c WL |
1541 | return IRQ_HANDLED; |
1542 | } | |
1543 | ||
b27d4795 | 1544 | static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi) |
d634bf2c | 1545 | { |
b27d4795 JG |
1546 | unsigned int work_queued; |
1547 | unsigned long flags; | |
d634bf2c | 1548 | |
b27d4795 JG |
1549 | if (unlikely(queue->info->broken)) |
1550 | return false; | |
1551 | ||
1552 | spin_lock_irqsave(&queue->rx_cons_lock, flags); | |
6fac592c | 1553 | work_queued = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx); |
b27d4795 JG |
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); | |
a884daa6 | 1567 | |
b27d4795 | 1568 | if (likely(netif_carrier_ok(queue->info->netdev) && work_queued)) |
76541869 | 1569 | napi_schedule(&queue->napi); |
cf66f9d4 | 1570 | |
b27d4795 JG |
1571 | return true; |
1572 | } | |
1573 | ||
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 | ||
d634bf2c WL |
1581 | return IRQ_HANDLED; |
1582 | } | |
cf66f9d4 | 1583 | |
d634bf2c WL |
1584 | static irqreturn_t xennet_interrupt(int irq, void *dev_id) |
1585 | { | |
b27d4795 JG |
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 | ||
cf66f9d4 KRW |
1592 | return IRQ_HANDLED; |
1593 | } | |
1594 | ||
1595 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1596 | static void xennet_poll_controller(struct net_device *dev) | |
1597 | { | |
2688fcb7 AB |
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; | |
a884daa6 JG |
1602 | |
1603 | if (info->broken) | |
1604 | return; | |
1605 | ||
2688fcb7 AB |
1606 | for (i = 0; i < num_queues; ++i) |
1607 | xennet_interrupt(0, &info->queues[i]); | |
cf66f9d4 KRW |
1608 | } |
1609 | #endif | |
1610 | ||
6c5aa6fc DK |
1611 | #define NETBACK_XDP_HEADROOM_DISABLE 0 |
1612 | #define NETBACK_XDP_HEADROOM_ENABLE 1 | |
1613 | ||
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 | ||
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 | ||
6c5aa6fc DK |
1675 | static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp) |
1676 | { | |
a884daa6 JG |
1677 | struct netfront_info *np = netdev_priv(dev); |
1678 | ||
1679 | if (np->broken) | |
1680 | return -ENODEV; | |
1681 | ||
6c5aa6fc DK |
1682 | switch (xdp->command) { |
1683 | case XDP_SETUP_PROG: | |
1684 | return xennet_xdp_set(dev, xdp->prog, xdp->extack); | |
6c5aa6fc DK |
1685 | default: |
1686 | return -EINVAL; | |
1687 | } | |
1688 | } | |
1689 | ||
0a0b9d2e | 1690 | static const struct net_device_ops xennet_netdev_ops = { |
dcf4ff7a | 1691 | .ndo_uninit = xennet_uninit, |
0a0b9d2e | 1692 | .ndo_open = xennet_open, |
0a0b9d2e SH |
1693 | .ndo_stop = xennet_close, |
1694 | .ndo_start_xmit = xennet_start_xmit, | |
1695 | .ndo_change_mtu = xennet_change_mtu, | |
e00f85be | 1696 | .ndo_get_stats64 = xennet_get_stats64, |
0a0b9d2e SH |
1697 | .ndo_set_mac_address = eth_mac_addr, |
1698 | .ndo_validate_addr = eth_validate_addr, | |
fb507934 MM |
1699 | .ndo_fix_features = xennet_fix_features, |
1700 | .ndo_set_features = xennet_set_features, | |
2688fcb7 | 1701 | .ndo_select_queue = xennet_select_queue, |
6c5aa6fc DK |
1702 | .ndo_bpf = xennet_xdp, |
1703 | .ndo_xdp_xmit = xennet_xdp_xmit, | |
cf66f9d4 KRW |
1704 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1705 | .ndo_poll_controller = xennet_poll_controller, | |
1706 | #endif | |
0a0b9d2e SH |
1707 | }; |
1708 | ||
900e1833 DV |
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 | ||
8e0e46bb | 1718 | static struct net_device *xennet_create_dev(struct xenbus_device *dev) |
0d160211 | 1719 | { |
2688fcb7 | 1720 | int err; |
0d160211 JF |
1721 | struct net_device *netdev; |
1722 | struct netfront_info *np; | |
1723 | ||
50ee6061 | 1724 | netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues); |
41de8d4c | 1725 | if (!netdev) |
0d160211 | 1726 | return ERR_PTR(-ENOMEM); |
0d160211 JF |
1727 | |
1728 | np = netdev_priv(netdev); | |
1729 | np->xbdev = dev; | |
1730 | ||
2688fcb7 | 1731 | np->queues = NULL; |
0d160211 | 1732 | |
e00f85be | 1733 | err = -ENOMEM; |
900e1833 DV |
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) | |
e00f85be | 1739 | goto exit; |
1740 | ||
0a0b9d2e SH |
1741 | netdev->netdev_ops = &xennet_netdev_ops; |
1742 | ||
fb507934 MM |
1743 | netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM | |
1744 | NETIF_F_GSO_ROBUST; | |
2c0057de PD |
1745 | netdev->hw_features = NETIF_F_SG | |
1746 | NETIF_F_IPV6_CSUM | | |
1747 | NETIF_F_TSO | NETIF_F_TSO6; | |
0d160211 | 1748 | |
fc3e5941 IC |
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; | |
66c0e13a MM |
1756 | netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | |
1757 | NETDEV_XDP_ACT_NDO_XMIT; | |
fc3e5941 | 1758 | |
7ad24ea4 | 1759 | netdev->ethtool_ops = &xennet_ethtool_ops; |
e1043a4b | 1760 | netdev->min_mtu = ETH_MIN_MTU; |
d0c2c997 | 1761 | netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE; |
0d160211 JF |
1762 | SET_NETDEV_DEV(netdev, &dev->dev); |
1763 | ||
1764 | np->netdev = netdev; | |
6c5aa6fc | 1765 | np->netfront_xdp_enabled = false; |
0d160211 JF |
1766 | |
1767 | netif_carrier_off(netdev); | |
1768 | ||
c2c63310 AR |
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 | ||
0d160211 JF |
1778 | return netdev; |
1779 | ||
0d160211 | 1780 | exit: |
900e1833 | 1781 | xennet_free_netdev(netdev); |
0d160211 JF |
1782 | return ERR_PTR(err); |
1783 | } | |
1784 | ||
80708602 | 1785 | /* |
0d160211 JF |
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 | */ | |
8e0e46bb | 1790 | static int netfront_probe(struct xenbus_device *dev, |
1dd06ae8 | 1791 | const struct xenbus_device_id *id) |
0d160211 JF |
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); | |
1b713e00 | 1805 | dev_set_drvdata(&dev->dev, info); |
27b917e5 TI |
1806 | #ifdef CONFIG_SYSFS |
1807 | info->netdev->sysfs_groups[0] = &xennet_dev_group; | |
1808 | #endif | |
0d160211 | 1809 | |
0d160211 | 1810 | return 0; |
0d160211 JF |
1811 | } |
1812 | ||
1813 | static void xennet_end_access(int ref, void *page) | |
1814 | { | |
1815 | /* This frees the page as a side-effect */ | |
145daab2 | 1816 | if (ref != INVALID_GRANT_REF) |
49f8b459 | 1817 | gnttab_end_foreign_access(ref, virt_to_page(page)); |
0d160211 JF |
1818 | } |
1819 | ||
1820 | static void xennet_disconnect_backend(struct netfront_info *info) | |
1821 | { | |
2688fcb7 | 1822 | unsigned int i = 0; |
2688fcb7 AB |
1823 | unsigned int num_queues = info->netdev->real_num_tx_queues; |
1824 | ||
f9feb1e6 DV |
1825 | netif_carrier_off(info->netdev); |
1826 | ||
9a873c71 | 1827 | for (i = 0; i < num_queues && info->queues; ++i) { |
76541869 DV |
1828 | struct netfront_queue *queue = &info->queues[i]; |
1829 | ||
8fa7292f | 1830 | timer_delete_sync(&queue->rx_refill_timer); |
74470954 | 1831 | |
2688fcb7 AB |
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; | |
0d160211 | 1840 | |
274b0455 CW |
1841 | if (netif_running(info->netdev)) |
1842 | napi_synchronize(&queue->napi); | |
f9feb1e6 | 1843 | |
a5b5dc3c DV |
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 | ||
2688fcb7 AB |
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); | |
0d160211 | 1852 | |
145daab2 JG |
1853 | queue->tx_ring_ref = INVALID_GRANT_REF; |
1854 | queue->rx_ring_ref = INVALID_GRANT_REF; | |
2688fcb7 AB |
1855 | queue->tx.sring = NULL; |
1856 | queue->rx.sring = NULL; | |
6c5aa6fc DK |
1857 | |
1858 | page_pool_destroy(queue->page_pool); | |
2688fcb7 | 1859 | } |
0d160211 JF |
1860 | } |
1861 | ||
80708602 | 1862 | /* |
0d160211 JF |
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 | */ | |
1868 | static int netfront_resume(struct xenbus_device *dev) | |
1869 | { | |
1b713e00 | 1870 | struct netfront_info *info = dev_get_drvdata(&dev->dev); |
0d160211 JF |
1871 | |
1872 | dev_dbg(&dev->dev, "%s\n", dev->nodename); | |
1873 | ||
042b2046 DZ |
1874 | netif_tx_lock_bh(info->netdev); |
1875 | netif_device_detach(info->netdev); | |
1876 | netif_tx_unlock_bh(info->netdev); | |
1877 | ||
0d160211 | 1878 | xennet_disconnect_backend(info); |
d50b7914 LL |
1879 | |
1880 | rtnl_lock(); | |
1881 | if (info->queues) | |
1882 | xennet_destroy_queues(info); | |
1883 | rtnl_unlock(); | |
1884 | ||
0d160211 JF |
1885 | return 0; |
1886 | } | |
1887 | ||
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 | ||
2688fcb7 | 1910 | static int setup_netfront_single(struct netfront_queue *queue) |
d634bf2c WL |
1911 | { |
1912 | int err; | |
1913 | ||
2688fcb7 | 1914 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); |
d634bf2c WL |
1915 | if (err < 0) |
1916 | goto fail; | |
1917 | ||
b27d4795 JG |
1918 | err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn, |
1919 | xennet_interrupt, 0, | |
1920 | queue->info->netdev->name, | |
1921 | queue); | |
d634bf2c WL |
1922 | if (err < 0) |
1923 | goto bind_fail; | |
2688fcb7 AB |
1924 | queue->rx_evtchn = queue->tx_evtchn; |
1925 | queue->rx_irq = queue->tx_irq = err; | |
d634bf2c WL |
1926 | |
1927 | return 0; | |
1928 | ||
1929 | bind_fail: | |
2688fcb7 AB |
1930 | xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); |
1931 | queue->tx_evtchn = 0; | |
d634bf2c WL |
1932 | fail: |
1933 | return err; | |
1934 | } | |
1935 | ||
2688fcb7 | 1936 | static int setup_netfront_split(struct netfront_queue *queue) |
d634bf2c WL |
1937 | { |
1938 | int err; | |
1939 | ||
2688fcb7 | 1940 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); |
d634bf2c WL |
1941 | if (err < 0) |
1942 | goto fail; | |
2688fcb7 | 1943 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn); |
d634bf2c WL |
1944 | if (err < 0) |
1945 | goto alloc_rx_evtchn_fail; | |
1946 | ||
2688fcb7 AB |
1947 | snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name), |
1948 | "%s-tx", queue->name); | |
b27d4795 JG |
1949 | err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn, |
1950 | xennet_tx_interrupt, 0, | |
1951 | queue->tx_irq_name, queue); | |
d634bf2c WL |
1952 | if (err < 0) |
1953 | goto bind_tx_fail; | |
2688fcb7 | 1954 | queue->tx_irq = err; |
d634bf2c | 1955 | |
2688fcb7 AB |
1956 | snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name), |
1957 | "%s-rx", queue->name); | |
b27d4795 JG |
1958 | err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn, |
1959 | xennet_rx_interrupt, 0, | |
1960 | queue->rx_irq_name, queue); | |
d634bf2c WL |
1961 | if (err < 0) |
1962 | goto bind_rx_fail; | |
2688fcb7 | 1963 | queue->rx_irq = err; |
d634bf2c WL |
1964 | |
1965 | return 0; | |
1966 | ||
1967 | bind_rx_fail: | |
2688fcb7 AB |
1968 | unbind_from_irqhandler(queue->tx_irq, queue); |
1969 | queue->tx_irq = 0; | |
d634bf2c | 1970 | bind_tx_fail: |
2688fcb7 AB |
1971 | xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn); |
1972 | queue->rx_evtchn = 0; | |
d634bf2c | 1973 | alloc_rx_evtchn_fail: |
2688fcb7 AB |
1974 | xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); |
1975 | queue->tx_evtchn = 0; | |
d634bf2c WL |
1976 | fail: |
1977 | return err; | |
1978 | } | |
1979 | ||
2688fcb7 AB |
1980 | static int setup_netfront(struct xenbus_device *dev, |
1981 | struct netfront_queue *queue, unsigned int feature_split_evtchn) | |
0d160211 JF |
1982 | { |
1983 | struct xen_netif_tx_sring *txs; | |
46e20d43 | 1984 | struct xen_netif_rx_sring *rxs; |
0d160211 | 1985 | int err; |
0d160211 | 1986 | |
145daab2 JG |
1987 | queue->tx_ring_ref = INVALID_GRANT_REF; |
1988 | queue->rx_ring_ref = INVALID_GRANT_REF; | |
2688fcb7 AB |
1989 | queue->rx.sring = NULL; |
1990 | queue->tx.sring = NULL; | |
0d160211 | 1991 | |
46e20d43 JG |
1992 | err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH, (void **)&txs, |
1993 | 1, &queue->tx_ring_ref); | |
1994 | if (err) | |
0d160211 | 1995 | goto fail; |
0d160211 | 1996 | |
46e20d43 | 1997 | XEN_FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE); |
0d160211 | 1998 | |
46e20d43 JG |
1999 | err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH, (void **)&rxs, |
2000 | 1, &queue->rx_ring_ref); | |
2001 | if (err) | |
66e3531b | 2002 | goto fail; |
0d160211 | 2003 | |
46e20d43 | 2004 | XEN_FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE); |
0d160211 | 2005 | |
d634bf2c | 2006 | if (feature_split_evtchn) |
2688fcb7 | 2007 | err = setup_netfront_split(queue); |
d634bf2c WL |
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 | */ | |
e93fac3b | 2012 | if (!feature_split_evtchn || err) |
2688fcb7 | 2013 | err = setup_netfront_single(queue); |
d634bf2c | 2014 | |
0d160211 | 2015 | if (err) |
66e3531b | 2016 | goto fail; |
0d160211 | 2017 | |
0d160211 JF |
2018 | return 0; |
2019 | ||
66e3531b | 2020 | fail: |
46e20d43 JG |
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 | ||
0d160211 JF |
2024 | return err; |
2025 | } | |
2026 | ||
2688fcb7 AB |
2027 | /* Queue-specific initialisation |
2028 | * This used to be done in xennet_create_dev() but must now | |
2029 | * be run per-queue. | |
2030 | */ | |
2031 | static int xennet_init_queue(struct netfront_queue *queue) | |
2032 | { | |
2033 | unsigned short i; | |
2034 | int err = 0; | |
21f2706b | 2035 | char *devid; |
2688fcb7 AB |
2036 | |
2037 | spin_lock_init(&queue->tx_lock); | |
2038 | spin_lock_init(&queue->rx_lock); | |
b27d4795 | 2039 | spin_lock_init(&queue->rx_cons_lock); |
2688fcb7 | 2040 | |
e99e88a9 | 2041 | timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0); |
2688fcb7 | 2042 | |
21f2706b XL |
2043 | devid = strrchr(queue->info->xbdev->nodename, '/') + 1; |
2044 | snprintf(queue->name, sizeof(queue->name), "vif%s-q%u", | |
2045 | devid, queue->id); | |
8b715010 | 2046 | |
21631d2d | 2047 | /* Initialise tx_skb_freelist as a free chain containing every entry. */ |
2688fcb7 | 2048 | queue->tx_skb_freelist = 0; |
a884daa6 | 2049 | queue->tx_pend_queue = TX_LINK_NONE; |
2688fcb7 | 2050 | for (i = 0; i < NET_TX_RING_SIZE; i++) { |
21631d2d | 2051 | queue->tx_link[i] = i + 1; |
145daab2 | 2052 | queue->grant_tx_ref[i] = INVALID_GRANT_REF; |
2688fcb7 AB |
2053 | queue->grant_tx_page[i] = NULL; |
2054 | } | |
21631d2d | 2055 | queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE; |
2688fcb7 AB |
2056 | |
2057 | /* Clear out rx_skbs */ | |
2058 | for (i = 0; i < NET_RX_RING_SIZE; i++) { | |
2059 | queue->rx_skbs[i] = NULL; | |
145daab2 | 2060 | queue->grant_rx_ref[i] = INVALID_GRANT_REF; |
2688fcb7 AB |
2061 | } |
2062 | ||
2063 | /* A grant for every tx ring slot */ | |
1f3c2eba | 2064 | if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, |
2688fcb7 AB |
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 */ | |
1f3c2eba | 2072 | if (gnttab_alloc_grant_references(NET_RX_RING_SIZE, |
2688fcb7 AB |
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 | ||
2688fcb7 AB |
2079 | return 0; |
2080 | ||
2081 | exit_free_tx: | |
2082 | gnttab_free_grant_references(queue->gref_tx_head); | |
2083 | exit: | |
2084 | return err; | |
2085 | } | |
2086 | ||
50ee6061 AB |
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 | ||
6c5aa6fc DK |
2169 | |
2170 | ||
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, | |
b02e5a0e | 2192 | queue->id, 0); |
6c5aa6fc DK |
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 | ||
ce58725f | 2214 | static int xennet_create_queues(struct netfront_info *info, |
ca88ea12 | 2215 | unsigned int *num_queues) |
ce58725f DV |
2216 | { |
2217 | unsigned int i; | |
2218 | int ret; | |
2219 | ||
ca88ea12 | 2220 | info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue), |
ce58725f DV |
2221 | GFP_KERNEL); |
2222 | if (!info->queues) | |
2223 | return -ENOMEM; | |
2224 | ||
ca88ea12 | 2225 | for (i = 0; i < *num_queues; i++) { |
ce58725f DV |
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) { | |
f599c64f | 2233 | dev_warn(&info->xbdev->dev, |
69cb8524 | 2234 | "only created %d queues\n", i); |
ca88ea12 | 2235 | *num_queues = i; |
ce58725f DV |
2236 | break; |
2237 | } | |
2238 | ||
6c5aa6fc DK |
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 | ||
b48b89f9 | 2247 | netif_napi_add(queue->info->netdev, &queue->napi, xennet_poll); |
ce58725f DV |
2248 | if (netif_running(info->netdev)) |
2249 | napi_enable(&queue->napi); | |
2250 | } | |
2251 | ||
ca88ea12 | 2252 | netif_set_real_num_tx_queues(info->netdev, *num_queues); |
ce58725f | 2253 | |
ca88ea12 | 2254 | if (*num_queues == 0) { |
f599c64f | 2255 | dev_err(&info->xbdev->dev, "no queues\n"); |
ce58725f DV |
2256 | return -EINVAL; |
2257 | } | |
2258 | return 0; | |
2259 | } | |
2260 | ||
0d160211 | 2261 | /* Common code used when first setting up, and when resuming. */ |
f502bf2b | 2262 | static int talk_to_netback(struct xenbus_device *dev, |
0d160211 JF |
2263 | struct netfront_info *info) |
2264 | { | |
2265 | const char *message; | |
2266 | struct xenbus_transaction xbt; | |
2267 | int err; | |
2688fcb7 AB |
2268 | unsigned int feature_split_evtchn; |
2269 | unsigned int i = 0; | |
50ee6061 | 2270 | unsigned int max_queues = 0; |
2688fcb7 AB |
2271 | struct netfront_queue *queue = NULL; |
2272 | unsigned int num_queues = 1; | |
93772114 | 2273 | u8 addr[ETH_ALEN]; |
0d160211 | 2274 | |
2688fcb7 AB |
2275 | info->netdev->irq = 0; |
2276 | ||
4491001c RPM |
2277 | /* Check if backend is trusted. */ |
2278 | info->bounce = !xennet_trusted || | |
2279 | !xenbus_read_unsigned(dev->nodename, "trusted", 1); | |
2280 | ||
50ee6061 | 2281 | /* Check if backend supports multiple queues */ |
2890ea5c JG |
2282 | max_queues = xenbus_read_unsigned(info->xbdev->otherend, |
2283 | "multi-queue-max-queues", 1); | |
50ee6061 AB |
2284 | num_queues = min(max_queues, xennet_max_queues); |
2285 | ||
2688fcb7 | 2286 | /* Check feature-split-event-channels */ |
2890ea5c JG |
2287 | feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend, |
2288 | "feature-split-event-channels", 0); | |
2688fcb7 AB |
2289 | |
2290 | /* Read mac addr. */ | |
93772114 | 2291 | err = xen_net_read_mac(dev, addr); |
2688fcb7 AB |
2292 | if (err) { |
2293 | xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename); | |
cb257783 | 2294 | goto out_unlocked; |
2688fcb7 | 2295 | } |
93772114 | 2296 | eth_hw_addr_set(info->netdev, addr); |
2688fcb7 | 2297 | |
6c5aa6fc DK |
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 | ||
f599c64f | 2309 | rtnl_lock(); |
ce58725f DV |
2310 | if (info->queues) |
2311 | xennet_destroy_queues(info); | |
2312 | ||
a884daa6 JG |
2313 | /* For the case of a reconnect reset the "broken" indicator. */ |
2314 | info->broken = false; | |
2315 | ||
ca88ea12 | 2316 | err = xennet_create_queues(info, &num_queues); |
e2e004ac RL |
2317 | if (err < 0) { |
2318 | xenbus_dev_fatal(dev, err, "creating queues"); | |
2319 | kfree(info->queues); | |
2320 | info->queues = NULL; | |
2321 | goto out; | |
2322 | } | |
f599c64f | 2323 | rtnl_unlock(); |
2688fcb7 AB |
2324 | |
2325 | /* Create shared ring, alloc event channel -- for each queue */ | |
2326 | for (i = 0; i < num_queues; ++i) { | |
2327 | queue = &info->queues[i]; | |
2688fcb7 | 2328 | err = setup_netfront(dev, queue, feature_split_evtchn); |
e2e004ac RL |
2329 | if (err) |
2330 | goto destroy_ring; | |
2688fcb7 | 2331 | } |
0d160211 JF |
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 | ||
812494d9 | 2340 | if (xenbus_exists(XBT_NIL, |
2341 | info->xbdev->otherend, "multi-queue-max-queues")) { | |
50ee6061 | 2342 | /* Write the number of queues */ |
812494d9 | 2343 | err = xenbus_printf(xbt, dev->nodename, |
2344 | "multi-queue-num-queues", "%u", num_queues); | |
d634bf2c | 2345 | if (err) { |
50ee6061 AB |
2346 | message = "writing multi-queue-num-queues"; |
2347 | goto abort_transaction_no_dev_fatal; | |
d634bf2c | 2348 | } |
812494d9 | 2349 | } |
50ee6061 | 2350 | |
812494d9 | 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 { | |
50ee6061 AB |
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; | |
d634bf2c | 2362 | } |
0d160211 JF |
2363 | } |
2364 | ||
50ee6061 | 2365 | /* The remaining keys are not queue-specific */ |
0d160211 JF |
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 | ||
2c0057de PD |
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 | ||
0d160211 JF |
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: | |
0d160211 | 2415 | xenbus_dev_fatal(dev, err, "%s", message); |
50ee6061 AB |
2416 | abort_transaction_no_dev_fatal: |
2417 | xenbus_transaction_end(xbt, 1); | |
0d160211 JF |
2418 | destroy_ring: |
2419 | xennet_disconnect_backend(info); | |
f599c64f | 2420 | rtnl_lock(); |
e2e004ac | 2421 | xennet_destroy_queues(info); |
0d160211 | 2422 | out: |
f599c64f | 2423 | rtnl_unlock(); |
cb257783 | 2424 | out_unlocked: |
d86b5672 | 2425 | device_unregister(&dev->dev); |
0d160211 JF |
2426 | return err; |
2427 | } | |
2428 | ||
0d160211 JF |
2429 | static int xennet_connect(struct net_device *dev) |
2430 | { | |
2431 | struct netfront_info *np = netdev_priv(dev); | |
2688fcb7 | 2432 | unsigned int num_queues = 0; |
a5b5dc3c | 2433 | int err; |
2688fcb7 AB |
2434 | unsigned int j = 0; |
2435 | struct netfront_queue *queue = NULL; | |
0d160211 | 2436 | |
2890ea5c | 2437 | if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) { |
0d160211 | 2438 | dev_info(&dev->dev, |
898eb71c | 2439 | "backend does not support copying receive path\n"); |
0d160211 JF |
2440 | return -ENODEV; |
2441 | } | |
2442 | ||
f502bf2b | 2443 | err = talk_to_netback(np->xbdev, np); |
0d160211 JF |
2444 | if (err) |
2445 | return err; | |
6c5aa6fc DK |
2446 | if (np->netback_has_xdp_headroom) |
2447 | pr_info("backend supports XDP headroom\n"); | |
4491001c RPM |
2448 | if (np->bounce) |
2449 | dev_info(&np->xbdev->dev, | |
2450 | "bouncing transmitted data to zeroed pages\n"); | |
0d160211 | 2451 | |
2688fcb7 AB |
2452 | /* talk_to_netback() sets the correct number of queues */ |
2453 | num_queues = dev->real_num_tx_queues; | |
2454 | ||
f599c64f RL |
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 | ||
45c8184c RL |
2464 | rtnl_lock(); |
2465 | netdev_update_features(dev); | |
2466 | rtnl_unlock(); | |
2467 | ||
0d160211 | 2468 | /* |
a5b5dc3c | 2469 | * All public and private state should now be sane. Get |
0d160211 JF |
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 | */ | |
042b2046 DZ |
2474 | netif_tx_lock_bh(np->netdev); |
2475 | netif_device_attach(np->netdev); | |
2476 | netif_tx_unlock_bh(np->netdev); | |
2477 | ||
0d160211 | 2478 | netif_carrier_on(np->netdev); |
2688fcb7 AB |
2479 | for (j = 0; j < num_queues; ++j) { |
2480 | queue = &np->queues[j]; | |
f50b4076 | 2481 | |
2688fcb7 AB |
2482 | notify_remote_via_irq(queue->tx_irq); |
2483 | if (queue->tx_irq != queue->rx_irq) | |
2484 | notify_remote_via_irq(queue->rx_irq); | |
2688fcb7 | 2485 | |
f50b4076 DV |
2486 | spin_lock_bh(&queue->rx_lock); |
2487 | xennet_alloc_rx_buffers(queue); | |
2688fcb7 AB |
2488 | spin_unlock_bh(&queue->rx_lock); |
2489 | } | |
0d160211 JF |
2490 | |
2491 | return 0; | |
2492 | } | |
2493 | ||
80708602 | 2494 | /* |
0d160211 JF |
2495 | * Callback received when the backend's state changes. |
2496 | */ | |
f502bf2b | 2497 | static void netback_changed(struct xenbus_device *dev, |
0d160211 JF |
2498 | enum xenbus_state backend_state) |
2499 | { | |
1b713e00 | 2500 | struct netfront_info *np = dev_get_drvdata(&dev->dev); |
0d160211 JF |
2501 | struct net_device *netdev = np->netdev; |
2502 | ||
2503 | dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state)); | |
2504 | ||
8edfe2e9 JG |
2505 | wake_up_all(&module_wq); |
2506 | ||
0d160211 JF |
2507 | switch (backend_state) { |
2508 | case XenbusStateInitialising: | |
2509 | case XenbusStateInitialised: | |
b78c9512 NI |
2510 | case XenbusStateReconfiguring: |
2511 | case XenbusStateReconfigured: | |
0d160211 | 2512 | case XenbusStateUnknown: |
0d160211 JF |
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); | |
08e34eb1 LE |
2521 | break; |
2522 | ||
2523 | case XenbusStateConnected: | |
ee89bab1 | 2524 | netdev_notify_peers(netdev); |
0d160211 JF |
2525 | break; |
2526 | ||
bce3ea81 DV |
2527 | case XenbusStateClosed: |
2528 | if (dev->state == XenbusStateClosed) | |
2529 | break; | |
df561f66 | 2530 | fallthrough; /* Missed the backend's CLOSING state */ |
0d160211 JF |
2531 | case XenbusStateClosing: |
2532 | xenbus_frontend_closed(dev); | |
2533 | break; | |
2534 | } | |
2535 | } | |
2536 | ||
e0ce4af9 IC |
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 | ||
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 | ||
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++) | |
2688fcb7 | 2564 | data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset)); |
e0ce4af9 IC |
2565 | } |
2566 | ||
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 | ||
0fc0b732 | 2580 | static const struct ethtool_ops xennet_ethtool_ops = |
0d160211 | 2581 | { |
0d160211 | 2582 | .get_link = ethtool_op_get_link, |
e0ce4af9 IC |
2583 | |
2584 | .get_sset_count = xennet_get_sset_count, | |
2585 | .get_ethtool_stats = xennet_get_ethtool_stats, | |
2586 | .get_strings = xennet_get_strings, | |
91ffb9d3 | 2587 | .get_ts_info = ethtool_op_get_ts_info, |
0d160211 JF |
2588 | }; |
2589 | ||
2590 | #ifdef CONFIG_SYSFS | |
1f3c2eba DV |
2591 | static ssize_t show_rxbuf(struct device *dev, |
2592 | struct device_attribute *attr, char *buf) | |
0d160211 | 2593 | { |
1f3c2eba | 2594 | return sprintf(buf, "%lu\n", NET_RX_RING_SIZE); |
0d160211 JF |
2595 | } |
2596 | ||
1f3c2eba DV |
2597 | static ssize_t store_rxbuf(struct device *dev, |
2598 | struct device_attribute *attr, | |
2599 | const char *buf, size_t len) | |
0d160211 | 2600 | { |
0d160211 | 2601 | char *endp; |
0d160211 JF |
2602 | |
2603 | if (!capable(CAP_NET_ADMIN)) | |
2604 | return -EPERM; | |
2605 | ||
8ed7ec13 | 2606 | simple_strtoul(buf, &endp, 0); |
0d160211 JF |
2607 | if (endp == buf) |
2608 | return -EBADMSG; | |
2609 | ||
1f3c2eba | 2610 | /* rxbuf_min and rxbuf_max are no longer configurable. */ |
0d160211 | 2611 | |
0d160211 JF |
2612 | return len; |
2613 | } | |
2614 | ||
d61e4038 JP |
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); | |
0d160211 | 2618 | |
27b917e5 TI |
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 | }; | |
0d160211 | 2625 | |
27b917e5 TI |
2626 | static const struct attribute_group xennet_dev_group = { |
2627 | .attrs = xennet_dev_attrs | |
2628 | }; | |
0d160211 JF |
2629 | #endif /* CONFIG_SYSFS */ |
2630 | ||
c2c63310 | 2631 | static void xennet_bus_close(struct xenbus_device *dev) |
0d160211 | 2632 | { |
c2c63310 | 2633 | int ret; |
0d160211 | 2634 | |
c2c63310 AR |
2635 | if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed) |
2636 | return; | |
2637 | do { | |
5b5971df | 2638 | xenbus_switch_state(dev, XenbusStateClosing); |
c2c63310 AR |
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; | |
5b5971df | 2651 | |
c2c63310 | 2652 | do { |
5b5971df | 2653 | xenbus_switch_state(dev, XenbusStateClosed); |
c2c63310 AR |
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 | ||
7cffcade | 2663 | static void xennet_remove(struct xenbus_device *dev) |
c2c63310 AR |
2664 | { |
2665 | struct netfront_info *info = dev_get_drvdata(&dev->dev); | |
5b5971df | 2666 | |
c2c63310 | 2667 | xennet_bus_close(dev); |
0d160211 JF |
2668 | xennet_disconnect_backend(info); |
2669 | ||
f599c64f RL |
2670 | if (info->netdev->reg_state == NETREG_REGISTERED) |
2671 | unregister_netdev(info->netdev); | |
6bc96d04 | 2672 | |
f599c64f RL |
2673 | if (info->queues) { |
2674 | rtnl_lock(); | |
9a873c71 | 2675 | xennet_destroy_queues(info); |
f599c64f RL |
2676 | rtnl_unlock(); |
2677 | } | |
900e1833 | 2678 | xennet_free_netdev(info->netdev); |
0d160211 JF |
2679 | } |
2680 | ||
95afae48 DV |
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, | |
0d160211 | 2688 | .probe = netfront_probe, |
8e0e46bb | 2689 | .remove = xennet_remove, |
0d160211 | 2690 | .resume = netfront_resume, |
f502bf2b | 2691 | .otherend_changed = netback_changed, |
95afae48 | 2692 | }; |
0d160211 JF |
2693 | |
2694 | static int __init netif_init(void) | |
2695 | { | |
6e833587 | 2696 | if (!xen_domain()) |
0d160211 JF |
2697 | return -ENODEV; |
2698 | ||
51c71a3b | 2699 | if (!xen_has_pv_nic_devices()) |
b9136d20 IM |
2700 | return -ENODEV; |
2701 | ||
383eda32 | 2702 | pr_info("Initialising Xen virtual ethernet driver\n"); |
0d160211 | 2703 | |
034702a6 | 2704 | /* Allow as many queues as there are CPUs inut max. 8 if user has not |
32a84405 WL |
2705 | * specified a value. |
2706 | */ | |
2707 | if (xennet_max_queues == 0) | |
034702a6 JG |
2708 | xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT, |
2709 | num_online_cpus()); | |
50ee6061 | 2710 | |
ffb78a26 | 2711 | return xenbus_register_frontend(&netfront_driver); |
0d160211 JF |
2712 | } |
2713 | module_init(netif_init); | |
2714 | ||
2715 | ||
2716 | static void __exit netif_exit(void) | |
2717 | { | |
ffb78a26 | 2718 | xenbus_unregister_driver(&netfront_driver); |
0d160211 JF |
2719 | } |
2720 | module_exit(netif_exit); | |
2721 | ||
2722 | MODULE_DESCRIPTION("Xen virtual network device frontend"); | |
2723 | MODULE_LICENSE("GPL"); | |
d2f0c52b | 2724 | MODULE_ALIAS("xen:vif"); |
4f93f09b | 2725 | MODULE_ALIAS("xennet"); |