2 * Back-end of the driver for virtual network devices. This portion of the
3 * driver exports a 'unified' network-device interface that can be accessed
4 * by any operating system that implements a compatible front end. A
5 * reference front-end implementation can be found in:
6 * drivers/net/xen-netfront.c
8 * Copyright (c) 2002-2005, K A Fraser
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
37 #include <linux/kthread.h>
38 #include <linux/if_vlan.h>
39 #include <linux/udp.h>
40 #include <linux/highmem.h>
45 #include <xen/events.h>
46 #include <xen/interface/memory.h>
49 #include <asm/xen/hypercall.h>
51 /* Provide an option to disable split event channels at load time as
52 * event channels are limited resource. Split event channels are
55 bool separate_tx_rx_irq = true;
56 module_param(separate_tx_rx_irq, bool, 0644);
58 /* The time that packets can stay on the guest Rx internal queue
59 * before they are dropped.
61 unsigned int rx_drain_timeout_msecs = 10000;
62 module_param(rx_drain_timeout_msecs, uint, 0444);
64 /* The length of time before the frontend is considered unresponsive
65 * because it isn't providing Rx slots.
67 unsigned int rx_stall_timeout_msecs = 60000;
68 module_param(rx_stall_timeout_msecs, uint, 0444);
70 #define MAX_QUEUES_DEFAULT 8
71 unsigned int xenvif_max_queues;
72 module_param_named(max_queues, xenvif_max_queues, uint, 0644);
73 MODULE_PARM_DESC(max_queues,
74 "Maximum number of queues per virtual interface");
77 * This is the maximum slots a skb can have. If a guest sends a skb
78 * which exceeds this limit it is considered malicious.
80 #define FATAL_SKB_SLOTS_DEFAULT 20
81 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
82 module_param(fatal_skb_slots, uint, 0444);
84 /* The amount to copy out of the first guest Tx slot into the skb's
85 * linear area. If the first slot has more data, it will be mapped
86 * and put into the first frag.
88 * This is sized to avoid pulling headers from the frags for most
91 #define XEN_NETBACK_TX_COPY_LEN 128
93 /* This is the maximum number of flows in the hash cache. */
94 #define XENVIF_HASH_CACHE_SIZE_DEFAULT 64
95 unsigned int xenvif_hash_cache_size = XENVIF_HASH_CACHE_SIZE_DEFAULT;
96 module_param_named(hash_cache_size, xenvif_hash_cache_size, uint, 0644);
97 MODULE_PARM_DESC(hash_cache_size, "Number of flows in the hash cache");
99 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
102 static void make_tx_response(struct xenvif_queue *queue,
103 const struct xen_netif_tx_request *txp,
104 unsigned int extra_count,
107 static void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx);
109 static inline int tx_work_todo(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 void xenvif_kick_thread(struct xenvif_queue *queue)
158 void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
162 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
165 napi_schedule(&queue->napi);
166 else if (atomic_fetch_andnot(NETBK_TX_EOI | NETBK_COMMON_EOI,
167 &queue->eoi_pending) &
168 (NETBK_TX_EOI | NETBK_COMMON_EOI))
169 xen_irq_lateeoi(queue->tx_irq, 0);
172 static void tx_add_credit(struct xenvif_queue *queue)
174 unsigned long max_burst, max_credit;
177 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
178 * Otherwise the interface can seize up due to insufficient credit.
180 max_burst = max(131072UL, queue->credit_bytes);
182 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
183 max_credit = queue->remaining_credit + queue->credit_bytes;
184 if (max_credit < queue->remaining_credit)
185 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
187 queue->remaining_credit = min(max_credit, max_burst);
188 queue->rate_limited = false;
191 void xenvif_tx_credit_callback(struct timer_list *t)
193 struct xenvif_queue *queue = from_timer(queue, t, credit_timeout);
194 tx_add_credit(queue);
195 xenvif_napi_schedule_or_enable_events(queue);
198 static void xenvif_tx_err(struct xenvif_queue *queue,
199 struct xen_netif_tx_request *txp,
200 unsigned int extra_count, RING_IDX end)
202 RING_IDX cons = queue->tx.req_cons;
205 make_tx_response(queue, txp, extra_count, XEN_NETIF_RSP_ERROR);
208 RING_COPY_REQUEST(&queue->tx, cons++, txp);
209 extra_count = 0; /* only the first frag can have extras */
211 queue->tx.req_cons = cons;
214 static void xenvif_fatal_tx_err(struct xenvif *vif)
216 netdev_err(vif->dev, "fatal error; disabling device\n");
217 vif->disabled = true;
218 /* Disable the vif from queue 0's kthread */
220 xenvif_kick_thread(&vif->queues[0]);
223 static int xenvif_count_requests(struct xenvif_queue *queue,
224 struct xen_netif_tx_request *first,
225 unsigned int extra_count,
226 struct xen_netif_tx_request *txp,
229 RING_IDX cons = queue->tx.req_cons;
234 if (!(first->flags & XEN_NETTXF_more_data))
238 struct xen_netif_tx_request dropped_tx = { 0 };
240 if (slots >= work_to_do) {
241 netdev_err(queue->vif->dev,
242 "Asked for %d slots but exceeds this limit\n",
244 xenvif_fatal_tx_err(queue->vif);
248 /* This guest is really using too many slots and
249 * considered malicious.
251 if (unlikely(slots >= fatal_skb_slots)) {
252 netdev_err(queue->vif->dev,
253 "Malicious frontend using %d slots, threshold %u\n",
254 slots, fatal_skb_slots);
255 xenvif_fatal_tx_err(queue->vif);
259 /* Xen network protocol had implicit dependency on
260 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
261 * the historical MAX_SKB_FRAGS value 18 to honor the
262 * same behavior as before. Any packet using more than
263 * 18 slots but less than fatal_skb_slots slots is
266 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
268 netdev_dbg(queue->vif->dev,
269 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
270 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
277 RING_COPY_REQUEST(&queue->tx, cons + slots, txp);
279 /* If the guest submitted a frame >= 64 KiB then
280 * first->size overflowed and following slots will
281 * appear to be larger than the frame.
283 * This cannot be fatal error as there are buggy
284 * frontends that do this.
286 * Consume all slots and drop the packet.
288 if (!drop_err && txp->size > first->size) {
290 netdev_dbg(queue->vif->dev,
291 "Invalid tx request, slot size %u > remaining size %u\n",
292 txp->size, first->size);
296 first->size -= txp->size;
299 if (unlikely((txp->offset + txp->size) > XEN_PAGE_SIZE)) {
300 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %u, size: %u\n",
301 txp->offset, txp->size);
302 xenvif_fatal_tx_err(queue->vif);
306 more_data = txp->flags & XEN_NETTXF_more_data;
314 xenvif_tx_err(queue, first, extra_count, cons + slots);
322 struct xenvif_tx_cb {
323 u16 copy_pending_idx[XEN_NETBK_LEGACY_SLOTS_MAX + 1];
328 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
329 #define copy_pending_idx(skb, i) (XENVIF_TX_CB(skb)->copy_pending_idx[i])
330 #define copy_count(skb) (XENVIF_TX_CB(skb)->copy_count)
332 static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
334 struct xen_netif_tx_request *txp,
335 unsigned int extra_count,
336 struct gnttab_map_grant_ref *mop)
338 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
339 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
340 GNTMAP_host_map | GNTMAP_readonly,
341 txp->gref, queue->vif->domid);
343 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
345 queue->pending_tx_info[pending_idx].extra_count = extra_count;
348 static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
350 struct sk_buff *skb =
351 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
352 GFP_ATOMIC | __GFP_NOWARN);
354 BUILD_BUG_ON(sizeof(*XENVIF_TX_CB(skb)) > sizeof(skb->cb));
355 if (unlikely(skb == NULL))
358 /* Packets passed to netif_rx() must have some headroom. */
359 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
361 /* Initialize it here to avoid later surprises */
362 skb_shinfo(skb)->destructor_arg = NULL;
367 static void xenvif_get_requests(struct xenvif_queue *queue,
369 struct xen_netif_tx_request *first,
370 struct xen_netif_tx_request *txfrags,
373 unsigned int frag_overflow,
374 struct sk_buff *nskb,
375 unsigned int extra_count,
376 unsigned int data_len)
378 struct skb_shared_info *shinfo = skb_shinfo(skb);
379 skb_frag_t *frags = shinfo->frags;
381 pending_ring_idx_t index;
382 unsigned int nr_slots;
383 struct gnttab_copy *cop = queue->tx_copy_ops + *copy_ops;
384 struct gnttab_map_grant_ref *gop = queue->tx_map_ops + *map_ops;
385 struct xen_netif_tx_request *txp = first;
387 nr_slots = shinfo->nr_frags + frag_overflow + 1;
390 XENVIF_TX_CB(skb)->split_mask = 0;
392 /* Create copy ops for exactly data_len bytes into the skb head. */
393 __skb_put(skb, data_len);
394 while (data_len > 0) {
395 int amount = data_len > txp->size ? txp->size : data_len;
398 cop->source.u.ref = txp->gref;
399 cop->source.domid = queue->vif->domid;
400 cop->source.offset = txp->offset;
402 cop->dest.domid = DOMID_SELF;
403 cop->dest.offset = (offset_in_page(skb->data +
405 data_len)) & ~XEN_PAGE_MASK;
406 cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
409 /* Don't cross local page boundary! */
410 if (cop->dest.offset + amount > XEN_PAGE_SIZE) {
411 amount = XEN_PAGE_SIZE - cop->dest.offset;
412 XENVIF_TX_CB(skb)->split_mask |= 1U << copy_count(skb);
417 cop->flags = GNTCOPY_source_gref;
419 index = pending_index(queue->pending_cons);
420 pending_idx = queue->pending_ring[index];
421 callback_param(queue, pending_idx).ctx = NULL;
422 copy_pending_idx(skb, copy_count(skb)) = pending_idx;
429 if (amount == txp->size) {
430 /* The copy op covered the full tx_request */
432 memcpy(&queue->pending_tx_info[pending_idx].req,
434 queue->pending_tx_info[pending_idx].extra_count =
435 (txp == first) ? extra_count : 0;
441 queue->pending_cons++;
444 /* The copy op partially covered the tx_request.
445 * The remainder will be mapped or copied in the next
448 txp->offset += amount;
453 for (shinfo->nr_frags = 0; nr_slots > 0 && shinfo->nr_frags < MAX_SKB_FRAGS;
455 if (unlikely(!txp->size)) {
456 make_tx_response(queue, txp, 0, XEN_NETIF_RSP_OKAY);
461 index = pending_index(queue->pending_cons++);
462 pending_idx = queue->pending_ring[index];
463 xenvif_tx_create_map_op(queue, pending_idx, txp,
464 txp == first ? extra_count : 0, gop);
465 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
477 shinfo = skb_shinfo(nskb);
478 frags = shinfo->frags;
480 for (shinfo->nr_frags = 0; shinfo->nr_frags < nr_slots; ++txp) {
481 if (unlikely(!txp->size)) {
482 make_tx_response(queue, txp, 0,
487 index = pending_index(queue->pending_cons++);
488 pending_idx = queue->pending_ring[index];
489 xenvif_tx_create_map_op(queue, pending_idx, txp, 0,
491 frag_set_pending_idx(&frags[shinfo->nr_frags],
497 if (shinfo->nr_frags) {
498 skb_shinfo(skb)->frag_list = nskb;
504 /* A frag_list skb was allocated but it is no longer needed
505 * because enough slots were converted to copy ops above or some
511 (*copy_ops) = cop - queue->tx_copy_ops;
512 (*map_ops) = gop - queue->tx_map_ops;
515 static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
517 grant_handle_t handle)
519 if (unlikely(queue->grant_tx_handle[pending_idx] !=
520 NETBACK_INVALID_HANDLE)) {
521 netdev_err(queue->vif->dev,
522 "Trying to overwrite active handle! pending_idx: 0x%x\n",
526 queue->grant_tx_handle[pending_idx] = handle;
529 static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
532 if (unlikely(queue->grant_tx_handle[pending_idx] ==
533 NETBACK_INVALID_HANDLE)) {
534 netdev_err(queue->vif->dev,
535 "Trying to unmap invalid handle! pending_idx: 0x%x\n",
539 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
542 static int xenvif_tx_check_gop(struct xenvif_queue *queue,
544 struct gnttab_map_grant_ref **gopp_map,
545 struct gnttab_copy **gopp_copy)
547 struct gnttab_map_grant_ref *gop_map = *gopp_map;
549 /* This always points to the shinfo of the skb being checked, which
550 * could be either the first or the one on the frag_list
552 struct skb_shared_info *shinfo = skb_shinfo(skb);
553 /* If this is non-NULL, we are currently checking the frag_list skb, and
554 * this points to the shinfo of the first one
556 struct skb_shared_info *first_shinfo = NULL;
557 int nr_frags = shinfo->nr_frags;
558 const bool sharedslot = nr_frags &&
559 frag_get_pending_idx(&shinfo->frags[0]) ==
560 copy_pending_idx(skb, copy_count(skb) - 1);
563 for (i = 0; i < copy_count(skb); i++) {
566 /* Check status of header. */
567 pending_idx = copy_pending_idx(skb, i);
569 newerr = (*gopp_copy)->status;
571 /* Split copies need to be handled together. */
572 if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
575 newerr = (*gopp_copy)->status;
577 if (likely(!newerr)) {
578 /* The first frag might still have this slot mapped */
579 if (i < copy_count(skb) - 1 || !sharedslot)
580 xenvif_idx_release(queue, pending_idx,
585 netdev_dbg(queue->vif->dev,
586 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
587 (*gopp_copy)->status,
589 (*gopp_copy)->source.u.ref);
590 /* The first frag might still have this slot mapped */
591 if (i < copy_count(skb) - 1 || !sharedslot)
592 xenvif_idx_release(queue, pending_idx,
593 XEN_NETIF_RSP_ERROR);
599 for (i = 0; i < nr_frags; i++, gop_map++) {
602 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
604 /* Check error status: if okay then remember grant handle. */
605 newerr = gop_map->status;
607 if (likely(!newerr)) {
608 xenvif_grant_handle_set(queue,
611 /* Had a previous error? Invalidate this fragment. */
613 xenvif_idx_unmap(queue, pending_idx);
614 /* If the mapping of the first frag was OK, but
615 * the header's copy failed, and they are
616 * sharing a slot, send an error
618 if (i == 0 && !first_shinfo && sharedslot)
619 xenvif_idx_release(queue, pending_idx,
620 XEN_NETIF_RSP_ERROR);
622 xenvif_idx_release(queue, pending_idx,
628 /* Error on this fragment: respond to client with an error. */
630 netdev_dbg(queue->vif->dev,
631 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
637 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
639 /* Not the first error? Preceding frags already invalidated. */
643 /* Invalidate preceding fragments of this skb. */
644 for (j = 0; j < i; j++) {
645 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
646 xenvif_idx_unmap(queue, pending_idx);
647 xenvif_idx_release(queue, pending_idx,
651 /* And if we found the error while checking the frag_list, unmap
652 * the first skb's frags
655 for (j = 0; j < first_shinfo->nr_frags; j++) {
656 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
657 xenvif_idx_unmap(queue, pending_idx);
658 xenvif_idx_release(queue, pending_idx,
663 /* Remember the error: invalidate all subsequent fragments. */
667 if (skb_has_frag_list(skb) && !first_shinfo) {
668 first_shinfo = skb_shinfo(skb);
669 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
670 nr_frags = shinfo->nr_frags;
679 static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
681 struct skb_shared_info *shinfo = skb_shinfo(skb);
682 int nr_frags = shinfo->nr_frags;
684 u16 prev_pending_idx = INVALID_PENDING_IDX;
686 for (i = 0; i < nr_frags; i++) {
687 skb_frag_t *frag = shinfo->frags + i;
688 struct xen_netif_tx_request *txp;
692 pending_idx = frag_get_pending_idx(frag);
694 /* If this is not the first frag, chain it to the previous*/
695 if (prev_pending_idx == INVALID_PENDING_IDX)
696 skb_shinfo(skb)->destructor_arg =
697 &callback_param(queue, pending_idx);
699 callback_param(queue, prev_pending_idx).ctx =
700 &callback_param(queue, pending_idx);
702 callback_param(queue, pending_idx).ctx = NULL;
703 prev_pending_idx = pending_idx;
705 txp = &queue->pending_tx_info[pending_idx].req;
706 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
707 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
708 skb->len += txp->size;
709 skb->data_len += txp->size;
710 skb->truesize += txp->size;
712 /* Take an extra reference to offset network stack's put_page */
713 get_page(queue->mmap_pages[pending_idx]);
717 static int xenvif_get_extras(struct xenvif_queue *queue,
718 struct xen_netif_extra_info *extras,
719 unsigned int *extra_count,
722 struct xen_netif_extra_info extra;
723 RING_IDX cons = queue->tx.req_cons;
726 if (unlikely(work_to_do-- <= 0)) {
727 netdev_err(queue->vif->dev, "Missing extra info\n");
728 xenvif_fatal_tx_err(queue->vif);
732 RING_COPY_REQUEST(&queue->tx, cons, &extra);
734 queue->tx.req_cons = ++cons;
737 if (unlikely(!extra.type ||
738 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
739 netdev_err(queue->vif->dev,
740 "Invalid extra type: %d\n", extra.type);
741 xenvif_fatal_tx_err(queue->vif);
745 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
746 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
751 static int xenvif_set_skb_gso(struct xenvif *vif,
753 struct xen_netif_extra_info *gso)
755 if (!gso->u.gso.size) {
756 netdev_err(vif->dev, "GSO size must not be zero.\n");
757 xenvif_fatal_tx_err(vif);
761 switch (gso->u.gso.type) {
762 case XEN_NETIF_GSO_TYPE_TCPV4:
763 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
765 case XEN_NETIF_GSO_TYPE_TCPV6:
766 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
769 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
770 xenvif_fatal_tx_err(vif);
774 skb_shinfo(skb)->gso_size = gso->u.gso.size;
775 /* gso_segs will be calculated later */
780 static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
782 bool recalculate_partial_csum = false;
784 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
785 * peers can fail to set NETRXF_csum_blank when sending a GSO
786 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
787 * recalculate the partial checksum.
789 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
790 queue->stats.rx_gso_checksum_fixup++;
791 skb->ip_summed = CHECKSUM_PARTIAL;
792 recalculate_partial_csum = true;
795 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
796 if (skb->ip_summed != CHECKSUM_PARTIAL)
799 return skb_checksum_setup(skb, recalculate_partial_csum);
802 static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
804 u64 now = get_jiffies_64();
805 u64 next_credit = queue->credit_window_start +
806 msecs_to_jiffies(queue->credit_usec / 1000);
808 /* Timer could already be pending in rare cases. */
809 if (timer_pending(&queue->credit_timeout)) {
810 queue->rate_limited = true;
814 /* Passed the point where we can replenish credit? */
815 if (time_after_eq64(now, next_credit)) {
816 queue->credit_window_start = now;
817 tx_add_credit(queue);
820 /* Still too big to send right now? Set a callback. */
821 if (size > queue->remaining_credit) {
822 mod_timer(&queue->credit_timeout,
824 queue->credit_window_start = next_credit;
825 queue->rate_limited = true;
833 /* No locking is required in xenvif_mcast_add/del() as they are
834 * only ever invoked from NAPI poll. An RCU list is used because
835 * xenvif_mcast_match() is called asynchronously, during start_xmit.
838 static int xenvif_mcast_add(struct xenvif *vif, const u8 *addr)
840 struct xenvif_mcast_addr *mcast;
842 if (vif->fe_mcast_count == XEN_NETBK_MCAST_MAX) {
845 "Too many multicast addresses\n");
849 mcast = kzalloc(sizeof(*mcast), GFP_ATOMIC);
853 ether_addr_copy(mcast->addr, addr);
854 list_add_tail_rcu(&mcast->entry, &vif->fe_mcast_addr);
855 vif->fe_mcast_count++;
860 static void xenvif_mcast_del(struct xenvif *vif, const u8 *addr)
862 struct xenvif_mcast_addr *mcast;
864 list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
865 if (ether_addr_equal(addr, mcast->addr)) {
866 --vif->fe_mcast_count;
867 list_del_rcu(&mcast->entry);
868 kfree_rcu(mcast, rcu);
874 bool xenvif_mcast_match(struct xenvif *vif, const u8 *addr)
876 struct xenvif_mcast_addr *mcast;
879 list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
880 if (ether_addr_equal(addr, mcast->addr)) {
890 void xenvif_mcast_addr_list_free(struct xenvif *vif)
892 /* No need for locking or RCU here. NAPI poll and TX queue
895 while (!list_empty(&vif->fe_mcast_addr)) {
896 struct xenvif_mcast_addr *mcast;
898 mcast = list_first_entry(&vif->fe_mcast_addr,
899 struct xenvif_mcast_addr,
901 --vif->fe_mcast_count;
902 list_del(&mcast->entry);
907 static void xenvif_tx_build_gops(struct xenvif_queue *queue,
912 struct sk_buff *skb, *nskb;
914 unsigned int frag_overflow;
916 while (skb_queue_len(&queue->tx_queue) < budget) {
917 struct xen_netif_tx_request txreq;
918 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
919 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
920 unsigned int extra_count;
924 unsigned int data_len;
925 pending_ring_idx_t index;
927 if (queue->tx.sring->req_prod - queue->tx.req_cons >
928 XEN_NETIF_TX_RING_SIZE) {
929 netdev_err(queue->vif->dev,
930 "Impossible number of requests. "
931 "req_prod %d, req_cons %d, size %ld\n",
932 queue->tx.sring->req_prod, queue->tx.req_cons,
933 XEN_NETIF_TX_RING_SIZE);
934 xenvif_fatal_tx_err(queue->vif);
938 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
942 idx = queue->tx.req_cons;
943 rmb(); /* Ensure that we see the request before we copy it. */
944 RING_COPY_REQUEST(&queue->tx, idx, &txreq);
946 /* Credit-based scheduling. */
947 if (txreq.size > queue->remaining_credit &&
948 tx_credit_exceeded(queue, txreq.size))
951 queue->remaining_credit -= txreq.size;
954 queue->tx.req_cons = ++idx;
956 memset(extras, 0, sizeof(extras));
958 if (txreq.flags & XEN_NETTXF_extra_info) {
959 work_to_do = xenvif_get_extras(queue, extras,
962 idx = queue->tx.req_cons;
963 if (unlikely(work_to_do < 0))
967 if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1].type) {
968 struct xen_netif_extra_info *extra;
970 extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1];
971 ret = xenvif_mcast_add(queue->vif, extra->u.mcast.addr);
973 make_tx_response(queue, &txreq, extra_count,
976 XEN_NETIF_RSP_ERROR);
980 if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1].type) {
981 struct xen_netif_extra_info *extra;
983 extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1];
984 xenvif_mcast_del(queue->vif, extra->u.mcast.addr);
986 make_tx_response(queue, &txreq, extra_count,
991 data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN) ?
992 XEN_NETBACK_TX_COPY_LEN : txreq.size;
994 ret = xenvif_count_requests(queue, &txreq, extra_count,
995 txfrags, work_to_do);
997 if (unlikely(ret < 0))
1002 if (unlikely(txreq.size < ETH_HLEN)) {
1003 netdev_dbg(queue->vif->dev,
1004 "Bad packet size: %d\n", txreq.size);
1005 xenvif_tx_err(queue, &txreq, extra_count, idx);
1009 /* No crossing a page as the payload mustn't fragment. */
1010 if (unlikely((txreq.offset + txreq.size) > XEN_PAGE_SIZE)) {
1011 netdev_err(queue->vif->dev, "Cross page boundary, txreq.offset: %u, size: %u\n",
1012 txreq.offset, txreq.size);
1013 xenvif_fatal_tx_err(queue->vif);
1017 index = pending_index(queue->pending_cons);
1018 pending_idx = queue->pending_ring[index];
1020 if (ret >= XEN_NETBK_LEGACY_SLOTS_MAX - 1 && data_len < txreq.size)
1021 data_len = txreq.size;
1023 skb = xenvif_alloc_skb(data_len);
1024 if (unlikely(skb == NULL)) {
1025 netdev_dbg(queue->vif->dev,
1026 "Can't allocate a skb in start_xmit.\n");
1027 xenvif_tx_err(queue, &txreq, extra_count, idx);
1031 skb_shinfo(skb)->nr_frags = ret;
1032 /* At this point shinfo->nr_frags is in fact the number of
1033 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
1037 if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
1038 frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
1039 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1040 skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
1041 nskb = xenvif_alloc_skb(0);
1042 if (unlikely(nskb == NULL)) {
1043 skb_shinfo(skb)->nr_frags = 0;
1045 xenvif_tx_err(queue, &txreq, extra_count, idx);
1046 if (net_ratelimit())
1047 netdev_err(queue->vif->dev,
1048 "Can't allocate the frag_list skb.\n");
1053 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1054 struct xen_netif_extra_info *gso;
1055 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1057 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
1058 /* Failure in xenvif_set_skb_gso is fatal. */
1059 skb_shinfo(skb)->nr_frags = 0;
1066 if (extras[XEN_NETIF_EXTRA_TYPE_HASH - 1].type) {
1067 struct xen_netif_extra_info *extra;
1068 enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
1070 extra = &extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
1072 switch (extra->u.hash.type) {
1073 case _XEN_NETIF_CTRL_HASH_TYPE_IPV4:
1074 case _XEN_NETIF_CTRL_HASH_TYPE_IPV6:
1075 type = PKT_HASH_TYPE_L3;
1078 case _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP:
1079 case _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP:
1080 type = PKT_HASH_TYPE_L4;
1087 if (type != PKT_HASH_TYPE_NONE)
1089 *(u32 *)extra->u.hash.value,
1093 xenvif_get_requests(queue, skb, &txreq, txfrags, copy_ops,
1094 map_ops, frag_overflow, nskb, extra_count,
1097 __skb_queue_tail(&queue->tx_queue, skb);
1099 queue->tx.req_cons = idx;
1101 if ((*map_ops >= ARRAY_SIZE(queue->tx_map_ops)) ||
1102 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
1109 /* Consolidate skb with a frag_list into a brand new one with local pages on
1110 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1112 static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
1114 unsigned int offset = skb_headlen(skb);
1115 skb_frag_t frags[MAX_SKB_FRAGS];
1117 struct ubuf_info *uarg;
1118 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1120 queue->stats.tx_zerocopy_sent += 2;
1121 queue->stats.tx_frag_overflow++;
1123 xenvif_fill_frags(queue, nskb);
1124 /* Subtract frags size, we will correct it later */
1125 skb->truesize -= skb->data_len;
1126 skb->len += nskb->len;
1127 skb->data_len += nskb->len;
1129 /* create a brand new frags array and coalesce there */
1130 for (i = 0; offset < skb->len; i++) {
1134 BUG_ON(i >= MAX_SKB_FRAGS);
1135 page = alloc_page(GFP_ATOMIC);
1138 skb->truesize += skb->data_len;
1139 for (j = 0; j < i; j++)
1140 put_page(frags[j].page.p);
1144 if (offset + PAGE_SIZE < skb->len)
1147 len = skb->len - offset;
1148 if (skb_copy_bits(skb, offset, page_address(page), len))
1152 frags[i].page.p = page;
1153 frags[i].page_offset = 0;
1154 skb_frag_size_set(&frags[i], len);
1157 /* Release all the original (foreign) frags. */
1158 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1159 skb_frag_unref(skb, f);
1160 uarg = skb_shinfo(skb)->destructor_arg;
1161 /* increase inflight counter to offset decrement in callback */
1162 atomic_inc(&queue->inflight_packets);
1163 uarg->callback(uarg, true);
1164 skb_shinfo(skb)->destructor_arg = NULL;
1166 /* Fill the skb with the new (local) frags. */
1167 memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
1168 skb_shinfo(skb)->nr_frags = i;
1169 skb->truesize += i * PAGE_SIZE;
1174 static int xenvif_tx_submit(struct xenvif_queue *queue)
1176 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1177 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
1178 struct sk_buff *skb;
1181 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
1182 struct xen_netif_tx_request *txp;
1185 pending_idx = copy_pending_idx(skb, 0);
1186 txp = &queue->pending_tx_info[pending_idx].req;
1188 /* Check the remap error code. */
1189 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
1190 /* If there was an error, xenvif_tx_check_gop is
1191 * expected to release all the frags which were mapped,
1192 * so kfree_skb shouldn't do it again
1194 skb_shinfo(skb)->nr_frags = 0;
1195 if (skb_has_frag_list(skb)) {
1196 struct sk_buff *nskb =
1197 skb_shinfo(skb)->frag_list;
1198 skb_shinfo(nskb)->nr_frags = 0;
1204 if (txp->flags & XEN_NETTXF_csum_blank)
1205 skb->ip_summed = CHECKSUM_PARTIAL;
1206 else if (txp->flags & XEN_NETTXF_data_validated)
1207 skb->ip_summed = CHECKSUM_UNNECESSARY;
1209 xenvif_fill_frags(queue, skb);
1211 if (unlikely(skb_has_frag_list(skb))) {
1212 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1213 xenvif_skb_zerocopy_prepare(queue, nskb);
1214 if (xenvif_handle_frag_list(queue, skb)) {
1215 if (net_ratelimit())
1216 netdev_err(queue->vif->dev,
1217 "Not enough memory to consolidate frag_list!\n");
1218 xenvif_skb_zerocopy_prepare(queue, skb);
1222 /* Copied all the bits from the frag list -- free it. */
1223 skb_frag_list_init(skb);
1227 skb->dev = queue->vif->dev;
1228 skb->protocol = eth_type_trans(skb, skb->dev);
1229 skb_reset_network_header(skb);
1231 if (checksum_setup(queue, skb)) {
1232 netdev_dbg(queue->vif->dev,
1233 "Can't setup checksum in net_tx_action\n");
1234 /* We have to set this flag to trigger the callback */
1235 if (skb_shinfo(skb)->destructor_arg)
1236 xenvif_skb_zerocopy_prepare(queue, skb);
1241 skb_probe_transport_header(skb, 0);
1243 /* If the packet is GSO then we will have just set up the
1244 * transport header offset in checksum_setup so it's now
1245 * straightforward to calculate gso_segs.
1247 if (skb_is_gso(skb)) {
1248 int mss = skb_shinfo(skb)->gso_size;
1249 int hdrlen = skb_transport_header(skb) -
1250 skb_mac_header(skb) +
1253 skb_shinfo(skb)->gso_segs =
1254 DIV_ROUND_UP(skb->len - hdrlen, mss);
1257 queue->stats.rx_bytes += skb->len;
1258 queue->stats.rx_packets++;
1262 /* Set this flag right before netif_receive_skb, otherwise
1263 * someone might think this packet already left netback, and
1264 * do a skb_copy_ubufs while we are still in control of the
1265 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1267 if (skb_shinfo(skb)->destructor_arg) {
1268 xenvif_skb_zerocopy_prepare(queue, skb);
1269 queue->stats.tx_zerocopy_sent++;
1272 netif_receive_skb(skb);
1278 void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1280 unsigned long flags;
1281 pending_ring_idx_t index;
1282 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
1284 /* This is the only place where we grab this lock, to protect callbacks
1287 spin_lock_irqsave(&queue->callback_lock, flags);
1289 u16 pending_idx = ubuf->desc;
1290 ubuf = (struct ubuf_info *) ubuf->ctx;
1291 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
1293 index = pending_index(queue->dealloc_prod);
1294 queue->dealloc_ring[index] = pending_idx;
1295 /* Sync with xenvif_tx_dealloc_action:
1296 * insert idx then incr producer.
1299 queue->dealloc_prod++;
1301 spin_unlock_irqrestore(&queue->callback_lock, flags);
1303 if (likely(zerocopy_success))
1304 queue->stats.tx_zerocopy_success++;
1306 queue->stats.tx_zerocopy_fail++;
1307 xenvif_skb_zerocopy_complete(queue);
1310 static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
1312 struct gnttab_unmap_grant_ref *gop;
1313 pending_ring_idx_t dc, dp;
1314 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1317 dc = queue->dealloc_cons;
1318 gop = queue->tx_unmap_ops;
1320 /* Free up any grants we have finished using */
1322 dp = queue->dealloc_prod;
1324 /* Ensure we see all indices enqueued by all
1325 * xenvif_zerocopy_callback().
1330 BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
1332 queue->dealloc_ring[pending_index(dc++)];
1334 pending_idx_release[gop - queue->tx_unmap_ops] =
1336 queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
1337 queue->mmap_pages[pending_idx];
1338 gnttab_set_unmap_op(gop,
1339 idx_to_kaddr(queue, pending_idx),
1341 queue->grant_tx_handle[pending_idx]);
1342 xenvif_grant_handle_reset(queue, pending_idx);
1346 } while (dp != queue->dealloc_prod);
1348 queue->dealloc_cons = dc;
1350 if (gop - queue->tx_unmap_ops > 0) {
1352 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
1354 queue->pages_to_unmap,
1355 gop - queue->tx_unmap_ops);
1357 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
1358 gop - queue->tx_unmap_ops, ret);
1359 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
1360 if (gop[i].status != GNTST_okay)
1361 netdev_err(queue->vif->dev,
1362 " host_addr: 0x%llx handle: 0x%x status: %d\n",
1371 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1372 xenvif_idx_release(queue, pending_idx_release[i],
1373 XEN_NETIF_RSP_OKAY);
1377 /* Called after netfront has transmitted */
1378 int xenvif_tx_action(struct xenvif_queue *queue, int budget)
1380 unsigned nr_mops = 0, nr_cops = 0;
1383 if (unlikely(!tx_work_todo(queue)))
1386 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
1391 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
1393 ret = gnttab_map_refs(queue->tx_map_ops,
1395 queue->pages_to_map,
1400 netdev_err(queue->vif->dev, "Map fail: nr %u ret %d\n",
1402 for (i = 0; i < nr_mops; ++i)
1403 WARN_ON_ONCE(queue->tx_map_ops[i].status ==
1408 work_done = xenvif_tx_submit(queue);
1413 static void _make_tx_response(struct xenvif_queue *queue,
1414 const struct xen_netif_tx_request *txp,
1415 unsigned int extra_count,
1418 RING_IDX i = queue->tx.rsp_prod_pvt;
1419 struct xen_netif_tx_response *resp;
1421 resp = RING_GET_RESPONSE(&queue->tx, i);
1423 resp->status = status;
1425 while (extra_count-- != 0)
1426 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1428 queue->tx.rsp_prod_pvt = ++i;
1431 static void push_tx_responses(struct xenvif_queue *queue)
1435 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
1437 notify_remote_via_irq(queue->tx_irq);
1440 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
1443 struct pending_tx_info *pending_tx_info;
1444 pending_ring_idx_t index;
1445 unsigned long flags;
1447 pending_tx_info = &queue->pending_tx_info[pending_idx];
1449 spin_lock_irqsave(&queue->response_lock, flags);
1451 _make_tx_response(queue, &pending_tx_info->req,
1452 pending_tx_info->extra_count, status);
1454 /* Release the pending index before pusing the Tx response so
1455 * its available before a new Tx request is pushed by the
1458 index = pending_index(queue->pending_prod++);
1459 queue->pending_ring[index] = pending_idx;
1461 push_tx_responses(queue);
1463 spin_unlock_irqrestore(&queue->response_lock, flags);
1466 static void make_tx_response(struct xenvif_queue *queue,
1467 const struct xen_netif_tx_request *txp,
1468 unsigned int extra_count,
1471 unsigned long flags;
1473 spin_lock_irqsave(&queue->response_lock, flags);
1475 _make_tx_response(queue, txp, extra_count, status);
1476 push_tx_responses(queue);
1478 spin_unlock_irqrestore(&queue->response_lock, flags);
1481 static void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
1484 struct gnttab_unmap_grant_ref tx_unmap_op;
1486 gnttab_set_unmap_op(&tx_unmap_op,
1487 idx_to_kaddr(queue, pending_idx),
1489 queue->grant_tx_handle[pending_idx]);
1490 xenvif_grant_handle_reset(queue, pending_idx);
1492 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1493 &queue->mmap_pages[pending_idx], 1);
1495 netdev_err(queue->vif->dev,
1496 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
1499 tx_unmap_op.host_addr,
1501 tx_unmap_op.status);
1506 static inline int tx_work_todo(struct xenvif_queue *queue)
1508 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
1514 static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
1516 return queue->dealloc_cons != queue->dealloc_prod;
1519 void xenvif_unmap_frontend_data_rings(struct xenvif_queue *queue)
1521 if (queue->tx.sring)
1522 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1524 if (queue->rx.sring)
1525 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1529 int xenvif_map_frontend_data_rings(struct xenvif_queue *queue,
1530 grant_ref_t tx_ring_ref,
1531 grant_ref_t rx_ring_ref)
1534 struct xen_netif_tx_sring *txs;
1535 struct xen_netif_rx_sring *rxs;
1539 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1540 &tx_ring_ref, 1, &addr);
1544 txs = (struct xen_netif_tx_sring *)addr;
1545 BACK_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1547 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1548 &rx_ring_ref, 1, &addr);
1552 rxs = (struct xen_netif_rx_sring *)addr;
1553 BACK_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1558 xenvif_unmap_frontend_data_rings(queue);
1562 static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
1564 /* Dealloc thread must remain running until all inflight
1567 return kthread_should_stop() &&
1568 !atomic_read(&queue->inflight_packets);
1571 int xenvif_dealloc_kthread(void *data)
1573 struct xenvif_queue *queue = data;
1576 wait_event_interruptible(queue->dealloc_wq,
1577 tx_dealloc_work_todo(queue) ||
1578 xenvif_dealloc_kthread_should_stop(queue));
1579 if (xenvif_dealloc_kthread_should_stop(queue))
1582 xenvif_tx_dealloc_action(queue);
1586 /* Unmap anything remaining*/
1587 if (tx_dealloc_work_todo(queue))
1588 xenvif_tx_dealloc_action(queue);
1593 static void make_ctrl_response(struct xenvif *vif,
1594 const struct xen_netif_ctrl_request *req,
1595 u32 status, u32 data)
1597 RING_IDX idx = vif->ctrl.rsp_prod_pvt;
1598 struct xen_netif_ctrl_response rsp = {
1605 *RING_GET_RESPONSE(&vif->ctrl, idx) = rsp;
1606 vif->ctrl.rsp_prod_pvt = ++idx;
1609 static void push_ctrl_response(struct xenvif *vif)
1613 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->ctrl, notify);
1615 notify_remote_via_irq(vif->ctrl_irq);
1618 static void process_ctrl_request(struct xenvif *vif,
1619 const struct xen_netif_ctrl_request *req)
1621 u32 status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
1624 switch (req->type) {
1625 case XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM:
1626 status = xenvif_set_hash_alg(vif, req->data[0]);
1629 case XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS:
1630 status = xenvif_get_hash_flags(vif, &data);
1633 case XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS:
1634 status = xenvif_set_hash_flags(vif, req->data[0]);
1637 case XEN_NETIF_CTRL_TYPE_SET_HASH_KEY:
1638 status = xenvif_set_hash_key(vif, req->data[0],
1642 case XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE:
1643 status = XEN_NETIF_CTRL_STATUS_SUCCESS;
1644 data = XEN_NETBK_MAX_HASH_MAPPING_SIZE;
1647 case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE:
1648 status = xenvif_set_hash_mapping_size(vif,
1652 case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING:
1653 status = xenvif_set_hash_mapping(vif, req->data[0],
1662 make_ctrl_response(vif, req, status, data);
1663 push_ctrl_response(vif);
1666 static void xenvif_ctrl_action(struct xenvif *vif)
1669 RING_IDX req_prod, req_cons;
1671 req_prod = vif->ctrl.sring->req_prod;
1672 req_cons = vif->ctrl.req_cons;
1674 /* Make sure we can see requests before we process them. */
1677 if (req_cons == req_prod)
1680 while (req_cons != req_prod) {
1681 struct xen_netif_ctrl_request req;
1683 RING_COPY_REQUEST(&vif->ctrl, req_cons, &req);
1686 process_ctrl_request(vif, &req);
1689 vif->ctrl.req_cons = req_cons;
1690 vif->ctrl.sring->req_event = req_cons + 1;
1694 static bool xenvif_ctrl_work_todo(struct xenvif *vif)
1696 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->ctrl)))
1702 irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data)
1704 struct xenvif *vif = data;
1705 unsigned int eoi_flag = XEN_EOI_FLAG_SPURIOUS;
1707 while (xenvif_ctrl_work_todo(vif)) {
1708 xenvif_ctrl_action(vif);
1712 xen_irq_lateeoi(irq, eoi_flag);
1717 static int __init netback_init(void)
1724 /* Allow as many queues as there are CPUs but max. 8 if user has not
1725 * specified a value.
1727 if (xenvif_max_queues == 0)
1728 xenvif_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
1731 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1732 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1733 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1734 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1737 rc = xenvif_xenbus_init();
1741 #ifdef CONFIG_DEBUG_FS
1742 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
1743 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
1744 pr_warn("Init of debugfs returned %ld!\n",
1745 PTR_ERR(xen_netback_dbg_root));
1746 #endif /* CONFIG_DEBUG_FS */
1754 module_init(netback_init);
1756 static void __exit netback_fini(void)
1758 #ifdef CONFIG_DEBUG_FS
1759 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
1760 debugfs_remove_recursive(xen_netback_dbg_root);
1761 #endif /* CONFIG_DEBUG_FS */
1762 xenvif_xenbus_fini();
1764 module_exit(netback_fini);
1766 MODULE_LICENSE("Dual BSD/GPL");
1767 MODULE_ALIAS("xen-backend:vif");