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);
63 unsigned int rx_drain_timeout_jiffies;
65 unsigned int xenvif_max_queues;
66 module_param_named(max_queues, xenvif_max_queues, uint, 0644);
67 MODULE_PARM_DESC(max_queues,
68 "Maximum number of queues per virtual interface");
71 * This is the maximum slots a skb can have. If a guest sends a skb
72 * which exceeds this limit it is considered malicious.
74 #define FATAL_SKB_SLOTS_DEFAULT 20
75 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
76 module_param(fatal_skb_slots, uint, 0444);
78 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
81 static void make_tx_response(struct xenvif_queue *queue,
82 struct xen_netif_tx_request *txp,
85 static inline int tx_work_todo(struct xenvif_queue *queue);
87 static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
94 static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
97 return page_to_pfn(queue->mmap_pages[idx]);
100 static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
103 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
106 #define callback_param(vif, pending_idx) \
107 (vif->pending_tx_info[pending_idx].callback_struct)
109 /* Find the containing VIF's structure from a pointer in pending_tx_info array
111 static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
113 u16 pending_idx = ubuf->desc;
114 struct pending_tx_info *temp =
115 container_of(ubuf, struct pending_tx_info, callback_struct);
116 return container_of(temp - pending_idx,
121 /* This is a miniumum size for the linear area to avoid lots of
122 * calls to __pskb_pull_tail() as we set up checksum offsets. The
123 * value 128 was chosen as it covers all IPv4 and most likely
126 #define PKT_PROT_LEN 128
128 static u16 frag_get_pending_idx(skb_frag_t *frag)
130 return (u16)frag->page_offset;
133 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
135 frag->page_offset = pending_idx;
138 static inline pending_ring_idx_t pending_index(unsigned i)
140 return i & (MAX_PENDING_REQS-1);
143 bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
148 prod = queue->rx.sring->req_prod;
149 cons = queue->rx.req_cons;
151 if (prod - cons >= needed)
154 queue->rx.sring->req_event = prod + 1;
156 /* Make sure event is visible before we check prod
160 } while (queue->rx.sring->req_prod != prod);
165 void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
169 spin_lock_irqsave(&queue->rx_queue.lock, flags);
171 __skb_queue_tail(&queue->rx_queue, skb);
173 queue->rx_queue_len += skb->len;
174 if (queue->rx_queue_len > queue->rx_queue_max)
175 netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
177 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
180 static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
184 spin_lock_irq(&queue->rx_queue.lock);
186 skb = __skb_dequeue(&queue->rx_queue);
188 queue->rx_queue_len -= skb->len;
190 spin_unlock_irq(&queue->rx_queue.lock);
195 static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
197 spin_lock_irq(&queue->rx_queue.lock);
199 if (queue->rx_queue_len < queue->rx_queue_max)
200 netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
202 spin_unlock_irq(&queue->rx_queue.lock);
206 static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
209 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
213 static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
218 skb = skb_peek(&queue->rx_queue);
221 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
223 xenvif_rx_dequeue(queue);
229 * Returns true if we should start a new receive buffer instead of
230 * adding 'size' bytes to a buffer which currently contains 'offset'
233 static bool start_new_rx_buffer(int offset, unsigned long size, int head,
236 /* simple case: we have completely filled the current buffer. */
237 if (offset == MAX_BUFFER_OFFSET)
241 * complex case: start a fresh buffer if the current frag
242 * would overflow the current buffer but only if:
243 * (i) this frag would fit completely in the next buffer
244 * and (ii) there is already some data in the current buffer
245 * and (iii) this is not the head buffer.
246 * and (iv) there is no need to fully utilize the buffers
249 * - (i) stops us splitting a frag into two copies
250 * unless the frag is too large for a single buffer.
251 * - (ii) stops us from leaving a buffer pointlessly empty.
252 * - (iii) stops us leaving the first buffer
253 * empty. Strictly speaking this is already covered
254 * by (ii) but is explicitly checked because
255 * netfront relies on the first buffer being
256 * non-empty and can crash otherwise.
257 * - (iv) is needed for skbs which can use up more than MAX_SKB_FRAGS
260 * This means we will effectively linearise small
261 * frags but do not needlessly split large buffers
262 * into multiple copies tend to give large frags their
263 * own buffers as before.
265 BUG_ON(size > MAX_BUFFER_OFFSET);
266 if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head &&
273 struct netrx_pending_operations {
274 unsigned copy_prod, copy_cons;
275 unsigned meta_prod, meta_cons;
276 struct gnttab_copy *copy;
277 struct xenvif_rx_meta *meta;
279 grant_ref_t copy_gref;
282 static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
283 struct netrx_pending_operations *npo)
285 struct xenvif_rx_meta *meta;
286 struct xen_netif_rx_request *req;
288 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
290 meta = npo->meta + npo->meta_prod++;
291 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
297 npo->copy_gref = req->gref;
303 * Set up the grant operations for this fragment. If it's a flipping
304 * interface, we also set up the unmap request from here.
306 static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
307 struct netrx_pending_operations *npo,
308 struct page *page, unsigned long size,
309 unsigned long offset, int *head,
310 struct xenvif_queue *foreign_queue,
311 grant_ref_t foreign_gref)
313 struct gnttab_copy *copy_gop;
314 struct xenvif_rx_meta *meta;
316 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
318 /* Data must not cross a page boundary. */
319 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
321 meta = npo->meta + npo->meta_prod - 1;
323 /* Skip unused frames from start of page */
324 page += offset >> PAGE_SHIFT;
325 offset &= ~PAGE_MASK;
328 BUG_ON(offset >= PAGE_SIZE);
329 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
331 bytes = PAGE_SIZE - offset;
336 if (start_new_rx_buffer(npo->copy_off,
339 XENVIF_RX_CB(skb)->full_coalesce)) {
341 * Netfront requires there to be some data in the head
346 meta = get_next_rx_buffer(queue, npo);
349 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
350 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
352 copy_gop = npo->copy + npo->copy_prod++;
353 copy_gop->flags = GNTCOPY_dest_gref;
354 copy_gop->len = bytes;
357 copy_gop->source.domid = foreign_queue->vif->domid;
358 copy_gop->source.u.ref = foreign_gref;
359 copy_gop->flags |= GNTCOPY_source_gref;
361 copy_gop->source.domid = DOMID_SELF;
362 copy_gop->source.u.gmfn =
363 virt_to_mfn(page_address(page));
365 copy_gop->source.offset = offset;
367 copy_gop->dest.domid = queue->vif->domid;
368 copy_gop->dest.offset = npo->copy_off;
369 copy_gop->dest.u.ref = npo->copy_gref;
371 npo->copy_off += bytes;
378 if (offset == PAGE_SIZE && size) {
379 BUG_ON(!PageCompound(page));
384 /* Leave a gap for the GSO descriptor. */
385 if (skb_is_gso(skb)) {
386 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
387 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
388 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
389 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
392 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
393 queue->rx.req_cons++;
395 *head = 0; /* There must be something in this buffer now. */
401 * Find the grant ref for a given frag in a chain of struct ubuf_info's
402 * skb: the skb itself
403 * i: the frag's number
404 * ubuf: a pointer to an element in the chain. It should not be NULL
406 * Returns a pointer to the element in the chain where the page were found. If
407 * not found, returns NULL.
408 * See the definition of callback_struct in common.h for more details about
411 static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb,
413 const struct ubuf_info *ubuf)
415 struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf);
418 u16 pending_idx = ubuf->desc;
420 if (skb_shinfo(skb)->frags[i].page.p ==
421 foreign_queue->mmap_pages[pending_idx])
423 ubuf = (struct ubuf_info *) ubuf->ctx;
430 * Prepare an SKB to be transmitted to the frontend.
432 * This function is responsible for allocating grant operations, meta
435 * It returns the number of meta structures consumed. The number of
436 * ring slots used is always equal to the number of meta slots used
437 * plus the number of GSO descriptors used. Currently, we use either
438 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
439 * frontend-side LRO).
441 static int xenvif_gop_skb(struct sk_buff *skb,
442 struct netrx_pending_operations *npo,
443 struct xenvif_queue *queue)
445 struct xenvif *vif = netdev_priv(skb->dev);
446 int nr_frags = skb_shinfo(skb)->nr_frags;
448 struct xen_netif_rx_request *req;
449 struct xenvif_rx_meta *meta;
454 const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
455 const struct ubuf_info *const head_ubuf = ubuf;
457 old_meta_prod = npo->meta_prod;
459 gso_type = XEN_NETIF_GSO_TYPE_NONE;
460 if (skb_is_gso(skb)) {
461 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
462 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
463 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
464 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
467 /* Set up a GSO prefix descriptor, if necessary */
468 if ((1 << gso_type) & vif->gso_prefix_mask) {
469 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
470 meta = npo->meta + npo->meta_prod++;
471 meta->gso_type = gso_type;
472 meta->gso_size = skb_shinfo(skb)->gso_size;
477 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
478 meta = npo->meta + npo->meta_prod++;
480 if ((1 << gso_type) & vif->gso_mask) {
481 meta->gso_type = gso_type;
482 meta->gso_size = skb_shinfo(skb)->gso_size;
484 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
491 npo->copy_gref = req->gref;
494 while (data < skb_tail_pointer(skb)) {
495 unsigned int offset = offset_in_page(data);
496 unsigned int len = PAGE_SIZE - offset;
498 if (data + len > skb_tail_pointer(skb))
499 len = skb_tail_pointer(skb) - data;
501 xenvif_gop_frag_copy(queue, skb, npo,
502 virt_to_page(data), len, offset, &head,
508 for (i = 0; i < nr_frags; i++) {
509 /* This variable also signals whether foreign_gref has a real
512 struct xenvif_queue *foreign_queue = NULL;
513 grant_ref_t foreign_gref;
515 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
516 (ubuf->callback == &xenvif_zerocopy_callback)) {
517 const struct ubuf_info *const startpoint = ubuf;
519 /* Ideally ubuf points to the chain element which
520 * belongs to this frag. Or if frags were removed from
521 * the beginning, then shortly before it.
523 ubuf = xenvif_find_gref(skb, i, ubuf);
525 /* Try again from the beginning of the list, if we
526 * haven't tried from there. This only makes sense in
527 * the unlikely event of reordering the original frags.
528 * For injected local pages it's an unnecessary second
531 if (unlikely(!ubuf) && startpoint != head_ubuf)
532 ubuf = xenvif_find_gref(skb, i, head_ubuf);
535 u16 pending_idx = ubuf->desc;
537 foreign_queue = ubuf_to_queue(ubuf);
539 foreign_queue->pending_tx_info[pending_idx].req.gref;
540 /* Just a safety measure. If this was the last
541 * element on the list, the for loop will
542 * iterate again if a local page were added to
543 * the end. Using head_ubuf here prevents the
544 * second search on the chain. Or the original
545 * frags changed order, but that's less likely.
546 * In any way, ubuf shouldn't be NULL.
549 (struct ubuf_info *) ubuf->ctx :
552 /* This frag was a local page, added to the
553 * array after the skb left netback.
557 xenvif_gop_frag_copy(queue, skb, npo,
558 skb_frag_page(&skb_shinfo(skb)->frags[i]),
559 skb_frag_size(&skb_shinfo(skb)->frags[i]),
560 skb_shinfo(skb)->frags[i].page_offset,
563 foreign_queue ? foreign_gref : UINT_MAX);
566 return npo->meta_prod - old_meta_prod;
570 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
571 * used to set up the operations on the top of
572 * netrx_pending_operations, which have since been done. Check that
573 * they didn't give any errors and advance over them.
575 static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
576 struct netrx_pending_operations *npo)
578 struct gnttab_copy *copy_op;
579 int status = XEN_NETIF_RSP_OKAY;
582 for (i = 0; i < nr_meta_slots; i++) {
583 copy_op = npo->copy + npo->copy_cons++;
584 if (copy_op->status != GNTST_okay) {
586 "Bad status %d from copy to DOM%d.\n",
587 copy_op->status, vif->domid);
588 status = XEN_NETIF_RSP_ERROR;
595 static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
596 struct xenvif_rx_meta *meta,
600 unsigned long offset;
602 /* No fragments used */
603 if (nr_meta_slots <= 1)
608 for (i = 0; i < nr_meta_slots; i++) {
610 if (i == nr_meta_slots - 1)
613 flags = XEN_NETRXF_more_data;
616 make_rx_response(queue, meta[i].id, status, offset,
617 meta[i].size, flags);
621 void xenvif_kick_thread(struct xenvif_queue *queue)
626 static void xenvif_rx_action(struct xenvif_queue *queue)
630 struct xen_netif_rx_response *resp;
631 struct sk_buff_head rxq;
635 unsigned long offset;
636 bool need_to_notify = false;
638 struct netrx_pending_operations npo = {
639 .copy = queue->grant_copy_op,
643 skb_queue_head_init(&rxq);
645 while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
646 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
647 RING_IDX max_slots_needed;
648 RING_IDX old_req_cons;
649 RING_IDX ring_slots_used;
652 /* We need a cheap worse case estimate for the number of
656 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
659 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
663 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
664 offset = skb_shinfo(skb)->frags[i].page_offset;
666 /* For a worse-case estimate we need to factor in
667 * the fragment page offset as this will affect the
668 * number of times xenvif_gop_frag_copy() will
669 * call start_new_rx_buffer().
671 max_slots_needed += DIV_ROUND_UP(offset + size,
675 /* To avoid the estimate becoming too pessimal for some
676 * frontends that limit posted rx requests, cap the estimate
677 * at MAX_SKB_FRAGS. In this case netback will fully coalesce
678 * the skb into the provided slots.
680 if (max_slots_needed > MAX_SKB_FRAGS) {
681 max_slots_needed = MAX_SKB_FRAGS;
682 XENVIF_RX_CB(skb)->full_coalesce = true;
684 XENVIF_RX_CB(skb)->full_coalesce = false;
687 /* We may need one more slot for GSO metadata */
688 if (skb_is_gso(skb) &&
689 (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
690 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
693 old_req_cons = queue->rx.req_cons;
694 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
695 ring_slots_used = queue->rx.req_cons - old_req_cons;
697 BUG_ON(ring_slots_used > max_slots_needed);
699 __skb_queue_tail(&rxq, skb);
702 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
707 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
708 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
710 while ((skb = __skb_dequeue(&rxq)) != NULL) {
712 if ((1 << queue->meta[npo.meta_cons].gso_type) &
713 queue->vif->gso_prefix_mask) {
714 resp = RING_GET_RESPONSE(&queue->rx,
715 queue->rx.rsp_prod_pvt++);
717 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
719 resp->offset = queue->meta[npo.meta_cons].gso_size;
720 resp->id = queue->meta[npo.meta_cons].id;
721 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
724 XENVIF_RX_CB(skb)->meta_slots_used--;
728 queue->stats.tx_bytes += skb->len;
729 queue->stats.tx_packets++;
731 status = xenvif_check_gop(queue->vif,
732 XENVIF_RX_CB(skb)->meta_slots_used,
735 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
738 flags = XEN_NETRXF_more_data;
740 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
741 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
742 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
743 /* remote but checksummed. */
744 flags |= XEN_NETRXF_data_validated;
747 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
749 queue->meta[npo.meta_cons].size,
752 if ((1 << queue->meta[npo.meta_cons].gso_type) &
753 queue->vif->gso_mask) {
754 struct xen_netif_extra_info *gso =
755 (struct xen_netif_extra_info *)
756 RING_GET_RESPONSE(&queue->rx,
757 queue->rx.rsp_prod_pvt++);
759 resp->flags |= XEN_NETRXF_extra_info;
761 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
762 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
764 gso->u.gso.features = 0;
766 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
770 xenvif_add_frag_responses(queue, status,
771 queue->meta + npo.meta_cons + 1,
772 XENVIF_RX_CB(skb)->meta_slots_used);
774 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
776 need_to_notify |= !!ret;
778 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
784 notify_remote_via_irq(queue->rx_irq);
787 void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
791 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
794 napi_schedule(&queue->napi);
797 static void tx_add_credit(struct xenvif_queue *queue)
799 unsigned long max_burst, max_credit;
802 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
803 * Otherwise the interface can seize up due to insufficient credit.
805 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
806 max_burst = min(max_burst, 131072UL);
807 max_burst = max(max_burst, queue->credit_bytes);
809 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
810 max_credit = queue->remaining_credit + queue->credit_bytes;
811 if (max_credit < queue->remaining_credit)
812 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
814 queue->remaining_credit = min(max_credit, max_burst);
817 static void tx_credit_callback(unsigned long data)
819 struct xenvif_queue *queue = (struct xenvif_queue *)data;
820 tx_add_credit(queue);
821 xenvif_napi_schedule_or_enable_events(queue);
824 static void xenvif_tx_err(struct xenvif_queue *queue,
825 struct xen_netif_tx_request *txp, RING_IDX end)
827 RING_IDX cons = queue->tx.req_cons;
831 spin_lock_irqsave(&queue->response_lock, flags);
832 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
833 spin_unlock_irqrestore(&queue->response_lock, flags);
836 txp = RING_GET_REQUEST(&queue->tx, cons++);
838 queue->tx.req_cons = cons;
841 static void xenvif_fatal_tx_err(struct xenvif *vif)
843 netdev_err(vif->dev, "fatal error; disabling device\n");
844 vif->disabled = true;
845 /* Disable the vif from queue 0's kthread */
847 xenvif_kick_thread(&vif->queues[0]);
850 static int xenvif_count_requests(struct xenvif_queue *queue,
851 struct xen_netif_tx_request *first,
852 struct xen_netif_tx_request *txp,
855 RING_IDX cons = queue->tx.req_cons;
860 if (!(first->flags & XEN_NETTXF_more_data))
864 struct xen_netif_tx_request dropped_tx = { 0 };
866 if (slots >= work_to_do) {
867 netdev_err(queue->vif->dev,
868 "Asked for %d slots but exceeds this limit\n",
870 xenvif_fatal_tx_err(queue->vif);
874 /* This guest is really using too many slots and
875 * considered malicious.
877 if (unlikely(slots >= fatal_skb_slots)) {
878 netdev_err(queue->vif->dev,
879 "Malicious frontend using %d slots, threshold %u\n",
880 slots, fatal_skb_slots);
881 xenvif_fatal_tx_err(queue->vif);
885 /* Xen network protocol had implicit dependency on
886 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
887 * the historical MAX_SKB_FRAGS value 18 to honor the
888 * same behavior as before. Any packet using more than
889 * 18 slots but less than fatal_skb_slots slots is
892 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
894 netdev_dbg(queue->vif->dev,
895 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
896 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
903 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
906 /* If the guest submitted a frame >= 64 KiB then
907 * first->size overflowed and following slots will
908 * appear to be larger than the frame.
910 * This cannot be fatal error as there are buggy
911 * frontends that do this.
913 * Consume all slots and drop the packet.
915 if (!drop_err && txp->size > first->size) {
917 netdev_dbg(queue->vif->dev,
918 "Invalid tx request, slot size %u > remaining size %u\n",
919 txp->size, first->size);
923 first->size -= txp->size;
926 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
927 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
928 txp->offset, txp->size);
929 xenvif_fatal_tx_err(queue->vif);
933 more_data = txp->flags & XEN_NETTXF_more_data;
941 xenvif_tx_err(queue, first, cons + slots);
949 struct xenvif_tx_cb {
953 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
955 static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
957 struct xen_netif_tx_request *txp,
958 struct gnttab_map_grant_ref *mop)
960 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
961 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
962 GNTMAP_host_map | GNTMAP_readonly,
963 txp->gref, queue->vif->domid);
965 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
969 static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
971 struct sk_buff *skb =
972 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
973 GFP_ATOMIC | __GFP_NOWARN);
974 if (unlikely(skb == NULL))
977 /* Packets passed to netif_rx() must have some headroom. */
978 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
980 /* Initialize it here to avoid later surprises */
981 skb_shinfo(skb)->destructor_arg = NULL;
986 static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
988 struct xen_netif_tx_request *txp,
989 struct gnttab_map_grant_ref *gop)
991 struct skb_shared_info *shinfo = skb_shinfo(skb);
992 skb_frag_t *frags = shinfo->frags;
993 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
995 pending_ring_idx_t index;
996 unsigned int nr_slots, frag_overflow = 0;
998 /* At this point shinfo->nr_frags is in fact the number of
999 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
1001 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
1002 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
1003 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1004 shinfo->nr_frags = MAX_SKB_FRAGS;
1006 nr_slots = shinfo->nr_frags;
1008 /* Skip first skb fragment if it is on same page as header fragment. */
1009 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
1011 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
1012 shinfo->nr_frags++, txp++, gop++) {
1013 index = pending_index(queue->pending_cons++);
1014 pending_idx = queue->pending_ring[index];
1015 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
1016 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
1019 if (frag_overflow) {
1020 struct sk_buff *nskb = xenvif_alloc_skb(0);
1021 if (unlikely(nskb == NULL)) {
1022 if (net_ratelimit())
1023 netdev_err(queue->vif->dev,
1024 "Can't allocate the frag_list skb.\n");
1028 shinfo = skb_shinfo(nskb);
1029 frags = shinfo->frags;
1031 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
1032 shinfo->nr_frags++, txp++, gop++) {
1033 index = pending_index(queue->pending_cons++);
1034 pending_idx = queue->pending_ring[index];
1035 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
1036 frag_set_pending_idx(&frags[shinfo->nr_frags],
1040 skb_shinfo(skb)->frag_list = nskb;
1046 static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
1048 grant_handle_t handle)
1050 if (unlikely(queue->grant_tx_handle[pending_idx] !=
1051 NETBACK_INVALID_HANDLE)) {
1052 netdev_err(queue->vif->dev,
1053 "Trying to overwrite active handle! pending_idx: %x\n",
1057 queue->grant_tx_handle[pending_idx] = handle;
1060 static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
1063 if (unlikely(queue->grant_tx_handle[pending_idx] ==
1064 NETBACK_INVALID_HANDLE)) {
1065 netdev_err(queue->vif->dev,
1066 "Trying to unmap invalid handle! pending_idx: %x\n",
1070 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
1073 static int xenvif_tx_check_gop(struct xenvif_queue *queue,
1074 struct sk_buff *skb,
1075 struct gnttab_map_grant_ref **gopp_map,
1076 struct gnttab_copy **gopp_copy)
1078 struct gnttab_map_grant_ref *gop_map = *gopp_map;
1079 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
1080 /* This always points to the shinfo of the skb being checked, which
1081 * could be either the first or the one on the frag_list
1083 struct skb_shared_info *shinfo = skb_shinfo(skb);
1084 /* If this is non-NULL, we are currently checking the frag_list skb, and
1085 * this points to the shinfo of the first one
1087 struct skb_shared_info *first_shinfo = NULL;
1088 int nr_frags = shinfo->nr_frags;
1089 const bool sharedslot = nr_frags &&
1090 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
1093 /* Check status of header. */
1094 err = (*gopp_copy)->status;
1095 if (unlikely(err)) {
1096 if (net_ratelimit())
1097 netdev_dbg(queue->vif->dev,
1098 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
1099 (*gopp_copy)->status,
1101 (*gopp_copy)->source.u.ref);
1102 /* The first frag might still have this slot mapped */
1104 xenvif_idx_release(queue, pending_idx,
1105 XEN_NETIF_RSP_ERROR);
1110 for (i = 0; i < nr_frags; i++, gop_map++) {
1113 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
1115 /* Check error status: if okay then remember grant handle. */
1116 newerr = gop_map->status;
1118 if (likely(!newerr)) {
1119 xenvif_grant_handle_set(queue,
1122 /* Had a previous error? Invalidate this fragment. */
1123 if (unlikely(err)) {
1124 xenvif_idx_unmap(queue, pending_idx);
1125 /* If the mapping of the first frag was OK, but
1126 * the header's copy failed, and they are
1127 * sharing a slot, send an error
1129 if (i == 0 && sharedslot)
1130 xenvif_idx_release(queue, pending_idx,
1131 XEN_NETIF_RSP_ERROR);
1133 xenvif_idx_release(queue, pending_idx,
1134 XEN_NETIF_RSP_OKAY);
1139 /* Error on this fragment: respond to client with an error. */
1140 if (net_ratelimit())
1141 netdev_dbg(queue->vif->dev,
1142 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
1148 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
1150 /* Not the first error? Preceding frags already invalidated. */
1154 /* First error: if the header haven't shared a slot with the
1155 * first frag, release it as well.
1158 xenvif_idx_release(queue,
1159 XENVIF_TX_CB(skb)->pending_idx,
1160 XEN_NETIF_RSP_OKAY);
1162 /* Invalidate preceding fragments of this skb. */
1163 for (j = 0; j < i; j++) {
1164 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1165 xenvif_idx_unmap(queue, pending_idx);
1166 xenvif_idx_release(queue, pending_idx,
1167 XEN_NETIF_RSP_OKAY);
1170 /* And if we found the error while checking the frag_list, unmap
1171 * the first skb's frags
1174 for (j = 0; j < first_shinfo->nr_frags; j++) {
1175 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1176 xenvif_idx_unmap(queue, pending_idx);
1177 xenvif_idx_release(queue, pending_idx,
1178 XEN_NETIF_RSP_OKAY);
1182 /* Remember the error: invalidate all subsequent fragments. */
1186 if (skb_has_frag_list(skb) && !first_shinfo) {
1187 first_shinfo = skb_shinfo(skb);
1188 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
1189 nr_frags = shinfo->nr_frags;
1194 *gopp_map = gop_map;
1198 static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
1200 struct skb_shared_info *shinfo = skb_shinfo(skb);
1201 int nr_frags = shinfo->nr_frags;
1203 u16 prev_pending_idx = INVALID_PENDING_IDX;
1205 for (i = 0; i < nr_frags; i++) {
1206 skb_frag_t *frag = shinfo->frags + i;
1207 struct xen_netif_tx_request *txp;
1211 pending_idx = frag_get_pending_idx(frag);
1213 /* If this is not the first frag, chain it to the previous*/
1214 if (prev_pending_idx == INVALID_PENDING_IDX)
1215 skb_shinfo(skb)->destructor_arg =
1216 &callback_param(queue, pending_idx);
1218 callback_param(queue, prev_pending_idx).ctx =
1219 &callback_param(queue, pending_idx);
1221 callback_param(queue, pending_idx).ctx = NULL;
1222 prev_pending_idx = pending_idx;
1224 txp = &queue->pending_tx_info[pending_idx].req;
1225 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
1226 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1227 skb->len += txp->size;
1228 skb->data_len += txp->size;
1229 skb->truesize += txp->size;
1231 /* Take an extra reference to offset network stack's put_page */
1232 get_page(queue->mmap_pages[pending_idx]);
1234 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1235 * overlaps with "index", and "mapping" is not set. I think mapping
1236 * should be set. If delivered to local stack, it would drop this
1237 * skb in sk_filter unless the socket has the right to use it.
1239 skb->pfmemalloc = false;
1242 static int xenvif_get_extras(struct xenvif_queue *queue,
1243 struct xen_netif_extra_info *extras,
1246 struct xen_netif_extra_info extra;
1247 RING_IDX cons = queue->tx.req_cons;
1250 if (unlikely(work_to_do-- <= 0)) {
1251 netdev_err(queue->vif->dev, "Missing extra info\n");
1252 xenvif_fatal_tx_err(queue->vif);
1256 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
1258 if (unlikely(!extra.type ||
1259 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1260 queue->tx.req_cons = ++cons;
1261 netdev_err(queue->vif->dev,
1262 "Invalid extra type: %d\n", extra.type);
1263 xenvif_fatal_tx_err(queue->vif);
1267 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1268 queue->tx.req_cons = ++cons;
1269 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1274 static int xenvif_set_skb_gso(struct xenvif *vif,
1275 struct sk_buff *skb,
1276 struct xen_netif_extra_info *gso)
1278 if (!gso->u.gso.size) {
1279 netdev_err(vif->dev, "GSO size must not be zero.\n");
1280 xenvif_fatal_tx_err(vif);
1284 switch (gso->u.gso.type) {
1285 case XEN_NETIF_GSO_TYPE_TCPV4:
1286 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1288 case XEN_NETIF_GSO_TYPE_TCPV6:
1289 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1292 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1293 xenvif_fatal_tx_err(vif);
1297 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1298 /* gso_segs will be calculated later */
1303 static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
1305 bool recalculate_partial_csum = false;
1307 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1308 * peers can fail to set NETRXF_csum_blank when sending a GSO
1309 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1310 * recalculate the partial checksum.
1312 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1313 queue->stats.rx_gso_checksum_fixup++;
1314 skb->ip_summed = CHECKSUM_PARTIAL;
1315 recalculate_partial_csum = true;
1318 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1319 if (skb->ip_summed != CHECKSUM_PARTIAL)
1322 return skb_checksum_setup(skb, recalculate_partial_csum);
1325 static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
1327 u64 now = get_jiffies_64();
1328 u64 next_credit = queue->credit_window_start +
1329 msecs_to_jiffies(queue->credit_usec / 1000);
1331 /* Timer could already be pending in rare cases. */
1332 if (timer_pending(&queue->credit_timeout))
1335 /* Passed the point where we can replenish credit? */
1336 if (time_after_eq64(now, next_credit)) {
1337 queue->credit_window_start = now;
1338 tx_add_credit(queue);
1341 /* Still too big to send right now? Set a callback. */
1342 if (size > queue->remaining_credit) {
1343 queue->credit_timeout.data =
1344 (unsigned long)queue;
1345 queue->credit_timeout.function =
1347 mod_timer(&queue->credit_timeout,
1349 queue->credit_window_start = next_credit;
1357 static void xenvif_tx_build_gops(struct xenvif_queue *queue,
1362 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
1363 struct sk_buff *skb;
1366 while (skb_queue_len(&queue->tx_queue) < budget) {
1367 struct xen_netif_tx_request txreq;
1368 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1369 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1373 unsigned int data_len;
1374 pending_ring_idx_t index;
1376 if (queue->tx.sring->req_prod - queue->tx.req_cons >
1377 XEN_NETIF_TX_RING_SIZE) {
1378 netdev_err(queue->vif->dev,
1379 "Impossible number of requests. "
1380 "req_prod %d, req_cons %d, size %ld\n",
1381 queue->tx.sring->req_prod, queue->tx.req_cons,
1382 XEN_NETIF_TX_RING_SIZE);
1383 xenvif_fatal_tx_err(queue->vif);
1387 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
1391 idx = queue->tx.req_cons;
1392 rmb(); /* Ensure that we see the request before we copy it. */
1393 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
1395 /* Credit-based scheduling. */
1396 if (txreq.size > queue->remaining_credit &&
1397 tx_credit_exceeded(queue, txreq.size))
1400 queue->remaining_credit -= txreq.size;
1403 queue->tx.req_cons = ++idx;
1405 memset(extras, 0, sizeof(extras));
1406 if (txreq.flags & XEN_NETTXF_extra_info) {
1407 work_to_do = xenvif_get_extras(queue, extras,
1409 idx = queue->tx.req_cons;
1410 if (unlikely(work_to_do < 0))
1414 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
1415 if (unlikely(ret < 0))
1420 if (unlikely(txreq.size < ETH_HLEN)) {
1421 netdev_dbg(queue->vif->dev,
1422 "Bad packet size: %d\n", txreq.size);
1423 xenvif_tx_err(queue, &txreq, idx);
1427 /* No crossing a page as the payload mustn't fragment. */
1428 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1429 netdev_err(queue->vif->dev,
1430 "txreq.offset: %x, size: %u, end: %lu\n",
1431 txreq.offset, txreq.size,
1432 (txreq.offset&~PAGE_MASK) + txreq.size);
1433 xenvif_fatal_tx_err(queue->vif);
1437 index = pending_index(queue->pending_cons);
1438 pending_idx = queue->pending_ring[index];
1440 data_len = (txreq.size > PKT_PROT_LEN &&
1441 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1442 PKT_PROT_LEN : txreq.size;
1444 skb = xenvif_alloc_skb(data_len);
1445 if (unlikely(skb == NULL)) {
1446 netdev_dbg(queue->vif->dev,
1447 "Can't allocate a skb in start_xmit.\n");
1448 xenvif_tx_err(queue, &txreq, idx);
1452 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1453 struct xen_netif_extra_info *gso;
1454 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1456 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
1457 /* Failure in xenvif_set_skb_gso is fatal. */
1463 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
1465 __skb_put(skb, data_len);
1466 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1467 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1468 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
1470 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
1471 virt_to_mfn(skb->data);
1472 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1473 queue->tx_copy_ops[*copy_ops].dest.offset =
1474 offset_in_page(skb->data);
1476 queue->tx_copy_ops[*copy_ops].len = data_len;
1477 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
1481 skb_shinfo(skb)->nr_frags = ret;
1482 if (data_len < txreq.size) {
1483 skb_shinfo(skb)->nr_frags++;
1484 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1486 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
1489 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1490 INVALID_PENDING_IDX);
1491 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
1495 queue->pending_cons++;
1497 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
1498 if (request_gop == NULL) {
1500 xenvif_tx_err(queue, &txreq, idx);
1505 __skb_queue_tail(&queue->tx_queue, skb);
1507 queue->tx.req_cons = idx;
1509 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1510 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
1514 (*map_ops) = gop - queue->tx_map_ops;
1518 /* Consolidate skb with a frag_list into a brand new one with local pages on
1519 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1521 static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
1523 unsigned int offset = skb_headlen(skb);
1524 skb_frag_t frags[MAX_SKB_FRAGS];
1526 struct ubuf_info *uarg;
1527 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1529 queue->stats.tx_zerocopy_sent += 2;
1530 queue->stats.tx_frag_overflow++;
1532 xenvif_fill_frags(queue, nskb);
1533 /* Subtract frags size, we will correct it later */
1534 skb->truesize -= skb->data_len;
1535 skb->len += nskb->len;
1536 skb->data_len += nskb->len;
1538 /* create a brand new frags array and coalesce there */
1539 for (i = 0; offset < skb->len; i++) {
1543 BUG_ON(i >= MAX_SKB_FRAGS);
1544 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
1547 skb->truesize += skb->data_len;
1548 for (j = 0; j < i; j++)
1549 put_page(frags[j].page.p);
1553 if (offset + PAGE_SIZE < skb->len)
1556 len = skb->len - offset;
1557 if (skb_copy_bits(skb, offset, page_address(page), len))
1561 frags[i].page.p = page;
1562 frags[i].page_offset = 0;
1563 skb_frag_size_set(&frags[i], len);
1565 /* swap out with old one */
1566 memcpy(skb_shinfo(skb)->frags,
1568 i * sizeof(skb_frag_t));
1569 skb_shinfo(skb)->nr_frags = i;
1570 skb->truesize += i * PAGE_SIZE;
1572 /* remove traces of mapped pages and frag_list */
1573 skb_frag_list_init(skb);
1574 uarg = skb_shinfo(skb)->destructor_arg;
1575 /* increase inflight counter to offset decrement in callback */
1576 atomic_inc(&queue->inflight_packets);
1577 uarg->callback(uarg, true);
1578 skb_shinfo(skb)->destructor_arg = NULL;
1580 xenvif_skb_zerocopy_prepare(queue, nskb);
1586 static int xenvif_tx_submit(struct xenvif_queue *queue)
1588 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1589 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
1590 struct sk_buff *skb;
1593 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
1594 struct xen_netif_tx_request *txp;
1598 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
1599 txp = &queue->pending_tx_info[pending_idx].req;
1601 /* Check the remap error code. */
1602 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
1603 /* If there was an error, xenvif_tx_check_gop is
1604 * expected to release all the frags which were mapped,
1605 * so kfree_skb shouldn't do it again
1607 skb_shinfo(skb)->nr_frags = 0;
1608 if (skb_has_frag_list(skb)) {
1609 struct sk_buff *nskb =
1610 skb_shinfo(skb)->frag_list;
1611 skb_shinfo(nskb)->nr_frags = 0;
1617 data_len = skb->len;
1618 callback_param(queue, pending_idx).ctx = NULL;
1619 if (data_len < txp->size) {
1620 /* Append the packet payload as a fragment. */
1621 txp->offset += data_len;
1622 txp->size -= data_len;
1624 /* Schedule a response immediately. */
1625 xenvif_idx_release(queue, pending_idx,
1626 XEN_NETIF_RSP_OKAY);
1629 if (txp->flags & XEN_NETTXF_csum_blank)
1630 skb->ip_summed = CHECKSUM_PARTIAL;
1631 else if (txp->flags & XEN_NETTXF_data_validated)
1632 skb->ip_summed = CHECKSUM_UNNECESSARY;
1634 xenvif_fill_frags(queue, skb);
1636 if (unlikely(skb_has_frag_list(skb))) {
1637 if (xenvif_handle_frag_list(queue, skb)) {
1638 if (net_ratelimit())
1639 netdev_err(queue->vif->dev,
1640 "Not enough memory to consolidate frag_list!\n");
1641 xenvif_skb_zerocopy_prepare(queue, skb);
1647 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
1648 int target = min_t(int, skb->len, PKT_PROT_LEN);
1649 __pskb_pull_tail(skb, target - skb_headlen(skb));
1652 skb->dev = queue->vif->dev;
1653 skb->protocol = eth_type_trans(skb, skb->dev);
1654 skb_reset_network_header(skb);
1656 if (checksum_setup(queue, skb)) {
1657 netdev_dbg(queue->vif->dev,
1658 "Can't setup checksum in net_tx_action\n");
1659 /* We have to set this flag to trigger the callback */
1660 if (skb_shinfo(skb)->destructor_arg)
1661 xenvif_skb_zerocopy_prepare(queue, skb);
1666 skb_probe_transport_header(skb, 0);
1668 /* If the packet is GSO then we will have just set up the
1669 * transport header offset in checksum_setup so it's now
1670 * straightforward to calculate gso_segs.
1672 if (skb_is_gso(skb)) {
1673 int mss = skb_shinfo(skb)->gso_size;
1674 int hdrlen = skb_transport_header(skb) -
1675 skb_mac_header(skb) +
1678 skb_shinfo(skb)->gso_segs =
1679 DIV_ROUND_UP(skb->len - hdrlen, mss);
1682 queue->stats.rx_bytes += skb->len;
1683 queue->stats.rx_packets++;
1687 /* Set this flag right before netif_receive_skb, otherwise
1688 * someone might think this packet already left netback, and
1689 * do a skb_copy_ubufs while we are still in control of the
1690 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1692 if (skb_shinfo(skb)->destructor_arg) {
1693 xenvif_skb_zerocopy_prepare(queue, skb);
1694 queue->stats.tx_zerocopy_sent++;
1697 netif_receive_skb(skb);
1703 void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1705 unsigned long flags;
1706 pending_ring_idx_t index;
1707 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
1709 /* This is the only place where we grab this lock, to protect callbacks
1712 spin_lock_irqsave(&queue->callback_lock, flags);
1714 u16 pending_idx = ubuf->desc;
1715 ubuf = (struct ubuf_info *) ubuf->ctx;
1716 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
1718 index = pending_index(queue->dealloc_prod);
1719 queue->dealloc_ring[index] = pending_idx;
1720 /* Sync with xenvif_tx_dealloc_action:
1721 * insert idx then incr producer.
1724 queue->dealloc_prod++;
1726 wake_up(&queue->dealloc_wq);
1727 spin_unlock_irqrestore(&queue->callback_lock, flags);
1729 if (likely(zerocopy_success))
1730 queue->stats.tx_zerocopy_success++;
1732 queue->stats.tx_zerocopy_fail++;
1733 xenvif_skb_zerocopy_complete(queue);
1736 static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
1738 struct gnttab_unmap_grant_ref *gop;
1739 pending_ring_idx_t dc, dp;
1740 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1743 dc = queue->dealloc_cons;
1744 gop = queue->tx_unmap_ops;
1746 /* Free up any grants we have finished using */
1748 dp = queue->dealloc_prod;
1750 /* Ensure we see all indices enqueued by all
1751 * xenvif_zerocopy_callback().
1756 BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
1758 queue->dealloc_ring[pending_index(dc++)];
1760 pending_idx_release[gop-queue->tx_unmap_ops] =
1762 queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1763 queue->mmap_pages[pending_idx];
1764 gnttab_set_unmap_op(gop,
1765 idx_to_kaddr(queue, pending_idx),
1767 queue->grant_tx_handle[pending_idx]);
1768 xenvif_grant_handle_reset(queue, pending_idx);
1772 } while (dp != queue->dealloc_prod);
1774 queue->dealloc_cons = dc;
1776 if (gop - queue->tx_unmap_ops > 0) {
1778 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
1780 queue->pages_to_unmap,
1781 gop - queue->tx_unmap_ops);
1783 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1784 gop - queue->tx_unmap_ops, ret);
1785 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
1786 if (gop[i].status != GNTST_okay)
1787 netdev_err(queue->vif->dev,
1788 " host_addr: %llx handle: %x status: %d\n",
1797 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1798 xenvif_idx_release(queue, pending_idx_release[i],
1799 XEN_NETIF_RSP_OKAY);
1803 /* Called after netfront has transmitted */
1804 int xenvif_tx_action(struct xenvif_queue *queue, int budget)
1806 unsigned nr_mops, nr_cops = 0;
1809 if (unlikely(!tx_work_todo(queue)))
1812 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
1817 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
1819 ret = gnttab_map_refs(queue->tx_map_ops,
1821 queue->pages_to_map,
1826 work_done = xenvif_tx_submit(queue);
1831 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
1834 struct pending_tx_info *pending_tx_info;
1835 pending_ring_idx_t index;
1836 unsigned long flags;
1838 pending_tx_info = &queue->pending_tx_info[pending_idx];
1839 spin_lock_irqsave(&queue->response_lock, flags);
1840 make_tx_response(queue, &pending_tx_info->req, status);
1841 index = pending_index(queue->pending_prod);
1842 queue->pending_ring[index] = pending_idx;
1843 /* TX shouldn't use the index before we give it back here */
1845 queue->pending_prod++;
1846 spin_unlock_irqrestore(&queue->response_lock, flags);
1850 static void make_tx_response(struct xenvif_queue *queue,
1851 struct xen_netif_tx_request *txp,
1854 RING_IDX i = queue->tx.rsp_prod_pvt;
1855 struct xen_netif_tx_response *resp;
1858 resp = RING_GET_RESPONSE(&queue->tx, i);
1862 if (txp->flags & XEN_NETTXF_extra_info)
1863 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1865 queue->tx.rsp_prod_pvt = ++i;
1866 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
1868 notify_remote_via_irq(queue->tx_irq);
1871 static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
1878 RING_IDX i = queue->rx.rsp_prod_pvt;
1879 struct xen_netif_rx_response *resp;
1881 resp = RING_GET_RESPONSE(&queue->rx, i);
1882 resp->offset = offset;
1883 resp->flags = flags;
1885 resp->status = (s16)size;
1887 resp->status = (s16)st;
1889 queue->rx.rsp_prod_pvt = ++i;
1894 void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
1897 struct gnttab_unmap_grant_ref tx_unmap_op;
1899 gnttab_set_unmap_op(&tx_unmap_op,
1900 idx_to_kaddr(queue, pending_idx),
1902 queue->grant_tx_handle[pending_idx]);
1903 xenvif_grant_handle_reset(queue, pending_idx);
1905 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1906 &queue->mmap_pages[pending_idx], 1);
1908 netdev_err(queue->vif->dev,
1909 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1912 tx_unmap_op.host_addr,
1914 tx_unmap_op.status);
1919 static inline int tx_work_todo(struct xenvif_queue *queue)
1921 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
1927 static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
1929 return queue->dealloc_cons != queue->dealloc_prod;
1932 void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
1934 if (queue->tx.sring)
1935 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1937 if (queue->rx.sring)
1938 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1942 int xenvif_map_frontend_rings(struct xenvif_queue *queue,
1943 grant_ref_t tx_ring_ref,
1944 grant_ref_t rx_ring_ref)
1947 struct xen_netif_tx_sring *txs;
1948 struct xen_netif_rx_sring *rxs;
1952 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1953 tx_ring_ref, &addr);
1957 txs = (struct xen_netif_tx_sring *)addr;
1958 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
1960 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1961 rx_ring_ref, &addr);
1965 rxs = (struct xen_netif_rx_sring *)addr;
1966 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
1971 xenvif_unmap_frontend_rings(queue);
1975 static bool xenvif_have_rx_work(struct xenvif_queue *queue)
1977 return (!skb_queue_empty(&queue->rx_queue)
1978 && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
1979 || kthread_should_stop()
1980 || queue->vif->disabled;
1983 static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
1985 struct sk_buff *skb;
1988 skb = skb_peek(&queue->rx_queue);
1990 return MAX_SCHEDULE_TIMEOUT;
1992 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
1993 return timeout < 0 ? 0 : timeout;
1996 /* Wait until the guest Rx thread has work.
1998 * The timeout needs to be adjusted based on the current head of the
1999 * queue (and not just the head at the beginning). In particular, if
2000 * the queue is initially empty an infinite timeout is used and this
2001 * needs to be reduced when a skb is queued.
2003 * This cannot be done with wait_event_timeout() because it only
2004 * calculates the timeout once.
2006 static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
2010 if (xenvif_have_rx_work(queue))
2016 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
2017 if (xenvif_have_rx_work(queue))
2019 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
2023 finish_wait(&queue->wq, &wait);
2026 int xenvif_kthread_guest_rx(void *data)
2028 struct xenvif_queue *queue = data;
2029 struct xenvif *vif = queue->vif;
2032 xenvif_wait_for_rx_work(queue);
2034 if (kthread_should_stop())
2037 /* This frontend is found to be rogue, disable it in
2038 * kthread context. Currently this is only set when
2039 * netback finds out frontend sends malformed packet,
2040 * but we cannot disable the interface in softirq
2041 * context so we defer it here, if this thread is
2042 * associated with queue 0.
2044 if (unlikely(vif->disabled && queue->id == 0)) {
2045 xenvif_carrier_off(vif);
2046 xenvif_rx_queue_purge(queue);
2050 if (!skb_queue_empty(&queue->rx_queue))
2051 xenvif_rx_action(queue);
2053 /* Queued packets may have foreign pages from other
2054 * domains. These cannot be queued indefinitely as
2055 * this would starve guests of grant refs and transmit
2058 xenvif_rx_queue_drop_expired(queue);
2060 xenvif_rx_queue_maybe_wake(queue);
2065 /* Bin any remaining skbs */
2066 xenvif_rx_queue_purge(queue);
2071 static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
2073 /* Dealloc thread must remain running until all inflight
2076 return kthread_should_stop() &&
2077 !atomic_read(&queue->inflight_packets);
2080 int xenvif_dealloc_kthread(void *data)
2082 struct xenvif_queue *queue = data;
2085 wait_event_interruptible(queue->dealloc_wq,
2086 tx_dealloc_work_todo(queue) ||
2087 xenvif_dealloc_kthread_should_stop(queue));
2088 if (xenvif_dealloc_kthread_should_stop(queue))
2091 xenvif_tx_dealloc_action(queue);
2095 /* Unmap anything remaining*/
2096 if (tx_dealloc_work_todo(queue))
2097 xenvif_tx_dealloc_action(queue);
2102 static int __init netback_init(void)
2109 /* Allow as many queues as there are CPUs, by default */
2110 xenvif_max_queues = num_online_cpus();
2112 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
2113 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2114 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
2115 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
2118 rc = xenvif_xenbus_init();
2122 rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs);
2124 #ifdef CONFIG_DEBUG_FS
2125 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2126 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2127 pr_warn("Init of debugfs returned %ld!\n",
2128 PTR_ERR(xen_netback_dbg_root));
2129 #endif /* CONFIG_DEBUG_FS */
2137 module_init(netback_init);
2139 static void __exit netback_fini(void)
2141 #ifdef CONFIG_DEBUG_FS
2142 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2143 debugfs_remove_recursive(xen_netback_dbg_root);
2144 #endif /* CONFIG_DEBUG_FS */
2145 xenvif_xenbus_fini();
2147 module_exit(netback_fini);
2149 MODULE_LICENSE("Dual BSD/GPL");
2150 MODULE_ALIAS("xen-backend:vif");