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
8 * Copyright (c) 2002-2005, K A Fraser
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:
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:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
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
37 #include <linux/kthread.h>
38 #include <linux/if_vlan.h>
39 #include <linux/udp.h>
44 #include <xen/events.h>
45 #include <xen/interface/memory.h>
47 #include <asm/xen/hypercall.h>
48 #include <asm/xen/page.h>
50 struct pending_tx_info {
51 struct xen_netif_tx_request req;
54 typedef unsigned int pending_ring_idx_t;
56 struct netbk_rx_meta {
62 #define MAX_PENDING_REQS 256
64 /* Discriminate from any valid pending_idx value. */
65 #define INVALID_PENDING_IDX 0xFFFF
67 #define MAX_BUFFER_OFFSET PAGE_SIZE
69 /* extra field used in struct page */
72 #if BITS_PER_LONG < 64
74 #define GROUP_WIDTH (BITS_PER_LONG - IDX_WIDTH)
75 unsigned int group:GROUP_WIDTH;
76 unsigned int idx:IDX_WIDTH;
78 unsigned int group, idx;
86 struct task_struct *task;
88 struct sk_buff_head rx_queue;
89 struct sk_buff_head tx_queue;
91 struct timer_list net_timer;
93 struct page *mmap_pages[MAX_PENDING_REQS];
95 pending_ring_idx_t pending_prod;
96 pending_ring_idx_t pending_cons;
97 struct list_head net_schedule_list;
99 /* Protect the net_schedule_list in netif. */
100 spinlock_t net_schedule_list_lock;
102 atomic_t netfront_count;
104 struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
105 struct gnttab_copy tx_copy_ops[MAX_PENDING_REQS];
107 u16 pending_ring[MAX_PENDING_REQS];
110 * Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
111 * head/fragment page uses 2 copy operations because it
112 * straddles two buffers in the frontend.
114 struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
115 struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
118 static struct xen_netbk *xen_netbk;
119 static int xen_netbk_group_nr;
121 void xen_netbk_add_xenvif(struct xenvif *vif)
124 int min_netfront_count;
126 struct xen_netbk *netbk;
128 min_netfront_count = atomic_read(&xen_netbk[0].netfront_count);
129 for (i = 0; i < xen_netbk_group_nr; i++) {
130 int netfront_count = atomic_read(&xen_netbk[i].netfront_count);
131 if (netfront_count < min_netfront_count) {
133 min_netfront_count = netfront_count;
137 netbk = &xen_netbk[min_group];
140 atomic_inc(&netbk->netfront_count);
143 void xen_netbk_remove_xenvif(struct xenvif *vif)
145 struct xen_netbk *netbk = vif->netbk;
147 atomic_dec(&netbk->netfront_count);
150 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
152 static void make_tx_response(struct xenvif *vif,
153 struct xen_netif_tx_request *txp,
155 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
162 static inline unsigned long idx_to_pfn(struct xen_netbk *netbk,
165 return page_to_pfn(netbk->mmap_pages[idx]);
168 static inline unsigned long idx_to_kaddr(struct xen_netbk *netbk,
171 return (unsigned long)pfn_to_kaddr(idx_to_pfn(netbk, idx));
174 /* extra field used in struct page */
175 static inline void set_page_ext(struct page *pg, struct xen_netbk *netbk,
178 unsigned int group = netbk - xen_netbk;
179 union page_ext ext = { .e = { .group = group + 1, .idx = idx } };
181 BUILD_BUG_ON(sizeof(ext) > sizeof(ext.mapping));
182 pg->mapping = ext.mapping;
185 static int get_page_ext(struct page *pg,
186 unsigned int *pgroup, unsigned int *pidx)
188 union page_ext ext = { .mapping = pg->mapping };
189 struct xen_netbk *netbk;
190 unsigned int group, idx;
192 group = ext.e.group - 1;
194 if (group < 0 || group >= xen_netbk_group_nr)
197 netbk = &xen_netbk[group];
201 if ((idx < 0) || (idx >= MAX_PENDING_REQS))
204 if (netbk->mmap_pages[idx] != pg)
214 * This is the amount of packet we copy rather than map, so that the
215 * guest can't fiddle with the contents of the headers while we do
216 * packet processing on them (netfilter, routing, etc).
218 #define PKT_PROT_LEN (ETH_HLEN + \
220 sizeof(struct iphdr) + MAX_IPOPTLEN + \
221 sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE)
223 static u16 frag_get_pending_idx(skb_frag_t *frag)
225 return (u16)frag->page_offset;
228 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
230 frag->page_offset = pending_idx;
233 static inline pending_ring_idx_t pending_index(unsigned i)
235 return i & (MAX_PENDING_REQS-1);
238 static inline pending_ring_idx_t nr_pending_reqs(struct xen_netbk *netbk)
240 return MAX_PENDING_REQS -
241 netbk->pending_prod + netbk->pending_cons;
244 static void xen_netbk_kick_thread(struct xen_netbk *netbk)
249 static int max_required_rx_slots(struct xenvif *vif)
251 int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
253 if (vif->can_sg || vif->gso || vif->gso_prefix)
254 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
259 int xen_netbk_rx_ring_full(struct xenvif *vif)
261 RING_IDX peek = vif->rx_req_cons_peek;
262 RING_IDX needed = max_required_rx_slots(vif);
264 return ((vif->rx.sring->req_prod - peek) < needed) ||
265 ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
268 int xen_netbk_must_stop_queue(struct xenvif *vif)
270 if (!xen_netbk_rx_ring_full(vif))
273 vif->rx.sring->req_event = vif->rx_req_cons_peek +
274 max_required_rx_slots(vif);
275 mb(); /* request notification /then/ check the queue */
277 return xen_netbk_rx_ring_full(vif);
281 * Returns true if we should start a new receive buffer instead of
282 * adding 'size' bytes to a buffer which currently contains 'offset'
285 static bool start_new_rx_buffer(int offset, unsigned long size, int head)
287 /* simple case: we have completely filled the current buffer. */
288 if (offset == MAX_BUFFER_OFFSET)
292 * complex case: start a fresh buffer if the current frag
293 * would overflow the current buffer but only if:
294 * (i) this frag would fit completely in the next buffer
295 * and (ii) there is already some data in the current buffer
296 * and (iii) this is not the head buffer.
299 * - (i) stops us splitting a frag into two copies
300 * unless the frag is too large for a single buffer.
301 * - (ii) stops us from leaving a buffer pointlessly empty.
302 * - (iii) stops us leaving the first buffer
303 * empty. Strictly speaking this is already covered
304 * by (ii) but is explicitly checked because
305 * netfront relies on the first buffer being
306 * non-empty and can crash otherwise.
308 * This means we will effectively linearise small
309 * frags but do not needlessly split large buffers
310 * into multiple copies tend to give large frags their
311 * own buffers as before.
313 if ((offset + size > MAX_BUFFER_OFFSET) &&
314 (size <= MAX_BUFFER_OFFSET) && offset && !head)
321 * Figure out how many ring slots we're going to need to send @skb to
322 * the guest. This function is essentially a dry run of
323 * netbk_gop_frag_copy.
325 unsigned int xen_netbk_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
330 count = DIV_ROUND_UP(skb_headlen(skb), PAGE_SIZE);
332 copy_off = skb_headlen(skb) % PAGE_SIZE;
334 if (skb_shinfo(skb)->gso_size)
337 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
338 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
339 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
342 offset &= ~PAGE_MASK;
345 BUG_ON(offset >= PAGE_SIZE);
346 BUG_ON(copy_off > MAX_BUFFER_OFFSET);
348 bytes = PAGE_SIZE - offset;
353 if (start_new_rx_buffer(copy_off, bytes, 0)) {
358 if (copy_off + bytes > MAX_BUFFER_OFFSET)
359 bytes = MAX_BUFFER_OFFSET - copy_off;
366 if (offset == PAGE_SIZE)
373 struct netrx_pending_operations {
374 unsigned copy_prod, copy_cons;
375 unsigned meta_prod, meta_cons;
376 struct gnttab_copy *copy;
377 struct netbk_rx_meta *meta;
379 grant_ref_t copy_gref;
382 static struct netbk_rx_meta *get_next_rx_buffer(struct xenvif *vif,
383 struct netrx_pending_operations *npo)
385 struct netbk_rx_meta *meta;
386 struct xen_netif_rx_request *req;
388 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
390 meta = npo->meta + npo->meta_prod++;
396 npo->copy_gref = req->gref;
402 * Set up the grant operations for this fragment. If it's a flipping
403 * interface, we also set up the unmap request from here.
405 static void netbk_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
406 struct netrx_pending_operations *npo,
407 struct page *page, unsigned long size,
408 unsigned long offset, int *head)
410 struct gnttab_copy *copy_gop;
411 struct netbk_rx_meta *meta;
413 * These variables are used iff get_page_ext returns true,
414 * in which case they are guaranteed to be initialized.
416 unsigned int uninitialized_var(group), uninitialized_var(idx);
417 int foreign = get_page_ext(page, &group, &idx);
420 /* Data must not cross a page boundary. */
421 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
423 meta = npo->meta + npo->meta_prod - 1;
425 /* Skip unused frames from start of page */
426 page += offset >> PAGE_SHIFT;
427 offset &= ~PAGE_MASK;
430 BUG_ON(offset >= PAGE_SIZE);
431 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
433 bytes = PAGE_SIZE - offset;
438 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
440 * Netfront requires there to be some data in the head
445 meta = get_next_rx_buffer(vif, npo);
448 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
449 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
451 copy_gop = npo->copy + npo->copy_prod++;
452 copy_gop->flags = GNTCOPY_dest_gref;
454 struct xen_netbk *netbk = &xen_netbk[group];
455 struct pending_tx_info *src_pend;
457 src_pend = &netbk->pending_tx_info[idx];
459 copy_gop->source.domid = src_pend->vif->domid;
460 copy_gop->source.u.ref = src_pend->req.gref;
461 copy_gop->flags |= GNTCOPY_source_gref;
463 void *vaddr = page_address(page);
464 copy_gop->source.domid = DOMID_SELF;
465 copy_gop->source.u.gmfn = virt_to_mfn(vaddr);
467 copy_gop->source.offset = offset;
468 copy_gop->dest.domid = vif->domid;
470 copy_gop->dest.offset = npo->copy_off;
471 copy_gop->dest.u.ref = npo->copy_gref;
472 copy_gop->len = bytes;
474 npo->copy_off += bytes;
481 if (offset == PAGE_SIZE && size) {
482 BUG_ON(!PageCompound(page));
487 /* Leave a gap for the GSO descriptor. */
488 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
491 *head = 0; /* There must be something in this buffer now. */
497 * Prepare an SKB to be transmitted to the frontend.
499 * This function is responsible for allocating grant operations, meta
502 * It returns the number of meta structures consumed. The number of
503 * ring slots used is always equal to the number of meta slots used
504 * plus the number of GSO descriptors used. Currently, we use either
505 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
506 * frontend-side LRO).
508 static int netbk_gop_skb(struct sk_buff *skb,
509 struct netrx_pending_operations *npo)
511 struct xenvif *vif = netdev_priv(skb->dev);
512 int nr_frags = skb_shinfo(skb)->nr_frags;
514 struct xen_netif_rx_request *req;
515 struct netbk_rx_meta *meta;
520 old_meta_prod = npo->meta_prod;
522 /* Set up a GSO prefix descriptor, if necessary */
523 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
524 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
525 meta = npo->meta + npo->meta_prod++;
526 meta->gso_size = skb_shinfo(skb)->gso_size;
531 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
532 meta = npo->meta + npo->meta_prod++;
534 if (!vif->gso_prefix)
535 meta->gso_size = skb_shinfo(skb)->gso_size;
542 npo->copy_gref = req->gref;
545 while (data < skb_tail_pointer(skb)) {
546 unsigned int offset = offset_in_page(data);
547 unsigned int len = PAGE_SIZE - offset;
549 if (data + len > skb_tail_pointer(skb))
550 len = skb_tail_pointer(skb) - data;
552 netbk_gop_frag_copy(vif, skb, npo,
553 virt_to_page(data), len, offset, &head);
557 for (i = 0; i < nr_frags; i++) {
558 netbk_gop_frag_copy(vif, skb, npo,
559 skb_frag_page(&skb_shinfo(skb)->frags[i]),
560 skb_frag_size(&skb_shinfo(skb)->frags[i]),
561 skb_shinfo(skb)->frags[i].page_offset,
565 return npo->meta_prod - old_meta_prod;
569 * This is a twin to netbk_gop_skb. Assume that netbk_gop_skb was
570 * used to set up the operations on the top of
571 * netrx_pending_operations, which have since been done. Check that
572 * they didn't give any errors and advance over them.
574 static int netbk_check_gop(struct xenvif *vif, int nr_meta_slots,
575 struct netrx_pending_operations *npo)
577 struct gnttab_copy *copy_op;
578 int status = XEN_NETIF_RSP_OKAY;
581 for (i = 0; i < nr_meta_slots; i++) {
582 copy_op = npo->copy + npo->copy_cons++;
583 if (copy_op->status != GNTST_okay) {
585 "Bad status %d from copy to DOM%d.\n",
586 copy_op->status, vif->domid);
587 status = XEN_NETIF_RSP_ERROR;
594 static void netbk_add_frag_responses(struct xenvif *vif, int status,
595 struct netbk_rx_meta *meta,
599 unsigned long offset;
601 /* No fragments used */
602 if (nr_meta_slots <= 1)
607 for (i = 0; i < nr_meta_slots; i++) {
609 if (i == nr_meta_slots - 1)
612 flags = XEN_NETRXF_more_data;
615 make_rx_response(vif, meta[i].id, status, offset,
616 meta[i].size, flags);
620 struct skb_cb_overlay {
624 static void xen_netbk_rx_action(struct xen_netbk *netbk)
626 struct xenvif *vif = NULL, *tmp;
629 struct xen_netif_rx_response *resp;
630 struct sk_buff_head rxq;
636 unsigned long offset;
637 struct skb_cb_overlay *sco;
639 struct netrx_pending_operations npo = {
640 .copy = netbk->grant_copy_op,
644 skb_queue_head_init(&rxq);
648 while ((skb = skb_dequeue(&netbk->rx_queue)) != NULL) {
649 vif = netdev_priv(skb->dev);
650 nr_frags = skb_shinfo(skb)->nr_frags;
652 sco = (struct skb_cb_overlay *)skb->cb;
653 sco->meta_slots_used = netbk_gop_skb(skb, &npo);
655 count += nr_frags + 1;
657 __skb_queue_tail(&rxq, skb);
659 /* Filled the batch queue? */
660 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
664 BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
669 BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
670 gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
672 while ((skb = __skb_dequeue(&rxq)) != NULL) {
673 sco = (struct skb_cb_overlay *)skb->cb;
675 vif = netdev_priv(skb->dev);
677 if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
678 resp = RING_GET_RESPONSE(&vif->rx,
679 vif->rx.rsp_prod_pvt++);
681 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
683 resp->offset = netbk->meta[npo.meta_cons].gso_size;
684 resp->id = netbk->meta[npo.meta_cons].id;
685 resp->status = sco->meta_slots_used;
688 sco->meta_slots_used--;
692 vif->dev->stats.tx_bytes += skb->len;
693 vif->dev->stats.tx_packets++;
695 status = netbk_check_gop(vif, sco->meta_slots_used, &npo);
697 if (sco->meta_slots_used == 1)
700 flags = XEN_NETRXF_more_data;
702 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
703 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
704 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
705 /* remote but checksummed. */
706 flags |= XEN_NETRXF_data_validated;
709 resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
711 netbk->meta[npo.meta_cons].size,
714 if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
715 struct xen_netif_extra_info *gso =
716 (struct xen_netif_extra_info *)
717 RING_GET_RESPONSE(&vif->rx,
718 vif->rx.rsp_prod_pvt++);
720 resp->flags |= XEN_NETRXF_extra_info;
722 gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
723 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
725 gso->u.gso.features = 0;
727 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
731 netbk_add_frag_responses(vif, status,
732 netbk->meta + npo.meta_cons + 1,
733 sco->meta_slots_used);
735 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
737 if (ret && list_empty(&vif->notify_list))
738 list_add_tail(&vif->notify_list, ¬ify);
740 xenvif_notify_tx_completion(vif);
743 npo.meta_cons += sco->meta_slots_used;
747 list_for_each_entry_safe(vif, tmp, ¬ify, notify_list) {
748 notify_remote_via_irq(vif->irq);
749 list_del_init(&vif->notify_list);
752 /* More work to do? */
753 if (!skb_queue_empty(&netbk->rx_queue) &&
754 !timer_pending(&netbk->net_timer))
755 xen_netbk_kick_thread(netbk);
758 void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
760 struct xen_netbk *netbk = vif->netbk;
762 skb_queue_tail(&netbk->rx_queue, skb);
764 xen_netbk_kick_thread(netbk);
767 static void xen_netbk_alarm(unsigned long data)
769 struct xen_netbk *netbk = (struct xen_netbk *)data;
770 xen_netbk_kick_thread(netbk);
773 static int __on_net_schedule_list(struct xenvif *vif)
775 return !list_empty(&vif->schedule_list);
778 /* Must be called with net_schedule_list_lock held */
779 static void remove_from_net_schedule_list(struct xenvif *vif)
781 if (likely(__on_net_schedule_list(vif))) {
782 list_del_init(&vif->schedule_list);
787 static struct xenvif *poll_net_schedule_list(struct xen_netbk *netbk)
789 struct xenvif *vif = NULL;
791 spin_lock_irq(&netbk->net_schedule_list_lock);
792 if (list_empty(&netbk->net_schedule_list))
795 vif = list_first_entry(&netbk->net_schedule_list,
796 struct xenvif, schedule_list);
802 remove_from_net_schedule_list(vif);
804 spin_unlock_irq(&netbk->net_schedule_list_lock);
808 void xen_netbk_schedule_xenvif(struct xenvif *vif)
811 struct xen_netbk *netbk = vif->netbk;
813 if (__on_net_schedule_list(vif))
816 spin_lock_irqsave(&netbk->net_schedule_list_lock, flags);
817 if (!__on_net_schedule_list(vif) &&
818 likely(xenvif_schedulable(vif))) {
819 list_add_tail(&vif->schedule_list, &netbk->net_schedule_list);
822 spin_unlock_irqrestore(&netbk->net_schedule_list_lock, flags);
826 if ((nr_pending_reqs(netbk) < (MAX_PENDING_REQS/2)) &&
827 !list_empty(&netbk->net_schedule_list))
828 xen_netbk_kick_thread(netbk);
831 void xen_netbk_deschedule_xenvif(struct xenvif *vif)
833 struct xen_netbk *netbk = vif->netbk;
834 spin_lock_irq(&netbk->net_schedule_list_lock);
835 remove_from_net_schedule_list(vif);
836 spin_unlock_irq(&netbk->net_schedule_list_lock);
839 void xen_netbk_check_rx_xenvif(struct xenvif *vif)
843 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
846 xen_netbk_schedule_xenvif(vif);
849 static void tx_add_credit(struct xenvif *vif)
851 unsigned long max_burst, max_credit;
854 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
855 * Otherwise the interface can seize up due to insufficient credit.
857 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
858 max_burst = min(max_burst, 131072UL);
859 max_burst = max(max_burst, vif->credit_bytes);
861 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
862 max_credit = vif->remaining_credit + vif->credit_bytes;
863 if (max_credit < vif->remaining_credit)
864 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
866 vif->remaining_credit = min(max_credit, max_burst);
869 static void tx_credit_callback(unsigned long data)
871 struct xenvif *vif = (struct xenvif *)data;
873 xen_netbk_check_rx_xenvif(vif);
876 static void netbk_tx_err(struct xenvif *vif,
877 struct xen_netif_tx_request *txp, RING_IDX end)
879 RING_IDX cons = vif->tx.req_cons;
882 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
885 txp = RING_GET_REQUEST(&vif->tx, cons++);
887 vif->tx.req_cons = cons;
888 xen_netbk_check_rx_xenvif(vif);
892 static void netbk_fatal_tx_err(struct xenvif *vif)
894 netdev_err(vif->dev, "fatal error; disabling device\n");
895 xenvif_carrier_off(vif);
898 static int netbk_count_requests(struct xenvif *vif,
899 struct xen_netif_tx_request *first,
900 struct xen_netif_tx_request *txp,
903 RING_IDX cons = vif->tx.req_cons;
906 if (!(first->flags & XEN_NETTXF_more_data))
910 if (frags >= work_to_do) {
911 netdev_err(vif->dev, "Need more frags\n");
912 netbk_fatal_tx_err(vif);
916 if (unlikely(frags >= MAX_SKB_FRAGS)) {
917 netdev_err(vif->dev, "Too many frags\n");
918 netbk_fatal_tx_err(vif);
922 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + frags),
924 if (txp->size > first->size) {
925 netdev_err(vif->dev, "Frag is bigger than frame.\n");
926 netbk_fatal_tx_err(vif);
930 first->size -= txp->size;
933 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
934 netdev_err(vif->dev, "txp->offset: %x, size: %u\n",
935 txp->offset, txp->size);
936 netbk_fatal_tx_err(vif);
939 } while ((txp++)->flags & XEN_NETTXF_more_data);
943 static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
948 page = alloc_page(GFP_KERNEL|__GFP_COLD);
951 set_page_ext(page, netbk, pending_idx);
952 netbk->mmap_pages[pending_idx] = page;
956 static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
959 struct xen_netif_tx_request *txp,
960 struct gnttab_copy *gop)
962 struct skb_shared_info *shinfo = skb_shinfo(skb);
963 skb_frag_t *frags = shinfo->frags;
964 u16 pending_idx = *((u16 *)skb->data);
967 /* Skip first skb fragment if it is on same page as header fragment. */
968 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
970 for (i = start; i < shinfo->nr_frags; i++, txp++) {
972 pending_ring_idx_t index;
973 struct pending_tx_info *pending_tx_info =
974 netbk->pending_tx_info;
976 index = pending_index(netbk->pending_cons++);
977 pending_idx = netbk->pending_ring[index];
978 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
982 gop->source.u.ref = txp->gref;
983 gop->source.domid = vif->domid;
984 gop->source.offset = txp->offset;
986 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
987 gop->dest.domid = DOMID_SELF;
988 gop->dest.offset = txp->offset;
990 gop->len = txp->size;
991 gop->flags = GNTCOPY_source_gref;
995 memcpy(&pending_tx_info[pending_idx].req, txp, sizeof(*txp));
997 pending_tx_info[pending_idx].vif = vif;
998 frag_set_pending_idx(&frags[i], pending_idx);
1003 /* Unwind, freeing all pages and sending error responses. */
1004 while (i-- > start) {
1005 xen_netbk_idx_release(netbk, frag_get_pending_idx(&frags[i]),
1006 XEN_NETIF_RSP_ERROR);
1008 /* The head too, if necessary. */
1010 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1015 static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
1016 struct sk_buff *skb,
1017 struct gnttab_copy **gopp)
1019 struct gnttab_copy *gop = *gopp;
1020 u16 pending_idx = *((u16 *)skb->data);
1021 struct skb_shared_info *shinfo = skb_shinfo(skb);
1022 int nr_frags = shinfo->nr_frags;
1025 /* Check status of header. */
1028 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1030 /* Skip first skb fragment if it is on same page as header fragment. */
1031 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
1033 for (i = start; i < nr_frags; i++) {
1036 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
1038 /* Check error status: if okay then remember grant handle. */
1039 newerr = (++gop)->status;
1040 if (likely(!newerr)) {
1041 /* Had a previous error? Invalidate this fragment. */
1043 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1047 /* Error on this fragment: respond to client with an error. */
1048 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1050 /* Not the first error? Preceding frags already invalidated. */
1054 /* First error: invalidate header and preceding fragments. */
1055 pending_idx = *((u16 *)skb->data);
1056 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1057 for (j = start; j < i; j++) {
1058 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1059 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1062 /* Remember the error: invalidate all subsequent fragments. */
1070 static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
1072 struct skb_shared_info *shinfo = skb_shinfo(skb);
1073 int nr_frags = shinfo->nr_frags;
1076 for (i = 0; i < nr_frags; i++) {
1077 skb_frag_t *frag = shinfo->frags + i;
1078 struct xen_netif_tx_request *txp;
1082 pending_idx = frag_get_pending_idx(frag);
1084 txp = &netbk->pending_tx_info[pending_idx].req;
1085 page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
1086 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1087 skb->len += txp->size;
1088 skb->data_len += txp->size;
1089 skb->truesize += txp->size;
1091 /* Take an extra reference to offset xen_netbk_idx_release */
1092 get_page(netbk->mmap_pages[pending_idx]);
1093 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1097 static int xen_netbk_get_extras(struct xenvif *vif,
1098 struct xen_netif_extra_info *extras,
1101 struct xen_netif_extra_info extra;
1102 RING_IDX cons = vif->tx.req_cons;
1105 if (unlikely(work_to_do-- <= 0)) {
1106 netdev_err(vif->dev, "Missing extra info\n");
1107 netbk_fatal_tx_err(vif);
1111 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1113 if (unlikely(!extra.type ||
1114 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1115 vif->tx.req_cons = ++cons;
1116 netdev_err(vif->dev,
1117 "Invalid extra type: %d\n", extra.type);
1118 netbk_fatal_tx_err(vif);
1122 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1123 vif->tx.req_cons = ++cons;
1124 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1129 static int netbk_set_skb_gso(struct xenvif *vif,
1130 struct sk_buff *skb,
1131 struct xen_netif_extra_info *gso)
1133 if (!gso->u.gso.size) {
1134 netdev_err(vif->dev, "GSO size must not be zero.\n");
1135 netbk_fatal_tx_err(vif);
1139 /* Currently only TCPv4 S.O. is supported. */
1140 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1141 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1142 netbk_fatal_tx_err(vif);
1146 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1147 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1149 /* Header must be checked, and gso_segs computed. */
1150 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1151 skb_shinfo(skb)->gso_segs = 0;
1156 static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1161 int recalculate_partial_csum = 0;
1164 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1165 * peers can fail to set NETRXF_csum_blank when sending a GSO
1166 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1167 * recalculate the partial checksum.
1169 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1170 vif->rx_gso_checksum_fixup++;
1171 skb->ip_summed = CHECKSUM_PARTIAL;
1172 recalculate_partial_csum = 1;
1175 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1176 if (skb->ip_summed != CHECKSUM_PARTIAL)
1179 if (skb->protocol != htons(ETH_P_IP))
1182 iph = (void *)skb->data;
1183 th = skb->data + 4 * iph->ihl;
1184 if (th >= skb_tail_pointer(skb))
1187 skb->csum_start = th - skb->head;
1188 switch (iph->protocol) {
1190 skb->csum_offset = offsetof(struct tcphdr, check);
1192 if (recalculate_partial_csum) {
1193 struct tcphdr *tcph = (struct tcphdr *)th;
1194 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1195 skb->len - iph->ihl*4,
1200 skb->csum_offset = offsetof(struct udphdr, check);
1202 if (recalculate_partial_csum) {
1203 struct udphdr *udph = (struct udphdr *)th;
1204 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1205 skb->len - iph->ihl*4,
1210 if (net_ratelimit())
1211 netdev_err(vif->dev,
1212 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1217 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
1226 static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1228 unsigned long now = jiffies;
1229 unsigned long next_credit =
1230 vif->credit_timeout.expires +
1231 msecs_to_jiffies(vif->credit_usec / 1000);
1233 /* Timer could already be pending in rare cases. */
1234 if (timer_pending(&vif->credit_timeout))
1237 /* Passed the point where we can replenish credit? */
1238 if (time_after_eq(now, next_credit)) {
1239 vif->credit_timeout.expires = now;
1243 /* Still too big to send right now? Set a callback. */
1244 if (size > vif->remaining_credit) {
1245 vif->credit_timeout.data =
1247 vif->credit_timeout.function =
1249 mod_timer(&vif->credit_timeout,
1258 static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
1260 struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
1261 struct sk_buff *skb;
1264 while (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1265 !list_empty(&netbk->net_schedule_list)) {
1267 struct xen_netif_tx_request txreq;
1268 struct xen_netif_tx_request txfrags[MAX_SKB_FRAGS];
1270 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1274 unsigned int data_len;
1275 pending_ring_idx_t index;
1277 /* Get a netif from the list with work to do. */
1278 vif = poll_net_schedule_list(netbk);
1279 /* This can sometimes happen because the test of
1280 * list_empty(net_schedule_list) at the top of the
1281 * loop is unlocked. Just go back and have another
1287 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1288 XEN_NETIF_TX_RING_SIZE) {
1289 netdev_err(vif->dev,
1290 "Impossible number of requests. "
1291 "req_prod %d, req_cons %d, size %ld\n",
1292 vif->tx.sring->req_prod, vif->tx.req_cons,
1293 XEN_NETIF_TX_RING_SIZE);
1294 netbk_fatal_tx_err(vif);
1298 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1304 idx = vif->tx.req_cons;
1305 rmb(); /* Ensure that we see the request before we copy it. */
1306 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1308 /* Credit-based scheduling. */
1309 if (txreq.size > vif->remaining_credit &&
1310 tx_credit_exceeded(vif, txreq.size)) {
1315 vif->remaining_credit -= txreq.size;
1318 vif->tx.req_cons = ++idx;
1320 memset(extras, 0, sizeof(extras));
1321 if (txreq.flags & XEN_NETTXF_extra_info) {
1322 work_to_do = xen_netbk_get_extras(vif, extras,
1324 idx = vif->tx.req_cons;
1325 if (unlikely(work_to_do < 0))
1329 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
1330 if (unlikely(ret < 0))
1335 if (unlikely(txreq.size < ETH_HLEN)) {
1336 netdev_dbg(vif->dev,
1337 "Bad packet size: %d\n", txreq.size);
1338 netbk_tx_err(vif, &txreq, idx);
1342 /* No crossing a page as the payload mustn't fragment. */
1343 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1344 netdev_err(vif->dev,
1345 "txreq.offset: %x, size: %u, end: %lu\n",
1346 txreq.offset, txreq.size,
1347 (txreq.offset&~PAGE_MASK) + txreq.size);
1348 netbk_fatal_tx_err(vif);
1352 index = pending_index(netbk->pending_cons);
1353 pending_idx = netbk->pending_ring[index];
1355 data_len = (txreq.size > PKT_PROT_LEN &&
1356 ret < MAX_SKB_FRAGS) ?
1357 PKT_PROT_LEN : txreq.size;
1359 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1360 GFP_ATOMIC | __GFP_NOWARN);
1361 if (unlikely(skb == NULL)) {
1362 netdev_dbg(vif->dev,
1363 "Can't allocate a skb in start_xmit.\n");
1364 netbk_tx_err(vif, &txreq, idx);
1368 /* Packets passed to netif_rx() must have some headroom. */
1369 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1371 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1372 struct xen_netif_extra_info *gso;
1373 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1375 if (netbk_set_skb_gso(vif, skb, gso)) {
1376 /* Failure in netbk_set_skb_gso is fatal. */
1382 /* XXX could copy straight to head */
1383 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
1386 netbk_tx_err(vif, &txreq, idx);
1390 gop->source.u.ref = txreq.gref;
1391 gop->source.domid = vif->domid;
1392 gop->source.offset = txreq.offset;
1394 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1395 gop->dest.domid = DOMID_SELF;
1396 gop->dest.offset = txreq.offset;
1398 gop->len = txreq.size;
1399 gop->flags = GNTCOPY_source_gref;
1403 memcpy(&netbk->pending_tx_info[pending_idx].req,
1404 &txreq, sizeof(txreq));
1405 netbk->pending_tx_info[pending_idx].vif = vif;
1406 *((u16 *)skb->data) = pending_idx;
1408 __skb_put(skb, data_len);
1410 skb_shinfo(skb)->nr_frags = ret;
1411 if (data_len < txreq.size) {
1412 skb_shinfo(skb)->nr_frags++;
1413 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1416 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1417 INVALID_PENDING_IDX);
1420 netbk->pending_cons++;
1422 request_gop = xen_netbk_get_requests(netbk, vif,
1424 if (request_gop == NULL) {
1426 netbk_tx_err(vif, &txreq, idx);
1431 __skb_queue_tail(&netbk->tx_queue, skb);
1433 vif->tx.req_cons = idx;
1434 xen_netbk_check_rx_xenvif(vif);
1436 if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
1440 return gop - netbk->tx_copy_ops;
1443 static void xen_netbk_tx_submit(struct xen_netbk *netbk)
1445 struct gnttab_copy *gop = netbk->tx_copy_ops;
1446 struct sk_buff *skb;
1448 while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
1449 struct xen_netif_tx_request *txp;
1454 pending_idx = *((u16 *)skb->data);
1455 vif = netbk->pending_tx_info[pending_idx].vif;
1456 txp = &netbk->pending_tx_info[pending_idx].req;
1458 /* Check the remap error code. */
1459 if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1460 netdev_dbg(vif->dev, "netback grant failed.\n");
1461 skb_shinfo(skb)->nr_frags = 0;
1466 data_len = skb->len;
1468 (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
1470 if (data_len < txp->size) {
1471 /* Append the packet payload as a fragment. */
1472 txp->offset += data_len;
1473 txp->size -= data_len;
1475 /* Schedule a response immediately. */
1476 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1479 if (txp->flags & XEN_NETTXF_csum_blank)
1480 skb->ip_summed = CHECKSUM_PARTIAL;
1481 else if (txp->flags & XEN_NETTXF_data_validated)
1482 skb->ip_summed = CHECKSUM_UNNECESSARY;
1484 xen_netbk_fill_frags(netbk, skb);
1487 * If the initial fragment was < PKT_PROT_LEN then
1488 * pull through some bytes from the other fragments to
1489 * increase the linear region to PKT_PROT_LEN bytes.
1491 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1492 int target = min_t(int, skb->len, PKT_PROT_LEN);
1493 __pskb_pull_tail(skb, target - skb_headlen(skb));
1496 skb->dev = vif->dev;
1497 skb->protocol = eth_type_trans(skb, skb->dev);
1499 if (checksum_setup(vif, skb)) {
1500 netdev_dbg(vif->dev,
1501 "Can't setup checksum in net_tx_action\n");
1506 vif->dev->stats.rx_bytes += skb->len;
1507 vif->dev->stats.rx_packets++;
1509 xenvif_receive_skb(vif, skb);
1513 /* Called after netfront has transmitted */
1514 static void xen_netbk_tx_action(struct xen_netbk *netbk)
1518 nr_gops = xen_netbk_tx_build_gops(netbk);
1523 gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
1525 xen_netbk_tx_submit(netbk);
1528 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
1532 struct pending_tx_info *pending_tx_info;
1533 pending_ring_idx_t index;
1535 /* Already complete? */
1536 if (netbk->mmap_pages[pending_idx] == NULL)
1539 pending_tx_info = &netbk->pending_tx_info[pending_idx];
1541 vif = pending_tx_info->vif;
1543 make_tx_response(vif, &pending_tx_info->req, status);
1545 index = pending_index(netbk->pending_prod++);
1546 netbk->pending_ring[index] = pending_idx;
1550 netbk->mmap_pages[pending_idx]->mapping = 0;
1551 put_page(netbk->mmap_pages[pending_idx]);
1552 netbk->mmap_pages[pending_idx] = NULL;
1555 static void make_tx_response(struct xenvif *vif,
1556 struct xen_netif_tx_request *txp,
1559 RING_IDX i = vif->tx.rsp_prod_pvt;
1560 struct xen_netif_tx_response *resp;
1563 resp = RING_GET_RESPONSE(&vif->tx, i);
1567 if (txp->flags & XEN_NETTXF_extra_info)
1568 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1570 vif->tx.rsp_prod_pvt = ++i;
1571 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1573 notify_remote_via_irq(vif->irq);
1576 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1583 RING_IDX i = vif->rx.rsp_prod_pvt;
1584 struct xen_netif_rx_response *resp;
1586 resp = RING_GET_RESPONSE(&vif->rx, i);
1587 resp->offset = offset;
1588 resp->flags = flags;
1590 resp->status = (s16)size;
1592 resp->status = (s16)st;
1594 vif->rx.rsp_prod_pvt = ++i;
1599 static inline int rx_work_todo(struct xen_netbk *netbk)
1601 return !skb_queue_empty(&netbk->rx_queue);
1604 static inline int tx_work_todo(struct xen_netbk *netbk)
1607 if (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1608 !list_empty(&netbk->net_schedule_list))
1614 static int xen_netbk_kthread(void *data)
1616 struct xen_netbk *netbk = data;
1617 while (!kthread_should_stop()) {
1618 wait_event_interruptible(netbk->wq,
1619 rx_work_todo(netbk) ||
1620 tx_work_todo(netbk) ||
1621 kthread_should_stop());
1624 if (kthread_should_stop())
1627 if (rx_work_todo(netbk))
1628 xen_netbk_rx_action(netbk);
1630 if (tx_work_todo(netbk))
1631 xen_netbk_tx_action(netbk);
1637 void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
1640 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1643 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1647 int xen_netbk_map_frontend_rings(struct xenvif *vif,
1648 grant_ref_t tx_ring_ref,
1649 grant_ref_t rx_ring_ref)
1652 struct xen_netif_tx_sring *txs;
1653 struct xen_netif_rx_sring *rxs;
1657 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1658 tx_ring_ref, &addr);
1662 txs = (struct xen_netif_tx_sring *)addr;
1663 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1665 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1666 rx_ring_ref, &addr);
1670 rxs = (struct xen_netif_rx_sring *)addr;
1671 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1673 vif->rx_req_cons_peek = 0;
1678 xen_netbk_unmap_frontend_rings(vif);
1682 static int __init netback_init(void)
1691 xen_netbk_group_nr = num_online_cpus();
1692 xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
1696 for (group = 0; group < xen_netbk_group_nr; group++) {
1697 struct xen_netbk *netbk = &xen_netbk[group];
1698 skb_queue_head_init(&netbk->rx_queue);
1699 skb_queue_head_init(&netbk->tx_queue);
1701 init_timer(&netbk->net_timer);
1702 netbk->net_timer.data = (unsigned long)netbk;
1703 netbk->net_timer.function = xen_netbk_alarm;
1705 netbk->pending_cons = 0;
1706 netbk->pending_prod = MAX_PENDING_REQS;
1707 for (i = 0; i < MAX_PENDING_REQS; i++)
1708 netbk->pending_ring[i] = i;
1710 init_waitqueue_head(&netbk->wq);
1711 netbk->task = kthread_create(xen_netbk_kthread,
1713 "netback/%u", group);
1715 if (IS_ERR(netbk->task)) {
1716 printk(KERN_ALERT "kthread_create() fails at netback\n");
1717 del_timer(&netbk->net_timer);
1718 rc = PTR_ERR(netbk->task);
1722 kthread_bind(netbk->task, group);
1724 INIT_LIST_HEAD(&netbk->net_schedule_list);
1726 spin_lock_init(&netbk->net_schedule_list_lock);
1728 atomic_set(&netbk->netfront_count, 0);
1730 wake_up_process(netbk->task);
1733 rc = xenvif_xenbus_init();
1740 while (--group >= 0) {
1741 struct xen_netbk *netbk = &xen_netbk[group];
1742 for (i = 0; i < MAX_PENDING_REQS; i++) {
1743 if (netbk->mmap_pages[i])
1744 __free_page(netbk->mmap_pages[i]);
1746 del_timer(&netbk->net_timer);
1747 kthread_stop(netbk->task);
1754 module_init(netback_init);
1756 MODULE_LICENSE("Dual BSD/GPL");
1757 MODULE_ALIAS("xen-backend:vif");