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