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