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>
42 #include <net/ip6_checksum.h>
45 #include <xen/events.h>
46 #include <xen/interface/memory.h>
48 #include <asm/xen/hypercall.h>
49 #include <asm/xen/page.h>
51 /* Provide an option to disable split event channels at load time as
52 * event channels are limited resource. Split event channels are
55 bool separate_tx_rx_irq = 1;
56 module_param(separate_tx_rx_irq, bool, 0644);
59 * This is the maximum slots a skb can have. If a guest sends a skb
60 * which exceeds this limit it is considered malicious.
62 #define FATAL_SKB_SLOTS_DEFAULT 20
63 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
64 module_param(fatal_skb_slots, uint, 0444);
67 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
68 * the maximum slots a valid packet can use. Now this value is defined
69 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
72 #define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
75 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
76 * one or more merged tx requests, otherwise it is the continuation of
77 * previous tx request.
79 static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
81 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
84 static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
87 static void make_tx_response(struct xenvif *vif,
88 struct xen_netif_tx_request *txp,
91 static inline int tx_work_todo(struct xenvif *vif);
92 static inline int rx_work_todo(struct xenvif *vif);
94 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
101 static inline unsigned long idx_to_pfn(struct xenvif *vif,
104 return page_to_pfn(vif->mmap_pages[idx]);
107 static inline unsigned long idx_to_kaddr(struct xenvif *vif,
110 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
113 /* This is a miniumum size for the linear area to avoid lots of
114 * calls to __pskb_pull_tail() as we set up checksum offsets. The
115 * value 128 was chosen as it covers all IPv4 and most likely
118 #define PKT_PROT_LEN 128
120 static u16 frag_get_pending_idx(skb_frag_t *frag)
122 return (u16)frag->page_offset;
125 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
127 frag->page_offset = pending_idx;
130 static inline pending_ring_idx_t pending_index(unsigned i)
132 return i & (MAX_PENDING_REQS-1);
135 static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
137 return MAX_PENDING_REQS -
138 vif->pending_prod + vif->pending_cons;
141 bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
146 prod = vif->rx.sring->req_prod;
147 cons = vif->rx.req_cons;
149 if (prod - cons >= needed)
152 vif->rx.sring->req_event = prod + 1;
154 /* Make sure event is visible before we check prod
158 } while (vif->rx.sring->req_prod != prod);
164 * Returns true if we should start a new receive buffer instead of
165 * adding 'size' bytes to a buffer which currently contains 'offset'
168 static bool start_new_rx_buffer(int offset, unsigned long size, int head)
170 /* simple case: we have completely filled the current buffer. */
171 if (offset == MAX_BUFFER_OFFSET)
175 * complex case: start a fresh buffer if the current frag
176 * would overflow the current buffer but only if:
177 * (i) this frag would fit completely in the next buffer
178 * and (ii) there is already some data in the current buffer
179 * and (iii) this is not the head buffer.
182 * - (i) stops us splitting a frag into two copies
183 * unless the frag is too large for a single buffer.
184 * - (ii) stops us from leaving a buffer pointlessly empty.
185 * - (iii) stops us leaving the first buffer
186 * empty. Strictly speaking this is already covered
187 * by (ii) but is explicitly checked because
188 * netfront relies on the first buffer being
189 * non-empty and can crash otherwise.
191 * This means we will effectively linearise small
192 * frags but do not needlessly split large buffers
193 * into multiple copies tend to give large frags their
194 * own buffers as before.
196 if ((offset + size > MAX_BUFFER_OFFSET) &&
197 (size <= MAX_BUFFER_OFFSET) && offset && !head)
203 struct netrx_pending_operations {
204 unsigned copy_prod, copy_cons;
205 unsigned meta_prod, meta_cons;
206 struct gnttab_copy *copy;
207 struct xenvif_rx_meta *meta;
209 grant_ref_t copy_gref;
212 static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
213 struct netrx_pending_operations *npo)
215 struct xenvif_rx_meta *meta;
216 struct xen_netif_rx_request *req;
218 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
220 meta = npo->meta + npo->meta_prod++;
221 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
227 npo->copy_gref = req->gref;
233 * Set up the grant operations for this fragment. If it's a flipping
234 * interface, we also set up the unmap request from here.
236 static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
237 struct netrx_pending_operations *npo,
238 struct page *page, unsigned long size,
239 unsigned long offset, int *head)
241 struct gnttab_copy *copy_gop;
242 struct xenvif_rx_meta *meta;
246 /* Data must not cross a page boundary. */
247 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
249 meta = npo->meta + npo->meta_prod - 1;
251 /* Skip unused frames from start of page */
252 page += offset >> PAGE_SHIFT;
253 offset &= ~PAGE_MASK;
256 BUG_ON(offset >= PAGE_SIZE);
257 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
259 bytes = PAGE_SIZE - offset;
264 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
266 * Netfront requires there to be some data in the head
271 meta = get_next_rx_buffer(vif, npo);
274 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
275 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
277 copy_gop = npo->copy + npo->copy_prod++;
278 copy_gop->flags = GNTCOPY_dest_gref;
279 copy_gop->len = bytes;
281 copy_gop->source.domid = DOMID_SELF;
282 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
283 copy_gop->source.offset = offset;
285 copy_gop->dest.domid = vif->domid;
286 copy_gop->dest.offset = npo->copy_off;
287 copy_gop->dest.u.ref = npo->copy_gref;
289 npo->copy_off += bytes;
296 if (offset == PAGE_SIZE && size) {
297 BUG_ON(!PageCompound(page));
302 /* Leave a gap for the GSO descriptor. */
303 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
304 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
305 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
306 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
308 gso_type = XEN_NETIF_GSO_TYPE_NONE;
310 if (*head && ((1 << gso_type) & vif->gso_mask))
313 *head = 0; /* There must be something in this buffer now. */
319 * Prepare an SKB to be transmitted to the frontend.
321 * This function is responsible for allocating grant operations, meta
324 * It returns the number of meta structures consumed. The number of
325 * ring slots used is always equal to the number of meta slots used
326 * plus the number of GSO descriptors used. Currently, we use either
327 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
328 * frontend-side LRO).
330 static int xenvif_gop_skb(struct sk_buff *skb,
331 struct netrx_pending_operations *npo)
333 struct xenvif *vif = netdev_priv(skb->dev);
334 int nr_frags = skb_shinfo(skb)->nr_frags;
336 struct xen_netif_rx_request *req;
337 struct xenvif_rx_meta *meta;
344 old_meta_prod = npo->meta_prod;
346 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
347 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
348 gso_size = skb_shinfo(skb)->gso_size;
349 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
350 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
351 gso_size = skb_shinfo(skb)->gso_size;
353 gso_type = XEN_NETIF_GSO_TYPE_NONE;
357 /* Set up a GSO prefix descriptor, if necessary */
358 if ((1 << gso_type) & vif->gso_prefix_mask) {
359 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
360 meta = npo->meta + npo->meta_prod++;
361 meta->gso_type = gso_type;
362 meta->gso_size = gso_size;
367 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
368 meta = npo->meta + npo->meta_prod++;
370 if ((1 << gso_type) & vif->gso_mask) {
371 meta->gso_type = gso_type;
372 meta->gso_size = gso_size;
374 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
381 npo->copy_gref = req->gref;
384 while (data < skb_tail_pointer(skb)) {
385 unsigned int offset = offset_in_page(data);
386 unsigned int len = PAGE_SIZE - offset;
388 if (data + len > skb_tail_pointer(skb))
389 len = skb_tail_pointer(skb) - data;
391 xenvif_gop_frag_copy(vif, skb, npo,
392 virt_to_page(data), len, offset, &head);
396 for (i = 0; i < nr_frags; i++) {
397 xenvif_gop_frag_copy(vif, skb, npo,
398 skb_frag_page(&skb_shinfo(skb)->frags[i]),
399 skb_frag_size(&skb_shinfo(skb)->frags[i]),
400 skb_shinfo(skb)->frags[i].page_offset,
404 return npo->meta_prod - old_meta_prod;
408 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
409 * used to set up the operations on the top of
410 * netrx_pending_operations, which have since been done. Check that
411 * they didn't give any errors and advance over them.
413 static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
414 struct netrx_pending_operations *npo)
416 struct gnttab_copy *copy_op;
417 int status = XEN_NETIF_RSP_OKAY;
420 for (i = 0; i < nr_meta_slots; i++) {
421 copy_op = npo->copy + npo->copy_cons++;
422 if (copy_op->status != GNTST_okay) {
424 "Bad status %d from copy to DOM%d.\n",
425 copy_op->status, vif->domid);
426 status = XEN_NETIF_RSP_ERROR;
433 static void xenvif_add_frag_responses(struct xenvif *vif, int status,
434 struct xenvif_rx_meta *meta,
438 unsigned long offset;
440 /* No fragments used */
441 if (nr_meta_slots <= 1)
446 for (i = 0; i < nr_meta_slots; i++) {
448 if (i == nr_meta_slots - 1)
451 flags = XEN_NETRXF_more_data;
454 make_rx_response(vif, meta[i].id, status, offset,
455 meta[i].size, flags);
459 struct skb_cb_overlay {
463 void xenvif_kick_thread(struct xenvif *vif)
468 static void xenvif_rx_action(struct xenvif *vif)
472 struct xen_netif_rx_response *resp;
473 struct sk_buff_head rxq;
477 unsigned long offset;
478 struct skb_cb_overlay *sco;
479 bool need_to_notify = false;
480 bool ring_full = false;
482 struct netrx_pending_operations npo = {
483 .copy = vif->grant_copy_op,
487 skb_queue_head_init(&rxq);
489 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
490 int max_slots_needed;
493 /* We need a cheap worse case estimate for the number of
497 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
500 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
502 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
503 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
505 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
506 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
509 /* If the skb may not fit then bail out now */
510 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
511 skb_queue_head(&vif->rx_queue, skb);
512 need_to_notify = true;
517 sco = (struct skb_cb_overlay *)skb->cb;
518 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
519 BUG_ON(sco->meta_slots_used > max_slots_needed);
521 __skb_queue_tail(&rxq, skb);
524 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
526 vif->rx_queue_stopped = !npo.copy_prod && ring_full;
531 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
532 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
534 while ((skb = __skb_dequeue(&rxq)) != NULL) {
535 sco = (struct skb_cb_overlay *)skb->cb;
537 if ((1 << vif->meta[npo.meta_cons].gso_type) &
538 vif->gso_prefix_mask) {
539 resp = RING_GET_RESPONSE(&vif->rx,
540 vif->rx.rsp_prod_pvt++);
542 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
544 resp->offset = vif->meta[npo.meta_cons].gso_size;
545 resp->id = vif->meta[npo.meta_cons].id;
546 resp->status = sco->meta_slots_used;
549 sco->meta_slots_used--;
553 vif->dev->stats.tx_bytes += skb->len;
554 vif->dev->stats.tx_packets++;
556 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
558 if (sco->meta_slots_used == 1)
561 flags = XEN_NETRXF_more_data;
563 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
564 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
565 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
566 /* remote but checksummed. */
567 flags |= XEN_NETRXF_data_validated;
570 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
572 vif->meta[npo.meta_cons].size,
575 if ((1 << vif->meta[npo.meta_cons].gso_type) &
577 struct xen_netif_extra_info *gso =
578 (struct xen_netif_extra_info *)
579 RING_GET_RESPONSE(&vif->rx,
580 vif->rx.rsp_prod_pvt++);
582 resp->flags |= XEN_NETRXF_extra_info;
584 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
585 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
587 gso->u.gso.features = 0;
589 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
593 xenvif_add_frag_responses(vif, status,
594 vif->meta + npo.meta_cons + 1,
595 sco->meta_slots_used);
597 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
599 need_to_notify |= !!ret;
601 npo.meta_cons += sco->meta_slots_used;
607 notify_remote_via_irq(vif->rx_irq);
610 void xenvif_check_rx_xenvif(struct xenvif *vif)
614 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
617 napi_schedule(&vif->napi);
620 static void tx_add_credit(struct xenvif *vif)
622 unsigned long max_burst, max_credit;
625 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
626 * Otherwise the interface can seize up due to insufficient credit.
628 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
629 max_burst = min(max_burst, 131072UL);
630 max_burst = max(max_burst, vif->credit_bytes);
632 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
633 max_credit = vif->remaining_credit + vif->credit_bytes;
634 if (max_credit < vif->remaining_credit)
635 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
637 vif->remaining_credit = min(max_credit, max_burst);
640 static void tx_credit_callback(unsigned long data)
642 struct xenvif *vif = (struct xenvif *)data;
644 xenvif_check_rx_xenvif(vif);
647 static void xenvif_tx_err(struct xenvif *vif,
648 struct xen_netif_tx_request *txp, RING_IDX end)
650 RING_IDX cons = vif->tx.req_cons;
653 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
656 txp = RING_GET_REQUEST(&vif->tx, cons++);
658 vif->tx.req_cons = cons;
661 static void xenvif_fatal_tx_err(struct xenvif *vif)
663 netdev_err(vif->dev, "fatal error; disabling device\n");
664 xenvif_carrier_off(vif);
667 static int xenvif_count_requests(struct xenvif *vif,
668 struct xen_netif_tx_request *first,
669 struct xen_netif_tx_request *txp,
672 RING_IDX cons = vif->tx.req_cons;
677 if (!(first->flags & XEN_NETTXF_more_data))
681 struct xen_netif_tx_request dropped_tx = { 0 };
683 if (slots >= work_to_do) {
685 "Asked for %d slots but exceeds this limit\n",
687 xenvif_fatal_tx_err(vif);
691 /* This guest is really using too many slots and
692 * considered malicious.
694 if (unlikely(slots >= fatal_skb_slots)) {
696 "Malicious frontend using %d slots, threshold %u\n",
697 slots, fatal_skb_slots);
698 xenvif_fatal_tx_err(vif);
702 /* Xen network protocol had implicit dependency on
703 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
704 * the historical MAX_SKB_FRAGS value 18 to honor the
705 * same behavior as before. Any packet using more than
706 * 18 slots but less than fatal_skb_slots slots is
709 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
712 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
713 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
720 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
723 /* If the guest submitted a frame >= 64 KiB then
724 * first->size overflowed and following slots will
725 * appear to be larger than the frame.
727 * This cannot be fatal error as there are buggy
728 * frontends that do this.
730 * Consume all slots and drop the packet.
732 if (!drop_err && txp->size > first->size) {
735 "Invalid tx request, slot size %u > remaining size %u\n",
736 txp->size, first->size);
740 first->size -= txp->size;
743 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
744 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
745 txp->offset, txp->size);
746 xenvif_fatal_tx_err(vif);
750 more_data = txp->flags & XEN_NETTXF_more_data;
758 xenvif_tx_err(vif, first, cons + slots);
765 static struct page *xenvif_alloc_page(struct xenvif *vif,
770 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
773 vif->mmap_pages[pending_idx] = page;
778 static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
780 struct xen_netif_tx_request *txp,
781 struct gnttab_copy *gop)
783 struct skb_shared_info *shinfo = skb_shinfo(skb);
784 skb_frag_t *frags = shinfo->frags;
785 u16 pending_idx = *((u16 *)skb->data);
789 pending_ring_idx_t index, start_idx = 0;
791 unsigned int nr_slots;
792 struct pending_tx_info *first = NULL;
794 /* At this point shinfo->nr_frags is in fact the number of
795 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
797 nr_slots = shinfo->nr_frags;
799 /* Skip first skb fragment if it is on same page as header fragment. */
800 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
802 /* Coalesce tx requests, at this point the packet passed in
803 * should be <= 64K. Any packets larger than 64K have been
804 * handled in xenvif_count_requests().
806 for (shinfo->nr_frags = slot = start; slot < nr_slots;
807 shinfo->nr_frags++) {
808 struct pending_tx_info *pending_tx_info =
809 vif->pending_tx_info;
811 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
817 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
818 gop->flags = GNTCOPY_source_gref;
820 gop->source.u.ref = txp->gref;
821 gop->source.domid = vif->domid;
822 gop->source.offset = txp->offset;
824 gop->dest.domid = DOMID_SELF;
826 gop->dest.offset = dst_offset;
827 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
829 if (dst_offset + txp->size > PAGE_SIZE) {
830 /* This page can only merge a portion
831 * of tx request. Do not increment any
832 * pointer / counter here. The txp
833 * will be dealt with in future
834 * rounds, eventually hitting the
837 gop->len = PAGE_SIZE - dst_offset;
838 txp->offset += gop->len;
839 txp->size -= gop->len;
840 dst_offset += gop->len; /* quit loop */
842 /* This tx request can be merged in the page */
843 gop->len = txp->size;
844 dst_offset += gop->len;
846 index = pending_index(vif->pending_cons++);
848 pending_idx = vif->pending_ring[index];
850 memcpy(&pending_tx_info[pending_idx].req, txp,
853 /* Poison these fields, corresponding
854 * fields for head tx req will be set
855 * to correct values after the loop.
857 vif->mmap_pages[pending_idx] = (void *)(~0UL);
858 pending_tx_info[pending_idx].head =
859 INVALID_PENDING_RING_IDX;
862 first = &pending_tx_info[pending_idx];
864 head_idx = pending_idx;
874 first->req.offset = 0;
875 first->req.size = dst_offset;
876 first->head = start_idx;
877 vif->mmap_pages[head_idx] = page;
878 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
881 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
885 /* Unwind, freeing all pages and sending error responses. */
886 while (shinfo->nr_frags-- > start) {
887 xenvif_idx_release(vif,
888 frag_get_pending_idx(&frags[shinfo->nr_frags]),
889 XEN_NETIF_RSP_ERROR);
891 /* The head too, if necessary. */
893 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
898 static int xenvif_tx_check_gop(struct xenvif *vif,
900 struct gnttab_copy **gopp)
902 struct gnttab_copy *gop = *gopp;
903 u16 pending_idx = *((u16 *)skb->data);
904 struct skb_shared_info *shinfo = skb_shinfo(skb);
905 struct pending_tx_info *tx_info;
906 int nr_frags = shinfo->nr_frags;
908 u16 peek; /* peek into next tx request */
910 /* Check status of header. */
913 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
915 /* Skip first skb fragment if it is on same page as header fragment. */
916 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
918 for (i = start; i < nr_frags; i++) {
920 pending_ring_idx_t head;
922 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
923 tx_info = &vif->pending_tx_info[pending_idx];
924 head = tx_info->head;
926 /* Check error status: if okay then remember grant handle. */
928 newerr = (++gop)->status;
931 peek = vif->pending_ring[pending_index(++head)];
932 } while (!pending_tx_is_head(vif, peek));
934 if (likely(!newerr)) {
935 /* Had a previous error? Invalidate this fragment. */
937 xenvif_idx_release(vif, pending_idx,
942 /* Error on this fragment: respond to client with an error. */
943 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
945 /* Not the first error? Preceding frags already invalidated. */
949 /* First error: invalidate header and preceding fragments. */
950 pending_idx = *((u16 *)skb->data);
951 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
952 for (j = start; j < i; j++) {
953 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
954 xenvif_idx_release(vif, pending_idx,
958 /* Remember the error: invalidate all subsequent fragments. */
966 static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
968 struct skb_shared_info *shinfo = skb_shinfo(skb);
969 int nr_frags = shinfo->nr_frags;
972 for (i = 0; i < nr_frags; i++) {
973 skb_frag_t *frag = shinfo->frags + i;
974 struct xen_netif_tx_request *txp;
978 pending_idx = frag_get_pending_idx(frag);
980 txp = &vif->pending_tx_info[pending_idx].req;
981 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
982 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
983 skb->len += txp->size;
984 skb->data_len += txp->size;
985 skb->truesize += txp->size;
987 /* Take an extra reference to offset xenvif_idx_release */
988 get_page(vif->mmap_pages[pending_idx]);
989 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
993 static int xenvif_get_extras(struct xenvif *vif,
994 struct xen_netif_extra_info *extras,
997 struct xen_netif_extra_info extra;
998 RING_IDX cons = vif->tx.req_cons;
1001 if (unlikely(work_to_do-- <= 0)) {
1002 netdev_err(vif->dev, "Missing extra info\n");
1003 xenvif_fatal_tx_err(vif);
1007 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1009 if (unlikely(!extra.type ||
1010 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1011 vif->tx.req_cons = ++cons;
1012 netdev_err(vif->dev,
1013 "Invalid extra type: %d\n", extra.type);
1014 xenvif_fatal_tx_err(vif);
1018 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1019 vif->tx.req_cons = ++cons;
1020 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1025 static int xenvif_set_skb_gso(struct xenvif *vif,
1026 struct sk_buff *skb,
1027 struct xen_netif_extra_info *gso)
1029 if (!gso->u.gso.size) {
1030 netdev_err(vif->dev, "GSO size must not be zero.\n");
1031 xenvif_fatal_tx_err(vif);
1035 switch (gso->u.gso.type) {
1036 case XEN_NETIF_GSO_TYPE_TCPV4:
1037 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1039 case XEN_NETIF_GSO_TYPE_TCPV6:
1040 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1043 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1044 xenvif_fatal_tx_err(vif);
1048 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1049 /* gso_segs will be calculated later */
1054 static inline int maybe_pull_tail(struct sk_buff *skb, unsigned int len,
1057 if (skb_headlen(skb) >= len)
1060 /* If we need to pullup then pullup to the max, so we
1061 * won't need to do it again.
1066 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
1069 if (skb_headlen(skb) < len)
1075 /* This value should be large enough to cover a tagged ethernet header plus
1076 * maximally sized IP and TCP or UDP headers.
1078 #define MAX_IP_HDR_LEN 128
1080 static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
1081 int recalculate_partial_csum)
1089 err = maybe_pull_tail(skb,
1090 sizeof(struct iphdr),
1095 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
1098 off = ip_hdrlen(skb);
1105 switch (ip_hdr(skb)->protocol) {
1107 err = maybe_pull_tail(skb,
1108 off + sizeof(struct tcphdr),
1113 if (!skb_partial_csum_set(skb, off,
1114 offsetof(struct tcphdr, check))) {
1119 if (recalculate_partial_csum)
1120 tcp_hdr(skb)->check =
1121 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1127 err = maybe_pull_tail(skb,
1128 off + sizeof(struct udphdr),
1133 if (!skb_partial_csum_set(skb, off,
1134 offsetof(struct udphdr, check))) {
1139 if (recalculate_partial_csum)
1140 udp_hdr(skb)->check =
1141 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1156 /* This value should be large enough to cover a tagged ethernet header plus
1157 * an IPv6 header, all options, and a maximal TCP or UDP header.
1159 #define MAX_IPV6_HDR_LEN 256
1161 #define OPT_HDR(type, skb, off) \
1162 (type *)(skb_network_header(skb) + (off))
1164 static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
1165 int recalculate_partial_csum)
1177 off = sizeof(struct ipv6hdr);
1179 err = maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
1183 nexthdr = ipv6_hdr(skb)->nexthdr;
1185 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
1186 while (off <= len && !done) {
1188 case IPPROTO_DSTOPTS:
1189 case IPPROTO_HOPOPTS:
1190 case IPPROTO_ROUTING: {
1191 struct ipv6_opt_hdr *hp;
1193 err = maybe_pull_tail(skb,
1195 sizeof(struct ipv6_opt_hdr),
1200 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
1201 nexthdr = hp->nexthdr;
1202 off += ipv6_optlen(hp);
1206 struct ip_auth_hdr *hp;
1208 err = maybe_pull_tail(skb,
1210 sizeof(struct ip_auth_hdr),
1215 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
1216 nexthdr = hp->nexthdr;
1217 off += ipv6_authlen(hp);
1220 case IPPROTO_FRAGMENT: {
1221 struct frag_hdr *hp;
1223 err = maybe_pull_tail(skb,
1225 sizeof(struct frag_hdr),
1230 hp = OPT_HDR(struct frag_hdr, skb, off);
1232 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
1235 nexthdr = hp->nexthdr;
1236 off += sizeof(struct frag_hdr);
1247 if (!done || fragment)
1252 err = maybe_pull_tail(skb,
1253 off + sizeof(struct tcphdr),
1258 if (!skb_partial_csum_set(skb, off,
1259 offsetof(struct tcphdr, check))) {
1264 if (recalculate_partial_csum)
1265 tcp_hdr(skb)->check =
1266 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1267 &ipv6_hdr(skb)->daddr,
1272 err = maybe_pull_tail(skb,
1273 off + sizeof(struct udphdr),
1278 if (!skb_partial_csum_set(skb, off,
1279 offsetof(struct udphdr, check))) {
1284 if (recalculate_partial_csum)
1285 udp_hdr(skb)->check =
1286 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1287 &ipv6_hdr(skb)->daddr,
1301 static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1304 int recalculate_partial_csum = 0;
1306 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1307 * peers can fail to set NETRXF_csum_blank when sending a GSO
1308 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1309 * recalculate the partial checksum.
1311 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1312 vif->rx_gso_checksum_fixup++;
1313 skb->ip_summed = CHECKSUM_PARTIAL;
1314 recalculate_partial_csum = 1;
1317 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1318 if (skb->ip_summed != CHECKSUM_PARTIAL)
1321 if (skb->protocol == htons(ETH_P_IP))
1322 err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
1323 else if (skb->protocol == htons(ETH_P_IPV6))
1324 err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
1329 static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1331 u64 now = get_jiffies_64();
1332 u64 next_credit = vif->credit_window_start +
1333 msecs_to_jiffies(vif->credit_usec / 1000);
1335 /* Timer could already be pending in rare cases. */
1336 if (timer_pending(&vif->credit_timeout))
1339 /* Passed the point where we can replenish credit? */
1340 if (time_after_eq64(now, next_credit)) {
1341 vif->credit_window_start = now;
1345 /* Still too big to send right now? Set a callback. */
1346 if (size > vif->remaining_credit) {
1347 vif->credit_timeout.data =
1349 vif->credit_timeout.function =
1351 mod_timer(&vif->credit_timeout,
1353 vif->credit_window_start = next_credit;
1361 static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
1363 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
1364 struct sk_buff *skb;
1367 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1368 < MAX_PENDING_REQS) &&
1369 (skb_queue_len(&vif->tx_queue) < budget)) {
1370 struct xen_netif_tx_request txreq;
1371 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1373 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1377 unsigned int data_len;
1378 pending_ring_idx_t index;
1380 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1381 XEN_NETIF_TX_RING_SIZE) {
1382 netdev_err(vif->dev,
1383 "Impossible number of requests. "
1384 "req_prod %d, req_cons %d, size %ld\n",
1385 vif->tx.sring->req_prod, vif->tx.req_cons,
1386 XEN_NETIF_TX_RING_SIZE);
1387 xenvif_fatal_tx_err(vif);
1391 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
1395 idx = vif->tx.req_cons;
1396 rmb(); /* Ensure that we see the request before we copy it. */
1397 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1399 /* Credit-based scheduling. */
1400 if (txreq.size > vif->remaining_credit &&
1401 tx_credit_exceeded(vif, txreq.size))
1404 vif->remaining_credit -= txreq.size;
1407 vif->tx.req_cons = ++idx;
1409 memset(extras, 0, sizeof(extras));
1410 if (txreq.flags & XEN_NETTXF_extra_info) {
1411 work_to_do = xenvif_get_extras(vif, extras,
1413 idx = vif->tx.req_cons;
1414 if (unlikely(work_to_do < 0))
1418 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
1419 if (unlikely(ret < 0))
1424 if (unlikely(txreq.size < ETH_HLEN)) {
1425 netdev_dbg(vif->dev,
1426 "Bad packet size: %d\n", txreq.size);
1427 xenvif_tx_err(vif, &txreq, idx);
1431 /* No crossing a page as the payload mustn't fragment. */
1432 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1433 netdev_err(vif->dev,
1434 "txreq.offset: %x, size: %u, end: %lu\n",
1435 txreq.offset, txreq.size,
1436 (txreq.offset&~PAGE_MASK) + txreq.size);
1437 xenvif_fatal_tx_err(vif);
1441 index = pending_index(vif->pending_cons);
1442 pending_idx = vif->pending_ring[index];
1444 data_len = (txreq.size > PKT_PROT_LEN &&
1445 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1446 PKT_PROT_LEN : txreq.size;
1448 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1449 GFP_ATOMIC | __GFP_NOWARN);
1450 if (unlikely(skb == NULL)) {
1451 netdev_dbg(vif->dev,
1452 "Can't allocate a skb in start_xmit.\n");
1453 xenvif_tx_err(vif, &txreq, idx);
1457 /* Packets passed to netif_rx() must have some headroom. */
1458 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1460 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1461 struct xen_netif_extra_info *gso;
1462 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1464 if (xenvif_set_skb_gso(vif, skb, gso)) {
1465 /* Failure in xenvif_set_skb_gso is fatal. */
1471 /* XXX could copy straight to head */
1472 page = xenvif_alloc_page(vif, pending_idx);
1475 xenvif_tx_err(vif, &txreq, idx);
1479 gop->source.u.ref = txreq.gref;
1480 gop->source.domid = vif->domid;
1481 gop->source.offset = txreq.offset;
1483 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1484 gop->dest.domid = DOMID_SELF;
1485 gop->dest.offset = txreq.offset;
1487 gop->len = txreq.size;
1488 gop->flags = GNTCOPY_source_gref;
1492 memcpy(&vif->pending_tx_info[pending_idx].req,
1493 &txreq, sizeof(txreq));
1494 vif->pending_tx_info[pending_idx].head = index;
1495 *((u16 *)skb->data) = pending_idx;
1497 __skb_put(skb, data_len);
1499 skb_shinfo(skb)->nr_frags = ret;
1500 if (data_len < txreq.size) {
1501 skb_shinfo(skb)->nr_frags++;
1502 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1505 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1506 INVALID_PENDING_IDX);
1509 vif->pending_cons++;
1511 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
1512 if (request_gop == NULL) {
1514 xenvif_tx_err(vif, &txreq, idx);
1519 __skb_queue_tail(&vif->tx_queue, skb);
1521 vif->tx.req_cons = idx;
1523 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
1527 return gop - vif->tx_copy_ops;
1531 static int xenvif_tx_submit(struct xenvif *vif)
1533 struct gnttab_copy *gop = vif->tx_copy_ops;
1534 struct sk_buff *skb;
1537 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
1538 struct xen_netif_tx_request *txp;
1542 pending_idx = *((u16 *)skb->data);
1543 txp = &vif->pending_tx_info[pending_idx].req;
1545 /* Check the remap error code. */
1546 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
1547 netdev_dbg(vif->dev, "netback grant failed.\n");
1548 skb_shinfo(skb)->nr_frags = 0;
1553 data_len = skb->len;
1555 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
1557 if (data_len < txp->size) {
1558 /* Append the packet payload as a fragment. */
1559 txp->offset += data_len;
1560 txp->size -= data_len;
1562 /* Schedule a response immediately. */
1563 xenvif_idx_release(vif, pending_idx,
1564 XEN_NETIF_RSP_OKAY);
1567 if (txp->flags & XEN_NETTXF_csum_blank)
1568 skb->ip_summed = CHECKSUM_PARTIAL;
1569 else if (txp->flags & XEN_NETTXF_data_validated)
1570 skb->ip_summed = CHECKSUM_UNNECESSARY;
1572 xenvif_fill_frags(vif, skb);
1574 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
1575 int target = min_t(int, skb->len, PKT_PROT_LEN);
1576 __pskb_pull_tail(skb, target - skb_headlen(skb));
1579 skb->dev = vif->dev;
1580 skb->protocol = eth_type_trans(skb, skb->dev);
1581 skb_reset_network_header(skb);
1583 if (checksum_setup(vif, skb)) {
1584 netdev_dbg(vif->dev,
1585 "Can't setup checksum in net_tx_action\n");
1590 skb_probe_transport_header(skb, 0);
1592 /* If the packet is GSO then we will have just set up the
1593 * transport header offset in checksum_setup so it's now
1594 * straightforward to calculate gso_segs.
1596 if (skb_is_gso(skb)) {
1597 int mss = skb_shinfo(skb)->gso_size;
1598 int hdrlen = skb_transport_header(skb) -
1599 skb_mac_header(skb) +
1602 skb_shinfo(skb)->gso_segs =
1603 DIV_ROUND_UP(skb->len - hdrlen, mss);
1606 vif->dev->stats.rx_bytes += skb->len;
1607 vif->dev->stats.rx_packets++;
1611 netif_receive_skb(skb);
1617 /* Called after netfront has transmitted */
1618 int xenvif_tx_action(struct xenvif *vif, int budget)
1623 if (unlikely(!tx_work_todo(vif)))
1626 nr_gops = xenvif_tx_build_gops(vif, budget);
1631 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
1633 work_done = xenvif_tx_submit(vif);
1638 static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1641 struct pending_tx_info *pending_tx_info;
1642 pending_ring_idx_t head;
1643 u16 peek; /* peek into next tx request */
1645 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
1647 /* Already complete? */
1648 if (vif->mmap_pages[pending_idx] == NULL)
1651 pending_tx_info = &vif->pending_tx_info[pending_idx];
1653 head = pending_tx_info->head;
1655 BUG_ON(!pending_tx_is_head(vif, head));
1656 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
1659 pending_ring_idx_t index;
1660 pending_ring_idx_t idx = pending_index(head);
1661 u16 info_idx = vif->pending_ring[idx];
1663 pending_tx_info = &vif->pending_tx_info[info_idx];
1664 make_tx_response(vif, &pending_tx_info->req, status);
1666 /* Setting any number other than
1667 * INVALID_PENDING_RING_IDX indicates this slot is
1668 * starting a new packet / ending a previous packet.
1670 pending_tx_info->head = 0;
1672 index = pending_index(vif->pending_prod++);
1673 vif->pending_ring[index] = vif->pending_ring[info_idx];
1675 peek = vif->pending_ring[pending_index(++head)];
1677 } while (!pending_tx_is_head(vif, peek));
1679 put_page(vif->mmap_pages[pending_idx]);
1680 vif->mmap_pages[pending_idx] = NULL;
1684 static void make_tx_response(struct xenvif *vif,
1685 struct xen_netif_tx_request *txp,
1688 RING_IDX i = vif->tx.rsp_prod_pvt;
1689 struct xen_netif_tx_response *resp;
1692 resp = RING_GET_RESPONSE(&vif->tx, i);
1696 if (txp->flags & XEN_NETTXF_extra_info)
1697 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1699 vif->tx.rsp_prod_pvt = ++i;
1700 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1702 notify_remote_via_irq(vif->tx_irq);
1705 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1712 RING_IDX i = vif->rx.rsp_prod_pvt;
1713 struct xen_netif_rx_response *resp;
1715 resp = RING_GET_RESPONSE(&vif->rx, i);
1716 resp->offset = offset;
1717 resp->flags = flags;
1719 resp->status = (s16)size;
1721 resp->status = (s16)st;
1723 vif->rx.rsp_prod_pvt = ++i;
1728 static inline int rx_work_todo(struct xenvif *vif)
1730 return (!skb_queue_empty(&vif->rx_queue) && !vif->rx_queue_stopped) ||
1734 static inline int tx_work_todo(struct xenvif *vif)
1737 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1738 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1739 < MAX_PENDING_REQS))
1745 void xenvif_unmap_frontend_rings(struct xenvif *vif)
1748 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1751 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1755 int xenvif_map_frontend_rings(struct xenvif *vif,
1756 grant_ref_t tx_ring_ref,
1757 grant_ref_t rx_ring_ref)
1760 struct xen_netif_tx_sring *txs;
1761 struct xen_netif_rx_sring *rxs;
1765 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1766 tx_ring_ref, &addr);
1770 txs = (struct xen_netif_tx_sring *)addr;
1771 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1773 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1774 rx_ring_ref, &addr);
1778 rxs = (struct xen_netif_rx_sring *)addr;
1779 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1784 xenvif_unmap_frontend_rings(vif);
1788 void xenvif_stop_queue(struct xenvif *vif)
1790 if (!vif->can_queue)
1793 netif_stop_queue(vif->dev);
1796 static void xenvif_start_queue(struct xenvif *vif)
1798 if (xenvif_schedulable(vif))
1799 netif_wake_queue(vif->dev);
1802 int xenvif_kthread(void *data)
1804 struct xenvif *vif = data;
1805 struct sk_buff *skb;
1807 while (!kthread_should_stop()) {
1808 wait_event_interruptible(vif->wq,
1809 rx_work_todo(vif) ||
1810 kthread_should_stop());
1811 if (kthread_should_stop())
1814 if (!skb_queue_empty(&vif->rx_queue))
1815 xenvif_rx_action(vif);
1817 vif->rx_event = false;
1819 if (skb_queue_empty(&vif->rx_queue) &&
1820 netif_queue_stopped(vif->dev))
1821 xenvif_start_queue(vif);
1826 /* Bin any remaining skbs */
1827 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1833 static int __init netback_init(void)
1840 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1841 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1842 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1843 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1846 rc = xenvif_xenbus_init();
1856 module_init(netback_init);
1858 static void __exit netback_fini(void)
1860 xenvif_xenbus_fini();
1862 module_exit(netback_fini);
1864 MODULE_LICENSE("Dual BSD/GPL");
1865 MODULE_ALIAS("xen-backend:vif");