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