2 * Back-end of the driver for virtual network devices. This portion of the
3 * driver exports a 'unified' network-device interface that can be accessed
4 * by any operating system that implements a compatible front end. A
5 * reference front-end implementation can be found in:
6 * drivers/net/xen-netfront.c
8 * Copyright (c) 2002-2005, K A Fraser
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
37 #include <linux/kthread.h>
38 #include <linux/if_vlan.h>
39 #include <linux/udp.h>
44 #include <xen/events.h>
45 #include <xen/interface/memory.h>
47 #include <asm/xen/hypercall.h>
48 #include <asm/xen/page.h>
50 /* Provide an option to disable split event channels at load time as
51 * event channels are limited resource. Split event channels are
54 bool separate_tx_rx_irq = 1;
55 module_param(separate_tx_rx_irq, bool, 0644);
58 * This is the maximum slots a skb can have. If a guest sends a skb
59 * which exceeds this limit it is considered malicious.
61 #define FATAL_SKB_SLOTS_DEFAULT 20
62 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
63 module_param(fatal_skb_slots, uint, 0444);
66 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
67 * the maximum slots a valid packet can use. Now this value is defined
68 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
71 #define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
74 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
75 * one or more merged tx requests, otherwise it is the continuation of
76 * previous tx request.
78 static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
80 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
83 static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
86 static void make_tx_response(struct xenvif *vif,
87 struct xen_netif_tx_request *txp,
90 static inline int tx_work_todo(struct xenvif *vif);
91 static inline int rx_work_todo(struct xenvif *vif);
93 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
100 static inline unsigned long idx_to_pfn(struct xenvif *vif,
103 return page_to_pfn(vif->mmap_pages[idx]);
106 static inline unsigned long idx_to_kaddr(struct xenvif *vif,
109 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
112 /* This is a miniumum size for the linear area to avoid lots of
113 * calls to __pskb_pull_tail() as we set up checksum offsets. The
114 * value 128 was chosen as it covers all IPv4 and most likely
117 #define PKT_PROT_LEN 128
119 static u16 frag_get_pending_idx(skb_frag_t *frag)
121 return (u16)frag->page_offset;
124 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
126 frag->page_offset = pending_idx;
129 static inline pending_ring_idx_t pending_index(unsigned i)
131 return i & (MAX_PENDING_REQS-1);
134 static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
136 return MAX_PENDING_REQS -
137 vif->pending_prod + vif->pending_cons;
140 static int max_required_rx_slots(struct xenvif *vif)
142 int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
144 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
145 if (vif->can_sg || vif->gso || vif->gso_prefix)
146 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
151 int xenvif_rx_ring_full(struct xenvif *vif)
153 RING_IDX peek = vif->rx_req_cons_peek;
154 RING_IDX needed = max_required_rx_slots(vif);
156 return ((vif->rx.sring->req_prod - peek) < needed) ||
157 ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
160 int xenvif_must_stop_queue(struct xenvif *vif)
162 if (!xenvif_rx_ring_full(vif))
165 vif->rx.sring->req_event = vif->rx_req_cons_peek +
166 max_required_rx_slots(vif);
167 mb(); /* request notification /then/ check the queue */
169 return xenvif_rx_ring_full(vif);
173 * Returns true if we should start a new receive buffer instead of
174 * adding 'size' bytes to a buffer which currently contains 'offset'
177 static bool start_new_rx_buffer(int offset, unsigned long size, int head)
179 /* simple case: we have completely filled the current buffer. */
180 if (offset == MAX_BUFFER_OFFSET)
184 * complex case: start a fresh buffer if the current frag
185 * would overflow the current buffer but only if:
186 * (i) this frag would fit completely in the next buffer
187 * and (ii) there is already some data in the current buffer
188 * and (iii) this is not the head buffer.
191 * - (i) stops us splitting a frag into two copies
192 * unless the frag is too large for a single buffer.
193 * - (ii) stops us from leaving a buffer pointlessly empty.
194 * - (iii) stops us leaving the first buffer
195 * empty. Strictly speaking this is already covered
196 * by (ii) but is explicitly checked because
197 * netfront relies on the first buffer being
198 * non-empty and can crash otherwise.
200 * This means we will effectively linearise small
201 * frags but do not needlessly split large buffers
202 * into multiple copies tend to give large frags their
203 * own buffers as before.
205 if ((offset + size > MAX_BUFFER_OFFSET) &&
206 (size <= MAX_BUFFER_OFFSET) && offset && !head)
212 struct xenvif_count_slot_state {
213 unsigned long copy_off;
217 unsigned int xenvif_count_frag_slots(struct xenvif *vif,
218 unsigned long offset, unsigned long size,
219 struct xenvif_count_slot_state *state)
223 offset &= ~PAGE_MASK;
228 bytes = PAGE_SIZE - offset;
233 if (start_new_rx_buffer(state->copy_off, bytes, state->head)) {
238 if (state->copy_off + bytes > MAX_BUFFER_OFFSET)
239 bytes = MAX_BUFFER_OFFSET - state->copy_off;
241 state->copy_off += bytes;
246 if (offset == PAGE_SIZE)
256 * Figure out how many ring slots we're going to need to send @skb to
257 * the guest. This function is essentially a dry run of
258 * xenvif_gop_frag_copy.
260 unsigned int xenvif_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
262 struct xenvif_count_slot_state state;
270 /* Slot for the first (partial) page of data. */
273 /* Need a slot for the GSO prefix for GSO extra data? */
274 if (skb_shinfo(skb)->gso_size)
278 while (data < skb_tail_pointer(skb)) {
279 unsigned long offset = offset_in_page(data);
280 unsigned long size = PAGE_SIZE - offset;
282 if (data + size > skb_tail_pointer(skb))
283 size = skb_tail_pointer(skb) - data;
285 count += xenvif_count_frag_slots(vif, offset, size, &state);
290 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
291 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
292 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
294 count += xenvif_count_frag_slots(vif, offset, size, &state);
299 struct netrx_pending_operations {
300 unsigned copy_prod, copy_cons;
301 unsigned meta_prod, meta_cons;
302 struct gnttab_copy *copy;
303 struct xenvif_rx_meta *meta;
305 grant_ref_t copy_gref;
308 static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
309 struct netrx_pending_operations *npo)
311 struct xenvif_rx_meta *meta;
312 struct xen_netif_rx_request *req;
314 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
316 meta = npo->meta + npo->meta_prod++;
322 npo->copy_gref = req->gref;
328 * Set up the grant operations for this fragment. If it's a flipping
329 * interface, we also set up the unmap request from here.
331 static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
332 struct netrx_pending_operations *npo,
333 struct page *page, unsigned long size,
334 unsigned long offset, int *head)
336 struct gnttab_copy *copy_gop;
337 struct xenvif_rx_meta *meta;
340 /* Data must not cross a page boundary. */
341 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
343 meta = npo->meta + npo->meta_prod - 1;
345 /* Skip unused frames from start of page */
346 page += offset >> PAGE_SHIFT;
347 offset &= ~PAGE_MASK;
350 BUG_ON(offset >= PAGE_SIZE);
351 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
353 bytes = PAGE_SIZE - offset;
358 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
360 * Netfront requires there to be some data in the head
365 meta = get_next_rx_buffer(vif, npo);
368 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
369 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
371 copy_gop = npo->copy + npo->copy_prod++;
372 copy_gop->flags = GNTCOPY_dest_gref;
373 copy_gop->len = bytes;
375 copy_gop->source.domid = DOMID_SELF;
376 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
377 copy_gop->source.offset = offset;
379 copy_gop->dest.domid = vif->domid;
380 copy_gop->dest.offset = npo->copy_off;
381 copy_gop->dest.u.ref = npo->copy_gref;
383 npo->copy_off += bytes;
390 if (offset == PAGE_SIZE && size) {
391 BUG_ON(!PageCompound(page));
396 /* Leave a gap for the GSO descriptor. */
397 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
400 *head = 0; /* There must be something in this buffer now. */
406 * Prepare an SKB to be transmitted to the frontend.
408 * This function is responsible for allocating grant operations, meta
411 * It returns the number of meta structures consumed. The number of
412 * ring slots used is always equal to the number of meta slots used
413 * plus the number of GSO descriptors used. Currently, we use either
414 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
415 * frontend-side LRO).
417 static int xenvif_gop_skb(struct sk_buff *skb,
418 struct netrx_pending_operations *npo)
420 struct xenvif *vif = netdev_priv(skb->dev);
421 int nr_frags = skb_shinfo(skb)->nr_frags;
423 struct xen_netif_rx_request *req;
424 struct xenvif_rx_meta *meta;
429 old_meta_prod = npo->meta_prod;
431 /* Set up a GSO prefix descriptor, if necessary */
432 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
433 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
434 meta = npo->meta + npo->meta_prod++;
435 meta->gso_size = skb_shinfo(skb)->gso_size;
440 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
441 meta = npo->meta + npo->meta_prod++;
443 if (!vif->gso_prefix)
444 meta->gso_size = skb_shinfo(skb)->gso_size;
451 npo->copy_gref = req->gref;
454 while (data < skb_tail_pointer(skb)) {
455 unsigned int offset = offset_in_page(data);
456 unsigned int len = PAGE_SIZE - offset;
458 if (data + len > skb_tail_pointer(skb))
459 len = skb_tail_pointer(skb) - data;
461 xenvif_gop_frag_copy(vif, skb, npo,
462 virt_to_page(data), len, offset, &head);
466 for (i = 0; i < nr_frags; i++) {
467 xenvif_gop_frag_copy(vif, skb, npo,
468 skb_frag_page(&skb_shinfo(skb)->frags[i]),
469 skb_frag_size(&skb_shinfo(skb)->frags[i]),
470 skb_shinfo(skb)->frags[i].page_offset,
474 return npo->meta_prod - old_meta_prod;
478 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
479 * used to set up the operations on the top of
480 * netrx_pending_operations, which have since been done. Check that
481 * they didn't give any errors and advance over them.
483 static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
484 struct netrx_pending_operations *npo)
486 struct gnttab_copy *copy_op;
487 int status = XEN_NETIF_RSP_OKAY;
490 for (i = 0; i < nr_meta_slots; i++) {
491 copy_op = npo->copy + npo->copy_cons++;
492 if (copy_op->status != GNTST_okay) {
494 "Bad status %d from copy to DOM%d.\n",
495 copy_op->status, vif->domid);
496 status = XEN_NETIF_RSP_ERROR;
503 static void xenvif_add_frag_responses(struct xenvif *vif, int status,
504 struct xenvif_rx_meta *meta,
508 unsigned long offset;
510 /* No fragments used */
511 if (nr_meta_slots <= 1)
516 for (i = 0; i < nr_meta_slots; i++) {
518 if (i == nr_meta_slots - 1)
521 flags = XEN_NETRXF_more_data;
524 make_rx_response(vif, meta[i].id, status, offset,
525 meta[i].size, flags);
529 struct skb_cb_overlay {
533 static void xenvif_kick_thread(struct xenvif *vif)
538 void xenvif_rx_action(struct xenvif *vif)
542 struct xen_netif_rx_response *resp;
543 struct sk_buff_head rxq;
549 unsigned long offset;
550 struct skb_cb_overlay *sco;
551 int need_to_notify = 0;
553 struct netrx_pending_operations npo = {
554 .copy = vif->grant_copy_op,
558 skb_queue_head_init(&rxq);
562 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
563 vif = netdev_priv(skb->dev);
564 nr_frags = skb_shinfo(skb)->nr_frags;
566 sco = (struct skb_cb_overlay *)skb->cb;
567 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
569 count += nr_frags + 1;
571 __skb_queue_tail(&rxq, skb);
573 /* Filled the batch queue? */
574 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
575 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
579 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
584 BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op));
585 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
587 while ((skb = __skb_dequeue(&rxq)) != NULL) {
588 sco = (struct skb_cb_overlay *)skb->cb;
590 vif = netdev_priv(skb->dev);
592 if (vif->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
593 resp = RING_GET_RESPONSE(&vif->rx,
594 vif->rx.rsp_prod_pvt++);
596 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
598 resp->offset = vif->meta[npo.meta_cons].gso_size;
599 resp->id = vif->meta[npo.meta_cons].id;
600 resp->status = sco->meta_slots_used;
603 sco->meta_slots_used--;
607 vif->dev->stats.tx_bytes += skb->len;
608 vif->dev->stats.tx_packets++;
610 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
612 if (sco->meta_slots_used == 1)
615 flags = XEN_NETRXF_more_data;
617 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
618 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
619 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
620 /* remote but checksummed. */
621 flags |= XEN_NETRXF_data_validated;
624 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
626 vif->meta[npo.meta_cons].size,
629 if (vif->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
630 struct xen_netif_extra_info *gso =
631 (struct xen_netif_extra_info *)
632 RING_GET_RESPONSE(&vif->rx,
633 vif->rx.rsp_prod_pvt++);
635 resp->flags |= XEN_NETRXF_extra_info;
637 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
638 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
640 gso->u.gso.features = 0;
642 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
646 xenvif_add_frag_responses(vif, status,
647 vif->meta + npo.meta_cons + 1,
648 sco->meta_slots_used);
650 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
655 xenvif_notify_tx_completion(vif);
657 npo.meta_cons += sco->meta_slots_used;
662 notify_remote_via_irq(vif->rx_irq);
664 /* More work to do? */
665 if (!skb_queue_empty(&vif->rx_queue))
666 xenvif_kick_thread(vif);
669 void xenvif_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
671 skb_queue_tail(&vif->rx_queue, skb);
673 xenvif_kick_thread(vif);
676 void xenvif_check_rx_xenvif(struct xenvif *vif)
680 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
683 napi_schedule(&vif->napi);
686 static void tx_add_credit(struct xenvif *vif)
688 unsigned long max_burst, max_credit;
691 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
692 * Otherwise the interface can seize up due to insufficient credit.
694 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
695 max_burst = min(max_burst, 131072UL);
696 max_burst = max(max_burst, vif->credit_bytes);
698 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
699 max_credit = vif->remaining_credit + vif->credit_bytes;
700 if (max_credit < vif->remaining_credit)
701 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
703 vif->remaining_credit = min(max_credit, max_burst);
706 static void tx_credit_callback(unsigned long data)
708 struct xenvif *vif = (struct xenvif *)data;
710 xenvif_check_rx_xenvif(vif);
713 static void xenvif_tx_err(struct xenvif *vif,
714 struct xen_netif_tx_request *txp, RING_IDX end)
716 RING_IDX cons = vif->tx.req_cons;
719 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
722 txp = RING_GET_REQUEST(&vif->tx, cons++);
724 vif->tx.req_cons = cons;
727 static void xenvif_fatal_tx_err(struct xenvif *vif)
729 netdev_err(vif->dev, "fatal error; disabling device\n");
730 xenvif_carrier_off(vif);
733 static int xenvif_count_requests(struct xenvif *vif,
734 struct xen_netif_tx_request *first,
735 struct xen_netif_tx_request *txp,
738 RING_IDX cons = vif->tx.req_cons;
743 if (!(first->flags & XEN_NETTXF_more_data))
747 struct xen_netif_tx_request dropped_tx = { 0 };
749 if (slots >= work_to_do) {
751 "Asked for %d slots but exceeds this limit\n",
753 xenvif_fatal_tx_err(vif);
757 /* This guest is really using too many slots and
758 * considered malicious.
760 if (unlikely(slots >= fatal_skb_slots)) {
762 "Malicious frontend using %d slots, threshold %u\n",
763 slots, fatal_skb_slots);
764 xenvif_fatal_tx_err(vif);
768 /* Xen network protocol had implicit dependency on
769 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
770 * the historical MAX_SKB_FRAGS value 18 to honor the
771 * same behavior as before. Any packet using more than
772 * 18 slots but less than fatal_skb_slots slots is
775 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
778 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
779 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
786 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
789 /* If the guest submitted a frame >= 64 KiB then
790 * first->size overflowed and following slots will
791 * appear to be larger than the frame.
793 * This cannot be fatal error as there are buggy
794 * frontends that do this.
796 * Consume all slots and drop the packet.
798 if (!drop_err && txp->size > first->size) {
801 "Invalid tx request, slot size %u > remaining size %u\n",
802 txp->size, first->size);
806 first->size -= txp->size;
809 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
810 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
811 txp->offset, txp->size);
812 xenvif_fatal_tx_err(vif);
816 more_data = txp->flags & XEN_NETTXF_more_data;
824 xenvif_tx_err(vif, first, cons + slots);
831 static struct page *xenvif_alloc_page(struct xenvif *vif,
836 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
839 vif->mmap_pages[pending_idx] = page;
844 static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
846 struct xen_netif_tx_request *txp,
847 struct gnttab_copy *gop)
849 struct skb_shared_info *shinfo = skb_shinfo(skb);
850 skb_frag_t *frags = shinfo->frags;
851 u16 pending_idx = *((u16 *)skb->data);
855 pending_ring_idx_t index, start_idx = 0;
857 unsigned int nr_slots;
858 struct pending_tx_info *first = NULL;
860 /* At this point shinfo->nr_frags is in fact the number of
861 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
863 nr_slots = shinfo->nr_frags;
865 /* Skip first skb fragment if it is on same page as header fragment. */
866 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
868 /* Coalesce tx requests, at this point the packet passed in
869 * should be <= 64K. Any packets larger than 64K have been
870 * handled in xenvif_count_requests().
872 for (shinfo->nr_frags = slot = start; slot < nr_slots;
873 shinfo->nr_frags++) {
874 struct pending_tx_info *pending_tx_info =
875 vif->pending_tx_info;
877 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
883 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
884 gop->flags = GNTCOPY_source_gref;
886 gop->source.u.ref = txp->gref;
887 gop->source.domid = vif->domid;
888 gop->source.offset = txp->offset;
890 gop->dest.domid = DOMID_SELF;
892 gop->dest.offset = dst_offset;
893 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
895 if (dst_offset + txp->size > PAGE_SIZE) {
896 /* This page can only merge a portion
897 * of tx request. Do not increment any
898 * pointer / counter here. The txp
899 * will be dealt with in future
900 * rounds, eventually hitting the
903 gop->len = PAGE_SIZE - dst_offset;
904 txp->offset += gop->len;
905 txp->size -= gop->len;
906 dst_offset += gop->len; /* quit loop */
908 /* This tx request can be merged in the page */
909 gop->len = txp->size;
910 dst_offset += gop->len;
912 index = pending_index(vif->pending_cons++);
914 pending_idx = vif->pending_ring[index];
916 memcpy(&pending_tx_info[pending_idx].req, txp,
919 /* Poison these fields, corresponding
920 * fields for head tx req will be set
921 * to correct values after the loop.
923 vif->mmap_pages[pending_idx] = (void *)(~0UL);
924 pending_tx_info[pending_idx].head =
925 INVALID_PENDING_RING_IDX;
928 first = &pending_tx_info[pending_idx];
930 head_idx = pending_idx;
940 first->req.offset = 0;
941 first->req.size = dst_offset;
942 first->head = start_idx;
943 vif->mmap_pages[head_idx] = page;
944 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
947 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
951 /* Unwind, freeing all pages and sending error responses. */
952 while (shinfo->nr_frags-- > start) {
953 xenvif_idx_release(vif,
954 frag_get_pending_idx(&frags[shinfo->nr_frags]),
955 XEN_NETIF_RSP_ERROR);
957 /* The head too, if necessary. */
959 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
964 static int xenvif_tx_check_gop(struct xenvif *vif,
966 struct gnttab_copy **gopp)
968 struct gnttab_copy *gop = *gopp;
969 u16 pending_idx = *((u16 *)skb->data);
970 struct skb_shared_info *shinfo = skb_shinfo(skb);
971 struct pending_tx_info *tx_info;
972 int nr_frags = shinfo->nr_frags;
974 u16 peek; /* peek into next tx request */
976 /* Check status of header. */
979 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
981 /* Skip first skb fragment if it is on same page as header fragment. */
982 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
984 for (i = start; i < nr_frags; i++) {
986 pending_ring_idx_t head;
988 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
989 tx_info = &vif->pending_tx_info[pending_idx];
990 head = tx_info->head;
992 /* Check error status: if okay then remember grant handle. */
994 newerr = (++gop)->status;
997 peek = vif->pending_ring[pending_index(++head)];
998 } while (!pending_tx_is_head(vif, peek));
1000 if (likely(!newerr)) {
1001 /* Had a previous error? Invalidate this fragment. */
1003 xenvif_idx_release(vif, pending_idx,
1004 XEN_NETIF_RSP_OKAY);
1008 /* Error on this fragment: respond to client with an error. */
1009 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
1011 /* Not the first error? Preceding frags already invalidated. */
1015 /* First error: invalidate header and preceding fragments. */
1016 pending_idx = *((u16 *)skb->data);
1017 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
1018 for (j = start; j < i; j++) {
1019 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1020 xenvif_idx_release(vif, pending_idx,
1021 XEN_NETIF_RSP_OKAY);
1024 /* Remember the error: invalidate all subsequent fragments. */
1032 static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
1034 struct skb_shared_info *shinfo = skb_shinfo(skb);
1035 int nr_frags = shinfo->nr_frags;
1038 for (i = 0; i < nr_frags; i++) {
1039 skb_frag_t *frag = shinfo->frags + i;
1040 struct xen_netif_tx_request *txp;
1044 pending_idx = frag_get_pending_idx(frag);
1046 txp = &vif->pending_tx_info[pending_idx].req;
1047 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
1048 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1049 skb->len += txp->size;
1050 skb->data_len += txp->size;
1051 skb->truesize += txp->size;
1053 /* Take an extra reference to offset xenvif_idx_release */
1054 get_page(vif->mmap_pages[pending_idx]);
1055 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
1059 static int xenvif_get_extras(struct xenvif *vif,
1060 struct xen_netif_extra_info *extras,
1063 struct xen_netif_extra_info extra;
1064 RING_IDX cons = vif->tx.req_cons;
1067 if (unlikely(work_to_do-- <= 0)) {
1068 netdev_err(vif->dev, "Missing extra info\n");
1069 xenvif_fatal_tx_err(vif);
1073 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1075 if (unlikely(!extra.type ||
1076 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1077 vif->tx.req_cons = ++cons;
1078 netdev_err(vif->dev,
1079 "Invalid extra type: %d\n", extra.type);
1080 xenvif_fatal_tx_err(vif);
1084 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1085 vif->tx.req_cons = ++cons;
1086 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1091 static int xenvif_set_skb_gso(struct xenvif *vif,
1092 struct sk_buff *skb,
1093 struct xen_netif_extra_info *gso)
1095 if (!gso->u.gso.size) {
1096 netdev_err(vif->dev, "GSO size must not be zero.\n");
1097 xenvif_fatal_tx_err(vif);
1101 switch (gso->u.gso.type) {
1102 case XEN_NETIF_GSO_TYPE_TCPV4:
1103 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1105 case XEN_NETIF_GSO_TYPE_TCPV6:
1106 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1109 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1110 xenvif_fatal_tx_err(vif);
1114 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1116 /* Header must be checked, and gso_segs computed. */
1117 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1118 skb_shinfo(skb)->gso_segs = 0;
1123 static inline void maybe_pull_tail(struct sk_buff *skb, unsigned int len)
1125 if (skb_is_nonlinear(skb) && skb_headlen(skb) < len) {
1126 /* If we need to pullup then pullup to the max, so we
1127 * won't need to do it again.
1129 int target = min_t(int, skb->len, MAX_TCP_HEADER);
1130 __pskb_pull_tail(skb, target - skb_headlen(skb));
1134 static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
1135 int recalculate_partial_csum)
1137 struct iphdr *iph = (void *)skb->data;
1138 unsigned int header_size;
1142 off = sizeof(struct iphdr);
1144 header_size = skb->network_header + off + MAX_IPOPTLEN;
1145 maybe_pull_tail(skb, header_size);
1149 switch (iph->protocol) {
1151 if (!skb_partial_csum_set(skb, off,
1152 offsetof(struct tcphdr, check)))
1155 if (recalculate_partial_csum) {
1156 struct tcphdr *tcph = tcp_hdr(skb);
1158 header_size = skb->network_header +
1160 sizeof(struct tcphdr);
1161 maybe_pull_tail(skb, header_size);
1163 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1169 if (!skb_partial_csum_set(skb, off,
1170 offsetof(struct udphdr, check)))
1173 if (recalculate_partial_csum) {
1174 struct udphdr *udph = udp_hdr(skb);
1176 header_size = skb->network_header +
1178 sizeof(struct udphdr);
1179 maybe_pull_tail(skb, header_size);
1181 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1187 if (net_ratelimit())
1188 netdev_err(vif->dev,
1189 "Attempting to checksum a non-TCP/UDP packet, "
1190 "dropping a protocol %d packet\n",
1201 static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
1202 int recalculate_partial_csum)
1205 struct ipv6hdr *ipv6h = (void *)skb->data;
1207 unsigned int header_size;
1214 off = sizeof(struct ipv6hdr);
1216 header_size = skb->network_header + off;
1217 maybe_pull_tail(skb, header_size);
1219 nexthdr = ipv6h->nexthdr;
1221 while ((off <= sizeof(struct ipv6hdr) + ntohs(ipv6h->payload_len)) &&
1224 case IPPROTO_DSTOPTS:
1225 case IPPROTO_HOPOPTS:
1226 case IPPROTO_ROUTING: {
1227 struct ipv6_opt_hdr *hp = (void *)(skb->data + off);
1229 header_size = skb->network_header +
1231 sizeof(struct ipv6_opt_hdr);
1232 maybe_pull_tail(skb, header_size);
1234 nexthdr = hp->nexthdr;
1235 off += ipv6_optlen(hp);
1239 struct ip_auth_hdr *hp = (void *)(skb->data + off);
1241 header_size = skb->network_header +
1243 sizeof(struct ip_auth_hdr);
1244 maybe_pull_tail(skb, header_size);
1246 nexthdr = hp->nexthdr;
1247 off += (hp->hdrlen+2)<<2;
1250 case IPPROTO_FRAGMENT:
1260 if (net_ratelimit())
1261 netdev_err(vif->dev, "Failed to parse packet header\n");
1266 if (net_ratelimit())
1267 netdev_err(vif->dev, "Packet is a fragment!\n");
1273 if (!skb_partial_csum_set(skb, off,
1274 offsetof(struct tcphdr, check)))
1277 if (recalculate_partial_csum) {
1278 struct tcphdr *tcph = tcp_hdr(skb);
1280 header_size = skb->network_header +
1282 sizeof(struct tcphdr);
1283 maybe_pull_tail(skb, header_size);
1285 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr,
1292 if (!skb_partial_csum_set(skb, off,
1293 offsetof(struct udphdr, check)))
1296 if (recalculate_partial_csum) {
1297 struct udphdr *udph = udp_hdr(skb);
1299 header_size = skb->network_header +
1301 sizeof(struct udphdr);
1302 maybe_pull_tail(skb, header_size);
1304 udph->check = ~csum_ipv6_magic(&ipv6h->saddr,
1311 if (net_ratelimit())
1312 netdev_err(vif->dev,
1313 "Attempting to checksum a non-TCP/UDP packet, "
1314 "dropping a protocol %d packet\n",
1325 static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1328 int recalculate_partial_csum = 0;
1330 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1331 * peers can fail to set NETRXF_csum_blank when sending a GSO
1332 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1333 * recalculate the partial checksum.
1335 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1336 vif->rx_gso_checksum_fixup++;
1337 skb->ip_summed = CHECKSUM_PARTIAL;
1338 recalculate_partial_csum = 1;
1341 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1342 if (skb->ip_summed != CHECKSUM_PARTIAL)
1345 if (skb->protocol == htons(ETH_P_IP))
1346 err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
1347 else if (skb->protocol == htons(ETH_P_IPV6))
1348 err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
1353 static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1355 unsigned long now = jiffies;
1356 unsigned long next_credit =
1357 vif->credit_timeout.expires +
1358 msecs_to_jiffies(vif->credit_usec / 1000);
1360 /* Timer could already be pending in rare cases. */
1361 if (timer_pending(&vif->credit_timeout))
1364 /* Passed the point where we can replenish credit? */
1365 if (time_after_eq(now, next_credit)) {
1366 vif->credit_timeout.expires = now;
1370 /* Still too big to send right now? Set a callback. */
1371 if (size > vif->remaining_credit) {
1372 vif->credit_timeout.data =
1374 vif->credit_timeout.function =
1376 mod_timer(&vif->credit_timeout,
1385 static unsigned xenvif_tx_build_gops(struct xenvif *vif)
1387 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
1388 struct sk_buff *skb;
1391 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1392 < MAX_PENDING_REQS)) {
1393 struct xen_netif_tx_request txreq;
1394 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1396 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1400 unsigned int data_len;
1401 pending_ring_idx_t index;
1403 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1404 XEN_NETIF_TX_RING_SIZE) {
1405 netdev_err(vif->dev,
1406 "Impossible number of requests. "
1407 "req_prod %d, req_cons %d, size %ld\n",
1408 vif->tx.sring->req_prod, vif->tx.req_cons,
1409 XEN_NETIF_TX_RING_SIZE);
1410 xenvif_fatal_tx_err(vif);
1414 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1418 idx = vif->tx.req_cons;
1419 rmb(); /* Ensure that we see the request before we copy it. */
1420 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1422 /* Credit-based scheduling. */
1423 if (txreq.size > vif->remaining_credit &&
1424 tx_credit_exceeded(vif, txreq.size))
1427 vif->remaining_credit -= txreq.size;
1430 vif->tx.req_cons = ++idx;
1432 memset(extras, 0, sizeof(extras));
1433 if (txreq.flags & XEN_NETTXF_extra_info) {
1434 work_to_do = xenvif_get_extras(vif, extras,
1436 idx = vif->tx.req_cons;
1437 if (unlikely(work_to_do < 0))
1441 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
1442 if (unlikely(ret < 0))
1447 if (unlikely(txreq.size < ETH_HLEN)) {
1448 netdev_dbg(vif->dev,
1449 "Bad packet size: %d\n", txreq.size);
1450 xenvif_tx_err(vif, &txreq, idx);
1454 /* No crossing a page as the payload mustn't fragment. */
1455 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1456 netdev_err(vif->dev,
1457 "txreq.offset: %x, size: %u, end: %lu\n",
1458 txreq.offset, txreq.size,
1459 (txreq.offset&~PAGE_MASK) + txreq.size);
1460 xenvif_fatal_tx_err(vif);
1464 index = pending_index(vif->pending_cons);
1465 pending_idx = vif->pending_ring[index];
1467 data_len = (txreq.size > PKT_PROT_LEN &&
1468 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1469 PKT_PROT_LEN : txreq.size;
1471 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1472 GFP_ATOMIC | __GFP_NOWARN);
1473 if (unlikely(skb == NULL)) {
1474 netdev_dbg(vif->dev,
1475 "Can't allocate a skb in start_xmit.\n");
1476 xenvif_tx_err(vif, &txreq, idx);
1480 /* Packets passed to netif_rx() must have some headroom. */
1481 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1483 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1484 struct xen_netif_extra_info *gso;
1485 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1487 if (xenvif_set_skb_gso(vif, skb, gso)) {
1488 /* Failure in xenvif_set_skb_gso is fatal. */
1494 /* XXX could copy straight to head */
1495 page = xenvif_alloc_page(vif, pending_idx);
1498 xenvif_tx_err(vif, &txreq, idx);
1502 gop->source.u.ref = txreq.gref;
1503 gop->source.domid = vif->domid;
1504 gop->source.offset = txreq.offset;
1506 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1507 gop->dest.domid = DOMID_SELF;
1508 gop->dest.offset = txreq.offset;
1510 gop->len = txreq.size;
1511 gop->flags = GNTCOPY_source_gref;
1515 memcpy(&vif->pending_tx_info[pending_idx].req,
1516 &txreq, sizeof(txreq));
1517 vif->pending_tx_info[pending_idx].head = index;
1518 *((u16 *)skb->data) = pending_idx;
1520 __skb_put(skb, data_len);
1522 skb_shinfo(skb)->nr_frags = ret;
1523 if (data_len < txreq.size) {
1524 skb_shinfo(skb)->nr_frags++;
1525 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1528 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1529 INVALID_PENDING_IDX);
1532 vif->pending_cons++;
1534 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
1535 if (request_gop == NULL) {
1537 xenvif_tx_err(vif, &txreq, idx);
1542 __skb_queue_tail(&vif->tx_queue, skb);
1544 vif->tx.req_cons = idx;
1546 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
1550 return gop - vif->tx_copy_ops;
1554 static int xenvif_tx_submit(struct xenvif *vif, int budget)
1556 struct gnttab_copy *gop = vif->tx_copy_ops;
1557 struct sk_buff *skb;
1560 while (work_done < budget &&
1561 (skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
1562 struct xen_netif_tx_request *txp;
1566 pending_idx = *((u16 *)skb->data);
1567 txp = &vif->pending_tx_info[pending_idx].req;
1569 /* Check the remap error code. */
1570 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
1571 netdev_dbg(vif->dev, "netback grant failed.\n");
1572 skb_shinfo(skb)->nr_frags = 0;
1577 data_len = skb->len;
1579 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
1581 if (data_len < txp->size) {
1582 /* Append the packet payload as a fragment. */
1583 txp->offset += data_len;
1584 txp->size -= data_len;
1586 /* Schedule a response immediately. */
1587 xenvif_idx_release(vif, pending_idx,
1588 XEN_NETIF_RSP_OKAY);
1591 if (txp->flags & XEN_NETTXF_csum_blank)
1592 skb->ip_summed = CHECKSUM_PARTIAL;
1593 else if (txp->flags & XEN_NETTXF_data_validated)
1594 skb->ip_summed = CHECKSUM_UNNECESSARY;
1596 xenvif_fill_frags(vif, skb);
1598 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
1599 int target = min_t(int, skb->len, PKT_PROT_LEN);
1600 __pskb_pull_tail(skb, target - skb_headlen(skb));
1603 skb->dev = vif->dev;
1604 skb->protocol = eth_type_trans(skb, skb->dev);
1605 skb_reset_network_header(skb);
1607 if (checksum_setup(vif, skb)) {
1608 netdev_dbg(vif->dev,
1609 "Can't setup checksum in net_tx_action\n");
1614 skb_probe_transport_header(skb, 0);
1616 vif->dev->stats.rx_bytes += skb->len;
1617 vif->dev->stats.rx_packets++;
1621 netif_receive_skb(skb);
1627 /* Called after netfront has transmitted */
1628 int xenvif_tx_action(struct xenvif *vif, int budget)
1633 if (unlikely(!tx_work_todo(vif)))
1636 nr_gops = xenvif_tx_build_gops(vif);
1641 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
1643 work_done = xenvif_tx_submit(vif, nr_gops);
1648 static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1651 struct pending_tx_info *pending_tx_info;
1652 pending_ring_idx_t head;
1653 u16 peek; /* peek into next tx request */
1655 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
1657 /* Already complete? */
1658 if (vif->mmap_pages[pending_idx] == NULL)
1661 pending_tx_info = &vif->pending_tx_info[pending_idx];
1663 head = pending_tx_info->head;
1665 BUG_ON(!pending_tx_is_head(vif, head));
1666 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
1669 pending_ring_idx_t index;
1670 pending_ring_idx_t idx = pending_index(head);
1671 u16 info_idx = vif->pending_ring[idx];
1673 pending_tx_info = &vif->pending_tx_info[info_idx];
1674 make_tx_response(vif, &pending_tx_info->req, status);
1676 /* Setting any number other than
1677 * INVALID_PENDING_RING_IDX indicates this slot is
1678 * starting a new packet / ending a previous packet.
1680 pending_tx_info->head = 0;
1682 index = pending_index(vif->pending_prod++);
1683 vif->pending_ring[index] = vif->pending_ring[info_idx];
1685 peek = vif->pending_ring[pending_index(++head)];
1687 } while (!pending_tx_is_head(vif, peek));
1689 put_page(vif->mmap_pages[pending_idx]);
1690 vif->mmap_pages[pending_idx] = NULL;
1694 static void make_tx_response(struct xenvif *vif,
1695 struct xen_netif_tx_request *txp,
1698 RING_IDX i = vif->tx.rsp_prod_pvt;
1699 struct xen_netif_tx_response *resp;
1702 resp = RING_GET_RESPONSE(&vif->tx, i);
1706 if (txp->flags & XEN_NETTXF_extra_info)
1707 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1709 vif->tx.rsp_prod_pvt = ++i;
1710 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1712 notify_remote_via_irq(vif->tx_irq);
1715 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1722 RING_IDX i = vif->rx.rsp_prod_pvt;
1723 struct xen_netif_rx_response *resp;
1725 resp = RING_GET_RESPONSE(&vif->rx, i);
1726 resp->offset = offset;
1727 resp->flags = flags;
1729 resp->status = (s16)size;
1731 resp->status = (s16)st;
1733 vif->rx.rsp_prod_pvt = ++i;
1738 static inline int rx_work_todo(struct xenvif *vif)
1740 return !skb_queue_empty(&vif->rx_queue);
1743 static inline int tx_work_todo(struct xenvif *vif)
1746 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1747 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1748 < MAX_PENDING_REQS))
1754 void xenvif_unmap_frontend_rings(struct xenvif *vif)
1757 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1760 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1764 int xenvif_map_frontend_rings(struct xenvif *vif,
1765 grant_ref_t tx_ring_ref,
1766 grant_ref_t rx_ring_ref)
1769 struct xen_netif_tx_sring *txs;
1770 struct xen_netif_rx_sring *rxs;
1774 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1775 tx_ring_ref, &addr);
1779 txs = (struct xen_netif_tx_sring *)addr;
1780 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1782 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1783 rx_ring_ref, &addr);
1787 rxs = (struct xen_netif_rx_sring *)addr;
1788 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1790 vif->rx_req_cons_peek = 0;
1795 xenvif_unmap_frontend_rings(vif);
1799 int xenvif_kthread(void *data)
1801 struct xenvif *vif = data;
1803 while (!kthread_should_stop()) {
1804 wait_event_interruptible(vif->wq,
1805 rx_work_todo(vif) ||
1806 kthread_should_stop());
1807 if (kthread_should_stop())
1810 if (rx_work_todo(vif))
1811 xenvif_rx_action(vif);
1819 static int __init netback_init(void)
1826 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1827 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1828 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1829 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1832 rc = xenvif_xenbus_init();
1842 module_init(netback_init);
1844 static void __exit netback_fini(void)
1846 xenvif_xenbus_fini();
1848 module_exit(netback_fini);
1850 MODULE_LICENSE("Dual BSD/GPL");
1851 MODULE_ALIAS("xen-backend:vif");