]>
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 DK |
47 | #include <linux/bpf.h> |
48 | #include <net/page_pool.h> | |
49 | #include <linux/bpf_trace.h> | |
0d160211 | 50 | |
1ccbf534 | 51 | #include <xen/xen.h> |
0d160211 JF |
52 | #include <xen/xenbus.h> |
53 | #include <xen/events.h> | |
54 | #include <xen/page.h> | |
b9136d20 | 55 | #include <xen/platform_pci.h> |
0d160211 JF |
56 | #include <xen/grant_table.h> |
57 | ||
58 | #include <xen/interface/io/netif.h> | |
59 | #include <xen/interface/memory.h> | |
60 | #include <xen/interface/grant_table.h> | |
61 | ||
50ee6061 | 62 | /* Module parameters */ |
034702a6 | 63 | #define MAX_QUEUES_DEFAULT 8 |
50ee6061 AB |
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 | ||
5c132c88 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 | ||
85 | #define GRANT_INVALID_REF 0 | |
86 | ||
30c5d7f0 JG |
87 | #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE) |
88 | #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE) | |
1f3c2eba DV |
89 | |
90 | /* Minimum number of Rx slots (includes slot for GSO metadata). */ | |
91 | #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1) | |
0d160211 | 92 | |
2688fcb7 AB |
93 | /* Queue name is interface name with "-qNNN" appended */ |
94 | #define QUEUE_NAME_SIZE (IFNAMSIZ + 6) | |
95 | ||
96 | /* IRQ name is queue name with "-tx" or "-rx" appended */ | |
97 | #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3) | |
98 | ||
8edfe2e9 | 99 | static DECLARE_WAIT_QUEUE_HEAD(module_wq); |
5b5971df | 100 | |
e00f85be | 101 | struct netfront_stats { |
900e1833 DV |
102 | u64 packets; |
103 | u64 bytes; | |
e00f85be | 104 | struct u64_stats_sync syncp; |
105 | }; | |
106 | ||
2688fcb7 AB |
107 | struct netfront_info; |
108 | ||
109 | struct netfront_queue { | |
110 | unsigned int id; /* Queue ID, 0-based */ | |
111 | char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */ | |
112 | struct netfront_info *info; | |
0d160211 | 113 | |
6c5aa6fc DK |
114 | struct bpf_prog __rcu *xdp_prog; |
115 | ||
bea3348e | 116 | struct napi_struct napi; |
0d160211 | 117 | |
d634bf2c WL |
118 | /* Split event channels support, tx_* == rx_* when using |
119 | * single event channel. | |
120 | */ | |
121 | unsigned int tx_evtchn, rx_evtchn; | |
122 | unsigned int tx_irq, rx_irq; | |
123 | /* Only used when split event channels support is enabled */ | |
2688fcb7 AB |
124 | char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */ |
125 | char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */ | |
0d160211 | 126 | |
84284d3c JF |
127 | spinlock_t tx_lock; |
128 | struct xen_netif_tx_front_ring tx; | |
129 | int tx_ring_ref; | |
0d160211 JF |
130 | |
131 | /* | |
132 | * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries | |
21631d2d | 133 | * are linked from tx_skb_freelist through tx_link. |
0d160211 | 134 | */ |
21631d2d JG |
135 | struct sk_buff *tx_skbs[NET_TX_RING_SIZE]; |
136 | unsigned short tx_link[NET_TX_RING_SIZE]; | |
137 | #define TX_LINK_NONE 0xffff | |
a884daa6 | 138 | #define TX_PENDING 0xfffe |
0d160211 JF |
139 | grant_ref_t gref_tx_head; |
140 | grant_ref_t grant_tx_ref[NET_TX_RING_SIZE]; | |
cefe0078 | 141 | struct page *grant_tx_page[NET_TX_RING_SIZE]; |
0d160211 | 142 | unsigned tx_skb_freelist; |
a884daa6 | 143 | unsigned int tx_pend_queue; |
0d160211 | 144 | |
84284d3c JF |
145 | spinlock_t rx_lock ____cacheline_aligned_in_smp; |
146 | struct xen_netif_rx_front_ring rx; | |
147 | int rx_ring_ref; | |
148 | ||
84284d3c JF |
149 | struct timer_list rx_refill_timer; |
150 | ||
0d160211 JF |
151 | struct sk_buff *rx_skbs[NET_RX_RING_SIZE]; |
152 | grant_ref_t gref_rx_head; | |
153 | grant_ref_t grant_rx_ref[NET_RX_RING_SIZE]; | |
6c5aa6fc | 154 | |
900e0ac2 JG |
155 | unsigned int rx_rsp_unconsumed; |
156 | spinlock_t rx_cons_lock; | |
157 | ||
6c5aa6fc DK |
158 | struct page_pool *page_pool; |
159 | struct xdp_rxq_info xdp_rxq; | |
2688fcb7 AB |
160 | }; |
161 | ||
162 | struct netfront_info { | |
163 | struct list_head list; | |
164 | struct net_device *netdev; | |
165 | ||
166 | struct xenbus_device *xbdev; | |
167 | ||
168 | /* Multi-queue support */ | |
169 | struct netfront_queue *queues; | |
e0ce4af9 IC |
170 | |
171 | /* Statistics */ | |
900e1833 DV |
172 | struct netfront_stats __percpu *rx_stats; |
173 | struct netfront_stats __percpu *tx_stats; | |
e00f85be | 174 | |
6c5aa6fc DK |
175 | /* XDP state */ |
176 | bool netback_has_xdp_headroom; | |
177 | bool netfront_xdp_enabled; | |
178 | ||
a884daa6 JG |
179 | /* Is device behaving sane? */ |
180 | bool broken; | |
181 | ||
5c132c88 RPM |
182 | /* Should skbs be bounced into a zeroed buffer? */ |
183 | bool bounce; | |
184 | ||
2688fcb7 | 185 | atomic_t rx_gso_checksum_fixup; |
0d160211 JF |
186 | }; |
187 | ||
188 | struct netfront_rx_info { | |
189 | struct xen_netif_rx_response rx; | |
190 | struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; | |
191 | }; | |
192 | ||
193 | /* | |
194 | * Access macros for acquiring freeing slots in tx_skbs[]. | |
195 | */ | |
196 | ||
21631d2d JG |
197 | static void add_id_to_list(unsigned *head, unsigned short *list, |
198 | unsigned short id) | |
0d160211 | 199 | { |
21631d2d | 200 | list[id] = *head; |
0d160211 JF |
201 | *head = id; |
202 | } | |
203 | ||
21631d2d | 204 | static unsigned short get_id_from_list(unsigned *head, unsigned short *list) |
0d160211 JF |
205 | { |
206 | unsigned int id = *head; | |
21631d2d JG |
207 | |
208 | if (id != TX_LINK_NONE) { | |
209 | *head = list[id]; | |
210 | list[id] = TX_LINK_NONE; | |
211 | } | |
0d160211 JF |
212 | return id; |
213 | } | |
214 | ||
215 | static int xennet_rxidx(RING_IDX idx) | |
216 | { | |
217 | return idx & (NET_RX_RING_SIZE - 1); | |
218 | } | |
219 | ||
2688fcb7 | 220 | static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue, |
0d160211 JF |
221 | RING_IDX ri) |
222 | { | |
223 | int i = xennet_rxidx(ri); | |
2688fcb7 AB |
224 | struct sk_buff *skb = queue->rx_skbs[i]; |
225 | queue->rx_skbs[i] = NULL; | |
0d160211 JF |
226 | return skb; |
227 | } | |
228 | ||
2688fcb7 | 229 | static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue, |
0d160211 JF |
230 | RING_IDX ri) |
231 | { | |
232 | int i = xennet_rxidx(ri); | |
2688fcb7 AB |
233 | grant_ref_t ref = queue->grant_rx_ref[i]; |
234 | queue->grant_rx_ref[i] = GRANT_INVALID_REF; | |
0d160211 JF |
235 | return ref; |
236 | } | |
237 | ||
238 | #ifdef CONFIG_SYSFS | |
27b917e5 | 239 | static const struct attribute_group xennet_dev_group; |
0d160211 JF |
240 | #endif |
241 | ||
3ad9b358 | 242 | static bool xennet_can_sg(struct net_device *dev) |
0d160211 | 243 | { |
3ad9b358 | 244 | return dev->features & NETIF_F_SG; |
0d160211 JF |
245 | } |
246 | ||
247 | ||
e99e88a9 | 248 | static void rx_refill_timeout(struct timer_list *t) |
0d160211 | 249 | { |
e99e88a9 | 250 | struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer); |
2688fcb7 | 251 | napi_schedule(&queue->napi); |
0d160211 JF |
252 | } |
253 | ||
2688fcb7 | 254 | static int netfront_tx_slot_available(struct netfront_queue *queue) |
0d160211 | 255 | { |
2688fcb7 | 256 | return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) < |
57f230ab | 257 | (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1); |
0d160211 JF |
258 | } |
259 | ||
2688fcb7 | 260 | static void xennet_maybe_wake_tx(struct netfront_queue *queue) |
0d160211 | 261 | { |
2688fcb7 AB |
262 | struct net_device *dev = queue->info->netdev; |
263 | struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id); | |
0d160211 | 264 | |
2688fcb7 AB |
265 | if (unlikely(netif_tx_queue_stopped(dev_queue)) && |
266 | netfront_tx_slot_available(queue) && | |
0d160211 | 267 | likely(netif_running(dev))) |
2688fcb7 | 268 | netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id)); |
0d160211 JF |
269 | } |
270 | ||
1f3c2eba DV |
271 | |
272 | static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue) | |
0d160211 | 273 | { |
0d160211 JF |
274 | struct sk_buff *skb; |
275 | struct page *page; | |
0d160211 | 276 | |
1f3c2eba DV |
277 | skb = __netdev_alloc_skb(queue->info->netdev, |
278 | RX_COPY_THRESHOLD + NET_IP_ALIGN, | |
279 | GFP_ATOMIC | __GFP_NOWARN); | |
280 | if (unlikely(!skb)) | |
281 | return NULL; | |
0d160211 | 282 | |
6240a8a6 RPM |
283 | page = page_pool_alloc_pages(queue->page_pool, |
284 | GFP_ATOMIC | __GFP_NOWARN | __GFP_ZERO); | |
6c5aa6fc | 285 | if (unlikely(!page)) { |
1f3c2eba DV |
286 | kfree_skb(skb); |
287 | return NULL; | |
0d160211 | 288 | } |
1f3c2eba DV |
289 | skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE); |
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 | ||
900e0ac2 | 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; |
900e0ac2 | 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 | |
900e0ac2 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; | |
b602c2e0 JG |
435 | if (unlikely(!gnttab_end_foreign_access_ref( |
436 | queue->grant_tx_ref[id], GNTMAP_readonly))) { | |
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 AB |
442 | &queue->gref_tx_head, queue->grant_tx_ref[id]); |
443 | queue->grant_tx_ref[id] = GRANT_INVALID_REF; | |
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 | |
900e0ac2 | 456 | return work_done; |
a884daa6 JG |
457 | |
458 | err: | |
459 | queue->info->broken = true; | |
460 | dev_alert(dev, "Disabled for further use\n"); | |
900e0ac2 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 | ||
5c132c88 RPM |
678 | struct sk_buff *bounce_skb(const struct sk_buff *skb) |
679 | { | |
680 | unsigned int headerlen = skb_headroom(skb); | |
681 | /* Align size to allocate full pages and avoid contiguous data leaks */ | |
682 | unsigned int size = ALIGN(skb_end_offset(skb) + skb->data_len, | |
683 | XEN_PAGE_SIZE); | |
684 | struct sk_buff *n = alloc_skb(size, GFP_ATOMIC | __GFP_ZERO); | |
685 | ||
686 | if (!n) | |
687 | return NULL; | |
688 | ||
689 | if (!IS_ALIGNED((uintptr_t)n->head, XEN_PAGE_SIZE)) { | |
690 | WARN_ONCE(1, "misaligned skb allocated\n"); | |
691 | kfree_skb(n); | |
692 | return NULL; | |
693 | } | |
694 | ||
695 | /* Set the data pointer */ | |
696 | skb_reserve(n, headerlen); | |
697 | /* Set the tail pointer and length */ | |
698 | skb_put(n, skb->len); | |
699 | ||
700 | BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); | |
701 | ||
702 | skb_copy_header(n, skb); | |
703 | return n; | |
704 | } | |
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. | |
5c132c88 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 | */ |
5c132c88 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); | |
2688fcb7 AB |
871 | unsigned int num_queues = dev->real_num_tx_queues; |
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 | ||
1d0ea124 MMG |
882 | static void xennet_destroy_queues(struct netfront_info *info) |
883 | { | |
884 | unsigned int i; | |
885 | ||
886 | for (i = 0; i < info->netdev->real_num_tx_queues; i++) { | |
887 | struct netfront_queue *queue = &info->queues[i]; | |
888 | ||
889 | if (netif_running(info->netdev)) | |
890 | napi_disable(&queue->napi); | |
891 | netif_napi_del(&queue->napi); | |
892 | } | |
893 | ||
894 | kfree(info->queues); | |
895 | info->queues = NULL; | |
896 | } | |
897 | ||
898 | static void xennet_uninit(struct net_device *dev) | |
899 | { | |
900 | struct netfront_info *np = netdev_priv(dev); | |
901 | xennet_destroy_queues(np); | |
902 | } | |
903 | ||
900e0ac2 JG |
904 | static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val) |
905 | { | |
906 | unsigned long flags; | |
907 | ||
908 | spin_lock_irqsave(&queue->rx_cons_lock, flags); | |
909 | queue->rx.rsp_cons = val; | |
910 | queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx); | |
911 | spin_unlock_irqrestore(&queue->rx_cons_lock, flags); | |
912 | } | |
913 | ||
2688fcb7 | 914 | static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb, |
0d160211 JF |
915 | grant_ref_t ref) |
916 | { | |
2688fcb7 AB |
917 | int new = xennet_rxidx(queue->rx.req_prod_pvt); |
918 | ||
919 | BUG_ON(queue->rx_skbs[new]); | |
920 | queue->rx_skbs[new] = skb; | |
921 | queue->grant_rx_ref[new] = ref; | |
922 | RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new; | |
923 | RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref; | |
924 | queue->rx.req_prod_pvt++; | |
0d160211 JF |
925 | } |
926 | ||
2688fcb7 | 927 | static int xennet_get_extras(struct netfront_queue *queue, |
0d160211 JF |
928 | struct xen_netif_extra_info *extras, |
929 | RING_IDX rp) | |
930 | ||
931 | { | |
8446066b | 932 | struct xen_netif_extra_info extra; |
2688fcb7 AB |
933 | struct device *dev = &queue->info->netdev->dev; |
934 | RING_IDX cons = queue->rx.rsp_cons; | |
0d160211 JF |
935 | int err = 0; |
936 | ||
937 | do { | |
938 | struct sk_buff *skb; | |
939 | grant_ref_t ref; | |
940 | ||
941 | if (unlikely(cons + 1 == rp)) { | |
942 | if (net_ratelimit()) | |
943 | dev_warn(dev, "Missing extra info\n"); | |
944 | err = -EBADR; | |
945 | break; | |
946 | } | |
947 | ||
8446066b | 948 | RING_COPY_RESPONSE(&queue->rx, ++cons, &extra); |
0d160211 | 949 | |
8446066b JG |
950 | if (unlikely(!extra.type || |
951 | extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) { | |
0d160211 JF |
952 | if (net_ratelimit()) |
953 | dev_warn(dev, "Invalid extra type: %d\n", | |
8446066b | 954 | extra.type); |
0d160211 JF |
955 | err = -EINVAL; |
956 | } else { | |
8446066b | 957 | extras[extra.type - 1] = extra; |
0d160211 JF |
958 | } |
959 | ||
2688fcb7 AB |
960 | skb = xennet_get_rx_skb(queue, cons); |
961 | ref = xennet_get_rx_ref(queue, cons); | |
962 | xennet_move_rx_slot(queue, skb, ref); | |
8446066b | 963 | } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); |
0d160211 | 964 | |
900e0ac2 | 965 | xennet_set_rx_rsp_cons(queue, cons); |
0d160211 JF |
966 | return err; |
967 | } | |
968 | ||
6c5aa6fc DK |
969 | static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata, |
970 | struct xen_netif_rx_response *rx, struct bpf_prog *prog, | |
971 | struct xdp_buff *xdp, bool *need_xdp_flush) | |
972 | { | |
973 | struct xdp_frame *xdpf; | |
974 | u32 len = rx->status; | |
e44f65fd | 975 | u32 act; |
6c5aa6fc DK |
976 | int err; |
977 | ||
43b5169d LB |
978 | xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM, |
979 | &queue->xdp_rxq); | |
be9df4af LB |
980 | xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM, |
981 | len, false); | |
6c5aa6fc DK |
982 | |
983 | act = bpf_prog_run_xdp(prog, xdp); | |
984 | switch (act) { | |
985 | case XDP_TX: | |
986 | get_page(pdata); | |
987 | xdpf = xdp_convert_buff_to_frame(xdp); | |
988 | err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0); | |
fdc13979 LB |
989 | if (unlikely(!err)) |
990 | xdp_return_frame_rx_napi(xdpf); | |
991 | else if (unlikely(err < 0)) | |
6c5aa6fc DK |
992 | trace_xdp_exception(queue->info->netdev, prog, act); |
993 | break; | |
994 | case XDP_REDIRECT: | |
995 | get_page(pdata); | |
996 | err = xdp_do_redirect(queue->info->netdev, xdp, prog); | |
997 | *need_xdp_flush = true; | |
998 | if (unlikely(err)) | |
999 | trace_xdp_exception(queue->info->netdev, prog, act); | |
1000 | break; | |
1001 | case XDP_PASS: | |
1002 | case XDP_DROP: | |
1003 | break; | |
1004 | ||
1005 | case XDP_ABORTED: | |
1006 | trace_xdp_exception(queue->info->netdev, prog, act); | |
1007 | break; | |
1008 | ||
1009 | default: | |
1010 | bpf_warn_invalid_xdp_action(act); | |
1011 | } | |
1012 | ||
1013 | return act; | |
1014 | } | |
1015 | ||
2688fcb7 | 1016 | static int xennet_get_responses(struct netfront_queue *queue, |
0d160211 | 1017 | struct netfront_rx_info *rinfo, RING_IDX rp, |
6c5aa6fc DK |
1018 | struct sk_buff_head *list, |
1019 | bool *need_xdp_flush) | |
0d160211 | 1020 | { |
8446066b | 1021 | struct xen_netif_rx_response *rx = &rinfo->rx, rx_local; |
6c5aa6fc | 1022 | int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD); |
2688fcb7 AB |
1023 | RING_IDX cons = queue->rx.rsp_cons; |
1024 | struct sk_buff *skb = xennet_get_rx_skb(queue, cons); | |
6c5aa6fc | 1025 | struct xen_netif_extra_info *extras = rinfo->extras; |
2688fcb7 | 1026 | grant_ref_t ref = xennet_get_rx_ref(queue, cons); |
6c5aa6fc DK |
1027 | struct device *dev = &queue->info->netdev->dev; |
1028 | struct bpf_prog *xdp_prog; | |
1029 | struct xdp_buff xdp; | |
7158ff6d | 1030 | int slots = 1; |
0d160211 | 1031 | int err = 0; |
6c5aa6fc | 1032 | u32 verdict; |
0d160211 | 1033 | |
f942dc25 | 1034 | if (rx->flags & XEN_NETRXF_extra_info) { |
2688fcb7 | 1035 | err = xennet_get_extras(queue, extras, rp); |
6c5aa6fc DK |
1036 | if (!err) { |
1037 | if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) { | |
1038 | struct xen_netif_extra_info *xdp; | |
1039 | ||
1040 | xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1]; | |
1041 | rx->offset = xdp->u.xdp.headroom; | |
1042 | } | |
1043 | } | |
2688fcb7 | 1044 | cons = queue->rx.rsp_cons; |
0d160211 JF |
1045 | } |
1046 | ||
1047 | for (;;) { | |
1048 | if (unlikely(rx->status < 0 || | |
30c5d7f0 | 1049 | rx->offset + rx->status > XEN_PAGE_SIZE)) { |
0d160211 | 1050 | if (net_ratelimit()) |
6c10127d | 1051 | dev_warn(dev, "rx->offset: %u, size: %d\n", |
0d160211 | 1052 | rx->offset, rx->status); |
2688fcb7 | 1053 | xennet_move_rx_slot(queue, skb, ref); |
0d160211 JF |
1054 | err = -EINVAL; |
1055 | goto next; | |
1056 | } | |
1057 | ||
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 JF |
1062 | */ |
1063 | if (ref == GRANT_INVALID_REF) { | |
1064 | if (net_ratelimit()) | |
1065 | dev_warn(dev, "Bad rx response id %d.\n", | |
1066 | rx->id); | |
1067 | err = -EINVAL; | |
1068 | goto next; | |
1069 | } | |
1070 | ||
60a69176 JG |
1071 | if (!gnttab_end_foreign_access_ref(ref, 0)) { |
1072 | dev_alert(dev, | |
1073 | "Grant still in use by backend domain\n"); | |
1074 | queue->info->broken = true; | |
1075 | dev_alert(dev, "Disabled for further use\n"); | |
1076 | return -EINVAL; | |
1077 | } | |
0d160211 | 1078 | |
2688fcb7 | 1079 | gnttab_release_grant_reference(&queue->gref_rx_head, ref); |
0d160211 | 1080 | |
6c5aa6fc DK |
1081 | rcu_read_lock(); |
1082 | xdp_prog = rcu_dereference(queue->xdp_prog); | |
1083 | if (xdp_prog) { | |
1084 | if (!(rx->flags & XEN_NETRXF_more_data)) { | |
1085 | /* currently only a single page contains data */ | |
1086 | verdict = xennet_run_xdp(queue, | |
1087 | skb_frag_page(&skb_shinfo(skb)->frags[0]), | |
1088 | rx, xdp_prog, &xdp, need_xdp_flush); | |
1089 | if (verdict != XDP_PASS) | |
1090 | err = -EINVAL; | |
1091 | } else { | |
1092 | /* drop the frame */ | |
1093 | err = -EINVAL; | |
1094 | } | |
1095 | } | |
1096 | rcu_read_unlock(); | |
24d76a00 | 1097 | |
6c5aa6fc | 1098 | __skb_queue_tail(list, skb); |
24d76a00 JB |
1099 | |
1100 | next: | |
f942dc25 | 1101 | if (!(rx->flags & XEN_NETRXF_more_data)) |
0d160211 JF |
1102 | break; |
1103 | ||
7158ff6d | 1104 | if (cons + slots == rp) { |
0d160211 | 1105 | if (net_ratelimit()) |
7158ff6d | 1106 | dev_warn(dev, "Need more slots\n"); |
0d160211 JF |
1107 | err = -ENOENT; |
1108 | break; | |
1109 | } | |
1110 | ||
8446066b JG |
1111 | RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local); |
1112 | rx = &rx_local; | |
2688fcb7 AB |
1113 | skb = xennet_get_rx_skb(queue, cons + slots); |
1114 | ref = xennet_get_rx_ref(queue, cons + slots); | |
7158ff6d | 1115 | slots++; |
0d160211 JF |
1116 | } |
1117 | ||
7158ff6d | 1118 | if (unlikely(slots > max)) { |
0d160211 | 1119 | if (net_ratelimit()) |
697089dc | 1120 | dev_warn(dev, "Too many slots\n"); |
0d160211 JF |
1121 | err = -E2BIG; |
1122 | } | |
1123 | ||
1124 | if (unlikely(err)) | |
900e0ac2 | 1125 | xennet_set_rx_rsp_cons(queue, cons + slots); |
0d160211 JF |
1126 | |
1127 | return err; | |
1128 | } | |
1129 | ||
1130 | static int xennet_set_skb_gso(struct sk_buff *skb, | |
1131 | struct xen_netif_extra_info *gso) | |
1132 | { | |
1133 | if (!gso->u.gso.size) { | |
1134 | if (net_ratelimit()) | |
383eda32 | 1135 | pr_warn("GSO size must not be zero\n"); |
0d160211 JF |
1136 | return -EINVAL; |
1137 | } | |
1138 | ||
2c0057de PD |
1139 | if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 && |
1140 | gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) { | |
0d160211 | 1141 | if (net_ratelimit()) |
383eda32 | 1142 | pr_warn("Bad GSO type %d\n", gso->u.gso.type); |
0d160211 JF |
1143 | return -EINVAL; |
1144 | } | |
1145 | ||
1146 | skb_shinfo(skb)->gso_size = gso->u.gso.size; | |
2c0057de PD |
1147 | skb_shinfo(skb)->gso_type = |
1148 | (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ? | |
1149 | SKB_GSO_TCPV4 : | |
1150 | SKB_GSO_TCPV6; | |
0d160211 JF |
1151 | |
1152 | /* Header must be checked, and gso_segs computed. */ | |
1153 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
1154 | skb_shinfo(skb)->gso_segs = 0; | |
1155 | ||
1156 | return 0; | |
1157 | } | |
1158 | ||
a761129e DZ |
1159 | static int xennet_fill_frags(struct netfront_queue *queue, |
1160 | struct sk_buff *skb, | |
1161 | struct sk_buff_head *list) | |
0d160211 | 1162 | { |
2688fcb7 | 1163 | RING_IDX cons = queue->rx.rsp_cons; |
0d160211 JF |
1164 | struct sk_buff *nskb; |
1165 | ||
1166 | while ((nskb = __skb_dequeue(list))) { | |
8446066b | 1167 | struct xen_netif_rx_response rx; |
01c68026 | 1168 | skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0]; |
0d160211 | 1169 | |
8446066b JG |
1170 | RING_COPY_RESPONSE(&queue->rx, ++cons, &rx); |
1171 | ||
d472b3a6 | 1172 | if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) { |
093b9c71 | 1173 | unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to; |
0d160211 | 1174 | |
d81c5054 | 1175 | BUG_ON(pull_to < skb_headlen(skb)); |
093b9c71 JB |
1176 | __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); |
1177 | } | |
ad4f15dc | 1178 | if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) { |
900e0ac2 JG |
1179 | xennet_set_rx_rsp_cons(queue, |
1180 | ++cons + skb_queue_len(list)); | |
ad4f15dc | 1181 | kfree_skb(nskb); |
a761129e | 1182 | return -ENOENT; |
ad4f15dc | 1183 | } |
093b9c71 | 1184 | |
d472b3a6 JG |
1185 | skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, |
1186 | skb_frag_page(nfrag), | |
8446066b | 1187 | rx.offset, rx.status, PAGE_SIZE); |
0d160211 JF |
1188 | |
1189 | skb_shinfo(nskb)->nr_frags = 0; | |
1190 | kfree_skb(nskb); | |
0d160211 JF |
1191 | } |
1192 | ||
900e0ac2 | 1193 | xennet_set_rx_rsp_cons(queue, cons); |
a761129e DZ |
1194 | |
1195 | return 0; | |
0d160211 JF |
1196 | } |
1197 | ||
e0ce4af9 | 1198 | static int checksum_setup(struct net_device *dev, struct sk_buff *skb) |
0d160211 | 1199 | { |
b5cf66cd | 1200 | bool recalculate_partial_csum = false; |
e0ce4af9 IC |
1201 | |
1202 | /* | |
1203 | * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy | |
1204 | * peers can fail to set NETRXF_csum_blank when sending a GSO | |
1205 | * frame. In this case force the SKB to CHECKSUM_PARTIAL and | |
1206 | * recalculate the partial checksum. | |
1207 | */ | |
1208 | if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) { | |
1209 | struct netfront_info *np = netdev_priv(dev); | |
2688fcb7 | 1210 | atomic_inc(&np->rx_gso_checksum_fixup); |
e0ce4af9 | 1211 | skb->ip_summed = CHECKSUM_PARTIAL; |
b5cf66cd | 1212 | recalculate_partial_csum = true; |
e0ce4af9 IC |
1213 | } |
1214 | ||
1215 | /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ | |
1216 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
1217 | return 0; | |
0d160211 | 1218 | |
b5cf66cd | 1219 | return skb_checksum_setup(skb, recalculate_partial_csum); |
0d160211 JF |
1220 | } |
1221 | ||
2688fcb7 | 1222 | static int handle_incoming_queue(struct netfront_queue *queue, |
09f75cd7 | 1223 | struct sk_buff_head *rxq) |
0d160211 | 1224 | { |
900e1833 | 1225 | struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats); |
0d160211 JF |
1226 | int packets_dropped = 0; |
1227 | struct sk_buff *skb; | |
1228 | ||
1229 | while ((skb = __skb_dequeue(rxq)) != NULL) { | |
3683243b | 1230 | int pull_to = NETFRONT_SKB_CB(skb)->pull_to; |
0d160211 | 1231 | |
093b9c71 JB |
1232 | if (pull_to > skb_headlen(skb)) |
1233 | __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); | |
0d160211 JF |
1234 | |
1235 | /* Ethernet work: Delayed to here as it peeks the header. */ | |
2688fcb7 | 1236 | skb->protocol = eth_type_trans(skb, queue->info->netdev); |
d554f73d | 1237 | skb_reset_network_header(skb); |
0d160211 | 1238 | |
2688fcb7 | 1239 | if (checksum_setup(queue->info->netdev, skb)) { |
e0ce4af9 IC |
1240 | kfree_skb(skb); |
1241 | packets_dropped++; | |
2688fcb7 | 1242 | queue->info->netdev->stats.rx_errors++; |
e0ce4af9 | 1243 | continue; |
0d160211 JF |
1244 | } |
1245 | ||
900e1833 DV |
1246 | u64_stats_update_begin(&rx_stats->syncp); |
1247 | rx_stats->packets++; | |
1248 | rx_stats->bytes += skb->len; | |
1249 | u64_stats_update_end(&rx_stats->syncp); | |
0d160211 JF |
1250 | |
1251 | /* Pass it up. */ | |
2688fcb7 | 1252 | napi_gro_receive(&queue->napi, skb); |
0d160211 JF |
1253 | } |
1254 | ||
1255 | return packets_dropped; | |
1256 | } | |
1257 | ||
bea3348e | 1258 | static int xennet_poll(struct napi_struct *napi, int budget) |
0d160211 | 1259 | { |
2688fcb7 AB |
1260 | struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi); |
1261 | struct net_device *dev = queue->info->netdev; | |
0d160211 JF |
1262 | struct sk_buff *skb; |
1263 | struct netfront_rx_info rinfo; | |
1264 | struct xen_netif_rx_response *rx = &rinfo.rx; | |
1265 | struct xen_netif_extra_info *extras = rinfo.extras; | |
1266 | RING_IDX i, rp; | |
bea3348e | 1267 | int work_done; |
0d160211 JF |
1268 | struct sk_buff_head rxq; |
1269 | struct sk_buff_head errq; | |
1270 | struct sk_buff_head tmpq; | |
0d160211 | 1271 | int err; |
6c5aa6fc | 1272 | bool need_xdp_flush = false; |
0d160211 | 1273 | |
2688fcb7 | 1274 | spin_lock(&queue->rx_lock); |
0d160211 | 1275 | |
0d160211 JF |
1276 | skb_queue_head_init(&rxq); |
1277 | skb_queue_head_init(&errq); | |
1278 | skb_queue_head_init(&tmpq); | |
1279 | ||
2688fcb7 | 1280 | rp = queue->rx.sring->rsp_prod; |
a884daa6 JG |
1281 | if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) { |
1282 | dev_alert(&dev->dev, "Illegal number of responses %u\n", | |
1283 | rp - queue->rx.rsp_cons); | |
1284 | queue->info->broken = true; | |
1285 | spin_unlock(&queue->rx_lock); | |
1286 | return 0; | |
1287 | } | |
0d160211 JF |
1288 | rmb(); /* Ensure we see queued responses up to 'rp'. */ |
1289 | ||
2688fcb7 | 1290 | i = queue->rx.rsp_cons; |
0d160211 JF |
1291 | work_done = 0; |
1292 | while ((i != rp) && (work_done < budget)) { | |
8446066b | 1293 | RING_COPY_RESPONSE(&queue->rx, i, rx); |
0d160211 JF |
1294 | memset(extras, 0, sizeof(rinfo.extras)); |
1295 | ||
6c5aa6fc DK |
1296 | err = xennet_get_responses(queue, &rinfo, rp, &tmpq, |
1297 | &need_xdp_flush); | |
0d160211 JF |
1298 | |
1299 | if (unlikely(err)) { | |
60a69176 JG |
1300 | if (queue->info->broken) { |
1301 | spin_unlock(&queue->rx_lock); | |
1302 | return 0; | |
1303 | } | |
0d160211 JF |
1304 | err: |
1305 | while ((skb = __skb_dequeue(&tmpq))) | |
1306 | __skb_queue_tail(&errq, skb); | |
09f75cd7 | 1307 | dev->stats.rx_errors++; |
2688fcb7 | 1308 | i = queue->rx.rsp_cons; |
0d160211 JF |
1309 | continue; |
1310 | } | |
1311 | ||
1312 | skb = __skb_dequeue(&tmpq); | |
1313 | ||
1314 | if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) { | |
1315 | struct xen_netif_extra_info *gso; | |
1316 | gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1]; | |
1317 | ||
1318 | if (unlikely(xennet_set_skb_gso(skb, gso))) { | |
1319 | __skb_queue_head(&tmpq, skb); | |
900e0ac2 JG |
1320 | xennet_set_rx_rsp_cons(queue, |
1321 | queue->rx.rsp_cons + | |
1322 | skb_queue_len(&tmpq)); | |
0d160211 JF |
1323 | goto err; |
1324 | } | |
1325 | } | |
1326 | ||
3683243b IC |
1327 | NETFRONT_SKB_CB(skb)->pull_to = rx->status; |
1328 | if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD) | |
1329 | NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD; | |
0d160211 | 1330 | |
b54c9d5b | 1331 | skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset); |
3683243b IC |
1332 | skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status); |
1333 | skb->data_len = rx->status; | |
093b9c71 | 1334 | skb->len += rx->status; |
0d160211 | 1335 | |
a761129e | 1336 | if (unlikely(xennet_fill_frags(queue, skb, &tmpq))) |
ad4f15dc | 1337 | goto err; |
0d160211 | 1338 | |
f942dc25 | 1339 | if (rx->flags & XEN_NETRXF_csum_blank) |
0d160211 | 1340 | skb->ip_summed = CHECKSUM_PARTIAL; |
f942dc25 | 1341 | else if (rx->flags & XEN_NETRXF_data_validated) |
0d160211 JF |
1342 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
1343 | ||
1344 | __skb_queue_tail(&rxq, skb); | |
1345 | ||
900e0ac2 JG |
1346 | i = queue->rx.rsp_cons + 1; |
1347 | xennet_set_rx_rsp_cons(queue, i); | |
0d160211 JF |
1348 | work_done++; |
1349 | } | |
6c5aa6fc DK |
1350 | if (need_xdp_flush) |
1351 | xdp_do_flush(); | |
0d160211 | 1352 | |
56cfe5d0 | 1353 | __skb_queue_purge(&errq); |
0d160211 | 1354 | |
2688fcb7 | 1355 | work_done -= handle_incoming_queue(queue, &rxq); |
0d160211 | 1356 | |
2688fcb7 | 1357 | xennet_alloc_rx_buffers(queue); |
0d160211 | 1358 | |
0d160211 | 1359 | if (work_done < budget) { |
bea3348e SH |
1360 | int more_to_do = 0; |
1361 | ||
6ad20165 | 1362 | napi_complete_done(napi, work_done); |
0d160211 | 1363 | |
2688fcb7 | 1364 | RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do); |
6a6dc08f DV |
1365 | if (more_to_do) |
1366 | napi_schedule(napi); | |
0d160211 JF |
1367 | } |
1368 | ||
2688fcb7 | 1369 | spin_unlock(&queue->rx_lock); |
0d160211 | 1370 | |
bea3348e | 1371 | return work_done; |
0d160211 JF |
1372 | } |
1373 | ||
1374 | static int xennet_change_mtu(struct net_device *dev, int mtu) | |
1375 | { | |
0c36820e | 1376 | int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN; |
0d160211 JF |
1377 | |
1378 | if (mtu > max) | |
1379 | return -EINVAL; | |
1380 | dev->mtu = mtu; | |
1381 | return 0; | |
1382 | } | |
1383 | ||
bc1f4470 | 1384 | static void xennet_get_stats64(struct net_device *dev, |
1385 | struct rtnl_link_stats64 *tot) | |
e00f85be | 1386 | { |
1387 | struct netfront_info *np = netdev_priv(dev); | |
1388 | int cpu; | |
1389 | ||
1390 | for_each_possible_cpu(cpu) { | |
900e1833 DV |
1391 | struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu); |
1392 | struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu); | |
e00f85be | 1393 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
1394 | unsigned int start; | |
1395 | ||
1396 | do { | |
900e1833 DV |
1397 | start = u64_stats_fetch_begin_irq(&tx_stats->syncp); |
1398 | tx_packets = tx_stats->packets; | |
1399 | tx_bytes = tx_stats->bytes; | |
1400 | } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start)); | |
e00f85be | 1401 | |
900e1833 DV |
1402 | do { |
1403 | start = u64_stats_fetch_begin_irq(&rx_stats->syncp); | |
1404 | rx_packets = rx_stats->packets; | |
1405 | rx_bytes = rx_stats->bytes; | |
1406 | } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start)); | |
e00f85be | 1407 | |
1408 | tot->rx_packets += rx_packets; | |
1409 | tot->tx_packets += tx_packets; | |
1410 | tot->rx_bytes += rx_bytes; | |
1411 | tot->tx_bytes += tx_bytes; | |
1412 | } | |
1413 | ||
1414 | tot->rx_errors = dev->stats.rx_errors; | |
1415 | tot->tx_dropped = dev->stats.tx_dropped; | |
e00f85be | 1416 | } |
1417 | ||
2688fcb7 | 1418 | static void xennet_release_tx_bufs(struct netfront_queue *queue) |
0d160211 JF |
1419 | { |
1420 | struct sk_buff *skb; | |
1421 | int i; | |
1422 | ||
1423 | for (i = 0; i < NET_TX_RING_SIZE; i++) { | |
1424 | /* Skip over entries which are actually freelist references */ | |
21631d2d | 1425 | if (!queue->tx_skbs[i]) |
0d160211 JF |
1426 | continue; |
1427 | ||
21631d2d JG |
1428 | skb = queue->tx_skbs[i]; |
1429 | queue->tx_skbs[i] = NULL; | |
2688fcb7 AB |
1430 | get_page(queue->grant_tx_page[i]); |
1431 | gnttab_end_foreign_access(queue->grant_tx_ref[i], | |
cefe0078 | 1432 | GNTMAP_readonly, |
2688fcb7 AB |
1433 | (unsigned long)page_address(queue->grant_tx_page[i])); |
1434 | queue->grant_tx_page[i] = NULL; | |
1435 | queue->grant_tx_ref[i] = GRANT_INVALID_REF; | |
21631d2d | 1436 | add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i); |
0d160211 JF |
1437 | dev_kfree_skb_irq(skb); |
1438 | } | |
1439 | } | |
1440 | ||
2688fcb7 | 1441 | static void xennet_release_rx_bufs(struct netfront_queue *queue) |
0d160211 | 1442 | { |
0d160211 JF |
1443 | int id, ref; |
1444 | ||
2688fcb7 | 1445 | spin_lock_bh(&queue->rx_lock); |
0d160211 JF |
1446 | |
1447 | for (id = 0; id < NET_RX_RING_SIZE; id++) { | |
cefe0078 AL |
1448 | struct sk_buff *skb; |
1449 | struct page *page; | |
0d160211 | 1450 | |
2688fcb7 | 1451 | skb = queue->rx_skbs[id]; |
cefe0078 | 1452 | if (!skb) |
0d160211 | 1453 | continue; |
0d160211 | 1454 | |
2688fcb7 | 1455 | ref = queue->grant_rx_ref[id]; |
cefe0078 AL |
1456 | if (ref == GRANT_INVALID_REF) |
1457 | continue; | |
0d160211 | 1458 | |
cefe0078 | 1459 | page = skb_frag_page(&skb_shinfo(skb)->frags[0]); |
0d160211 | 1460 | |
cefe0078 AL |
1461 | /* gnttab_end_foreign_access() needs a page ref until |
1462 | * foreign access is ended (which may be deferred). | |
1463 | */ | |
1464 | get_page(page); | |
1465 | gnttab_end_foreign_access(ref, 0, | |
1466 | (unsigned long)page_address(page)); | |
2688fcb7 | 1467 | queue->grant_rx_ref[id] = GRANT_INVALID_REF; |
0d160211 | 1468 | |
cefe0078 | 1469 | kfree_skb(skb); |
0d160211 JF |
1470 | } |
1471 | ||
2688fcb7 | 1472 | spin_unlock_bh(&queue->rx_lock); |
0d160211 JF |
1473 | } |
1474 | ||
c8f44aff MM |
1475 | static netdev_features_t xennet_fix_features(struct net_device *dev, |
1476 | netdev_features_t features) | |
8f7b01a1 ED |
1477 | { |
1478 | struct netfront_info *np = netdev_priv(dev); | |
8f7b01a1 | 1479 | |
2890ea5c JG |
1480 | if (features & NETIF_F_SG && |
1481 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0)) | |
1482 | features &= ~NETIF_F_SG; | |
8f7b01a1 | 1483 | |
2890ea5c JG |
1484 | if (features & NETIF_F_IPV6_CSUM && |
1485 | !xenbus_read_unsigned(np->xbdev->otherend, | |
1486 | "feature-ipv6-csum-offload", 0)) | |
1487 | features &= ~NETIF_F_IPV6_CSUM; | |
8f7b01a1 | 1488 | |
2890ea5c JG |
1489 | if (features & NETIF_F_TSO && |
1490 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0)) | |
1491 | features &= ~NETIF_F_TSO; | |
8f7b01a1 | 1492 | |
2890ea5c JG |
1493 | if (features & NETIF_F_TSO6 && |
1494 | !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0)) | |
1495 | features &= ~NETIF_F_TSO6; | |
2c0057de | 1496 | |
8f7b01a1 ED |
1497 | return features; |
1498 | } | |
1499 | ||
c8f44aff MM |
1500 | static int xennet_set_features(struct net_device *dev, |
1501 | netdev_features_t features) | |
8f7b01a1 ED |
1502 | { |
1503 | if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) { | |
1504 | netdev_info(dev, "Reducing MTU because no SG offload"); | |
1505 | dev->mtu = ETH_DATA_LEN; | |
1506 | } | |
1507 | ||
1508 | return 0; | |
1509 | } | |
1510 | ||
900e0ac2 | 1511 | static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi) |
cf66f9d4 | 1512 | { |
cf66f9d4 KRW |
1513 | unsigned long flags; |
1514 | ||
900e0ac2 JG |
1515 | if (unlikely(queue->info->broken)) |
1516 | return false; | |
a884daa6 | 1517 | |
2688fcb7 | 1518 | spin_lock_irqsave(&queue->tx_lock, flags); |
900e0ac2 JG |
1519 | if (xennet_tx_buf_gc(queue)) |
1520 | *eoi = 0; | |
2688fcb7 | 1521 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
cf66f9d4 | 1522 | |
900e0ac2 JG |
1523 | return true; |
1524 | } | |
1525 | ||
1526 | static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id) | |
1527 | { | |
1528 | unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; | |
1529 | ||
1530 | if (likely(xennet_handle_tx(dev_id, &eoiflag))) | |
1531 | xen_irq_lateeoi(irq, eoiflag); | |
1532 | ||
d634bf2c WL |
1533 | return IRQ_HANDLED; |
1534 | } | |
1535 | ||
900e0ac2 | 1536 | static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi) |
d634bf2c | 1537 | { |
900e0ac2 JG |
1538 | unsigned int work_queued; |
1539 | unsigned long flags; | |
d634bf2c | 1540 | |
900e0ac2 JG |
1541 | if (unlikely(queue->info->broken)) |
1542 | return false; | |
1543 | ||
1544 | spin_lock_irqsave(&queue->rx_cons_lock, flags); | |
1545 | work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx); | |
1546 | if (work_queued > queue->rx_rsp_unconsumed) { | |
1547 | queue->rx_rsp_unconsumed = work_queued; | |
1548 | *eoi = 0; | |
1549 | } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) { | |
1550 | const struct device *dev = &queue->info->netdev->dev; | |
1551 | ||
1552 | spin_unlock_irqrestore(&queue->rx_cons_lock, flags); | |
1553 | dev_alert(dev, "RX producer index going backwards\n"); | |
1554 | dev_alert(dev, "Disabled for further use\n"); | |
1555 | queue->info->broken = true; | |
1556 | return false; | |
1557 | } | |
1558 | spin_unlock_irqrestore(&queue->rx_cons_lock, flags); | |
a884daa6 | 1559 | |
900e0ac2 | 1560 | if (likely(netif_carrier_ok(queue->info->netdev) && work_queued)) |
76541869 | 1561 | napi_schedule(&queue->napi); |
cf66f9d4 | 1562 | |
900e0ac2 JG |
1563 | return true; |
1564 | } | |
1565 | ||
1566 | static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id) | |
1567 | { | |
1568 | unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; | |
1569 | ||
1570 | if (likely(xennet_handle_rx(dev_id, &eoiflag))) | |
1571 | xen_irq_lateeoi(irq, eoiflag); | |
1572 | ||
d634bf2c WL |
1573 | return IRQ_HANDLED; |
1574 | } | |
cf66f9d4 | 1575 | |
d634bf2c WL |
1576 | static irqreturn_t xennet_interrupt(int irq, void *dev_id) |
1577 | { | |
900e0ac2 JG |
1578 | unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; |
1579 | ||
1580 | if (xennet_handle_tx(dev_id, &eoiflag) && | |
1581 | xennet_handle_rx(dev_id, &eoiflag)) | |
1582 | xen_irq_lateeoi(irq, eoiflag); | |
1583 | ||
cf66f9d4 KRW |
1584 | return IRQ_HANDLED; |
1585 | } | |
1586 | ||
1587 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1588 | static void xennet_poll_controller(struct net_device *dev) | |
1589 | { | |
2688fcb7 AB |
1590 | /* Poll each queue */ |
1591 | struct netfront_info *info = netdev_priv(dev); | |
1592 | unsigned int num_queues = dev->real_num_tx_queues; | |
1593 | unsigned int i; | |
a884daa6 JG |
1594 | |
1595 | if (info->broken) | |
1596 | return; | |
1597 | ||
2688fcb7 AB |
1598 | for (i = 0; i < num_queues; ++i) |
1599 | xennet_interrupt(0, &info->queues[i]); | |
cf66f9d4 KRW |
1600 | } |
1601 | #endif | |
1602 | ||
6c5aa6fc DK |
1603 | #define NETBACK_XDP_HEADROOM_DISABLE 0 |
1604 | #define NETBACK_XDP_HEADROOM_ENABLE 1 | |
1605 | ||
1606 | static int talk_to_netback_xdp(struct netfront_info *np, int xdp) | |
1607 | { | |
1608 | int err; | |
1609 | unsigned short headroom; | |
1610 | ||
1611 | headroom = xdp ? XDP_PACKET_HEADROOM : 0; | |
1612 | err = xenbus_printf(XBT_NIL, np->xbdev->nodename, | |
1613 | "xdp-headroom", "%hu", | |
1614 | headroom); | |
1615 | if (err) | |
1616 | pr_warn("Error writing xdp-headroom\n"); | |
1617 | ||
1618 | return err; | |
1619 | } | |
1620 | ||
1621 | static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog, | |
1622 | struct netlink_ext_ack *extack) | |
1623 | { | |
1624 | unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM; | |
1625 | struct netfront_info *np = netdev_priv(dev); | |
1626 | struct bpf_prog *old_prog; | |
1627 | unsigned int i, err; | |
1628 | ||
1629 | if (dev->mtu > max_mtu) { | |
1630 | netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu); | |
1631 | return -EINVAL; | |
1632 | } | |
1633 | ||
1634 | if (!np->netback_has_xdp_headroom) | |
1635 | return 0; | |
1636 | ||
1637 | xenbus_switch_state(np->xbdev, XenbusStateReconfiguring); | |
1638 | ||
1639 | err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE : | |
1640 | NETBACK_XDP_HEADROOM_DISABLE); | |
1641 | if (err) | |
1642 | return err; | |
1643 | ||
1644 | /* avoid the race with XDP headroom adjustment */ | |
1645 | wait_event(module_wq, | |
1646 | xenbus_read_driver_state(np->xbdev->otherend) == | |
1647 | XenbusStateReconfigured); | |
1648 | np->netfront_xdp_enabled = true; | |
1649 | ||
1650 | old_prog = rtnl_dereference(np->queues[0].xdp_prog); | |
1651 | ||
1652 | if (prog) | |
1653 | bpf_prog_add(prog, dev->real_num_tx_queues); | |
1654 | ||
1655 | for (i = 0; i < dev->real_num_tx_queues; ++i) | |
1656 | rcu_assign_pointer(np->queues[i].xdp_prog, prog); | |
1657 | ||
1658 | if (old_prog) | |
1659 | for (i = 0; i < dev->real_num_tx_queues; ++i) | |
1660 | bpf_prog_put(old_prog); | |
1661 | ||
1662 | xenbus_switch_state(np->xbdev, XenbusStateConnected); | |
1663 | ||
1664 | return 0; | |
1665 | } | |
1666 | ||
6c5aa6fc DK |
1667 | static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp) |
1668 | { | |
a884daa6 JG |
1669 | struct netfront_info *np = netdev_priv(dev); |
1670 | ||
1671 | if (np->broken) | |
1672 | return -ENODEV; | |
1673 | ||
6c5aa6fc DK |
1674 | switch (xdp->command) { |
1675 | case XDP_SETUP_PROG: | |
1676 | return xennet_xdp_set(dev, xdp->prog, xdp->extack); | |
6c5aa6fc DK |
1677 | default: |
1678 | return -EINVAL; | |
1679 | } | |
1680 | } | |
1681 | ||
0a0b9d2e | 1682 | static const struct net_device_ops xennet_netdev_ops = { |
1d0ea124 | 1683 | .ndo_uninit = xennet_uninit, |
0a0b9d2e | 1684 | .ndo_open = xennet_open, |
0a0b9d2e SH |
1685 | .ndo_stop = xennet_close, |
1686 | .ndo_start_xmit = xennet_start_xmit, | |
1687 | .ndo_change_mtu = xennet_change_mtu, | |
e00f85be | 1688 | .ndo_get_stats64 = xennet_get_stats64, |
0a0b9d2e SH |
1689 | .ndo_set_mac_address = eth_mac_addr, |
1690 | .ndo_validate_addr = eth_validate_addr, | |
fb507934 MM |
1691 | .ndo_fix_features = xennet_fix_features, |
1692 | .ndo_set_features = xennet_set_features, | |
2688fcb7 | 1693 | .ndo_select_queue = xennet_select_queue, |
6c5aa6fc DK |
1694 | .ndo_bpf = xennet_xdp, |
1695 | .ndo_xdp_xmit = xennet_xdp_xmit, | |
cf66f9d4 KRW |
1696 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1697 | .ndo_poll_controller = xennet_poll_controller, | |
1698 | #endif | |
0a0b9d2e SH |
1699 | }; |
1700 | ||
900e1833 DV |
1701 | static void xennet_free_netdev(struct net_device *netdev) |
1702 | { | |
1703 | struct netfront_info *np = netdev_priv(netdev); | |
1704 | ||
1705 | free_percpu(np->rx_stats); | |
1706 | free_percpu(np->tx_stats); | |
1707 | free_netdev(netdev); | |
1708 | } | |
1709 | ||
8e0e46bb | 1710 | static struct net_device *xennet_create_dev(struct xenbus_device *dev) |
0d160211 | 1711 | { |
2688fcb7 | 1712 | int err; |
0d160211 JF |
1713 | struct net_device *netdev; |
1714 | struct netfront_info *np; | |
1715 | ||
50ee6061 | 1716 | netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues); |
41de8d4c | 1717 | if (!netdev) |
0d160211 | 1718 | return ERR_PTR(-ENOMEM); |
0d160211 JF |
1719 | |
1720 | np = netdev_priv(netdev); | |
1721 | np->xbdev = dev; | |
1722 | ||
2688fcb7 | 1723 | np->queues = NULL; |
0d160211 | 1724 | |
e00f85be | 1725 | err = -ENOMEM; |
900e1833 DV |
1726 | np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats); |
1727 | if (np->rx_stats == NULL) | |
1728 | goto exit; | |
1729 | np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats); | |
1730 | if (np->tx_stats == NULL) | |
e00f85be | 1731 | goto exit; |
1732 | ||
0a0b9d2e SH |
1733 | netdev->netdev_ops = &xennet_netdev_ops; |
1734 | ||
fb507934 MM |
1735 | netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM | |
1736 | NETIF_F_GSO_ROBUST; | |
2c0057de PD |
1737 | netdev->hw_features = NETIF_F_SG | |
1738 | NETIF_F_IPV6_CSUM | | |
1739 | NETIF_F_TSO | NETIF_F_TSO6; | |
0d160211 | 1740 | |
fc3e5941 IC |
1741 | /* |
1742 | * Assume that all hw features are available for now. This set | |
1743 | * will be adjusted by the call to netdev_update_features() in | |
1744 | * xennet_connect() which is the earliest point where we can | |
1745 | * negotiate with the backend regarding supported features. | |
1746 | */ | |
1747 | netdev->features |= netdev->hw_features; | |
1748 | ||
7ad24ea4 | 1749 | netdev->ethtool_ops = &xennet_ethtool_ops; |
e1043a4b | 1750 | netdev->min_mtu = ETH_MIN_MTU; |
d0c2c997 | 1751 | netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE; |
0d160211 JF |
1752 | SET_NETDEV_DEV(netdev, &dev->dev); |
1753 | ||
1754 | np->netdev = netdev; | |
6c5aa6fc | 1755 | np->netfront_xdp_enabled = false; |
0d160211 JF |
1756 | |
1757 | netif_carrier_off(netdev); | |
1758 | ||
c2c63310 AR |
1759 | do { |
1760 | xenbus_switch_state(dev, XenbusStateInitialising); | |
1761 | err = wait_event_timeout(module_wq, | |
1762 | xenbus_read_driver_state(dev->otherend) != | |
1763 | XenbusStateClosed && | |
1764 | xenbus_read_driver_state(dev->otherend) != | |
1765 | XenbusStateUnknown, XENNET_TIMEOUT); | |
1766 | } while (!err); | |
1767 | ||
0d160211 JF |
1768 | return netdev; |
1769 | ||
0d160211 | 1770 | exit: |
900e1833 | 1771 | xennet_free_netdev(netdev); |
0d160211 JF |
1772 | return ERR_PTR(err); |
1773 | } | |
1774 | ||
80708602 | 1775 | /* |
0d160211 JF |
1776 | * Entry point to this code when a new device is created. Allocate the basic |
1777 | * structures and the ring buffers for communication with the backend, and | |
1778 | * inform the backend of the appropriate details for those. | |
1779 | */ | |
8e0e46bb | 1780 | static int netfront_probe(struct xenbus_device *dev, |
1dd06ae8 | 1781 | const struct xenbus_device_id *id) |
0d160211 JF |
1782 | { |
1783 | int err; | |
1784 | struct net_device *netdev; | |
1785 | struct netfront_info *info; | |
1786 | ||
1787 | netdev = xennet_create_dev(dev); | |
1788 | if (IS_ERR(netdev)) { | |
1789 | err = PTR_ERR(netdev); | |
1790 | xenbus_dev_fatal(dev, err, "creating netdev"); | |
1791 | return err; | |
1792 | } | |
1793 | ||
1794 | info = netdev_priv(netdev); | |
1b713e00 | 1795 | dev_set_drvdata(&dev->dev, info); |
27b917e5 TI |
1796 | #ifdef CONFIG_SYSFS |
1797 | info->netdev->sysfs_groups[0] = &xennet_dev_group; | |
1798 | #endif | |
0d160211 | 1799 | |
0d160211 | 1800 | return 0; |
0d160211 JF |
1801 | } |
1802 | ||
1803 | static void xennet_end_access(int ref, void *page) | |
1804 | { | |
1805 | /* This frees the page as a side-effect */ | |
1806 | if (ref != GRANT_INVALID_REF) | |
1807 | gnttab_end_foreign_access(ref, 0, (unsigned long)page); | |
1808 | } | |
1809 | ||
1810 | static void xennet_disconnect_backend(struct netfront_info *info) | |
1811 | { | |
2688fcb7 | 1812 | unsigned int i = 0; |
2688fcb7 AB |
1813 | unsigned int num_queues = info->netdev->real_num_tx_queues; |
1814 | ||
f9feb1e6 DV |
1815 | netif_carrier_off(info->netdev); |
1816 | ||
9a873c71 | 1817 | for (i = 0; i < num_queues && info->queues; ++i) { |
76541869 DV |
1818 | struct netfront_queue *queue = &info->queues[i]; |
1819 | ||
74470954 BO |
1820 | del_timer_sync(&queue->rx_refill_timer); |
1821 | ||
2688fcb7 AB |
1822 | if (queue->tx_irq && (queue->tx_irq == queue->rx_irq)) |
1823 | unbind_from_irqhandler(queue->tx_irq, queue); | |
1824 | if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) { | |
1825 | unbind_from_irqhandler(queue->tx_irq, queue); | |
1826 | unbind_from_irqhandler(queue->rx_irq, queue); | |
1827 | } | |
1828 | queue->tx_evtchn = queue->rx_evtchn = 0; | |
1829 | queue->tx_irq = queue->rx_irq = 0; | |
0d160211 | 1830 | |
274b0455 CW |
1831 | if (netif_running(info->netdev)) |
1832 | napi_synchronize(&queue->napi); | |
f9feb1e6 | 1833 | |
a5b5dc3c DV |
1834 | xennet_release_tx_bufs(queue); |
1835 | xennet_release_rx_bufs(queue); | |
1836 | gnttab_free_grant_references(queue->gref_tx_head); | |
1837 | gnttab_free_grant_references(queue->gref_rx_head); | |
1838 | ||
2688fcb7 AB |
1839 | /* End access and free the pages */ |
1840 | xennet_end_access(queue->tx_ring_ref, queue->tx.sring); | |
1841 | xennet_end_access(queue->rx_ring_ref, queue->rx.sring); | |
0d160211 | 1842 | |
2688fcb7 AB |
1843 | queue->tx_ring_ref = GRANT_INVALID_REF; |
1844 | queue->rx_ring_ref = GRANT_INVALID_REF; | |
1845 | queue->tx.sring = NULL; | |
1846 | queue->rx.sring = NULL; | |
6c5aa6fc DK |
1847 | |
1848 | page_pool_destroy(queue->page_pool); | |
2688fcb7 | 1849 | } |
0d160211 JF |
1850 | } |
1851 | ||
80708602 | 1852 | /* |
0d160211 JF |
1853 | * We are reconnecting to the backend, due to a suspend/resume, or a backend |
1854 | * driver restart. We tear down our netif structure and recreate it, but | |
1855 | * leave the device-layer structures intact so that this is transparent to the | |
1856 | * rest of the kernel. | |
1857 | */ | |
1858 | static int netfront_resume(struct xenbus_device *dev) | |
1859 | { | |
1b713e00 | 1860 | struct netfront_info *info = dev_get_drvdata(&dev->dev); |
0d160211 JF |
1861 | |
1862 | dev_dbg(&dev->dev, "%s\n", dev->nodename); | |
1863 | ||
042b2046 DZ |
1864 | netif_tx_lock_bh(info->netdev); |
1865 | netif_device_detach(info->netdev); | |
1866 | netif_tx_unlock_bh(info->netdev); | |
1867 | ||
0d160211 JF |
1868 | xennet_disconnect_backend(info); |
1869 | return 0; | |
1870 | } | |
1871 | ||
1872 | static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[]) | |
1873 | { | |
1874 | char *s, *e, *macstr; | |
1875 | int i; | |
1876 | ||
1877 | macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL); | |
1878 | if (IS_ERR(macstr)) | |
1879 | return PTR_ERR(macstr); | |
1880 | ||
1881 | for (i = 0; i < ETH_ALEN; i++) { | |
1882 | mac[i] = simple_strtoul(s, &e, 16); | |
1883 | if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) { | |
1884 | kfree(macstr); | |
1885 | return -ENOENT; | |
1886 | } | |
1887 | s = e+1; | |
1888 | } | |
1889 | ||
1890 | kfree(macstr); | |
1891 | return 0; | |
1892 | } | |
1893 | ||
2688fcb7 | 1894 | static int setup_netfront_single(struct netfront_queue *queue) |
d634bf2c WL |
1895 | { |
1896 | int err; | |
1897 | ||
2688fcb7 | 1898 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); |
d634bf2c WL |
1899 | if (err < 0) |
1900 | goto fail; | |
1901 | ||
900e0ac2 JG |
1902 | err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn, |
1903 | xennet_interrupt, 0, | |
1904 | queue->info->netdev->name, | |
1905 | queue); | |
d634bf2c WL |
1906 | if (err < 0) |
1907 | goto bind_fail; | |
2688fcb7 AB |
1908 | queue->rx_evtchn = queue->tx_evtchn; |
1909 | queue->rx_irq = queue->tx_irq = err; | |
d634bf2c WL |
1910 | |
1911 | return 0; | |
1912 | ||
1913 | bind_fail: | |
2688fcb7 AB |
1914 | xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); |
1915 | queue->tx_evtchn = 0; | |
d634bf2c WL |
1916 | fail: |
1917 | return err; | |
1918 | } | |
1919 | ||
2688fcb7 | 1920 | static int setup_netfront_split(struct netfront_queue *queue) |
d634bf2c WL |
1921 | { |
1922 | int err; | |
1923 | ||
2688fcb7 | 1924 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); |
d634bf2c WL |
1925 | if (err < 0) |
1926 | goto fail; | |
2688fcb7 | 1927 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn); |
d634bf2c WL |
1928 | if (err < 0) |
1929 | goto alloc_rx_evtchn_fail; | |
1930 | ||
2688fcb7 AB |
1931 | snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name), |
1932 | "%s-tx", queue->name); | |
900e0ac2 JG |
1933 | err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn, |
1934 | xennet_tx_interrupt, 0, | |
1935 | queue->tx_irq_name, queue); | |
d634bf2c WL |
1936 | if (err < 0) |
1937 | goto bind_tx_fail; | |
2688fcb7 | 1938 | queue->tx_irq = err; |
d634bf2c | 1939 | |
2688fcb7 AB |
1940 | snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name), |
1941 | "%s-rx", queue->name); | |
900e0ac2 JG |
1942 | err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn, |
1943 | xennet_rx_interrupt, 0, | |
1944 | queue->rx_irq_name, queue); | |
d634bf2c WL |
1945 | if (err < 0) |
1946 | goto bind_rx_fail; | |
2688fcb7 | 1947 | queue->rx_irq = err; |
d634bf2c WL |
1948 | |
1949 | return 0; | |
1950 | ||
1951 | bind_rx_fail: | |
2688fcb7 AB |
1952 | unbind_from_irqhandler(queue->tx_irq, queue); |
1953 | queue->tx_irq = 0; | |
d634bf2c | 1954 | bind_tx_fail: |
2688fcb7 AB |
1955 | xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn); |
1956 | queue->rx_evtchn = 0; | |
d634bf2c | 1957 | alloc_rx_evtchn_fail: |
2688fcb7 AB |
1958 | xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); |
1959 | queue->tx_evtchn = 0; | |
d634bf2c WL |
1960 | fail: |
1961 | return err; | |
1962 | } | |
1963 | ||
2688fcb7 AB |
1964 | static int setup_netfront(struct xenbus_device *dev, |
1965 | struct netfront_queue *queue, unsigned int feature_split_evtchn) | |
0d160211 JF |
1966 | { |
1967 | struct xen_netif_tx_sring *txs; | |
60a69176 | 1968 | struct xen_netif_rx_sring *rxs = NULL; |
ccc9d90a | 1969 | grant_ref_t gref; |
0d160211 | 1970 | int err; |
0d160211 | 1971 | |
2688fcb7 AB |
1972 | queue->tx_ring_ref = GRANT_INVALID_REF; |
1973 | queue->rx_ring_ref = GRANT_INVALID_REF; | |
1974 | queue->rx.sring = NULL; | |
1975 | queue->tx.sring = NULL; | |
0d160211 | 1976 | |
a144ff09 | 1977 | txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH); |
0d160211 JF |
1978 | if (!txs) { |
1979 | err = -ENOMEM; | |
1980 | xenbus_dev_fatal(dev, err, "allocating tx ring page"); | |
1981 | goto fail; | |
1982 | } | |
1983 | SHARED_RING_INIT(txs); | |
30c5d7f0 | 1984 | FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE); |
0d160211 | 1985 | |
ccc9d90a | 1986 | err = xenbus_grant_ring(dev, txs, 1, &gref); |
1ca2983a | 1987 | if (err < 0) |
60a69176 | 1988 | goto fail; |
ccc9d90a | 1989 | queue->tx_ring_ref = gref; |
0d160211 | 1990 | |
a144ff09 | 1991 | rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH); |
0d160211 JF |
1992 | if (!rxs) { |
1993 | err = -ENOMEM; | |
1994 | xenbus_dev_fatal(dev, err, "allocating rx ring page"); | |
60a69176 | 1995 | goto fail; |
0d160211 JF |
1996 | } |
1997 | SHARED_RING_INIT(rxs); | |
30c5d7f0 | 1998 | FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE); |
0d160211 | 1999 | |
ccc9d90a | 2000 | err = xenbus_grant_ring(dev, rxs, 1, &gref); |
1ca2983a | 2001 | if (err < 0) |
60a69176 | 2002 | goto fail; |
ccc9d90a | 2003 | queue->rx_ring_ref = gref; |
0d160211 | 2004 | |
d634bf2c | 2005 | if (feature_split_evtchn) |
2688fcb7 | 2006 | err = setup_netfront_split(queue); |
d634bf2c WL |
2007 | /* setup single event channel if |
2008 | * a) feature-split-event-channels == 0 | |
2009 | * b) feature-split-event-channels == 1 but failed to setup | |
2010 | */ | |
e93fac3b | 2011 | if (!feature_split_evtchn || err) |
2688fcb7 | 2012 | err = setup_netfront_single(queue); |
d634bf2c | 2013 | |
0d160211 | 2014 | if (err) |
60a69176 | 2015 | goto fail; |
0d160211 | 2016 | |
0d160211 JF |
2017 | return 0; |
2018 | ||
1ca2983a WL |
2019 | /* If we fail to setup netfront, it is safe to just revoke access to |
2020 | * granted pages because backend is not accessing it at this point. | |
2021 | */ | |
60a69176 JG |
2022 | fail: |
2023 | if (queue->rx_ring_ref != GRANT_INVALID_REF) { | |
2024 | gnttab_end_foreign_access(queue->rx_ring_ref, 0, | |
2025 | (unsigned long)rxs); | |
2026 | queue->rx_ring_ref = GRANT_INVALID_REF; | |
2027 | } else { | |
2028 | free_page((unsigned long)rxs); | |
2029 | } | |
2030 | if (queue->tx_ring_ref != GRANT_INVALID_REF) { | |
2031 | gnttab_end_foreign_access(queue->tx_ring_ref, 0, | |
2032 | (unsigned long)txs); | |
2033 | queue->tx_ring_ref = GRANT_INVALID_REF; | |
2034 | } else { | |
2035 | free_page((unsigned long)txs); | |
2036 | } | |
0d160211 JF |
2037 | return err; |
2038 | } | |
2039 | ||
2688fcb7 AB |
2040 | /* Queue-specific initialisation |
2041 | * This used to be done in xennet_create_dev() but must now | |
2042 | * be run per-queue. | |
2043 | */ | |
2044 | static int xennet_init_queue(struct netfront_queue *queue) | |
2045 | { | |
2046 | unsigned short i; | |
2047 | int err = 0; | |
21f2706b | 2048 | char *devid; |
2688fcb7 AB |
2049 | |
2050 | spin_lock_init(&queue->tx_lock); | |
2051 | spin_lock_init(&queue->rx_lock); | |
900e0ac2 | 2052 | spin_lock_init(&queue->rx_cons_lock); |
2688fcb7 | 2053 | |
e99e88a9 | 2054 | timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0); |
2688fcb7 | 2055 | |
21f2706b XL |
2056 | devid = strrchr(queue->info->xbdev->nodename, '/') + 1; |
2057 | snprintf(queue->name, sizeof(queue->name), "vif%s-q%u", | |
2058 | devid, queue->id); | |
8b715010 | 2059 | |
21631d2d | 2060 | /* Initialise tx_skb_freelist as a free chain containing every entry. */ |
2688fcb7 | 2061 | queue->tx_skb_freelist = 0; |
a884daa6 | 2062 | queue->tx_pend_queue = TX_LINK_NONE; |
2688fcb7 | 2063 | for (i = 0; i < NET_TX_RING_SIZE; i++) { |
21631d2d | 2064 | queue->tx_link[i] = i + 1; |
2688fcb7 AB |
2065 | queue->grant_tx_ref[i] = GRANT_INVALID_REF; |
2066 | queue->grant_tx_page[i] = NULL; | |
2067 | } | |
21631d2d | 2068 | queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE; |
2688fcb7 AB |
2069 | |
2070 | /* Clear out rx_skbs */ | |
2071 | for (i = 0; i < NET_RX_RING_SIZE; i++) { | |
2072 | queue->rx_skbs[i] = NULL; | |
2073 | queue->grant_rx_ref[i] = GRANT_INVALID_REF; | |
2074 | } | |
2075 | ||
2076 | /* A grant for every tx ring slot */ | |
1f3c2eba | 2077 | if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, |
2688fcb7 AB |
2078 | &queue->gref_tx_head) < 0) { |
2079 | pr_alert("can't alloc tx grant refs\n"); | |
2080 | err = -ENOMEM; | |
2081 | goto exit; | |
2082 | } | |
2083 | ||
2084 | /* A grant for every rx ring slot */ | |
1f3c2eba | 2085 | if (gnttab_alloc_grant_references(NET_RX_RING_SIZE, |
2688fcb7 AB |
2086 | &queue->gref_rx_head) < 0) { |
2087 | pr_alert("can't alloc rx grant refs\n"); | |
2088 | err = -ENOMEM; | |
2089 | goto exit_free_tx; | |
2090 | } | |
2091 | ||
2688fcb7 AB |
2092 | return 0; |
2093 | ||
2094 | exit_free_tx: | |
2095 | gnttab_free_grant_references(queue->gref_tx_head); | |
2096 | exit: | |
2097 | return err; | |
2098 | } | |
2099 | ||
50ee6061 AB |
2100 | static int write_queue_xenstore_keys(struct netfront_queue *queue, |
2101 | struct xenbus_transaction *xbt, int write_hierarchical) | |
2102 | { | |
2103 | /* Write the queue-specific keys into XenStore in the traditional | |
2104 | * way for a single queue, or in a queue subkeys for multiple | |
2105 | * queues. | |
2106 | */ | |
2107 | struct xenbus_device *dev = queue->info->xbdev; | |
2108 | int err; | |
2109 | const char *message; | |
2110 | char *path; | |
2111 | size_t pathsize; | |
2112 | ||
2113 | /* Choose the correct place to write the keys */ | |
2114 | if (write_hierarchical) { | |
2115 | pathsize = strlen(dev->nodename) + 10; | |
2116 | path = kzalloc(pathsize, GFP_KERNEL); | |
2117 | if (!path) { | |
2118 | err = -ENOMEM; | |
2119 | message = "out of memory while writing ring references"; | |
2120 | goto error; | |
2121 | } | |
2122 | snprintf(path, pathsize, "%s/queue-%u", | |
2123 | dev->nodename, queue->id); | |
2124 | } else { | |
2125 | path = (char *)dev->nodename; | |
2126 | } | |
2127 | ||
2128 | /* Write ring references */ | |
2129 | err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u", | |
2130 | queue->tx_ring_ref); | |
2131 | if (err) { | |
2132 | message = "writing tx-ring-ref"; | |
2133 | goto error; | |
2134 | } | |
2135 | ||
2136 | err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u", | |
2137 | queue->rx_ring_ref); | |
2138 | if (err) { | |
2139 | message = "writing rx-ring-ref"; | |
2140 | goto error; | |
2141 | } | |
2142 | ||
2143 | /* Write event channels; taking into account both shared | |
2144 | * and split event channel scenarios. | |
2145 | */ | |
2146 | if (queue->tx_evtchn == queue->rx_evtchn) { | |
2147 | /* Shared event channel */ | |
2148 | err = xenbus_printf(*xbt, path, | |
2149 | "event-channel", "%u", queue->tx_evtchn); | |
2150 | if (err) { | |
2151 | message = "writing event-channel"; | |
2152 | goto error; | |
2153 | } | |
2154 | } else { | |
2155 | /* Split event channels */ | |
2156 | err = xenbus_printf(*xbt, path, | |
2157 | "event-channel-tx", "%u", queue->tx_evtchn); | |
2158 | if (err) { | |
2159 | message = "writing event-channel-tx"; | |
2160 | goto error; | |
2161 | } | |
2162 | ||
2163 | err = xenbus_printf(*xbt, path, | |
2164 | "event-channel-rx", "%u", queue->rx_evtchn); | |
2165 | if (err) { | |
2166 | message = "writing event-channel-rx"; | |
2167 | goto error; | |
2168 | } | |
2169 | } | |
2170 | ||
2171 | if (write_hierarchical) | |
2172 | kfree(path); | |
2173 | return 0; | |
2174 | ||
2175 | error: | |
2176 | if (write_hierarchical) | |
2177 | kfree(path); | |
2178 | xenbus_dev_fatal(dev, err, "%s", message); | |
2179 | return err; | |
2180 | } | |
2181 | ||
6c5aa6fc DK |
2182 | |
2183 | ||
2184 | static int xennet_create_page_pool(struct netfront_queue *queue) | |
2185 | { | |
2186 | int err; | |
2187 | struct page_pool_params pp_params = { | |
2188 | .order = 0, | |
2189 | .flags = 0, | |
2190 | .pool_size = NET_RX_RING_SIZE, | |
2191 | .nid = NUMA_NO_NODE, | |
2192 | .dev = &queue->info->netdev->dev, | |
2193 | .offset = XDP_PACKET_HEADROOM, | |
2194 | .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM, | |
2195 | }; | |
2196 | ||
2197 | queue->page_pool = page_pool_create(&pp_params); | |
2198 | if (IS_ERR(queue->page_pool)) { | |
2199 | err = PTR_ERR(queue->page_pool); | |
2200 | queue->page_pool = NULL; | |
2201 | return err; | |
2202 | } | |
2203 | ||
2204 | err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev, | |
b02e5a0e | 2205 | queue->id, 0); |
6c5aa6fc DK |
2206 | if (err) { |
2207 | netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n"); | |
2208 | goto err_free_pp; | |
2209 | } | |
2210 | ||
2211 | err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq, | |
2212 | MEM_TYPE_PAGE_POOL, queue->page_pool); | |
2213 | if (err) { | |
2214 | netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n"); | |
2215 | goto err_unregister_rxq; | |
2216 | } | |
2217 | return 0; | |
2218 | ||
2219 | err_unregister_rxq: | |
2220 | xdp_rxq_info_unreg(&queue->xdp_rxq); | |
2221 | err_free_pp: | |
2222 | page_pool_destroy(queue->page_pool); | |
2223 | queue->page_pool = NULL; | |
2224 | return err; | |
2225 | } | |
2226 | ||
ce58725f | 2227 | static int xennet_create_queues(struct netfront_info *info, |
ca88ea12 | 2228 | unsigned int *num_queues) |
ce58725f DV |
2229 | { |
2230 | unsigned int i; | |
2231 | int ret; | |
2232 | ||
ca88ea12 | 2233 | info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue), |
ce58725f DV |
2234 | GFP_KERNEL); |
2235 | if (!info->queues) | |
2236 | return -ENOMEM; | |
2237 | ||
ca88ea12 | 2238 | for (i = 0; i < *num_queues; i++) { |
ce58725f DV |
2239 | struct netfront_queue *queue = &info->queues[i]; |
2240 | ||
2241 | queue->id = i; | |
2242 | queue->info = info; | |
2243 | ||
2244 | ret = xennet_init_queue(queue); | |
2245 | if (ret < 0) { | |
f599c64f | 2246 | dev_warn(&info->xbdev->dev, |
69cb8524 | 2247 | "only created %d queues\n", i); |
ca88ea12 | 2248 | *num_queues = i; |
ce58725f DV |
2249 | break; |
2250 | } | |
2251 | ||
6c5aa6fc DK |
2252 | /* use page pool recycling instead of buddy allocator */ |
2253 | ret = xennet_create_page_pool(queue); | |
2254 | if (ret < 0) { | |
2255 | dev_err(&info->xbdev->dev, "can't allocate page pool\n"); | |
2256 | *num_queues = i; | |
2257 | return ret; | |
2258 | } | |
2259 | ||
ce58725f DV |
2260 | netif_napi_add(queue->info->netdev, &queue->napi, |
2261 | xennet_poll, 64); | |
2262 | if (netif_running(info->netdev)) | |
2263 | napi_enable(&queue->napi); | |
2264 | } | |
2265 | ||
ca88ea12 | 2266 | netif_set_real_num_tx_queues(info->netdev, *num_queues); |
ce58725f | 2267 | |
ca88ea12 | 2268 | if (*num_queues == 0) { |
f599c64f | 2269 | dev_err(&info->xbdev->dev, "no queues\n"); |
ce58725f DV |
2270 | return -EINVAL; |
2271 | } | |
2272 | return 0; | |
2273 | } | |
2274 | ||
0d160211 | 2275 | /* Common code used when first setting up, and when resuming. */ |
f502bf2b | 2276 | static int talk_to_netback(struct xenbus_device *dev, |
0d160211 JF |
2277 | struct netfront_info *info) |
2278 | { | |
2279 | const char *message; | |
2280 | struct xenbus_transaction xbt; | |
2281 | int err; | |
2688fcb7 AB |
2282 | unsigned int feature_split_evtchn; |
2283 | unsigned int i = 0; | |
50ee6061 | 2284 | unsigned int max_queues = 0; |
2688fcb7 AB |
2285 | struct netfront_queue *queue = NULL; |
2286 | unsigned int num_queues = 1; | |
0d160211 | 2287 | |
2688fcb7 AB |
2288 | info->netdev->irq = 0; |
2289 | ||
5c132c88 RPM |
2290 | /* Check if backend is trusted. */ |
2291 | info->bounce = !xennet_trusted || | |
2292 | !xenbus_read_unsigned(dev->nodename, "trusted", 1); | |
2293 | ||
50ee6061 | 2294 | /* Check if backend supports multiple queues */ |
2890ea5c JG |
2295 | max_queues = xenbus_read_unsigned(info->xbdev->otherend, |
2296 | "multi-queue-max-queues", 1); | |
50ee6061 AB |
2297 | num_queues = min(max_queues, xennet_max_queues); |
2298 | ||
2688fcb7 | 2299 | /* Check feature-split-event-channels */ |
2890ea5c JG |
2300 | feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend, |
2301 | "feature-split-event-channels", 0); | |
2688fcb7 AB |
2302 | |
2303 | /* Read mac addr. */ | |
2304 | err = xen_net_read_mac(dev, info->netdev->dev_addr); | |
2305 | if (err) { | |
2306 | xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename); | |
cb257783 | 2307 | goto out_unlocked; |
2688fcb7 AB |
2308 | } |
2309 | ||
6c5aa6fc DK |
2310 | info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend, |
2311 | "feature-xdp-headroom", 0); | |
2312 | if (info->netback_has_xdp_headroom) { | |
2313 | /* set the current xen-netfront xdp state */ | |
2314 | err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ? | |
2315 | NETBACK_XDP_HEADROOM_ENABLE : | |
2316 | NETBACK_XDP_HEADROOM_DISABLE); | |
2317 | if (err) | |
2318 | goto out_unlocked; | |
2319 | } | |
2320 | ||
f599c64f | 2321 | rtnl_lock(); |
ce58725f DV |
2322 | if (info->queues) |
2323 | xennet_destroy_queues(info); | |
2324 | ||
a884daa6 JG |
2325 | /* For the case of a reconnect reset the "broken" indicator. */ |
2326 | info->broken = false; | |
2327 | ||
ca88ea12 | 2328 | err = xennet_create_queues(info, &num_queues); |
e2e004ac RL |
2329 | if (err < 0) { |
2330 | xenbus_dev_fatal(dev, err, "creating queues"); | |
2331 | kfree(info->queues); | |
2332 | info->queues = NULL; | |
2333 | goto out; | |
2334 | } | |
f599c64f | 2335 | rtnl_unlock(); |
2688fcb7 AB |
2336 | |
2337 | /* Create shared ring, alloc event channel -- for each queue */ | |
2338 | for (i = 0; i < num_queues; ++i) { | |
2339 | queue = &info->queues[i]; | |
2688fcb7 | 2340 | err = setup_netfront(dev, queue, feature_split_evtchn); |
e2e004ac RL |
2341 | if (err) |
2342 | goto destroy_ring; | |
2688fcb7 | 2343 | } |
0d160211 JF |
2344 | |
2345 | again: | |
2346 | err = xenbus_transaction_start(&xbt); | |
2347 | if (err) { | |
2348 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
2349 | goto destroy_ring; | |
2350 | } | |
2351 | ||
812494d9 | 2352 | if (xenbus_exists(XBT_NIL, |
2353 | info->xbdev->otherend, "multi-queue-max-queues")) { | |
50ee6061 | 2354 | /* Write the number of queues */ |
812494d9 | 2355 | err = xenbus_printf(xbt, dev->nodename, |
2356 | "multi-queue-num-queues", "%u", num_queues); | |
d634bf2c | 2357 | if (err) { |
50ee6061 AB |
2358 | message = "writing multi-queue-num-queues"; |
2359 | goto abort_transaction_no_dev_fatal; | |
d634bf2c | 2360 | } |
812494d9 | 2361 | } |
50ee6061 | 2362 | |
812494d9 | 2363 | if (num_queues == 1) { |
2364 | err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */ | |
2365 | if (err) | |
2366 | goto abort_transaction_no_dev_fatal; | |
2367 | } else { | |
50ee6061 AB |
2368 | /* Write the keys for each queue */ |
2369 | for (i = 0; i < num_queues; ++i) { | |
2370 | queue = &info->queues[i]; | |
2371 | err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */ | |
2372 | if (err) | |
2373 | goto abort_transaction_no_dev_fatal; | |
d634bf2c | 2374 | } |
0d160211 JF |
2375 | } |
2376 | ||
50ee6061 | 2377 | /* The remaining keys are not queue-specific */ |
0d160211 JF |
2378 | err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u", |
2379 | 1); | |
2380 | if (err) { | |
2381 | message = "writing request-rx-copy"; | |
2382 | goto abort_transaction; | |
2383 | } | |
2384 | ||
2385 | err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1); | |
2386 | if (err) { | |
2387 | message = "writing feature-rx-notify"; | |
2388 | goto abort_transaction; | |
2389 | } | |
2390 | ||
2391 | err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1); | |
2392 | if (err) { | |
2393 | message = "writing feature-sg"; | |
2394 | goto abort_transaction; | |
2395 | } | |
2396 | ||
2397 | err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1); | |
2398 | if (err) { | |
2399 | message = "writing feature-gso-tcpv4"; | |
2400 | goto abort_transaction; | |
2401 | } | |
2402 | ||
2c0057de PD |
2403 | err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1"); |
2404 | if (err) { | |
2405 | message = "writing feature-gso-tcpv6"; | |
2406 | goto abort_transaction; | |
2407 | } | |
2408 | ||
2409 | err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload", | |
2410 | "1"); | |
2411 | if (err) { | |
2412 | message = "writing feature-ipv6-csum-offload"; | |
2413 | goto abort_transaction; | |
2414 | } | |
2415 | ||
0d160211 JF |
2416 | err = xenbus_transaction_end(xbt, 0); |
2417 | if (err) { | |
2418 | if (err == -EAGAIN) | |
2419 | goto again; | |
2420 | xenbus_dev_fatal(dev, err, "completing transaction"); | |
2421 | goto destroy_ring; | |
2422 | } | |
2423 | ||
2424 | return 0; | |
2425 | ||
2426 | abort_transaction: | |
0d160211 | 2427 | xenbus_dev_fatal(dev, err, "%s", message); |
50ee6061 AB |
2428 | abort_transaction_no_dev_fatal: |
2429 | xenbus_transaction_end(xbt, 1); | |
0d160211 JF |
2430 | destroy_ring: |
2431 | xennet_disconnect_backend(info); | |
f599c64f | 2432 | rtnl_lock(); |
e2e004ac | 2433 | xennet_destroy_queues(info); |
0d160211 | 2434 | out: |
f599c64f | 2435 | rtnl_unlock(); |
cb257783 | 2436 | out_unlocked: |
d86b5672 | 2437 | device_unregister(&dev->dev); |
0d160211 JF |
2438 | return err; |
2439 | } | |
2440 | ||
0d160211 JF |
2441 | static int xennet_connect(struct net_device *dev) |
2442 | { | |
2443 | struct netfront_info *np = netdev_priv(dev); | |
2688fcb7 | 2444 | unsigned int num_queues = 0; |
a5b5dc3c | 2445 | int err; |
2688fcb7 AB |
2446 | unsigned int j = 0; |
2447 | struct netfront_queue *queue = NULL; | |
0d160211 | 2448 | |
2890ea5c | 2449 | if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) { |
0d160211 | 2450 | dev_info(&dev->dev, |
898eb71c | 2451 | "backend does not support copying receive path\n"); |
0d160211 JF |
2452 | return -ENODEV; |
2453 | } | |
2454 | ||
f502bf2b | 2455 | err = talk_to_netback(np->xbdev, np); |
0d160211 JF |
2456 | if (err) |
2457 | return err; | |
6c5aa6fc DK |
2458 | if (np->netback_has_xdp_headroom) |
2459 | pr_info("backend supports XDP headroom\n"); | |
5c132c88 RPM |
2460 | if (np->bounce) |
2461 | dev_info(&np->xbdev->dev, | |
2462 | "bouncing transmitted data to zeroed pages\n"); | |
0d160211 | 2463 | |
2688fcb7 AB |
2464 | /* talk_to_netback() sets the correct number of queues */ |
2465 | num_queues = dev->real_num_tx_queues; | |
2466 | ||
f599c64f RL |
2467 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
2468 | err = register_netdev(dev); | |
2469 | if (err) { | |
2470 | pr_warn("%s: register_netdev err=%d\n", __func__, err); | |
2471 | device_unregister(&np->xbdev->dev); | |
2472 | return err; | |
2473 | } | |
2474 | } | |
2475 | ||
45c8184c RL |
2476 | rtnl_lock(); |
2477 | netdev_update_features(dev); | |
2478 | rtnl_unlock(); | |
2479 | ||
0d160211 | 2480 | /* |
a5b5dc3c | 2481 | * All public and private state should now be sane. Get |
0d160211 JF |
2482 | * ready to start sending and receiving packets and give the driver |
2483 | * domain a kick because we've probably just requeued some | |
2484 | * packets. | |
2485 | */ | |
042b2046 DZ |
2486 | netif_tx_lock_bh(np->netdev); |
2487 | netif_device_attach(np->netdev); | |
2488 | netif_tx_unlock_bh(np->netdev); | |
2489 | ||
0d160211 | 2490 | netif_carrier_on(np->netdev); |
2688fcb7 AB |
2491 | for (j = 0; j < num_queues; ++j) { |
2492 | queue = &np->queues[j]; | |
f50b4076 | 2493 | |
2688fcb7 AB |
2494 | notify_remote_via_irq(queue->tx_irq); |
2495 | if (queue->tx_irq != queue->rx_irq) | |
2496 | notify_remote_via_irq(queue->rx_irq); | |
2688fcb7 | 2497 | |
f50b4076 DV |
2498 | spin_lock_irq(&queue->tx_lock); |
2499 | xennet_tx_buf_gc(queue); | |
2688fcb7 | 2500 | spin_unlock_irq(&queue->tx_lock); |
f50b4076 DV |
2501 | |
2502 | spin_lock_bh(&queue->rx_lock); | |
2503 | xennet_alloc_rx_buffers(queue); | |
2688fcb7 AB |
2504 | spin_unlock_bh(&queue->rx_lock); |
2505 | } | |
0d160211 JF |
2506 | |
2507 | return 0; | |
2508 | } | |
2509 | ||
80708602 | 2510 | /* |
0d160211 JF |
2511 | * Callback received when the backend's state changes. |
2512 | */ | |
f502bf2b | 2513 | static void netback_changed(struct xenbus_device *dev, |
0d160211 JF |
2514 | enum xenbus_state backend_state) |
2515 | { | |
1b713e00 | 2516 | struct netfront_info *np = dev_get_drvdata(&dev->dev); |
0d160211 JF |
2517 | struct net_device *netdev = np->netdev; |
2518 | ||
2519 | dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state)); | |
2520 | ||
8edfe2e9 JG |
2521 | wake_up_all(&module_wq); |
2522 | ||
0d160211 JF |
2523 | switch (backend_state) { |
2524 | case XenbusStateInitialising: | |
2525 | case XenbusStateInitialised: | |
b78c9512 NI |
2526 | case XenbusStateReconfiguring: |
2527 | case XenbusStateReconfigured: | |
0d160211 | 2528 | case XenbusStateUnknown: |
0d160211 JF |
2529 | break; |
2530 | ||
2531 | case XenbusStateInitWait: | |
2532 | if (dev->state != XenbusStateInitialising) | |
2533 | break; | |
2534 | if (xennet_connect(netdev) != 0) | |
2535 | break; | |
2536 | xenbus_switch_state(dev, XenbusStateConnected); | |
08e34eb1 LE |
2537 | break; |
2538 | ||
2539 | case XenbusStateConnected: | |
ee89bab1 | 2540 | netdev_notify_peers(netdev); |
0d160211 JF |
2541 | break; |
2542 | ||
bce3ea81 DV |
2543 | case XenbusStateClosed: |
2544 | if (dev->state == XenbusStateClosed) | |
2545 | break; | |
df561f66 | 2546 | fallthrough; /* Missed the backend's CLOSING state */ |
0d160211 JF |
2547 | case XenbusStateClosing: |
2548 | xenbus_frontend_closed(dev); | |
2549 | break; | |
2550 | } | |
2551 | } | |
2552 | ||
e0ce4af9 IC |
2553 | static const struct xennet_stat { |
2554 | char name[ETH_GSTRING_LEN]; | |
2555 | u16 offset; | |
2556 | } xennet_stats[] = { | |
2557 | { | |
2558 | "rx_gso_checksum_fixup", | |
2559 | offsetof(struct netfront_info, rx_gso_checksum_fixup) | |
2560 | }, | |
2561 | }; | |
2562 | ||
2563 | static int xennet_get_sset_count(struct net_device *dev, int string_set) | |
2564 | { | |
2565 | switch (string_set) { | |
2566 | case ETH_SS_STATS: | |
2567 | return ARRAY_SIZE(xennet_stats); | |
2568 | default: | |
2569 | return -EINVAL; | |
2570 | } | |
2571 | } | |
2572 | ||
2573 | static void xennet_get_ethtool_stats(struct net_device *dev, | |
2574 | struct ethtool_stats *stats, u64 * data) | |
2575 | { | |
2576 | void *np = netdev_priv(dev); | |
2577 | int i; | |
2578 | ||
2579 | for (i = 0; i < ARRAY_SIZE(xennet_stats); i++) | |
2688fcb7 | 2580 | data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset)); |
e0ce4af9 IC |
2581 | } |
2582 | ||
2583 | static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data) | |
2584 | { | |
2585 | int i; | |
2586 | ||
2587 | switch (stringset) { | |
2588 | case ETH_SS_STATS: | |
2589 | for (i = 0; i < ARRAY_SIZE(xennet_stats); i++) | |
2590 | memcpy(data + i * ETH_GSTRING_LEN, | |
2591 | xennet_stats[i].name, ETH_GSTRING_LEN); | |
2592 | break; | |
2593 | } | |
2594 | } | |
2595 | ||
0fc0b732 | 2596 | static const struct ethtool_ops xennet_ethtool_ops = |
0d160211 | 2597 | { |
0d160211 | 2598 | .get_link = ethtool_op_get_link, |
e0ce4af9 IC |
2599 | |
2600 | .get_sset_count = xennet_get_sset_count, | |
2601 | .get_ethtool_stats = xennet_get_ethtool_stats, | |
2602 | .get_strings = xennet_get_strings, | |
91ffb9d3 | 2603 | .get_ts_info = ethtool_op_get_ts_info, |
0d160211 JF |
2604 | }; |
2605 | ||
2606 | #ifdef CONFIG_SYSFS | |
1f3c2eba DV |
2607 | static ssize_t show_rxbuf(struct device *dev, |
2608 | struct device_attribute *attr, char *buf) | |
0d160211 | 2609 | { |
1f3c2eba | 2610 | return sprintf(buf, "%lu\n", NET_RX_RING_SIZE); |
0d160211 JF |
2611 | } |
2612 | ||
1f3c2eba DV |
2613 | static ssize_t store_rxbuf(struct device *dev, |
2614 | struct device_attribute *attr, | |
2615 | const char *buf, size_t len) | |
0d160211 | 2616 | { |
0d160211 | 2617 | char *endp; |
0d160211 JF |
2618 | |
2619 | if (!capable(CAP_NET_ADMIN)) | |
2620 | return -EPERM; | |
2621 | ||
8ed7ec13 | 2622 | simple_strtoul(buf, &endp, 0); |
0d160211 JF |
2623 | if (endp == buf) |
2624 | return -EBADMSG; | |
2625 | ||
1f3c2eba | 2626 | /* rxbuf_min and rxbuf_max are no longer configurable. */ |
0d160211 | 2627 | |
0d160211 JF |
2628 | return len; |
2629 | } | |
2630 | ||
d61e4038 JP |
2631 | static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf); |
2632 | static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf); | |
2633 | static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL); | |
0d160211 | 2634 | |
27b917e5 TI |
2635 | static struct attribute *xennet_dev_attrs[] = { |
2636 | &dev_attr_rxbuf_min.attr, | |
2637 | &dev_attr_rxbuf_max.attr, | |
2638 | &dev_attr_rxbuf_cur.attr, | |
2639 | NULL | |
2640 | }; | |
0d160211 | 2641 | |
27b917e5 TI |
2642 | static const struct attribute_group xennet_dev_group = { |
2643 | .attrs = xennet_dev_attrs | |
2644 | }; | |
0d160211 JF |
2645 | #endif /* CONFIG_SYSFS */ |
2646 | ||
c2c63310 | 2647 | static void xennet_bus_close(struct xenbus_device *dev) |
0d160211 | 2648 | { |
c2c63310 | 2649 | int ret; |
0d160211 | 2650 | |
c2c63310 AR |
2651 | if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed) |
2652 | return; | |
2653 | do { | |
5b5971df | 2654 | xenbus_switch_state(dev, XenbusStateClosing); |
c2c63310 AR |
2655 | ret = wait_event_timeout(module_wq, |
2656 | xenbus_read_driver_state(dev->otherend) == | |
2657 | XenbusStateClosing || | |
2658 | xenbus_read_driver_state(dev->otherend) == | |
2659 | XenbusStateClosed || | |
2660 | xenbus_read_driver_state(dev->otherend) == | |
2661 | XenbusStateUnknown, | |
2662 | XENNET_TIMEOUT); | |
2663 | } while (!ret); | |
2664 | ||
2665 | if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed) | |
2666 | return; | |
5b5971df | 2667 | |
c2c63310 | 2668 | do { |
5b5971df | 2669 | xenbus_switch_state(dev, XenbusStateClosed); |
c2c63310 AR |
2670 | ret = wait_event_timeout(module_wq, |
2671 | xenbus_read_driver_state(dev->otherend) == | |
2672 | XenbusStateClosed || | |
2673 | xenbus_read_driver_state(dev->otherend) == | |
2674 | XenbusStateUnknown, | |
2675 | XENNET_TIMEOUT); | |
2676 | } while (!ret); | |
2677 | } | |
2678 | ||
2679 | static int xennet_remove(struct xenbus_device *dev) | |
2680 | { | |
2681 | struct netfront_info *info = dev_get_drvdata(&dev->dev); | |
5b5971df | 2682 | |
c2c63310 | 2683 | xennet_bus_close(dev); |
0d160211 JF |
2684 | xennet_disconnect_backend(info); |
2685 | ||
f599c64f RL |
2686 | if (info->netdev->reg_state == NETREG_REGISTERED) |
2687 | unregister_netdev(info->netdev); | |
6bc96d04 | 2688 | |
f599c64f RL |
2689 | if (info->queues) { |
2690 | rtnl_lock(); | |
9a873c71 | 2691 | xennet_destroy_queues(info); |
f599c64f RL |
2692 | rtnl_unlock(); |
2693 | } | |
900e1833 | 2694 | xennet_free_netdev(info->netdev); |
0d160211 JF |
2695 | |
2696 | return 0; | |
2697 | } | |
2698 | ||
95afae48 DV |
2699 | static const struct xenbus_device_id netfront_ids[] = { |
2700 | { "vif" }, | |
2701 | { "" } | |
2702 | }; | |
2703 | ||
2704 | static struct xenbus_driver netfront_driver = { | |
2705 | .ids = netfront_ids, | |
0d160211 | 2706 | .probe = netfront_probe, |
8e0e46bb | 2707 | .remove = xennet_remove, |
0d160211 | 2708 | .resume = netfront_resume, |
f502bf2b | 2709 | .otherend_changed = netback_changed, |
95afae48 | 2710 | }; |
0d160211 JF |
2711 | |
2712 | static int __init netif_init(void) | |
2713 | { | |
6e833587 | 2714 | if (!xen_domain()) |
0d160211 JF |
2715 | return -ENODEV; |
2716 | ||
51c71a3b | 2717 | if (!xen_has_pv_nic_devices()) |
b9136d20 IM |
2718 | return -ENODEV; |
2719 | ||
383eda32 | 2720 | pr_info("Initialising Xen virtual ethernet driver\n"); |
0d160211 | 2721 | |
034702a6 | 2722 | /* Allow as many queues as there are CPUs inut max. 8 if user has not |
32a84405 WL |
2723 | * specified a value. |
2724 | */ | |
2725 | if (xennet_max_queues == 0) | |
034702a6 JG |
2726 | xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT, |
2727 | num_online_cpus()); | |
50ee6061 | 2728 | |
ffb78a26 | 2729 | return xenbus_register_frontend(&netfront_driver); |
0d160211 JF |
2730 | } |
2731 | module_init(netif_init); | |
2732 | ||
2733 | ||
2734 | static void __exit netif_exit(void) | |
2735 | { | |
ffb78a26 | 2736 | xenbus_unregister_driver(&netfront_driver); |
0d160211 JF |
2737 | } |
2738 | module_exit(netif_exit); | |
2739 | ||
2740 | MODULE_DESCRIPTION("Xen virtual network device frontend"); | |
2741 | MODULE_LICENSE("GPL"); | |
d2f0c52b | 2742 | MODULE_ALIAS("xen:vif"); |
4f93f09b | 2743 | MODULE_ALIAS("xennet"); |