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