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>
48 #include <asm/xen/hypercall.h>
49 #include <asm/xen/page.h>
51 /* Provide an option to disable split event channels at load time as
52 * event channels are limited resource. Split event channels are
55 bool separate_tx_rx_irq = 1;
56 module_param(separate_tx_rx_irq, bool, 0644);
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 bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
158 prod = queue->rx.sring->req_prod;
159 cons = queue->rx.req_cons;
161 if (prod - cons >= needed)
164 queue->rx.sring->req_event = prod + 1;
166 /* Make sure event is visible before we check prod
170 } while (queue->rx.sring->req_prod != prod);
175 void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
179 spin_lock_irqsave(&queue->rx_queue.lock, flags);
181 __skb_queue_tail(&queue->rx_queue, skb);
183 queue->rx_queue_len += skb->len;
184 if (queue->rx_queue_len > queue->rx_queue_max)
185 netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
187 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
190 static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
194 spin_lock_irq(&queue->rx_queue.lock);
196 skb = __skb_dequeue(&queue->rx_queue);
198 queue->rx_queue_len -= skb->len;
200 spin_unlock_irq(&queue->rx_queue.lock);
205 static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
207 spin_lock_irq(&queue->rx_queue.lock);
209 if (queue->rx_queue_len < queue->rx_queue_max)
210 netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
212 spin_unlock_irq(&queue->rx_queue.lock);
216 static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
219 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
223 static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
228 skb = skb_peek(&queue->rx_queue);
231 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
233 xenvif_rx_dequeue(queue);
238 struct netrx_pending_operations {
239 unsigned copy_prod, copy_cons;
240 unsigned meta_prod, meta_cons;
241 struct gnttab_copy *copy;
242 struct xenvif_rx_meta *meta;
244 grant_ref_t copy_gref;
247 static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
248 struct netrx_pending_operations *npo)
250 struct xenvif_rx_meta *meta;
251 struct xen_netif_rx_request *req;
253 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
255 meta = npo->meta + npo->meta_prod++;
256 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
262 npo->copy_gref = req->gref;
268 * Set up the grant operations for this fragment. If it's a flipping
269 * interface, we also set up the unmap request from here.
271 static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
272 struct netrx_pending_operations *npo,
273 struct page *page, unsigned long size,
274 unsigned long offset, int *head)
276 struct gnttab_copy *copy_gop;
277 struct xenvif_rx_meta *meta;
279 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
281 /* Data must not cross a page boundary. */
282 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
284 meta = npo->meta + npo->meta_prod - 1;
286 /* Skip unused frames from start of page */
287 page += offset >> PAGE_SHIFT;
288 offset &= ~PAGE_MASK;
291 struct xen_page_foreign *foreign;
293 BUG_ON(offset >= PAGE_SIZE);
294 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
296 if (npo->copy_off == MAX_BUFFER_OFFSET)
297 meta = get_next_rx_buffer(queue, npo);
299 bytes = PAGE_SIZE - offset;
303 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
304 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
306 copy_gop = npo->copy + npo->copy_prod++;
307 copy_gop->flags = GNTCOPY_dest_gref;
308 copy_gop->len = bytes;
310 foreign = xen_page_foreign(page);
312 copy_gop->source.domid = foreign->domid;
313 copy_gop->source.u.ref = foreign->gref;
314 copy_gop->flags |= GNTCOPY_source_gref;
316 copy_gop->source.domid = DOMID_SELF;
317 copy_gop->source.u.gmfn =
318 virt_to_mfn(page_address(page));
320 copy_gop->source.offset = offset;
322 copy_gop->dest.domid = queue->vif->domid;
323 copy_gop->dest.offset = npo->copy_off;
324 copy_gop->dest.u.ref = npo->copy_gref;
326 npo->copy_off += bytes;
333 if (offset == PAGE_SIZE && size) {
334 BUG_ON(!PageCompound(page));
339 /* Leave a gap for the GSO descriptor. */
340 if (skb_is_gso(skb)) {
341 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
342 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
343 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
344 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
347 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
348 queue->rx.req_cons++;
350 *head = 0; /* There must be something in this buffer now. */
356 * Prepare an SKB to be transmitted to the frontend.
358 * This function is responsible for allocating grant operations, meta
361 * It returns the number of meta structures consumed. The number of
362 * ring slots used is always equal to the number of meta slots used
363 * plus the number of GSO descriptors used. Currently, we use either
364 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
365 * frontend-side LRO).
367 static int xenvif_gop_skb(struct sk_buff *skb,
368 struct netrx_pending_operations *npo,
369 struct xenvif_queue *queue)
371 struct xenvif *vif = netdev_priv(skb->dev);
372 int nr_frags = skb_shinfo(skb)->nr_frags;
374 struct xen_netif_rx_request *req;
375 struct xenvif_rx_meta *meta;
381 old_meta_prod = npo->meta_prod;
383 gso_type = XEN_NETIF_GSO_TYPE_NONE;
384 if (skb_is_gso(skb)) {
385 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
386 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
387 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
388 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
391 /* Set up a GSO prefix descriptor, if necessary */
392 if ((1 << gso_type) & vif->gso_prefix_mask) {
393 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
394 meta = npo->meta + npo->meta_prod++;
395 meta->gso_type = gso_type;
396 meta->gso_size = skb_shinfo(skb)->gso_size;
401 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
402 meta = npo->meta + npo->meta_prod++;
404 if ((1 << gso_type) & vif->gso_mask) {
405 meta->gso_type = gso_type;
406 meta->gso_size = skb_shinfo(skb)->gso_size;
408 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
415 npo->copy_gref = req->gref;
418 while (data < skb_tail_pointer(skb)) {
419 unsigned int offset = offset_in_page(data);
420 unsigned int len = PAGE_SIZE - offset;
422 if (data + len > skb_tail_pointer(skb))
423 len = skb_tail_pointer(skb) - data;
425 xenvif_gop_frag_copy(queue, skb, npo,
426 virt_to_page(data), len, offset, &head);
430 for (i = 0; i < nr_frags; i++) {
431 xenvif_gop_frag_copy(queue, skb, npo,
432 skb_frag_page(&skb_shinfo(skb)->frags[i]),
433 skb_frag_size(&skb_shinfo(skb)->frags[i]),
434 skb_shinfo(skb)->frags[i].page_offset,
438 return npo->meta_prod - old_meta_prod;
442 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
443 * used to set up the operations on the top of
444 * netrx_pending_operations, which have since been done. Check that
445 * they didn't give any errors and advance over them.
447 static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
448 struct netrx_pending_operations *npo)
450 struct gnttab_copy *copy_op;
451 int status = XEN_NETIF_RSP_OKAY;
454 for (i = 0; i < nr_meta_slots; i++) {
455 copy_op = npo->copy + npo->copy_cons++;
456 if (copy_op->status != GNTST_okay) {
458 "Bad status %d from copy to DOM%d.\n",
459 copy_op->status, vif->domid);
460 status = XEN_NETIF_RSP_ERROR;
467 static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
468 struct xenvif_rx_meta *meta,
472 unsigned long offset;
474 /* No fragments used */
475 if (nr_meta_slots <= 1)
480 for (i = 0; i < nr_meta_slots; i++) {
482 if (i == nr_meta_slots - 1)
485 flags = XEN_NETRXF_more_data;
488 make_rx_response(queue, meta[i].id, status, offset,
489 meta[i].size, flags);
493 void xenvif_kick_thread(struct xenvif_queue *queue)
498 static void xenvif_rx_action(struct xenvif_queue *queue)
502 struct xen_netif_rx_response *resp;
503 struct sk_buff_head rxq;
507 unsigned long offset;
508 bool need_to_notify = false;
510 struct netrx_pending_operations npo = {
511 .copy = queue->grant_copy_op,
515 skb_queue_head_init(&rxq);
517 while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
518 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
519 RING_IDX old_req_cons;
520 RING_IDX ring_slots_used;
522 queue->last_rx_time = jiffies;
524 old_req_cons = queue->rx.req_cons;
525 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
526 ring_slots_used = queue->rx.req_cons - old_req_cons;
528 __skb_queue_tail(&rxq, skb);
531 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
536 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
537 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
539 while ((skb = __skb_dequeue(&rxq)) != NULL) {
541 if ((1 << queue->meta[npo.meta_cons].gso_type) &
542 queue->vif->gso_prefix_mask) {
543 resp = RING_GET_RESPONSE(&queue->rx,
544 queue->rx.rsp_prod_pvt++);
546 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
548 resp->offset = queue->meta[npo.meta_cons].gso_size;
549 resp->id = queue->meta[npo.meta_cons].id;
550 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
553 XENVIF_RX_CB(skb)->meta_slots_used--;
557 queue->stats.tx_bytes += skb->len;
558 queue->stats.tx_packets++;
560 status = xenvif_check_gop(queue->vif,
561 XENVIF_RX_CB(skb)->meta_slots_used,
564 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
567 flags = XEN_NETRXF_more_data;
569 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
570 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
571 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
572 /* remote but checksummed. */
573 flags |= XEN_NETRXF_data_validated;
576 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
578 queue->meta[npo.meta_cons].size,
581 if ((1 << queue->meta[npo.meta_cons].gso_type) &
582 queue->vif->gso_mask) {
583 struct xen_netif_extra_info *gso =
584 (struct xen_netif_extra_info *)
585 RING_GET_RESPONSE(&queue->rx,
586 queue->rx.rsp_prod_pvt++);
588 resp->flags |= XEN_NETRXF_extra_info;
590 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
591 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
593 gso->u.gso.features = 0;
595 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
599 xenvif_add_frag_responses(queue, status,
600 queue->meta + npo.meta_cons + 1,
601 XENVIF_RX_CB(skb)->meta_slots_used);
603 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
605 need_to_notify |= !!ret;
607 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
613 notify_remote_via_irq(queue->rx_irq);
616 void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
620 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
623 napi_schedule(&queue->napi);
626 static void tx_add_credit(struct xenvif_queue *queue)
628 unsigned long max_burst, max_credit;
631 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
632 * Otherwise the interface can seize up due to insufficient credit.
634 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
635 max_burst = min(max_burst, 131072UL);
636 max_burst = max(max_burst, queue->credit_bytes);
638 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
639 max_credit = queue->remaining_credit + queue->credit_bytes;
640 if (max_credit < queue->remaining_credit)
641 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
643 queue->remaining_credit = min(max_credit, max_burst);
644 queue->rate_limited = false;
647 void xenvif_tx_credit_callback(unsigned long data)
649 struct xenvif_queue *queue = (struct xenvif_queue *)data;
650 tx_add_credit(queue);
651 xenvif_napi_schedule_or_enable_events(queue);
654 static void xenvif_tx_err(struct xenvif_queue *queue,
655 struct xen_netif_tx_request *txp, RING_IDX end)
657 RING_IDX cons = queue->tx.req_cons;
661 spin_lock_irqsave(&queue->response_lock, flags);
662 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
663 push_tx_responses(queue);
664 spin_unlock_irqrestore(&queue->response_lock, flags);
667 txp = RING_GET_REQUEST(&queue->tx, cons++);
669 queue->tx.req_cons = cons;
672 static void xenvif_fatal_tx_err(struct xenvif *vif)
674 netdev_err(vif->dev, "fatal error; disabling device\n");
675 vif->disabled = true;
676 /* Disable the vif from queue 0's kthread */
678 xenvif_kick_thread(&vif->queues[0]);
681 static int xenvif_count_requests(struct xenvif_queue *queue,
682 struct xen_netif_tx_request *first,
683 struct xen_netif_tx_request *txp,
686 RING_IDX cons = queue->tx.req_cons;
691 if (!(first->flags & XEN_NETTXF_more_data))
695 struct xen_netif_tx_request dropped_tx = { 0 };
697 if (slots >= work_to_do) {
698 netdev_err(queue->vif->dev,
699 "Asked for %d slots but exceeds this limit\n",
701 xenvif_fatal_tx_err(queue->vif);
705 /* This guest is really using too many slots and
706 * considered malicious.
708 if (unlikely(slots >= fatal_skb_slots)) {
709 netdev_err(queue->vif->dev,
710 "Malicious frontend using %d slots, threshold %u\n",
711 slots, fatal_skb_slots);
712 xenvif_fatal_tx_err(queue->vif);
716 /* Xen network protocol had implicit dependency on
717 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
718 * the historical MAX_SKB_FRAGS value 18 to honor the
719 * same behavior as before. Any packet using more than
720 * 18 slots but less than fatal_skb_slots slots is
723 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
725 netdev_dbg(queue->vif->dev,
726 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
727 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
734 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
737 /* If the guest submitted a frame >= 64 KiB then
738 * first->size overflowed and following slots will
739 * appear to be larger than the frame.
741 * This cannot be fatal error as there are buggy
742 * frontends that do this.
744 * Consume all slots and drop the packet.
746 if (!drop_err && txp->size > first->size) {
748 netdev_dbg(queue->vif->dev,
749 "Invalid tx request, slot size %u > remaining size %u\n",
750 txp->size, first->size);
754 first->size -= txp->size;
757 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
758 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
759 txp->offset, txp->size);
760 xenvif_fatal_tx_err(queue->vif);
764 more_data = txp->flags & XEN_NETTXF_more_data;
772 xenvif_tx_err(queue, first, cons + slots);
780 struct xenvif_tx_cb {
784 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
786 static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
788 struct xen_netif_tx_request *txp,
789 struct gnttab_map_grant_ref *mop)
791 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
792 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
793 GNTMAP_host_map | GNTMAP_readonly,
794 txp->gref, queue->vif->domid);
796 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
800 static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
802 struct sk_buff *skb =
803 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
804 GFP_ATOMIC | __GFP_NOWARN);
805 if (unlikely(skb == NULL))
808 /* Packets passed to netif_rx() must have some headroom. */
809 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
811 /* Initialize it here to avoid later surprises */
812 skb_shinfo(skb)->destructor_arg = NULL;
817 static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
819 struct xen_netif_tx_request *txp,
820 struct gnttab_map_grant_ref *gop)
822 struct skb_shared_info *shinfo = skb_shinfo(skb);
823 skb_frag_t *frags = shinfo->frags;
824 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
826 pending_ring_idx_t index;
827 unsigned int nr_slots, frag_overflow = 0;
829 /* At this point shinfo->nr_frags is in fact the number of
830 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
832 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
833 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
834 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
835 shinfo->nr_frags = MAX_SKB_FRAGS;
837 nr_slots = shinfo->nr_frags;
839 /* Skip first skb fragment if it is on same page as header fragment. */
840 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
842 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
843 shinfo->nr_frags++, txp++, gop++) {
844 index = pending_index(queue->pending_cons++);
845 pending_idx = queue->pending_ring[index];
846 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
847 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
851 struct sk_buff *nskb = xenvif_alloc_skb(0);
852 if (unlikely(nskb == NULL)) {
854 netdev_err(queue->vif->dev,
855 "Can't allocate the frag_list skb.\n");
859 shinfo = skb_shinfo(nskb);
860 frags = shinfo->frags;
862 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
863 shinfo->nr_frags++, txp++, gop++) {
864 index = pending_index(queue->pending_cons++);
865 pending_idx = queue->pending_ring[index];
866 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
867 frag_set_pending_idx(&frags[shinfo->nr_frags],
871 skb_shinfo(skb)->frag_list = nskb;
877 static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
879 grant_handle_t handle)
881 if (unlikely(queue->grant_tx_handle[pending_idx] !=
882 NETBACK_INVALID_HANDLE)) {
883 netdev_err(queue->vif->dev,
884 "Trying to overwrite active handle! pending_idx: %x\n",
888 queue->grant_tx_handle[pending_idx] = handle;
891 static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
894 if (unlikely(queue->grant_tx_handle[pending_idx] ==
895 NETBACK_INVALID_HANDLE)) {
896 netdev_err(queue->vif->dev,
897 "Trying to unmap invalid handle! pending_idx: %x\n",
901 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
904 static int xenvif_tx_check_gop(struct xenvif_queue *queue,
906 struct gnttab_map_grant_ref **gopp_map,
907 struct gnttab_copy **gopp_copy)
909 struct gnttab_map_grant_ref *gop_map = *gopp_map;
910 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
911 /* This always points to the shinfo of the skb being checked, which
912 * could be either the first or the one on the frag_list
914 struct skb_shared_info *shinfo = skb_shinfo(skb);
915 /* If this is non-NULL, we are currently checking the frag_list skb, and
916 * this points to the shinfo of the first one
918 struct skb_shared_info *first_shinfo = NULL;
919 int nr_frags = shinfo->nr_frags;
920 const bool sharedslot = nr_frags &&
921 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
924 /* Check status of header. */
925 err = (*gopp_copy)->status;
928 netdev_dbg(queue->vif->dev,
929 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
930 (*gopp_copy)->status,
932 (*gopp_copy)->source.u.ref);
933 /* The first frag might still have this slot mapped */
935 xenvif_idx_release(queue, pending_idx,
936 XEN_NETIF_RSP_ERROR);
941 for (i = 0; i < nr_frags; i++, gop_map++) {
944 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
946 /* Check error status: if okay then remember grant handle. */
947 newerr = gop_map->status;
949 if (likely(!newerr)) {
950 xenvif_grant_handle_set(queue,
953 /* Had a previous error? Invalidate this fragment. */
955 xenvif_idx_unmap(queue, pending_idx);
956 /* If the mapping of the first frag was OK, but
957 * the header's copy failed, and they are
958 * sharing a slot, send an error
960 if (i == 0 && sharedslot)
961 xenvif_idx_release(queue, pending_idx,
962 XEN_NETIF_RSP_ERROR);
964 xenvif_idx_release(queue, pending_idx,
970 /* Error on this fragment: respond to client with an error. */
972 netdev_dbg(queue->vif->dev,
973 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
979 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
981 /* Not the first error? Preceding frags already invalidated. */
985 /* First error: if the header haven't shared a slot with the
986 * first frag, release it as well.
989 xenvif_idx_release(queue,
990 XENVIF_TX_CB(skb)->pending_idx,
993 /* Invalidate preceding fragments of this skb. */
994 for (j = 0; j < i; j++) {
995 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
996 xenvif_idx_unmap(queue, pending_idx);
997 xenvif_idx_release(queue, pending_idx,
1001 /* And if we found the error while checking the frag_list, unmap
1002 * the first skb's frags
1005 for (j = 0; j < first_shinfo->nr_frags; j++) {
1006 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1007 xenvif_idx_unmap(queue, pending_idx);
1008 xenvif_idx_release(queue, pending_idx,
1009 XEN_NETIF_RSP_OKAY);
1013 /* Remember the error: invalidate all subsequent fragments. */
1017 if (skb_has_frag_list(skb) && !first_shinfo) {
1018 first_shinfo = skb_shinfo(skb);
1019 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
1020 nr_frags = shinfo->nr_frags;
1025 *gopp_map = gop_map;
1029 static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
1031 struct skb_shared_info *shinfo = skb_shinfo(skb);
1032 int nr_frags = shinfo->nr_frags;
1034 u16 prev_pending_idx = INVALID_PENDING_IDX;
1036 for (i = 0; i < nr_frags; i++) {
1037 skb_frag_t *frag = shinfo->frags + i;
1038 struct xen_netif_tx_request *txp;
1042 pending_idx = frag_get_pending_idx(frag);
1044 /* If this is not the first frag, chain it to the previous*/
1045 if (prev_pending_idx == INVALID_PENDING_IDX)
1046 skb_shinfo(skb)->destructor_arg =
1047 &callback_param(queue, pending_idx);
1049 callback_param(queue, prev_pending_idx).ctx =
1050 &callback_param(queue, pending_idx);
1052 callback_param(queue, pending_idx).ctx = NULL;
1053 prev_pending_idx = pending_idx;
1055 txp = &queue->pending_tx_info[pending_idx].req;
1056 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
1057 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1058 skb->len += txp->size;
1059 skb->data_len += txp->size;
1060 skb->truesize += txp->size;
1062 /* Take an extra reference to offset network stack's put_page */
1063 get_page(queue->mmap_pages[pending_idx]);
1067 static int xenvif_get_extras(struct xenvif_queue *queue,
1068 struct xen_netif_extra_info *extras,
1071 struct xen_netif_extra_info extra;
1072 RING_IDX cons = queue->tx.req_cons;
1075 if (unlikely(work_to_do-- <= 0)) {
1076 netdev_err(queue->vif->dev, "Missing extra info\n");
1077 xenvif_fatal_tx_err(queue->vif);
1081 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
1083 if (unlikely(!extra.type ||
1084 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1085 queue->tx.req_cons = ++cons;
1086 netdev_err(queue->vif->dev,
1087 "Invalid extra type: %d\n", extra.type);
1088 xenvif_fatal_tx_err(queue->vif);
1092 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1093 queue->tx.req_cons = ++cons;
1094 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1099 static int xenvif_set_skb_gso(struct xenvif *vif,
1100 struct sk_buff *skb,
1101 struct xen_netif_extra_info *gso)
1103 if (!gso->u.gso.size) {
1104 netdev_err(vif->dev, "GSO size must not be zero.\n");
1105 xenvif_fatal_tx_err(vif);
1109 switch (gso->u.gso.type) {
1110 case XEN_NETIF_GSO_TYPE_TCPV4:
1111 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1113 case XEN_NETIF_GSO_TYPE_TCPV6:
1114 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1117 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1118 xenvif_fatal_tx_err(vif);
1122 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1123 /* gso_segs will be calculated later */
1128 static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
1130 bool recalculate_partial_csum = false;
1132 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1133 * peers can fail to set NETRXF_csum_blank when sending a GSO
1134 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1135 * recalculate the partial checksum.
1137 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1138 queue->stats.rx_gso_checksum_fixup++;
1139 skb->ip_summed = CHECKSUM_PARTIAL;
1140 recalculate_partial_csum = true;
1143 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1144 if (skb->ip_summed != CHECKSUM_PARTIAL)
1147 return skb_checksum_setup(skb, recalculate_partial_csum);
1150 static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
1152 u64 now = get_jiffies_64();
1153 u64 next_credit = queue->credit_window_start +
1154 msecs_to_jiffies(queue->credit_usec / 1000);
1156 /* Timer could already be pending in rare cases. */
1157 if (timer_pending(&queue->credit_timeout)) {
1158 queue->rate_limited = true;
1162 /* Passed the point where we can replenish credit? */
1163 if (time_after_eq64(now, next_credit)) {
1164 queue->credit_window_start = now;
1165 tx_add_credit(queue);
1168 /* Still too big to send right now? Set a callback. */
1169 if (size > queue->remaining_credit) {
1170 queue->credit_timeout.data =
1171 (unsigned long)queue;
1172 mod_timer(&queue->credit_timeout,
1174 queue->credit_window_start = next_credit;
1175 queue->rate_limited = true;
1183 static void xenvif_tx_build_gops(struct xenvif_queue *queue,
1188 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
1189 struct sk_buff *skb;
1192 while (skb_queue_len(&queue->tx_queue) < budget) {
1193 struct xen_netif_tx_request txreq;
1194 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1195 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1199 unsigned int data_len;
1200 pending_ring_idx_t index;
1202 if (queue->tx.sring->req_prod - queue->tx.req_cons >
1203 XEN_NETIF_TX_RING_SIZE) {
1204 netdev_err(queue->vif->dev,
1205 "Impossible number of requests. "
1206 "req_prod %d, req_cons %d, size %ld\n",
1207 queue->tx.sring->req_prod, queue->tx.req_cons,
1208 XEN_NETIF_TX_RING_SIZE);
1209 xenvif_fatal_tx_err(queue->vif);
1213 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
1217 idx = queue->tx.req_cons;
1218 rmb(); /* Ensure that we see the request before we copy it. */
1219 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
1221 /* Credit-based scheduling. */
1222 if (txreq.size > queue->remaining_credit &&
1223 tx_credit_exceeded(queue, txreq.size))
1226 queue->remaining_credit -= txreq.size;
1229 queue->tx.req_cons = ++idx;
1231 memset(extras, 0, sizeof(extras));
1232 if (txreq.flags & XEN_NETTXF_extra_info) {
1233 work_to_do = xenvif_get_extras(queue, extras,
1235 idx = queue->tx.req_cons;
1236 if (unlikely(work_to_do < 0))
1240 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
1241 if (unlikely(ret < 0))
1246 if (unlikely(txreq.size < ETH_HLEN)) {
1247 netdev_dbg(queue->vif->dev,
1248 "Bad packet size: %d\n", txreq.size);
1249 xenvif_tx_err(queue, &txreq, idx);
1253 /* No crossing a page as the payload mustn't fragment. */
1254 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1255 netdev_err(queue->vif->dev,
1256 "txreq.offset: %x, size: %u, end: %lu\n",
1257 txreq.offset, txreq.size,
1258 (unsigned long)(txreq.offset&~PAGE_MASK) + txreq.size);
1259 xenvif_fatal_tx_err(queue->vif);
1263 index = pending_index(queue->pending_cons);
1264 pending_idx = queue->pending_ring[index];
1266 data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
1267 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1268 XEN_NETBACK_TX_COPY_LEN : txreq.size;
1270 skb = xenvif_alloc_skb(data_len);
1271 if (unlikely(skb == NULL)) {
1272 netdev_dbg(queue->vif->dev,
1273 "Can't allocate a skb in start_xmit.\n");
1274 xenvif_tx_err(queue, &txreq, idx);
1278 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1279 struct xen_netif_extra_info *gso;
1280 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1282 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
1283 /* Failure in xenvif_set_skb_gso is fatal. */
1289 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
1291 __skb_put(skb, data_len);
1292 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1293 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1294 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
1296 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
1297 virt_to_mfn(skb->data);
1298 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1299 queue->tx_copy_ops[*copy_ops].dest.offset =
1300 offset_in_page(skb->data);
1302 queue->tx_copy_ops[*copy_ops].len = data_len;
1303 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
1307 skb_shinfo(skb)->nr_frags = ret;
1308 if (data_len < txreq.size) {
1309 skb_shinfo(skb)->nr_frags++;
1310 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1312 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
1315 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1316 INVALID_PENDING_IDX);
1317 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
1321 queue->pending_cons++;
1323 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
1324 if (request_gop == NULL) {
1326 xenvif_tx_err(queue, &txreq, idx);
1331 __skb_queue_tail(&queue->tx_queue, skb);
1333 queue->tx.req_cons = idx;
1335 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1336 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
1340 (*map_ops) = gop - queue->tx_map_ops;
1344 /* Consolidate skb with a frag_list into a brand new one with local pages on
1345 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1347 static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
1349 unsigned int offset = skb_headlen(skb);
1350 skb_frag_t frags[MAX_SKB_FRAGS];
1352 struct ubuf_info *uarg;
1353 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1355 queue->stats.tx_zerocopy_sent += 2;
1356 queue->stats.tx_frag_overflow++;
1358 xenvif_fill_frags(queue, nskb);
1359 /* Subtract frags size, we will correct it later */
1360 skb->truesize -= skb->data_len;
1361 skb->len += nskb->len;
1362 skb->data_len += nskb->len;
1364 /* create a brand new frags array and coalesce there */
1365 for (i = 0; offset < skb->len; i++) {
1369 BUG_ON(i >= MAX_SKB_FRAGS);
1370 page = alloc_page(GFP_ATOMIC);
1373 skb->truesize += skb->data_len;
1374 for (j = 0; j < i; j++)
1375 put_page(frags[j].page.p);
1379 if (offset + PAGE_SIZE < skb->len)
1382 len = skb->len - offset;
1383 if (skb_copy_bits(skb, offset, page_address(page), len))
1387 frags[i].page.p = page;
1388 frags[i].page_offset = 0;
1389 skb_frag_size_set(&frags[i], len);
1392 /* Copied all the bits from the frag list -- free it. */
1393 skb_frag_list_init(skb);
1394 xenvif_skb_zerocopy_prepare(queue, nskb);
1397 /* Release all the original (foreign) frags. */
1398 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1399 skb_frag_unref(skb, f);
1400 uarg = skb_shinfo(skb)->destructor_arg;
1401 /* increase inflight counter to offset decrement in callback */
1402 atomic_inc(&queue->inflight_packets);
1403 uarg->callback(uarg, true);
1404 skb_shinfo(skb)->destructor_arg = NULL;
1406 /* Fill the skb with the new (local) frags. */
1407 memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
1408 skb_shinfo(skb)->nr_frags = i;
1409 skb->truesize += i * PAGE_SIZE;
1414 static int xenvif_tx_submit(struct xenvif_queue *queue)
1416 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1417 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
1418 struct sk_buff *skb;
1421 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
1422 struct xen_netif_tx_request *txp;
1426 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
1427 txp = &queue->pending_tx_info[pending_idx].req;
1429 /* Check the remap error code. */
1430 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
1431 /* If there was an error, xenvif_tx_check_gop is
1432 * expected to release all the frags which were mapped,
1433 * so kfree_skb shouldn't do it again
1435 skb_shinfo(skb)->nr_frags = 0;
1436 if (skb_has_frag_list(skb)) {
1437 struct sk_buff *nskb =
1438 skb_shinfo(skb)->frag_list;
1439 skb_shinfo(nskb)->nr_frags = 0;
1445 data_len = skb->len;
1446 callback_param(queue, pending_idx).ctx = NULL;
1447 if (data_len < txp->size) {
1448 /* Append the packet payload as a fragment. */
1449 txp->offset += data_len;
1450 txp->size -= data_len;
1452 /* Schedule a response immediately. */
1453 xenvif_idx_release(queue, pending_idx,
1454 XEN_NETIF_RSP_OKAY);
1457 if (txp->flags & XEN_NETTXF_csum_blank)
1458 skb->ip_summed = CHECKSUM_PARTIAL;
1459 else if (txp->flags & XEN_NETTXF_data_validated)
1460 skb->ip_summed = CHECKSUM_UNNECESSARY;
1462 xenvif_fill_frags(queue, skb);
1464 if (unlikely(skb_has_frag_list(skb))) {
1465 if (xenvif_handle_frag_list(queue, skb)) {
1466 if (net_ratelimit())
1467 netdev_err(queue->vif->dev,
1468 "Not enough memory to consolidate frag_list!\n");
1469 xenvif_skb_zerocopy_prepare(queue, skb);
1475 skb->dev = queue->vif->dev;
1476 skb->protocol = eth_type_trans(skb, skb->dev);
1477 skb_reset_network_header(skb);
1479 if (checksum_setup(queue, skb)) {
1480 netdev_dbg(queue->vif->dev,
1481 "Can't setup checksum in net_tx_action\n");
1482 /* We have to set this flag to trigger the callback */
1483 if (skb_shinfo(skb)->destructor_arg)
1484 xenvif_skb_zerocopy_prepare(queue, skb);
1489 skb_probe_transport_header(skb, 0);
1491 /* If the packet is GSO then we will have just set up the
1492 * transport header offset in checksum_setup so it's now
1493 * straightforward to calculate gso_segs.
1495 if (skb_is_gso(skb)) {
1496 int mss = skb_shinfo(skb)->gso_size;
1497 int hdrlen = skb_transport_header(skb) -
1498 skb_mac_header(skb) +
1501 skb_shinfo(skb)->gso_segs =
1502 DIV_ROUND_UP(skb->len - hdrlen, mss);
1505 queue->stats.rx_bytes += skb->len;
1506 queue->stats.rx_packets++;
1510 /* Set this flag right before netif_receive_skb, otherwise
1511 * someone might think this packet already left netback, and
1512 * do a skb_copy_ubufs while we are still in control of the
1513 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1515 if (skb_shinfo(skb)->destructor_arg) {
1516 xenvif_skb_zerocopy_prepare(queue, skb);
1517 queue->stats.tx_zerocopy_sent++;
1520 netif_receive_skb(skb);
1526 void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1528 unsigned long flags;
1529 pending_ring_idx_t index;
1530 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
1532 /* This is the only place where we grab this lock, to protect callbacks
1535 spin_lock_irqsave(&queue->callback_lock, flags);
1537 u16 pending_idx = ubuf->desc;
1538 ubuf = (struct ubuf_info *) ubuf->ctx;
1539 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
1541 index = pending_index(queue->dealloc_prod);
1542 queue->dealloc_ring[index] = pending_idx;
1543 /* Sync with xenvif_tx_dealloc_action:
1544 * insert idx then incr producer.
1547 queue->dealloc_prod++;
1549 wake_up(&queue->dealloc_wq);
1550 spin_unlock_irqrestore(&queue->callback_lock, flags);
1552 if (likely(zerocopy_success))
1553 queue->stats.tx_zerocopy_success++;
1555 queue->stats.tx_zerocopy_fail++;
1556 xenvif_skb_zerocopy_complete(queue);
1559 static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
1561 struct gnttab_unmap_grant_ref *gop;
1562 pending_ring_idx_t dc, dp;
1563 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1566 dc = queue->dealloc_cons;
1567 gop = queue->tx_unmap_ops;
1569 /* Free up any grants we have finished using */
1571 dp = queue->dealloc_prod;
1573 /* Ensure we see all indices enqueued by all
1574 * xenvif_zerocopy_callback().
1579 BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
1581 queue->dealloc_ring[pending_index(dc++)];
1583 pending_idx_release[gop - queue->tx_unmap_ops] =
1585 queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
1586 queue->mmap_pages[pending_idx];
1587 gnttab_set_unmap_op(gop,
1588 idx_to_kaddr(queue, pending_idx),
1590 queue->grant_tx_handle[pending_idx]);
1591 xenvif_grant_handle_reset(queue, pending_idx);
1595 } while (dp != queue->dealloc_prod);
1597 queue->dealloc_cons = dc;
1599 if (gop - queue->tx_unmap_ops > 0) {
1601 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
1603 queue->pages_to_unmap,
1604 gop - queue->tx_unmap_ops);
1606 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1607 gop - queue->tx_unmap_ops, ret);
1608 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
1609 if (gop[i].status != GNTST_okay)
1610 netdev_err(queue->vif->dev,
1611 " host_addr: %llx handle: %x status: %d\n",
1620 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1621 xenvif_idx_release(queue, pending_idx_release[i],
1622 XEN_NETIF_RSP_OKAY);
1626 /* Called after netfront has transmitted */
1627 int xenvif_tx_action(struct xenvif_queue *queue, int budget)
1629 unsigned nr_mops, nr_cops = 0;
1632 if (unlikely(!tx_work_todo(queue)))
1635 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
1640 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
1642 ret = gnttab_map_refs(queue->tx_map_ops,
1644 queue->pages_to_map,
1649 work_done = xenvif_tx_submit(queue);
1654 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
1657 struct pending_tx_info *pending_tx_info;
1658 pending_ring_idx_t index;
1659 unsigned long flags;
1661 pending_tx_info = &queue->pending_tx_info[pending_idx];
1663 spin_lock_irqsave(&queue->response_lock, flags);
1665 make_tx_response(queue, &pending_tx_info->req, status);
1667 /* Release the pending index before pusing the Tx response so
1668 * its available before a new Tx request is pushed by the
1671 index = pending_index(queue->pending_prod++);
1672 queue->pending_ring[index] = pending_idx;
1674 push_tx_responses(queue);
1676 spin_unlock_irqrestore(&queue->response_lock, flags);
1680 static void make_tx_response(struct xenvif_queue *queue,
1681 struct xen_netif_tx_request *txp,
1684 RING_IDX i = queue->tx.rsp_prod_pvt;
1685 struct xen_netif_tx_response *resp;
1687 resp = RING_GET_RESPONSE(&queue->tx, i);
1691 if (txp->flags & XEN_NETTXF_extra_info)
1692 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1694 queue->tx.rsp_prod_pvt = ++i;
1697 static void push_tx_responses(struct xenvif_queue *queue)
1701 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
1703 notify_remote_via_irq(queue->tx_irq);
1706 static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
1713 RING_IDX i = queue->rx.rsp_prod_pvt;
1714 struct xen_netif_rx_response *resp;
1716 resp = RING_GET_RESPONSE(&queue->rx, i);
1717 resp->offset = offset;
1718 resp->flags = flags;
1720 resp->status = (s16)size;
1722 resp->status = (s16)st;
1724 queue->rx.rsp_prod_pvt = ++i;
1729 void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
1732 struct gnttab_unmap_grant_ref tx_unmap_op;
1734 gnttab_set_unmap_op(&tx_unmap_op,
1735 idx_to_kaddr(queue, pending_idx),
1737 queue->grant_tx_handle[pending_idx]);
1738 xenvif_grant_handle_reset(queue, pending_idx);
1740 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1741 &queue->mmap_pages[pending_idx], 1);
1743 netdev_err(queue->vif->dev,
1744 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1747 tx_unmap_op.host_addr,
1749 tx_unmap_op.status);
1754 static inline int tx_work_todo(struct xenvif_queue *queue)
1756 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
1762 static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
1764 return queue->dealloc_cons != queue->dealloc_prod;
1767 void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
1769 if (queue->tx.sring)
1770 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1772 if (queue->rx.sring)
1773 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1777 int xenvif_map_frontend_rings(struct xenvif_queue *queue,
1778 grant_ref_t tx_ring_ref,
1779 grant_ref_t rx_ring_ref)
1782 struct xen_netif_tx_sring *txs;
1783 struct xen_netif_rx_sring *rxs;
1787 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1788 &tx_ring_ref, 1, &addr);
1792 txs = (struct xen_netif_tx_sring *)addr;
1793 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
1795 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1796 &rx_ring_ref, 1, &addr);
1800 rxs = (struct xen_netif_rx_sring *)addr;
1801 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
1806 xenvif_unmap_frontend_rings(queue);
1810 static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
1812 struct xenvif *vif = queue->vif;
1814 queue->stalled = true;
1816 /* At least one queue has stalled? Disable the carrier. */
1817 spin_lock(&vif->lock);
1818 if (vif->stalled_queues++ == 0) {
1819 netdev_info(vif->dev, "Guest Rx stalled");
1820 netif_carrier_off(vif->dev);
1822 spin_unlock(&vif->lock);
1825 static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
1827 struct xenvif *vif = queue->vif;
1829 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
1830 queue->stalled = false;
1832 /* All queues are ready? Enable the carrier. */
1833 spin_lock(&vif->lock);
1834 if (--vif->stalled_queues == 0) {
1835 netdev_info(vif->dev, "Guest Rx ready");
1836 netif_carrier_on(vif->dev);
1838 spin_unlock(&vif->lock);
1841 static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
1843 RING_IDX prod, cons;
1845 prod = queue->rx.sring->req_prod;
1846 cons = queue->rx.req_cons;
1848 return !queue->stalled
1849 && prod - cons < XEN_NETBK_RX_SLOTS_MAX
1850 && time_after(jiffies,
1851 queue->last_rx_time + queue->vif->stall_timeout);
1854 static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
1856 RING_IDX prod, cons;
1858 prod = queue->rx.sring->req_prod;
1859 cons = queue->rx.req_cons;
1861 return queue->stalled
1862 && prod - cons >= XEN_NETBK_RX_SLOTS_MAX;
1865 static bool xenvif_have_rx_work(struct xenvif_queue *queue)
1867 return (!skb_queue_empty(&queue->rx_queue)
1868 && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
1869 || (queue->vif->stall_timeout &&
1870 (xenvif_rx_queue_stalled(queue)
1871 || xenvif_rx_queue_ready(queue)))
1872 || kthread_should_stop()
1873 || queue->vif->disabled;
1876 static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
1878 struct sk_buff *skb;
1881 skb = skb_peek(&queue->rx_queue);
1883 return MAX_SCHEDULE_TIMEOUT;
1885 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
1886 return timeout < 0 ? 0 : timeout;
1889 /* Wait until the guest Rx thread has work.
1891 * The timeout needs to be adjusted based on the current head of the
1892 * queue (and not just the head at the beginning). In particular, if
1893 * the queue is initially empty an infinite timeout is used and this
1894 * needs to be reduced when a skb is queued.
1896 * This cannot be done with wait_event_timeout() because it only
1897 * calculates the timeout once.
1899 static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
1903 if (xenvif_have_rx_work(queue))
1909 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
1910 if (xenvif_have_rx_work(queue))
1912 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
1916 finish_wait(&queue->wq, &wait);
1919 int xenvif_kthread_guest_rx(void *data)
1921 struct xenvif_queue *queue = data;
1922 struct xenvif *vif = queue->vif;
1924 if (!vif->stall_timeout)
1925 xenvif_queue_carrier_on(queue);
1928 xenvif_wait_for_rx_work(queue);
1930 if (kthread_should_stop())
1933 /* This frontend is found to be rogue, disable it in
1934 * kthread context. Currently this is only set when
1935 * netback finds out frontend sends malformed packet,
1936 * but we cannot disable the interface in softirq
1937 * context so we defer it here, if this thread is
1938 * associated with queue 0.
1940 if (unlikely(vif->disabled && queue->id == 0)) {
1941 xenvif_carrier_off(vif);
1945 if (!skb_queue_empty(&queue->rx_queue))
1946 xenvif_rx_action(queue);
1948 /* If the guest hasn't provided any Rx slots for a
1949 * while it's probably not responsive, drop the
1950 * carrier so packets are dropped earlier.
1952 if (vif->stall_timeout) {
1953 if (xenvif_rx_queue_stalled(queue))
1954 xenvif_queue_carrier_off(queue);
1955 else if (xenvif_rx_queue_ready(queue))
1956 xenvif_queue_carrier_on(queue);
1959 /* Queued packets may have foreign pages from other
1960 * domains. These cannot be queued indefinitely as
1961 * this would starve guests of grant refs and transmit
1964 xenvif_rx_queue_drop_expired(queue);
1966 xenvif_rx_queue_maybe_wake(queue);
1971 /* Bin any remaining skbs */
1972 xenvif_rx_queue_purge(queue);
1977 static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
1979 /* Dealloc thread must remain running until all inflight
1982 return kthread_should_stop() &&
1983 !atomic_read(&queue->inflight_packets);
1986 int xenvif_dealloc_kthread(void *data)
1988 struct xenvif_queue *queue = data;
1991 wait_event_interruptible(queue->dealloc_wq,
1992 tx_dealloc_work_todo(queue) ||
1993 xenvif_dealloc_kthread_should_stop(queue));
1994 if (xenvif_dealloc_kthread_should_stop(queue))
1997 xenvif_tx_dealloc_action(queue);
2001 /* Unmap anything remaining*/
2002 if (tx_dealloc_work_todo(queue))
2003 xenvif_tx_dealloc_action(queue);
2008 static int __init netback_init(void)
2015 /* Allow as many queues as there are CPUs but max. 8 if user has not
2016 * specified a value.
2018 if (xenvif_max_queues == 0)
2019 xenvif_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2022 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
2023 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2024 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
2025 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
2028 rc = xenvif_xenbus_init();
2032 #ifdef CONFIG_DEBUG_FS
2033 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2034 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2035 pr_warn("Init of debugfs returned %ld!\n",
2036 PTR_ERR(xen_netback_dbg_root));
2037 #endif /* CONFIG_DEBUG_FS */
2045 module_init(netback_init);
2047 static void __exit netback_fini(void)
2049 #ifdef CONFIG_DEBUG_FS
2050 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2051 debugfs_remove_recursive(xen_netback_dbg_root);
2052 #endif /* CONFIG_DEBUG_FS */
2053 xenvif_xenbus_fini();
2055 module_exit(netback_fini);
2057 MODULE_LICENSE("Dual BSD/GPL");
2058 MODULE_ALIAS("xen-backend:vif");