x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / net / xen-netback / netback.c
bloba1186533cee8ae74c707b95a973048764bf66e7b
1 /*
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
32 * IN THE SOFTWARE.
35 #include "common.h"
37 #include <linux/kthread.h>
38 #include <linux/if_vlan.h>
39 #include <linux/udp.h>
41 #include <net/tcp.h>
42 #include <net/ip6_checksum.h>
44 #include <xen/xen.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
53 * enabled by default.
55 bool separate_tx_rx_irq = 1;
56 module_param(separate_tx_rx_irq, bool, 0644);
59 * This is the maximum slots a skb can have. If a guest sends a skb
60 * which exceeds this limit it is considered malicious.
62 #define FATAL_SKB_SLOTS_DEFAULT 20
63 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
64 module_param(fatal_skb_slots, uint, 0444);
67 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
68 * the maximum slots a valid packet can use. Now this value is defined
69 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
70 * all backend.
72 #define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
75 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
76 * one or more merged tx requests, otherwise it is the continuation of
77 * previous tx request.
79 static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
81 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
84 static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
85 u8 status);
87 static void make_tx_response(struct xenvif *vif,
88 struct xen_netif_tx_request *txp,
89 s8 st);
91 static inline int tx_work_todo(struct xenvif *vif);
92 static inline int rx_work_todo(struct xenvif *vif);
94 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
95 u16 id,
96 s8 st,
97 u16 offset,
98 u16 size,
99 u16 flags);
101 static inline unsigned long idx_to_pfn(struct xenvif *vif,
102 u16 idx)
104 return page_to_pfn(vif->mmap_pages[idx]);
107 static inline unsigned long idx_to_kaddr(struct xenvif *vif,
108 u16 idx)
110 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
114 * This is the amount of packet we copy rather than map, so that the
115 * guest can't fiddle with the contents of the headers while we do
116 * packet processing on them (netfilter, routing, etc).
118 #define PKT_PROT_LEN (ETH_HLEN + \
119 VLAN_HLEN + \
120 sizeof(struct iphdr) + MAX_IPOPTLEN + \
121 sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE)
123 static u16 frag_get_pending_idx(skb_frag_t *frag)
125 return (u16)frag->page_offset;
128 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
130 frag->page_offset = pending_idx;
133 static inline pending_ring_idx_t pending_index(unsigned i)
135 return i & (MAX_PENDING_REQS-1);
138 static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
140 return MAX_PENDING_REQS -
141 vif->pending_prod + vif->pending_cons;
144 static int max_required_rx_slots(struct xenvif *vif)
146 int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
148 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
149 if (vif->can_sg || vif->gso || vif->gso_prefix)
150 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
152 return max;
155 int xenvif_rx_ring_full(struct xenvif *vif)
157 RING_IDX peek = vif->rx_req_cons_peek;
158 RING_IDX needed = max_required_rx_slots(vif);
160 return ((vif->rx.sring->req_prod - peek) < needed) ||
161 ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
164 int xenvif_must_stop_queue(struct xenvif *vif)
166 if (!xenvif_rx_ring_full(vif))
167 return 0;
169 vif->rx.sring->req_event = vif->rx_req_cons_peek +
170 max_required_rx_slots(vif);
171 mb(); /* request notification /then/ check the queue */
173 return xenvif_rx_ring_full(vif);
177 * Returns true if we should start a new receive buffer instead of
178 * adding 'size' bytes to a buffer which currently contains 'offset'
179 * bytes.
181 static bool start_new_rx_buffer(int offset, unsigned long size, int head)
183 /* simple case: we have completely filled the current buffer. */
184 if (offset == MAX_BUFFER_OFFSET)
185 return true;
188 * complex case: start a fresh buffer if the current frag
189 * would overflow the current buffer but only if:
190 * (i) this frag would fit completely in the next buffer
191 * and (ii) there is already some data in the current buffer
192 * and (iii) this is not the head buffer.
194 * Where:
195 * - (i) stops us splitting a frag into two copies
196 * unless the frag is too large for a single buffer.
197 * - (ii) stops us from leaving a buffer pointlessly empty.
198 * - (iii) stops us leaving the first buffer
199 * empty. Strictly speaking this is already covered
200 * by (ii) but is explicitly checked because
201 * netfront relies on the first buffer being
202 * non-empty and can crash otherwise.
204 * This means we will effectively linearise small
205 * frags but do not needlessly split large buffers
206 * into multiple copies tend to give large frags their
207 * own buffers as before.
209 BUG_ON(size > MAX_BUFFER_OFFSET);
210 if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head)
211 return true;
213 return false;
216 struct xenvif_count_slot_state {
217 unsigned long copy_off;
218 bool head;
221 unsigned int xenvif_count_frag_slots(struct xenvif *vif,
222 unsigned long offset, unsigned long size,
223 struct xenvif_count_slot_state *state)
225 unsigned count = 0;
227 offset &= ~PAGE_MASK;
229 while (size > 0) {
230 unsigned long bytes;
232 bytes = PAGE_SIZE - offset;
234 if (bytes > size)
235 bytes = size;
237 if (start_new_rx_buffer(state->copy_off, bytes, state->head)) {
238 count++;
239 state->copy_off = 0;
242 if (state->copy_off + bytes > MAX_BUFFER_OFFSET)
243 bytes = MAX_BUFFER_OFFSET - state->copy_off;
245 state->copy_off += bytes;
247 offset += bytes;
248 size -= bytes;
250 if (offset == PAGE_SIZE)
251 offset = 0;
253 state->head = false;
256 return count;
260 * Figure out how many ring slots we're going to need to send @skb to
261 * the guest. This function is essentially a dry run of
262 * xenvif_gop_frag_copy.
264 unsigned int xenvif_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
266 struct xenvif_count_slot_state state;
267 unsigned int count;
268 unsigned char *data;
269 unsigned i;
271 state.head = true;
272 state.copy_off = 0;
274 /* Slot for the first (partial) page of data. */
275 count = 1;
277 /* Need a slot for the GSO prefix for GSO extra data? */
278 if (skb_shinfo(skb)->gso_size)
279 count++;
281 data = skb->data;
282 while (data < skb_tail_pointer(skb)) {
283 unsigned long offset = offset_in_page(data);
284 unsigned long size = PAGE_SIZE - offset;
286 if (data + size > skb_tail_pointer(skb))
287 size = skb_tail_pointer(skb) - data;
289 count += xenvif_count_frag_slots(vif, offset, size, &state);
291 data += size;
294 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
295 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
296 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
298 count += xenvif_count_frag_slots(vif, offset, size, &state);
300 return count;
303 struct netrx_pending_operations {
304 unsigned copy_prod, copy_cons;
305 unsigned meta_prod, meta_cons;
306 struct gnttab_copy *copy;
307 struct xenvif_rx_meta *meta;
308 int copy_off;
309 grant_ref_t copy_gref;
312 static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
313 struct netrx_pending_operations *npo)
315 struct xenvif_rx_meta *meta;
316 struct xen_netif_rx_request *req;
318 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
320 meta = npo->meta + npo->meta_prod++;
321 meta->gso_size = 0;
322 meta->size = 0;
323 meta->id = req->id;
325 npo->copy_off = 0;
326 npo->copy_gref = req->gref;
328 return meta;
332 * Set up the grant operations for this fragment. If it's a flipping
333 * interface, we also set up the unmap request from here.
335 static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
336 struct netrx_pending_operations *npo,
337 struct page *page, unsigned long size,
338 unsigned long offset, int *head)
340 struct gnttab_copy *copy_gop;
341 struct xenvif_rx_meta *meta;
342 unsigned long bytes;
344 /* Data must not cross a page boundary. */
345 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
347 meta = npo->meta + npo->meta_prod - 1;
349 /* Skip unused frames from start of page */
350 page += offset >> PAGE_SHIFT;
351 offset &= ~PAGE_MASK;
353 while (size > 0) {
354 BUG_ON(offset >= PAGE_SIZE);
355 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
357 bytes = PAGE_SIZE - offset;
359 if (bytes > size)
360 bytes = size;
362 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
364 * Netfront requires there to be some data in the head
365 * buffer.
367 BUG_ON(*head);
369 meta = get_next_rx_buffer(vif, npo);
372 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
373 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
375 copy_gop = npo->copy + npo->copy_prod++;
376 copy_gop->flags = GNTCOPY_dest_gref;
377 copy_gop->len = bytes;
379 copy_gop->source.domid = DOMID_SELF;
380 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
381 copy_gop->source.offset = offset;
383 copy_gop->dest.domid = vif->domid;
384 copy_gop->dest.offset = npo->copy_off;
385 copy_gop->dest.u.ref = npo->copy_gref;
387 npo->copy_off += bytes;
388 meta->size += bytes;
390 offset += bytes;
391 size -= bytes;
393 /* Next frame */
394 if (offset == PAGE_SIZE && size) {
395 BUG_ON(!PageCompound(page));
396 page++;
397 offset = 0;
400 /* Leave a gap for the GSO descriptor. */
401 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
402 vif->rx.req_cons++;
404 *head = 0; /* There must be something in this buffer now. */
410 * Prepare an SKB to be transmitted to the frontend.
412 * This function is responsible for allocating grant operations, meta
413 * structures, etc.
415 * It returns the number of meta structures consumed. The number of
416 * ring slots used is always equal to the number of meta slots used
417 * plus the number of GSO descriptors used. Currently, we use either
418 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
419 * frontend-side LRO).
421 static int xenvif_gop_skb(struct sk_buff *skb,
422 struct netrx_pending_operations *npo)
424 struct xenvif *vif = netdev_priv(skb->dev);
425 int nr_frags = skb_shinfo(skb)->nr_frags;
426 int i;
427 struct xen_netif_rx_request *req;
428 struct xenvif_rx_meta *meta;
429 unsigned char *data;
430 int head = 1;
431 int old_meta_prod;
433 old_meta_prod = npo->meta_prod;
435 /* Set up a GSO prefix descriptor, if necessary */
436 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
437 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
438 meta = npo->meta + npo->meta_prod++;
439 meta->gso_size = skb_shinfo(skb)->gso_size;
440 meta->size = 0;
441 meta->id = req->id;
444 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
445 meta = npo->meta + npo->meta_prod++;
447 if (!vif->gso_prefix)
448 meta->gso_size = skb_shinfo(skb)->gso_size;
449 else
450 meta->gso_size = 0;
452 meta->size = 0;
453 meta->id = req->id;
454 npo->copy_off = 0;
455 npo->copy_gref = req->gref;
457 data = skb->data;
458 while (data < skb_tail_pointer(skb)) {
459 unsigned int offset = offset_in_page(data);
460 unsigned int len = PAGE_SIZE - offset;
462 if (data + len > skb_tail_pointer(skb))
463 len = skb_tail_pointer(skb) - data;
465 xenvif_gop_frag_copy(vif, skb, npo,
466 virt_to_page(data), len, offset, &head);
467 data += len;
470 for (i = 0; i < nr_frags; i++) {
471 xenvif_gop_frag_copy(vif, skb, npo,
472 skb_frag_page(&skb_shinfo(skb)->frags[i]),
473 skb_frag_size(&skb_shinfo(skb)->frags[i]),
474 skb_shinfo(skb)->frags[i].page_offset,
475 &head);
478 return npo->meta_prod - old_meta_prod;
482 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
483 * used to set up the operations on the top of
484 * netrx_pending_operations, which have since been done. Check that
485 * they didn't give any errors and advance over them.
487 static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
488 struct netrx_pending_operations *npo)
490 struct gnttab_copy *copy_op;
491 int status = XEN_NETIF_RSP_OKAY;
492 int i;
494 for (i = 0; i < nr_meta_slots; i++) {
495 copy_op = npo->copy + npo->copy_cons++;
496 if (copy_op->status != GNTST_okay) {
497 netdev_dbg(vif->dev,
498 "Bad status %d from copy to DOM%d.\n",
499 copy_op->status, vif->domid);
500 status = XEN_NETIF_RSP_ERROR;
504 return status;
507 static void xenvif_add_frag_responses(struct xenvif *vif, int status,
508 struct xenvif_rx_meta *meta,
509 int nr_meta_slots)
511 int i;
512 unsigned long offset;
514 /* No fragments used */
515 if (nr_meta_slots <= 1)
516 return;
518 nr_meta_slots--;
520 for (i = 0; i < nr_meta_slots; i++) {
521 int flags;
522 if (i == nr_meta_slots - 1)
523 flags = 0;
524 else
525 flags = XEN_NETRXF_more_data;
527 offset = 0;
528 make_rx_response(vif, meta[i].id, status, offset,
529 meta[i].size, flags);
533 struct skb_cb_overlay {
534 int meta_slots_used;
537 static void xenvif_kick_thread(struct xenvif *vif)
539 wake_up(&vif->wq);
542 void xenvif_rx_action(struct xenvif *vif)
544 s8 status;
545 u16 flags;
546 struct xen_netif_rx_response *resp;
547 struct sk_buff_head rxq;
548 struct sk_buff *skb;
549 LIST_HEAD(notify);
550 int ret;
551 int nr_frags;
552 int count;
553 unsigned long offset;
554 struct skb_cb_overlay *sco;
555 int need_to_notify = 0;
557 struct netrx_pending_operations npo = {
558 .copy = vif->grant_copy_op,
559 .meta = vif->meta,
562 skb_queue_head_init(&rxq);
564 count = 0;
566 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
567 vif = netdev_priv(skb->dev);
568 nr_frags = skb_shinfo(skb)->nr_frags;
570 sco = (struct skb_cb_overlay *)skb->cb;
571 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
573 count += nr_frags + 1;
575 __skb_queue_tail(&rxq, skb);
577 /* Filled the batch queue? */
578 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
579 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
580 break;
583 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
585 if (!npo.copy_prod)
586 return;
588 BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op));
589 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
591 while ((skb = __skb_dequeue(&rxq)) != NULL) {
592 sco = (struct skb_cb_overlay *)skb->cb;
594 vif = netdev_priv(skb->dev);
596 if (vif->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
597 resp = RING_GET_RESPONSE(&vif->rx,
598 vif->rx.rsp_prod_pvt++);
600 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
602 resp->offset = vif->meta[npo.meta_cons].gso_size;
603 resp->id = vif->meta[npo.meta_cons].id;
604 resp->status = sco->meta_slots_used;
606 npo.meta_cons++;
607 sco->meta_slots_used--;
611 vif->dev->stats.tx_bytes += skb->len;
612 vif->dev->stats.tx_packets++;
614 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
616 if (sco->meta_slots_used == 1)
617 flags = 0;
618 else
619 flags = XEN_NETRXF_more_data;
621 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
622 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
623 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
624 /* remote but checksummed. */
625 flags |= XEN_NETRXF_data_validated;
627 offset = 0;
628 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
629 status, offset,
630 vif->meta[npo.meta_cons].size,
631 flags);
633 if (vif->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
634 struct xen_netif_extra_info *gso =
635 (struct xen_netif_extra_info *)
636 RING_GET_RESPONSE(&vif->rx,
637 vif->rx.rsp_prod_pvt++);
639 resp->flags |= XEN_NETRXF_extra_info;
641 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
642 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
643 gso->u.gso.pad = 0;
644 gso->u.gso.features = 0;
646 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
647 gso->flags = 0;
650 xenvif_add_frag_responses(vif, status,
651 vif->meta + npo.meta_cons + 1,
652 sco->meta_slots_used);
654 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
656 if (ret)
657 need_to_notify = 1;
659 xenvif_notify_tx_completion(vif);
661 npo.meta_cons += sco->meta_slots_used;
662 dev_kfree_skb(skb);
665 if (need_to_notify)
666 notify_remote_via_irq(vif->rx_irq);
668 /* More work to do? */
669 if (!skb_queue_empty(&vif->rx_queue))
670 xenvif_kick_thread(vif);
673 void xenvif_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
675 skb_queue_tail(&vif->rx_queue, skb);
677 xenvif_kick_thread(vif);
680 void xenvif_check_rx_xenvif(struct xenvif *vif)
682 int more_to_do;
684 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
686 if (more_to_do)
687 napi_schedule(&vif->napi);
690 static void tx_add_credit(struct xenvif *vif)
692 unsigned long max_burst, max_credit;
695 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
696 * Otherwise the interface can seize up due to insufficient credit.
698 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
699 max_burst = min(max_burst, 131072UL);
700 max_burst = max(max_burst, vif->credit_bytes);
702 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
703 max_credit = vif->remaining_credit + vif->credit_bytes;
704 if (max_credit < vif->remaining_credit)
705 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
707 vif->remaining_credit = min(max_credit, max_burst);
710 static void tx_credit_callback(unsigned long data)
712 struct xenvif *vif = (struct xenvif *)data;
713 tx_add_credit(vif);
714 xenvif_check_rx_xenvif(vif);
717 static void xenvif_tx_err(struct xenvif *vif,
718 struct xen_netif_tx_request *txp, RING_IDX end)
720 RING_IDX cons = vif->tx.req_cons;
722 do {
723 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
724 if (cons == end)
725 break;
726 txp = RING_GET_REQUEST(&vif->tx, cons++);
727 } while (1);
728 vif->tx.req_cons = cons;
731 static void xenvif_fatal_tx_err(struct xenvif *vif)
733 netdev_err(vif->dev, "fatal error; disabling device\n");
734 vif->disabled = true;
735 xenvif_kick_thread(vif);
738 static int xenvif_count_requests(struct xenvif *vif,
739 struct xen_netif_tx_request *first,
740 struct xen_netif_tx_request *txp,
741 int work_to_do)
743 RING_IDX cons = vif->tx.req_cons;
744 int slots = 0;
745 int drop_err = 0;
746 int more_data;
748 if (!(first->flags & XEN_NETTXF_more_data))
749 return 0;
751 do {
752 struct xen_netif_tx_request dropped_tx = { 0 };
754 if (slots >= work_to_do) {
755 netdev_err(vif->dev,
756 "Asked for %d slots but exceeds this limit\n",
757 work_to_do);
758 xenvif_fatal_tx_err(vif);
759 return -ENODATA;
762 /* This guest is really using too many slots and
763 * considered malicious.
765 if (unlikely(slots >= fatal_skb_slots)) {
766 netdev_err(vif->dev,
767 "Malicious frontend using %d slots, threshold %u\n",
768 slots, fatal_skb_slots);
769 xenvif_fatal_tx_err(vif);
770 return -E2BIG;
773 /* Xen network protocol had implicit dependency on
774 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
775 * the historical MAX_SKB_FRAGS value 18 to honor the
776 * same behavior as before. Any packet using more than
777 * 18 slots but less than fatal_skb_slots slots is
778 * dropped
780 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
781 if (net_ratelimit())
782 netdev_dbg(vif->dev,
783 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
784 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
785 drop_err = -E2BIG;
788 if (drop_err)
789 txp = &dropped_tx;
791 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
792 sizeof(*txp));
794 /* If the guest submitted a frame >= 64 KiB then
795 * first->size overflowed and following slots will
796 * appear to be larger than the frame.
798 * This cannot be fatal error as there are buggy
799 * frontends that do this.
801 * Consume all slots and drop the packet.
803 if (!drop_err && txp->size > first->size) {
804 if (net_ratelimit())
805 netdev_dbg(vif->dev,
806 "Invalid tx request, slot size %u > remaining size %u\n",
807 txp->size, first->size);
808 drop_err = -EIO;
811 first->size -= txp->size;
812 slots++;
814 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
815 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
816 txp->offset, txp->size);
817 xenvif_fatal_tx_err(vif);
818 return -EINVAL;
821 more_data = txp->flags & XEN_NETTXF_more_data;
823 if (!drop_err)
824 txp++;
826 } while (more_data);
828 if (drop_err) {
829 xenvif_tx_err(vif, first, cons + slots);
830 return drop_err;
833 return slots;
836 static struct page *xenvif_alloc_page(struct xenvif *vif,
837 u16 pending_idx)
839 struct page *page;
841 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
842 if (!page)
843 return NULL;
844 vif->mmap_pages[pending_idx] = page;
846 return page;
849 static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
850 struct sk_buff *skb,
851 struct xen_netif_tx_request *txp,
852 struct gnttab_copy *gop)
854 struct skb_shared_info *shinfo = skb_shinfo(skb);
855 skb_frag_t *frags = shinfo->frags;
856 u16 pending_idx = *((u16 *)skb->data);
857 u16 head_idx = 0;
858 int slot, start;
859 struct page *page;
860 pending_ring_idx_t index, start_idx = 0;
861 uint16_t dst_offset;
862 unsigned int nr_slots;
863 struct pending_tx_info *first = NULL;
865 /* At this point shinfo->nr_frags is in fact the number of
866 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
868 nr_slots = shinfo->nr_frags;
870 /* Skip first skb fragment if it is on same page as header fragment. */
871 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
873 /* Coalesce tx requests, at this point the packet passed in
874 * should be <= 64K. Any packets larger than 64K have been
875 * handled in xenvif_count_requests().
877 for (shinfo->nr_frags = slot = start; slot < nr_slots;
878 shinfo->nr_frags++) {
879 struct pending_tx_info *pending_tx_info =
880 vif->pending_tx_info;
882 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
883 if (!page)
884 goto err;
886 dst_offset = 0;
887 first = NULL;
888 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
889 gop->flags = GNTCOPY_source_gref;
891 gop->source.u.ref = txp->gref;
892 gop->source.domid = vif->domid;
893 gop->source.offset = txp->offset;
895 gop->dest.domid = DOMID_SELF;
897 gop->dest.offset = dst_offset;
898 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
900 if (dst_offset + txp->size > PAGE_SIZE) {
901 /* This page can only merge a portion
902 * of tx request. Do not increment any
903 * pointer / counter here. The txp
904 * will be dealt with in future
905 * rounds, eventually hitting the
906 * `else` branch.
908 gop->len = PAGE_SIZE - dst_offset;
909 txp->offset += gop->len;
910 txp->size -= gop->len;
911 dst_offset += gop->len; /* quit loop */
912 } else {
913 /* This tx request can be merged in the page */
914 gop->len = txp->size;
915 dst_offset += gop->len;
917 index = pending_index(vif->pending_cons++);
919 pending_idx = vif->pending_ring[index];
921 memcpy(&pending_tx_info[pending_idx].req, txp,
922 sizeof(*txp));
924 /* Poison these fields, corresponding
925 * fields for head tx req will be set
926 * to correct values after the loop.
928 vif->mmap_pages[pending_idx] = (void *)(~0UL);
929 pending_tx_info[pending_idx].head =
930 INVALID_PENDING_RING_IDX;
932 if (!first) {
933 first = &pending_tx_info[pending_idx];
934 start_idx = index;
935 head_idx = pending_idx;
938 txp++;
939 slot++;
942 gop++;
945 first->req.offset = 0;
946 first->req.size = dst_offset;
947 first->head = start_idx;
948 vif->mmap_pages[head_idx] = page;
949 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
952 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
954 return gop;
955 err:
956 /* Unwind, freeing all pages and sending error responses. */
957 while (shinfo->nr_frags-- > start) {
958 xenvif_idx_release(vif,
959 frag_get_pending_idx(&frags[shinfo->nr_frags]),
960 XEN_NETIF_RSP_ERROR);
962 /* The head too, if necessary. */
963 if (start)
964 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
966 return NULL;
969 static int xenvif_tx_check_gop(struct xenvif *vif,
970 struct sk_buff *skb,
971 struct gnttab_copy **gopp)
973 struct gnttab_copy *gop = *gopp;
974 u16 pending_idx = *((u16 *)skb->data);
975 struct skb_shared_info *shinfo = skb_shinfo(skb);
976 struct pending_tx_info *tx_info;
977 int nr_frags = shinfo->nr_frags;
978 int i, err, start;
979 u16 peek; /* peek into next tx request */
981 /* Check status of header. */
982 err = gop->status;
983 if (unlikely(err))
984 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
986 /* Skip first skb fragment if it is on same page as header fragment. */
987 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
989 for (i = start; i < nr_frags; i++) {
990 int j, newerr;
991 pending_ring_idx_t head;
993 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
994 tx_info = &vif->pending_tx_info[pending_idx];
995 head = tx_info->head;
997 /* Check error status: if okay then remember grant handle. */
998 do {
999 newerr = (++gop)->status;
1000 if (newerr)
1001 break;
1002 peek = vif->pending_ring[pending_index(++head)];
1003 } while (!pending_tx_is_head(vif, peek));
1005 if (likely(!newerr)) {
1006 /* Had a previous error? Invalidate this fragment. */
1007 if (unlikely(err))
1008 xenvif_idx_release(vif, pending_idx,
1009 XEN_NETIF_RSP_OKAY);
1010 continue;
1013 /* Error on this fragment: respond to client with an error. */
1014 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
1016 /* Not the first error? Preceding frags already invalidated. */
1017 if (err)
1018 continue;
1020 /* First error: invalidate header and preceding fragments. */
1021 pending_idx = *((u16 *)skb->data);
1022 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
1023 for (j = start; j < i; j++) {
1024 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1025 xenvif_idx_release(vif, pending_idx,
1026 XEN_NETIF_RSP_OKAY);
1029 /* Remember the error: invalidate all subsequent fragments. */
1030 err = newerr;
1033 *gopp = gop + 1;
1034 return err;
1037 static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
1039 struct skb_shared_info *shinfo = skb_shinfo(skb);
1040 int nr_frags = shinfo->nr_frags;
1041 int i;
1043 for (i = 0; i < nr_frags; i++) {
1044 skb_frag_t *frag = shinfo->frags + i;
1045 struct xen_netif_tx_request *txp;
1046 struct page *page;
1047 u16 pending_idx;
1049 pending_idx = frag_get_pending_idx(frag);
1051 txp = &vif->pending_tx_info[pending_idx].req;
1052 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
1053 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1054 skb->len += txp->size;
1055 skb->data_len += txp->size;
1056 skb->truesize += txp->size;
1058 /* Take an extra reference to offset xenvif_idx_release */
1059 get_page(vif->mmap_pages[pending_idx]);
1060 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
1064 static int xenvif_get_extras(struct xenvif *vif,
1065 struct xen_netif_extra_info *extras,
1066 int work_to_do)
1068 struct xen_netif_extra_info extra;
1069 RING_IDX cons = vif->tx.req_cons;
1071 do {
1072 if (unlikely(work_to_do-- <= 0)) {
1073 netdev_err(vif->dev, "Missing extra info\n");
1074 xenvif_fatal_tx_err(vif);
1075 return -EBADR;
1078 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1079 sizeof(extra));
1080 if (unlikely(!extra.type ||
1081 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1082 vif->tx.req_cons = ++cons;
1083 netdev_err(vif->dev,
1084 "Invalid extra type: %d\n", extra.type);
1085 xenvif_fatal_tx_err(vif);
1086 return -EINVAL;
1089 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1090 vif->tx.req_cons = ++cons;
1091 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1093 return work_to_do;
1096 static int xenvif_set_skb_gso(struct xenvif *vif,
1097 struct sk_buff *skb,
1098 struct xen_netif_extra_info *gso)
1100 if (!gso->u.gso.size) {
1101 netdev_err(vif->dev, "GSO size must not be zero.\n");
1102 xenvif_fatal_tx_err(vif);
1103 return -EINVAL;
1106 /* Currently only TCPv4 S.O. is supported. */
1107 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1108 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1109 xenvif_fatal_tx_err(vif);
1110 return -EINVAL;
1113 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1114 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1116 /* Header must be checked, and gso_segs computed. */
1117 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1118 skb_shinfo(skb)->gso_segs = 0;
1120 return 0;
1123 static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1125 struct iphdr *iph;
1126 int err = -EPROTO;
1127 int recalculate_partial_csum = 0;
1130 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1131 * peers can fail to set NETRXF_csum_blank when sending a GSO
1132 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1133 * recalculate the partial checksum.
1135 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1136 vif->rx_gso_checksum_fixup++;
1137 skb->ip_summed = CHECKSUM_PARTIAL;
1138 recalculate_partial_csum = 1;
1141 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1142 if (skb->ip_summed != CHECKSUM_PARTIAL)
1143 return 0;
1145 if (skb->protocol != htons(ETH_P_IP))
1146 goto out;
1148 iph = (void *)skb->data;
1149 switch (iph->protocol) {
1150 case IPPROTO_TCP:
1151 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1152 offsetof(struct tcphdr, check)))
1153 goto out;
1155 if (recalculate_partial_csum) {
1156 struct tcphdr *tcph = tcp_hdr(skb);
1157 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1158 skb->len - iph->ihl*4,
1159 IPPROTO_TCP, 0);
1161 break;
1162 case IPPROTO_UDP:
1163 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1164 offsetof(struct udphdr, check)))
1165 goto out;
1167 if (recalculate_partial_csum) {
1168 struct udphdr *udph = udp_hdr(skb);
1169 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1170 skb->len - iph->ihl*4,
1171 IPPROTO_UDP, 0);
1173 break;
1174 default:
1175 if (net_ratelimit())
1176 netdev_err(vif->dev,
1177 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1178 iph->protocol);
1179 goto out;
1182 err = 0;
1184 out:
1185 return err;
1188 static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1190 u64 now = get_jiffies_64();
1191 u64 next_credit = vif->credit_window_start +
1192 msecs_to_jiffies(vif->credit_usec / 1000);
1194 /* Timer could already be pending in rare cases. */
1195 if (timer_pending(&vif->credit_timeout))
1196 return true;
1198 /* Passed the point where we can replenish credit? */
1199 if (time_after_eq64(now, next_credit)) {
1200 vif->credit_window_start = now;
1201 tx_add_credit(vif);
1204 /* Still too big to send right now? Set a callback. */
1205 if (size > vif->remaining_credit) {
1206 vif->credit_timeout.data =
1207 (unsigned long)vif;
1208 vif->credit_timeout.function =
1209 tx_credit_callback;
1210 mod_timer(&vif->credit_timeout,
1211 next_credit);
1212 vif->credit_window_start = next_credit;
1214 return true;
1217 return false;
1220 static unsigned xenvif_tx_build_gops(struct xenvif *vif)
1222 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
1223 struct sk_buff *skb;
1224 int ret;
1226 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1227 < MAX_PENDING_REQS)) {
1228 struct xen_netif_tx_request txreq;
1229 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1230 struct page *page;
1231 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1232 u16 pending_idx;
1233 RING_IDX idx;
1234 int work_to_do;
1235 unsigned int data_len;
1236 pending_ring_idx_t index;
1238 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1239 XEN_NETIF_TX_RING_SIZE) {
1240 netdev_err(vif->dev,
1241 "Impossible number of requests. "
1242 "req_prod %d, req_cons %d, size %ld\n",
1243 vif->tx.sring->req_prod, vif->tx.req_cons,
1244 XEN_NETIF_TX_RING_SIZE);
1245 xenvif_fatal_tx_err(vif);
1246 break;
1249 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1250 if (!work_to_do)
1251 break;
1253 idx = vif->tx.req_cons;
1254 rmb(); /* Ensure that we see the request before we copy it. */
1255 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1257 /* Credit-based scheduling. */
1258 if (txreq.size > vif->remaining_credit &&
1259 tx_credit_exceeded(vif, txreq.size))
1260 break;
1262 vif->remaining_credit -= txreq.size;
1264 work_to_do--;
1265 vif->tx.req_cons = ++idx;
1267 memset(extras, 0, sizeof(extras));
1268 if (txreq.flags & XEN_NETTXF_extra_info) {
1269 work_to_do = xenvif_get_extras(vif, extras,
1270 work_to_do);
1271 idx = vif->tx.req_cons;
1272 if (unlikely(work_to_do < 0))
1273 break;
1276 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
1277 if (unlikely(ret < 0))
1278 break;
1280 idx += ret;
1282 if (unlikely(txreq.size < ETH_HLEN)) {
1283 netdev_dbg(vif->dev,
1284 "Bad packet size: %d\n", txreq.size);
1285 xenvif_tx_err(vif, &txreq, idx);
1286 break;
1289 /* No crossing a page as the payload mustn't fragment. */
1290 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1291 netdev_err(vif->dev,
1292 "txreq.offset: %x, size: %u, end: %lu\n",
1293 txreq.offset, txreq.size,
1294 (txreq.offset&~PAGE_MASK) + txreq.size);
1295 xenvif_fatal_tx_err(vif);
1296 break;
1299 index = pending_index(vif->pending_cons);
1300 pending_idx = vif->pending_ring[index];
1302 data_len = (txreq.size > PKT_PROT_LEN &&
1303 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1304 PKT_PROT_LEN : txreq.size;
1306 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1307 GFP_ATOMIC | __GFP_NOWARN);
1308 if (unlikely(skb == NULL)) {
1309 netdev_dbg(vif->dev,
1310 "Can't allocate a skb in start_xmit.\n");
1311 xenvif_tx_err(vif, &txreq, idx);
1312 break;
1315 /* Packets passed to netif_rx() must have some headroom. */
1316 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1318 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1319 struct xen_netif_extra_info *gso;
1320 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1322 if (xenvif_set_skb_gso(vif, skb, gso)) {
1323 /* Failure in xenvif_set_skb_gso is fatal. */
1324 kfree_skb(skb);
1325 break;
1329 /* XXX could copy straight to head */
1330 page = xenvif_alloc_page(vif, pending_idx);
1331 if (!page) {
1332 kfree_skb(skb);
1333 xenvif_tx_err(vif, &txreq, idx);
1334 break;
1337 gop->source.u.ref = txreq.gref;
1338 gop->source.domid = vif->domid;
1339 gop->source.offset = txreq.offset;
1341 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1342 gop->dest.domid = DOMID_SELF;
1343 gop->dest.offset = txreq.offset;
1345 gop->len = txreq.size;
1346 gop->flags = GNTCOPY_source_gref;
1348 gop++;
1350 memcpy(&vif->pending_tx_info[pending_idx].req,
1351 &txreq, sizeof(txreq));
1352 vif->pending_tx_info[pending_idx].head = index;
1353 *((u16 *)skb->data) = pending_idx;
1355 __skb_put(skb, data_len);
1357 skb_shinfo(skb)->nr_frags = ret;
1358 if (data_len < txreq.size) {
1359 skb_shinfo(skb)->nr_frags++;
1360 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1361 pending_idx);
1362 } else {
1363 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1364 INVALID_PENDING_IDX);
1367 vif->pending_cons++;
1369 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
1370 if (request_gop == NULL) {
1371 kfree_skb(skb);
1372 xenvif_tx_err(vif, &txreq, idx);
1373 break;
1375 gop = request_gop;
1377 __skb_queue_tail(&vif->tx_queue, skb);
1379 vif->tx.req_cons = idx;
1381 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
1382 break;
1385 return gop - vif->tx_copy_ops;
1389 static int xenvif_tx_submit(struct xenvif *vif, int budget)
1391 struct gnttab_copy *gop = vif->tx_copy_ops;
1392 struct sk_buff *skb;
1393 int work_done = 0;
1395 while (work_done < budget &&
1396 (skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
1397 struct xen_netif_tx_request *txp;
1398 u16 pending_idx;
1399 unsigned data_len;
1401 pending_idx = *((u16 *)skb->data);
1402 txp = &vif->pending_tx_info[pending_idx].req;
1404 /* Check the remap error code. */
1405 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
1406 netdev_dbg(vif->dev, "netback grant failed.\n");
1407 skb_shinfo(skb)->nr_frags = 0;
1408 kfree_skb(skb);
1409 continue;
1412 data_len = skb->len;
1413 memcpy(skb->data,
1414 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
1415 data_len);
1416 if (data_len < txp->size) {
1417 /* Append the packet payload as a fragment. */
1418 txp->offset += data_len;
1419 txp->size -= data_len;
1420 } else {
1421 /* Schedule a response immediately. */
1422 xenvif_idx_release(vif, pending_idx,
1423 XEN_NETIF_RSP_OKAY);
1426 if (txp->flags & XEN_NETTXF_csum_blank)
1427 skb->ip_summed = CHECKSUM_PARTIAL;
1428 else if (txp->flags & XEN_NETTXF_data_validated)
1429 skb->ip_summed = CHECKSUM_UNNECESSARY;
1431 xenvif_fill_frags(vif, skb);
1434 * If the initial fragment was < PKT_PROT_LEN then
1435 * pull through some bytes from the other fragments to
1436 * increase the linear region to PKT_PROT_LEN bytes.
1438 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1439 int target = min_t(int, skb->len, PKT_PROT_LEN);
1440 __pskb_pull_tail(skb, target - skb_headlen(skb));
1443 skb->dev = vif->dev;
1444 skb->protocol = eth_type_trans(skb, skb->dev);
1445 skb_reset_network_header(skb);
1447 if (checksum_setup(vif, skb)) {
1448 netdev_dbg(vif->dev,
1449 "Can't setup checksum in net_tx_action\n");
1450 kfree_skb(skb);
1451 continue;
1454 skb_probe_transport_header(skb, 0);
1456 vif->dev->stats.rx_bytes += skb->len;
1457 vif->dev->stats.rx_packets++;
1459 work_done++;
1461 netif_receive_skb(skb);
1464 return work_done;
1467 /* Called after netfront has transmitted */
1468 int xenvif_tx_action(struct xenvif *vif, int budget)
1470 unsigned nr_gops;
1471 int work_done;
1473 if (unlikely(!tx_work_todo(vif)))
1474 return 0;
1476 nr_gops = xenvif_tx_build_gops(vif);
1478 if (nr_gops == 0)
1479 return 0;
1481 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
1483 work_done = xenvif_tx_submit(vif, nr_gops);
1485 return work_done;
1488 static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1489 u8 status)
1491 struct pending_tx_info *pending_tx_info;
1492 pending_ring_idx_t head;
1493 u16 peek; /* peek into next tx request */
1495 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
1497 /* Already complete? */
1498 if (vif->mmap_pages[pending_idx] == NULL)
1499 return;
1501 pending_tx_info = &vif->pending_tx_info[pending_idx];
1503 head = pending_tx_info->head;
1505 BUG_ON(!pending_tx_is_head(vif, head));
1506 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
1508 do {
1509 pending_ring_idx_t index;
1510 pending_ring_idx_t idx = pending_index(head);
1511 u16 info_idx = vif->pending_ring[idx];
1513 pending_tx_info = &vif->pending_tx_info[info_idx];
1514 make_tx_response(vif, &pending_tx_info->req, status);
1516 /* Setting any number other than
1517 * INVALID_PENDING_RING_IDX indicates this slot is
1518 * starting a new packet / ending a previous packet.
1520 pending_tx_info->head = 0;
1522 index = pending_index(vif->pending_prod++);
1523 vif->pending_ring[index] = vif->pending_ring[info_idx];
1525 peek = vif->pending_ring[pending_index(++head)];
1527 } while (!pending_tx_is_head(vif, peek));
1529 put_page(vif->mmap_pages[pending_idx]);
1530 vif->mmap_pages[pending_idx] = NULL;
1534 static void make_tx_response(struct xenvif *vif,
1535 struct xen_netif_tx_request *txp,
1536 s8 st)
1538 RING_IDX i = vif->tx.rsp_prod_pvt;
1539 struct xen_netif_tx_response *resp;
1540 int notify;
1542 resp = RING_GET_RESPONSE(&vif->tx, i);
1543 resp->id = txp->id;
1544 resp->status = st;
1546 if (txp->flags & XEN_NETTXF_extra_info)
1547 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1549 vif->tx.rsp_prod_pvt = ++i;
1550 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1551 if (notify)
1552 notify_remote_via_irq(vif->tx_irq);
1555 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1556 u16 id,
1557 s8 st,
1558 u16 offset,
1559 u16 size,
1560 u16 flags)
1562 RING_IDX i = vif->rx.rsp_prod_pvt;
1563 struct xen_netif_rx_response *resp;
1565 resp = RING_GET_RESPONSE(&vif->rx, i);
1566 resp->offset = offset;
1567 resp->flags = flags;
1568 resp->id = id;
1569 resp->status = (s16)size;
1570 if (st < 0)
1571 resp->status = (s16)st;
1573 vif->rx.rsp_prod_pvt = ++i;
1575 return resp;
1578 static inline int rx_work_todo(struct xenvif *vif)
1580 return !skb_queue_empty(&vif->rx_queue);
1583 static inline int tx_work_todo(struct xenvif *vif)
1586 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1587 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1588 < MAX_PENDING_REQS))
1589 return 1;
1591 return 0;
1594 void xenvif_unmap_frontend_rings(struct xenvif *vif)
1596 if (vif->tx.sring)
1597 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1598 vif->tx.sring);
1599 if (vif->rx.sring)
1600 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1601 vif->rx.sring);
1604 int xenvif_map_frontend_rings(struct xenvif *vif,
1605 grant_ref_t tx_ring_ref,
1606 grant_ref_t rx_ring_ref)
1608 void *addr;
1609 struct xen_netif_tx_sring *txs;
1610 struct xen_netif_rx_sring *rxs;
1612 int err = -ENOMEM;
1614 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1615 tx_ring_ref, &addr);
1616 if (err)
1617 goto err;
1619 txs = (struct xen_netif_tx_sring *)addr;
1620 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1622 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1623 rx_ring_ref, &addr);
1624 if (err)
1625 goto err;
1627 rxs = (struct xen_netif_rx_sring *)addr;
1628 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1630 vif->rx_req_cons_peek = 0;
1632 return 0;
1634 err:
1635 xenvif_unmap_frontend_rings(vif);
1636 return err;
1639 int xenvif_kthread(void *data)
1641 struct xenvif *vif = data;
1643 while (!kthread_should_stop()) {
1644 wait_event_interruptible(vif->wq,
1645 rx_work_todo(vif) ||
1646 vif->disabled ||
1647 kthread_should_stop());
1649 /* This frontend is found to be rogue, disable it in
1650 * kthread context. Currently this is only set when
1651 * netback finds out frontend sends malformed packet,
1652 * but we cannot disable the interface in softirq
1653 * context so we defer it here.
1655 if (unlikely(vif->disabled && netif_carrier_ok(vif->dev)))
1656 xenvif_carrier_off(vif);
1658 if (kthread_should_stop())
1659 break;
1661 if (rx_work_todo(vif))
1662 xenvif_rx_action(vif);
1664 cond_resched();
1667 return 0;
1670 static int __init netback_init(void)
1672 int rc = 0;
1674 if (!xen_domain())
1675 return -ENODEV;
1677 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1678 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1679 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1680 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1683 rc = xenvif_xenbus_init();
1684 if (rc)
1685 goto failed_init;
1687 return 0;
1689 failed_init:
1690 return rc;
1693 module_init(netback_init);
1695 static void __exit netback_fini(void)
1697 xenvif_xenbus_fini();
1699 module_exit(netback_fini);
1701 MODULE_LICENSE("Dual BSD/GPL");
1702 MODULE_ALIAS("xen-backend:vif");