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