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 bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
145 prod = vif->rx.sring->req_prod;
146 cons = vif->rx.req_cons;
148 if (prod - cons >= needed)
151 vif->rx.sring->req_event = prod + 1;
153 /* Make sure event is visible before we check prod
157 } while (vif->rx.sring->req_prod != prod);
163 * Returns true if we should start a new receive buffer instead of
164 * adding 'size' bytes to a buffer which currently contains 'offset'
167 static bool start_new_rx_buffer(int offset, unsigned long size, int head)
169 /* simple case: we have completely filled the current buffer. */
170 if (offset == MAX_BUFFER_OFFSET)
174 * complex case: start a fresh buffer if the current frag
175 * would overflow the current buffer but only if:
176 * (i) this frag would fit completely in the next buffer
177 * and (ii) there is already some data in the current buffer
178 * and (iii) this is not the head buffer.
181 * - (i) stops us splitting a frag into two copies
182 * unless the frag is too large for a single buffer.
183 * - (ii) stops us from leaving a buffer pointlessly empty.
184 * - (iii) stops us leaving the first buffer
185 * empty. Strictly speaking this is already covered
186 * by (ii) but is explicitly checked because
187 * netfront relies on the first buffer being
188 * non-empty and can crash otherwise.
190 * This means we will effectively linearise small
191 * frags but do not needlessly split large buffers
192 * into multiple copies tend to give large frags their
193 * own buffers as before.
195 if ((offset + size > MAX_BUFFER_OFFSET) &&
196 (size <= MAX_BUFFER_OFFSET) && offset && !head)
202 struct netrx_pending_operations {
203 unsigned copy_prod, copy_cons;
204 unsigned meta_prod, meta_cons;
205 struct gnttab_copy *copy;
206 struct xenvif_rx_meta *meta;
208 grant_ref_t copy_gref;
211 static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
212 struct netrx_pending_operations *npo)
214 struct xenvif_rx_meta *meta;
215 struct xen_netif_rx_request *req;
217 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
219 meta = npo->meta + npo->meta_prod++;
220 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
226 npo->copy_gref = req->gref;
232 * Set up the grant operations for this fragment. If it's a flipping
233 * interface, we also set up the unmap request from here.
235 static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
236 struct netrx_pending_operations *npo,
237 struct page *page, unsigned long size,
238 unsigned long offset, int *head)
240 struct gnttab_copy *copy_gop;
241 struct xenvif_rx_meta *meta;
245 /* Data must not cross a page boundary. */
246 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
248 meta = npo->meta + npo->meta_prod - 1;
250 /* Skip unused frames from start of page */
251 page += offset >> PAGE_SHIFT;
252 offset &= ~PAGE_MASK;
255 BUG_ON(offset >= PAGE_SIZE);
256 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
258 bytes = PAGE_SIZE - offset;
263 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
265 * Netfront requires there to be some data in the head
270 meta = get_next_rx_buffer(vif, npo);
273 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
274 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
276 copy_gop = npo->copy + npo->copy_prod++;
277 copy_gop->flags = GNTCOPY_dest_gref;
278 copy_gop->len = bytes;
280 copy_gop->source.domid = DOMID_SELF;
281 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
282 copy_gop->source.offset = offset;
284 copy_gop->dest.domid = vif->domid;
285 copy_gop->dest.offset = npo->copy_off;
286 copy_gop->dest.u.ref = npo->copy_gref;
288 npo->copy_off += bytes;
295 if (offset == PAGE_SIZE && size) {
296 BUG_ON(!PageCompound(page));
301 /* Leave a gap for the GSO descriptor. */
302 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
303 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
304 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
305 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
307 gso_type = XEN_NETIF_GSO_TYPE_NONE;
309 if (*head && ((1 << gso_type) & vif->gso_mask))
312 *head = 0; /* There must be something in this buffer now. */
318 * Prepare an SKB to be transmitted to the frontend.
320 * This function is responsible for allocating grant operations, meta
323 * It returns the number of meta structures consumed. The number of
324 * ring slots used is always equal to the number of meta slots used
325 * plus the number of GSO descriptors used. Currently, we use either
326 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
327 * frontend-side LRO).
329 static int xenvif_gop_skb(struct sk_buff *skb,
330 struct netrx_pending_operations *npo)
332 struct xenvif *vif = netdev_priv(skb->dev);
333 int nr_frags = skb_shinfo(skb)->nr_frags;
335 struct xen_netif_rx_request *req;
336 struct xenvif_rx_meta *meta;
343 old_meta_prod = npo->meta_prod;
345 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
346 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
347 gso_size = skb_shinfo(skb)->gso_size;
348 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
349 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
350 gso_size = skb_shinfo(skb)->gso_size;
352 gso_type = XEN_NETIF_GSO_TYPE_NONE;
356 /* Set up a GSO prefix descriptor, if necessary */
357 if ((1 << gso_type) & vif->gso_prefix_mask) {
358 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
359 meta = npo->meta + npo->meta_prod++;
360 meta->gso_type = gso_type;
361 meta->gso_size = gso_size;
366 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
367 meta = npo->meta + npo->meta_prod++;
369 if ((1 << gso_type) & vif->gso_mask) {
370 meta->gso_type = gso_type;
371 meta->gso_size = gso_size;
373 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
380 npo->copy_gref = req->gref;
383 while (data < skb_tail_pointer(skb)) {
384 unsigned int offset = offset_in_page(data);
385 unsigned int len = PAGE_SIZE - offset;
387 if (data + len > skb_tail_pointer(skb))
388 len = skb_tail_pointer(skb) - data;
390 xenvif_gop_frag_copy(vif, skb, npo,
391 virt_to_page(data), len, offset, &head);
395 for (i = 0; i < nr_frags; i++) {
396 xenvif_gop_frag_copy(vif, skb, npo,
397 skb_frag_page(&skb_shinfo(skb)->frags[i]),
398 skb_frag_size(&skb_shinfo(skb)->frags[i]),
399 skb_shinfo(skb)->frags[i].page_offset,
403 return npo->meta_prod - old_meta_prod;
407 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
408 * used to set up the operations on the top of
409 * netrx_pending_operations, which have since been done. Check that
410 * they didn't give any errors and advance over them.
412 static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
413 struct netrx_pending_operations *npo)
415 struct gnttab_copy *copy_op;
416 int status = XEN_NETIF_RSP_OKAY;
419 for (i = 0; i < nr_meta_slots; i++) {
420 copy_op = npo->copy + npo->copy_cons++;
421 if (copy_op->status != GNTST_okay) {
423 "Bad status %d from copy to DOM%d.\n",
424 copy_op->status, vif->domid);
425 status = XEN_NETIF_RSP_ERROR;
432 static void xenvif_add_frag_responses(struct xenvif *vif, int status,
433 struct xenvif_rx_meta *meta,
437 unsigned long offset;
439 /* No fragments used */
440 if (nr_meta_slots <= 1)
445 for (i = 0; i < nr_meta_slots; i++) {
447 if (i == nr_meta_slots - 1)
450 flags = XEN_NETRXF_more_data;
453 make_rx_response(vif, meta[i].id, status, offset,
454 meta[i].size, flags);
458 struct xenvif_rx_cb {
462 #define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
464 void xenvif_kick_thread(struct xenvif *vif)
469 static void xenvif_rx_action(struct xenvif *vif)
473 struct xen_netif_rx_response *resp;
474 struct sk_buff_head rxq;
478 unsigned long offset;
479 bool need_to_notify = false;
481 struct netrx_pending_operations npo = {
482 .copy = vif->grant_copy_op,
486 skb_queue_head_init(&rxq);
488 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
489 RING_IDX max_slots_needed;
492 /* We need a cheap worse case estimate for the number of
496 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
499 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
501 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
502 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
504 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
505 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
508 /* If the skb may not fit then bail out now */
509 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
510 skb_queue_head(&vif->rx_queue, skb);
511 need_to_notify = true;
512 vif->rx_last_skb_slots = max_slots_needed;
515 vif->rx_last_skb_slots = 0;
517 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo);
518 BUG_ON(XENVIF_RX_CB(skb)->meta_slots_used > max_slots_needed);
520 __skb_queue_tail(&rxq, skb);
523 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
528 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
529 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
531 while ((skb = __skb_dequeue(&rxq)) != NULL) {
533 if ((1 << vif->meta[npo.meta_cons].gso_type) &
534 vif->gso_prefix_mask) {
535 resp = RING_GET_RESPONSE(&vif->rx,
536 vif->rx.rsp_prod_pvt++);
538 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
540 resp->offset = vif->meta[npo.meta_cons].gso_size;
541 resp->id = vif->meta[npo.meta_cons].id;
542 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
545 XENVIF_RX_CB(skb)->meta_slots_used--;
549 vif->dev->stats.tx_bytes += skb->len;
550 vif->dev->stats.tx_packets++;
552 status = xenvif_check_gop(vif,
553 XENVIF_RX_CB(skb)->meta_slots_used,
556 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
559 flags = XEN_NETRXF_more_data;
561 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
562 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
563 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
564 /* remote but checksummed. */
565 flags |= XEN_NETRXF_data_validated;
568 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
570 vif->meta[npo.meta_cons].size,
573 if ((1 << vif->meta[npo.meta_cons].gso_type) &
575 struct xen_netif_extra_info *gso =
576 (struct xen_netif_extra_info *)
577 RING_GET_RESPONSE(&vif->rx,
578 vif->rx.rsp_prod_pvt++);
580 resp->flags |= XEN_NETRXF_extra_info;
582 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
583 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
585 gso->u.gso.features = 0;
587 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
591 xenvif_add_frag_responses(vif, status,
592 vif->meta + npo.meta_cons + 1,
593 XENVIF_RX_CB(skb)->meta_slots_used);
595 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
597 need_to_notify |= !!ret;
599 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
605 notify_remote_via_irq(vif->rx_irq);
608 void xenvif_check_rx_xenvif(struct xenvif *vif)
612 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
615 napi_schedule(&vif->napi);
618 static void tx_add_credit(struct xenvif *vif)
620 unsigned long max_burst, max_credit;
623 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
624 * Otherwise the interface can seize up due to insufficient credit.
626 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
627 max_burst = min(max_burst, 131072UL);
628 max_burst = max(max_burst, vif->credit_bytes);
630 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
631 max_credit = vif->remaining_credit + vif->credit_bytes;
632 if (max_credit < vif->remaining_credit)
633 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
635 vif->remaining_credit = min(max_credit, max_burst);
638 static void tx_credit_callback(unsigned long data)
640 struct xenvif *vif = (struct xenvif *)data;
642 xenvif_check_rx_xenvif(vif);
645 static void xenvif_tx_err(struct xenvif *vif,
646 struct xen_netif_tx_request *txp, RING_IDX end)
648 RING_IDX cons = vif->tx.req_cons;
651 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
654 txp = RING_GET_REQUEST(&vif->tx, cons++);
656 vif->tx.req_cons = cons;
659 static void xenvif_fatal_tx_err(struct xenvif *vif)
661 netdev_err(vif->dev, "fatal error; disabling device\n");
662 xenvif_carrier_off(vif);
665 static int xenvif_count_requests(struct xenvif *vif,
666 struct xen_netif_tx_request *first,
667 struct xen_netif_tx_request *txp,
670 RING_IDX cons = vif->tx.req_cons;
675 if (!(first->flags & XEN_NETTXF_more_data))
679 struct xen_netif_tx_request dropped_tx = { 0 };
681 if (slots >= work_to_do) {
683 "Asked for %d slots but exceeds this limit\n",
685 xenvif_fatal_tx_err(vif);
689 /* This guest is really using too many slots and
690 * considered malicious.
692 if (unlikely(slots >= fatal_skb_slots)) {
694 "Malicious frontend using %d slots, threshold %u\n",
695 slots, fatal_skb_slots);
696 xenvif_fatal_tx_err(vif);
700 /* Xen network protocol had implicit dependency on
701 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
702 * the historical MAX_SKB_FRAGS value 18 to honor the
703 * same behavior as before. Any packet using more than
704 * 18 slots but less than fatal_skb_slots slots is
707 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
710 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
711 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
718 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
721 /* If the guest submitted a frame >= 64 KiB then
722 * first->size overflowed and following slots will
723 * appear to be larger than the frame.
725 * This cannot be fatal error as there are buggy
726 * frontends that do this.
728 * Consume all slots and drop the packet.
730 if (!drop_err && txp->size > first->size) {
733 "Invalid tx request, slot size %u > remaining size %u\n",
734 txp->size, first->size);
738 first->size -= txp->size;
741 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
742 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
743 txp->offset, txp->size);
744 xenvif_fatal_tx_err(vif);
748 more_data = txp->flags & XEN_NETTXF_more_data;
756 xenvif_tx_err(vif, first, cons + slots);
763 static struct page *xenvif_alloc_page(struct xenvif *vif,
768 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
771 vif->mmap_pages[pending_idx] = page;
777 struct xenvif_tx_cb {
781 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
783 static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
785 struct xen_netif_tx_request *txp,
786 struct gnttab_copy *gop)
788 struct skb_shared_info *shinfo = skb_shinfo(skb);
789 skb_frag_t *frags = shinfo->frags;
790 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
794 pending_ring_idx_t index, start_idx = 0;
796 unsigned int nr_slots;
797 struct pending_tx_info *first = NULL;
799 /* At this point shinfo->nr_frags is in fact the number of
800 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
802 nr_slots = shinfo->nr_frags;
804 /* Skip first skb fragment if it is on same page as header fragment. */
805 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
807 /* Coalesce tx requests, at this point the packet passed in
808 * should be <= 64K. Any packets larger than 64K have been
809 * handled in xenvif_count_requests().
811 for (shinfo->nr_frags = slot = start; slot < nr_slots;
812 shinfo->nr_frags++) {
813 struct pending_tx_info *pending_tx_info =
814 vif->pending_tx_info;
816 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
822 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
823 gop->flags = GNTCOPY_source_gref;
825 gop->source.u.ref = txp->gref;
826 gop->source.domid = vif->domid;
827 gop->source.offset = txp->offset;
829 gop->dest.domid = DOMID_SELF;
831 gop->dest.offset = dst_offset;
832 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
834 if (dst_offset + txp->size > PAGE_SIZE) {
835 /* This page can only merge a portion
836 * of tx request. Do not increment any
837 * pointer / counter here. The txp
838 * will be dealt with in future
839 * rounds, eventually hitting the
842 gop->len = PAGE_SIZE - dst_offset;
843 txp->offset += gop->len;
844 txp->size -= gop->len;
845 dst_offset += gop->len; /* quit loop */
847 /* This tx request can be merged in the page */
848 gop->len = txp->size;
849 dst_offset += gop->len;
851 index = pending_index(vif->pending_cons++);
853 pending_idx = vif->pending_ring[index];
855 memcpy(&pending_tx_info[pending_idx].req, txp,
858 /* Poison these fields, corresponding
859 * fields for head tx req will be set
860 * to correct values after the loop.
862 vif->mmap_pages[pending_idx] = (void *)(~0UL);
863 pending_tx_info[pending_idx].head =
864 INVALID_PENDING_RING_IDX;
867 first = &pending_tx_info[pending_idx];
869 head_idx = pending_idx;
879 first->req.offset = 0;
880 first->req.size = dst_offset;
881 first->head = start_idx;
882 vif->mmap_pages[head_idx] = page;
883 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
886 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
890 /* Unwind, freeing all pages and sending error responses. */
891 while (shinfo->nr_frags-- > start) {
892 xenvif_idx_release(vif,
893 frag_get_pending_idx(&frags[shinfo->nr_frags]),
894 XEN_NETIF_RSP_ERROR);
896 /* The head too, if necessary. */
898 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
903 static int xenvif_tx_check_gop(struct xenvif *vif,
905 struct gnttab_copy **gopp)
907 struct gnttab_copy *gop = *gopp;
908 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
909 struct skb_shared_info *shinfo = skb_shinfo(skb);
910 struct pending_tx_info *tx_info;
911 int nr_frags = shinfo->nr_frags;
913 u16 peek; /* peek into next tx request */
915 /* Check status of header. */
918 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
920 /* Skip first skb fragment if it is on same page as header fragment. */
921 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
923 for (i = start; i < nr_frags; i++) {
925 pending_ring_idx_t head;
927 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
928 tx_info = &vif->pending_tx_info[pending_idx];
929 head = tx_info->head;
931 /* Check error status: if okay then remember grant handle. */
933 newerr = (++gop)->status;
936 peek = vif->pending_ring[pending_index(++head)];
937 } while (!pending_tx_is_head(vif, peek));
939 if (likely(!newerr)) {
940 /* Had a previous error? Invalidate this fragment. */
942 xenvif_idx_release(vif, pending_idx,
947 /* Error on this fragment: respond to client with an error. */
948 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
950 /* Not the first error? Preceding frags already invalidated. */
954 /* First error: invalidate header and preceding fragments. */
955 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
956 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
957 for (j = start; j < i; j++) {
958 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
959 xenvif_idx_release(vif, pending_idx,
963 /* Remember the error: invalidate all subsequent fragments. */
971 static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
973 struct skb_shared_info *shinfo = skb_shinfo(skb);
974 int nr_frags = shinfo->nr_frags;
977 for (i = 0; i < nr_frags; i++) {
978 skb_frag_t *frag = shinfo->frags + i;
979 struct xen_netif_tx_request *txp;
983 pending_idx = frag_get_pending_idx(frag);
985 txp = &vif->pending_tx_info[pending_idx].req;
986 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
987 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
988 skb->len += txp->size;
989 skb->data_len += txp->size;
990 skb->truesize += txp->size;
992 /* Take an extra reference to offset xenvif_idx_release */
993 get_page(vif->mmap_pages[pending_idx]);
994 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
998 static int xenvif_get_extras(struct xenvif *vif,
999 struct xen_netif_extra_info *extras,
1002 struct xen_netif_extra_info extra;
1003 RING_IDX cons = vif->tx.req_cons;
1006 if (unlikely(work_to_do-- <= 0)) {
1007 netdev_err(vif->dev, "Missing extra info\n");
1008 xenvif_fatal_tx_err(vif);
1012 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1014 if (unlikely(!extra.type ||
1015 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1016 vif->tx.req_cons = ++cons;
1017 netdev_err(vif->dev,
1018 "Invalid extra type: %d\n", extra.type);
1019 xenvif_fatal_tx_err(vif);
1023 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1024 vif->tx.req_cons = ++cons;
1025 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1030 static int xenvif_set_skb_gso(struct xenvif *vif,
1031 struct sk_buff *skb,
1032 struct xen_netif_extra_info *gso)
1034 if (!gso->u.gso.size) {
1035 netdev_err(vif->dev, "GSO size must not be zero.\n");
1036 xenvif_fatal_tx_err(vif);
1040 switch (gso->u.gso.type) {
1041 case XEN_NETIF_GSO_TYPE_TCPV4:
1042 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1044 case XEN_NETIF_GSO_TYPE_TCPV6:
1045 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1048 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1049 xenvif_fatal_tx_err(vif);
1053 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1054 /* gso_segs will be calculated later */
1059 static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1061 bool recalculate_partial_csum = false;
1063 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1064 * peers can fail to set NETRXF_csum_blank when sending a GSO
1065 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1066 * recalculate the partial checksum.
1068 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1069 vif->rx_gso_checksum_fixup++;
1070 skb->ip_summed = CHECKSUM_PARTIAL;
1071 recalculate_partial_csum = true;
1074 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1075 if (skb->ip_summed != CHECKSUM_PARTIAL)
1078 return skb_checksum_setup(skb, recalculate_partial_csum);
1081 static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1083 u64 now = get_jiffies_64();
1084 u64 next_credit = vif->credit_window_start +
1085 msecs_to_jiffies(vif->credit_usec / 1000);
1087 /* Timer could already be pending in rare cases. */
1088 if (timer_pending(&vif->credit_timeout))
1091 /* Passed the point where we can replenish credit? */
1092 if (time_after_eq64(now, next_credit)) {
1093 vif->credit_window_start = now;
1097 /* Still too big to send right now? Set a callback. */
1098 if (size > vif->remaining_credit) {
1099 vif->credit_timeout.data =
1101 vif->credit_timeout.function =
1103 mod_timer(&vif->credit_timeout,
1105 vif->credit_window_start = next_credit;
1113 static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
1115 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
1116 struct sk_buff *skb;
1119 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1120 < MAX_PENDING_REQS) &&
1121 (skb_queue_len(&vif->tx_queue) < budget)) {
1122 struct xen_netif_tx_request txreq;
1123 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1125 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1129 unsigned int data_len;
1130 pending_ring_idx_t index;
1132 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1133 XEN_NETIF_TX_RING_SIZE) {
1134 netdev_err(vif->dev,
1135 "Impossible number of requests. "
1136 "req_prod %d, req_cons %d, size %ld\n",
1137 vif->tx.sring->req_prod, vif->tx.req_cons,
1138 XEN_NETIF_TX_RING_SIZE);
1139 xenvif_fatal_tx_err(vif);
1143 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
1147 idx = vif->tx.req_cons;
1148 rmb(); /* Ensure that we see the request before we copy it. */
1149 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1151 /* Credit-based scheduling. */
1152 if (txreq.size > vif->remaining_credit &&
1153 tx_credit_exceeded(vif, txreq.size))
1156 vif->remaining_credit -= txreq.size;
1159 vif->tx.req_cons = ++idx;
1161 memset(extras, 0, sizeof(extras));
1162 if (txreq.flags & XEN_NETTXF_extra_info) {
1163 work_to_do = xenvif_get_extras(vif, extras,
1165 idx = vif->tx.req_cons;
1166 if (unlikely(work_to_do < 0))
1170 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
1171 if (unlikely(ret < 0))
1176 if (unlikely(txreq.size < ETH_HLEN)) {
1177 netdev_dbg(vif->dev,
1178 "Bad packet size: %d\n", txreq.size);
1179 xenvif_tx_err(vif, &txreq, idx);
1183 /* No crossing a page as the payload mustn't fragment. */
1184 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1185 netdev_err(vif->dev,
1186 "txreq.offset: %x, size: %u, end: %lu\n",
1187 txreq.offset, txreq.size,
1188 (txreq.offset&~PAGE_MASK) + txreq.size);
1189 xenvif_fatal_tx_err(vif);
1193 index = pending_index(vif->pending_cons);
1194 pending_idx = vif->pending_ring[index];
1196 data_len = (txreq.size > PKT_PROT_LEN &&
1197 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1198 PKT_PROT_LEN : txreq.size;
1200 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1201 GFP_ATOMIC | __GFP_NOWARN);
1202 if (unlikely(skb == NULL)) {
1203 netdev_dbg(vif->dev,
1204 "Can't allocate a skb in start_xmit.\n");
1205 xenvif_tx_err(vif, &txreq, idx);
1209 /* Packets passed to netif_rx() must have some headroom. */
1210 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1212 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1213 struct xen_netif_extra_info *gso;
1214 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1216 if (xenvif_set_skb_gso(vif, skb, gso)) {
1217 /* Failure in xenvif_set_skb_gso is fatal. */
1223 /* XXX could copy straight to head */
1224 page = xenvif_alloc_page(vif, pending_idx);
1227 xenvif_tx_err(vif, &txreq, idx);
1231 gop->source.u.ref = txreq.gref;
1232 gop->source.domid = vif->domid;
1233 gop->source.offset = txreq.offset;
1235 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1236 gop->dest.domid = DOMID_SELF;
1237 gop->dest.offset = txreq.offset;
1239 gop->len = txreq.size;
1240 gop->flags = GNTCOPY_source_gref;
1244 memcpy(&vif->pending_tx_info[pending_idx].req,
1245 &txreq, sizeof(txreq));
1246 vif->pending_tx_info[pending_idx].head = index;
1247 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
1249 __skb_put(skb, data_len);
1251 skb_shinfo(skb)->nr_frags = ret;
1252 if (data_len < txreq.size) {
1253 skb_shinfo(skb)->nr_frags++;
1254 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1257 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1258 INVALID_PENDING_IDX);
1261 vif->pending_cons++;
1263 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
1264 if (request_gop == NULL) {
1266 xenvif_tx_err(vif, &txreq, idx);
1271 __skb_queue_tail(&vif->tx_queue, skb);
1273 vif->tx.req_cons = idx;
1275 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
1279 return gop - vif->tx_copy_ops;
1283 static int xenvif_tx_submit(struct xenvif *vif)
1285 struct gnttab_copy *gop = vif->tx_copy_ops;
1286 struct sk_buff *skb;
1289 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
1290 struct xen_netif_tx_request *txp;
1294 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
1295 txp = &vif->pending_tx_info[pending_idx].req;
1297 /* Check the remap error code. */
1298 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
1299 netdev_dbg(vif->dev, "netback grant failed.\n");
1300 skb_shinfo(skb)->nr_frags = 0;
1305 data_len = skb->len;
1307 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
1309 if (data_len < txp->size) {
1310 /* Append the packet payload as a fragment. */
1311 txp->offset += data_len;
1312 txp->size -= data_len;
1314 /* Schedule a response immediately. */
1315 xenvif_idx_release(vif, pending_idx,
1316 XEN_NETIF_RSP_OKAY);
1319 if (txp->flags & XEN_NETTXF_csum_blank)
1320 skb->ip_summed = CHECKSUM_PARTIAL;
1321 else if (txp->flags & XEN_NETTXF_data_validated)
1322 skb->ip_summed = CHECKSUM_UNNECESSARY;
1324 xenvif_fill_frags(vif, skb);
1326 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
1327 int target = min_t(int, skb->len, PKT_PROT_LEN);
1328 __pskb_pull_tail(skb, target - skb_headlen(skb));
1331 skb->dev = vif->dev;
1332 skb->protocol = eth_type_trans(skb, skb->dev);
1333 skb_reset_network_header(skb);
1335 if (checksum_setup(vif, skb)) {
1336 netdev_dbg(vif->dev,
1337 "Can't setup checksum in net_tx_action\n");
1342 skb_probe_transport_header(skb, 0);
1344 /* If the packet is GSO then we will have just set up the
1345 * transport header offset in checksum_setup so it's now
1346 * straightforward to calculate gso_segs.
1348 if (skb_is_gso(skb)) {
1349 int mss = skb_shinfo(skb)->gso_size;
1350 int hdrlen = skb_transport_header(skb) -
1351 skb_mac_header(skb) +
1354 skb_shinfo(skb)->gso_segs =
1355 DIV_ROUND_UP(skb->len - hdrlen, mss);
1358 vif->dev->stats.rx_bytes += skb->len;
1359 vif->dev->stats.rx_packets++;
1363 netif_receive_skb(skb);
1369 /* Called after netfront has transmitted */
1370 int xenvif_tx_action(struct xenvif *vif, int budget)
1375 if (unlikely(!tx_work_todo(vif)))
1378 nr_gops = xenvif_tx_build_gops(vif, budget);
1383 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
1385 work_done = xenvif_tx_submit(vif);
1390 static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1393 struct pending_tx_info *pending_tx_info;
1394 pending_ring_idx_t head;
1395 u16 peek; /* peek into next tx request */
1397 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
1399 /* Already complete? */
1400 if (vif->mmap_pages[pending_idx] == NULL)
1403 pending_tx_info = &vif->pending_tx_info[pending_idx];
1405 head = pending_tx_info->head;
1407 BUG_ON(!pending_tx_is_head(vif, head));
1408 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
1411 pending_ring_idx_t index;
1412 pending_ring_idx_t idx = pending_index(head);
1413 u16 info_idx = vif->pending_ring[idx];
1415 pending_tx_info = &vif->pending_tx_info[info_idx];
1416 make_tx_response(vif, &pending_tx_info->req, status);
1418 /* Setting any number other than
1419 * INVALID_PENDING_RING_IDX indicates this slot is
1420 * starting a new packet / ending a previous packet.
1422 pending_tx_info->head = 0;
1424 index = pending_index(vif->pending_prod++);
1425 vif->pending_ring[index] = vif->pending_ring[info_idx];
1427 peek = vif->pending_ring[pending_index(++head)];
1429 } while (!pending_tx_is_head(vif, peek));
1431 put_page(vif->mmap_pages[pending_idx]);
1432 vif->mmap_pages[pending_idx] = NULL;
1436 static void make_tx_response(struct xenvif *vif,
1437 struct xen_netif_tx_request *txp,
1440 RING_IDX i = vif->tx.rsp_prod_pvt;
1441 struct xen_netif_tx_response *resp;
1444 resp = RING_GET_RESPONSE(&vif->tx, i);
1448 if (txp->flags & XEN_NETTXF_extra_info)
1449 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1451 vif->tx.rsp_prod_pvt = ++i;
1452 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1454 notify_remote_via_irq(vif->tx_irq);
1457 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1464 RING_IDX i = vif->rx.rsp_prod_pvt;
1465 struct xen_netif_rx_response *resp;
1467 resp = RING_GET_RESPONSE(&vif->rx, i);
1468 resp->offset = offset;
1469 resp->flags = flags;
1471 resp->status = (s16)size;
1473 resp->status = (s16)st;
1475 vif->rx.rsp_prod_pvt = ++i;
1480 static inline int rx_work_todo(struct xenvif *vif)
1482 return !skb_queue_empty(&vif->rx_queue) &&
1483 xenvif_rx_ring_slots_available(vif, vif->rx_last_skb_slots);
1486 static inline int tx_work_todo(struct xenvif *vif)
1489 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1490 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1491 < MAX_PENDING_REQS))
1497 void xenvif_unmap_frontend_rings(struct xenvif *vif)
1500 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1503 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1507 int xenvif_map_frontend_rings(struct xenvif *vif,
1508 grant_ref_t tx_ring_ref,
1509 grant_ref_t rx_ring_ref)
1512 struct xen_netif_tx_sring *txs;
1513 struct xen_netif_rx_sring *rxs;
1517 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1518 tx_ring_ref, &addr);
1522 txs = (struct xen_netif_tx_sring *)addr;
1523 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1525 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1526 rx_ring_ref, &addr);
1530 rxs = (struct xen_netif_rx_sring *)addr;
1531 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1536 xenvif_unmap_frontend_rings(vif);
1540 void xenvif_stop_queue(struct xenvif *vif)
1542 if (!vif->can_queue)
1545 netif_stop_queue(vif->dev);
1548 static void xenvif_start_queue(struct xenvif *vif)
1550 if (xenvif_schedulable(vif))
1551 netif_wake_queue(vif->dev);
1554 int xenvif_kthread(void *data)
1556 struct xenvif *vif = data;
1557 struct sk_buff *skb;
1559 while (!kthread_should_stop()) {
1560 wait_event_interruptible(vif->wq,
1561 rx_work_todo(vif) ||
1562 kthread_should_stop());
1563 if (kthread_should_stop())
1566 if (!skb_queue_empty(&vif->rx_queue))
1567 xenvif_rx_action(vif);
1569 if (skb_queue_empty(&vif->rx_queue) &&
1570 netif_queue_stopped(vif->dev))
1571 xenvif_start_queue(vif);
1576 /* Bin any remaining skbs */
1577 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1583 static int __init netback_init(void)
1590 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1591 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1592 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1593 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1596 rc = xenvif_xenbus_init();
1606 module_init(netback_init);
1608 static void __exit netback_fini(void)
1610 xenvif_xenbus_fini();
1612 module_exit(netback_fini);
1614 MODULE_LICENSE("Dual BSD/GPL");
1615 MODULE_ALIAS("xen-backend:vif");