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 BUG_ON(size > MAX_BUFFER_OFFSET);
351 if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head)
357 struct xenvif_count_slot_state {
358 unsigned long copy_off;
362 unsigned int xenvif_count_frag_slots(struct xenvif *vif,
363 unsigned long offset, unsigned long size,
364 struct xenvif_count_slot_state *state)
368 offset &= ~PAGE_MASK;
373 bytes = PAGE_SIZE - offset;
378 if (start_new_rx_buffer(state->copy_off, bytes, state->head)) {
383 if (state->copy_off + bytes > MAX_BUFFER_OFFSET)
384 bytes = MAX_BUFFER_OFFSET - state->copy_off;
386 state->copy_off += bytes;
391 if (offset == PAGE_SIZE)
401 * Figure out how many ring slots we're going to need to send @skb to
402 * the guest. This function is essentially a dry run of
403 * netbk_gop_frag_copy.
405 unsigned int xen_netbk_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
407 struct xenvif_count_slot_state state;
415 /* Slot for the first (partial) page of data. */
418 /* Need a slot for the GSO prefix for GSO extra data? */
419 if (skb_shinfo(skb)->gso_size)
423 while (data < skb_tail_pointer(skb)) {
424 unsigned long offset = offset_in_page(data);
425 unsigned long size = PAGE_SIZE - offset;
427 if (data + size > skb_tail_pointer(skb))
428 size = skb_tail_pointer(skb) - data;
430 count += xenvif_count_frag_slots(vif, offset, size, &state);
435 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
436 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
437 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
439 count += xenvif_count_frag_slots(vif, offset, size, &state);
444 struct netrx_pending_operations {
445 unsigned copy_prod, copy_cons;
446 unsigned meta_prod, meta_cons;
447 struct gnttab_copy *copy;
448 struct netbk_rx_meta *meta;
450 grant_ref_t copy_gref;
453 static struct netbk_rx_meta *get_next_rx_buffer(struct xenvif *vif,
454 struct netrx_pending_operations *npo)
456 struct netbk_rx_meta *meta;
457 struct xen_netif_rx_request *req;
459 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
461 meta = npo->meta + npo->meta_prod++;
467 npo->copy_gref = req->gref;
473 * Set up the grant operations for this fragment. If it's a flipping
474 * interface, we also set up the unmap request from here.
476 static void netbk_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
477 struct netrx_pending_operations *npo,
478 struct page *page, unsigned long size,
479 unsigned long offset, int *head)
481 struct gnttab_copy *copy_gop;
482 struct netbk_rx_meta *meta;
484 * These variables are used iff get_page_ext returns true,
485 * in which case they are guaranteed to be initialized.
487 unsigned int uninitialized_var(group), uninitialized_var(idx);
488 int foreign = get_page_ext(page, &group, &idx);
491 /* Data must not cross a page boundary. */
492 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
494 meta = npo->meta + npo->meta_prod - 1;
496 /* Skip unused frames from start of page */
497 page += offset >> PAGE_SHIFT;
498 offset &= ~PAGE_MASK;
501 BUG_ON(offset >= PAGE_SIZE);
502 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
504 bytes = PAGE_SIZE - offset;
509 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
511 * Netfront requires there to be some data in the head
516 meta = get_next_rx_buffer(vif, npo);
519 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
520 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
522 copy_gop = npo->copy + npo->copy_prod++;
523 copy_gop->flags = GNTCOPY_dest_gref;
525 struct xen_netbk *netbk = &xen_netbk[group];
526 struct pending_tx_info *src_pend;
528 src_pend = &netbk->pending_tx_info[idx];
530 copy_gop->source.domid = src_pend->vif->domid;
531 copy_gop->source.u.ref = src_pend->req.gref;
532 copy_gop->flags |= GNTCOPY_source_gref;
534 void *vaddr = page_address(page);
535 copy_gop->source.domid = DOMID_SELF;
536 copy_gop->source.u.gmfn = virt_to_mfn(vaddr);
538 copy_gop->source.offset = offset;
539 copy_gop->dest.domid = vif->domid;
541 copy_gop->dest.offset = npo->copy_off;
542 copy_gop->dest.u.ref = npo->copy_gref;
543 copy_gop->len = bytes;
545 npo->copy_off += bytes;
552 if (offset == PAGE_SIZE && size) {
553 BUG_ON(!PageCompound(page));
558 /* Leave a gap for the GSO descriptor. */
559 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
562 *head = 0; /* There must be something in this buffer now. */
568 * Prepare an SKB to be transmitted to the frontend.
570 * This function is responsible for allocating grant operations, meta
573 * It returns the number of meta structures consumed. The number of
574 * ring slots used is always equal to the number of meta slots used
575 * plus the number of GSO descriptors used. Currently, we use either
576 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
577 * frontend-side LRO).
579 static int netbk_gop_skb(struct sk_buff *skb,
580 struct netrx_pending_operations *npo)
582 struct xenvif *vif = netdev_priv(skb->dev);
583 int nr_frags = skb_shinfo(skb)->nr_frags;
585 struct xen_netif_rx_request *req;
586 struct netbk_rx_meta *meta;
591 old_meta_prod = npo->meta_prod;
593 /* Set up a GSO prefix descriptor, if necessary */
594 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
595 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
596 meta = npo->meta + npo->meta_prod++;
597 meta->gso_size = skb_shinfo(skb)->gso_size;
602 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
603 meta = npo->meta + npo->meta_prod++;
605 if (!vif->gso_prefix)
606 meta->gso_size = skb_shinfo(skb)->gso_size;
613 npo->copy_gref = req->gref;
616 while (data < skb_tail_pointer(skb)) {
617 unsigned int offset = offset_in_page(data);
618 unsigned int len = PAGE_SIZE - offset;
620 if (data + len > skb_tail_pointer(skb))
621 len = skb_tail_pointer(skb) - data;
623 netbk_gop_frag_copy(vif, skb, npo,
624 virt_to_page(data), len, offset, &head);
628 for (i = 0; i < nr_frags; i++) {
629 netbk_gop_frag_copy(vif, skb, npo,
630 skb_frag_page(&skb_shinfo(skb)->frags[i]),
631 skb_frag_size(&skb_shinfo(skb)->frags[i]),
632 skb_shinfo(skb)->frags[i].page_offset,
636 return npo->meta_prod - old_meta_prod;
640 * This is a twin to netbk_gop_skb. Assume that netbk_gop_skb was
641 * used to set up the operations on the top of
642 * netrx_pending_operations, which have since been done. Check that
643 * they didn't give any errors and advance over them.
645 static int netbk_check_gop(struct xenvif *vif, int nr_meta_slots,
646 struct netrx_pending_operations *npo)
648 struct gnttab_copy *copy_op;
649 int status = XEN_NETIF_RSP_OKAY;
652 for (i = 0; i < nr_meta_slots; i++) {
653 copy_op = npo->copy + npo->copy_cons++;
654 if (copy_op->status != GNTST_okay) {
656 "Bad status %d from copy to DOM%d.\n",
657 copy_op->status, vif->domid);
658 status = XEN_NETIF_RSP_ERROR;
665 static void netbk_add_frag_responses(struct xenvif *vif, int status,
666 struct netbk_rx_meta *meta,
670 unsigned long offset;
672 /* No fragments used */
673 if (nr_meta_slots <= 1)
678 for (i = 0; i < nr_meta_slots; i++) {
680 if (i == nr_meta_slots - 1)
683 flags = XEN_NETRXF_more_data;
686 make_rx_response(vif, meta[i].id, status, offset,
687 meta[i].size, flags);
691 struct skb_cb_overlay {
695 static void xen_netbk_rx_action(struct xen_netbk *netbk)
697 struct xenvif *vif = NULL, *tmp;
700 struct xen_netif_rx_response *resp;
701 struct sk_buff_head rxq;
707 unsigned long offset;
708 struct skb_cb_overlay *sco;
710 struct netrx_pending_operations npo = {
711 .copy = netbk->grant_copy_op,
715 skb_queue_head_init(&rxq);
719 while ((skb = skb_dequeue(&netbk->rx_queue)) != NULL) {
720 vif = netdev_priv(skb->dev);
721 nr_frags = skb_shinfo(skb)->nr_frags;
723 sco = (struct skb_cb_overlay *)skb->cb;
724 sco->meta_slots_used = netbk_gop_skb(skb, &npo);
726 count += nr_frags + 1;
728 __skb_queue_tail(&rxq, skb);
730 /* Filled the batch queue? */
731 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
732 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
736 BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
741 BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
742 gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
744 while ((skb = __skb_dequeue(&rxq)) != NULL) {
745 sco = (struct skb_cb_overlay *)skb->cb;
747 vif = netdev_priv(skb->dev);
749 if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
750 resp = RING_GET_RESPONSE(&vif->rx,
751 vif->rx.rsp_prod_pvt++);
753 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
755 resp->offset = netbk->meta[npo.meta_cons].gso_size;
756 resp->id = netbk->meta[npo.meta_cons].id;
757 resp->status = sco->meta_slots_used;
760 sco->meta_slots_used--;
764 vif->dev->stats.tx_bytes += skb->len;
765 vif->dev->stats.tx_packets++;
767 status = netbk_check_gop(vif, sco->meta_slots_used, &npo);
769 if (sco->meta_slots_used == 1)
772 flags = XEN_NETRXF_more_data;
774 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
775 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
776 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
777 /* remote but checksummed. */
778 flags |= XEN_NETRXF_data_validated;
781 resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
783 netbk->meta[npo.meta_cons].size,
786 if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
787 struct xen_netif_extra_info *gso =
788 (struct xen_netif_extra_info *)
789 RING_GET_RESPONSE(&vif->rx,
790 vif->rx.rsp_prod_pvt++);
792 resp->flags |= XEN_NETRXF_extra_info;
794 gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
795 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
797 gso->u.gso.features = 0;
799 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
803 netbk_add_frag_responses(vif, status,
804 netbk->meta + npo.meta_cons + 1,
805 sco->meta_slots_used);
807 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
809 xenvif_notify_tx_completion(vif);
811 if (ret && list_empty(&vif->notify_list))
812 list_add_tail(&vif->notify_list, ¬ify);
815 npo.meta_cons += sco->meta_slots_used;
819 list_for_each_entry_safe(vif, tmp, ¬ify, notify_list) {
820 notify_remote_via_irq(vif->irq);
821 list_del_init(&vif->notify_list);
825 /* More work to do? */
826 if (!skb_queue_empty(&netbk->rx_queue) &&
827 !timer_pending(&netbk->net_timer))
828 xen_netbk_kick_thread(netbk);
831 void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
833 struct xen_netbk *netbk = vif->netbk;
835 skb_queue_tail(&netbk->rx_queue, skb);
837 xen_netbk_kick_thread(netbk);
840 static void xen_netbk_alarm(unsigned long data)
842 struct xen_netbk *netbk = (struct xen_netbk *)data;
843 xen_netbk_kick_thread(netbk);
846 static int __on_net_schedule_list(struct xenvif *vif)
848 return !list_empty(&vif->schedule_list);
851 /* Must be called with net_schedule_list_lock held */
852 static void remove_from_net_schedule_list(struct xenvif *vif)
854 if (likely(__on_net_schedule_list(vif))) {
855 list_del_init(&vif->schedule_list);
860 static struct xenvif *poll_net_schedule_list(struct xen_netbk *netbk)
862 struct xenvif *vif = NULL;
864 spin_lock_irq(&netbk->net_schedule_list_lock);
865 if (list_empty(&netbk->net_schedule_list))
868 vif = list_first_entry(&netbk->net_schedule_list,
869 struct xenvif, schedule_list);
875 remove_from_net_schedule_list(vif);
877 spin_unlock_irq(&netbk->net_schedule_list_lock);
881 void xen_netbk_schedule_xenvif(struct xenvif *vif)
884 struct xen_netbk *netbk = vif->netbk;
886 if (__on_net_schedule_list(vif))
889 spin_lock_irqsave(&netbk->net_schedule_list_lock, flags);
890 if (!__on_net_schedule_list(vif) &&
891 likely(xenvif_schedulable(vif))) {
892 list_add_tail(&vif->schedule_list, &netbk->net_schedule_list);
895 spin_unlock_irqrestore(&netbk->net_schedule_list_lock, flags);
899 if ((nr_pending_reqs(netbk) < (MAX_PENDING_REQS/2)) &&
900 !list_empty(&netbk->net_schedule_list))
901 xen_netbk_kick_thread(netbk);
904 void xen_netbk_deschedule_xenvif(struct xenvif *vif)
906 struct xen_netbk *netbk = vif->netbk;
907 spin_lock_irq(&netbk->net_schedule_list_lock);
908 remove_from_net_schedule_list(vif);
909 spin_unlock_irq(&netbk->net_schedule_list_lock);
912 void xen_netbk_check_rx_xenvif(struct xenvif *vif)
916 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
919 xen_netbk_schedule_xenvif(vif);
922 static void tx_add_credit(struct xenvif *vif)
924 unsigned long max_burst, max_credit;
927 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
928 * Otherwise the interface can seize up due to insufficient credit.
930 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
931 max_burst = min(max_burst, 131072UL);
932 max_burst = max(max_burst, vif->credit_bytes);
934 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
935 max_credit = vif->remaining_credit + vif->credit_bytes;
936 if (max_credit < vif->remaining_credit)
937 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
939 vif->remaining_credit = min(max_credit, max_burst);
942 static void tx_credit_callback(unsigned long data)
944 struct xenvif *vif = (struct xenvif *)data;
946 xen_netbk_check_rx_xenvif(vif);
949 static void netbk_tx_err(struct xenvif *vif,
950 struct xen_netif_tx_request *txp, RING_IDX end)
952 RING_IDX cons = vif->tx.req_cons;
955 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
958 txp = RING_GET_REQUEST(&vif->tx, cons++);
960 vif->tx.req_cons = cons;
961 xen_netbk_check_rx_xenvif(vif);
965 static void netbk_fatal_tx_err(struct xenvif *vif)
967 netdev_err(vif->dev, "fatal error; disabling device\n");
968 xenvif_carrier_off(vif);
972 static int netbk_count_requests(struct xenvif *vif,
973 struct xen_netif_tx_request *first,
974 struct xen_netif_tx_request *txp,
977 RING_IDX cons = vif->tx.req_cons;
982 if (!(first->flags & XEN_NETTXF_more_data))
986 struct xen_netif_tx_request dropped_tx = { 0 };
988 if (slots >= work_to_do) {
990 "Asked for %d slots but exceeds this limit\n",
992 netbk_fatal_tx_err(vif);
996 /* This guest is really using too many slots and
997 * considered malicious.
999 if (unlikely(slots >= fatal_skb_slots)) {
1000 netdev_err(vif->dev,
1001 "Malicious frontend using %d slots, threshold %u\n",
1002 slots, fatal_skb_slots);
1003 netbk_fatal_tx_err(vif);
1007 /* Xen network protocol had implicit dependency on
1008 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
1009 * the historical MAX_SKB_FRAGS value 18 to honor the
1010 * same behavior as before. Any packet using more than
1011 * 18 slots but less than fatal_skb_slots slots is
1014 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
1015 if (net_ratelimit())
1016 netdev_dbg(vif->dev,
1017 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
1018 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1025 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
1028 /* If the guest submitted a frame >= 64 KiB then
1029 * first->size overflowed and following slots will
1030 * appear to be larger than the frame.
1032 * This cannot be fatal error as there are buggy
1033 * frontends that do this.
1035 * Consume all slots and drop the packet.
1037 if (!drop_err && txp->size > first->size) {
1038 if (net_ratelimit())
1039 netdev_dbg(vif->dev,
1040 "Invalid tx request, slot size %u > remaining size %u\n",
1041 txp->size, first->size);
1045 first->size -= txp->size;
1048 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
1049 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
1050 txp->offset, txp->size);
1051 netbk_fatal_tx_err(vif);
1055 more_data = txp->flags & XEN_NETTXF_more_data;
1060 } while (more_data);
1063 netbk_tx_err(vif, first, cons + slots);
1070 static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
1074 page = alloc_page(GFP_KERNEL|__GFP_COLD);
1077 set_page_ext(page, netbk, pending_idx);
1078 netbk->mmap_pages[pending_idx] = page;
1082 static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
1084 struct sk_buff *skb,
1085 struct xen_netif_tx_request *txp,
1086 struct gnttab_copy *gop)
1088 struct skb_shared_info *shinfo = skb_shinfo(skb);
1089 skb_frag_t *frags = shinfo->frags;
1090 u16 pending_idx = *((u16 *)skb->data);
1094 pending_ring_idx_t index, start_idx = 0;
1095 uint16_t dst_offset;
1096 unsigned int nr_slots;
1097 struct pending_tx_info *first = NULL;
1099 /* At this point shinfo->nr_frags is in fact the number of
1100 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
1102 nr_slots = shinfo->nr_frags;
1104 /* Skip first skb fragment if it is on same page as header fragment. */
1105 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
1107 /* Coalesce tx requests, at this point the packet passed in
1108 * should be <= 64K. Any packets larger than 64K have been
1109 * handled in netbk_count_requests().
1111 for (shinfo->nr_frags = slot = start; slot < nr_slots;
1112 shinfo->nr_frags++) {
1113 struct pending_tx_info *pending_tx_info =
1114 netbk->pending_tx_info;
1116 page = alloc_page(GFP_KERNEL|__GFP_COLD);
1122 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
1123 gop->flags = GNTCOPY_source_gref;
1125 gop->source.u.ref = txp->gref;
1126 gop->source.domid = vif->domid;
1127 gop->source.offset = txp->offset;
1129 gop->dest.domid = DOMID_SELF;
1131 gop->dest.offset = dst_offset;
1132 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1134 if (dst_offset + txp->size > PAGE_SIZE) {
1135 /* This page can only merge a portion
1136 * of tx request. Do not increment any
1137 * pointer / counter here. The txp
1138 * will be dealt with in future
1139 * rounds, eventually hitting the
1142 gop->len = PAGE_SIZE - dst_offset;
1143 txp->offset += gop->len;
1144 txp->size -= gop->len;
1145 dst_offset += gop->len; /* quit loop */
1147 /* This tx request can be merged in the page */
1148 gop->len = txp->size;
1149 dst_offset += gop->len;
1151 index = pending_index(netbk->pending_cons++);
1153 pending_idx = netbk->pending_ring[index];
1155 memcpy(&pending_tx_info[pending_idx].req, txp,
1159 pending_tx_info[pending_idx].vif = vif;
1161 /* Poison these fields, corresponding
1162 * fields for head tx req will be set
1163 * to correct values after the loop.
1165 netbk->mmap_pages[pending_idx] = (void *)(~0UL);
1166 pending_tx_info[pending_idx].head =
1167 INVALID_PENDING_RING_IDX;
1170 first = &pending_tx_info[pending_idx];
1172 head_idx = pending_idx;
1182 first->req.offset = 0;
1183 first->req.size = dst_offset;
1184 first->head = start_idx;
1185 set_page_ext(page, netbk, head_idx);
1186 netbk->mmap_pages[head_idx] = page;
1187 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
1190 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
1194 /* Unwind, freeing all pages and sending error responses. */
1195 while (shinfo->nr_frags-- > start) {
1196 xen_netbk_idx_release(netbk,
1197 frag_get_pending_idx(&frags[shinfo->nr_frags]),
1198 XEN_NETIF_RSP_ERROR);
1200 /* The head too, if necessary. */
1202 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1207 static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
1208 struct sk_buff *skb,
1209 struct gnttab_copy **gopp)
1211 struct gnttab_copy *gop = *gopp;
1212 u16 pending_idx = *((u16 *)skb->data);
1213 struct skb_shared_info *shinfo = skb_shinfo(skb);
1214 struct pending_tx_info *tx_info;
1215 int nr_frags = shinfo->nr_frags;
1217 u16 peek; /* peek into next tx request */
1219 /* Check status of header. */
1222 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1224 /* Skip first skb fragment if it is on same page as header fragment. */
1225 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
1227 for (i = start; i < nr_frags; i++) {
1229 pending_ring_idx_t head;
1231 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
1232 tx_info = &netbk->pending_tx_info[pending_idx];
1233 head = tx_info->head;
1235 /* Check error status: if okay then remember grant handle. */
1237 newerr = (++gop)->status;
1240 peek = netbk->pending_ring[pending_index(++head)];
1241 } while (!pending_tx_is_head(netbk, peek));
1243 if (likely(!newerr)) {
1244 /* Had a previous error? Invalidate this fragment. */
1246 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1250 /* Error on this fragment: respond to client with an error. */
1251 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1253 /* Not the first error? Preceding frags already invalidated. */
1257 /* First error: invalidate header and preceding fragments. */
1258 pending_idx = *((u16 *)skb->data);
1259 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1260 for (j = start; j < i; j++) {
1261 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1262 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1265 /* Remember the error: invalidate all subsequent fragments. */
1273 static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
1275 struct skb_shared_info *shinfo = skb_shinfo(skb);
1276 int nr_frags = shinfo->nr_frags;
1279 for (i = 0; i < nr_frags; i++) {
1280 skb_frag_t *frag = shinfo->frags + i;
1281 struct xen_netif_tx_request *txp;
1285 pending_idx = frag_get_pending_idx(frag);
1287 txp = &netbk->pending_tx_info[pending_idx].req;
1288 page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
1289 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1290 skb->len += txp->size;
1291 skb->data_len += txp->size;
1292 skb->truesize += txp->size;
1294 /* Take an extra reference to offset xen_netbk_idx_release */
1295 get_page(netbk->mmap_pages[pending_idx]);
1296 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1300 static int xen_netbk_get_extras(struct xenvif *vif,
1301 struct xen_netif_extra_info *extras,
1304 struct xen_netif_extra_info extra;
1305 RING_IDX cons = vif->tx.req_cons;
1308 if (unlikely(work_to_do-- <= 0)) {
1309 netdev_err(vif->dev, "Missing extra info\n");
1310 netbk_fatal_tx_err(vif);
1314 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1316 if (unlikely(!extra.type ||
1317 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1318 vif->tx.req_cons = ++cons;
1319 netdev_err(vif->dev,
1320 "Invalid extra type: %d\n", extra.type);
1321 netbk_fatal_tx_err(vif);
1325 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1326 vif->tx.req_cons = ++cons;
1327 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1332 static int netbk_set_skb_gso(struct xenvif *vif,
1333 struct sk_buff *skb,
1334 struct xen_netif_extra_info *gso)
1336 if (!gso->u.gso.size) {
1337 netdev_err(vif->dev, "GSO size must not be zero.\n");
1338 netbk_fatal_tx_err(vif);
1342 /* Currently only TCPv4 S.O. is supported. */
1343 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1344 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1345 netbk_fatal_tx_err(vif);
1349 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1350 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1352 /* Header must be checked, and gso_segs computed. */
1353 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1354 skb_shinfo(skb)->gso_segs = 0;
1359 static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1363 int recalculate_partial_csum = 0;
1366 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1367 * peers can fail to set NETRXF_csum_blank when sending a GSO
1368 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1369 * recalculate the partial checksum.
1371 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1372 vif->rx_gso_checksum_fixup++;
1373 skb->ip_summed = CHECKSUM_PARTIAL;
1374 recalculate_partial_csum = 1;
1377 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1378 if (skb->ip_summed != CHECKSUM_PARTIAL)
1381 if (skb->protocol != htons(ETH_P_IP))
1384 iph = (void *)skb->data;
1385 switch (iph->protocol) {
1387 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1388 offsetof(struct tcphdr, check)))
1391 if (recalculate_partial_csum) {
1392 struct tcphdr *tcph = tcp_hdr(skb);
1393 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1394 skb->len - iph->ihl*4,
1399 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1400 offsetof(struct udphdr, check)))
1403 if (recalculate_partial_csum) {
1404 struct udphdr *udph = udp_hdr(skb);
1405 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1406 skb->len - iph->ihl*4,
1411 if (net_ratelimit())
1412 netdev_err(vif->dev,
1413 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1424 static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1426 u64 now = get_jiffies_64();
1427 u64 next_credit = vif->credit_window_start +
1428 msecs_to_jiffies(vif->credit_usec / 1000);
1430 /* Timer could already be pending in rare cases. */
1431 if (timer_pending(&vif->credit_timeout))
1434 /* Passed the point where we can replenish credit? */
1435 if (time_after_eq64(now, next_credit)) {
1436 vif->credit_window_start = now;
1440 /* Still too big to send right now? Set a callback. */
1441 if (size > vif->remaining_credit) {
1442 vif->credit_timeout.data =
1444 vif->credit_timeout.function =
1446 mod_timer(&vif->credit_timeout,
1448 vif->credit_window_start = next_credit;
1456 static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
1458 struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
1459 struct sk_buff *skb;
1462 while ((nr_pending_reqs(netbk) + XEN_NETBK_LEGACY_SLOTS_MAX
1463 < MAX_PENDING_REQS) &&
1464 !list_empty(&netbk->net_schedule_list)) {
1466 struct xen_netif_tx_request txreq;
1467 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1469 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1473 unsigned int data_len;
1474 pending_ring_idx_t index;
1476 /* Get a netif from the list with work to do. */
1477 vif = poll_net_schedule_list(netbk);
1478 /* This can sometimes happen because the test of
1479 * list_empty(net_schedule_list) at the top of the
1480 * loop is unlocked. Just go back and have another
1486 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1487 XEN_NETIF_TX_RING_SIZE) {
1488 netdev_err(vif->dev,
1489 "Impossible number of requests. "
1490 "req_prod %d, req_cons %d, size %ld\n",
1491 vif->tx.sring->req_prod, vif->tx.req_cons,
1492 XEN_NETIF_TX_RING_SIZE);
1493 netbk_fatal_tx_err(vif);
1497 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1503 idx = vif->tx.req_cons;
1504 rmb(); /* Ensure that we see the request before we copy it. */
1505 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1507 /* Credit-based scheduling. */
1508 if (txreq.size > vif->remaining_credit &&
1509 tx_credit_exceeded(vif, txreq.size)) {
1514 vif->remaining_credit -= txreq.size;
1517 vif->tx.req_cons = ++idx;
1519 memset(extras, 0, sizeof(extras));
1520 if (txreq.flags & XEN_NETTXF_extra_info) {
1521 work_to_do = xen_netbk_get_extras(vif, extras,
1523 idx = vif->tx.req_cons;
1524 if (unlikely(work_to_do < 0))
1528 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
1529 if (unlikely(ret < 0))
1534 if (unlikely(txreq.size < ETH_HLEN)) {
1535 netdev_dbg(vif->dev,
1536 "Bad packet size: %d\n", txreq.size);
1537 netbk_tx_err(vif, &txreq, idx);
1541 /* No crossing a page as the payload mustn't fragment. */
1542 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1543 netdev_err(vif->dev,
1544 "txreq.offset: %x, size: %u, end: %lu\n",
1545 txreq.offset, txreq.size,
1546 (txreq.offset&~PAGE_MASK) + txreq.size);
1547 netbk_fatal_tx_err(vif);
1551 index = pending_index(netbk->pending_cons);
1552 pending_idx = netbk->pending_ring[index];
1554 data_len = (txreq.size > PKT_PROT_LEN &&
1555 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1556 PKT_PROT_LEN : txreq.size;
1558 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1559 GFP_ATOMIC | __GFP_NOWARN);
1560 if (unlikely(skb == NULL)) {
1561 netdev_dbg(vif->dev,
1562 "Can't allocate a skb in start_xmit.\n");
1563 netbk_tx_err(vif, &txreq, idx);
1567 /* Packets passed to netif_rx() must have some headroom. */
1568 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1570 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1571 struct xen_netif_extra_info *gso;
1572 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1574 if (netbk_set_skb_gso(vif, skb, gso)) {
1575 /* Failure in netbk_set_skb_gso is fatal. */
1581 /* XXX could copy straight to head */
1582 page = xen_netbk_alloc_page(netbk, pending_idx);
1585 netbk_tx_err(vif, &txreq, idx);
1589 gop->source.u.ref = txreq.gref;
1590 gop->source.domid = vif->domid;
1591 gop->source.offset = txreq.offset;
1593 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1594 gop->dest.domid = DOMID_SELF;
1595 gop->dest.offset = txreq.offset;
1597 gop->len = txreq.size;
1598 gop->flags = GNTCOPY_source_gref;
1602 memcpy(&netbk->pending_tx_info[pending_idx].req,
1603 &txreq, sizeof(txreq));
1604 netbk->pending_tx_info[pending_idx].vif = vif;
1605 netbk->pending_tx_info[pending_idx].head = index;
1606 *((u16 *)skb->data) = pending_idx;
1608 __skb_put(skb, data_len);
1610 skb_shinfo(skb)->nr_frags = ret;
1611 if (data_len < txreq.size) {
1612 skb_shinfo(skb)->nr_frags++;
1613 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1616 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1617 INVALID_PENDING_IDX);
1620 netbk->pending_cons++;
1622 request_gop = xen_netbk_get_requests(netbk, vif,
1624 if (request_gop == NULL) {
1626 netbk_tx_err(vif, &txreq, idx);
1631 __skb_queue_tail(&netbk->tx_queue, skb);
1633 vif->tx.req_cons = idx;
1634 xen_netbk_check_rx_xenvif(vif);
1636 if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
1640 return gop - netbk->tx_copy_ops;
1643 static void xen_netbk_tx_submit(struct xen_netbk *netbk)
1645 struct gnttab_copy *gop = netbk->tx_copy_ops;
1646 struct sk_buff *skb;
1648 while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
1649 struct xen_netif_tx_request *txp;
1654 pending_idx = *((u16 *)skb->data);
1655 vif = netbk->pending_tx_info[pending_idx].vif;
1656 txp = &netbk->pending_tx_info[pending_idx].req;
1658 /* Check the remap error code. */
1659 if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1660 netdev_dbg(vif->dev, "netback grant failed.\n");
1661 skb_shinfo(skb)->nr_frags = 0;
1666 data_len = skb->len;
1668 (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
1670 if (data_len < txp->size) {
1671 /* Append the packet payload as a fragment. */
1672 txp->offset += data_len;
1673 txp->size -= data_len;
1675 /* Schedule a response immediately. */
1676 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1679 if (txp->flags & XEN_NETTXF_csum_blank)
1680 skb->ip_summed = CHECKSUM_PARTIAL;
1681 else if (txp->flags & XEN_NETTXF_data_validated)
1682 skb->ip_summed = CHECKSUM_UNNECESSARY;
1684 xen_netbk_fill_frags(netbk, skb);
1687 * If the initial fragment was < PKT_PROT_LEN then
1688 * pull through some bytes from the other fragments to
1689 * increase the linear region to PKT_PROT_LEN bytes.
1691 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1692 int target = min_t(int, skb->len, PKT_PROT_LEN);
1693 __pskb_pull_tail(skb, target - skb_headlen(skb));
1696 skb->dev = vif->dev;
1697 skb->protocol = eth_type_trans(skb, skb->dev);
1698 skb_reset_network_header(skb);
1700 if (checksum_setup(vif, skb)) {
1701 netdev_dbg(vif->dev,
1702 "Can't setup checksum in net_tx_action\n");
1707 skb_probe_transport_header(skb, 0);
1709 vif->dev->stats.rx_bytes += skb->len;
1710 vif->dev->stats.rx_packets++;
1712 xenvif_receive_skb(vif, skb);
1716 /* Called after netfront has transmitted */
1717 static void xen_netbk_tx_action(struct xen_netbk *netbk)
1721 nr_gops = xen_netbk_tx_build_gops(netbk);
1726 gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
1728 xen_netbk_tx_submit(netbk);
1731 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
1735 struct pending_tx_info *pending_tx_info;
1736 pending_ring_idx_t head;
1737 u16 peek; /* peek into next tx request */
1739 BUG_ON(netbk->mmap_pages[pending_idx] == (void *)(~0UL));
1741 /* Already complete? */
1742 if (netbk->mmap_pages[pending_idx] == NULL)
1745 pending_tx_info = &netbk->pending_tx_info[pending_idx];
1747 vif = pending_tx_info->vif;
1748 head = pending_tx_info->head;
1750 BUG_ON(!pending_tx_is_head(netbk, head));
1751 BUG_ON(netbk->pending_ring[pending_index(head)] != pending_idx);
1754 pending_ring_idx_t index;
1755 pending_ring_idx_t idx = pending_index(head);
1756 u16 info_idx = netbk->pending_ring[idx];
1758 pending_tx_info = &netbk->pending_tx_info[info_idx];
1759 make_tx_response(vif, &pending_tx_info->req, status);
1761 /* Setting any number other than
1762 * INVALID_PENDING_RING_IDX indicates this slot is
1763 * starting a new packet / ending a previous packet.
1765 pending_tx_info->head = 0;
1767 index = pending_index(netbk->pending_prod++);
1768 netbk->pending_ring[index] = netbk->pending_ring[info_idx];
1772 peek = netbk->pending_ring[pending_index(++head)];
1774 } while (!pending_tx_is_head(netbk, peek));
1776 netbk->mmap_pages[pending_idx]->mapping = 0;
1777 put_page(netbk->mmap_pages[pending_idx]);
1778 netbk->mmap_pages[pending_idx] = NULL;
1782 static void make_tx_response(struct xenvif *vif,
1783 struct xen_netif_tx_request *txp,
1786 RING_IDX i = vif->tx.rsp_prod_pvt;
1787 struct xen_netif_tx_response *resp;
1790 resp = RING_GET_RESPONSE(&vif->tx, i);
1794 if (txp->flags & XEN_NETTXF_extra_info)
1795 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1797 vif->tx.rsp_prod_pvt = ++i;
1798 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1800 notify_remote_via_irq(vif->irq);
1803 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1810 RING_IDX i = vif->rx.rsp_prod_pvt;
1811 struct xen_netif_rx_response *resp;
1813 resp = RING_GET_RESPONSE(&vif->rx, i);
1814 resp->offset = offset;
1815 resp->flags = flags;
1817 resp->status = (s16)size;
1819 resp->status = (s16)st;
1821 vif->rx.rsp_prod_pvt = ++i;
1826 static inline int rx_work_todo(struct xen_netbk *netbk)
1828 return !skb_queue_empty(&netbk->rx_queue);
1831 static inline int tx_work_todo(struct xen_netbk *netbk)
1834 if ((nr_pending_reqs(netbk) + XEN_NETBK_LEGACY_SLOTS_MAX
1835 < MAX_PENDING_REQS) &&
1836 !list_empty(&netbk->net_schedule_list))
1842 static int xen_netbk_kthread(void *data)
1844 struct xen_netbk *netbk = data;
1845 while (!kthread_should_stop()) {
1846 wait_event_interruptible(netbk->wq,
1847 rx_work_todo(netbk) ||
1848 tx_work_todo(netbk) ||
1849 kthread_should_stop());
1852 if (kthread_should_stop())
1855 if (rx_work_todo(netbk))
1856 xen_netbk_rx_action(netbk);
1858 if (tx_work_todo(netbk))
1859 xen_netbk_tx_action(netbk);
1865 void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
1868 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1871 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1875 int xen_netbk_map_frontend_rings(struct xenvif *vif,
1876 grant_ref_t tx_ring_ref,
1877 grant_ref_t rx_ring_ref)
1880 struct xen_netif_tx_sring *txs;
1881 struct xen_netif_rx_sring *rxs;
1885 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1886 tx_ring_ref, &addr);
1890 txs = (struct xen_netif_tx_sring *)addr;
1891 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1893 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1894 rx_ring_ref, &addr);
1898 rxs = (struct xen_netif_rx_sring *)addr;
1899 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1901 vif->rx_req_cons_peek = 0;
1906 xen_netbk_unmap_frontend_rings(vif);
1910 static int __init netback_init(void)
1919 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1921 "xen-netback: fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1922 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1923 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1926 xen_netbk_group_nr = num_online_cpus();
1927 xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
1931 for (group = 0; group < xen_netbk_group_nr; group++) {
1932 struct xen_netbk *netbk = &xen_netbk[group];
1933 skb_queue_head_init(&netbk->rx_queue);
1934 skb_queue_head_init(&netbk->tx_queue);
1936 init_timer(&netbk->net_timer);
1937 netbk->net_timer.data = (unsigned long)netbk;
1938 netbk->net_timer.function = xen_netbk_alarm;
1940 netbk->pending_cons = 0;
1941 netbk->pending_prod = MAX_PENDING_REQS;
1942 for (i = 0; i < MAX_PENDING_REQS; i++)
1943 netbk->pending_ring[i] = i;
1945 init_waitqueue_head(&netbk->wq);
1946 netbk->task = kthread_create(xen_netbk_kthread,
1948 "netback/%u", group);
1950 if (IS_ERR(netbk->task)) {
1951 printk(KERN_ALERT "kthread_create() fails at netback\n");
1952 del_timer(&netbk->net_timer);
1953 rc = PTR_ERR(netbk->task);
1957 kthread_bind(netbk->task, group);
1959 INIT_LIST_HEAD(&netbk->net_schedule_list);
1961 spin_lock_init(&netbk->net_schedule_list_lock);
1963 atomic_set(&netbk->netfront_count, 0);
1965 wake_up_process(netbk->task);
1968 rc = xenvif_xenbus_init();
1975 while (--group >= 0) {
1976 struct xen_netbk *netbk = &xen_netbk[group];
1977 for (i = 0; i < MAX_PENDING_REQS; i++) {
1978 if (netbk->mmap_pages[i])
1979 __free_page(netbk->mmap_pages[i]);
1981 del_timer(&netbk->net_timer);
1982 kthread_stop(netbk->task);
1989 module_init(netback_init);
1991 MODULE_LICENSE("Dual BSD/GPL");
1992 MODULE_ALIAS("xen-backend:vif");