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>
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.
54 #define FATAL_SKB_SLOTS_DEFAULT 20
55 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
56 module_param(fatal_skb_slots, uint, 0444);
59 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
60 * the maximum slots a valid packet can use. Now this value is defined
61 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
64 #define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
66 typedef unsigned int pending_ring_idx_t;
67 #define INVALID_PENDING_RING_IDX (~0U)
69 struct pending_tx_info {
70 struct xen_netif_tx_request req; /* coalesced tx request */
72 pending_ring_idx_t head; /* head != INVALID_PENDING_RING_IDX
73 * if it is head of one or more tx
78 struct netbk_rx_meta {
84 #define MAX_PENDING_REQS 256
86 /* Discriminate from any valid pending_idx value. */
87 #define INVALID_PENDING_IDX 0xFFFF
89 #define MAX_BUFFER_OFFSET PAGE_SIZE
91 /* extra field used in struct page */
94 #if BITS_PER_LONG < 64
96 #define GROUP_WIDTH (BITS_PER_LONG - IDX_WIDTH)
97 unsigned int group:GROUP_WIDTH;
98 unsigned int idx:IDX_WIDTH;
100 unsigned int group, idx;
107 wait_queue_head_t wq;
108 struct task_struct *task;
110 struct sk_buff_head rx_queue;
111 struct sk_buff_head tx_queue;
113 struct timer_list net_timer;
115 struct page *mmap_pages[MAX_PENDING_REQS];
117 pending_ring_idx_t pending_prod;
118 pending_ring_idx_t pending_cons;
119 struct list_head net_schedule_list;
121 /* Protect the net_schedule_list in netif. */
122 spinlock_t net_schedule_list_lock;
124 atomic_t netfront_count;
126 struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
127 /* Coalescing tx requests before copying makes number of grant
128 * copy ops greater or equal to number of slots required. In
129 * worst case a tx request consumes 2 gnttab_copy.
131 struct gnttab_copy tx_copy_ops[2*MAX_PENDING_REQS];
133 u16 pending_ring[MAX_PENDING_REQS];
136 * Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
137 * head/fragment page uses 2 copy operations because it
138 * straddles two buffers in the frontend.
140 struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
141 struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
144 static struct xen_netbk *xen_netbk;
145 static int xen_netbk_group_nr;
148 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
149 * one or more merged tx requests, otherwise it is the continuation of
150 * previous tx request.
152 static inline int pending_tx_is_head(struct xen_netbk *netbk, RING_IDX idx)
154 return netbk->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
157 void xen_netbk_add_xenvif(struct xenvif *vif)
160 int min_netfront_count;
162 struct xen_netbk *netbk;
164 min_netfront_count = atomic_read(&xen_netbk[0].netfront_count);
165 for (i = 0; i < xen_netbk_group_nr; i++) {
166 int netfront_count = atomic_read(&xen_netbk[i].netfront_count);
167 if (netfront_count < min_netfront_count) {
169 min_netfront_count = netfront_count;
173 netbk = &xen_netbk[min_group];
176 atomic_inc(&netbk->netfront_count);
179 void xen_netbk_remove_xenvif(struct xenvif *vif)
181 struct xen_netbk *netbk = vif->netbk;
183 atomic_dec(&netbk->netfront_count);
186 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
188 static void make_tx_response(struct xenvif *vif,
189 struct xen_netif_tx_request *txp,
191 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
198 static inline unsigned long idx_to_pfn(struct xen_netbk *netbk,
201 return page_to_pfn(netbk->mmap_pages[idx]);
204 static inline unsigned long idx_to_kaddr(struct xen_netbk *netbk,
207 return (unsigned long)pfn_to_kaddr(idx_to_pfn(netbk, idx));
210 /* extra field used in struct page */
211 static inline void set_page_ext(struct page *pg, struct xen_netbk *netbk,
214 unsigned int group = netbk - xen_netbk;
215 union page_ext ext = { .e = { .group = group + 1, .idx = idx } };
217 BUILD_BUG_ON(sizeof(ext) > sizeof(ext.mapping));
218 pg->mapping = ext.mapping;
221 static int get_page_ext(struct page *pg,
222 unsigned int *pgroup, unsigned int *pidx)
224 union page_ext ext = { .mapping = pg->mapping };
225 struct xen_netbk *netbk;
226 unsigned int group, idx;
228 group = ext.e.group - 1;
230 if (group < 0 || group >= xen_netbk_group_nr)
233 netbk = &xen_netbk[group];
237 if ((idx < 0) || (idx >= MAX_PENDING_REQS))
240 if (netbk->mmap_pages[idx] != pg)
250 * This is the amount of packet we copy rather than map, so that the
251 * guest can't fiddle with the contents of the headers while we do
252 * packet processing on them (netfilter, routing, etc).
254 #define PKT_PROT_LEN (ETH_HLEN + \
256 sizeof(struct iphdr) + MAX_IPOPTLEN + \
257 sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE)
259 static u16 frag_get_pending_idx(skb_frag_t *frag)
261 return (u16)frag->page_offset;
264 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
266 frag->page_offset = pending_idx;
269 static inline pending_ring_idx_t pending_index(unsigned i)
271 return i & (MAX_PENDING_REQS-1);
274 static inline pending_ring_idx_t nr_pending_reqs(struct xen_netbk *netbk)
276 return MAX_PENDING_REQS -
277 netbk->pending_prod + netbk->pending_cons;
280 static void xen_netbk_kick_thread(struct xen_netbk *netbk)
285 static int max_required_rx_slots(struct xenvif *vif)
287 int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
289 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
290 if (vif->can_sg || vif->gso || vif->gso_prefix)
291 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
296 int xen_netbk_rx_ring_full(struct xenvif *vif)
298 RING_IDX peek = vif->rx_req_cons_peek;
299 RING_IDX needed = max_required_rx_slots(vif);
301 return ((vif->rx.sring->req_prod - peek) < needed) ||
302 ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
305 int xen_netbk_must_stop_queue(struct xenvif *vif)
307 if (!xen_netbk_rx_ring_full(vif))
310 vif->rx.sring->req_event = vif->rx_req_cons_peek +
311 max_required_rx_slots(vif);
312 mb(); /* request notification /then/ check the queue */
314 return xen_netbk_rx_ring_full(vif);
318 * Returns true if we should start a new receive buffer instead of
319 * adding 'size' bytes to a buffer which currently contains 'offset'
322 static bool start_new_rx_buffer(int offset, unsigned long size, int head)
324 /* simple case: we have completely filled the current buffer. */
325 if (offset == MAX_BUFFER_OFFSET)
329 * complex case: start a fresh buffer if the current frag
330 * would overflow the current buffer but only if:
331 * (i) this frag would fit completely in the next buffer
332 * and (ii) there is already some data in the current buffer
333 * and (iii) this is not the head buffer.
336 * - (i) stops us splitting a frag into two copies
337 * unless the frag is too large for a single buffer.
338 * - (ii) stops us from leaving a buffer pointlessly empty.
339 * - (iii) stops us leaving the first buffer
340 * empty. Strictly speaking this is already covered
341 * by (ii) but is explicitly checked because
342 * netfront relies on the first buffer being
343 * non-empty and can crash otherwise.
345 * This means we will effectively linearise small
346 * frags but do not needlessly split large buffers
347 * into multiple copies tend to give large frags their
348 * own buffers as before.
350 if ((offset + size > MAX_BUFFER_OFFSET) &&
351 (size <= MAX_BUFFER_OFFSET) && offset && !head)
358 * Figure out how many ring slots we're going to need to send @skb to
359 * the guest. This function is essentially a dry run of
360 * netbk_gop_frag_copy.
362 unsigned int xen_netbk_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
367 count = DIV_ROUND_UP(skb_headlen(skb), PAGE_SIZE);
369 copy_off = skb_headlen(skb) % PAGE_SIZE;
371 if (skb_shinfo(skb)->gso_size)
374 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
375 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
376 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
379 offset &= ~PAGE_MASK;
382 BUG_ON(offset >= PAGE_SIZE);
383 BUG_ON(copy_off > MAX_BUFFER_OFFSET);
385 bytes = PAGE_SIZE - offset;
390 if (start_new_rx_buffer(copy_off, bytes, 0)) {
395 if (copy_off + bytes > MAX_BUFFER_OFFSET)
396 bytes = MAX_BUFFER_OFFSET - copy_off;
403 if (offset == PAGE_SIZE)
410 struct netrx_pending_operations {
411 unsigned copy_prod, copy_cons;
412 unsigned meta_prod, meta_cons;
413 struct gnttab_copy *copy;
414 struct netbk_rx_meta *meta;
416 grant_ref_t copy_gref;
419 static struct netbk_rx_meta *get_next_rx_buffer(struct xenvif *vif,
420 struct netrx_pending_operations *npo)
422 struct netbk_rx_meta *meta;
423 struct xen_netif_rx_request *req;
425 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
427 meta = npo->meta + npo->meta_prod++;
433 npo->copy_gref = req->gref;
439 * Set up the grant operations for this fragment. If it's a flipping
440 * interface, we also set up the unmap request from here.
442 static void netbk_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
443 struct netrx_pending_operations *npo,
444 struct page *page, unsigned long size,
445 unsigned long offset, int *head)
447 struct gnttab_copy *copy_gop;
448 struct netbk_rx_meta *meta;
450 * These variables are used iff get_page_ext returns true,
451 * in which case they are guaranteed to be initialized.
453 unsigned int uninitialized_var(group), uninitialized_var(idx);
454 int foreign = get_page_ext(page, &group, &idx);
457 /* Data must not cross a page boundary. */
458 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
460 meta = npo->meta + npo->meta_prod - 1;
462 /* Skip unused frames from start of page */
463 page += offset >> PAGE_SHIFT;
464 offset &= ~PAGE_MASK;
467 BUG_ON(offset >= PAGE_SIZE);
468 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
470 bytes = PAGE_SIZE - offset;
475 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
477 * Netfront requires there to be some data in the head
482 meta = get_next_rx_buffer(vif, npo);
485 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
486 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
488 copy_gop = npo->copy + npo->copy_prod++;
489 copy_gop->flags = GNTCOPY_dest_gref;
491 struct xen_netbk *netbk = &xen_netbk[group];
492 struct pending_tx_info *src_pend;
494 src_pend = &netbk->pending_tx_info[idx];
496 copy_gop->source.domid = src_pend->vif->domid;
497 copy_gop->source.u.ref = src_pend->req.gref;
498 copy_gop->flags |= GNTCOPY_source_gref;
500 void *vaddr = page_address(page);
501 copy_gop->source.domid = DOMID_SELF;
502 copy_gop->source.u.gmfn = virt_to_mfn(vaddr);
504 copy_gop->source.offset = offset;
505 copy_gop->dest.domid = vif->domid;
507 copy_gop->dest.offset = npo->copy_off;
508 copy_gop->dest.u.ref = npo->copy_gref;
509 copy_gop->len = bytes;
511 npo->copy_off += bytes;
518 if (offset == PAGE_SIZE && size) {
519 BUG_ON(!PageCompound(page));
524 /* Leave a gap for the GSO descriptor. */
525 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
528 *head = 0; /* There must be something in this buffer now. */
534 * Prepare an SKB to be transmitted to the frontend.
536 * This function is responsible for allocating grant operations, meta
539 * It returns the number of meta structures consumed. The number of
540 * ring slots used is always equal to the number of meta slots used
541 * plus the number of GSO descriptors used. Currently, we use either
542 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
543 * frontend-side LRO).
545 static int netbk_gop_skb(struct sk_buff *skb,
546 struct netrx_pending_operations *npo)
548 struct xenvif *vif = netdev_priv(skb->dev);
549 int nr_frags = skb_shinfo(skb)->nr_frags;
551 struct xen_netif_rx_request *req;
552 struct netbk_rx_meta *meta;
557 old_meta_prod = npo->meta_prod;
559 /* Set up a GSO prefix descriptor, if necessary */
560 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
561 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
562 meta = npo->meta + npo->meta_prod++;
563 meta->gso_size = skb_shinfo(skb)->gso_size;
568 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
569 meta = npo->meta + npo->meta_prod++;
571 if (!vif->gso_prefix)
572 meta->gso_size = skb_shinfo(skb)->gso_size;
579 npo->copy_gref = req->gref;
582 while (data < skb_tail_pointer(skb)) {
583 unsigned int offset = offset_in_page(data);
584 unsigned int len = PAGE_SIZE - offset;
586 if (data + len > skb_tail_pointer(skb))
587 len = skb_tail_pointer(skb) - data;
589 netbk_gop_frag_copy(vif, skb, npo,
590 virt_to_page(data), len, offset, &head);
594 for (i = 0; i < nr_frags; i++) {
595 netbk_gop_frag_copy(vif, skb, npo,
596 skb_frag_page(&skb_shinfo(skb)->frags[i]),
597 skb_frag_size(&skb_shinfo(skb)->frags[i]),
598 skb_shinfo(skb)->frags[i].page_offset,
602 return npo->meta_prod - old_meta_prod;
606 * This is a twin to netbk_gop_skb. Assume that netbk_gop_skb was
607 * used to set up the operations on the top of
608 * netrx_pending_operations, which have since been done. Check that
609 * they didn't give any errors and advance over them.
611 static int netbk_check_gop(struct xenvif *vif, int nr_meta_slots,
612 struct netrx_pending_operations *npo)
614 struct gnttab_copy *copy_op;
615 int status = XEN_NETIF_RSP_OKAY;
618 for (i = 0; i < nr_meta_slots; i++) {
619 copy_op = npo->copy + npo->copy_cons++;
620 if (copy_op->status != GNTST_okay) {
622 "Bad status %d from copy to DOM%d.\n",
623 copy_op->status, vif->domid);
624 status = XEN_NETIF_RSP_ERROR;
631 static void netbk_add_frag_responses(struct xenvif *vif, int status,
632 struct netbk_rx_meta *meta,
636 unsigned long offset;
638 /* No fragments used */
639 if (nr_meta_slots <= 1)
644 for (i = 0; i < nr_meta_slots; i++) {
646 if (i == nr_meta_slots - 1)
649 flags = XEN_NETRXF_more_data;
652 make_rx_response(vif, meta[i].id, status, offset,
653 meta[i].size, flags);
657 struct skb_cb_overlay {
661 static void xen_netbk_rx_action(struct xen_netbk *netbk)
663 struct xenvif *vif = NULL, *tmp;
666 struct xen_netif_rx_response *resp;
667 struct sk_buff_head rxq;
673 unsigned long offset;
674 struct skb_cb_overlay *sco;
676 struct netrx_pending_operations npo = {
677 .copy = netbk->grant_copy_op,
681 skb_queue_head_init(&rxq);
685 while ((skb = skb_dequeue(&netbk->rx_queue)) != NULL) {
686 vif = netdev_priv(skb->dev);
687 nr_frags = skb_shinfo(skb)->nr_frags;
689 sco = (struct skb_cb_overlay *)skb->cb;
690 sco->meta_slots_used = netbk_gop_skb(skb, &npo);
692 count += nr_frags + 1;
694 __skb_queue_tail(&rxq, skb);
696 /* Filled the batch queue? */
697 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
698 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
702 BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
707 BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
708 gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
710 while ((skb = __skb_dequeue(&rxq)) != NULL) {
711 sco = (struct skb_cb_overlay *)skb->cb;
713 vif = netdev_priv(skb->dev);
715 if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
716 resp = RING_GET_RESPONSE(&vif->rx,
717 vif->rx.rsp_prod_pvt++);
719 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
721 resp->offset = netbk->meta[npo.meta_cons].gso_size;
722 resp->id = netbk->meta[npo.meta_cons].id;
723 resp->status = sco->meta_slots_used;
726 sco->meta_slots_used--;
730 vif->dev->stats.tx_bytes += skb->len;
731 vif->dev->stats.tx_packets++;
733 status = netbk_check_gop(vif, sco->meta_slots_used, &npo);
735 if (sco->meta_slots_used == 1)
738 flags = XEN_NETRXF_more_data;
740 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
741 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
742 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
743 /* remote but checksummed. */
744 flags |= XEN_NETRXF_data_validated;
747 resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
749 netbk->meta[npo.meta_cons].size,
752 if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
753 struct xen_netif_extra_info *gso =
754 (struct xen_netif_extra_info *)
755 RING_GET_RESPONSE(&vif->rx,
756 vif->rx.rsp_prod_pvt++);
758 resp->flags |= XEN_NETRXF_extra_info;
760 gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
761 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
763 gso->u.gso.features = 0;
765 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
769 netbk_add_frag_responses(vif, status,
770 netbk->meta + npo.meta_cons + 1,
771 sco->meta_slots_used);
773 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
775 if (ret && list_empty(&vif->notify_list))
776 list_add_tail(&vif->notify_list, ¬ify);
778 xenvif_notify_tx_completion(vif);
781 npo.meta_cons += sco->meta_slots_used;
785 list_for_each_entry_safe(vif, tmp, ¬ify, notify_list) {
786 notify_remote_via_irq(vif->irq);
787 list_del_init(&vif->notify_list);
790 /* More work to do? */
791 if (!skb_queue_empty(&netbk->rx_queue) &&
792 !timer_pending(&netbk->net_timer))
793 xen_netbk_kick_thread(netbk);
796 void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
798 struct xen_netbk *netbk = vif->netbk;
800 skb_queue_tail(&netbk->rx_queue, skb);
802 xen_netbk_kick_thread(netbk);
805 static void xen_netbk_alarm(unsigned long data)
807 struct xen_netbk *netbk = (struct xen_netbk *)data;
808 xen_netbk_kick_thread(netbk);
811 static int __on_net_schedule_list(struct xenvif *vif)
813 return !list_empty(&vif->schedule_list);
816 /* Must be called with net_schedule_list_lock held */
817 static void remove_from_net_schedule_list(struct xenvif *vif)
819 if (likely(__on_net_schedule_list(vif))) {
820 list_del_init(&vif->schedule_list);
825 static struct xenvif *poll_net_schedule_list(struct xen_netbk *netbk)
827 struct xenvif *vif = NULL;
829 spin_lock_irq(&netbk->net_schedule_list_lock);
830 if (list_empty(&netbk->net_schedule_list))
833 vif = list_first_entry(&netbk->net_schedule_list,
834 struct xenvif, schedule_list);
840 remove_from_net_schedule_list(vif);
842 spin_unlock_irq(&netbk->net_schedule_list_lock);
846 void xen_netbk_schedule_xenvif(struct xenvif *vif)
849 struct xen_netbk *netbk = vif->netbk;
851 if (__on_net_schedule_list(vif))
854 spin_lock_irqsave(&netbk->net_schedule_list_lock, flags);
855 if (!__on_net_schedule_list(vif) &&
856 likely(xenvif_schedulable(vif))) {
857 list_add_tail(&vif->schedule_list, &netbk->net_schedule_list);
860 spin_unlock_irqrestore(&netbk->net_schedule_list_lock, flags);
864 if ((nr_pending_reqs(netbk) < (MAX_PENDING_REQS/2)) &&
865 !list_empty(&netbk->net_schedule_list))
866 xen_netbk_kick_thread(netbk);
869 void xen_netbk_deschedule_xenvif(struct xenvif *vif)
871 struct xen_netbk *netbk = vif->netbk;
872 spin_lock_irq(&netbk->net_schedule_list_lock);
873 remove_from_net_schedule_list(vif);
874 spin_unlock_irq(&netbk->net_schedule_list_lock);
877 void xen_netbk_check_rx_xenvif(struct xenvif *vif)
881 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
884 xen_netbk_schedule_xenvif(vif);
887 static void tx_add_credit(struct xenvif *vif)
889 unsigned long max_burst, max_credit;
892 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
893 * Otherwise the interface can seize up due to insufficient credit.
895 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
896 max_burst = min(max_burst, 131072UL);
897 max_burst = max(max_burst, vif->credit_bytes);
899 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
900 max_credit = vif->remaining_credit + vif->credit_bytes;
901 if (max_credit < vif->remaining_credit)
902 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
904 vif->remaining_credit = min(max_credit, max_burst);
907 static void tx_credit_callback(unsigned long data)
909 struct xenvif *vif = (struct xenvif *)data;
911 xen_netbk_check_rx_xenvif(vif);
914 static void netbk_tx_err(struct xenvif *vif,
915 struct xen_netif_tx_request *txp, RING_IDX end)
917 RING_IDX cons = vif->tx.req_cons;
920 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
923 txp = RING_GET_REQUEST(&vif->tx, cons++);
925 vif->tx.req_cons = cons;
926 xen_netbk_check_rx_xenvif(vif);
930 static void netbk_fatal_tx_err(struct xenvif *vif)
932 netdev_err(vif->dev, "fatal error; disabling device\n");
933 xenvif_carrier_off(vif);
937 static int netbk_count_requests(struct xenvif *vif,
938 struct xen_netif_tx_request *first,
939 struct xen_netif_tx_request *txp,
942 RING_IDX cons = vif->tx.req_cons;
947 if (!(first->flags & XEN_NETTXF_more_data))
951 struct xen_netif_tx_request dropped_tx = { 0 };
953 if (slots >= work_to_do) {
955 "Asked for %d slots but exceeds this limit\n",
957 netbk_fatal_tx_err(vif);
961 /* This guest is really using too many slots and
962 * considered malicious.
964 if (unlikely(slots >= fatal_skb_slots)) {
966 "Malicious frontend using %d slots, threshold %u\n",
967 slots, fatal_skb_slots);
968 netbk_fatal_tx_err(vif);
972 /* Xen network protocol had implicit dependency on
973 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
974 * the historical MAX_SKB_FRAGS value 18 to honor the
975 * same behavior as before. Any packet using more than
976 * 18 slots but less than fatal_skb_slots slots is
979 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
982 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
983 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
990 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
993 /* If the guest submitted a frame >= 64 KiB then
994 * first->size overflowed and following slots will
995 * appear to be larger than the frame.
997 * This cannot be fatal error as there are buggy
998 * frontends that do this.
1000 * Consume all slots and drop the packet.
1002 if (!drop_err && txp->size > first->size) {
1003 if (net_ratelimit())
1004 netdev_dbg(vif->dev,
1005 "Invalid tx request, slot size %u > remaining size %u\n",
1006 txp->size, first->size);
1010 first->size -= txp->size;
1013 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
1014 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
1015 txp->offset, txp->size);
1016 netbk_fatal_tx_err(vif);
1020 more_data = txp->flags & XEN_NETTXF_more_data;
1025 } while (more_data);
1028 netbk_tx_err(vif, first, cons + slots);
1035 static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
1039 page = alloc_page(GFP_KERNEL|__GFP_COLD);
1042 set_page_ext(page, netbk, pending_idx);
1043 netbk->mmap_pages[pending_idx] = page;
1047 static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
1049 struct sk_buff *skb,
1050 struct xen_netif_tx_request *txp,
1051 struct gnttab_copy *gop)
1053 struct skb_shared_info *shinfo = skb_shinfo(skb);
1054 skb_frag_t *frags = shinfo->frags;
1055 u16 pending_idx = *((u16 *)skb->data);
1059 pending_ring_idx_t index, start_idx = 0;
1060 uint16_t dst_offset;
1061 unsigned int nr_slots;
1062 struct pending_tx_info *first = NULL;
1064 /* At this point shinfo->nr_frags is in fact the number of
1065 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
1067 nr_slots = shinfo->nr_frags;
1069 /* Skip first skb fragment if it is on same page as header fragment. */
1070 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
1072 /* Coalesce tx requests, at this point the packet passed in
1073 * should be <= 64K. Any packets larger than 64K have been
1074 * handled in netbk_count_requests().
1076 for (shinfo->nr_frags = slot = start; slot < nr_slots;
1077 shinfo->nr_frags++) {
1078 struct pending_tx_info *pending_tx_info =
1079 netbk->pending_tx_info;
1081 page = alloc_page(GFP_KERNEL|__GFP_COLD);
1087 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
1088 gop->flags = GNTCOPY_source_gref;
1090 gop->source.u.ref = txp->gref;
1091 gop->source.domid = vif->domid;
1092 gop->source.offset = txp->offset;
1094 gop->dest.domid = DOMID_SELF;
1096 gop->dest.offset = dst_offset;
1097 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1099 if (dst_offset + txp->size > PAGE_SIZE) {
1100 /* This page can only merge a portion
1101 * of tx request. Do not increment any
1102 * pointer / counter here. The txp
1103 * will be dealt with in future
1104 * rounds, eventually hitting the
1107 gop->len = PAGE_SIZE - dst_offset;
1108 txp->offset += gop->len;
1109 txp->size -= gop->len;
1110 dst_offset += gop->len; /* quit loop */
1112 /* This tx request can be merged in the page */
1113 gop->len = txp->size;
1114 dst_offset += gop->len;
1116 index = pending_index(netbk->pending_cons++);
1118 pending_idx = netbk->pending_ring[index];
1120 memcpy(&pending_tx_info[pending_idx].req, txp,
1124 pending_tx_info[pending_idx].vif = vif;
1126 /* Poison these fields, corresponding
1127 * fields for head tx req will be set
1128 * to correct values after the loop.
1130 netbk->mmap_pages[pending_idx] = (void *)(~0UL);
1131 pending_tx_info[pending_idx].head =
1132 INVALID_PENDING_RING_IDX;
1135 first = &pending_tx_info[pending_idx];
1137 head_idx = pending_idx;
1147 first->req.offset = 0;
1148 first->req.size = dst_offset;
1149 first->head = start_idx;
1150 set_page_ext(page, netbk, head_idx);
1151 netbk->mmap_pages[head_idx] = page;
1152 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
1155 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
1159 /* Unwind, freeing all pages and sending error responses. */
1160 while (shinfo->nr_frags-- > start) {
1161 xen_netbk_idx_release(netbk,
1162 frag_get_pending_idx(&frags[shinfo->nr_frags]),
1163 XEN_NETIF_RSP_ERROR);
1165 /* The head too, if necessary. */
1167 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1172 static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
1173 struct sk_buff *skb,
1174 struct gnttab_copy **gopp)
1176 struct gnttab_copy *gop = *gopp;
1177 u16 pending_idx = *((u16 *)skb->data);
1178 struct skb_shared_info *shinfo = skb_shinfo(skb);
1179 struct pending_tx_info *tx_info;
1180 int nr_frags = shinfo->nr_frags;
1182 u16 peek; /* peek into next tx request */
1184 /* Check status of header. */
1187 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1189 /* Skip first skb fragment if it is on same page as header fragment. */
1190 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
1192 for (i = start; i < nr_frags; i++) {
1194 pending_ring_idx_t head;
1196 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
1197 tx_info = &netbk->pending_tx_info[pending_idx];
1198 head = tx_info->head;
1200 /* Check error status: if okay then remember grant handle. */
1202 newerr = (++gop)->status;
1205 peek = netbk->pending_ring[pending_index(++head)];
1206 } while (!pending_tx_is_head(netbk, peek));
1208 if (likely(!newerr)) {
1209 /* Had a previous error? Invalidate this fragment. */
1211 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1215 /* Error on this fragment: respond to client with an error. */
1216 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1218 /* Not the first error? Preceding frags already invalidated. */
1222 /* First error: invalidate header and preceding fragments. */
1223 pending_idx = *((u16 *)skb->data);
1224 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1225 for (j = start; j < i; j++) {
1226 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1227 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1230 /* Remember the error: invalidate all subsequent fragments. */
1238 static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
1240 struct skb_shared_info *shinfo = skb_shinfo(skb);
1241 int nr_frags = shinfo->nr_frags;
1244 for (i = 0; i < nr_frags; i++) {
1245 skb_frag_t *frag = shinfo->frags + i;
1246 struct xen_netif_tx_request *txp;
1250 pending_idx = frag_get_pending_idx(frag);
1252 txp = &netbk->pending_tx_info[pending_idx].req;
1253 page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
1254 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1255 skb->len += txp->size;
1256 skb->data_len += txp->size;
1257 skb->truesize += txp->size;
1259 /* Take an extra reference to offset xen_netbk_idx_release */
1260 get_page(netbk->mmap_pages[pending_idx]);
1261 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1265 static int xen_netbk_get_extras(struct xenvif *vif,
1266 struct xen_netif_extra_info *extras,
1269 struct xen_netif_extra_info extra;
1270 RING_IDX cons = vif->tx.req_cons;
1273 if (unlikely(work_to_do-- <= 0)) {
1274 netdev_err(vif->dev, "Missing extra info\n");
1275 netbk_fatal_tx_err(vif);
1279 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1281 if (unlikely(!extra.type ||
1282 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1283 vif->tx.req_cons = ++cons;
1284 netdev_err(vif->dev,
1285 "Invalid extra type: %d\n", extra.type);
1286 netbk_fatal_tx_err(vif);
1290 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1291 vif->tx.req_cons = ++cons;
1292 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1297 static int netbk_set_skb_gso(struct xenvif *vif,
1298 struct sk_buff *skb,
1299 struct xen_netif_extra_info *gso)
1301 if (!gso->u.gso.size) {
1302 netdev_err(vif->dev, "GSO size must not be zero.\n");
1303 netbk_fatal_tx_err(vif);
1307 /* Currently only TCPv4 S.O. is supported. */
1308 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1309 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1310 netbk_fatal_tx_err(vif);
1314 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1315 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1317 /* Header must be checked, and gso_segs computed. */
1318 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1319 skb_shinfo(skb)->gso_segs = 0;
1324 static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1328 int recalculate_partial_csum = 0;
1331 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1332 * peers can fail to set NETRXF_csum_blank when sending a GSO
1333 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1334 * recalculate the partial checksum.
1336 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1337 vif->rx_gso_checksum_fixup++;
1338 skb->ip_summed = CHECKSUM_PARTIAL;
1339 recalculate_partial_csum = 1;
1342 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1343 if (skb->ip_summed != CHECKSUM_PARTIAL)
1346 if (skb->protocol != htons(ETH_P_IP))
1349 iph = (void *)skb->data;
1350 switch (iph->protocol) {
1352 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1353 offsetof(struct tcphdr, check)))
1356 if (recalculate_partial_csum) {
1357 struct tcphdr *tcph = tcp_hdr(skb);
1358 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1359 skb->len - iph->ihl*4,
1364 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1365 offsetof(struct udphdr, check)))
1368 if (recalculate_partial_csum) {
1369 struct udphdr *udph = udp_hdr(skb);
1370 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1371 skb->len - iph->ihl*4,
1376 if (net_ratelimit())
1377 netdev_err(vif->dev,
1378 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1389 static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1391 unsigned long now = jiffies;
1392 unsigned long next_credit =
1393 vif->credit_timeout.expires +
1394 msecs_to_jiffies(vif->credit_usec / 1000);
1396 /* Timer could already be pending in rare cases. */
1397 if (timer_pending(&vif->credit_timeout))
1400 /* Passed the point where we can replenish credit? */
1401 if (time_after_eq(now, next_credit)) {
1402 vif->credit_timeout.expires = now;
1406 /* Still too big to send right now? Set a callback. */
1407 if (size > vif->remaining_credit) {
1408 vif->credit_timeout.data =
1410 vif->credit_timeout.function =
1412 mod_timer(&vif->credit_timeout,
1421 static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
1423 struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
1424 struct sk_buff *skb;
1427 while ((nr_pending_reqs(netbk) + XEN_NETBK_LEGACY_SLOTS_MAX
1428 < MAX_PENDING_REQS) &&
1429 !list_empty(&netbk->net_schedule_list)) {
1431 struct xen_netif_tx_request txreq;
1432 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1434 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1438 unsigned int data_len;
1439 pending_ring_idx_t index;
1441 /* Get a netif from the list with work to do. */
1442 vif = poll_net_schedule_list(netbk);
1443 /* This can sometimes happen because the test of
1444 * list_empty(net_schedule_list) at the top of the
1445 * loop is unlocked. Just go back and have another
1451 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1452 XEN_NETIF_TX_RING_SIZE) {
1453 netdev_err(vif->dev,
1454 "Impossible number of requests. "
1455 "req_prod %d, req_cons %d, size %ld\n",
1456 vif->tx.sring->req_prod, vif->tx.req_cons,
1457 XEN_NETIF_TX_RING_SIZE);
1458 netbk_fatal_tx_err(vif);
1462 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1468 idx = vif->tx.req_cons;
1469 rmb(); /* Ensure that we see the request before we copy it. */
1470 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1472 /* Credit-based scheduling. */
1473 if (txreq.size > vif->remaining_credit &&
1474 tx_credit_exceeded(vif, txreq.size)) {
1479 vif->remaining_credit -= txreq.size;
1482 vif->tx.req_cons = ++idx;
1484 memset(extras, 0, sizeof(extras));
1485 if (txreq.flags & XEN_NETTXF_extra_info) {
1486 work_to_do = xen_netbk_get_extras(vif, extras,
1488 idx = vif->tx.req_cons;
1489 if (unlikely(work_to_do < 0))
1493 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
1494 if (unlikely(ret < 0))
1499 if (unlikely(txreq.size < ETH_HLEN)) {
1500 netdev_dbg(vif->dev,
1501 "Bad packet size: %d\n", txreq.size);
1502 netbk_tx_err(vif, &txreq, idx);
1506 /* No crossing a page as the payload mustn't fragment. */
1507 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1508 netdev_err(vif->dev,
1509 "txreq.offset: %x, size: %u, end: %lu\n",
1510 txreq.offset, txreq.size,
1511 (txreq.offset&~PAGE_MASK) + txreq.size);
1512 netbk_fatal_tx_err(vif);
1516 index = pending_index(netbk->pending_cons);
1517 pending_idx = netbk->pending_ring[index];
1519 data_len = (txreq.size > PKT_PROT_LEN &&
1520 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1521 PKT_PROT_LEN : txreq.size;
1523 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1524 GFP_ATOMIC | __GFP_NOWARN);
1525 if (unlikely(skb == NULL)) {
1526 netdev_dbg(vif->dev,
1527 "Can't allocate a skb in start_xmit.\n");
1528 netbk_tx_err(vif, &txreq, idx);
1532 /* Packets passed to netif_rx() must have some headroom. */
1533 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1535 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1536 struct xen_netif_extra_info *gso;
1537 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1539 if (netbk_set_skb_gso(vif, skb, gso)) {
1540 /* Failure in netbk_set_skb_gso is fatal. */
1546 /* XXX could copy straight to head */
1547 page = xen_netbk_alloc_page(netbk, pending_idx);
1550 netbk_tx_err(vif, &txreq, idx);
1554 gop->source.u.ref = txreq.gref;
1555 gop->source.domid = vif->domid;
1556 gop->source.offset = txreq.offset;
1558 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1559 gop->dest.domid = DOMID_SELF;
1560 gop->dest.offset = txreq.offset;
1562 gop->len = txreq.size;
1563 gop->flags = GNTCOPY_source_gref;
1567 memcpy(&netbk->pending_tx_info[pending_idx].req,
1568 &txreq, sizeof(txreq));
1569 netbk->pending_tx_info[pending_idx].vif = vif;
1570 netbk->pending_tx_info[pending_idx].head = index;
1571 *((u16 *)skb->data) = pending_idx;
1573 __skb_put(skb, data_len);
1575 skb_shinfo(skb)->nr_frags = ret;
1576 if (data_len < txreq.size) {
1577 skb_shinfo(skb)->nr_frags++;
1578 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1581 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1582 INVALID_PENDING_IDX);
1585 netbk->pending_cons++;
1587 request_gop = xen_netbk_get_requests(netbk, vif,
1589 if (request_gop == NULL) {
1591 netbk_tx_err(vif, &txreq, idx);
1596 __skb_queue_tail(&netbk->tx_queue, skb);
1598 vif->tx.req_cons = idx;
1599 xen_netbk_check_rx_xenvif(vif);
1601 if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
1605 return gop - netbk->tx_copy_ops;
1608 static void xen_netbk_tx_submit(struct xen_netbk *netbk)
1610 struct gnttab_copy *gop = netbk->tx_copy_ops;
1611 struct sk_buff *skb;
1613 while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
1614 struct xen_netif_tx_request *txp;
1619 pending_idx = *((u16 *)skb->data);
1620 vif = netbk->pending_tx_info[pending_idx].vif;
1621 txp = &netbk->pending_tx_info[pending_idx].req;
1623 /* Check the remap error code. */
1624 if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1625 netdev_dbg(vif->dev, "netback grant failed.\n");
1626 skb_shinfo(skb)->nr_frags = 0;
1631 data_len = skb->len;
1633 (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
1635 if (data_len < txp->size) {
1636 /* Append the packet payload as a fragment. */
1637 txp->offset += data_len;
1638 txp->size -= data_len;
1640 /* Schedule a response immediately. */
1641 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1644 if (txp->flags & XEN_NETTXF_csum_blank)
1645 skb->ip_summed = CHECKSUM_PARTIAL;
1646 else if (txp->flags & XEN_NETTXF_data_validated)
1647 skb->ip_summed = CHECKSUM_UNNECESSARY;
1649 xen_netbk_fill_frags(netbk, skb);
1652 * If the initial fragment was < PKT_PROT_LEN then
1653 * pull through some bytes from the other fragments to
1654 * increase the linear region to PKT_PROT_LEN bytes.
1656 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1657 int target = min_t(int, skb->len, PKT_PROT_LEN);
1658 __pskb_pull_tail(skb, target - skb_headlen(skb));
1661 skb->dev = vif->dev;
1662 skb->protocol = eth_type_trans(skb, skb->dev);
1663 skb_reset_network_header(skb);
1665 if (checksum_setup(vif, skb)) {
1666 netdev_dbg(vif->dev,
1667 "Can't setup checksum in net_tx_action\n");
1672 skb_probe_transport_header(skb, 0);
1674 vif->dev->stats.rx_bytes += skb->len;
1675 vif->dev->stats.rx_packets++;
1677 xenvif_receive_skb(vif, skb);
1681 /* Called after netfront has transmitted */
1682 static void xen_netbk_tx_action(struct xen_netbk *netbk)
1686 nr_gops = xen_netbk_tx_build_gops(netbk);
1691 gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
1693 xen_netbk_tx_submit(netbk);
1696 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
1700 struct pending_tx_info *pending_tx_info;
1701 pending_ring_idx_t head;
1702 u16 peek; /* peek into next tx request */
1704 BUG_ON(netbk->mmap_pages[pending_idx] == (void *)(~0UL));
1706 /* Already complete? */
1707 if (netbk->mmap_pages[pending_idx] == NULL)
1710 pending_tx_info = &netbk->pending_tx_info[pending_idx];
1712 vif = pending_tx_info->vif;
1713 head = pending_tx_info->head;
1715 BUG_ON(!pending_tx_is_head(netbk, head));
1716 BUG_ON(netbk->pending_ring[pending_index(head)] != pending_idx);
1719 pending_ring_idx_t index;
1720 pending_ring_idx_t idx = pending_index(head);
1721 u16 info_idx = netbk->pending_ring[idx];
1723 pending_tx_info = &netbk->pending_tx_info[info_idx];
1724 make_tx_response(vif, &pending_tx_info->req, status);
1726 /* Setting any number other than
1727 * INVALID_PENDING_RING_IDX indicates this slot is
1728 * starting a new packet / ending a previous packet.
1730 pending_tx_info->head = 0;
1732 index = pending_index(netbk->pending_prod++);
1733 netbk->pending_ring[index] = netbk->pending_ring[info_idx];
1737 peek = netbk->pending_ring[pending_index(++head)];
1739 } while (!pending_tx_is_head(netbk, peek));
1741 netbk->mmap_pages[pending_idx]->mapping = 0;
1742 put_page(netbk->mmap_pages[pending_idx]);
1743 netbk->mmap_pages[pending_idx] = NULL;
1747 static void make_tx_response(struct xenvif *vif,
1748 struct xen_netif_tx_request *txp,
1751 RING_IDX i = vif->tx.rsp_prod_pvt;
1752 struct xen_netif_tx_response *resp;
1755 resp = RING_GET_RESPONSE(&vif->tx, i);
1759 if (txp->flags & XEN_NETTXF_extra_info)
1760 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1762 vif->tx.rsp_prod_pvt = ++i;
1763 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1765 notify_remote_via_irq(vif->irq);
1768 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1775 RING_IDX i = vif->rx.rsp_prod_pvt;
1776 struct xen_netif_rx_response *resp;
1778 resp = RING_GET_RESPONSE(&vif->rx, i);
1779 resp->offset = offset;
1780 resp->flags = flags;
1782 resp->status = (s16)size;
1784 resp->status = (s16)st;
1786 vif->rx.rsp_prod_pvt = ++i;
1791 static inline int rx_work_todo(struct xen_netbk *netbk)
1793 return !skb_queue_empty(&netbk->rx_queue);
1796 static inline int tx_work_todo(struct xen_netbk *netbk)
1799 if ((nr_pending_reqs(netbk) + XEN_NETBK_LEGACY_SLOTS_MAX
1800 < MAX_PENDING_REQS) &&
1801 !list_empty(&netbk->net_schedule_list))
1807 static int xen_netbk_kthread(void *data)
1809 struct xen_netbk *netbk = data;
1810 while (!kthread_should_stop()) {
1811 wait_event_interruptible(netbk->wq,
1812 rx_work_todo(netbk) ||
1813 tx_work_todo(netbk) ||
1814 kthread_should_stop());
1817 if (kthread_should_stop())
1820 if (rx_work_todo(netbk))
1821 xen_netbk_rx_action(netbk);
1823 if (tx_work_todo(netbk))
1824 xen_netbk_tx_action(netbk);
1830 void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
1833 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1836 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1840 int xen_netbk_map_frontend_rings(struct xenvif *vif,
1841 grant_ref_t tx_ring_ref,
1842 grant_ref_t rx_ring_ref)
1845 struct xen_netif_tx_sring *txs;
1846 struct xen_netif_rx_sring *rxs;
1850 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1851 tx_ring_ref, &addr);
1855 txs = (struct xen_netif_tx_sring *)addr;
1856 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1858 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1859 rx_ring_ref, &addr);
1863 rxs = (struct xen_netif_rx_sring *)addr;
1864 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1866 vif->rx_req_cons_peek = 0;
1871 xen_netbk_unmap_frontend_rings(vif);
1875 static int __init netback_init(void)
1884 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1886 "xen-netback: fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1887 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1888 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1891 xen_netbk_group_nr = num_online_cpus();
1892 xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
1896 for (group = 0; group < xen_netbk_group_nr; group++) {
1897 struct xen_netbk *netbk = &xen_netbk[group];
1898 skb_queue_head_init(&netbk->rx_queue);
1899 skb_queue_head_init(&netbk->tx_queue);
1901 init_timer(&netbk->net_timer);
1902 netbk->net_timer.data = (unsigned long)netbk;
1903 netbk->net_timer.function = xen_netbk_alarm;
1905 netbk->pending_cons = 0;
1906 netbk->pending_prod = MAX_PENDING_REQS;
1907 for (i = 0; i < MAX_PENDING_REQS; i++)
1908 netbk->pending_ring[i] = i;
1910 init_waitqueue_head(&netbk->wq);
1911 netbk->task = kthread_create(xen_netbk_kthread,
1913 "netback/%u", group);
1915 if (IS_ERR(netbk->task)) {
1916 printk(KERN_ALERT "kthread_create() fails at netback\n");
1917 del_timer(&netbk->net_timer);
1918 rc = PTR_ERR(netbk->task);
1922 kthread_bind(netbk->task, group);
1924 INIT_LIST_HEAD(&netbk->net_schedule_list);
1926 spin_lock_init(&netbk->net_schedule_list_lock);
1928 atomic_set(&netbk->netfront_count, 0);
1930 wake_up_process(netbk->task);
1933 rc = xenvif_xenbus_init();
1940 while (--group >= 0) {
1941 struct xen_netbk *netbk = &xen_netbk[group];
1942 for (i = 0; i < MAX_PENDING_REQS; i++) {
1943 if (netbk->mmap_pages[i])
1944 __free_page(netbk->mmap_pages[i]);
1946 del_timer(&netbk->net_timer);
1947 kthread_stop(netbk->task);
1954 module_init(netback_init);
1956 MODULE_LICENSE("Dual BSD/GPL");
1957 MODULE_ALIAS("xen-backend:vif");