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> | |
40 | ||
41 | #include <net/tcp.h> | |
42 | ||
ca981633 | 43 | #include <xen/xen.h> |
f942dc25 IC |
44 | #include <xen/events.h> |
45 | #include <xen/interface/memory.h> | |
46 | ||
47 | #include <asm/xen/hypercall.h> | |
48 | #include <asm/xen/page.h> | |
49 | ||
4f0581d2 WL |
50 | /* SKB control block overlay is used to store useful information when |
51 | * doing guest RX. | |
52 | */ | |
53 | struct skb_cb_overlay { | |
54 | int meta_slots_used; | |
55 | int peek_slots_count; | |
56 | }; | |
57 | ||
e1f00a69 WL |
58 | /* Provide an option to disable split event channels at load time as |
59 | * event channels are limited resource. Split event channels are | |
60 | * enabled by default. | |
61 | */ | |
62 | bool separate_tx_rx_irq = 1; | |
63 | module_param(separate_tx_rx_irq, bool, 0644); | |
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 | ||
73 | /* | |
74 | * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating | |
75 | * the maximum slots a valid packet can use. Now this value is defined | |
76 | * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by | |
77 | * all backend. | |
78 | */ | |
79 | #define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN | |
2810e5b9 | 80 | |
2810e5b9 WL |
81 | /* |
82 | * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of | |
83 | * one or more merged tx requests, otherwise it is the continuation of | |
84 | * previous tx request. | |
85 | */ | |
b3f980bd | 86 | static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx) |
f942dc25 | 87 | { |
b3f980bd | 88 | return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX; |
f942dc25 IC |
89 | } |
90 | ||
7376419a WL |
91 | static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx, |
92 | u8 status); | |
93 | ||
f942dc25 IC |
94 | static void make_tx_response(struct xenvif *vif, |
95 | struct xen_netif_tx_request *txp, | |
96 | s8 st); | |
b3f980bd WL |
97 | |
98 | static inline int tx_work_todo(struct xenvif *vif); | |
99 | static inline int rx_work_todo(struct xenvif *vif); | |
100 | ||
f942dc25 IC |
101 | static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif, |
102 | u16 id, | |
103 | s8 st, | |
104 | u16 offset, | |
105 | u16 size, | |
106 | u16 flags); | |
107 | ||
b3f980bd | 108 | static inline unsigned long idx_to_pfn(struct xenvif *vif, |
ea066ad1 | 109 | u16 idx) |
f942dc25 | 110 | { |
b3f980bd | 111 | return page_to_pfn(vif->mmap_pages[idx]); |
f942dc25 IC |
112 | } |
113 | ||
b3f980bd | 114 | static inline unsigned long idx_to_kaddr(struct xenvif *vif, |
ea066ad1 | 115 | u16 idx) |
f942dc25 | 116 | { |
b3f980bd | 117 | return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx)); |
f942dc25 IC |
118 | } |
119 | ||
f942dc25 IC |
120 | /* |
121 | * This is the amount of packet we copy rather than map, so that the | |
122 | * guest can't fiddle with the contents of the headers while we do | |
123 | * packet processing on them (netfilter, routing, etc). | |
124 | */ | |
125 | #define PKT_PROT_LEN (ETH_HLEN + \ | |
126 | VLAN_HLEN + \ | |
127 | sizeof(struct iphdr) + MAX_IPOPTLEN + \ | |
128 | sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE) | |
129 | ||
ea066ad1 IC |
130 | static u16 frag_get_pending_idx(skb_frag_t *frag) |
131 | { | |
132 | return (u16)frag->page_offset; | |
133 | } | |
134 | ||
135 | static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx) | |
136 | { | |
137 | frag->page_offset = pending_idx; | |
138 | } | |
139 | ||
f942dc25 IC |
140 | static inline pending_ring_idx_t pending_index(unsigned i) |
141 | { | |
142 | return i & (MAX_PENDING_REQS-1); | |
143 | } | |
144 | ||
b3f980bd | 145 | static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif) |
f942dc25 IC |
146 | { |
147 | return MAX_PENDING_REQS - | |
b3f980bd | 148 | vif->pending_prod + vif->pending_cons; |
f942dc25 IC |
149 | } |
150 | ||
151 | static int max_required_rx_slots(struct xenvif *vif) | |
152 | { | |
153 | int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE); | |
154 | ||
2810e5b9 | 155 | /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */ |
f942dc25 IC |
156 | if (vif->can_sg || vif->gso || vif->gso_prefix) |
157 | max += MAX_SKB_FRAGS + 1; /* extra_info + frags */ | |
158 | ||
159 | return max; | |
160 | } | |
161 | ||
7376419a | 162 | int xenvif_rx_ring_full(struct xenvif *vif) |
f942dc25 IC |
163 | { |
164 | RING_IDX peek = vif->rx_req_cons_peek; | |
165 | RING_IDX needed = max_required_rx_slots(vif); | |
166 | ||
167 | return ((vif->rx.sring->req_prod - peek) < needed) || | |
168 | ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed); | |
169 | } | |
170 | ||
7376419a | 171 | int xenvif_must_stop_queue(struct xenvif *vif) |
f942dc25 | 172 | { |
7376419a | 173 | if (!xenvif_rx_ring_full(vif)) |
f942dc25 IC |
174 | return 0; |
175 | ||
176 | vif->rx.sring->req_event = vif->rx_req_cons_peek + | |
177 | max_required_rx_slots(vif); | |
178 | mb(); /* request notification /then/ check the queue */ | |
179 | ||
7376419a | 180 | return xenvif_rx_ring_full(vif); |
f942dc25 IC |
181 | } |
182 | ||
183 | /* | |
184 | * Returns true if we should start a new receive buffer instead of | |
185 | * adding 'size' bytes to a buffer which currently contains 'offset' | |
186 | * bytes. | |
187 | */ | |
188 | static bool start_new_rx_buffer(int offset, unsigned long size, int head) | |
189 | { | |
190 | /* simple case: we have completely filled the current buffer. */ | |
191 | if (offset == MAX_BUFFER_OFFSET) | |
192 | return true; | |
193 | ||
194 | /* | |
195 | * complex case: start a fresh buffer if the current frag | |
196 | * would overflow the current buffer but only if: | |
197 | * (i) this frag would fit completely in the next buffer | |
198 | * and (ii) there is already some data in the current buffer | |
199 | * and (iii) this is not the head buffer. | |
200 | * | |
201 | * Where: | |
202 | * - (i) stops us splitting a frag into two copies | |
203 | * unless the frag is too large for a single buffer. | |
204 | * - (ii) stops us from leaving a buffer pointlessly empty. | |
205 | * - (iii) stops us leaving the first buffer | |
206 | * empty. Strictly speaking this is already covered | |
207 | * by (ii) but is explicitly checked because | |
208 | * netfront relies on the first buffer being | |
209 | * non-empty and can crash otherwise. | |
210 | * | |
211 | * This means we will effectively linearise small | |
212 | * frags but do not needlessly split large buffers | |
213 | * into multiple copies tend to give large frags their | |
214 | * own buffers as before. | |
215 | */ | |
216 | if ((offset + size > MAX_BUFFER_OFFSET) && | |
217 | (size <= MAX_BUFFER_OFFSET) && offset && !head) | |
218 | return true; | |
219 | ||
220 | return false; | |
221 | } | |
222 | ||
223 | /* | |
224 | * Figure out how many ring slots we're going to need to send @skb to | |
225 | * the guest. This function is essentially a dry run of | |
7376419a | 226 | * xenvif_gop_frag_copy. |
f942dc25 | 227 | */ |
7376419a | 228 | unsigned int xenvif_count_skb_slots(struct xenvif *vif, struct sk_buff *skb) |
f942dc25 IC |
229 | { |
230 | unsigned int count; | |
4f0581d2 WL |
231 | int i, copy_off; |
232 | struct skb_cb_overlay *sco; | |
f942dc25 | 233 | |
4f0581d2 | 234 | count = DIV_ROUND_UP(skb_headlen(skb), PAGE_SIZE); |
f942dc25 | 235 | |
4f0581d2 | 236 | copy_off = skb_headlen(skb) % PAGE_SIZE; |
f942dc25 IC |
237 | |
238 | if (skb_shinfo(skb)->gso_size) | |
239 | count++; | |
240 | ||
4f0581d2 WL |
241 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
242 | unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]); | |
243 | unsigned long offset = skb_shinfo(skb)->frags[i].page_offset; | |
244 | unsigned long bytes; | |
6a8ed462 | 245 | |
4f0581d2 | 246 | offset &= ~PAGE_MASK; |
f942dc25 | 247 | |
4f0581d2 WL |
248 | while (size > 0) { |
249 | BUG_ON(offset >= PAGE_SIZE); | |
250 | BUG_ON(copy_off > MAX_BUFFER_OFFSET); | |
6a8ed462 | 251 | |
4f0581d2 | 252 | bytes = PAGE_SIZE - offset; |
6a8ed462 | 253 | |
4f0581d2 WL |
254 | if (bytes > size) |
255 | bytes = size; | |
256 | ||
257 | if (start_new_rx_buffer(copy_off, bytes, 0)) { | |
258 | count++; | |
259 | copy_off = 0; | |
260 | } | |
6a8ed462 | 261 | |
4f0581d2 WL |
262 | if (copy_off + bytes > MAX_BUFFER_OFFSET) |
263 | bytes = MAX_BUFFER_OFFSET - copy_off; | |
264 | ||
265 | copy_off += bytes; | |
266 | ||
267 | offset += bytes; | |
268 | size -= bytes; | |
269 | ||
270 | if (offset == PAGE_SIZE) | |
271 | offset = 0; | |
272 | } | |
f942dc25 | 273 | } |
4f0581d2 WL |
274 | |
275 | sco = (struct skb_cb_overlay *)skb->cb; | |
276 | sco->peek_slots_count = count; | |
f942dc25 IC |
277 | return count; |
278 | } | |
279 | ||
280 | struct netrx_pending_operations { | |
281 | unsigned copy_prod, copy_cons; | |
282 | unsigned meta_prod, meta_cons; | |
283 | struct gnttab_copy *copy; | |
b3f980bd | 284 | struct xenvif_rx_meta *meta; |
f942dc25 IC |
285 | int copy_off; |
286 | grant_ref_t copy_gref; | |
287 | }; | |
288 | ||
b3f980bd WL |
289 | static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif, |
290 | struct netrx_pending_operations *npo) | |
f942dc25 | 291 | { |
b3f980bd | 292 | struct xenvif_rx_meta *meta; |
f942dc25 IC |
293 | struct xen_netif_rx_request *req; |
294 | ||
295 | req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++); | |
296 | ||
297 | meta = npo->meta + npo->meta_prod++; | |
298 | meta->gso_size = 0; | |
299 | meta->size = 0; | |
300 | meta->id = req->id; | |
301 | ||
302 | npo->copy_off = 0; | |
303 | npo->copy_gref = req->gref; | |
304 | ||
305 | return meta; | |
306 | } | |
307 | ||
4f0581d2 | 308 | /* Set up the grant operations for this fragment. */ |
7376419a WL |
309 | static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb, |
310 | struct netrx_pending_operations *npo, | |
311 | struct page *page, unsigned long size, | |
4f0581d2 | 312 | unsigned long offset, int head, int *first) |
f942dc25 IC |
313 | { |
314 | struct gnttab_copy *copy_gop; | |
b3f980bd | 315 | struct xenvif_rx_meta *meta; |
f942dc25 IC |
316 | unsigned long bytes; |
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 | ||
4f0581d2 | 336 | if (start_new_rx_buffer(npo->copy_off, bytes, head)) { |
f942dc25 IC |
337 | /* |
338 | * Netfront requires there to be some data in the head | |
339 | * buffer. | |
340 | */ | |
4f0581d2 | 341 | BUG_ON(*first); |
f942dc25 IC |
342 | |
343 | meta = get_next_rx_buffer(vif, npo); | |
344 | } | |
345 | ||
f942dc25 IC |
346 | if (npo->copy_off + bytes > MAX_BUFFER_OFFSET) |
347 | bytes = MAX_BUFFER_OFFSET - npo->copy_off; | |
348 | ||
349 | copy_gop = npo->copy + npo->copy_prod++; | |
350 | copy_gop->flags = GNTCOPY_dest_gref; | |
b3f980bd WL |
351 | copy_gop->len = bytes; |
352 | ||
43e9d194 WL |
353 | copy_gop->source.domid = DOMID_SELF; |
354 | copy_gop->source.u.gmfn = virt_to_mfn(page_address(page)); | |
f942dc25 | 355 | copy_gop->source.offset = offset; |
f942dc25 | 356 | |
b3f980bd | 357 | copy_gop->dest.domid = vif->domid; |
f942dc25 IC |
358 | copy_gop->dest.offset = npo->copy_off; |
359 | copy_gop->dest.u.ref = npo->copy_gref; | |
f942dc25 IC |
360 | |
361 | npo->copy_off += bytes; | |
362 | meta->size += bytes; | |
363 | ||
364 | offset += bytes; | |
365 | size -= bytes; | |
366 | ||
6a8ed462 IC |
367 | /* Next frame */ |
368 | if (offset == PAGE_SIZE && size) { | |
369 | BUG_ON(!PageCompound(page)); | |
370 | page++; | |
371 | offset = 0; | |
372 | } | |
373 | ||
f942dc25 | 374 | /* Leave a gap for the GSO descriptor. */ |
4f0581d2 | 375 | if (*first && skb_shinfo(skb)->gso_size && !vif->gso_prefix) |
f942dc25 IC |
376 | vif->rx.req_cons++; |
377 | ||
4f0581d2 | 378 | *first = 0; /* There must be something in this buffer now. */ |
f942dc25 IC |
379 | |
380 | } | |
381 | } | |
382 | ||
383 | /* | |
384 | * Prepare an SKB to be transmitted to the frontend. | |
385 | * | |
386 | * This function is responsible for allocating grant operations, meta | |
387 | * structures, etc. | |
388 | * | |
389 | * It returns the number of meta structures consumed. The number of | |
390 | * ring slots used is always equal to the number of meta slots used | |
391 | * plus the number of GSO descriptors used. Currently, we use either | |
392 | * zero GSO descriptors (for non-GSO packets) or one descriptor (for | |
393 | * frontend-side LRO). | |
394 | */ | |
7376419a WL |
395 | static int xenvif_gop_skb(struct sk_buff *skb, |
396 | struct netrx_pending_operations *npo) | |
f942dc25 IC |
397 | { |
398 | struct xenvif *vif = netdev_priv(skb->dev); | |
399 | int nr_frags = skb_shinfo(skb)->nr_frags; | |
400 | int i; | |
401 | struct xen_netif_rx_request *req; | |
b3f980bd | 402 | struct xenvif_rx_meta *meta; |
f942dc25 | 403 | unsigned char *data; |
4f0581d2 | 404 | int first = 1; |
f942dc25 IC |
405 | int old_meta_prod; |
406 | ||
407 | old_meta_prod = npo->meta_prod; | |
408 | ||
409 | /* Set up a GSO prefix descriptor, if necessary */ | |
410 | if (skb_shinfo(skb)->gso_size && vif->gso_prefix) { | |
411 | req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++); | |
412 | meta = npo->meta + npo->meta_prod++; | |
413 | meta->gso_size = skb_shinfo(skb)->gso_size; | |
414 | meta->size = 0; | |
415 | meta->id = req->id; | |
416 | } | |
417 | ||
418 | req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++); | |
419 | meta = npo->meta + npo->meta_prod++; | |
420 | ||
421 | if (!vif->gso_prefix) | |
422 | meta->gso_size = skb_shinfo(skb)->gso_size; | |
423 | else | |
424 | meta->gso_size = 0; | |
425 | ||
426 | meta->size = 0; | |
427 | meta->id = req->id; | |
428 | npo->copy_off = 0; | |
429 | npo->copy_gref = req->gref; | |
430 | ||
431 | data = skb->data; | |
432 | while (data < skb_tail_pointer(skb)) { | |
433 | unsigned int offset = offset_in_page(data); | |
434 | unsigned int len = PAGE_SIZE - offset; | |
435 | ||
436 | if (data + len > skb_tail_pointer(skb)) | |
437 | len = skb_tail_pointer(skb) - data; | |
438 | ||
7376419a | 439 | xenvif_gop_frag_copy(vif, skb, npo, |
4f0581d2 | 440 | virt_to_page(data), len, offset, 1, &first); |
f942dc25 IC |
441 | data += len; |
442 | } | |
443 | ||
444 | for (i = 0; i < nr_frags; i++) { | |
7376419a WL |
445 | xenvif_gop_frag_copy(vif, skb, npo, |
446 | skb_frag_page(&skb_shinfo(skb)->frags[i]), | |
447 | skb_frag_size(&skb_shinfo(skb)->frags[i]), | |
448 | skb_shinfo(skb)->frags[i].page_offset, | |
4f0581d2 | 449 | 0, &first); |
f942dc25 IC |
450 | } |
451 | ||
452 | return npo->meta_prod - old_meta_prod; | |
453 | } | |
454 | ||
455 | /* | |
7376419a | 456 | * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was |
f942dc25 IC |
457 | * used to set up the operations on the top of |
458 | * netrx_pending_operations, which have since been done. Check that | |
459 | * they didn't give any errors and advance over them. | |
460 | */ | |
7376419a WL |
461 | static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots, |
462 | struct netrx_pending_operations *npo) | |
f942dc25 IC |
463 | { |
464 | struct gnttab_copy *copy_op; | |
465 | int status = XEN_NETIF_RSP_OKAY; | |
466 | int i; | |
467 | ||
468 | for (i = 0; i < nr_meta_slots; i++) { | |
469 | copy_op = npo->copy + npo->copy_cons++; | |
470 | if (copy_op->status != GNTST_okay) { | |
471 | netdev_dbg(vif->dev, | |
472 | "Bad status %d from copy to DOM%d.\n", | |
473 | copy_op->status, vif->domid); | |
474 | status = XEN_NETIF_RSP_ERROR; | |
475 | } | |
476 | } | |
477 | ||
478 | return status; | |
479 | } | |
480 | ||
7376419a WL |
481 | static void xenvif_add_frag_responses(struct xenvif *vif, int status, |
482 | struct xenvif_rx_meta *meta, | |
483 | int nr_meta_slots) | |
f942dc25 IC |
484 | { |
485 | int i; | |
486 | unsigned long offset; | |
487 | ||
488 | /* No fragments used */ | |
489 | if (nr_meta_slots <= 1) | |
490 | return; | |
491 | ||
492 | nr_meta_slots--; | |
493 | ||
494 | for (i = 0; i < nr_meta_slots; i++) { | |
495 | int flags; | |
496 | if (i == nr_meta_slots - 1) | |
497 | flags = 0; | |
498 | else | |
499 | flags = XEN_NETRXF_more_data; | |
500 | ||
501 | offset = 0; | |
502 | make_rx_response(vif, meta[i].id, status, offset, | |
503 | meta[i].size, flags); | |
504 | } | |
505 | } | |
506 | ||
7376419a | 507 | static void xenvif_kick_thread(struct xenvif *vif) |
b3f980bd WL |
508 | { |
509 | wake_up(&vif->wq); | |
510 | } | |
511 | ||
7376419a | 512 | void xenvif_rx_action(struct xenvif *vif) |
f942dc25 | 513 | { |
f942dc25 | 514 | s8 status; |
e1f00a69 | 515 | u16 flags; |
f942dc25 IC |
516 | struct xen_netif_rx_response *resp; |
517 | struct sk_buff_head rxq; | |
518 | struct sk_buff *skb; | |
519 | LIST_HEAD(notify); | |
520 | int ret; | |
521 | int nr_frags; | |
522 | int count; | |
523 | unsigned long offset; | |
524 | struct skb_cb_overlay *sco; | |
b3f980bd | 525 | int need_to_notify = 0; |
f942dc25 IC |
526 | |
527 | struct netrx_pending_operations npo = { | |
b3f980bd WL |
528 | .copy = vif->grant_copy_op, |
529 | .meta = vif->meta, | |
f942dc25 IC |
530 | }; |
531 | ||
532 | skb_queue_head_init(&rxq); | |
533 | ||
534 | count = 0; | |
535 | ||
b3f980bd | 536 | while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) { |
4f0581d2 WL |
537 | RING_IDX old_rx_req_cons; |
538 | ||
f942dc25 IC |
539 | vif = netdev_priv(skb->dev); |
540 | nr_frags = skb_shinfo(skb)->nr_frags; | |
541 | ||
4f0581d2 | 542 | old_rx_req_cons = vif->rx.req_cons; |
f942dc25 | 543 | sco = (struct skb_cb_overlay *)skb->cb; |
7376419a | 544 | sco->meta_slots_used = xenvif_gop_skb(skb, &npo); |
f942dc25 | 545 | |
4f0581d2 | 546 | count += vif->rx.req_cons - old_rx_req_cons; |
f942dc25 IC |
547 | |
548 | __skb_queue_tail(&rxq, skb); | |
549 | ||
4f0581d2 WL |
550 | skb = skb_peek(&vif->rx_queue); |
551 | if (skb == NULL) | |
552 | break; | |
553 | sco = (struct skb_cb_overlay *)skb->cb; | |
554 | ||
f942dc25 | 555 | /* Filled the batch queue? */ |
4f0581d2 | 556 | if (count + sco->peek_slots_count >= XEN_NETIF_RX_RING_SIZE) |
f942dc25 IC |
557 | break; |
558 | } | |
559 | ||
b3f980bd | 560 | BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta)); |
f942dc25 IC |
561 | |
562 | if (!npo.copy_prod) | |
563 | return; | |
564 | ||
b3f980bd WL |
565 | BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op)); |
566 | gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod); | |
f942dc25 IC |
567 | |
568 | while ((skb = __skb_dequeue(&rxq)) != NULL) { | |
569 | sco = (struct skb_cb_overlay *)skb->cb; | |
570 | ||
571 | vif = netdev_priv(skb->dev); | |
572 | ||
b3f980bd | 573 | if (vif->meta[npo.meta_cons].gso_size && vif->gso_prefix) { |
f942dc25 | 574 | resp = RING_GET_RESPONSE(&vif->rx, |
b3f980bd | 575 | vif->rx.rsp_prod_pvt++); |
f942dc25 IC |
576 | |
577 | resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data; | |
578 | ||
b3f980bd WL |
579 | resp->offset = vif->meta[npo.meta_cons].gso_size; |
580 | resp->id = vif->meta[npo.meta_cons].id; | |
f942dc25 IC |
581 | resp->status = sco->meta_slots_used; |
582 | ||
583 | npo.meta_cons++; | |
584 | sco->meta_slots_used--; | |
585 | } | |
586 | ||
587 | ||
588 | vif->dev->stats.tx_bytes += skb->len; | |
589 | vif->dev->stats.tx_packets++; | |
590 | ||
7376419a | 591 | status = xenvif_check_gop(vif, sco->meta_slots_used, &npo); |
f942dc25 IC |
592 | |
593 | if (sco->meta_slots_used == 1) | |
594 | flags = 0; | |
595 | else | |
596 | flags = XEN_NETRXF_more_data; | |
597 | ||
598 | if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */ | |
599 | flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated; | |
600 | else if (skb->ip_summed == CHECKSUM_UNNECESSARY) | |
601 | /* remote but checksummed. */ | |
602 | flags |= XEN_NETRXF_data_validated; | |
603 | ||
604 | offset = 0; | |
b3f980bd | 605 | resp = make_rx_response(vif, vif->meta[npo.meta_cons].id, |
f942dc25 | 606 | status, offset, |
b3f980bd | 607 | vif->meta[npo.meta_cons].size, |
f942dc25 IC |
608 | flags); |
609 | ||
b3f980bd | 610 | if (vif->meta[npo.meta_cons].gso_size && !vif->gso_prefix) { |
f942dc25 IC |
611 | struct xen_netif_extra_info *gso = |
612 | (struct xen_netif_extra_info *) | |
613 | RING_GET_RESPONSE(&vif->rx, | |
614 | vif->rx.rsp_prod_pvt++); | |
615 | ||
616 | resp->flags |= XEN_NETRXF_extra_info; | |
617 | ||
b3f980bd | 618 | gso->u.gso.size = vif->meta[npo.meta_cons].gso_size; |
f942dc25 IC |
619 | gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; |
620 | gso->u.gso.pad = 0; | |
621 | gso->u.gso.features = 0; | |
622 | ||
623 | gso->type = XEN_NETIF_EXTRA_TYPE_GSO; | |
624 | gso->flags = 0; | |
625 | } | |
626 | ||
7376419a WL |
627 | xenvif_add_frag_responses(vif, status, |
628 | vif->meta + npo.meta_cons + 1, | |
629 | sco->meta_slots_used); | |
f942dc25 IC |
630 | |
631 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret); | |
f942dc25 | 632 | |
b3f980bd WL |
633 | if (ret) |
634 | need_to_notify = 1; | |
635 | ||
f942dc25 IC |
636 | xenvif_notify_tx_completion(vif); |
637 | ||
f942dc25 IC |
638 | npo.meta_cons += sco->meta_slots_used; |
639 | dev_kfree_skb(skb); | |
640 | } | |
641 | ||
b3f980bd | 642 | if (need_to_notify) |
e1f00a69 | 643 | notify_remote_via_irq(vif->rx_irq); |
f942dc25 IC |
644 | |
645 | /* More work to do? */ | |
b3f980bd | 646 | if (!skb_queue_empty(&vif->rx_queue)) |
7376419a | 647 | xenvif_kick_thread(vif); |
f942dc25 IC |
648 | } |
649 | ||
7376419a | 650 | void xenvif_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb) |
f942dc25 | 651 | { |
b3f980bd | 652 | skb_queue_tail(&vif->rx_queue, skb); |
f942dc25 | 653 | |
7376419a | 654 | xenvif_kick_thread(vif); |
f942dc25 IC |
655 | } |
656 | ||
7376419a | 657 | void xenvif_check_rx_xenvif(struct xenvif *vif) |
f942dc25 IC |
658 | { |
659 | int more_to_do; | |
660 | ||
661 | RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do); | |
662 | ||
663 | if (more_to_do) | |
b3f980bd | 664 | napi_schedule(&vif->napi); |
f942dc25 IC |
665 | } |
666 | ||
667 | static void tx_add_credit(struct xenvif *vif) | |
668 | { | |
669 | unsigned long max_burst, max_credit; | |
670 | ||
671 | /* | |
672 | * Allow a burst big enough to transmit a jumbo packet of up to 128kB. | |
673 | * Otherwise the interface can seize up due to insufficient credit. | |
674 | */ | |
675 | max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size; | |
676 | max_burst = min(max_burst, 131072UL); | |
677 | max_burst = max(max_burst, vif->credit_bytes); | |
678 | ||
679 | /* Take care that adding a new chunk of credit doesn't wrap to zero. */ | |
680 | max_credit = vif->remaining_credit + vif->credit_bytes; | |
681 | if (max_credit < vif->remaining_credit) | |
682 | max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */ | |
683 | ||
684 | vif->remaining_credit = min(max_credit, max_burst); | |
685 | } | |
686 | ||
687 | static void tx_credit_callback(unsigned long data) | |
688 | { | |
689 | struct xenvif *vif = (struct xenvif *)data; | |
690 | tx_add_credit(vif); | |
7376419a | 691 | xenvif_check_rx_xenvif(vif); |
f942dc25 IC |
692 | } |
693 | ||
7376419a WL |
694 | static void xenvif_tx_err(struct xenvif *vif, |
695 | struct xen_netif_tx_request *txp, RING_IDX end) | |
f942dc25 IC |
696 | { |
697 | RING_IDX cons = vif->tx.req_cons; | |
698 | ||
699 | do { | |
700 | make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR); | |
b9149729 | 701 | if (cons == end) |
f942dc25 IC |
702 | break; |
703 | txp = RING_GET_REQUEST(&vif->tx, cons++); | |
704 | } while (1); | |
705 | vif->tx.req_cons = cons; | |
f942dc25 IC |
706 | } |
707 | ||
7376419a | 708 | static void xenvif_fatal_tx_err(struct xenvif *vif) |
48856286 IC |
709 | { |
710 | netdev_err(vif->dev, "fatal error; disabling device\n"); | |
711 | xenvif_carrier_off(vif); | |
48856286 IC |
712 | } |
713 | ||
7376419a WL |
714 | static int xenvif_count_requests(struct xenvif *vif, |
715 | struct xen_netif_tx_request *first, | |
716 | struct xen_netif_tx_request *txp, | |
717 | int work_to_do) | |
f942dc25 IC |
718 | { |
719 | RING_IDX cons = vif->tx.req_cons; | |
2810e5b9 WL |
720 | int slots = 0; |
721 | int drop_err = 0; | |
59ccb4eb | 722 | int more_data; |
f942dc25 IC |
723 | |
724 | if (!(first->flags & XEN_NETTXF_more_data)) | |
725 | return 0; | |
726 | ||
727 | do { | |
59ccb4eb WL |
728 | struct xen_netif_tx_request dropped_tx = { 0 }; |
729 | ||
2810e5b9 WL |
730 | if (slots >= work_to_do) { |
731 | netdev_err(vif->dev, | |
732 | "Asked for %d slots but exceeds this limit\n", | |
733 | work_to_do); | |
7376419a | 734 | xenvif_fatal_tx_err(vif); |
35876b5f | 735 | return -ENODATA; |
f942dc25 IC |
736 | } |
737 | ||
2810e5b9 WL |
738 | /* This guest is really using too many slots and |
739 | * considered malicious. | |
740 | */ | |
37641494 | 741 | if (unlikely(slots >= fatal_skb_slots)) { |
2810e5b9 WL |
742 | netdev_err(vif->dev, |
743 | "Malicious frontend using %d slots, threshold %u\n", | |
37641494 | 744 | slots, fatal_skb_slots); |
7376419a | 745 | xenvif_fatal_tx_err(vif); |
35876b5f | 746 | return -E2BIG; |
f942dc25 IC |
747 | } |
748 | ||
2810e5b9 | 749 | /* Xen network protocol had implicit dependency on |
37641494 WL |
750 | * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to |
751 | * the historical MAX_SKB_FRAGS value 18 to honor the | |
752 | * same behavior as before. Any packet using more than | |
753 | * 18 slots but less than fatal_skb_slots slots is | |
754 | * dropped | |
2810e5b9 | 755 | */ |
37641494 | 756 | if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) { |
2810e5b9 WL |
757 | if (net_ratelimit()) |
758 | netdev_dbg(vif->dev, | |
759 | "Too many slots (%d) exceeding limit (%d), dropping packet\n", | |
37641494 | 760 | slots, XEN_NETBK_LEGACY_SLOTS_MAX); |
2810e5b9 WL |
761 | drop_err = -E2BIG; |
762 | } | |
763 | ||
59ccb4eb WL |
764 | if (drop_err) |
765 | txp = &dropped_tx; | |
766 | ||
2810e5b9 | 767 | memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots), |
f942dc25 | 768 | sizeof(*txp)); |
03393fd5 WL |
769 | |
770 | /* If the guest submitted a frame >= 64 KiB then | |
771 | * first->size overflowed and following slots will | |
772 | * appear to be larger than the frame. | |
773 | * | |
774 | * This cannot be fatal error as there are buggy | |
775 | * frontends that do this. | |
776 | * | |
777 | * Consume all slots and drop the packet. | |
778 | */ | |
779 | if (!drop_err && txp->size > first->size) { | |
780 | if (net_ratelimit()) | |
781 | netdev_dbg(vif->dev, | |
782 | "Invalid tx request, slot size %u > remaining size %u\n", | |
783 | txp->size, first->size); | |
784 | drop_err = -EIO; | |
f942dc25 IC |
785 | } |
786 | ||
787 | first->size -= txp->size; | |
2810e5b9 | 788 | slots++; |
f942dc25 IC |
789 | |
790 | if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) { | |
2810e5b9 | 791 | netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n", |
f942dc25 | 792 | txp->offset, txp->size); |
7376419a | 793 | xenvif_fatal_tx_err(vif); |
35876b5f | 794 | return -EINVAL; |
f942dc25 | 795 | } |
59ccb4eb WL |
796 | |
797 | more_data = txp->flags & XEN_NETTXF_more_data; | |
798 | ||
799 | if (!drop_err) | |
800 | txp++; | |
801 | ||
802 | } while (more_data); | |
2810e5b9 WL |
803 | |
804 | if (drop_err) { | |
7376419a | 805 | xenvif_tx_err(vif, first, cons + slots); |
2810e5b9 WL |
806 | return drop_err; |
807 | } | |
808 | ||
809 | return slots; | |
f942dc25 IC |
810 | } |
811 | ||
7376419a WL |
812 | static struct page *xenvif_alloc_page(struct xenvif *vif, |
813 | u16 pending_idx) | |
f942dc25 IC |
814 | { |
815 | struct page *page; | |
b3f980bd WL |
816 | |
817 | page = alloc_page(GFP_ATOMIC|__GFP_COLD); | |
f942dc25 IC |
818 | if (!page) |
819 | return NULL; | |
b3f980bd WL |
820 | vif->mmap_pages[pending_idx] = page; |
821 | ||
f942dc25 IC |
822 | return page; |
823 | } | |
824 | ||
7376419a WL |
825 | static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif, |
826 | struct sk_buff *skb, | |
827 | struct xen_netif_tx_request *txp, | |
828 | struct gnttab_copy *gop) | |
f942dc25 IC |
829 | { |
830 | struct skb_shared_info *shinfo = skb_shinfo(skb); | |
831 | skb_frag_t *frags = shinfo->frags; | |
ea066ad1 | 832 | u16 pending_idx = *((u16 *)skb->data); |
2810e5b9 WL |
833 | u16 head_idx = 0; |
834 | int slot, start; | |
835 | struct page *page; | |
836 | pending_ring_idx_t index, start_idx = 0; | |
837 | uint16_t dst_offset; | |
838 | unsigned int nr_slots; | |
839 | struct pending_tx_info *first = NULL; | |
840 | ||
841 | /* At this point shinfo->nr_frags is in fact the number of | |
37641494 | 842 | * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX. |
2810e5b9 WL |
843 | */ |
844 | nr_slots = shinfo->nr_frags; | |
f942dc25 IC |
845 | |
846 | /* Skip first skb fragment if it is on same page as header fragment. */ | |
ea066ad1 | 847 | start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx); |
f942dc25 | 848 | |
2810e5b9 WL |
849 | /* Coalesce tx requests, at this point the packet passed in |
850 | * should be <= 64K. Any packets larger than 64K have been | |
7376419a | 851 | * handled in xenvif_count_requests(). |
2810e5b9 WL |
852 | */ |
853 | for (shinfo->nr_frags = slot = start; slot < nr_slots; | |
854 | shinfo->nr_frags++) { | |
f942dc25 | 855 | struct pending_tx_info *pending_tx_info = |
b3f980bd | 856 | vif->pending_tx_info; |
f942dc25 | 857 | |
b3f980bd | 858 | page = alloc_page(GFP_ATOMIC|__GFP_COLD); |
f942dc25 | 859 | if (!page) |
4cc7c1cb | 860 | goto err; |
f942dc25 | 861 | |
2810e5b9 WL |
862 | dst_offset = 0; |
863 | first = NULL; | |
864 | while (dst_offset < PAGE_SIZE && slot < nr_slots) { | |
865 | gop->flags = GNTCOPY_source_gref; | |
866 | ||
867 | gop->source.u.ref = txp->gref; | |
868 | gop->source.domid = vif->domid; | |
869 | gop->source.offset = txp->offset; | |
870 | ||
871 | gop->dest.domid = DOMID_SELF; | |
872 | ||
873 | gop->dest.offset = dst_offset; | |
874 | gop->dest.u.gmfn = virt_to_mfn(page_address(page)); | |
875 | ||
876 | if (dst_offset + txp->size > PAGE_SIZE) { | |
877 | /* This page can only merge a portion | |
878 | * of tx request. Do not increment any | |
879 | * pointer / counter here. The txp | |
880 | * will be dealt with in future | |
881 | * rounds, eventually hitting the | |
882 | * `else` branch. | |
883 | */ | |
884 | gop->len = PAGE_SIZE - dst_offset; | |
885 | txp->offset += gop->len; | |
886 | txp->size -= gop->len; | |
887 | dst_offset += gop->len; /* quit loop */ | |
888 | } else { | |
889 | /* This tx request can be merged in the page */ | |
890 | gop->len = txp->size; | |
891 | dst_offset += gop->len; | |
892 | ||
b3f980bd | 893 | index = pending_index(vif->pending_cons++); |
2810e5b9 | 894 | |
b3f980bd | 895 | pending_idx = vif->pending_ring[index]; |
2810e5b9 WL |
896 | |
897 | memcpy(&pending_tx_info[pending_idx].req, txp, | |
898 | sizeof(*txp)); | |
2810e5b9 WL |
899 | |
900 | /* Poison these fields, corresponding | |
901 | * fields for head tx req will be set | |
902 | * to correct values after the loop. | |
903 | */ | |
b3f980bd | 904 | vif->mmap_pages[pending_idx] = (void *)(~0UL); |
2810e5b9 WL |
905 | pending_tx_info[pending_idx].head = |
906 | INVALID_PENDING_RING_IDX; | |
907 | ||
908 | if (!first) { | |
909 | first = &pending_tx_info[pending_idx]; | |
910 | start_idx = index; | |
911 | head_idx = pending_idx; | |
912 | } | |
913 | ||
914 | txp++; | |
915 | slot++; | |
916 | } | |
f942dc25 | 917 | |
2810e5b9 WL |
918 | gop++; |
919 | } | |
f942dc25 | 920 | |
2810e5b9 WL |
921 | first->req.offset = 0; |
922 | first->req.size = dst_offset; | |
923 | first->head = start_idx; | |
b3f980bd | 924 | vif->mmap_pages[head_idx] = page; |
2810e5b9 | 925 | frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx); |
f942dc25 IC |
926 | } |
927 | ||
2810e5b9 WL |
928 | BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS); |
929 | ||
f942dc25 | 930 | return gop; |
4cc7c1cb IC |
931 | err: |
932 | /* Unwind, freeing all pages and sending error responses. */ | |
2810e5b9 | 933 | while (shinfo->nr_frags-- > start) { |
7376419a | 934 | xenvif_idx_release(vif, |
2810e5b9 WL |
935 | frag_get_pending_idx(&frags[shinfo->nr_frags]), |
936 | XEN_NETIF_RSP_ERROR); | |
4cc7c1cb IC |
937 | } |
938 | /* The head too, if necessary. */ | |
939 | if (start) | |
7376419a | 940 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR); |
4cc7c1cb IC |
941 | |
942 | return NULL; | |
f942dc25 IC |
943 | } |
944 | ||
7376419a WL |
945 | static int xenvif_tx_check_gop(struct xenvif *vif, |
946 | struct sk_buff *skb, | |
947 | struct gnttab_copy **gopp) | |
f942dc25 IC |
948 | { |
949 | struct gnttab_copy *gop = *gopp; | |
ea066ad1 | 950 | u16 pending_idx = *((u16 *)skb->data); |
f942dc25 | 951 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
2810e5b9 | 952 | struct pending_tx_info *tx_info; |
f942dc25 IC |
953 | int nr_frags = shinfo->nr_frags; |
954 | int i, err, start; | |
2810e5b9 | 955 | u16 peek; /* peek into next tx request */ |
f942dc25 IC |
956 | |
957 | /* Check status of header. */ | |
958 | err = gop->status; | |
7d5145d8 | 959 | if (unlikely(err)) |
7376419a | 960 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR); |
f942dc25 IC |
961 | |
962 | /* Skip first skb fragment if it is on same page as header fragment. */ | |
ea066ad1 | 963 | start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx); |
f942dc25 IC |
964 | |
965 | for (i = start; i < nr_frags; i++) { | |
966 | int j, newerr; | |
2810e5b9 | 967 | pending_ring_idx_t head; |
f942dc25 | 968 | |
ea066ad1 | 969 | pending_idx = frag_get_pending_idx(&shinfo->frags[i]); |
b3f980bd | 970 | tx_info = &vif->pending_tx_info[pending_idx]; |
2810e5b9 | 971 | head = tx_info->head; |
f942dc25 IC |
972 | |
973 | /* Check error status: if okay then remember grant handle. */ | |
2810e5b9 WL |
974 | do { |
975 | newerr = (++gop)->status; | |
976 | if (newerr) | |
977 | break; | |
b3f980bd WL |
978 | peek = vif->pending_ring[pending_index(++head)]; |
979 | } while (!pending_tx_is_head(vif, peek)); | |
2810e5b9 | 980 | |
f942dc25 IC |
981 | if (likely(!newerr)) { |
982 | /* Had a previous error? Invalidate this fragment. */ | |
983 | if (unlikely(err)) | |
7376419a WL |
984 | xenvif_idx_release(vif, pending_idx, |
985 | XEN_NETIF_RSP_OKAY); | |
f942dc25 IC |
986 | continue; |
987 | } | |
988 | ||
989 | /* Error on this fragment: respond to client with an error. */ | |
7376419a | 990 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR); |
f942dc25 IC |
991 | |
992 | /* Not the first error? Preceding frags already invalidated. */ | |
993 | if (err) | |
994 | continue; | |
995 | ||
996 | /* First error: invalidate header and preceding fragments. */ | |
997 | pending_idx = *((u16 *)skb->data); | |
7376419a | 998 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY); |
f942dc25 | 999 | for (j = start; j < i; j++) { |
5ccb3ea7 | 1000 | pending_idx = frag_get_pending_idx(&shinfo->frags[j]); |
7376419a WL |
1001 | xenvif_idx_release(vif, pending_idx, |
1002 | XEN_NETIF_RSP_OKAY); | |
f942dc25 IC |
1003 | } |
1004 | ||
1005 | /* Remember the error: invalidate all subsequent fragments. */ | |
1006 | err = newerr; | |
1007 | } | |
1008 | ||
1009 | *gopp = gop + 1; | |
1010 | return err; | |
1011 | } | |
1012 | ||
7376419a | 1013 | static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb) |
f942dc25 IC |
1014 | { |
1015 | struct skb_shared_info *shinfo = skb_shinfo(skb); | |
1016 | int nr_frags = shinfo->nr_frags; | |
1017 | int i; | |
1018 | ||
1019 | for (i = 0; i < nr_frags; i++) { | |
1020 | skb_frag_t *frag = shinfo->frags + i; | |
1021 | struct xen_netif_tx_request *txp; | |
ea066ad1 IC |
1022 | struct page *page; |
1023 | u16 pending_idx; | |
f942dc25 | 1024 | |
ea066ad1 | 1025 | pending_idx = frag_get_pending_idx(frag); |
f942dc25 | 1026 | |
b3f980bd WL |
1027 | txp = &vif->pending_tx_info[pending_idx].req; |
1028 | page = virt_to_page(idx_to_kaddr(vif, pending_idx)); | |
ea066ad1 | 1029 | __skb_fill_page_desc(skb, i, page, txp->offset, txp->size); |
f942dc25 IC |
1030 | skb->len += txp->size; |
1031 | skb->data_len += txp->size; | |
1032 | skb->truesize += txp->size; | |
1033 | ||
7376419a | 1034 | /* Take an extra reference to offset xenvif_idx_release */ |
b3f980bd | 1035 | get_page(vif->mmap_pages[pending_idx]); |
7376419a | 1036 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY); |
f942dc25 IC |
1037 | } |
1038 | } | |
1039 | ||
7376419a | 1040 | static int xenvif_get_extras(struct xenvif *vif, |
f942dc25 IC |
1041 | struct xen_netif_extra_info *extras, |
1042 | int work_to_do) | |
1043 | { | |
1044 | struct xen_netif_extra_info extra; | |
1045 | RING_IDX cons = vif->tx.req_cons; | |
1046 | ||
1047 | do { | |
1048 | if (unlikely(work_to_do-- <= 0)) { | |
48856286 | 1049 | netdev_err(vif->dev, "Missing extra info\n"); |
7376419a | 1050 | xenvif_fatal_tx_err(vif); |
f942dc25 IC |
1051 | return -EBADR; |
1052 | } | |
1053 | ||
1054 | memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons), | |
1055 | sizeof(extra)); | |
1056 | if (unlikely(!extra.type || | |
1057 | extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) { | |
1058 | vif->tx.req_cons = ++cons; | |
48856286 | 1059 | netdev_err(vif->dev, |
f942dc25 | 1060 | "Invalid extra type: %d\n", extra.type); |
7376419a | 1061 | xenvif_fatal_tx_err(vif); |
f942dc25 IC |
1062 | return -EINVAL; |
1063 | } | |
1064 | ||
1065 | memcpy(&extras[extra.type - 1], &extra, sizeof(extra)); | |
1066 | vif->tx.req_cons = ++cons; | |
1067 | } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); | |
1068 | ||
1069 | return work_to_do; | |
1070 | } | |
1071 | ||
7376419a WL |
1072 | static int xenvif_set_skb_gso(struct xenvif *vif, |
1073 | struct sk_buff *skb, | |
1074 | struct xen_netif_extra_info *gso) | |
f942dc25 IC |
1075 | { |
1076 | if (!gso->u.gso.size) { | |
48856286 | 1077 | netdev_err(vif->dev, "GSO size must not be zero.\n"); |
7376419a | 1078 | xenvif_fatal_tx_err(vif); |
f942dc25 IC |
1079 | return -EINVAL; |
1080 | } | |
1081 | ||
1082 | /* Currently only TCPv4 S.O. is supported. */ | |
1083 | if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) { | |
48856286 | 1084 | netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type); |
7376419a | 1085 | xenvif_fatal_tx_err(vif); |
f942dc25 IC |
1086 | return -EINVAL; |
1087 | } | |
1088 | ||
1089 | skb_shinfo(skb)->gso_size = gso->u.gso.size; | |
1090 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; | |
1091 | ||
1092 | /* Header must be checked, and gso_segs computed. */ | |
1093 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
1094 | skb_shinfo(skb)->gso_segs = 0; | |
1095 | ||
1096 | return 0; | |
1097 | } | |
1098 | ||
1099 | static int checksum_setup(struct xenvif *vif, struct sk_buff *skb) | |
1100 | { | |
1101 | struct iphdr *iph; | |
f942dc25 IC |
1102 | int err = -EPROTO; |
1103 | int recalculate_partial_csum = 0; | |
1104 | ||
1105 | /* | |
1106 | * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy | |
1107 | * peers can fail to set NETRXF_csum_blank when sending a GSO | |
1108 | * frame. In this case force the SKB to CHECKSUM_PARTIAL and | |
1109 | * recalculate the partial checksum. | |
1110 | */ | |
1111 | if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) { | |
1112 | vif->rx_gso_checksum_fixup++; | |
1113 | skb->ip_summed = CHECKSUM_PARTIAL; | |
1114 | recalculate_partial_csum = 1; | |
1115 | } | |
1116 | ||
1117 | /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ | |
1118 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
1119 | return 0; | |
1120 | ||
1121 | if (skb->protocol != htons(ETH_P_IP)) | |
1122 | goto out; | |
1123 | ||
1124 | iph = (void *)skb->data; | |
f942dc25 IC |
1125 | switch (iph->protocol) { |
1126 | case IPPROTO_TCP: | |
bea89336 JW |
1127 | if (!skb_partial_csum_set(skb, 4 * iph->ihl, |
1128 | offsetof(struct tcphdr, check))) | |
1129 | goto out; | |
f942dc25 IC |
1130 | |
1131 | if (recalculate_partial_csum) { | |
bea89336 | 1132 | struct tcphdr *tcph = tcp_hdr(skb); |
f942dc25 IC |
1133 | tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, |
1134 | skb->len - iph->ihl*4, | |
1135 | IPPROTO_TCP, 0); | |
1136 | } | |
1137 | break; | |
1138 | case IPPROTO_UDP: | |
bea89336 JW |
1139 | if (!skb_partial_csum_set(skb, 4 * iph->ihl, |
1140 | offsetof(struct udphdr, check))) | |
1141 | goto out; | |
f942dc25 IC |
1142 | |
1143 | if (recalculate_partial_csum) { | |
bea89336 | 1144 | struct udphdr *udph = udp_hdr(skb); |
f942dc25 IC |
1145 | udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, |
1146 | skb->len - iph->ihl*4, | |
1147 | IPPROTO_UDP, 0); | |
1148 | } | |
1149 | break; | |
1150 | default: | |
1151 | if (net_ratelimit()) | |
1152 | netdev_err(vif->dev, | |
1153 | "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n", | |
1154 | iph->protocol); | |
1155 | goto out; | |
1156 | } | |
1157 | ||
f942dc25 IC |
1158 | err = 0; |
1159 | ||
1160 | out: | |
1161 | return err; | |
1162 | } | |
1163 | ||
1164 | static bool tx_credit_exceeded(struct xenvif *vif, unsigned size) | |
1165 | { | |
1166 | unsigned long now = jiffies; | |
1167 | unsigned long next_credit = | |
1168 | vif->credit_timeout.expires + | |
1169 | msecs_to_jiffies(vif->credit_usec / 1000); | |
1170 | ||
1171 | /* Timer could already be pending in rare cases. */ | |
1172 | if (timer_pending(&vif->credit_timeout)) | |
1173 | return true; | |
1174 | ||
1175 | /* Passed the point where we can replenish credit? */ | |
1176 | if (time_after_eq(now, next_credit)) { | |
1177 | vif->credit_timeout.expires = now; | |
1178 | tx_add_credit(vif); | |
1179 | } | |
1180 | ||
1181 | /* Still too big to send right now? Set a callback. */ | |
1182 | if (size > vif->remaining_credit) { | |
1183 | vif->credit_timeout.data = | |
1184 | (unsigned long)vif; | |
1185 | vif->credit_timeout.function = | |
1186 | tx_credit_callback; | |
1187 | mod_timer(&vif->credit_timeout, | |
1188 | next_credit); | |
1189 | ||
1190 | return true; | |
1191 | } | |
1192 | ||
1193 | return false; | |
1194 | } | |
1195 | ||
7376419a | 1196 | static unsigned xenvif_tx_build_gops(struct xenvif *vif) |
f942dc25 | 1197 | { |
b3f980bd | 1198 | struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop; |
f942dc25 IC |
1199 | struct sk_buff *skb; |
1200 | int ret; | |
1201 | ||
b3f980bd WL |
1202 | while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX |
1203 | < MAX_PENDING_REQS)) { | |
f942dc25 | 1204 | struct xen_netif_tx_request txreq; |
37641494 | 1205 | struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX]; |
f942dc25 IC |
1206 | struct page *page; |
1207 | struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1]; | |
1208 | u16 pending_idx; | |
1209 | RING_IDX idx; | |
1210 | int work_to_do; | |
1211 | unsigned int data_len; | |
1212 | pending_ring_idx_t index; | |
1213 | ||
48856286 IC |
1214 | if (vif->tx.sring->req_prod - vif->tx.req_cons > |
1215 | XEN_NETIF_TX_RING_SIZE) { | |
1216 | netdev_err(vif->dev, | |
1217 | "Impossible number of requests. " | |
1218 | "req_prod %d, req_cons %d, size %ld\n", | |
1219 | vif->tx.sring->req_prod, vif->tx.req_cons, | |
1220 | XEN_NETIF_TX_RING_SIZE); | |
7376419a | 1221 | xenvif_fatal_tx_err(vif); |
48856286 IC |
1222 | continue; |
1223 | } | |
1224 | ||
f942dc25 | 1225 | RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do); |
b3f980bd WL |
1226 | if (!work_to_do) |
1227 | break; | |
f942dc25 IC |
1228 | |
1229 | idx = vif->tx.req_cons; | |
1230 | rmb(); /* Ensure that we see the request before we copy it. */ | |
1231 | memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq)); | |
1232 | ||
1233 | /* Credit-based scheduling. */ | |
1234 | if (txreq.size > vif->remaining_credit && | |
b3f980bd WL |
1235 | tx_credit_exceeded(vif, txreq.size)) |
1236 | break; | |
f942dc25 IC |
1237 | |
1238 | vif->remaining_credit -= txreq.size; | |
1239 | ||
1240 | work_to_do--; | |
1241 | vif->tx.req_cons = ++idx; | |
1242 | ||
1243 | memset(extras, 0, sizeof(extras)); | |
1244 | if (txreq.flags & XEN_NETTXF_extra_info) { | |
7376419a WL |
1245 | work_to_do = xenvif_get_extras(vif, extras, |
1246 | work_to_do); | |
f942dc25 | 1247 | idx = vif->tx.req_cons; |
48856286 | 1248 | if (unlikely(work_to_do < 0)) |
b3f980bd | 1249 | break; |
f942dc25 IC |
1250 | } |
1251 | ||
7376419a | 1252 | ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do); |
48856286 | 1253 | if (unlikely(ret < 0)) |
b3f980bd | 1254 | break; |
48856286 | 1255 | |
f942dc25 IC |
1256 | idx += ret; |
1257 | ||
1258 | if (unlikely(txreq.size < ETH_HLEN)) { | |
1259 | netdev_dbg(vif->dev, | |
1260 | "Bad packet size: %d\n", txreq.size); | |
7376419a | 1261 | xenvif_tx_err(vif, &txreq, idx); |
b3f980bd | 1262 | break; |
f942dc25 IC |
1263 | } |
1264 | ||
1265 | /* No crossing a page as the payload mustn't fragment. */ | |
1266 | if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) { | |
48856286 | 1267 | netdev_err(vif->dev, |
f942dc25 IC |
1268 | "txreq.offset: %x, size: %u, end: %lu\n", |
1269 | txreq.offset, txreq.size, | |
1270 | (txreq.offset&~PAGE_MASK) + txreq.size); | |
7376419a | 1271 | xenvif_fatal_tx_err(vif); |
b3f980bd | 1272 | break; |
f942dc25 IC |
1273 | } |
1274 | ||
b3f980bd WL |
1275 | index = pending_index(vif->pending_cons); |
1276 | pending_idx = vif->pending_ring[index]; | |
f942dc25 IC |
1277 | |
1278 | data_len = (txreq.size > PKT_PROT_LEN && | |
37641494 | 1279 | ret < XEN_NETBK_LEGACY_SLOTS_MAX) ? |
f942dc25 IC |
1280 | PKT_PROT_LEN : txreq.size; |
1281 | ||
1282 | skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN, | |
1283 | GFP_ATOMIC | __GFP_NOWARN); | |
1284 | if (unlikely(skb == NULL)) { | |
1285 | netdev_dbg(vif->dev, | |
1286 | "Can't allocate a skb in start_xmit.\n"); | |
7376419a | 1287 | xenvif_tx_err(vif, &txreq, idx); |
f942dc25 IC |
1288 | break; |
1289 | } | |
1290 | ||
1291 | /* Packets passed to netif_rx() must have some headroom. */ | |
1292 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); | |
1293 | ||
1294 | if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) { | |
1295 | struct xen_netif_extra_info *gso; | |
1296 | gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1]; | |
1297 | ||
7376419a WL |
1298 | if (xenvif_set_skb_gso(vif, skb, gso)) { |
1299 | /* Failure in xenvif_set_skb_gso is fatal. */ | |
f942dc25 | 1300 | kfree_skb(skb); |
b3f980bd | 1301 | break; |
f942dc25 IC |
1302 | } |
1303 | } | |
1304 | ||
1305 | /* XXX could copy straight to head */ | |
7376419a | 1306 | page = xenvif_alloc_page(vif, pending_idx); |
f942dc25 IC |
1307 | if (!page) { |
1308 | kfree_skb(skb); | |
7376419a | 1309 | xenvif_tx_err(vif, &txreq, idx); |
b3f980bd | 1310 | break; |
f942dc25 IC |
1311 | } |
1312 | ||
f942dc25 IC |
1313 | gop->source.u.ref = txreq.gref; |
1314 | gop->source.domid = vif->domid; | |
1315 | gop->source.offset = txreq.offset; | |
1316 | ||
1317 | gop->dest.u.gmfn = virt_to_mfn(page_address(page)); | |
1318 | gop->dest.domid = DOMID_SELF; | |
1319 | gop->dest.offset = txreq.offset; | |
1320 | ||
1321 | gop->len = txreq.size; | |
1322 | gop->flags = GNTCOPY_source_gref; | |
1323 | ||
1324 | gop++; | |
1325 | ||
b3f980bd | 1326 | memcpy(&vif->pending_tx_info[pending_idx].req, |
f942dc25 | 1327 | &txreq, sizeof(txreq)); |
b3f980bd | 1328 | vif->pending_tx_info[pending_idx].head = index; |
f942dc25 IC |
1329 | *((u16 *)skb->data) = pending_idx; |
1330 | ||
1331 | __skb_put(skb, data_len); | |
1332 | ||
1333 | skb_shinfo(skb)->nr_frags = ret; | |
1334 | if (data_len < txreq.size) { | |
1335 | skb_shinfo(skb)->nr_frags++; | |
ea066ad1 IC |
1336 | frag_set_pending_idx(&skb_shinfo(skb)->frags[0], |
1337 | pending_idx); | |
f942dc25 | 1338 | } else { |
ea066ad1 IC |
1339 | frag_set_pending_idx(&skb_shinfo(skb)->frags[0], |
1340 | INVALID_PENDING_IDX); | |
f942dc25 IC |
1341 | } |
1342 | ||
b3f980bd | 1343 | vif->pending_cons++; |
f942dc25 | 1344 | |
7376419a | 1345 | request_gop = xenvif_get_requests(vif, skb, txfrags, gop); |
f942dc25 IC |
1346 | if (request_gop == NULL) { |
1347 | kfree_skb(skb); | |
7376419a | 1348 | xenvif_tx_err(vif, &txreq, idx); |
b3f980bd | 1349 | break; |
f942dc25 IC |
1350 | } |
1351 | gop = request_gop; | |
1352 | ||
b3f980bd | 1353 | __skb_queue_tail(&vif->tx_queue, skb); |
1e0b6eac | 1354 | |
f942dc25 | 1355 | vif->tx.req_cons = idx; |
f942dc25 | 1356 | |
b3f980bd | 1357 | if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops)) |
f942dc25 IC |
1358 | break; |
1359 | } | |
1360 | ||
b3f980bd | 1361 | return gop - vif->tx_copy_ops; |
f942dc25 IC |
1362 | } |
1363 | ||
b3f980bd | 1364 | |
7376419a | 1365 | static int xenvif_tx_submit(struct xenvif *vif, int budget) |
f942dc25 | 1366 | { |
b3f980bd | 1367 | struct gnttab_copy *gop = vif->tx_copy_ops; |
f942dc25 | 1368 | struct sk_buff *skb; |
b3f980bd | 1369 | int work_done = 0; |
f942dc25 | 1370 | |
b3f980bd WL |
1371 | while (work_done < budget && |
1372 | (skb = __skb_dequeue(&vif->tx_queue)) != NULL) { | |
f942dc25 | 1373 | struct xen_netif_tx_request *txp; |
f942dc25 IC |
1374 | u16 pending_idx; |
1375 | unsigned data_len; | |
1376 | ||
1377 | pending_idx = *((u16 *)skb->data); | |
b3f980bd | 1378 | txp = &vif->pending_tx_info[pending_idx].req; |
f942dc25 IC |
1379 | |
1380 | /* Check the remap error code. */ | |
7376419a | 1381 | if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) { |
f942dc25 IC |
1382 | netdev_dbg(vif->dev, "netback grant failed.\n"); |
1383 | skb_shinfo(skb)->nr_frags = 0; | |
1384 | kfree_skb(skb); | |
1385 | continue; | |
1386 | } | |
1387 | ||
1388 | data_len = skb->len; | |
1389 | memcpy(skb->data, | |
b3f980bd | 1390 | (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset), |
f942dc25 IC |
1391 | data_len); |
1392 | if (data_len < txp->size) { | |
1393 | /* Append the packet payload as a fragment. */ | |
1394 | txp->offset += data_len; | |
1395 | txp->size -= data_len; | |
1396 | } else { | |
1397 | /* Schedule a response immediately. */ | |
7376419a WL |
1398 | xenvif_idx_release(vif, pending_idx, |
1399 | XEN_NETIF_RSP_OKAY); | |
f942dc25 IC |
1400 | } |
1401 | ||
1402 | if (txp->flags & XEN_NETTXF_csum_blank) | |
1403 | skb->ip_summed = CHECKSUM_PARTIAL; | |
1404 | else if (txp->flags & XEN_NETTXF_data_validated) | |
1405 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
1406 | ||
7376419a | 1407 | xenvif_fill_frags(vif, skb); |
f942dc25 IC |
1408 | |
1409 | /* | |
1410 | * If the initial fragment was < PKT_PROT_LEN then | |
1411 | * pull through some bytes from the other fragments to | |
1412 | * increase the linear region to PKT_PROT_LEN bytes. | |
1413 | */ | |
1414 | if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) { | |
1415 | int target = min_t(int, skb->len, PKT_PROT_LEN); | |
1416 | __pskb_pull_tail(skb, target - skb_headlen(skb)); | |
1417 | } | |
1418 | ||
1419 | skb->dev = vif->dev; | |
1420 | skb->protocol = eth_type_trans(skb, skb->dev); | |
f9ca8f74 | 1421 | skb_reset_network_header(skb); |
f942dc25 IC |
1422 | |
1423 | if (checksum_setup(vif, skb)) { | |
1424 | netdev_dbg(vif->dev, | |
1425 | "Can't setup checksum in net_tx_action\n"); | |
1426 | kfree_skb(skb); | |
1427 | continue; | |
1428 | } | |
1429 | ||
40893fd0 | 1430 | skb_probe_transport_header(skb, 0); |
f9ca8f74 | 1431 | |
f942dc25 IC |
1432 | vif->dev->stats.rx_bytes += skb->len; |
1433 | vif->dev->stats.rx_packets++; | |
1434 | ||
b3f980bd WL |
1435 | work_done++; |
1436 | ||
1437 | netif_receive_skb(skb); | |
f942dc25 | 1438 | } |
b3f980bd WL |
1439 | |
1440 | return work_done; | |
f942dc25 IC |
1441 | } |
1442 | ||
1443 | /* Called after netfront has transmitted */ | |
7376419a | 1444 | int xenvif_tx_action(struct xenvif *vif, int budget) |
f942dc25 IC |
1445 | { |
1446 | unsigned nr_gops; | |
b3f980bd | 1447 | int work_done; |
f942dc25 | 1448 | |
b3f980bd WL |
1449 | if (unlikely(!tx_work_todo(vif))) |
1450 | return 0; | |
1451 | ||
7376419a | 1452 | nr_gops = xenvif_tx_build_gops(vif); |
f942dc25 IC |
1453 | |
1454 | if (nr_gops == 0) | |
b3f980bd WL |
1455 | return 0; |
1456 | ||
1457 | gnttab_batch_copy(vif->tx_copy_ops, nr_gops); | |
f942dc25 | 1458 | |
7376419a | 1459 | work_done = xenvif_tx_submit(vif, nr_gops); |
f942dc25 | 1460 | |
b3f980bd | 1461 | return work_done; |
f942dc25 IC |
1462 | } |
1463 | ||
7376419a WL |
1464 | static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx, |
1465 | u8 status) | |
f942dc25 | 1466 | { |
f942dc25 | 1467 | struct pending_tx_info *pending_tx_info; |
2810e5b9 WL |
1468 | pending_ring_idx_t head; |
1469 | u16 peek; /* peek into next tx request */ | |
1470 | ||
b3f980bd | 1471 | BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL)); |
f942dc25 IC |
1472 | |
1473 | /* Already complete? */ | |
b3f980bd | 1474 | if (vif->mmap_pages[pending_idx] == NULL) |
f942dc25 IC |
1475 | return; |
1476 | ||
b3f980bd | 1477 | pending_tx_info = &vif->pending_tx_info[pending_idx]; |
f942dc25 | 1478 | |
2810e5b9 | 1479 | head = pending_tx_info->head; |
f942dc25 | 1480 | |
b3f980bd WL |
1481 | BUG_ON(!pending_tx_is_head(vif, head)); |
1482 | BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx); | |
f942dc25 | 1483 | |
2810e5b9 WL |
1484 | do { |
1485 | pending_ring_idx_t index; | |
1486 | pending_ring_idx_t idx = pending_index(head); | |
b3f980bd | 1487 | u16 info_idx = vif->pending_ring[idx]; |
f942dc25 | 1488 | |
b3f980bd | 1489 | pending_tx_info = &vif->pending_tx_info[info_idx]; |
2810e5b9 WL |
1490 | make_tx_response(vif, &pending_tx_info->req, status); |
1491 | ||
1492 | /* Setting any number other than | |
1493 | * INVALID_PENDING_RING_IDX indicates this slot is | |
1494 | * starting a new packet / ending a previous packet. | |
1495 | */ | |
1496 | pending_tx_info->head = 0; | |
1497 | ||
b3f980bd WL |
1498 | index = pending_index(vif->pending_prod++); |
1499 | vif->pending_ring[index] = vif->pending_ring[info_idx]; | |
f942dc25 | 1500 | |
b3f980bd | 1501 | peek = vif->pending_ring[pending_index(++head)]; |
2810e5b9 | 1502 | |
b3f980bd | 1503 | } while (!pending_tx_is_head(vif, peek)); |
2810e5b9 | 1504 | |
b3f980bd WL |
1505 | put_page(vif->mmap_pages[pending_idx]); |
1506 | vif->mmap_pages[pending_idx] = NULL; | |
f942dc25 IC |
1507 | } |
1508 | ||
2810e5b9 | 1509 | |
f942dc25 IC |
1510 | static void make_tx_response(struct xenvif *vif, |
1511 | struct xen_netif_tx_request *txp, | |
1512 | s8 st) | |
1513 | { | |
1514 | RING_IDX i = vif->tx.rsp_prod_pvt; | |
1515 | struct xen_netif_tx_response *resp; | |
1516 | int notify; | |
1517 | ||
1518 | resp = RING_GET_RESPONSE(&vif->tx, i); | |
1519 | resp->id = txp->id; | |
1520 | resp->status = st; | |
1521 | ||
1522 | if (txp->flags & XEN_NETTXF_extra_info) | |
1523 | RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL; | |
1524 | ||
1525 | vif->tx.rsp_prod_pvt = ++i; | |
1526 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify); | |
1527 | if (notify) | |
e1f00a69 | 1528 | notify_remote_via_irq(vif->tx_irq); |
f942dc25 IC |
1529 | } |
1530 | ||
1531 | static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif, | |
1532 | u16 id, | |
1533 | s8 st, | |
1534 | u16 offset, | |
1535 | u16 size, | |
1536 | u16 flags) | |
1537 | { | |
1538 | RING_IDX i = vif->rx.rsp_prod_pvt; | |
1539 | struct xen_netif_rx_response *resp; | |
1540 | ||
1541 | resp = RING_GET_RESPONSE(&vif->rx, i); | |
1542 | resp->offset = offset; | |
1543 | resp->flags = flags; | |
1544 | resp->id = id; | |
1545 | resp->status = (s16)size; | |
1546 | if (st < 0) | |
1547 | resp->status = (s16)st; | |
1548 | ||
1549 | vif->rx.rsp_prod_pvt = ++i; | |
1550 | ||
1551 | return resp; | |
1552 | } | |
1553 | ||
b3f980bd | 1554 | static inline int rx_work_todo(struct xenvif *vif) |
f942dc25 | 1555 | { |
b3f980bd | 1556 | return !skb_queue_empty(&vif->rx_queue); |
f942dc25 IC |
1557 | } |
1558 | ||
b3f980bd | 1559 | static inline int tx_work_todo(struct xenvif *vif) |
f942dc25 IC |
1560 | { |
1561 | ||
b3f980bd WL |
1562 | if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) && |
1563 | (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX | |
1564 | < MAX_PENDING_REQS)) | |
f942dc25 IC |
1565 | return 1; |
1566 | ||
1567 | return 0; | |
1568 | } | |
1569 | ||
7376419a | 1570 | void xenvif_unmap_frontend_rings(struct xenvif *vif) |
f942dc25 | 1571 | { |
c9d63699 DV |
1572 | if (vif->tx.sring) |
1573 | xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif), | |
1574 | vif->tx.sring); | |
1575 | if (vif->rx.sring) | |
1576 | xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif), | |
1577 | vif->rx.sring); | |
f942dc25 IC |
1578 | } |
1579 | ||
7376419a WL |
1580 | int xenvif_map_frontend_rings(struct xenvif *vif, |
1581 | grant_ref_t tx_ring_ref, | |
1582 | grant_ref_t rx_ring_ref) | |
f942dc25 | 1583 | { |
c9d63699 | 1584 | void *addr; |
f942dc25 IC |
1585 | struct xen_netif_tx_sring *txs; |
1586 | struct xen_netif_rx_sring *rxs; | |
1587 | ||
1588 | int err = -ENOMEM; | |
1589 | ||
c9d63699 DV |
1590 | err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif), |
1591 | tx_ring_ref, &addr); | |
1592 | if (err) | |
f942dc25 IC |
1593 | goto err; |
1594 | ||
c9d63699 | 1595 | txs = (struct xen_netif_tx_sring *)addr; |
f942dc25 IC |
1596 | BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE); |
1597 | ||
c9d63699 DV |
1598 | err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif), |
1599 | rx_ring_ref, &addr); | |
1600 | if (err) | |
f942dc25 | 1601 | goto err; |
f942dc25 | 1602 | |
c9d63699 | 1603 | rxs = (struct xen_netif_rx_sring *)addr; |
f942dc25 IC |
1604 | BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE); |
1605 | ||
c9d63699 DV |
1606 | vif->rx_req_cons_peek = 0; |
1607 | ||
f942dc25 IC |
1608 | return 0; |
1609 | ||
1610 | err: | |
7376419a | 1611 | xenvif_unmap_frontend_rings(vif); |
f942dc25 IC |
1612 | return err; |
1613 | } | |
1614 | ||
7376419a | 1615 | int xenvif_kthread(void *data) |
b3f980bd WL |
1616 | { |
1617 | struct xenvif *vif = data; | |
1618 | ||
1619 | while (!kthread_should_stop()) { | |
1620 | wait_event_interruptible(vif->wq, | |
1621 | rx_work_todo(vif) || | |
1622 | kthread_should_stop()); | |
1623 | if (kthread_should_stop()) | |
1624 | break; | |
1625 | ||
1626 | if (rx_work_todo(vif)) | |
7376419a | 1627 | xenvif_rx_action(vif); |
b3f980bd WL |
1628 | |
1629 | cond_resched(); | |
1630 | } | |
1631 | ||
1632 | return 0; | |
1633 | } | |
1634 | ||
f942dc25 IC |
1635 | static int __init netback_init(void) |
1636 | { | |
f942dc25 | 1637 | int rc = 0; |
f942dc25 | 1638 | |
2a14b244 | 1639 | if (!xen_domain()) |
f942dc25 IC |
1640 | return -ENODEV; |
1641 | ||
37641494 | 1642 | if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) { |
383eda32 JP |
1643 | pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n", |
1644 | fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX); | |
37641494 | 1645 | fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX; |
2810e5b9 WL |
1646 | } |
1647 | ||
f942dc25 IC |
1648 | rc = xenvif_xenbus_init(); |
1649 | if (rc) | |
1650 | goto failed_init; | |
1651 | ||
1652 | return 0; | |
1653 | ||
1654 | failed_init: | |
f942dc25 | 1655 | return rc; |
f942dc25 IC |
1656 | } |
1657 | ||
1658 | module_init(netback_init); | |
1659 | ||
b103f358 WL |
1660 | static void __exit netback_fini(void) |
1661 | { | |
b103f358 | 1662 | xenvif_xenbus_fini(); |
b103f358 WL |
1663 | } |
1664 | module_exit(netback_fini); | |
1665 | ||
f942dc25 | 1666 | MODULE_LICENSE("Dual BSD/GPL"); |
f984cec6 | 1667 | MODULE_ALIAS("xen-backend:vif"); |