Commit | Line | Data |
---|---|---|
f942dc25 IC |
1 | /* |
2 | * Back-end of the driver for virtual network devices. This portion of the | |
3 | * driver exports a 'unified' network-device interface that can be accessed | |
4 | * by any operating system that implements a compatible front end. A | |
5 | * reference front-end implementation can be found in: | |
6 | * drivers/net/xen-netfront.c | |
7 | * | |
8 | * Copyright (c) 2002-2005, K A Fraser | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License version 2 | |
12 | * as published by the Free Software Foundation; or, when distributed | |
13 | * separately from the Linux kernel or incorporated into other | |
14 | * software packages, subject to the following license: | |
15 | * | |
16 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
17 | * of this source file (the "Software"), to deal in the Software without | |
18 | * restriction, including without limitation the rights to use, copy, modify, | |
19 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
20 | * and to permit persons to whom the Software is furnished to do so, subject to | |
21 | * the following conditions: | |
22 | * | |
23 | * The above copyright notice and this permission notice shall be included in | |
24 | * all copies or substantial portions of the Software. | |
25 | * | |
26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
31 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
32 | * IN THE SOFTWARE. | |
33 | */ | |
34 | ||
35 | #include "common.h" | |
36 | ||
37 | #include <linux/kthread.h> | |
38 | #include <linux/if_vlan.h> | |
39 | #include <linux/udp.h> | |
e3377f36 | 40 | #include <linux/highmem.h> |
f942dc25 IC |
41 | |
42 | #include <net/tcp.h> | |
43 | ||
ca981633 | 44 | #include <xen/xen.h> |
f942dc25 IC |
45 | #include <xen/events.h> |
46 | #include <xen/interface/memory.h> | |
47 | ||
48 | #include <asm/xen/hypercall.h> | |
49 | #include <asm/xen/page.h> | |
50 | ||
e1f00a69 WL |
51 | /* Provide an option to disable split event channels at load time as |
52 | * event channels are limited resource. Split event channels are | |
53 | * enabled by default. | |
54 | */ | |
55 | bool separate_tx_rx_irq = 1; | |
56 | module_param(separate_tx_rx_irq, bool, 0644); | |
57 | ||
f48da8b1 DV |
58 | /* The time that packets can stay on the guest Rx internal queue |
59 | * before they are dropped. | |
09350788 ZK |
60 | */ |
61 | unsigned int rx_drain_timeout_msecs = 10000; | |
62 | module_param(rx_drain_timeout_msecs, uint, 0444); | |
09350788 | 63 | |
ecf08d2d DV |
64 | /* The length of time before the frontend is considered unresponsive |
65 | * because it isn't providing Rx slots. | |
66 | */ | |
26c0e102 | 67 | unsigned int rx_stall_timeout_msecs = 60000; |
ecf08d2d | 68 | module_param(rx_stall_timeout_msecs, uint, 0444); |
ecf08d2d | 69 | |
8d3d53b3 AB |
70 | unsigned int xenvif_max_queues; |
71 | module_param_named(max_queues, xenvif_max_queues, uint, 0644); | |
72 | MODULE_PARM_DESC(max_queues, | |
73 | "Maximum number of queues per virtual interface"); | |
74 | ||
2810e5b9 WL |
75 | /* |
76 | * This is the maximum slots a skb can have. If a guest sends a skb | |
77 | * which exceeds this limit it is considered malicious. | |
78 | */ | |
37641494 WL |
79 | #define FATAL_SKB_SLOTS_DEFAULT 20 |
80 | static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT; | |
81 | module_param(fatal_skb_slots, uint, 0444); | |
82 | ||
7e5d7753 MC |
83 | /* The amount to copy out of the first guest Tx slot into the skb's |
84 | * linear area. If the first slot has more data, it will be mapped | |
85 | * and put into the first frag. | |
86 | * | |
87 | * This is sized to avoid pulling headers from the frags for most | |
88 | * TCP/IP packets. | |
89 | */ | |
90 | #define XEN_NETBACK_TX_COPY_LEN 128 | |
91 | ||
92 | ||
e9ce7cb6 | 93 | static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx, |
7376419a WL |
94 | u8 status); |
95 | ||
e9ce7cb6 | 96 | static void make_tx_response(struct xenvif_queue *queue, |
f942dc25 IC |
97 | struct xen_netif_tx_request *txp, |
98 | s8 st); | |
c8a4d299 | 99 | static void push_tx_responses(struct xenvif_queue *queue); |
b3f980bd | 100 | |
e9ce7cb6 | 101 | static inline int tx_work_todo(struct xenvif_queue *queue); |
b3f980bd | 102 | |
e9ce7cb6 | 103 | static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue, |
f942dc25 IC |
104 | u16 id, |
105 | s8 st, | |
106 | u16 offset, | |
107 | u16 size, | |
108 | u16 flags); | |
109 | ||
e9ce7cb6 | 110 | static inline unsigned long idx_to_pfn(struct xenvif_queue *queue, |
ea066ad1 | 111 | u16 idx) |
f942dc25 | 112 | { |
e9ce7cb6 | 113 | return page_to_pfn(queue->mmap_pages[idx]); |
f942dc25 IC |
114 | } |
115 | ||
e9ce7cb6 | 116 | static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue, |
ea066ad1 | 117 | u16 idx) |
f942dc25 | 118 | { |
e9ce7cb6 | 119 | return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx)); |
f942dc25 IC |
120 | } |
121 | ||
7aceb47a ZK |
122 | #define callback_param(vif, pending_idx) \ |
123 | (vif->pending_tx_info[pending_idx].callback_struct) | |
124 | ||
f53c3fe8 ZK |
125 | /* Find the containing VIF's structure from a pointer in pending_tx_info array |
126 | */ | |
e9ce7cb6 | 127 | static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf) |
3e2234b3 | 128 | { |
f53c3fe8 ZK |
129 | u16 pending_idx = ubuf->desc; |
130 | struct pending_tx_info *temp = | |
131 | container_of(ubuf, struct pending_tx_info, callback_struct); | |
132 | return container_of(temp - pending_idx, | |
e9ce7cb6 | 133 | struct xenvif_queue, |
f53c3fe8 | 134 | pending_tx_info[0]); |
3e2234b3 | 135 | } |
f53c3fe8 | 136 | |
ea066ad1 IC |
137 | static u16 frag_get_pending_idx(skb_frag_t *frag) |
138 | { | |
139 | return (u16)frag->page_offset; | |
140 | } | |
141 | ||
142 | static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx) | |
143 | { | |
144 | frag->page_offset = pending_idx; | |
145 | } | |
146 | ||
f942dc25 IC |
147 | static inline pending_ring_idx_t pending_index(unsigned i) |
148 | { | |
149 | return i & (MAX_PENDING_REQS-1); | |
150 | } | |
151 | ||
e9ce7cb6 | 152 | bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed) |
f942dc25 | 153 | { |
ca2f09f2 | 154 | RING_IDX prod, cons; |
f942dc25 | 155 | |
ca2f09f2 | 156 | do { |
e9ce7cb6 WL |
157 | prod = queue->rx.sring->req_prod; |
158 | cons = queue->rx.req_cons; | |
f942dc25 | 159 | |
ca2f09f2 PD |
160 | if (prod - cons >= needed) |
161 | return true; | |
f942dc25 | 162 | |
e9ce7cb6 | 163 | queue->rx.sring->req_event = prod + 1; |
f942dc25 | 164 | |
ca2f09f2 PD |
165 | /* Make sure event is visible before we check prod |
166 | * again. | |
167 | */ | |
168 | mb(); | |
e9ce7cb6 | 169 | } while (queue->rx.sring->req_prod != prod); |
f942dc25 | 170 | |
ca2f09f2 | 171 | return false; |
f942dc25 IC |
172 | } |
173 | ||
f48da8b1 DV |
174 | void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb) |
175 | { | |
176 | unsigned long flags; | |
177 | ||
178 | spin_lock_irqsave(&queue->rx_queue.lock, flags); | |
179 | ||
180 | __skb_queue_tail(&queue->rx_queue, skb); | |
181 | ||
182 | queue->rx_queue_len += skb->len; | |
183 | if (queue->rx_queue_len > queue->rx_queue_max) | |
184 | netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id)); | |
185 | ||
186 | spin_unlock_irqrestore(&queue->rx_queue.lock, flags); | |
187 | } | |
188 | ||
189 | static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue) | |
190 | { | |
191 | struct sk_buff *skb; | |
192 | ||
193 | spin_lock_irq(&queue->rx_queue.lock); | |
194 | ||
195 | skb = __skb_dequeue(&queue->rx_queue); | |
196 | if (skb) | |
197 | queue->rx_queue_len -= skb->len; | |
198 | ||
199 | spin_unlock_irq(&queue->rx_queue.lock); | |
200 | ||
201 | return skb; | |
202 | } | |
203 | ||
204 | static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue) | |
205 | { | |
206 | spin_lock_irq(&queue->rx_queue.lock); | |
207 | ||
208 | if (queue->rx_queue_len < queue->rx_queue_max) | |
209 | netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id)); | |
210 | ||
211 | spin_unlock_irq(&queue->rx_queue.lock); | |
212 | } | |
213 | ||
214 | ||
215 | static void xenvif_rx_queue_purge(struct xenvif_queue *queue) | |
216 | { | |
217 | struct sk_buff *skb; | |
218 | while ((skb = xenvif_rx_dequeue(queue)) != NULL) | |
219 | kfree_skb(skb); | |
220 | } | |
221 | ||
222 | static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue) | |
223 | { | |
224 | struct sk_buff *skb; | |
225 | ||
226 | for(;;) { | |
227 | skb = skb_peek(&queue->rx_queue); | |
228 | if (!skb) | |
229 | break; | |
230 | if (time_before(jiffies, XENVIF_RX_CB(skb)->expires)) | |
231 | break; | |
232 | xenvif_rx_dequeue(queue); | |
233 | kfree_skb(skb); | |
234 | } | |
235 | } | |
236 | ||
f942dc25 IC |
237 | struct netrx_pending_operations { |
238 | unsigned copy_prod, copy_cons; | |
239 | unsigned meta_prod, meta_cons; | |
240 | struct gnttab_copy *copy; | |
b3f980bd | 241 | struct xenvif_rx_meta *meta; |
f942dc25 IC |
242 | int copy_off; |
243 | grant_ref_t copy_gref; | |
244 | }; | |
245 | ||
e9ce7cb6 | 246 | static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue, |
b3f980bd | 247 | struct netrx_pending_operations *npo) |
f942dc25 | 248 | { |
b3f980bd | 249 | struct xenvif_rx_meta *meta; |
f942dc25 IC |
250 | struct xen_netif_rx_request *req; |
251 | ||
e9ce7cb6 | 252 | req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++); |
f942dc25 IC |
253 | |
254 | meta = npo->meta + npo->meta_prod++; | |
82cada22 | 255 | meta->gso_type = XEN_NETIF_GSO_TYPE_NONE; |
f942dc25 IC |
256 | meta->gso_size = 0; |
257 | meta->size = 0; | |
258 | meta->id = req->id; | |
259 | ||
260 | npo->copy_off = 0; | |
261 | npo->copy_gref = req->gref; | |
262 | ||
263 | return meta; | |
264 | } | |
265 | ||
33bc801d WL |
266 | /* |
267 | * Set up the grant operations for this fragment. If it's a flipping | |
268 | * interface, we also set up the unmap request from here. | |
269 | */ | |
e9ce7cb6 | 270 | static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb, |
7376419a WL |
271 | struct netrx_pending_operations *npo, |
272 | struct page *page, unsigned long size, | |
c2677a6f | 273 | unsigned long offset, int *head) |
f942dc25 IC |
274 | { |
275 | struct gnttab_copy *copy_gop; | |
b3f980bd | 276 | struct xenvif_rx_meta *meta; |
f942dc25 | 277 | unsigned long bytes; |
5bd07670 | 278 | int gso_type = XEN_NETIF_GSO_TYPE_NONE; |
f942dc25 IC |
279 | |
280 | /* Data must not cross a page boundary. */ | |
6a8ed462 | 281 | BUG_ON(size + offset > PAGE_SIZE<<compound_order(page)); |
f942dc25 IC |
282 | |
283 | meta = npo->meta + npo->meta_prod - 1; | |
284 | ||
6a8ed462 IC |
285 | /* Skip unused frames from start of page */ |
286 | page += offset >> PAGE_SHIFT; | |
287 | offset &= ~PAGE_MASK; | |
288 | ||
f942dc25 | 289 | while (size > 0) { |
c2677a6f JH |
290 | struct xen_page_foreign *foreign; |
291 | ||
6a8ed462 | 292 | BUG_ON(offset >= PAGE_SIZE); |
f942dc25 IC |
293 | BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET); |
294 | ||
1650d545 DV |
295 | if (npo->copy_off == MAX_BUFFER_OFFSET) |
296 | meta = get_next_rx_buffer(queue, npo); | |
6a8ed462 | 297 | |
1650d545 | 298 | bytes = PAGE_SIZE - offset; |
6a8ed462 IC |
299 | if (bytes > size) |
300 | bytes = size; | |
301 | ||
f942dc25 IC |
302 | if (npo->copy_off + bytes > MAX_BUFFER_OFFSET) |
303 | bytes = MAX_BUFFER_OFFSET - npo->copy_off; | |
304 | ||
305 | copy_gop = npo->copy + npo->copy_prod++; | |
306 | copy_gop->flags = GNTCOPY_dest_gref; | |
b3f980bd WL |
307 | copy_gop->len = bytes; |
308 | ||
c2677a6f JH |
309 | foreign = xen_page_foreign(page); |
310 | if (foreign) { | |
311 | copy_gop->source.domid = foreign->domid; | |
312 | copy_gop->source.u.ref = foreign->gref; | |
3e2234b3 ZK |
313 | copy_gop->flags |= GNTCOPY_source_gref; |
314 | } else { | |
315 | copy_gop->source.domid = DOMID_SELF; | |
316 | copy_gop->source.u.gmfn = | |
317 | virt_to_mfn(page_address(page)); | |
318 | } | |
f942dc25 | 319 | copy_gop->source.offset = offset; |
f942dc25 | 320 | |
e9ce7cb6 | 321 | copy_gop->dest.domid = queue->vif->domid; |
f942dc25 IC |
322 | copy_gop->dest.offset = npo->copy_off; |
323 | copy_gop->dest.u.ref = npo->copy_gref; | |
f942dc25 IC |
324 | |
325 | npo->copy_off += bytes; | |
326 | meta->size += bytes; | |
327 | ||
328 | offset += bytes; | |
329 | size -= bytes; | |
330 | ||
6a8ed462 IC |
331 | /* Next frame */ |
332 | if (offset == PAGE_SIZE && size) { | |
333 | BUG_ON(!PageCompound(page)); | |
334 | page++; | |
335 | offset = 0; | |
336 | } | |
337 | ||
f942dc25 | 338 | /* Leave a gap for the GSO descriptor. */ |
5bd07670 AL |
339 | if (skb_is_gso(skb)) { |
340 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) | |
341 | gso_type = XEN_NETIF_GSO_TYPE_TCPV4; | |
342 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) | |
343 | gso_type = XEN_NETIF_GSO_TYPE_TCPV6; | |
344 | } | |
82cada22 | 345 | |
e9ce7cb6 WL |
346 | if (*head && ((1 << gso_type) & queue->vif->gso_mask)) |
347 | queue->rx.req_cons++; | |
f942dc25 | 348 | |
33bc801d | 349 | *head = 0; /* There must be something in this buffer now. */ |
f942dc25 IC |
350 | |
351 | } | |
352 | } | |
353 | ||
354 | /* | |
355 | * Prepare an SKB to be transmitted to the frontend. | |
356 | * | |
357 | * This function is responsible for allocating grant operations, meta | |
358 | * structures, etc. | |
359 | * | |
360 | * It returns the number of meta structures consumed. The number of | |
361 | * ring slots used is always equal to the number of meta slots used | |
362 | * plus the number of GSO descriptors used. Currently, we use either | |
363 | * zero GSO descriptors (for non-GSO packets) or one descriptor (for | |
364 | * frontend-side LRO). | |
365 | */ | |
7376419a | 366 | static int xenvif_gop_skb(struct sk_buff *skb, |
e9ce7cb6 WL |
367 | struct netrx_pending_operations *npo, |
368 | struct xenvif_queue *queue) | |
f942dc25 IC |
369 | { |
370 | struct xenvif *vif = netdev_priv(skb->dev); | |
371 | int nr_frags = skb_shinfo(skb)->nr_frags; | |
372 | int i; | |
373 | struct xen_netif_rx_request *req; | |
b3f980bd | 374 | struct xenvif_rx_meta *meta; |
f942dc25 | 375 | unsigned char *data; |
33bc801d | 376 | int head = 1; |
f942dc25 | 377 | int old_meta_prod; |
82cada22 | 378 | int gso_type; |
f942dc25 IC |
379 | |
380 | old_meta_prod = npo->meta_prod; | |
381 | ||
5bd07670 AL |
382 | gso_type = XEN_NETIF_GSO_TYPE_NONE; |
383 | if (skb_is_gso(skb)) { | |
384 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) | |
385 | gso_type = XEN_NETIF_GSO_TYPE_TCPV4; | |
386 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) | |
387 | gso_type = XEN_NETIF_GSO_TYPE_TCPV6; | |
82cada22 PD |
388 | } |
389 | ||
f942dc25 | 390 | /* Set up a GSO prefix descriptor, if necessary */ |
a3314f3d | 391 | if ((1 << gso_type) & vif->gso_prefix_mask) { |
e9ce7cb6 | 392 | req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++); |
f942dc25 | 393 | meta = npo->meta + npo->meta_prod++; |
82cada22 | 394 | meta->gso_type = gso_type; |
5bd07670 | 395 | meta->gso_size = skb_shinfo(skb)->gso_size; |
f942dc25 IC |
396 | meta->size = 0; |
397 | meta->id = req->id; | |
398 | } | |
399 | ||
e9ce7cb6 | 400 | req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++); |
f942dc25 IC |
401 | meta = npo->meta + npo->meta_prod++; |
402 | ||
82cada22 PD |
403 | if ((1 << gso_type) & vif->gso_mask) { |
404 | meta->gso_type = gso_type; | |
5bd07670 | 405 | meta->gso_size = skb_shinfo(skb)->gso_size; |
82cada22 PD |
406 | } else { |
407 | meta->gso_type = XEN_NETIF_GSO_TYPE_NONE; | |
f942dc25 | 408 | meta->gso_size = 0; |
82cada22 | 409 | } |
f942dc25 IC |
410 | |
411 | meta->size = 0; | |
412 | meta->id = req->id; | |
413 | npo->copy_off = 0; | |
414 | npo->copy_gref = req->gref; | |
415 | ||
416 | data = skb->data; | |
417 | while (data < skb_tail_pointer(skb)) { | |
418 | unsigned int offset = offset_in_page(data); | |
419 | unsigned int len = PAGE_SIZE - offset; | |
420 | ||
421 | if (data + len > skb_tail_pointer(skb)) | |
422 | len = skb_tail_pointer(skb) - data; | |
423 | ||
e9ce7cb6 | 424 | xenvif_gop_frag_copy(queue, skb, npo, |
c2677a6f | 425 | virt_to_page(data), len, offset, &head); |
f942dc25 IC |
426 | data += len; |
427 | } | |
428 | ||
429 | for (i = 0; i < nr_frags; i++) { | |
e9ce7cb6 | 430 | xenvif_gop_frag_copy(queue, skb, npo, |
7376419a WL |
431 | skb_frag_page(&skb_shinfo(skb)->frags[i]), |
432 | skb_frag_size(&skb_shinfo(skb)->frags[i]), | |
433 | skb_shinfo(skb)->frags[i].page_offset, | |
c2677a6f | 434 | &head); |
f942dc25 IC |
435 | } |
436 | ||
437 | return npo->meta_prod - old_meta_prod; | |
438 | } | |
439 | ||
440 | /* | |
7376419a | 441 | * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was |
f942dc25 IC |
442 | * used to set up the operations on the top of |
443 | * netrx_pending_operations, which have since been done. Check that | |
444 | * they didn't give any errors and advance over them. | |
445 | */ | |
7376419a WL |
446 | static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots, |
447 | struct netrx_pending_operations *npo) | |
f942dc25 IC |
448 | { |
449 | struct gnttab_copy *copy_op; | |
450 | int status = XEN_NETIF_RSP_OKAY; | |
451 | int i; | |
452 | ||
453 | for (i = 0; i < nr_meta_slots; i++) { | |
454 | copy_op = npo->copy + npo->copy_cons++; | |
455 | if (copy_op->status != GNTST_okay) { | |
456 | netdev_dbg(vif->dev, | |
457 | "Bad status %d from copy to DOM%d.\n", | |
458 | copy_op->status, vif->domid); | |
459 | status = XEN_NETIF_RSP_ERROR; | |
460 | } | |
461 | } | |
462 | ||
463 | return status; | |
464 | } | |
465 | ||
e9ce7cb6 | 466 | static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status, |
7376419a WL |
467 | struct xenvif_rx_meta *meta, |
468 | int nr_meta_slots) | |
f942dc25 IC |
469 | { |
470 | int i; | |
471 | unsigned long offset; | |
472 | ||
473 | /* No fragments used */ | |
474 | if (nr_meta_slots <= 1) | |
475 | return; | |
476 | ||
477 | nr_meta_slots--; | |
478 | ||
479 | for (i = 0; i < nr_meta_slots; i++) { | |
480 | int flags; | |
481 | if (i == nr_meta_slots - 1) | |
482 | flags = 0; | |
483 | else | |
484 | flags = XEN_NETRXF_more_data; | |
485 | ||
486 | offset = 0; | |
e9ce7cb6 | 487 | make_rx_response(queue, meta[i].id, status, offset, |
f942dc25 IC |
488 | meta[i].size, flags); |
489 | } | |
490 | } | |
491 | ||
e9ce7cb6 | 492 | void xenvif_kick_thread(struct xenvif_queue *queue) |
b3f980bd | 493 | { |
e9ce7cb6 | 494 | wake_up(&queue->wq); |
b3f980bd WL |
495 | } |
496 | ||
e9ce7cb6 | 497 | static void xenvif_rx_action(struct xenvif_queue *queue) |
f942dc25 | 498 | { |
f942dc25 | 499 | s8 status; |
e1f00a69 | 500 | u16 flags; |
f942dc25 IC |
501 | struct xen_netif_rx_response *resp; |
502 | struct sk_buff_head rxq; | |
503 | struct sk_buff *skb; | |
504 | LIST_HEAD(notify); | |
505 | int ret; | |
f942dc25 | 506 | unsigned long offset; |
11b57f90 | 507 | bool need_to_notify = false; |
f942dc25 IC |
508 | |
509 | struct netrx_pending_operations npo = { | |
e9ce7cb6 WL |
510 | .copy = queue->grant_copy_op, |
511 | .meta = queue->meta, | |
f942dc25 IC |
512 | }; |
513 | ||
514 | skb_queue_head_init(&rxq); | |
515 | ||
f48da8b1 DV |
516 | while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX) |
517 | && (skb = xenvif_rx_dequeue(queue)) != NULL) { | |
1425c7a4 PD |
518 | RING_IDX old_req_cons; |
519 | RING_IDX ring_slots_used; | |
ca2f09f2 | 520 | |
ecf08d2d DV |
521 | queue->last_rx_time = jiffies; |
522 | ||
e9ce7cb6 WL |
523 | old_req_cons = queue->rx.req_cons; |
524 | XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue); | |
525 | ring_slots_used = queue->rx.req_cons - old_req_cons; | |
1425c7a4 | 526 | |
f942dc25 | 527 | __skb_queue_tail(&rxq, skb); |
f942dc25 IC |
528 | } |
529 | ||
e9ce7cb6 | 530 | BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta)); |
f942dc25 IC |
531 | |
532 | if (!npo.copy_prod) | |
ca2f09f2 | 533 | goto done; |
f942dc25 | 534 | |
ac3d5ac2 | 535 | BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS); |
e9ce7cb6 | 536 | gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod); |
f942dc25 IC |
537 | |
538 | while ((skb = __skb_dequeue(&rxq)) != NULL) { | |
f942dc25 | 539 | |
e9ce7cb6 WL |
540 | if ((1 << queue->meta[npo.meta_cons].gso_type) & |
541 | queue->vif->gso_prefix_mask) { | |
542 | resp = RING_GET_RESPONSE(&queue->rx, | |
543 | queue->rx.rsp_prod_pvt++); | |
f942dc25 IC |
544 | |
545 | resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data; | |
546 | ||
e9ce7cb6 WL |
547 | resp->offset = queue->meta[npo.meta_cons].gso_size; |
548 | resp->id = queue->meta[npo.meta_cons].id; | |
8f13dd96 | 549 | resp->status = XENVIF_RX_CB(skb)->meta_slots_used; |
f942dc25 IC |
550 | |
551 | npo.meta_cons++; | |
8f13dd96 | 552 | XENVIF_RX_CB(skb)->meta_slots_used--; |
f942dc25 IC |
553 | } |
554 | ||
555 | ||
e9ce7cb6 WL |
556 | queue->stats.tx_bytes += skb->len; |
557 | queue->stats.tx_packets++; | |
f942dc25 | 558 | |
e9ce7cb6 | 559 | status = xenvif_check_gop(queue->vif, |
8f13dd96 ZK |
560 | XENVIF_RX_CB(skb)->meta_slots_used, |
561 | &npo); | |
f942dc25 | 562 | |
8f13dd96 | 563 | if (XENVIF_RX_CB(skb)->meta_slots_used == 1) |
f942dc25 IC |
564 | flags = 0; |
565 | else | |
566 | flags = XEN_NETRXF_more_data; | |
567 | ||
568 | if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */ | |
569 | flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated; | |
570 | else if (skb->ip_summed == CHECKSUM_UNNECESSARY) | |
571 | /* remote but checksummed. */ | |
572 | flags |= XEN_NETRXF_data_validated; | |
573 | ||
574 | offset = 0; | |
e9ce7cb6 | 575 | resp = make_rx_response(queue, queue->meta[npo.meta_cons].id, |
f942dc25 | 576 | status, offset, |
e9ce7cb6 | 577 | queue->meta[npo.meta_cons].size, |
f942dc25 IC |
578 | flags); |
579 | ||
e9ce7cb6 WL |
580 | if ((1 << queue->meta[npo.meta_cons].gso_type) & |
581 | queue->vif->gso_mask) { | |
f942dc25 IC |
582 | struct xen_netif_extra_info *gso = |
583 | (struct xen_netif_extra_info *) | |
e9ce7cb6 WL |
584 | RING_GET_RESPONSE(&queue->rx, |
585 | queue->rx.rsp_prod_pvt++); | |
f942dc25 IC |
586 | |
587 | resp->flags |= XEN_NETRXF_extra_info; | |
588 | ||
e9ce7cb6 WL |
589 | gso->u.gso.type = queue->meta[npo.meta_cons].gso_type; |
590 | gso->u.gso.size = queue->meta[npo.meta_cons].gso_size; | |
f942dc25 IC |
591 | gso->u.gso.pad = 0; |
592 | gso->u.gso.features = 0; | |
593 | ||
594 | gso->type = XEN_NETIF_EXTRA_TYPE_GSO; | |
595 | gso->flags = 0; | |
596 | } | |
597 | ||
e9ce7cb6 WL |
598 | xenvif_add_frag_responses(queue, status, |
599 | queue->meta + npo.meta_cons + 1, | |
8f13dd96 | 600 | XENVIF_RX_CB(skb)->meta_slots_used); |
f942dc25 | 601 | |
e9ce7cb6 | 602 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret); |
f942dc25 | 603 | |
11b57f90 | 604 | need_to_notify |= !!ret; |
b3f980bd | 605 | |
8f13dd96 | 606 | npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used; |
f942dc25 IC |
607 | dev_kfree_skb(skb); |
608 | } | |
609 | ||
ca2f09f2 | 610 | done: |
b3f980bd | 611 | if (need_to_notify) |
e9ce7cb6 | 612 | notify_remote_via_irq(queue->rx_irq); |
f942dc25 IC |
613 | } |
614 | ||
e9ce7cb6 | 615 | void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue) |
f942dc25 IC |
616 | { |
617 | int more_to_do; | |
618 | ||
e9ce7cb6 | 619 | RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do); |
f942dc25 IC |
620 | |
621 | if (more_to_do) | |
e9ce7cb6 | 622 | napi_schedule(&queue->napi); |
f942dc25 IC |
623 | } |
624 | ||
e9ce7cb6 | 625 | static void tx_add_credit(struct xenvif_queue *queue) |
f942dc25 IC |
626 | { |
627 | unsigned long max_burst, max_credit; | |
628 | ||
629 | /* | |
630 | * Allow a burst big enough to transmit a jumbo packet of up to 128kB. | |
631 | * Otherwise the interface can seize up due to insufficient credit. | |
632 | */ | |
e9ce7cb6 | 633 | max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size; |
f942dc25 | 634 | max_burst = min(max_burst, 131072UL); |
e9ce7cb6 | 635 | max_burst = max(max_burst, queue->credit_bytes); |
f942dc25 IC |
636 | |
637 | /* Take care that adding a new chunk of credit doesn't wrap to zero. */ | |
e9ce7cb6 WL |
638 | max_credit = queue->remaining_credit + queue->credit_bytes; |
639 | if (max_credit < queue->remaining_credit) | |
f942dc25 IC |
640 | max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */ |
641 | ||
e9ce7cb6 | 642 | queue->remaining_credit = min(max_credit, max_burst); |
f942dc25 IC |
643 | } |
644 | ||
645 | static void tx_credit_callback(unsigned long data) | |
646 | { | |
e9ce7cb6 WL |
647 | struct xenvif_queue *queue = (struct xenvif_queue *)data; |
648 | tx_add_credit(queue); | |
649 | xenvif_napi_schedule_or_enable_events(queue); | |
f942dc25 IC |
650 | } |
651 | ||
e9ce7cb6 | 652 | static void xenvif_tx_err(struct xenvif_queue *queue, |
7376419a | 653 | struct xen_netif_tx_request *txp, RING_IDX end) |
f942dc25 | 654 | { |
e9ce7cb6 | 655 | RING_IDX cons = queue->tx.req_cons; |
f53c3fe8 | 656 | unsigned long flags; |
f942dc25 IC |
657 | |
658 | do { | |
e9ce7cb6 WL |
659 | spin_lock_irqsave(&queue->response_lock, flags); |
660 | make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR); | |
c8a4d299 | 661 | push_tx_responses(queue); |
e9ce7cb6 | 662 | spin_unlock_irqrestore(&queue->response_lock, flags); |
b9149729 | 663 | if (cons == end) |
f942dc25 | 664 | break; |
e9ce7cb6 | 665 | txp = RING_GET_REQUEST(&queue->tx, cons++); |
f942dc25 | 666 | } while (1); |
e9ce7cb6 | 667 | queue->tx.req_cons = cons; |
f942dc25 IC |
668 | } |
669 | ||
7376419a | 670 | static void xenvif_fatal_tx_err(struct xenvif *vif) |
48856286 IC |
671 | { |
672 | netdev_err(vif->dev, "fatal error; disabling device\n"); | |
e9d8b2c2 | 673 | vif->disabled = true; |
e9ce7cb6 WL |
674 | /* Disable the vif from queue 0's kthread */ |
675 | if (vif->queues) | |
676 | xenvif_kick_thread(&vif->queues[0]); | |
48856286 IC |
677 | } |
678 | ||
e9ce7cb6 | 679 | static int xenvif_count_requests(struct xenvif_queue *queue, |
7376419a WL |
680 | struct xen_netif_tx_request *first, |
681 | struct xen_netif_tx_request *txp, | |
682 | int work_to_do) | |
f942dc25 | 683 | { |
e9ce7cb6 | 684 | RING_IDX cons = queue->tx.req_cons; |
2810e5b9 WL |
685 | int slots = 0; |
686 | int drop_err = 0; | |
59ccb4eb | 687 | int more_data; |
f942dc25 IC |
688 | |
689 | if (!(first->flags & XEN_NETTXF_more_data)) | |
690 | return 0; | |
691 | ||
692 | do { | |
59ccb4eb WL |
693 | struct xen_netif_tx_request dropped_tx = { 0 }; |
694 | ||
2810e5b9 | 695 | if (slots >= work_to_do) { |
e9ce7cb6 | 696 | netdev_err(queue->vif->dev, |
2810e5b9 WL |
697 | "Asked for %d slots but exceeds this limit\n", |
698 | work_to_do); | |
e9ce7cb6 | 699 | xenvif_fatal_tx_err(queue->vif); |
35876b5f | 700 | return -ENODATA; |
f942dc25 IC |
701 | } |
702 | ||
2810e5b9 WL |
703 | /* This guest is really using too many slots and |
704 | * considered malicious. | |
705 | */ | |
37641494 | 706 | if (unlikely(slots >= fatal_skb_slots)) { |
e9ce7cb6 | 707 | netdev_err(queue->vif->dev, |
2810e5b9 | 708 | "Malicious frontend using %d slots, threshold %u\n", |
37641494 | 709 | slots, fatal_skb_slots); |
e9ce7cb6 | 710 | xenvif_fatal_tx_err(queue->vif); |
35876b5f | 711 | return -E2BIG; |
f942dc25 IC |
712 | } |
713 | ||
2810e5b9 | 714 | /* Xen network protocol had implicit dependency on |
37641494 WL |
715 | * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to |
716 | * the historical MAX_SKB_FRAGS value 18 to honor the | |
717 | * same behavior as before. Any packet using more than | |
718 | * 18 slots but less than fatal_skb_slots slots is | |
719 | * dropped | |
2810e5b9 | 720 | */ |
37641494 | 721 | if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) { |
2810e5b9 | 722 | if (net_ratelimit()) |
e9ce7cb6 | 723 | netdev_dbg(queue->vif->dev, |
2810e5b9 | 724 | "Too many slots (%d) exceeding limit (%d), dropping packet\n", |
37641494 | 725 | slots, XEN_NETBK_LEGACY_SLOTS_MAX); |
2810e5b9 WL |
726 | drop_err = -E2BIG; |
727 | } | |
728 | ||
59ccb4eb WL |
729 | if (drop_err) |
730 | txp = &dropped_tx; | |
731 | ||
e9ce7cb6 | 732 | memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots), |
f942dc25 | 733 | sizeof(*txp)); |
03393fd5 WL |
734 | |
735 | /* If the guest submitted a frame >= 64 KiB then | |
736 | * first->size overflowed and following slots will | |
737 | * appear to be larger than the frame. | |
738 | * | |
739 | * This cannot be fatal error as there are buggy | |
740 | * frontends that do this. | |
741 | * | |
742 | * Consume all slots and drop the packet. | |
743 | */ | |
744 | if (!drop_err && txp->size > first->size) { | |
745 | if (net_ratelimit()) | |
e9ce7cb6 | 746 | netdev_dbg(queue->vif->dev, |
03393fd5 WL |
747 | "Invalid tx request, slot size %u > remaining size %u\n", |
748 | txp->size, first->size); | |
749 | drop_err = -EIO; | |
f942dc25 IC |
750 | } |
751 | ||
752 | first->size -= txp->size; | |
2810e5b9 | 753 | slots++; |
f942dc25 IC |
754 | |
755 | if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) { | |
e9ce7cb6 | 756 | netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n", |
f942dc25 | 757 | txp->offset, txp->size); |
e9ce7cb6 | 758 | xenvif_fatal_tx_err(queue->vif); |
35876b5f | 759 | return -EINVAL; |
f942dc25 | 760 | } |
59ccb4eb WL |
761 | |
762 | more_data = txp->flags & XEN_NETTXF_more_data; | |
763 | ||
764 | if (!drop_err) | |
765 | txp++; | |
766 | ||
767 | } while (more_data); | |
2810e5b9 WL |
768 | |
769 | if (drop_err) { | |
e9ce7cb6 | 770 | xenvif_tx_err(queue, first, cons + slots); |
2810e5b9 WL |
771 | return drop_err; |
772 | } | |
773 | ||
774 | return slots; | |
f942dc25 IC |
775 | } |
776 | ||
8f13dd96 ZK |
777 | |
778 | struct xenvif_tx_cb { | |
779 | u16 pending_idx; | |
780 | }; | |
781 | ||
782 | #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb) | |
783 | ||
e9ce7cb6 | 784 | static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue, |
9074ce24 ZK |
785 | u16 pending_idx, |
786 | struct xen_netif_tx_request *txp, | |
787 | struct gnttab_map_grant_ref *mop) | |
f53c3fe8 | 788 | { |
e9ce7cb6 WL |
789 | queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx]; |
790 | gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx), | |
f53c3fe8 | 791 | GNTMAP_host_map | GNTMAP_readonly, |
e9ce7cb6 | 792 | txp->gref, queue->vif->domid); |
f53c3fe8 | 793 | |
e9ce7cb6 | 794 | memcpy(&queue->pending_tx_info[pending_idx].req, txp, |
f53c3fe8 ZK |
795 | sizeof(*txp)); |
796 | } | |
797 | ||
e3377f36 ZK |
798 | static inline struct sk_buff *xenvif_alloc_skb(unsigned int size) |
799 | { | |
800 | struct sk_buff *skb = | |
801 | alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN, | |
802 | GFP_ATOMIC | __GFP_NOWARN); | |
803 | if (unlikely(skb == NULL)) | |
804 | return NULL; | |
805 | ||
806 | /* Packets passed to netif_rx() must have some headroom. */ | |
807 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); | |
808 | ||
809 | /* Initialize it here to avoid later surprises */ | |
810 | skb_shinfo(skb)->destructor_arg = NULL; | |
811 | ||
812 | return skb; | |
813 | } | |
814 | ||
e9ce7cb6 | 815 | static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue, |
f53c3fe8 ZK |
816 | struct sk_buff *skb, |
817 | struct xen_netif_tx_request *txp, | |
818 | struct gnttab_map_grant_ref *gop) | |
f942dc25 IC |
819 | { |
820 | struct skb_shared_info *shinfo = skb_shinfo(skb); | |
821 | skb_frag_t *frags = shinfo->frags; | |
8f13dd96 | 822 | u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx; |
62bad319 ZK |
823 | int start; |
824 | pending_ring_idx_t index; | |
e3377f36 | 825 | unsigned int nr_slots, frag_overflow = 0; |
2810e5b9 WL |
826 | |
827 | /* At this point shinfo->nr_frags is in fact the number of | |
37641494 | 828 | * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX. |
2810e5b9 | 829 | */ |
e3377f36 ZK |
830 | if (shinfo->nr_frags > MAX_SKB_FRAGS) { |
831 | frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS; | |
832 | BUG_ON(frag_overflow > MAX_SKB_FRAGS); | |
833 | shinfo->nr_frags = MAX_SKB_FRAGS; | |
834 | } | |
2810e5b9 | 835 | nr_slots = shinfo->nr_frags; |
f942dc25 IC |
836 | |
837 | /* Skip first skb fragment if it is on same page as header fragment. */ | |
ea066ad1 | 838 | start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx); |
f942dc25 | 839 | |
f53c3fe8 ZK |
840 | for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots; |
841 | shinfo->nr_frags++, txp++, gop++) { | |
e9ce7cb6 WL |
842 | index = pending_index(queue->pending_cons++); |
843 | pending_idx = queue->pending_ring[index]; | |
844 | xenvif_tx_create_map_op(queue, pending_idx, txp, gop); | |
f53c3fe8 | 845 | frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx); |
f942dc25 IC |
846 | } |
847 | ||
e3377f36 ZK |
848 | if (frag_overflow) { |
849 | struct sk_buff *nskb = xenvif_alloc_skb(0); | |
850 | if (unlikely(nskb == NULL)) { | |
851 | if (net_ratelimit()) | |
e9ce7cb6 | 852 | netdev_err(queue->vif->dev, |
e3377f36 ZK |
853 | "Can't allocate the frag_list skb.\n"); |
854 | return NULL; | |
855 | } | |
856 | ||
857 | shinfo = skb_shinfo(nskb); | |
858 | frags = shinfo->frags; | |
859 | ||
860 | for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow; | |
861 | shinfo->nr_frags++, txp++, gop++) { | |
e9ce7cb6 WL |
862 | index = pending_index(queue->pending_cons++); |
863 | pending_idx = queue->pending_ring[index]; | |
864 | xenvif_tx_create_map_op(queue, pending_idx, txp, gop); | |
e3377f36 ZK |
865 | frag_set_pending_idx(&frags[shinfo->nr_frags], |
866 | pending_idx); | |
867 | } | |
868 | ||
869 | skb_shinfo(skb)->frag_list = nskb; | |
870 | } | |
2810e5b9 | 871 | |
f942dc25 IC |
872 | return gop; |
873 | } | |
874 | ||
e9ce7cb6 | 875 | static inline void xenvif_grant_handle_set(struct xenvif_queue *queue, |
f53c3fe8 ZK |
876 | u16 pending_idx, |
877 | grant_handle_t handle) | |
878 | { | |
e9ce7cb6 | 879 | if (unlikely(queue->grant_tx_handle[pending_idx] != |
f53c3fe8 | 880 | NETBACK_INVALID_HANDLE)) { |
e9ce7cb6 | 881 | netdev_err(queue->vif->dev, |
f53c3fe8 ZK |
882 | "Trying to overwrite active handle! pending_idx: %x\n", |
883 | pending_idx); | |
884 | BUG(); | |
885 | } | |
e9ce7cb6 | 886 | queue->grant_tx_handle[pending_idx] = handle; |
f53c3fe8 ZK |
887 | } |
888 | ||
e9ce7cb6 | 889 | static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue, |
f53c3fe8 ZK |
890 | u16 pending_idx) |
891 | { | |
e9ce7cb6 | 892 | if (unlikely(queue->grant_tx_handle[pending_idx] == |
f53c3fe8 | 893 | NETBACK_INVALID_HANDLE)) { |
e9ce7cb6 | 894 | netdev_err(queue->vif->dev, |
f53c3fe8 ZK |
895 | "Trying to unmap invalid handle! pending_idx: %x\n", |
896 | pending_idx); | |
897 | BUG(); | |
898 | } | |
e9ce7cb6 | 899 | queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE; |
f53c3fe8 ZK |
900 | } |
901 | ||
e9ce7cb6 | 902 | static int xenvif_tx_check_gop(struct xenvif_queue *queue, |
7376419a | 903 | struct sk_buff *skb, |
bdab8275 ZK |
904 | struct gnttab_map_grant_ref **gopp_map, |
905 | struct gnttab_copy **gopp_copy) | |
f942dc25 | 906 | { |
9074ce24 | 907 | struct gnttab_map_grant_ref *gop_map = *gopp_map; |
8f13dd96 | 908 | u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx; |
1a998d3e ZK |
909 | /* This always points to the shinfo of the skb being checked, which |
910 | * could be either the first or the one on the frag_list | |
911 | */ | |
f942dc25 | 912 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
1a998d3e ZK |
913 | /* If this is non-NULL, we are currently checking the frag_list skb, and |
914 | * this points to the shinfo of the first one | |
915 | */ | |
916 | struct skb_shared_info *first_shinfo = NULL; | |
f942dc25 | 917 | int nr_frags = shinfo->nr_frags; |
1b860da0 ZK |
918 | const bool sharedslot = nr_frags && |
919 | frag_get_pending_idx(&shinfo->frags[0]) == pending_idx; | |
bdab8275 | 920 | int i, err; |
f942dc25 IC |
921 | |
922 | /* Check status of header. */ | |
bdab8275 | 923 | err = (*gopp_copy)->status; |
bdab8275 ZK |
924 | if (unlikely(err)) { |
925 | if (net_ratelimit()) | |
e9ce7cb6 | 926 | netdev_dbg(queue->vif->dev, |
00aefceb | 927 | "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n", |
bdab8275 ZK |
928 | (*gopp_copy)->status, |
929 | pending_idx, | |
930 | (*gopp_copy)->source.u.ref); | |
1b860da0 ZK |
931 | /* The first frag might still have this slot mapped */ |
932 | if (!sharedslot) | |
933 | xenvif_idx_release(queue, pending_idx, | |
934 | XEN_NETIF_RSP_ERROR); | |
bdab8275 | 935 | } |
d8cfbfc4 | 936 | (*gopp_copy)++; |
f942dc25 | 937 | |
e3377f36 | 938 | check_frags: |
bdab8275 | 939 | for (i = 0; i < nr_frags; i++, gop_map++) { |
f942dc25 | 940 | int j, newerr; |
f942dc25 | 941 | |
ea066ad1 | 942 | pending_idx = frag_get_pending_idx(&shinfo->frags[i]); |
f942dc25 IC |
943 | |
944 | /* Check error status: if okay then remember grant handle. */ | |
bdab8275 | 945 | newerr = gop_map->status; |
2810e5b9 | 946 | |
f942dc25 | 947 | if (likely(!newerr)) { |
e9ce7cb6 | 948 | xenvif_grant_handle_set(queue, |
9074ce24 ZK |
949 | pending_idx, |
950 | gop_map->handle); | |
f942dc25 | 951 | /* Had a previous error? Invalidate this fragment. */ |
1b860da0 | 952 | if (unlikely(err)) { |
e9ce7cb6 | 953 | xenvif_idx_unmap(queue, pending_idx); |
1b860da0 ZK |
954 | /* If the mapping of the first frag was OK, but |
955 | * the header's copy failed, and they are | |
956 | * sharing a slot, send an error | |
957 | */ | |
958 | if (i == 0 && sharedslot) | |
959 | xenvif_idx_release(queue, pending_idx, | |
960 | XEN_NETIF_RSP_ERROR); | |
961 | else | |
962 | xenvif_idx_release(queue, pending_idx, | |
963 | XEN_NETIF_RSP_OKAY); | |
964 | } | |
f942dc25 IC |
965 | continue; |
966 | } | |
967 | ||
968 | /* Error on this fragment: respond to client with an error. */ | |
bdab8275 | 969 | if (net_ratelimit()) |
e9ce7cb6 | 970 | netdev_dbg(queue->vif->dev, |
00aefceb | 971 | "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n", |
bdab8275 ZK |
972 | i, |
973 | gop_map->status, | |
974 | pending_idx, | |
975 | gop_map->ref); | |
1b860da0 | 976 | |
e9ce7cb6 | 977 | xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR); |
f942dc25 IC |
978 | |
979 | /* Not the first error? Preceding frags already invalidated. */ | |
980 | if (err) | |
981 | continue; | |
1b860da0 ZK |
982 | |
983 | /* First error: if the header haven't shared a slot with the | |
984 | * first frag, release it as well. | |
985 | */ | |
986 | if (!sharedslot) | |
987 | xenvif_idx_release(queue, | |
988 | XENVIF_TX_CB(skb)->pending_idx, | |
989 | XEN_NETIF_RSP_OKAY); | |
990 | ||
991 | /* Invalidate preceding fragments of this skb. */ | |
bdab8275 | 992 | for (j = 0; j < i; j++) { |
5ccb3ea7 | 993 | pending_idx = frag_get_pending_idx(&shinfo->frags[j]); |
e9ce7cb6 | 994 | xenvif_idx_unmap(queue, pending_idx); |
1b860da0 ZK |
995 | xenvif_idx_release(queue, pending_idx, |
996 | XEN_NETIF_RSP_OKAY); | |
f942dc25 IC |
997 | } |
998 | ||
1a998d3e ZK |
999 | /* And if we found the error while checking the frag_list, unmap |
1000 | * the first skb's frags | |
1001 | */ | |
1002 | if (first_shinfo) { | |
1003 | for (j = 0; j < first_shinfo->nr_frags; j++) { | |
1004 | pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]); | |
1005 | xenvif_idx_unmap(queue, pending_idx); | |
1b860da0 ZK |
1006 | xenvif_idx_release(queue, pending_idx, |
1007 | XEN_NETIF_RSP_OKAY); | |
1a998d3e | 1008 | } |
f942dc25 IC |
1009 | } |
1010 | ||
1011 | /* Remember the error: invalidate all subsequent fragments. */ | |
1012 | err = newerr; | |
1013 | } | |
1014 | ||
1a998d3e ZK |
1015 | if (skb_has_frag_list(skb) && !first_shinfo) { |
1016 | first_shinfo = skb_shinfo(skb); | |
1017 | shinfo = skb_shinfo(skb_shinfo(skb)->frag_list); | |
e3377f36 | 1018 | nr_frags = shinfo->nr_frags; |
e3377f36 ZK |
1019 | |
1020 | goto check_frags; | |
1021 | } | |
1022 | ||
bdab8275 | 1023 | *gopp_map = gop_map; |
f942dc25 IC |
1024 | return err; |
1025 | } | |
1026 | ||
e9ce7cb6 | 1027 | static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb) |
f942dc25 IC |
1028 | { |
1029 | struct skb_shared_info *shinfo = skb_shinfo(skb); | |
1030 | int nr_frags = shinfo->nr_frags; | |
1031 | int i; | |
f53c3fe8 ZK |
1032 | u16 prev_pending_idx = INVALID_PENDING_IDX; |
1033 | ||
f942dc25 IC |
1034 | for (i = 0; i < nr_frags; i++) { |
1035 | skb_frag_t *frag = shinfo->frags + i; | |
1036 | struct xen_netif_tx_request *txp; | |
ea066ad1 IC |
1037 | struct page *page; |
1038 | u16 pending_idx; | |
f942dc25 | 1039 | |
ea066ad1 | 1040 | pending_idx = frag_get_pending_idx(frag); |
f942dc25 | 1041 | |
f53c3fe8 | 1042 | /* If this is not the first frag, chain it to the previous*/ |
bdab8275 | 1043 | if (prev_pending_idx == INVALID_PENDING_IDX) |
f53c3fe8 | 1044 | skb_shinfo(skb)->destructor_arg = |
e9ce7cb6 | 1045 | &callback_param(queue, pending_idx); |
bdab8275 | 1046 | else |
e9ce7cb6 WL |
1047 | callback_param(queue, prev_pending_idx).ctx = |
1048 | &callback_param(queue, pending_idx); | |
f53c3fe8 | 1049 | |
e9ce7cb6 | 1050 | callback_param(queue, pending_idx).ctx = NULL; |
f53c3fe8 ZK |
1051 | prev_pending_idx = pending_idx; |
1052 | ||
e9ce7cb6 WL |
1053 | txp = &queue->pending_tx_info[pending_idx].req; |
1054 | page = virt_to_page(idx_to_kaddr(queue, pending_idx)); | |
ea066ad1 | 1055 | __skb_fill_page_desc(skb, i, page, txp->offset, txp->size); |
f942dc25 IC |
1056 | skb->len += txp->size; |
1057 | skb->data_len += txp->size; | |
1058 | skb->truesize += txp->size; | |
1059 | ||
f53c3fe8 | 1060 | /* Take an extra reference to offset network stack's put_page */ |
e9ce7cb6 | 1061 | get_page(queue->mmap_pages[pending_idx]); |
f942dc25 IC |
1062 | } |
1063 | } | |
1064 | ||
e9ce7cb6 | 1065 | static int xenvif_get_extras(struct xenvif_queue *queue, |
f942dc25 IC |
1066 | struct xen_netif_extra_info *extras, |
1067 | int work_to_do) | |
1068 | { | |
1069 | struct xen_netif_extra_info extra; | |
e9ce7cb6 | 1070 | RING_IDX cons = queue->tx.req_cons; |
f942dc25 IC |
1071 | |
1072 | do { | |
1073 | if (unlikely(work_to_do-- <= 0)) { | |
e9ce7cb6 WL |
1074 | netdev_err(queue->vif->dev, "Missing extra info\n"); |
1075 | xenvif_fatal_tx_err(queue->vif); | |
f942dc25 IC |
1076 | return -EBADR; |
1077 | } | |
1078 | ||
e9ce7cb6 | 1079 | memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons), |
f942dc25 IC |
1080 | sizeof(extra)); |
1081 | if (unlikely(!extra.type || | |
1082 | extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) { | |
e9ce7cb6 WL |
1083 | queue->tx.req_cons = ++cons; |
1084 | netdev_err(queue->vif->dev, | |
f942dc25 | 1085 | "Invalid extra type: %d\n", extra.type); |
e9ce7cb6 | 1086 | xenvif_fatal_tx_err(queue->vif); |
f942dc25 IC |
1087 | return -EINVAL; |
1088 | } | |
1089 | ||
1090 | memcpy(&extras[extra.type - 1], &extra, sizeof(extra)); | |
e9ce7cb6 | 1091 | queue->tx.req_cons = ++cons; |
f942dc25 IC |
1092 | } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); |
1093 | ||
1094 | return work_to_do; | |
1095 | } | |
1096 | ||
7376419a WL |
1097 | static int xenvif_set_skb_gso(struct xenvif *vif, |
1098 | struct sk_buff *skb, | |
1099 | struct xen_netif_extra_info *gso) | |
f942dc25 IC |
1100 | { |
1101 | if (!gso->u.gso.size) { | |
48856286 | 1102 | netdev_err(vif->dev, "GSO size must not be zero.\n"); |
7376419a | 1103 | xenvif_fatal_tx_err(vif); |
f942dc25 IC |
1104 | return -EINVAL; |
1105 | } | |
1106 | ||
a9468587 PD |
1107 | switch (gso->u.gso.type) { |
1108 | case XEN_NETIF_GSO_TYPE_TCPV4: | |
1109 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; | |
1110 | break; | |
1111 | case XEN_NETIF_GSO_TYPE_TCPV6: | |
1112 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; | |
1113 | break; | |
1114 | default: | |
48856286 | 1115 | netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type); |
7376419a | 1116 | xenvif_fatal_tx_err(vif); |
f942dc25 IC |
1117 | return -EINVAL; |
1118 | } | |
1119 | ||
1120 | skb_shinfo(skb)->gso_size = gso->u.gso.size; | |
b89587a7 | 1121 | /* gso_segs will be calculated later */ |
f942dc25 IC |
1122 | |
1123 | return 0; | |
1124 | } | |
1125 | ||
e9ce7cb6 | 1126 | static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb) |
2eba61d5 | 1127 | { |
2721637c | 1128 | bool recalculate_partial_csum = false; |
2eba61d5 PD |
1129 | |
1130 | /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy | |
1131 | * peers can fail to set NETRXF_csum_blank when sending a GSO | |
1132 | * frame. In this case force the SKB to CHECKSUM_PARTIAL and | |
1133 | * recalculate the partial checksum. | |
1134 | */ | |
1135 | if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) { | |
e9ce7cb6 | 1136 | queue->stats.rx_gso_checksum_fixup++; |
2eba61d5 | 1137 | skb->ip_summed = CHECKSUM_PARTIAL; |
2721637c | 1138 | recalculate_partial_csum = true; |
2eba61d5 PD |
1139 | } |
1140 | ||
1141 | /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ | |
1142 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
1143 | return 0; | |
1144 | ||
2721637c | 1145 | return skb_checksum_setup(skb, recalculate_partial_csum); |
2eba61d5 PD |
1146 | } |
1147 | ||
e9ce7cb6 | 1148 | static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size) |
f942dc25 | 1149 | { |
059dfa6a | 1150 | u64 now = get_jiffies_64(); |
e9ce7cb6 WL |
1151 | u64 next_credit = queue->credit_window_start + |
1152 | msecs_to_jiffies(queue->credit_usec / 1000); | |
f942dc25 IC |
1153 | |
1154 | /* Timer could already be pending in rare cases. */ | |
e9ce7cb6 | 1155 | if (timer_pending(&queue->credit_timeout)) |
f942dc25 IC |
1156 | return true; |
1157 | ||
1158 | /* Passed the point where we can replenish credit? */ | |
059dfa6a | 1159 | if (time_after_eq64(now, next_credit)) { |
e9ce7cb6 WL |
1160 | queue->credit_window_start = now; |
1161 | tx_add_credit(queue); | |
f942dc25 IC |
1162 | } |
1163 | ||
1164 | /* Still too big to send right now? Set a callback. */ | |
e9ce7cb6 WL |
1165 | if (size > queue->remaining_credit) { |
1166 | queue->credit_timeout.data = | |
1167 | (unsigned long)queue; | |
1168 | queue->credit_timeout.function = | |
f942dc25 | 1169 | tx_credit_callback; |
e9ce7cb6 | 1170 | mod_timer(&queue->credit_timeout, |
f942dc25 | 1171 | next_credit); |
e9ce7cb6 | 1172 | queue->credit_window_start = next_credit; |
f942dc25 IC |
1173 | |
1174 | return true; | |
1175 | } | |
1176 | ||
1177 | return false; | |
1178 | } | |
1179 | ||
e9ce7cb6 | 1180 | static void xenvif_tx_build_gops(struct xenvif_queue *queue, |
bdab8275 ZK |
1181 | int budget, |
1182 | unsigned *copy_ops, | |
1183 | unsigned *map_ops) | |
f942dc25 | 1184 | { |
e9ce7cb6 | 1185 | struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop; |
f942dc25 IC |
1186 | struct sk_buff *skb; |
1187 | int ret; | |
1188 | ||
e9ce7cb6 | 1189 | while (skb_queue_len(&queue->tx_queue) < budget) { |
f942dc25 | 1190 | struct xen_netif_tx_request txreq; |
37641494 | 1191 | struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX]; |
f942dc25 IC |
1192 | struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1]; |
1193 | u16 pending_idx; | |
1194 | RING_IDX idx; | |
1195 | int work_to_do; | |
1196 | unsigned int data_len; | |
1197 | pending_ring_idx_t index; | |
1198 | ||
e9ce7cb6 | 1199 | if (queue->tx.sring->req_prod - queue->tx.req_cons > |
48856286 | 1200 | XEN_NETIF_TX_RING_SIZE) { |
e9ce7cb6 | 1201 | netdev_err(queue->vif->dev, |
48856286 IC |
1202 | "Impossible number of requests. " |
1203 | "req_prod %d, req_cons %d, size %ld\n", | |
e9ce7cb6 | 1204 | queue->tx.sring->req_prod, queue->tx.req_cons, |
48856286 | 1205 | XEN_NETIF_TX_RING_SIZE); |
e9ce7cb6 | 1206 | xenvif_fatal_tx_err(queue->vif); |
e9d8b2c2 | 1207 | break; |
48856286 IC |
1208 | } |
1209 | ||
e9ce7cb6 | 1210 | work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx); |
b3f980bd WL |
1211 | if (!work_to_do) |
1212 | break; | |
f942dc25 | 1213 | |
e9ce7cb6 | 1214 | idx = queue->tx.req_cons; |
f942dc25 | 1215 | rmb(); /* Ensure that we see the request before we copy it. */ |
e9ce7cb6 | 1216 | memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq)); |
f942dc25 IC |
1217 | |
1218 | /* Credit-based scheduling. */ | |
e9ce7cb6 WL |
1219 | if (txreq.size > queue->remaining_credit && |
1220 | tx_credit_exceeded(queue, txreq.size)) | |
b3f980bd | 1221 | break; |
f942dc25 | 1222 | |
e9ce7cb6 | 1223 | queue->remaining_credit -= txreq.size; |
f942dc25 IC |
1224 | |
1225 | work_to_do--; | |
e9ce7cb6 | 1226 | queue->tx.req_cons = ++idx; |
f942dc25 IC |
1227 | |
1228 | memset(extras, 0, sizeof(extras)); | |
1229 | if (txreq.flags & XEN_NETTXF_extra_info) { | |
e9ce7cb6 | 1230 | work_to_do = xenvif_get_extras(queue, extras, |
7376419a | 1231 | work_to_do); |
e9ce7cb6 | 1232 | idx = queue->tx.req_cons; |
48856286 | 1233 | if (unlikely(work_to_do < 0)) |
b3f980bd | 1234 | break; |
f942dc25 IC |
1235 | } |
1236 | ||
e9ce7cb6 | 1237 | ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do); |
48856286 | 1238 | if (unlikely(ret < 0)) |
b3f980bd | 1239 | break; |
48856286 | 1240 | |
f942dc25 IC |
1241 | idx += ret; |
1242 | ||
1243 | if (unlikely(txreq.size < ETH_HLEN)) { | |
e9ce7cb6 | 1244 | netdev_dbg(queue->vif->dev, |
f942dc25 | 1245 | "Bad packet size: %d\n", txreq.size); |
e9ce7cb6 | 1246 | xenvif_tx_err(queue, &txreq, idx); |
b3f980bd | 1247 | break; |
f942dc25 IC |
1248 | } |
1249 | ||
1250 | /* No crossing a page as the payload mustn't fragment. */ | |
1251 | if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) { | |
e9ce7cb6 | 1252 | netdev_err(queue->vif->dev, |
f942dc25 IC |
1253 | "txreq.offset: %x, size: %u, end: %lu\n", |
1254 | txreq.offset, txreq.size, | |
1255 | (txreq.offset&~PAGE_MASK) + txreq.size); | |
e9ce7cb6 | 1256 | xenvif_fatal_tx_err(queue->vif); |
b3f980bd | 1257 | break; |
f942dc25 IC |
1258 | } |
1259 | ||
e9ce7cb6 WL |
1260 | index = pending_index(queue->pending_cons); |
1261 | pending_idx = queue->pending_ring[index]; | |
f942dc25 | 1262 | |
7e5d7753 | 1263 | data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN && |
37641494 | 1264 | ret < XEN_NETBK_LEGACY_SLOTS_MAX) ? |
7e5d7753 | 1265 | XEN_NETBACK_TX_COPY_LEN : txreq.size; |
f942dc25 | 1266 | |
e3377f36 | 1267 | skb = xenvif_alloc_skb(data_len); |
f942dc25 | 1268 | if (unlikely(skb == NULL)) { |
e9ce7cb6 | 1269 | netdev_dbg(queue->vif->dev, |
f942dc25 | 1270 | "Can't allocate a skb in start_xmit.\n"); |
e9ce7cb6 | 1271 | xenvif_tx_err(queue, &txreq, idx); |
f942dc25 IC |
1272 | break; |
1273 | } | |
1274 | ||
f942dc25 IC |
1275 | if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) { |
1276 | struct xen_netif_extra_info *gso; | |
1277 | gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1]; | |
1278 | ||
e9ce7cb6 | 1279 | if (xenvif_set_skb_gso(queue->vif, skb, gso)) { |
7376419a | 1280 | /* Failure in xenvif_set_skb_gso is fatal. */ |
f942dc25 | 1281 | kfree_skb(skb); |
b3f980bd | 1282 | break; |
f942dc25 IC |
1283 | } |
1284 | } | |
1285 | ||
8f13dd96 | 1286 | XENVIF_TX_CB(skb)->pending_idx = pending_idx; |
f942dc25 IC |
1287 | |
1288 | __skb_put(skb, data_len); | |
e9ce7cb6 WL |
1289 | queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref; |
1290 | queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid; | |
1291 | queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset; | |
bdab8275 | 1292 | |
e9ce7cb6 | 1293 | queue->tx_copy_ops[*copy_ops].dest.u.gmfn = |
bdab8275 | 1294 | virt_to_mfn(skb->data); |
e9ce7cb6 WL |
1295 | queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF; |
1296 | queue->tx_copy_ops[*copy_ops].dest.offset = | |
bdab8275 ZK |
1297 | offset_in_page(skb->data); |
1298 | ||
e9ce7cb6 WL |
1299 | queue->tx_copy_ops[*copy_ops].len = data_len; |
1300 | queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref; | |
bdab8275 ZK |
1301 | |
1302 | (*copy_ops)++; | |
f942dc25 IC |
1303 | |
1304 | skb_shinfo(skb)->nr_frags = ret; | |
1305 | if (data_len < txreq.size) { | |
1306 | skb_shinfo(skb)->nr_frags++; | |
ea066ad1 IC |
1307 | frag_set_pending_idx(&skb_shinfo(skb)->frags[0], |
1308 | pending_idx); | |
e9ce7cb6 | 1309 | xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop); |
bdab8275 | 1310 | gop++; |
f942dc25 | 1311 | } else { |
ea066ad1 IC |
1312 | frag_set_pending_idx(&skb_shinfo(skb)->frags[0], |
1313 | INVALID_PENDING_IDX); | |
e9ce7cb6 | 1314 | memcpy(&queue->pending_tx_info[pending_idx].req, &txreq, |
bdab8275 | 1315 | sizeof(txreq)); |
f942dc25 IC |
1316 | } |
1317 | ||
e9ce7cb6 | 1318 | queue->pending_cons++; |
f942dc25 | 1319 | |
e9ce7cb6 | 1320 | request_gop = xenvif_get_requests(queue, skb, txfrags, gop); |
f942dc25 IC |
1321 | if (request_gop == NULL) { |
1322 | kfree_skb(skb); | |
e9ce7cb6 | 1323 | xenvif_tx_err(queue, &txreq, idx); |
b3f980bd | 1324 | break; |
f942dc25 IC |
1325 | } |
1326 | gop = request_gop; | |
1327 | ||
e9ce7cb6 | 1328 | __skb_queue_tail(&queue->tx_queue, skb); |
1e0b6eac | 1329 | |
e9ce7cb6 | 1330 | queue->tx.req_cons = idx; |
f942dc25 | 1331 | |
e9ce7cb6 WL |
1332 | if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) || |
1333 | (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops))) | |
f942dc25 IC |
1334 | break; |
1335 | } | |
1336 | ||
e9ce7cb6 | 1337 | (*map_ops) = gop - queue->tx_map_ops; |
bdab8275 | 1338 | return; |
f942dc25 IC |
1339 | } |
1340 | ||
e3377f36 ZK |
1341 | /* Consolidate skb with a frag_list into a brand new one with local pages on |
1342 | * frags. Returns 0 or -ENOMEM if can't allocate new pages. | |
1343 | */ | |
e9ce7cb6 | 1344 | static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb) |
e3377f36 ZK |
1345 | { |
1346 | unsigned int offset = skb_headlen(skb); | |
1347 | skb_frag_t frags[MAX_SKB_FRAGS]; | |
49d9991a | 1348 | int i, f; |
e3377f36 ZK |
1349 | struct ubuf_info *uarg; |
1350 | struct sk_buff *nskb = skb_shinfo(skb)->frag_list; | |
1351 | ||
e9ce7cb6 WL |
1352 | queue->stats.tx_zerocopy_sent += 2; |
1353 | queue->stats.tx_frag_overflow++; | |
e3377f36 | 1354 | |
e9ce7cb6 | 1355 | xenvif_fill_frags(queue, nskb); |
e3377f36 ZK |
1356 | /* Subtract frags size, we will correct it later */ |
1357 | skb->truesize -= skb->data_len; | |
1358 | skb->len += nskb->len; | |
1359 | skb->data_len += nskb->len; | |
1360 | ||
1361 | /* create a brand new frags array and coalesce there */ | |
1362 | for (i = 0; offset < skb->len; i++) { | |
1363 | struct page *page; | |
1364 | unsigned int len; | |
1365 | ||
1366 | BUG_ON(i >= MAX_SKB_FRAGS); | |
44cc8ed1 | 1367 | page = alloc_page(GFP_ATOMIC); |
e3377f36 ZK |
1368 | if (!page) { |
1369 | int j; | |
1370 | skb->truesize += skb->data_len; | |
1371 | for (j = 0; j < i; j++) | |
1372 | put_page(frags[j].page.p); | |
1373 | return -ENOMEM; | |
1374 | } | |
1375 | ||
1376 | if (offset + PAGE_SIZE < skb->len) | |
1377 | len = PAGE_SIZE; | |
1378 | else | |
1379 | len = skb->len - offset; | |
1380 | if (skb_copy_bits(skb, offset, page_address(page), len)) | |
1381 | BUG(); | |
1382 | ||
1383 | offset += len; | |
1384 | frags[i].page.p = page; | |
1385 | frags[i].page_offset = 0; | |
1386 | skb_frag_size_set(&frags[i], len); | |
1387 | } | |
49d9991a | 1388 | |
b0c21bad DV |
1389 | /* Copied all the bits from the frag list -- free it. */ |
1390 | skb_frag_list_init(skb); | |
1391 | xenvif_skb_zerocopy_prepare(queue, nskb); | |
1392 | kfree_skb(nskb); | |
1393 | ||
49d9991a DV |
1394 | /* Release all the original (foreign) frags. */ |
1395 | for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) | |
1396 | skb_frag_unref(skb, f); | |
e3377f36 | 1397 | uarg = skb_shinfo(skb)->destructor_arg; |
a64bd934 WL |
1398 | /* increase inflight counter to offset decrement in callback */ |
1399 | atomic_inc(&queue->inflight_packets); | |
e3377f36 ZK |
1400 | uarg->callback(uarg, true); |
1401 | skb_shinfo(skb)->destructor_arg = NULL; | |
1402 | ||
b0c21bad DV |
1403 | /* Fill the skb with the new (local) frags. */ |
1404 | memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t)); | |
1405 | skb_shinfo(skb)->nr_frags = i; | |
1406 | skb->truesize += i * PAGE_SIZE; | |
e3377f36 ZK |
1407 | |
1408 | return 0; | |
1409 | } | |
b3f980bd | 1410 | |
e9ce7cb6 | 1411 | static int xenvif_tx_submit(struct xenvif_queue *queue) |
f942dc25 | 1412 | { |
e9ce7cb6 WL |
1413 | struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops; |
1414 | struct gnttab_copy *gop_copy = queue->tx_copy_ops; | |
f942dc25 | 1415 | struct sk_buff *skb; |
b3f980bd | 1416 | int work_done = 0; |
f942dc25 | 1417 | |
e9ce7cb6 | 1418 | while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) { |
f942dc25 | 1419 | struct xen_netif_tx_request *txp; |
f942dc25 IC |
1420 | u16 pending_idx; |
1421 | unsigned data_len; | |
1422 | ||
8f13dd96 | 1423 | pending_idx = XENVIF_TX_CB(skb)->pending_idx; |
e9ce7cb6 | 1424 | txp = &queue->pending_tx_info[pending_idx].req; |
f942dc25 IC |
1425 | |
1426 | /* Check the remap error code. */ | |
e9ce7cb6 | 1427 | if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) { |
b42cc6e4 ZK |
1428 | /* If there was an error, xenvif_tx_check_gop is |
1429 | * expected to release all the frags which were mapped, | |
1430 | * so kfree_skb shouldn't do it again | |
1431 | */ | |
f942dc25 | 1432 | skb_shinfo(skb)->nr_frags = 0; |
b42cc6e4 ZK |
1433 | if (skb_has_frag_list(skb)) { |
1434 | struct sk_buff *nskb = | |
1435 | skb_shinfo(skb)->frag_list; | |
1436 | skb_shinfo(nskb)->nr_frags = 0; | |
1437 | } | |
f942dc25 IC |
1438 | kfree_skb(skb); |
1439 | continue; | |
1440 | } | |
1441 | ||
1442 | data_len = skb->len; | |
e9ce7cb6 | 1443 | callback_param(queue, pending_idx).ctx = NULL; |
f942dc25 IC |
1444 | if (data_len < txp->size) { |
1445 | /* Append the packet payload as a fragment. */ | |
1446 | txp->offset += data_len; | |
1447 | txp->size -= data_len; | |
1448 | } else { | |
1449 | /* Schedule a response immediately. */ | |
e9ce7cb6 | 1450 | xenvif_idx_release(queue, pending_idx, |
bdab8275 | 1451 | XEN_NETIF_RSP_OKAY); |
f942dc25 IC |
1452 | } |
1453 | ||
1454 | if (txp->flags & XEN_NETTXF_csum_blank) | |
1455 | skb->ip_summed = CHECKSUM_PARTIAL; | |
1456 | else if (txp->flags & XEN_NETTXF_data_validated) | |
1457 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
1458 | ||
e9ce7cb6 | 1459 | xenvif_fill_frags(queue, skb); |
f942dc25 | 1460 | |
e3377f36 | 1461 | if (unlikely(skb_has_frag_list(skb))) { |
e9ce7cb6 | 1462 | if (xenvif_handle_frag_list(queue, skb)) { |
e3377f36 | 1463 | if (net_ratelimit()) |
e9ce7cb6 | 1464 | netdev_err(queue->vif->dev, |
e3377f36 | 1465 | "Not enough memory to consolidate frag_list!\n"); |
a64bd934 | 1466 | xenvif_skb_zerocopy_prepare(queue, skb); |
e3377f36 ZK |
1467 | kfree_skb(skb); |
1468 | continue; | |
1469 | } | |
1470 | } | |
1471 | ||
e9ce7cb6 | 1472 | skb->dev = queue->vif->dev; |
f942dc25 | 1473 | skb->protocol = eth_type_trans(skb, skb->dev); |
f9ca8f74 | 1474 | skb_reset_network_header(skb); |
f942dc25 | 1475 | |
e9ce7cb6 WL |
1476 | if (checksum_setup(queue, skb)) { |
1477 | netdev_dbg(queue->vif->dev, | |
f942dc25 | 1478 | "Can't setup checksum in net_tx_action\n"); |
f53c3fe8 ZK |
1479 | /* We have to set this flag to trigger the callback */ |
1480 | if (skb_shinfo(skb)->destructor_arg) | |
a64bd934 | 1481 | xenvif_skb_zerocopy_prepare(queue, skb); |
f942dc25 IC |
1482 | kfree_skb(skb); |
1483 | continue; | |
1484 | } | |
1485 | ||
40893fd0 | 1486 | skb_probe_transport_header(skb, 0); |
f9ca8f74 | 1487 | |
b89587a7 PD |
1488 | /* If the packet is GSO then we will have just set up the |
1489 | * transport header offset in checksum_setup so it's now | |
1490 | * straightforward to calculate gso_segs. | |
1491 | */ | |
1492 | if (skb_is_gso(skb)) { | |
1493 | int mss = skb_shinfo(skb)->gso_size; | |
1494 | int hdrlen = skb_transport_header(skb) - | |
1495 | skb_mac_header(skb) + | |
1496 | tcp_hdrlen(skb); | |
1497 | ||
1498 | skb_shinfo(skb)->gso_segs = | |
1499 | DIV_ROUND_UP(skb->len - hdrlen, mss); | |
1500 | } | |
1501 | ||
e9ce7cb6 WL |
1502 | queue->stats.rx_bytes += skb->len; |
1503 | queue->stats.rx_packets++; | |
f942dc25 | 1504 | |
b3f980bd WL |
1505 | work_done++; |
1506 | ||
f53c3fe8 ZK |
1507 | /* Set this flag right before netif_receive_skb, otherwise |
1508 | * someone might think this packet already left netback, and | |
1509 | * do a skb_copy_ubufs while we are still in control of the | |
1510 | * skb. E.g. the __pskb_pull_tail earlier can do such thing. | |
1511 | */ | |
1bb332af | 1512 | if (skb_shinfo(skb)->destructor_arg) { |
a64bd934 | 1513 | xenvif_skb_zerocopy_prepare(queue, skb); |
e9ce7cb6 | 1514 | queue->stats.tx_zerocopy_sent++; |
1bb332af | 1515 | } |
f53c3fe8 | 1516 | |
b3f980bd | 1517 | netif_receive_skb(skb); |
f942dc25 | 1518 | } |
b3f980bd WL |
1519 | |
1520 | return work_done; | |
f942dc25 IC |
1521 | } |
1522 | ||
3e2234b3 ZK |
1523 | void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success) |
1524 | { | |
f53c3fe8 ZK |
1525 | unsigned long flags; |
1526 | pending_ring_idx_t index; | |
e9ce7cb6 | 1527 | struct xenvif_queue *queue = ubuf_to_queue(ubuf); |
f53c3fe8 ZK |
1528 | |
1529 | /* This is the only place where we grab this lock, to protect callbacks | |
1530 | * from each other. | |
1531 | */ | |
e9ce7cb6 | 1532 | spin_lock_irqsave(&queue->callback_lock, flags); |
f53c3fe8 ZK |
1533 | do { |
1534 | u16 pending_idx = ubuf->desc; | |
1535 | ubuf = (struct ubuf_info *) ubuf->ctx; | |
e9ce7cb6 | 1536 | BUG_ON(queue->dealloc_prod - queue->dealloc_cons >= |
f53c3fe8 | 1537 | MAX_PENDING_REQS); |
e9ce7cb6 WL |
1538 | index = pending_index(queue->dealloc_prod); |
1539 | queue->dealloc_ring[index] = pending_idx; | |
f53c3fe8 ZK |
1540 | /* Sync with xenvif_tx_dealloc_action: |
1541 | * insert idx then incr producer. | |
1542 | */ | |
1543 | smp_wmb(); | |
e9ce7cb6 | 1544 | queue->dealloc_prod++; |
f53c3fe8 | 1545 | } while (ubuf); |
e9ce7cb6 WL |
1546 | wake_up(&queue->dealloc_wq); |
1547 | spin_unlock_irqrestore(&queue->callback_lock, flags); | |
f53c3fe8 | 1548 | |
1bb332af | 1549 | if (likely(zerocopy_success)) |
e9ce7cb6 | 1550 | queue->stats.tx_zerocopy_success++; |
1bb332af | 1551 | else |
e9ce7cb6 | 1552 | queue->stats.tx_zerocopy_fail++; |
a64bd934 | 1553 | xenvif_skb_zerocopy_complete(queue); |
f53c3fe8 ZK |
1554 | } |
1555 | ||
e9ce7cb6 | 1556 | static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue) |
f53c3fe8 ZK |
1557 | { |
1558 | struct gnttab_unmap_grant_ref *gop; | |
1559 | pending_ring_idx_t dc, dp; | |
1560 | u16 pending_idx, pending_idx_release[MAX_PENDING_REQS]; | |
1561 | unsigned int i = 0; | |
1562 | ||
e9ce7cb6 WL |
1563 | dc = queue->dealloc_cons; |
1564 | gop = queue->tx_unmap_ops; | |
f53c3fe8 ZK |
1565 | |
1566 | /* Free up any grants we have finished using */ | |
1567 | do { | |
e9ce7cb6 | 1568 | dp = queue->dealloc_prod; |
f53c3fe8 ZK |
1569 | |
1570 | /* Ensure we see all indices enqueued by all | |
1571 | * xenvif_zerocopy_callback(). | |
1572 | */ | |
1573 | smp_rmb(); | |
1574 | ||
1575 | while (dc != dp) { | |
e9ce7cb6 | 1576 | BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS); |
f53c3fe8 | 1577 | pending_idx = |
e9ce7cb6 | 1578 | queue->dealloc_ring[pending_index(dc++)]; |
f53c3fe8 | 1579 | |
e9ce7cb6 | 1580 | pending_idx_release[gop-queue->tx_unmap_ops] = |
f53c3fe8 | 1581 | pending_idx; |
e9ce7cb6 WL |
1582 | queue->pages_to_unmap[gop-queue->tx_unmap_ops] = |
1583 | queue->mmap_pages[pending_idx]; | |
f53c3fe8 | 1584 | gnttab_set_unmap_op(gop, |
e9ce7cb6 | 1585 | idx_to_kaddr(queue, pending_idx), |
f53c3fe8 | 1586 | GNTMAP_host_map, |
e9ce7cb6 WL |
1587 | queue->grant_tx_handle[pending_idx]); |
1588 | xenvif_grant_handle_reset(queue, pending_idx); | |
f53c3fe8 ZK |
1589 | ++gop; |
1590 | } | |
1591 | ||
e9ce7cb6 | 1592 | } while (dp != queue->dealloc_prod); |
f53c3fe8 | 1593 | |
e9ce7cb6 | 1594 | queue->dealloc_cons = dc; |
f53c3fe8 | 1595 | |
e9ce7cb6 | 1596 | if (gop - queue->tx_unmap_ops > 0) { |
f53c3fe8 | 1597 | int ret; |
e9ce7cb6 | 1598 | ret = gnttab_unmap_refs(queue->tx_unmap_ops, |
f53c3fe8 | 1599 | NULL, |
e9ce7cb6 WL |
1600 | queue->pages_to_unmap, |
1601 | gop - queue->tx_unmap_ops); | |
f53c3fe8 | 1602 | if (ret) { |
e9ce7cb6 WL |
1603 | netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n", |
1604 | gop - queue->tx_unmap_ops, ret); | |
1605 | for (i = 0; i < gop - queue->tx_unmap_ops; ++i) { | |
f53c3fe8 | 1606 | if (gop[i].status != GNTST_okay) |
e9ce7cb6 | 1607 | netdev_err(queue->vif->dev, |
f53c3fe8 ZK |
1608 | " host_addr: %llx handle: %x status: %d\n", |
1609 | gop[i].host_addr, | |
1610 | gop[i].handle, | |
1611 | gop[i].status); | |
1612 | } | |
1613 | BUG(); | |
1614 | } | |
1615 | } | |
1616 | ||
e9ce7cb6 WL |
1617 | for (i = 0; i < gop - queue->tx_unmap_ops; ++i) |
1618 | xenvif_idx_release(queue, pending_idx_release[i], | |
f53c3fe8 | 1619 | XEN_NETIF_RSP_OKAY); |
3e2234b3 ZK |
1620 | } |
1621 | ||
f53c3fe8 | 1622 | |
f942dc25 | 1623 | /* Called after netfront has transmitted */ |
e9ce7cb6 | 1624 | int xenvif_tx_action(struct xenvif_queue *queue, int budget) |
f942dc25 | 1625 | { |
bdab8275 | 1626 | unsigned nr_mops, nr_cops = 0; |
f53c3fe8 | 1627 | int work_done, ret; |
f942dc25 | 1628 | |
e9ce7cb6 | 1629 | if (unlikely(!tx_work_todo(queue))) |
b3f980bd WL |
1630 | return 0; |
1631 | ||
e9ce7cb6 | 1632 | xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops); |
f942dc25 | 1633 | |
bdab8275 | 1634 | if (nr_cops == 0) |
b3f980bd WL |
1635 | return 0; |
1636 | ||
e9ce7cb6 | 1637 | gnttab_batch_copy(queue->tx_copy_ops, nr_cops); |
bdab8275 | 1638 | if (nr_mops != 0) { |
e9ce7cb6 | 1639 | ret = gnttab_map_refs(queue->tx_map_ops, |
bdab8275 | 1640 | NULL, |
e9ce7cb6 | 1641 | queue->pages_to_map, |
bdab8275 ZK |
1642 | nr_mops); |
1643 | BUG_ON(ret); | |
1644 | } | |
f942dc25 | 1645 | |
e9ce7cb6 | 1646 | work_done = xenvif_tx_submit(queue); |
f942dc25 | 1647 | |
b3f980bd | 1648 | return work_done; |
f942dc25 IC |
1649 | } |
1650 | ||
e9ce7cb6 | 1651 | static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx, |
7376419a | 1652 | u8 status) |
f942dc25 | 1653 | { |
f942dc25 | 1654 | struct pending_tx_info *pending_tx_info; |
f53c3fe8 | 1655 | pending_ring_idx_t index; |
f53c3fe8 | 1656 | unsigned long flags; |
2810e5b9 | 1657 | |
e9ce7cb6 | 1658 | pending_tx_info = &queue->pending_tx_info[pending_idx]; |
7fbb9d84 | 1659 | |
e9ce7cb6 | 1660 | spin_lock_irqsave(&queue->response_lock, flags); |
7fbb9d84 | 1661 | |
e9ce7cb6 | 1662 | make_tx_response(queue, &pending_tx_info->req, status); |
7fbb9d84 DV |
1663 | |
1664 | /* Release the pending index before pusing the Tx response so | |
1665 | * its available before a new Tx request is pushed by the | |
1666 | * frontend. | |
1667 | */ | |
1668 | index = pending_index(queue->pending_prod++); | |
e9ce7cb6 | 1669 | queue->pending_ring[index] = pending_idx; |
7fbb9d84 | 1670 | |
c8a4d299 | 1671 | push_tx_responses(queue); |
7fbb9d84 | 1672 | |
e9ce7cb6 | 1673 | spin_unlock_irqrestore(&queue->response_lock, flags); |
f942dc25 IC |
1674 | } |
1675 | ||
2810e5b9 | 1676 | |
e9ce7cb6 | 1677 | static void make_tx_response(struct xenvif_queue *queue, |
f942dc25 IC |
1678 | struct xen_netif_tx_request *txp, |
1679 | s8 st) | |
1680 | { | |
e9ce7cb6 | 1681 | RING_IDX i = queue->tx.rsp_prod_pvt; |
f942dc25 | 1682 | struct xen_netif_tx_response *resp; |
f942dc25 | 1683 | |
e9ce7cb6 | 1684 | resp = RING_GET_RESPONSE(&queue->tx, i); |
f942dc25 IC |
1685 | resp->id = txp->id; |
1686 | resp->status = st; | |
1687 | ||
1688 | if (txp->flags & XEN_NETTXF_extra_info) | |
e9ce7cb6 | 1689 | RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL; |
f942dc25 | 1690 | |
e9ce7cb6 | 1691 | queue->tx.rsp_prod_pvt = ++i; |
f942dc25 IC |
1692 | } |
1693 | ||
c8a4d299 DV |
1694 | static void push_tx_responses(struct xenvif_queue *queue) |
1695 | { | |
1696 | int notify; | |
1697 | ||
1698 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify); | |
1699 | if (notify) | |
1700 | notify_remote_via_irq(queue->tx_irq); | |
1701 | } | |
1702 | ||
e9ce7cb6 | 1703 | static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue, |
f942dc25 IC |
1704 | u16 id, |
1705 | s8 st, | |
1706 | u16 offset, | |
1707 | u16 size, | |
1708 | u16 flags) | |
1709 | { | |
e9ce7cb6 | 1710 | RING_IDX i = queue->rx.rsp_prod_pvt; |
f942dc25 IC |
1711 | struct xen_netif_rx_response *resp; |
1712 | ||
e9ce7cb6 | 1713 | resp = RING_GET_RESPONSE(&queue->rx, i); |
f942dc25 IC |
1714 | resp->offset = offset; |
1715 | resp->flags = flags; | |
1716 | resp->id = id; | |
1717 | resp->status = (s16)size; | |
1718 | if (st < 0) | |
1719 | resp->status = (s16)st; | |
1720 | ||
e9ce7cb6 | 1721 | queue->rx.rsp_prod_pvt = ++i; |
f942dc25 IC |
1722 | |
1723 | return resp; | |
1724 | } | |
1725 | ||
e9ce7cb6 | 1726 | void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx) |
f53c3fe8 ZK |
1727 | { |
1728 | int ret; | |
1729 | struct gnttab_unmap_grant_ref tx_unmap_op; | |
1730 | ||
1731 | gnttab_set_unmap_op(&tx_unmap_op, | |
e9ce7cb6 | 1732 | idx_to_kaddr(queue, pending_idx), |
f53c3fe8 | 1733 | GNTMAP_host_map, |
e9ce7cb6 WL |
1734 | queue->grant_tx_handle[pending_idx]); |
1735 | xenvif_grant_handle_reset(queue, pending_idx); | |
f53c3fe8 ZK |
1736 | |
1737 | ret = gnttab_unmap_refs(&tx_unmap_op, NULL, | |
e9ce7cb6 | 1738 | &queue->mmap_pages[pending_idx], 1); |
7aceb47a | 1739 | if (ret) { |
e9ce7cb6 | 1740 | netdev_err(queue->vif->dev, |
7aceb47a ZK |
1741 | "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n", |
1742 | ret, | |
1743 | pending_idx, | |
1744 | tx_unmap_op.host_addr, | |
1745 | tx_unmap_op.handle, | |
1746 | tx_unmap_op.status); | |
1747 | BUG(); | |
1748 | } | |
f53c3fe8 ZK |
1749 | } |
1750 | ||
e9ce7cb6 | 1751 | static inline int tx_work_todo(struct xenvif_queue *queue) |
f942dc25 | 1752 | { |
e9ce7cb6 | 1753 | if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx))) |
f942dc25 IC |
1754 | return 1; |
1755 | ||
1756 | return 0; | |
1757 | } | |
1758 | ||
e9ce7cb6 | 1759 | static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue) |
f53c3fe8 | 1760 | { |
e9ce7cb6 | 1761 | return queue->dealloc_cons != queue->dealloc_prod; |
f53c3fe8 ZK |
1762 | } |
1763 | ||
e9ce7cb6 | 1764 | void xenvif_unmap_frontend_rings(struct xenvif_queue *queue) |
f942dc25 | 1765 | { |
e9ce7cb6 WL |
1766 | if (queue->tx.sring) |
1767 | xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif), | |
1768 | queue->tx.sring); | |
1769 | if (queue->rx.sring) | |
1770 | xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif), | |
1771 | queue->rx.sring); | |
f942dc25 IC |
1772 | } |
1773 | ||
e9ce7cb6 | 1774 | int xenvif_map_frontend_rings(struct xenvif_queue *queue, |
7376419a WL |
1775 | grant_ref_t tx_ring_ref, |
1776 | grant_ref_t rx_ring_ref) | |
f942dc25 | 1777 | { |
c9d63699 | 1778 | void *addr; |
f942dc25 IC |
1779 | struct xen_netif_tx_sring *txs; |
1780 | struct xen_netif_rx_sring *rxs; | |
1781 | ||
1782 | int err = -ENOMEM; | |
1783 | ||
e9ce7cb6 | 1784 | err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif), |
c9d63699 DV |
1785 | tx_ring_ref, &addr); |
1786 | if (err) | |
f942dc25 IC |
1787 | goto err; |
1788 | ||
c9d63699 | 1789 | txs = (struct xen_netif_tx_sring *)addr; |
e9ce7cb6 | 1790 | BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE); |
f942dc25 | 1791 | |
e9ce7cb6 | 1792 | err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif), |
c9d63699 DV |
1793 | rx_ring_ref, &addr); |
1794 | if (err) | |
f942dc25 | 1795 | goto err; |
f942dc25 | 1796 | |
c9d63699 | 1797 | rxs = (struct xen_netif_rx_sring *)addr; |
e9ce7cb6 | 1798 | BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE); |
f942dc25 IC |
1799 | |
1800 | return 0; | |
1801 | ||
1802 | err: | |
e9ce7cb6 | 1803 | xenvif_unmap_frontend_rings(queue); |
f942dc25 IC |
1804 | return err; |
1805 | } | |
1806 | ||
ecf08d2d | 1807 | static void xenvif_queue_carrier_off(struct xenvif_queue *queue) |
ca2f09f2 | 1808 | { |
ecf08d2d DV |
1809 | struct xenvif *vif = queue->vif; |
1810 | ||
1811 | queue->stalled = true; | |
1812 | ||
1813 | /* At least one queue has stalled? Disable the carrier. */ | |
1814 | spin_lock(&vif->lock); | |
1815 | if (vif->stalled_queues++ == 0) { | |
1816 | netdev_info(vif->dev, "Guest Rx stalled"); | |
1817 | netif_carrier_off(vif->dev); | |
1818 | } | |
1819 | spin_unlock(&vif->lock); | |
ca2f09f2 PD |
1820 | } |
1821 | ||
ecf08d2d | 1822 | static void xenvif_queue_carrier_on(struct xenvif_queue *queue) |
f34a4cf9 | 1823 | { |
ecf08d2d | 1824 | struct xenvif *vif = queue->vif; |
f34a4cf9 | 1825 | |
ecf08d2d DV |
1826 | queue->last_rx_time = jiffies; /* Reset Rx stall detection. */ |
1827 | queue->stalled = false; | |
f34a4cf9 | 1828 | |
ecf08d2d DV |
1829 | /* All queues are ready? Enable the carrier. */ |
1830 | spin_lock(&vif->lock); | |
1831 | if (--vif->stalled_queues == 0) { | |
1832 | netdev_info(vif->dev, "Guest Rx ready"); | |
1833 | netif_carrier_on(vif->dev); | |
1834 | } | |
1835 | spin_unlock(&vif->lock); | |
1836 | } | |
f34a4cf9 | 1837 | |
ecf08d2d DV |
1838 | static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue) |
1839 | { | |
1840 | RING_IDX prod, cons; | |
1841 | ||
1842 | prod = queue->rx.sring->req_prod; | |
1843 | cons = queue->rx.req_cons; | |
1844 | ||
1845 | return !queue->stalled | |
1846 | && prod - cons < XEN_NETBK_RX_SLOTS_MAX | |
1847 | && time_after(jiffies, | |
26c0e102 | 1848 | queue->last_rx_time + queue->vif->stall_timeout); |
ecf08d2d DV |
1849 | } |
1850 | ||
1851 | static bool xenvif_rx_queue_ready(struct xenvif_queue *queue) | |
1852 | { | |
1853 | RING_IDX prod, cons; | |
1854 | ||
1855 | prod = queue->rx.sring->req_prod; | |
1856 | cons = queue->rx.req_cons; | |
1857 | ||
1858 | return queue->stalled | |
1859 | && prod - cons >= XEN_NETBK_RX_SLOTS_MAX; | |
1860 | } | |
1861 | ||
f48da8b1 | 1862 | static bool xenvif_have_rx_work(struct xenvif_queue *queue) |
ca2f09f2 | 1863 | { |
f48da8b1 DV |
1864 | return (!skb_queue_empty(&queue->rx_queue) |
1865 | && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)) | |
26c0e102 DV |
1866 | || (queue->vif->stall_timeout && |
1867 | (xenvif_rx_queue_stalled(queue) | |
1868 | || xenvif_rx_queue_ready(queue))) | |
f48da8b1 DV |
1869 | || kthread_should_stop() |
1870 | || queue->vif->disabled; | |
ca2f09f2 PD |
1871 | } |
1872 | ||
f48da8b1 | 1873 | static long xenvif_rx_queue_timeout(struct xenvif_queue *queue) |
f34a4cf9 | 1874 | { |
f48da8b1 DV |
1875 | struct sk_buff *skb; |
1876 | long timeout; | |
f34a4cf9 | 1877 | |
f48da8b1 DV |
1878 | skb = skb_peek(&queue->rx_queue); |
1879 | if (!skb) | |
1880 | return MAX_SCHEDULE_TIMEOUT; | |
f34a4cf9 | 1881 | |
f48da8b1 DV |
1882 | timeout = XENVIF_RX_CB(skb)->expires - jiffies; |
1883 | return timeout < 0 ? 0 : timeout; | |
1884 | } | |
f34a4cf9 | 1885 | |
f48da8b1 DV |
1886 | /* Wait until the guest Rx thread has work. |
1887 | * | |
1888 | * The timeout needs to be adjusted based on the current head of the | |
1889 | * queue (and not just the head at the beginning). In particular, if | |
1890 | * the queue is initially empty an infinite timeout is used and this | |
1891 | * needs to be reduced when a skb is queued. | |
1892 | * | |
1893 | * This cannot be done with wait_event_timeout() because it only | |
1894 | * calculates the timeout once. | |
1895 | */ | |
1896 | static void xenvif_wait_for_rx_work(struct xenvif_queue *queue) | |
1897 | { | |
1898 | DEFINE_WAIT(wait); | |
1899 | ||
1900 | if (xenvif_have_rx_work(queue)) | |
1901 | return; | |
1902 | ||
1903 | for (;;) { | |
1904 | long ret; | |
1905 | ||
1906 | prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE); | |
1907 | if (xenvif_have_rx_work(queue)) | |
1908 | break; | |
1909 | ret = schedule_timeout(xenvif_rx_queue_timeout(queue)); | |
1910 | if (!ret) | |
1911 | break; | |
f34a4cf9 | 1912 | } |
f48da8b1 | 1913 | finish_wait(&queue->wq, &wait); |
f34a4cf9 ZK |
1914 | } |
1915 | ||
121fa4b7 | 1916 | int xenvif_kthread_guest_rx(void *data) |
b3f980bd | 1917 | { |
e9ce7cb6 | 1918 | struct xenvif_queue *queue = data; |
f48da8b1 | 1919 | struct xenvif *vif = queue->vif; |
b3f980bd | 1920 | |
26c0e102 DV |
1921 | if (!vif->stall_timeout) |
1922 | xenvif_queue_carrier_on(queue); | |
1923 | ||
f48da8b1 DV |
1924 | for (;;) { |
1925 | xenvif_wait_for_rx_work(queue); | |
e9d8b2c2 | 1926 | |
f34a4cf9 ZK |
1927 | if (kthread_should_stop()) |
1928 | break; | |
1929 | ||
e9d8b2c2 WL |
1930 | /* This frontend is found to be rogue, disable it in |
1931 | * kthread context. Currently this is only set when | |
1932 | * netback finds out frontend sends malformed packet, | |
1933 | * but we cannot disable the interface in softirq | |
e9ce7cb6 WL |
1934 | * context so we defer it here, if this thread is |
1935 | * associated with queue 0. | |
e9d8b2c2 | 1936 | */ |
f48da8b1 DV |
1937 | if (unlikely(vif->disabled && queue->id == 0)) { |
1938 | xenvif_carrier_off(vif); | |
42b5212f | 1939 | break; |
09350788 ZK |
1940 | } |
1941 | ||
e9ce7cb6 WL |
1942 | if (!skb_queue_empty(&queue->rx_queue)) |
1943 | xenvif_rx_action(queue); | |
b3f980bd | 1944 | |
ecf08d2d DV |
1945 | /* If the guest hasn't provided any Rx slots for a |
1946 | * while it's probably not responsive, drop the | |
1947 | * carrier so packets are dropped earlier. | |
1948 | */ | |
26c0e102 DV |
1949 | if (vif->stall_timeout) { |
1950 | if (xenvif_rx_queue_stalled(queue)) | |
1951 | xenvif_queue_carrier_off(queue); | |
1952 | else if (xenvif_rx_queue_ready(queue)) | |
1953 | xenvif_queue_carrier_on(queue); | |
1954 | } | |
ecf08d2d | 1955 | |
f48da8b1 DV |
1956 | /* Queued packets may have foreign pages from other |
1957 | * domains. These cannot be queued indefinitely as | |
1958 | * this would starve guests of grant refs and transmit | |
1959 | * slots. | |
1960 | */ | |
1961 | xenvif_rx_queue_drop_expired(queue); | |
1962 | ||
1963 | xenvif_rx_queue_maybe_wake(queue); | |
1964 | ||
b3f980bd WL |
1965 | cond_resched(); |
1966 | } | |
1967 | ||
ca2f09f2 | 1968 | /* Bin any remaining skbs */ |
f48da8b1 | 1969 | xenvif_rx_queue_purge(queue); |
ca2f09f2 | 1970 | |
b3f980bd WL |
1971 | return 0; |
1972 | } | |
1973 | ||
a64bd934 WL |
1974 | static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue) |
1975 | { | |
1976 | /* Dealloc thread must remain running until all inflight | |
1977 | * packets complete. | |
1978 | */ | |
1979 | return kthread_should_stop() && | |
1980 | !atomic_read(&queue->inflight_packets); | |
1981 | } | |
1982 | ||
f53c3fe8 ZK |
1983 | int xenvif_dealloc_kthread(void *data) |
1984 | { | |
e9ce7cb6 | 1985 | struct xenvif_queue *queue = data; |
f53c3fe8 | 1986 | |
a64bd934 | 1987 | for (;;) { |
e9ce7cb6 WL |
1988 | wait_event_interruptible(queue->dealloc_wq, |
1989 | tx_dealloc_work_todo(queue) || | |
a64bd934 WL |
1990 | xenvif_dealloc_kthread_should_stop(queue)); |
1991 | if (xenvif_dealloc_kthread_should_stop(queue)) | |
f53c3fe8 ZK |
1992 | break; |
1993 | ||
e9ce7cb6 | 1994 | xenvif_tx_dealloc_action(queue); |
f53c3fe8 ZK |
1995 | cond_resched(); |
1996 | } | |
1997 | ||
1998 | /* Unmap anything remaining*/ | |
e9ce7cb6 WL |
1999 | if (tx_dealloc_work_todo(queue)) |
2000 | xenvif_tx_dealloc_action(queue); | |
f53c3fe8 ZK |
2001 | |
2002 | return 0; | |
2003 | } | |
2004 | ||
f942dc25 IC |
2005 | static int __init netback_init(void) |
2006 | { | |
f942dc25 | 2007 | int rc = 0; |
f942dc25 | 2008 | |
2a14b244 | 2009 | if (!xen_domain()) |
f942dc25 IC |
2010 | return -ENODEV; |
2011 | ||
8d3d53b3 AB |
2012 | /* Allow as many queues as there are CPUs, by default */ |
2013 | xenvif_max_queues = num_online_cpus(); | |
2014 | ||
37641494 | 2015 | if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) { |
383eda32 JP |
2016 | pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n", |
2017 | fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX); | |
37641494 | 2018 | fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX; |
2810e5b9 WL |
2019 | } |
2020 | ||
f942dc25 IC |
2021 | rc = xenvif_xenbus_init(); |
2022 | if (rc) | |
2023 | goto failed_init; | |
2024 | ||
f51de243 ZK |
2025 | #ifdef CONFIG_DEBUG_FS |
2026 | xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL); | |
2027 | if (IS_ERR_OR_NULL(xen_netback_dbg_root)) | |
2028 | pr_warn("Init of debugfs returned %ld!\n", | |
2029 | PTR_ERR(xen_netback_dbg_root)); | |
2030 | #endif /* CONFIG_DEBUG_FS */ | |
2031 | ||
f942dc25 IC |
2032 | return 0; |
2033 | ||
2034 | failed_init: | |
f942dc25 | 2035 | return rc; |
f942dc25 IC |
2036 | } |
2037 | ||
2038 | module_init(netback_init); | |
2039 | ||
b103f358 WL |
2040 | static void __exit netback_fini(void) |
2041 | { | |
f51de243 ZK |
2042 | #ifdef CONFIG_DEBUG_FS |
2043 | if (!IS_ERR_OR_NULL(xen_netback_dbg_root)) | |
2044 | debugfs_remove_recursive(xen_netback_dbg_root); | |
2045 | #endif /* CONFIG_DEBUG_FS */ | |
b103f358 | 2046 | xenvif_xenbus_fini(); |
b103f358 WL |
2047 | } |
2048 | module_exit(netback_fini); | |
2049 | ||
f942dc25 | 2050 | MODULE_LICENSE("Dual BSD/GPL"); |
f984cec6 | 2051 | MODULE_ALIAS("xen-backend:vif"); |