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