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>
40 #include <linux/highmem.h>
45 #include <xen/events.h>
46 #include <xen/interface/memory.h>
49 #include <asm/xen/hypercall.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 = true;
56 module_param(separate_tx_rx_irq, bool, 0644);
58 /* The time that packets can stay on the guest Rx internal queue
59 * before they are dropped.
61 unsigned int rx_drain_timeout_msecs = 10000;
62 module_param(rx_drain_timeout_msecs, uint, 0444);
64 /* The length of time before the frontend is considered unresponsive
65 * because it isn't providing Rx slots.
67 unsigned int rx_stall_timeout_msecs = 60000;
68 module_param(rx_stall_timeout_msecs, uint, 0444);
70 #define MAX_QUEUES_DEFAULT 8
71 unsigned int xenvif_max_queues;
72 module_param_named(max_queues, xenvif_max_queues, uint, 0644);
73 MODULE_PARM_DESC(max_queues,
74 "Maximum number of queues per virtual interface");
77 * This is the maximum slots a skb can have. If a guest sends a skb
78 * which exceeds this limit it is considered malicious.
80 #define FATAL_SKB_SLOTS_DEFAULT 20
81 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
82 module_param(fatal_skb_slots, uint, 0444);
84 /* The amount to copy out of the first guest Tx slot into the skb's
85 * linear area. If the first slot has more data, it will be mapped
86 * and put into the first frag.
88 * This is sized to avoid pulling headers from the frags for most
91 #define XEN_NETBACK_TX_COPY_LEN 128
94 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
97 static void make_tx_response(struct xenvif_queue *queue,
98 struct xen_netif_tx_request *txp,
100 static void push_tx_responses(struct xenvif_queue *queue);
102 static inline int tx_work_todo(struct xenvif_queue *queue);
104 static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
111 static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
114 return page_to_pfn(queue->mmap_pages[idx]);
117 static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
120 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
123 #define callback_param(vif, pending_idx) \
124 (vif->pending_tx_info[pending_idx].callback_struct)
126 /* Find the containing VIF's structure from a pointer in pending_tx_info array
128 static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
130 u16 pending_idx = ubuf->desc;
131 struct pending_tx_info *temp =
132 container_of(ubuf, struct pending_tx_info, callback_struct);
133 return container_of(temp - pending_idx,
138 static u16 frag_get_pending_idx(skb_frag_t *frag)
140 return (u16)frag->page_offset;
143 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
145 frag->page_offset = pending_idx;
148 static inline pending_ring_idx_t pending_index(unsigned i)
150 return i & (MAX_PENDING_REQS-1);
153 static int xenvif_rx_ring_slots_needed(struct xenvif *vif)
156 return DIV_ROUND_UP(vif->dev->gso_max_size, XEN_PAGE_SIZE) + 1;
158 return DIV_ROUND_UP(vif->dev->mtu, XEN_PAGE_SIZE);
161 static bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue)
166 needed = xenvif_rx_ring_slots_needed(queue->vif);
169 prod = queue->rx.sring->req_prod;
170 cons = queue->rx.req_cons;
172 if (prod - cons >= needed)
175 queue->rx.sring->req_event = prod + 1;
177 /* Make sure event is visible before we check prod
181 } while (queue->rx.sring->req_prod != prod);
186 void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
190 spin_lock_irqsave(&queue->rx_queue.lock, flags);
192 if (queue->rx_queue_len >= queue->rx_queue_max) {
193 netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
195 queue->vif->dev->stats.rx_dropped++;
197 __skb_queue_tail(&queue->rx_queue, skb);
199 queue->rx_queue_len += skb->len;
202 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
205 static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
209 spin_lock_irq(&queue->rx_queue.lock);
211 skb = __skb_dequeue(&queue->rx_queue);
213 queue->rx_queue_len -= skb->len;
215 spin_unlock_irq(&queue->rx_queue.lock);
220 static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
222 spin_lock_irq(&queue->rx_queue.lock);
224 if (queue->rx_queue_len < queue->rx_queue_max)
225 netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
227 spin_unlock_irq(&queue->rx_queue.lock);
231 static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
234 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
238 static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
243 skb = skb_peek(&queue->rx_queue);
246 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
248 xenvif_rx_dequeue(queue);
250 queue->vif->dev->stats.rx_dropped++;
254 struct netrx_pending_operations {
255 unsigned copy_prod, copy_cons;
256 unsigned meta_prod, meta_cons;
257 struct gnttab_copy *copy;
258 struct xenvif_rx_meta *meta;
260 grant_ref_t copy_gref;
263 static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
264 struct netrx_pending_operations *npo)
266 struct xenvif_rx_meta *meta;
267 struct xen_netif_rx_request req;
269 RING_COPY_REQUEST(&queue->rx, queue->rx.req_cons++, &req);
271 meta = npo->meta + npo->meta_prod++;
272 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
278 npo->copy_gref = req.gref;
283 struct gop_frag_copy {
284 struct xenvif_queue *queue;
285 struct netrx_pending_operations *npo;
286 struct xenvif_rx_meta *meta;
293 static void xenvif_setup_copy_gop(unsigned long gfn,
296 struct gop_frag_copy *info)
298 struct gnttab_copy *copy_gop;
299 struct xen_page_foreign *foreign;
300 /* Convenient aliases */
301 struct xenvif_queue *queue = info->queue;
302 struct netrx_pending_operations *npo = info->npo;
303 struct page *page = info->page;
305 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
307 if (npo->copy_off == MAX_BUFFER_OFFSET)
308 info->meta = get_next_rx_buffer(queue, npo);
310 if (npo->copy_off + *len > MAX_BUFFER_OFFSET)
311 *len = MAX_BUFFER_OFFSET - npo->copy_off;
313 copy_gop = npo->copy + npo->copy_prod++;
314 copy_gop->flags = GNTCOPY_dest_gref;
315 copy_gop->len = *len;
317 foreign = xen_page_foreign(page);
319 copy_gop->source.domid = foreign->domid;
320 copy_gop->source.u.ref = foreign->gref;
321 copy_gop->flags |= GNTCOPY_source_gref;
323 copy_gop->source.domid = DOMID_SELF;
324 copy_gop->source.u.gmfn = gfn;
326 copy_gop->source.offset = offset;
328 copy_gop->dest.domid = queue->vif->domid;
329 copy_gop->dest.offset = npo->copy_off;
330 copy_gop->dest.u.ref = npo->copy_gref;
332 npo->copy_off += *len;
333 info->meta->size += *len;
335 /* Leave a gap for the GSO descriptor. */
336 if (info->head && ((1 << info->gso_type) & queue->vif->gso_mask))
337 queue->rx.req_cons++;
339 info->head = 0; /* There must be something in this buffer now */
342 static void xenvif_gop_frag_copy_grant(unsigned long gfn,
351 xenvif_setup_copy_gop(gfn, offset, &bytes, data);
358 * Set up the grant operations for this fragment. If it's a flipping
359 * interface, we also set up the unmap request from here.
361 static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
362 struct netrx_pending_operations *npo,
363 struct page *page, unsigned long size,
364 unsigned long offset, int *head)
366 struct gop_frag_copy info = {
370 .gso_type = XEN_NETIF_GSO_TYPE_NONE,
374 if (skb_is_gso(skb)) {
375 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
376 info.gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
377 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
378 info.gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
381 /* Data must not cross a page boundary. */
382 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
384 info.meta = npo->meta + npo->meta_prod - 1;
386 /* Skip unused frames from start of page */
387 page += offset >> PAGE_SHIFT;
388 offset &= ~PAGE_MASK;
391 BUG_ON(offset >= PAGE_SIZE);
393 bytes = PAGE_SIZE - offset;
398 gnttab_foreach_grant_in_range(page, offset, bytes,
399 xenvif_gop_frag_copy_grant,
406 BUG_ON(!PageCompound(page));
415 * Prepare an SKB to be transmitted to the frontend.
417 * This function is responsible for allocating grant operations, meta
420 * It returns the number of meta structures consumed. The number of
421 * ring slots used is always equal to the number of meta slots used
422 * plus the number of GSO descriptors used. Currently, we use either
423 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
424 * frontend-side LRO).
426 static int xenvif_gop_skb(struct sk_buff *skb,
427 struct netrx_pending_operations *npo,
428 struct xenvif_queue *queue)
430 struct xenvif *vif = netdev_priv(skb->dev);
431 int nr_frags = skb_shinfo(skb)->nr_frags;
433 struct xen_netif_rx_request req;
434 struct xenvif_rx_meta *meta;
440 old_meta_prod = npo->meta_prod;
442 gso_type = XEN_NETIF_GSO_TYPE_NONE;
443 if (skb_is_gso(skb)) {
444 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
445 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
446 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
447 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
450 /* Set up a GSO prefix descriptor, if necessary */
451 if ((1 << gso_type) & vif->gso_prefix_mask) {
452 RING_COPY_REQUEST(&queue->rx, queue->rx.req_cons++, &req);
453 meta = npo->meta + npo->meta_prod++;
454 meta->gso_type = gso_type;
455 meta->gso_size = skb_shinfo(skb)->gso_size;
460 RING_COPY_REQUEST(&queue->rx, queue->rx.req_cons++, &req);
461 meta = npo->meta + npo->meta_prod++;
463 if ((1 << gso_type) & vif->gso_mask) {
464 meta->gso_type = gso_type;
465 meta->gso_size = skb_shinfo(skb)->gso_size;
467 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
474 npo->copy_gref = req.gref;
477 while (data < skb_tail_pointer(skb)) {
478 unsigned int offset = offset_in_page(data);
479 unsigned int len = PAGE_SIZE - offset;
481 if (data + len > skb_tail_pointer(skb))
482 len = skb_tail_pointer(skb) - data;
484 xenvif_gop_frag_copy(queue, skb, npo,
485 virt_to_page(data), len, offset, &head);
489 for (i = 0; i < nr_frags; i++) {
490 xenvif_gop_frag_copy(queue, skb, npo,
491 skb_frag_page(&skb_shinfo(skb)->frags[i]),
492 skb_frag_size(&skb_shinfo(skb)->frags[i]),
493 skb_shinfo(skb)->frags[i].page_offset,
497 return npo->meta_prod - old_meta_prod;
501 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
502 * used to set up the operations on the top of
503 * netrx_pending_operations, which have since been done. Check that
504 * they didn't give any errors and advance over them.
506 static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
507 struct netrx_pending_operations *npo)
509 struct gnttab_copy *copy_op;
510 int status = XEN_NETIF_RSP_OKAY;
513 for (i = 0; i < nr_meta_slots; i++) {
514 copy_op = npo->copy + npo->copy_cons++;
515 if (copy_op->status != GNTST_okay) {
517 "Bad status %d from copy to DOM%d.\n",
518 copy_op->status, vif->domid);
519 status = XEN_NETIF_RSP_ERROR;
526 static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
527 struct xenvif_rx_meta *meta,
531 unsigned long offset;
533 /* No fragments used */
534 if (nr_meta_slots <= 1)
539 for (i = 0; i < nr_meta_slots; i++) {
541 if (i == nr_meta_slots - 1)
544 flags = XEN_NETRXF_more_data;
547 make_rx_response(queue, meta[i].id, status, offset,
548 meta[i].size, flags);
552 void xenvif_kick_thread(struct xenvif_queue *queue)
557 static void xenvif_rx_action(struct xenvif_queue *queue)
561 struct xen_netif_rx_response *resp;
562 struct sk_buff_head rxq;
566 unsigned long offset;
567 bool need_to_notify = false;
569 struct netrx_pending_operations npo = {
570 .copy = queue->grant_copy_op,
574 skb_queue_head_init(&rxq);
576 while (xenvif_rx_ring_slots_available(queue)
577 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
578 queue->last_rx_time = jiffies;
580 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
582 __skb_queue_tail(&rxq, skb);
585 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
590 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
591 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
593 while ((skb = __skb_dequeue(&rxq)) != NULL) {
595 if ((1 << queue->meta[npo.meta_cons].gso_type) &
596 queue->vif->gso_prefix_mask) {
597 resp = RING_GET_RESPONSE(&queue->rx,
598 queue->rx.rsp_prod_pvt++);
600 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
602 resp->offset = queue->meta[npo.meta_cons].gso_size;
603 resp->id = queue->meta[npo.meta_cons].id;
604 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
607 XENVIF_RX_CB(skb)->meta_slots_used--;
611 queue->stats.tx_bytes += skb->len;
612 queue->stats.tx_packets++;
614 status = xenvif_check_gop(queue->vif,
615 XENVIF_RX_CB(skb)->meta_slots_used,
618 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
621 flags = XEN_NETRXF_more_data;
623 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
624 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
625 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
626 /* remote but checksummed. */
627 flags |= XEN_NETRXF_data_validated;
630 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
632 queue->meta[npo.meta_cons].size,
635 if ((1 << queue->meta[npo.meta_cons].gso_type) &
636 queue->vif->gso_mask) {
637 struct xen_netif_extra_info *gso =
638 (struct xen_netif_extra_info *)
639 RING_GET_RESPONSE(&queue->rx,
640 queue->rx.rsp_prod_pvt++);
642 resp->flags |= XEN_NETRXF_extra_info;
644 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
645 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
647 gso->u.gso.features = 0;
649 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
653 xenvif_add_frag_responses(queue, status,
654 queue->meta + npo.meta_cons + 1,
655 XENVIF_RX_CB(skb)->meta_slots_used);
657 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
659 need_to_notify |= !!ret;
661 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
667 notify_remote_via_irq(queue->rx_irq);
670 void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
674 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
677 napi_schedule(&queue->napi);
678 else if (xenvif_atomic_fetch_andnot(NETBK_TX_EOI | NETBK_COMMON_EOI,
679 &queue->eoi_pending) &
680 (NETBK_TX_EOI | NETBK_COMMON_EOI))
681 xen_irq_lateeoi(queue->tx_irq, 0);
684 static void tx_add_credit(struct xenvif_queue *queue)
686 unsigned long max_burst, max_credit;
689 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
690 * Otherwise the interface can seize up due to insufficient credit.
692 max_burst = max(131072UL, queue->credit_bytes);
694 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
695 max_credit = queue->remaining_credit + queue->credit_bytes;
696 if (max_credit < queue->remaining_credit)
697 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
699 queue->remaining_credit = min(max_credit, max_burst);
700 queue->rate_limited = false;
703 void xenvif_tx_credit_callback(unsigned long data)
705 struct xenvif_queue *queue = (struct xenvif_queue *)data;
706 tx_add_credit(queue);
707 xenvif_napi_schedule_or_enable_events(queue);
710 static void xenvif_tx_err(struct xenvif_queue *queue,
711 struct xen_netif_tx_request *txp, RING_IDX end)
713 RING_IDX cons = queue->tx.req_cons;
717 spin_lock_irqsave(&queue->response_lock, flags);
718 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
719 push_tx_responses(queue);
720 spin_unlock_irqrestore(&queue->response_lock, flags);
723 RING_COPY_REQUEST(&queue->tx, cons++, txp);
725 queue->tx.req_cons = cons;
728 static void xenvif_fatal_tx_err(struct xenvif *vif)
730 netdev_err(vif->dev, "fatal error; disabling device\n");
731 vif->disabled = true;
732 /* Disable the vif from queue 0's kthread */
734 xenvif_kick_thread(&vif->queues[0]);
737 static int xenvif_count_requests(struct xenvif_queue *queue,
738 struct xen_netif_tx_request *first,
739 struct xen_netif_tx_request *txp,
742 RING_IDX cons = queue->tx.req_cons;
747 if (!(first->flags & XEN_NETTXF_more_data))
751 struct xen_netif_tx_request dropped_tx = { 0 };
753 if (slots >= work_to_do) {
754 netdev_err(queue->vif->dev,
755 "Asked for %d slots but exceeds this limit\n",
757 xenvif_fatal_tx_err(queue->vif);
761 /* This guest is really using too many slots and
762 * considered malicious.
764 if (unlikely(slots >= fatal_skb_slots)) {
765 netdev_err(queue->vif->dev,
766 "Malicious frontend using %d slots, threshold %u\n",
767 slots, fatal_skb_slots);
768 xenvif_fatal_tx_err(queue->vif);
772 /* Xen network protocol had implicit dependency on
773 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
774 * the historical MAX_SKB_FRAGS value 18 to honor the
775 * same behavior as before. Any packet using more than
776 * 18 slots but less than fatal_skb_slots slots is
779 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
781 netdev_dbg(queue->vif->dev,
782 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
783 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
790 RING_COPY_REQUEST(&queue->tx, cons + slots, txp);
792 /* If the guest submitted a frame >= 64 KiB then
793 * first->size overflowed and following slots will
794 * appear to be larger than the frame.
796 * This cannot be fatal error as there are buggy
797 * frontends that do this.
799 * Consume all slots and drop the packet.
801 if (!drop_err && txp->size > first->size) {
803 netdev_dbg(queue->vif->dev,
804 "Invalid tx request, slot size %u > remaining size %u\n",
805 txp->size, first->size);
809 first->size -= txp->size;
812 if (unlikely((txp->offset + txp->size) > XEN_PAGE_SIZE)) {
813 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %u, size: %u\n",
814 txp->offset, txp->size);
815 xenvif_fatal_tx_err(queue->vif);
819 more_data = txp->flags & XEN_NETTXF_more_data;
827 xenvif_tx_err(queue, first, cons + slots);
835 struct xenvif_tx_cb {
839 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
841 static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
843 struct xen_netif_tx_request *txp,
844 struct gnttab_map_grant_ref *mop)
846 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
847 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
848 GNTMAP_host_map | GNTMAP_readonly,
849 txp->gref, queue->vif->domid);
851 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
855 static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
857 struct sk_buff *skb =
858 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
859 GFP_ATOMIC | __GFP_NOWARN);
860 if (unlikely(skb == NULL))
863 /* Packets passed to netif_rx() must have some headroom. */
864 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
866 /* Initialize it here to avoid later surprises */
867 skb_shinfo(skb)->destructor_arg = NULL;
872 static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
874 struct xen_netif_tx_request *txp,
875 struct gnttab_map_grant_ref *gop,
876 unsigned int frag_overflow,
877 struct sk_buff *nskb)
879 struct skb_shared_info *shinfo = skb_shinfo(skb);
880 skb_frag_t *frags = shinfo->frags;
881 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
883 pending_ring_idx_t index;
884 unsigned int nr_slots;
886 nr_slots = shinfo->nr_frags;
888 /* Skip first skb fragment if it is on same page as header fragment. */
889 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
891 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
892 shinfo->nr_frags++, txp++, gop++) {
893 index = pending_index(queue->pending_cons++);
894 pending_idx = queue->pending_ring[index];
895 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
896 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
901 shinfo = skb_shinfo(nskb);
902 frags = shinfo->frags;
904 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
905 shinfo->nr_frags++, txp++, gop++) {
906 index = pending_index(queue->pending_cons++);
907 pending_idx = queue->pending_ring[index];
908 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
909 frag_set_pending_idx(&frags[shinfo->nr_frags],
913 skb_shinfo(skb)->frag_list = nskb;
919 static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
921 grant_handle_t handle)
923 if (unlikely(queue->grant_tx_handle[pending_idx] !=
924 NETBACK_INVALID_HANDLE)) {
925 netdev_err(queue->vif->dev,
926 "Trying to overwrite active handle! pending_idx: 0x%x\n",
930 queue->grant_tx_handle[pending_idx] = handle;
933 static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
936 if (unlikely(queue->grant_tx_handle[pending_idx] ==
937 NETBACK_INVALID_HANDLE)) {
938 netdev_err(queue->vif->dev,
939 "Trying to unmap invalid handle! pending_idx: 0x%x\n",
943 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
946 static int xenvif_tx_check_gop(struct xenvif_queue *queue,
948 struct gnttab_map_grant_ref **gopp_map,
949 struct gnttab_copy **gopp_copy)
951 struct gnttab_map_grant_ref *gop_map = *gopp_map;
952 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
953 /* This always points to the shinfo of the skb being checked, which
954 * could be either the first or the one on the frag_list
956 struct skb_shared_info *shinfo = skb_shinfo(skb);
957 /* If this is non-NULL, we are currently checking the frag_list skb, and
958 * this points to the shinfo of the first one
960 struct skb_shared_info *first_shinfo = NULL;
961 int nr_frags = shinfo->nr_frags;
962 const bool sharedslot = nr_frags &&
963 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
966 /* Check status of header. */
967 err = (*gopp_copy)->status;
970 netdev_dbg(queue->vif->dev,
971 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
972 (*gopp_copy)->status,
974 (*gopp_copy)->source.u.ref);
975 /* The first frag might still have this slot mapped */
977 xenvif_idx_release(queue, pending_idx,
978 XEN_NETIF_RSP_ERROR);
983 for (i = 0; i < nr_frags; i++, gop_map++) {
986 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
988 /* Check error status: if okay then remember grant handle. */
989 newerr = gop_map->status;
991 if (likely(!newerr)) {
992 xenvif_grant_handle_set(queue,
995 /* Had a previous error? Invalidate this fragment. */
997 xenvif_idx_unmap(queue, pending_idx);
998 /* If the mapping of the first frag was OK, but
999 * the header's copy failed, and they are
1000 * sharing a slot, send an error
1002 if (i == 0 && !first_shinfo && sharedslot)
1003 xenvif_idx_release(queue, pending_idx,
1004 XEN_NETIF_RSP_ERROR);
1006 xenvif_idx_release(queue, pending_idx,
1007 XEN_NETIF_RSP_OKAY);
1012 /* Error on this fragment: respond to client with an error. */
1013 if (net_ratelimit())
1014 netdev_dbg(queue->vif->dev,
1015 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
1021 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
1023 /* Not the first error? Preceding frags already invalidated. */
1027 /* First error: if the header haven't shared a slot with the
1028 * first frag, release it as well.
1031 xenvif_idx_release(queue,
1032 XENVIF_TX_CB(skb)->pending_idx,
1033 XEN_NETIF_RSP_OKAY);
1035 /* Invalidate preceding fragments of this skb. */
1036 for (j = 0; j < i; j++) {
1037 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1038 xenvif_idx_unmap(queue, pending_idx);
1039 xenvif_idx_release(queue, pending_idx,
1040 XEN_NETIF_RSP_OKAY);
1043 /* And if we found the error while checking the frag_list, unmap
1044 * the first skb's frags
1047 for (j = 0; j < first_shinfo->nr_frags; j++) {
1048 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1049 xenvif_idx_unmap(queue, pending_idx);
1050 xenvif_idx_release(queue, pending_idx,
1051 XEN_NETIF_RSP_OKAY);
1055 /* Remember the error: invalidate all subsequent fragments. */
1059 if (skb_has_frag_list(skb) && !first_shinfo) {
1060 first_shinfo = skb_shinfo(skb);
1061 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
1062 nr_frags = shinfo->nr_frags;
1067 *gopp_map = gop_map;
1071 static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
1073 struct skb_shared_info *shinfo = skb_shinfo(skb);
1074 int nr_frags = shinfo->nr_frags;
1076 u16 prev_pending_idx = INVALID_PENDING_IDX;
1078 for (i = 0; i < nr_frags; i++) {
1079 skb_frag_t *frag = shinfo->frags + i;
1080 struct xen_netif_tx_request *txp;
1084 pending_idx = frag_get_pending_idx(frag);
1086 /* If this is not the first frag, chain it to the previous*/
1087 if (prev_pending_idx == INVALID_PENDING_IDX)
1088 skb_shinfo(skb)->destructor_arg =
1089 &callback_param(queue, pending_idx);
1091 callback_param(queue, prev_pending_idx).ctx =
1092 &callback_param(queue, pending_idx);
1094 callback_param(queue, pending_idx).ctx = NULL;
1095 prev_pending_idx = pending_idx;
1097 txp = &queue->pending_tx_info[pending_idx].req;
1098 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
1099 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1100 skb->len += txp->size;
1101 skb->data_len += txp->size;
1102 skb->truesize += txp->size;
1104 /* Take an extra reference to offset network stack's put_page */
1105 get_page(queue->mmap_pages[pending_idx]);
1109 static int xenvif_get_extras(struct xenvif_queue *queue,
1110 struct xen_netif_extra_info *extras,
1113 struct xen_netif_extra_info extra;
1114 RING_IDX cons = queue->tx.req_cons;
1117 if (unlikely(work_to_do-- <= 0)) {
1118 netdev_err(queue->vif->dev, "Missing extra info\n");
1119 xenvif_fatal_tx_err(queue->vif);
1123 RING_COPY_REQUEST(&queue->tx, cons, &extra);
1124 if (unlikely(!extra.type ||
1125 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1126 queue->tx.req_cons = ++cons;
1127 netdev_err(queue->vif->dev,
1128 "Invalid extra type: %d\n", extra.type);
1129 xenvif_fatal_tx_err(queue->vif);
1133 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1134 queue->tx.req_cons = ++cons;
1135 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1140 static int xenvif_set_skb_gso(struct xenvif *vif,
1141 struct sk_buff *skb,
1142 struct xen_netif_extra_info *gso)
1144 if (!gso->u.gso.size) {
1145 netdev_err(vif->dev, "GSO size must not be zero.\n");
1146 xenvif_fatal_tx_err(vif);
1150 switch (gso->u.gso.type) {
1151 case XEN_NETIF_GSO_TYPE_TCPV4:
1152 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1154 case XEN_NETIF_GSO_TYPE_TCPV6:
1155 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1158 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1159 xenvif_fatal_tx_err(vif);
1163 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1164 /* gso_segs will be calculated later */
1169 static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
1171 bool recalculate_partial_csum = false;
1173 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1174 * peers can fail to set NETRXF_csum_blank when sending a GSO
1175 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1176 * recalculate the partial checksum.
1178 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1179 queue->stats.rx_gso_checksum_fixup++;
1180 skb->ip_summed = CHECKSUM_PARTIAL;
1181 recalculate_partial_csum = true;
1184 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1185 if (skb->ip_summed != CHECKSUM_PARTIAL)
1188 return skb_checksum_setup(skb, recalculate_partial_csum);
1191 static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
1193 u64 now = get_jiffies_64();
1194 u64 next_credit = queue->credit_window_start +
1195 msecs_to_jiffies(queue->credit_usec / 1000);
1197 /* Timer could already be pending in rare cases. */
1198 if (timer_pending(&queue->credit_timeout)) {
1199 queue->rate_limited = true;
1203 /* Passed the point where we can replenish credit? */
1204 if (time_after_eq64(now, next_credit)) {
1205 queue->credit_window_start = now;
1206 tx_add_credit(queue);
1209 /* Still too big to send right now? Set a callback. */
1210 if (size > queue->remaining_credit) {
1211 queue->credit_timeout.data =
1212 (unsigned long)queue;
1213 mod_timer(&queue->credit_timeout,
1215 queue->credit_window_start = next_credit;
1216 queue->rate_limited = true;
1224 /* No locking is required in xenvif_mcast_add/del() as they are
1225 * only ever invoked from NAPI poll. An RCU list is used because
1226 * xenvif_mcast_match() is called asynchronously, during start_xmit.
1229 static int xenvif_mcast_add(struct xenvif *vif, const u8 *addr)
1231 struct xenvif_mcast_addr *mcast;
1233 if (vif->fe_mcast_count == XEN_NETBK_MCAST_MAX) {
1234 if (net_ratelimit())
1235 netdev_err(vif->dev,
1236 "Too many multicast addresses\n");
1240 mcast = kzalloc(sizeof(*mcast), GFP_ATOMIC);
1244 ether_addr_copy(mcast->addr, addr);
1245 list_add_tail_rcu(&mcast->entry, &vif->fe_mcast_addr);
1246 vif->fe_mcast_count++;
1251 static void xenvif_mcast_del(struct xenvif *vif, const u8 *addr)
1253 struct xenvif_mcast_addr *mcast;
1255 list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
1256 if (ether_addr_equal(addr, mcast->addr)) {
1257 --vif->fe_mcast_count;
1258 list_del_rcu(&mcast->entry);
1259 kfree_rcu(mcast, rcu);
1265 bool xenvif_mcast_match(struct xenvif *vif, const u8 *addr)
1267 struct xenvif_mcast_addr *mcast;
1270 list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
1271 if (ether_addr_equal(addr, mcast->addr)) {
1281 void xenvif_mcast_addr_list_free(struct xenvif *vif)
1283 /* No need for locking or RCU here. NAPI poll and TX queue
1286 while (!list_empty(&vif->fe_mcast_addr)) {
1287 struct xenvif_mcast_addr *mcast;
1289 mcast = list_first_entry(&vif->fe_mcast_addr,
1290 struct xenvif_mcast_addr,
1292 --vif->fe_mcast_count;
1293 list_del(&mcast->entry);
1298 static void xenvif_tx_build_gops(struct xenvif_queue *queue,
1303 struct gnttab_map_grant_ref *gop = queue->tx_map_ops;
1304 struct sk_buff *skb, *nskb;
1306 unsigned int frag_overflow;
1308 while (skb_queue_len(&queue->tx_queue) < budget) {
1309 struct xen_netif_tx_request txreq;
1310 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1311 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1315 unsigned int data_len;
1316 pending_ring_idx_t index;
1318 if (queue->tx.sring->req_prod - queue->tx.req_cons >
1319 XEN_NETIF_TX_RING_SIZE) {
1320 netdev_err(queue->vif->dev,
1321 "Impossible number of requests. "
1322 "req_prod %d, req_cons %d, size %ld\n",
1323 queue->tx.sring->req_prod, queue->tx.req_cons,
1324 XEN_NETIF_TX_RING_SIZE);
1325 xenvif_fatal_tx_err(queue->vif);
1329 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
1333 idx = queue->tx.req_cons;
1334 rmb(); /* Ensure that we see the request before we copy it. */
1335 RING_COPY_REQUEST(&queue->tx, idx, &txreq);
1337 /* Credit-based scheduling. */
1338 if (txreq.size > queue->remaining_credit &&
1339 tx_credit_exceeded(queue, txreq.size))
1342 queue->remaining_credit -= txreq.size;
1345 queue->tx.req_cons = ++idx;
1347 memset(extras, 0, sizeof(extras));
1348 if (txreq.flags & XEN_NETTXF_extra_info) {
1349 work_to_do = xenvif_get_extras(queue, extras,
1351 idx = queue->tx.req_cons;
1352 if (unlikely(work_to_do < 0))
1356 if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1].type) {
1357 struct xen_netif_extra_info *extra;
1359 extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1];
1360 ret = xenvif_mcast_add(queue->vif, extra->u.mcast.addr);
1362 make_tx_response(queue, &txreq,
1364 XEN_NETIF_RSP_OKAY :
1365 XEN_NETIF_RSP_ERROR);
1366 push_tx_responses(queue);
1370 if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1].type) {
1371 struct xen_netif_extra_info *extra;
1373 extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1];
1374 xenvif_mcast_del(queue->vif, extra->u.mcast.addr);
1376 make_tx_response(queue, &txreq, XEN_NETIF_RSP_OKAY);
1377 push_tx_responses(queue);
1381 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
1382 if (unlikely(ret < 0))
1387 if (unlikely(txreq.size < ETH_HLEN)) {
1388 netdev_dbg(queue->vif->dev,
1389 "Bad packet size: %d\n", txreq.size);
1390 xenvif_tx_err(queue, &txreq, idx);
1394 /* No crossing a page as the payload mustn't fragment. */
1395 if (unlikely((txreq.offset + txreq.size) > XEN_PAGE_SIZE)) {
1396 netdev_err(queue->vif->dev,
1397 "txreq.offset: %u, size: %u, end: %lu\n",
1398 txreq.offset, txreq.size,
1399 (unsigned long)(txreq.offset&~XEN_PAGE_MASK) + txreq.size);
1400 xenvif_fatal_tx_err(queue->vif);
1404 index = pending_index(queue->pending_cons);
1405 pending_idx = queue->pending_ring[index];
1407 data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
1408 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1409 XEN_NETBACK_TX_COPY_LEN : txreq.size;
1411 skb = xenvif_alloc_skb(data_len);
1412 if (unlikely(skb == NULL)) {
1413 netdev_dbg(queue->vif->dev,
1414 "Can't allocate a skb in start_xmit.\n");
1415 xenvif_tx_err(queue, &txreq, idx);
1419 skb_shinfo(skb)->nr_frags = ret;
1420 if (data_len < txreq.size)
1421 skb_shinfo(skb)->nr_frags++;
1422 /* At this point shinfo->nr_frags is in fact the number of
1423 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
1427 if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
1428 frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
1429 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1430 skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
1431 nskb = xenvif_alloc_skb(0);
1432 if (unlikely(nskb == NULL)) {
1433 skb_shinfo(skb)->nr_frags = 0;
1435 xenvif_tx_err(queue, &txreq, idx);
1436 if (net_ratelimit())
1437 netdev_err(queue->vif->dev,
1438 "Can't allocate the frag_list skb.\n");
1443 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1444 struct xen_netif_extra_info *gso;
1445 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1447 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
1448 /* Failure in xenvif_set_skb_gso is fatal. */
1449 skb_shinfo(skb)->nr_frags = 0;
1456 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
1458 __skb_put(skb, data_len);
1459 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1460 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1461 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
1463 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
1464 virt_to_gfn(skb->data);
1465 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1466 queue->tx_copy_ops[*copy_ops].dest.offset =
1467 offset_in_page(skb->data) & ~XEN_PAGE_MASK;
1469 queue->tx_copy_ops[*copy_ops].len = data_len;
1470 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
1474 if (data_len < txreq.size) {
1475 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1477 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
1480 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1481 INVALID_PENDING_IDX);
1482 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
1486 queue->pending_cons++;
1488 gop = xenvif_get_requests(queue, skb, txfrags, gop,
1489 frag_overflow, nskb);
1491 __skb_queue_tail(&queue->tx_queue, skb);
1493 queue->tx.req_cons = idx;
1495 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1496 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
1500 (*map_ops) = gop - queue->tx_map_ops;
1504 /* Consolidate skb with a frag_list into a brand new one with local pages on
1505 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1507 static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
1509 unsigned int offset = skb_headlen(skb);
1510 skb_frag_t frags[MAX_SKB_FRAGS];
1512 struct ubuf_info *uarg;
1513 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1515 queue->stats.tx_zerocopy_sent += 2;
1516 queue->stats.tx_frag_overflow++;
1518 xenvif_fill_frags(queue, nskb);
1519 /* Subtract frags size, we will correct it later */
1520 skb->truesize -= skb->data_len;
1521 skb->len += nskb->len;
1522 skb->data_len += nskb->len;
1524 /* create a brand new frags array and coalesce there */
1525 for (i = 0; offset < skb->len; i++) {
1529 BUG_ON(i >= MAX_SKB_FRAGS);
1530 page = alloc_page(GFP_ATOMIC);
1533 skb->truesize += skb->data_len;
1534 for (j = 0; j < i; j++)
1535 put_page(frags[j].page.p);
1539 if (offset + PAGE_SIZE < skb->len)
1542 len = skb->len - offset;
1543 if (skb_copy_bits(skb, offset, page_address(page), len))
1547 frags[i].page.p = page;
1548 frags[i].page_offset = 0;
1549 skb_frag_size_set(&frags[i], len);
1552 /* Release all the original (foreign) frags. */
1553 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1554 skb_frag_unref(skb, f);
1555 uarg = skb_shinfo(skb)->destructor_arg;
1556 /* increase inflight counter to offset decrement in callback */
1557 atomic_inc(&queue->inflight_packets);
1558 uarg->callback(uarg, true);
1559 skb_shinfo(skb)->destructor_arg = NULL;
1561 /* Fill the skb with the new (local) frags. */
1562 memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
1563 skb_shinfo(skb)->nr_frags = i;
1564 skb->truesize += i * PAGE_SIZE;
1569 static int xenvif_tx_submit(struct xenvif_queue *queue)
1571 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1572 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
1573 struct sk_buff *skb;
1576 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
1577 struct xen_netif_tx_request *txp;
1581 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
1582 txp = &queue->pending_tx_info[pending_idx].req;
1584 /* Check the remap error code. */
1585 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
1586 /* If there was an error, xenvif_tx_check_gop is
1587 * expected to release all the frags which were mapped,
1588 * so kfree_skb shouldn't do it again
1590 skb_shinfo(skb)->nr_frags = 0;
1591 if (skb_has_frag_list(skb)) {
1592 struct sk_buff *nskb =
1593 skb_shinfo(skb)->frag_list;
1594 skb_shinfo(nskb)->nr_frags = 0;
1600 data_len = skb->len;
1601 callback_param(queue, pending_idx).ctx = NULL;
1602 if (data_len < txp->size) {
1603 /* Append the packet payload as a fragment. */
1604 txp->offset += data_len;
1605 txp->size -= data_len;
1607 /* Schedule a response immediately. */
1608 xenvif_idx_release(queue, pending_idx,
1609 XEN_NETIF_RSP_OKAY);
1612 if (txp->flags & XEN_NETTXF_csum_blank)
1613 skb->ip_summed = CHECKSUM_PARTIAL;
1614 else if (txp->flags & XEN_NETTXF_data_validated)
1615 skb->ip_summed = CHECKSUM_UNNECESSARY;
1617 xenvif_fill_frags(queue, skb);
1619 if (unlikely(skb_has_frag_list(skb))) {
1620 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1621 xenvif_skb_zerocopy_prepare(queue, nskb);
1622 if (xenvif_handle_frag_list(queue, skb)) {
1623 if (net_ratelimit())
1624 netdev_err(queue->vif->dev,
1625 "Not enough memory to consolidate frag_list!\n");
1626 xenvif_skb_zerocopy_prepare(queue, skb);
1630 /* Copied all the bits from the frag list -- free it. */
1631 skb_frag_list_init(skb);
1635 skb->dev = queue->vif->dev;
1636 skb->protocol = eth_type_trans(skb, skb->dev);
1637 skb_reset_network_header(skb);
1639 if (checksum_setup(queue, skb)) {
1640 netdev_dbg(queue->vif->dev,
1641 "Can't setup checksum in net_tx_action\n");
1642 /* We have to set this flag to trigger the callback */
1643 if (skb_shinfo(skb)->destructor_arg)
1644 xenvif_skb_zerocopy_prepare(queue, skb);
1649 skb_probe_transport_header(skb, 0);
1651 /* If the packet is GSO then we will have just set up the
1652 * transport header offset in checksum_setup so it's now
1653 * straightforward to calculate gso_segs.
1655 if (skb_is_gso(skb)) {
1656 int mss = skb_shinfo(skb)->gso_size;
1657 int hdrlen = skb_transport_header(skb) -
1658 skb_mac_header(skb) +
1661 skb_shinfo(skb)->gso_segs =
1662 DIV_ROUND_UP(skb->len - hdrlen, mss);
1665 queue->stats.rx_bytes += skb->len;
1666 queue->stats.rx_packets++;
1670 /* Set this flag right before netif_receive_skb, otherwise
1671 * someone might think this packet already left netback, and
1672 * do a skb_copy_ubufs while we are still in control of the
1673 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1675 if (skb_shinfo(skb)->destructor_arg) {
1676 xenvif_skb_zerocopy_prepare(queue, skb);
1677 queue->stats.tx_zerocopy_sent++;
1680 netif_receive_skb(skb);
1686 void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1688 unsigned long flags;
1689 pending_ring_idx_t index;
1690 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
1692 /* This is the only place where we grab this lock, to protect callbacks
1695 spin_lock_irqsave(&queue->callback_lock, flags);
1697 u16 pending_idx = ubuf->desc;
1698 ubuf = (struct ubuf_info *) ubuf->ctx;
1699 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
1701 index = pending_index(queue->dealloc_prod);
1702 queue->dealloc_ring[index] = pending_idx;
1703 /* Sync with xenvif_tx_dealloc_action:
1704 * insert idx then incr producer.
1707 queue->dealloc_prod++;
1709 spin_unlock_irqrestore(&queue->callback_lock, flags);
1711 if (likely(zerocopy_success))
1712 queue->stats.tx_zerocopy_success++;
1714 queue->stats.tx_zerocopy_fail++;
1715 xenvif_skb_zerocopy_complete(queue);
1718 static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
1720 struct gnttab_unmap_grant_ref *gop;
1721 pending_ring_idx_t dc, dp;
1722 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1725 dc = queue->dealloc_cons;
1726 gop = queue->tx_unmap_ops;
1728 /* Free up any grants we have finished using */
1730 dp = queue->dealloc_prod;
1732 /* Ensure we see all indices enqueued by all
1733 * xenvif_zerocopy_callback().
1738 BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
1740 queue->dealloc_ring[pending_index(dc++)];
1742 pending_idx_release[gop - queue->tx_unmap_ops] =
1744 queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
1745 queue->mmap_pages[pending_idx];
1746 gnttab_set_unmap_op(gop,
1747 idx_to_kaddr(queue, pending_idx),
1749 queue->grant_tx_handle[pending_idx]);
1750 xenvif_grant_handle_reset(queue, pending_idx);
1754 } while (dp != queue->dealloc_prod);
1756 queue->dealloc_cons = dc;
1758 if (gop - queue->tx_unmap_ops > 0) {
1760 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
1762 queue->pages_to_unmap,
1763 gop - queue->tx_unmap_ops);
1765 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
1766 gop - queue->tx_unmap_ops, ret);
1767 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
1768 if (gop[i].status != GNTST_okay)
1769 netdev_err(queue->vif->dev,
1770 " host_addr: 0x%llx handle: 0x%x status: %d\n",
1779 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1780 xenvif_idx_release(queue, pending_idx_release[i],
1781 XEN_NETIF_RSP_OKAY);
1785 /* Called after netfront has transmitted */
1786 int xenvif_tx_action(struct xenvif_queue *queue, int budget)
1788 unsigned nr_mops, nr_cops = 0;
1791 if (unlikely(!tx_work_todo(queue)))
1794 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
1799 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
1801 ret = gnttab_map_refs(queue->tx_map_ops,
1803 queue->pages_to_map,
1808 netdev_err(queue->vif->dev, "Map fail: nr %u ret %d\n",
1810 for (i = 0; i < nr_mops; ++i)
1811 WARN_ON_ONCE(queue->tx_map_ops[i].status ==
1816 work_done = xenvif_tx_submit(queue);
1821 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
1824 struct pending_tx_info *pending_tx_info;
1825 pending_ring_idx_t index;
1826 unsigned long flags;
1828 pending_tx_info = &queue->pending_tx_info[pending_idx];
1830 spin_lock_irqsave(&queue->response_lock, flags);
1832 make_tx_response(queue, &pending_tx_info->req, status);
1834 /* Release the pending index before pusing the Tx response so
1835 * its available before a new Tx request is pushed by the
1838 index = pending_index(queue->pending_prod++);
1839 queue->pending_ring[index] = pending_idx;
1841 push_tx_responses(queue);
1843 spin_unlock_irqrestore(&queue->response_lock, flags);
1847 static void make_tx_response(struct xenvif_queue *queue,
1848 struct xen_netif_tx_request *txp,
1851 RING_IDX i = queue->tx.rsp_prod_pvt;
1852 struct xen_netif_tx_response *resp;
1854 resp = RING_GET_RESPONSE(&queue->tx, i);
1858 if (txp->flags & XEN_NETTXF_extra_info)
1859 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1861 queue->tx.rsp_prod_pvt = ++i;
1864 static void push_tx_responses(struct xenvif_queue *queue)
1868 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
1870 notify_remote_via_irq(queue->tx_irq);
1873 static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
1880 RING_IDX i = queue->rx.rsp_prod_pvt;
1881 struct xen_netif_rx_response *resp;
1883 resp = RING_GET_RESPONSE(&queue->rx, i);
1884 resp->offset = offset;
1885 resp->flags = flags;
1887 resp->status = (s16)size;
1889 resp->status = (s16)st;
1891 queue->rx.rsp_prod_pvt = ++i;
1896 void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
1899 struct gnttab_unmap_grant_ref tx_unmap_op;
1901 gnttab_set_unmap_op(&tx_unmap_op,
1902 idx_to_kaddr(queue, pending_idx),
1904 queue->grant_tx_handle[pending_idx]);
1905 xenvif_grant_handle_reset(queue, pending_idx);
1907 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1908 &queue->mmap_pages[pending_idx], 1);
1910 netdev_err(queue->vif->dev,
1911 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
1914 tx_unmap_op.host_addr,
1916 tx_unmap_op.status);
1921 static inline int tx_work_todo(struct xenvif_queue *queue)
1923 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
1929 static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
1931 return queue->dealloc_cons != queue->dealloc_prod;
1934 void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
1936 if (queue->tx.sring)
1937 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1939 if (queue->rx.sring)
1940 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1944 int xenvif_map_frontend_rings(struct xenvif_queue *queue,
1945 grant_ref_t tx_ring_ref,
1946 grant_ref_t rx_ring_ref)
1949 struct xen_netif_tx_sring *txs;
1950 struct xen_netif_rx_sring *rxs;
1954 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1955 &tx_ring_ref, 1, &addr);
1959 txs = (struct xen_netif_tx_sring *)addr;
1960 BACK_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1962 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1963 &rx_ring_ref, 1, &addr);
1967 rxs = (struct xen_netif_rx_sring *)addr;
1968 BACK_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1973 xenvif_unmap_frontend_rings(queue);
1977 static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
1979 struct xenvif *vif = queue->vif;
1981 queue->stalled = true;
1983 /* At least one queue has stalled? Disable the carrier. */
1984 spin_lock(&vif->lock);
1985 if (vif->stalled_queues++ == 0) {
1986 netdev_info(vif->dev, "Guest Rx stalled");
1987 netif_carrier_off(vif->dev);
1989 spin_unlock(&vif->lock);
1992 static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
1994 struct xenvif *vif = queue->vif;
1996 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
1997 queue->stalled = false;
1999 /* All queues are ready? Enable the carrier. */
2000 spin_lock(&vif->lock);
2001 if (--vif->stalled_queues == 0) {
2002 netdev_info(vif->dev, "Guest Rx ready");
2003 netif_carrier_on(vif->dev);
2005 spin_unlock(&vif->lock);
2008 static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
2010 RING_IDX prod, cons;
2012 prod = queue->rx.sring->req_prod;
2013 cons = queue->rx.req_cons;
2015 return !queue->stalled && prod - cons < 1
2016 && time_after(jiffies,
2017 queue->last_rx_time + queue->vif->stall_timeout);
2020 static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
2022 RING_IDX prod, cons;
2024 prod = queue->rx.sring->req_prod;
2025 cons = queue->rx.req_cons;
2027 return queue->stalled && prod - cons >= 1;
2030 bool xenvif_have_rx_work(struct xenvif_queue *queue, bool test_kthread)
2032 return (!skb_queue_empty(&queue->rx_queue)
2033 && xenvif_rx_ring_slots_available(queue))
2034 || (queue->vif->stall_timeout &&
2035 (xenvif_rx_queue_stalled(queue)
2036 || xenvif_rx_queue_ready(queue)))
2037 || (test_kthread && kthread_should_stop())
2038 || queue->vif->disabled;
2041 static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
2043 struct sk_buff *skb;
2046 skb = skb_peek(&queue->rx_queue);
2048 return MAX_SCHEDULE_TIMEOUT;
2050 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
2051 return timeout < 0 ? 0 : timeout;
2054 /* Wait until the guest Rx thread has work.
2056 * The timeout needs to be adjusted based on the current head of the
2057 * queue (and not just the head at the beginning). In particular, if
2058 * the queue is initially empty an infinite timeout is used and this
2059 * needs to be reduced when a skb is queued.
2061 * This cannot be done with wait_event_timeout() because it only
2062 * calculates the timeout once.
2064 static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
2068 if (xenvif_have_rx_work(queue, true))
2074 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
2075 if (xenvif_have_rx_work(queue, true))
2077 if (xenvif_atomic_fetch_andnot(NETBK_RX_EOI | NETBK_COMMON_EOI,
2078 &queue->eoi_pending) &
2079 (NETBK_RX_EOI | NETBK_COMMON_EOI))
2080 xen_irq_lateeoi(queue->rx_irq, 0);
2082 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
2086 finish_wait(&queue->wq, &wait);
2089 int xenvif_kthread_guest_rx(void *data)
2091 struct xenvif_queue *queue = data;
2092 struct xenvif *vif = queue->vif;
2094 if (!vif->stall_timeout)
2095 xenvif_queue_carrier_on(queue);
2098 xenvif_wait_for_rx_work(queue);
2100 if (kthread_should_stop())
2103 /* This frontend is found to be rogue, disable it in
2104 * kthread context. Currently this is only set when
2105 * netback finds out frontend sends malformed packet,
2106 * but we cannot disable the interface in softirq
2107 * context so we defer it here, if this thread is
2108 * associated with queue 0.
2110 if (unlikely(vif->disabled && queue->id == 0)) {
2111 xenvif_carrier_off(vif);
2115 if (!skb_queue_empty(&queue->rx_queue))
2116 xenvif_rx_action(queue);
2118 /* If the guest hasn't provided any Rx slots for a
2119 * while it's probably not responsive, drop the
2120 * carrier so packets are dropped earlier.
2122 if (vif->stall_timeout) {
2123 if (xenvif_rx_queue_stalled(queue))
2124 xenvif_queue_carrier_off(queue);
2125 else if (xenvif_rx_queue_ready(queue))
2126 xenvif_queue_carrier_on(queue);
2129 /* Queued packets may have foreign pages from other
2130 * domains. These cannot be queued indefinitely as
2131 * this would starve guests of grant refs and transmit
2134 xenvif_rx_queue_drop_expired(queue);
2136 xenvif_rx_queue_maybe_wake(queue);
2141 /* Bin any remaining skbs */
2142 xenvif_rx_queue_purge(queue);
2147 static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
2149 /* Dealloc thread must remain running until all inflight
2152 return kthread_should_stop() &&
2153 !atomic_read(&queue->inflight_packets);
2156 int xenvif_dealloc_kthread(void *data)
2158 struct xenvif_queue *queue = data;
2161 wait_event_interruptible(queue->dealloc_wq,
2162 tx_dealloc_work_todo(queue) ||
2163 xenvif_dealloc_kthread_should_stop(queue));
2164 if (xenvif_dealloc_kthread_should_stop(queue))
2167 xenvif_tx_dealloc_action(queue);
2171 /* Unmap anything remaining*/
2172 if (tx_dealloc_work_todo(queue))
2173 xenvif_tx_dealloc_action(queue);
2178 static int __init netback_init(void)
2185 /* Allow as many queues as there are CPUs but max. 8 if user has not
2186 * specified a value.
2188 if (xenvif_max_queues == 0)
2189 xenvif_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2192 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
2193 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2194 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
2195 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
2198 rc = xenvif_xenbus_init();
2202 #ifdef CONFIG_DEBUG_FS
2203 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2204 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2205 pr_warn("Init of debugfs returned %ld!\n",
2206 PTR_ERR(xen_netback_dbg_root));
2207 #endif /* CONFIG_DEBUG_FS */
2215 module_init(netback_init);
2217 static void __exit netback_fini(void)
2219 #ifdef CONFIG_DEBUG_FS
2220 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2221 debugfs_remove_recursive(xen_netback_dbg_root);
2222 #endif /* CONFIG_DEBUG_FS */
2223 xenvif_xenbus_fini();
2225 module_exit(netback_fini);
2227 MODULE_LICENSE("Dual BSD/GPL");
2228 MODULE_ALIAS("xen-backend:vif");