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