blob: e2793d06e5d1c24d2633997947cac1fc620fe406 [file] [log] [blame]
Ian Campbellf942dc22011-03-15 00:06:18 +00001/*
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
7 *
8 * Copyright (c) 2002-2005, K A Fraser
9 *
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:
15 *
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:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
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.
33 */
34
35#include "common.h"
36
37#include <linux/kthread.h>
38#include <linux/if_vlan.h>
39#include <linux/udp.h>
40
41#include <net/tcp.h>
42
43#include <xen/events.h>
44#include <xen/interface/memory.h>
45
46#include <asm/xen/hypercall.h>
47#include <asm/xen/page.h>
48
49struct pending_tx_info {
50 struct xen_netif_tx_request req;
51 struct xenvif *vif;
52};
53typedef unsigned int pending_ring_idx_t;
54
55struct netbk_rx_meta {
56 int id;
57 int size;
58 int gso_size;
59};
60
61#define MAX_PENDING_REQS 256
62
Ian Campbellea066ad2011-10-05 00:28:46 +000063/* Discriminate from any valid pending_idx value. */
64#define INVALID_PENDING_IDX 0xFFFF
65
Ian Campbellf942dc22011-03-15 00:06:18 +000066#define MAX_BUFFER_OFFSET PAGE_SIZE
67
68/* extra field used in struct page */
69union page_ext {
70 struct {
71#if BITS_PER_LONG < 64
72#define IDX_WIDTH 8
73#define GROUP_WIDTH (BITS_PER_LONG - IDX_WIDTH)
74 unsigned int group:GROUP_WIDTH;
75 unsigned int idx:IDX_WIDTH;
76#else
77 unsigned int group, idx;
78#endif
79 } e;
80 void *mapping;
81};
82
83struct xen_netbk {
84 wait_queue_head_t wq;
85 struct task_struct *task;
86
87 struct sk_buff_head rx_queue;
88 struct sk_buff_head tx_queue;
89
90 struct timer_list net_timer;
91
92 struct page *mmap_pages[MAX_PENDING_REQS];
93
94 pending_ring_idx_t pending_prod;
95 pending_ring_idx_t pending_cons;
96 struct list_head net_schedule_list;
97
98 /* Protect the net_schedule_list in netif. */
99 spinlock_t net_schedule_list_lock;
100
101 atomic_t netfront_count;
102
103 struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
104 struct gnttab_copy tx_copy_ops[MAX_PENDING_REQS];
105
106 u16 pending_ring[MAX_PENDING_REQS];
107
108 /*
109 * Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
110 * head/fragment page uses 2 copy operations because it
111 * straddles two buffers in the frontend.
112 */
113 struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
114 struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
115};
116
117static struct xen_netbk *xen_netbk;
118static int xen_netbk_group_nr;
119
120void xen_netbk_add_xenvif(struct xenvif *vif)
121{
122 int i;
123 int min_netfront_count;
124 int min_group = 0;
125 struct xen_netbk *netbk;
126
127 min_netfront_count = atomic_read(&xen_netbk[0].netfront_count);
128 for (i = 0; i < xen_netbk_group_nr; i++) {
129 int netfront_count = atomic_read(&xen_netbk[i].netfront_count);
130 if (netfront_count < min_netfront_count) {
131 min_group = i;
132 min_netfront_count = netfront_count;
133 }
134 }
135
136 netbk = &xen_netbk[min_group];
137
138 vif->netbk = netbk;
139 atomic_inc(&netbk->netfront_count);
140}
141
142void xen_netbk_remove_xenvif(struct xenvif *vif)
143{
144 struct xen_netbk *netbk = vif->netbk;
145 vif->netbk = NULL;
146 atomic_dec(&netbk->netfront_count);
147}
148
Matthew Daley33eb2602013-02-06 23:41:36 +0000149static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
150 u8 status);
Ian Campbellf942dc22011-03-15 00:06:18 +0000151static void make_tx_response(struct xenvif *vif,
152 struct xen_netif_tx_request *txp,
153 s8 st);
154static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
155 u16 id,
156 s8 st,
157 u16 offset,
158 u16 size,
159 u16 flags);
160
161static inline unsigned long idx_to_pfn(struct xen_netbk *netbk,
Ian Campbellea066ad2011-10-05 00:28:46 +0000162 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000163{
164 return page_to_pfn(netbk->mmap_pages[idx]);
165}
166
167static inline unsigned long idx_to_kaddr(struct xen_netbk *netbk,
Ian Campbellea066ad2011-10-05 00:28:46 +0000168 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000169{
170 return (unsigned long)pfn_to_kaddr(idx_to_pfn(netbk, idx));
171}
172
173/* extra field used in struct page */
174static inline void set_page_ext(struct page *pg, struct xen_netbk *netbk,
175 unsigned int idx)
176{
177 unsigned int group = netbk - xen_netbk;
178 union page_ext ext = { .e = { .group = group + 1, .idx = idx } };
179
180 BUILD_BUG_ON(sizeof(ext) > sizeof(ext.mapping));
181 pg->mapping = ext.mapping;
182}
183
184static int get_page_ext(struct page *pg,
185 unsigned int *pgroup, unsigned int *pidx)
186{
187 union page_ext ext = { .mapping = pg->mapping };
188 struct xen_netbk *netbk;
189 unsigned int group, idx;
190
191 group = ext.e.group - 1;
192
193 if (group < 0 || group >= xen_netbk_group_nr)
194 return 0;
195
196 netbk = &xen_netbk[group];
197
198 idx = ext.e.idx;
199
200 if ((idx < 0) || (idx >= MAX_PENDING_REQS))
201 return 0;
202
203 if (netbk->mmap_pages[idx] != pg)
204 return 0;
205
206 *pgroup = group;
207 *pidx = idx;
208
209 return 1;
210}
211
212/*
213 * This is the amount of packet we copy rather than map, so that the
214 * guest can't fiddle with the contents of the headers while we do
215 * packet processing on them (netfilter, routing, etc).
216 */
217#define PKT_PROT_LEN (ETH_HLEN + \
218 VLAN_HLEN + \
219 sizeof(struct iphdr) + MAX_IPOPTLEN + \
220 sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE)
221
Ian Campbellea066ad2011-10-05 00:28:46 +0000222static u16 frag_get_pending_idx(skb_frag_t *frag)
223{
224 return (u16)frag->page_offset;
225}
226
227static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
228{
229 frag->page_offset = pending_idx;
230}
231
Ian Campbellf942dc22011-03-15 00:06:18 +0000232static inline pending_ring_idx_t pending_index(unsigned i)
233{
234 return i & (MAX_PENDING_REQS-1);
235}
236
237static inline pending_ring_idx_t nr_pending_reqs(struct xen_netbk *netbk)
238{
239 return MAX_PENDING_REQS -
240 netbk->pending_prod + netbk->pending_cons;
241}
242
243static void xen_netbk_kick_thread(struct xen_netbk *netbk)
244{
245 wake_up(&netbk->wq);
246}
247
248static int max_required_rx_slots(struct xenvif *vif)
249{
250 int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
251
252 if (vif->can_sg || vif->gso || vif->gso_prefix)
253 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
254
255 return max;
256}
257
258int xen_netbk_rx_ring_full(struct xenvif *vif)
259{
260 RING_IDX peek = vif->rx_req_cons_peek;
261 RING_IDX needed = max_required_rx_slots(vif);
262
263 return ((vif->rx.sring->req_prod - peek) < needed) ||
264 ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
265}
266
267int xen_netbk_must_stop_queue(struct xenvif *vif)
268{
269 if (!xen_netbk_rx_ring_full(vif))
270 return 0;
271
272 vif->rx.sring->req_event = vif->rx_req_cons_peek +
273 max_required_rx_slots(vif);
274 mb(); /* request notification /then/ check the queue */
275
276 return xen_netbk_rx_ring_full(vif);
277}
278
279/*
280 * Returns true if we should start a new receive buffer instead of
281 * adding 'size' bytes to a buffer which currently contains 'offset'
282 * bytes.
283 */
284static bool start_new_rx_buffer(int offset, unsigned long size, int head)
285{
286 /* simple case: we have completely filled the current buffer. */
287 if (offset == MAX_BUFFER_OFFSET)
288 return true;
289
290 /*
291 * complex case: start a fresh buffer if the current frag
292 * would overflow the current buffer but only if:
293 * (i) this frag would fit completely in the next buffer
294 * and (ii) there is already some data in the current buffer
295 * and (iii) this is not the head buffer.
296 *
297 * Where:
298 * - (i) stops us splitting a frag into two copies
299 * unless the frag is too large for a single buffer.
300 * - (ii) stops us from leaving a buffer pointlessly empty.
301 * - (iii) stops us leaving the first buffer
302 * empty. Strictly speaking this is already covered
303 * by (ii) but is explicitly checked because
304 * netfront relies on the first buffer being
305 * non-empty and can crash otherwise.
306 *
307 * This means we will effectively linearise small
308 * frags but do not needlessly split large buffers
309 * into multiple copies tend to give large frags their
310 * own buffers as before.
311 */
312 if ((offset + size > MAX_BUFFER_OFFSET) &&
313 (size <= MAX_BUFFER_OFFSET) && offset && !head)
314 return true;
315
316 return false;
317}
318
319/*
320 * Figure out how many ring slots we're going to need to send @skb to
321 * the guest. This function is essentially a dry run of
322 * netbk_gop_frag_copy.
323 */
324unsigned int xen_netbk_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
325{
326 unsigned int count;
327 int i, copy_off;
328
329 count = DIV_ROUND_UP(
330 offset_in_page(skb->data)+skb_headlen(skb), PAGE_SIZE);
331
332 copy_off = skb_headlen(skb) % PAGE_SIZE;
333
334 if (skb_shinfo(skb)->gso_size)
335 count++;
336
337 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000338 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000339 unsigned long bytes;
340 while (size > 0) {
341 BUG_ON(copy_off > MAX_BUFFER_OFFSET);
342
343 if (start_new_rx_buffer(copy_off, size, 0)) {
344 count++;
345 copy_off = 0;
346 }
347
348 bytes = size;
349 if (copy_off + bytes > MAX_BUFFER_OFFSET)
350 bytes = MAX_BUFFER_OFFSET - copy_off;
351
352 copy_off += bytes;
353 size -= bytes;
354 }
355 }
356 return count;
357}
358
359struct netrx_pending_operations {
360 unsigned copy_prod, copy_cons;
361 unsigned meta_prod, meta_cons;
362 struct gnttab_copy *copy;
363 struct netbk_rx_meta *meta;
364 int copy_off;
365 grant_ref_t copy_gref;
366};
367
368static struct netbk_rx_meta *get_next_rx_buffer(struct xenvif *vif,
369 struct netrx_pending_operations *npo)
370{
371 struct netbk_rx_meta *meta;
372 struct xen_netif_rx_request *req;
373
374 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
375
376 meta = npo->meta + npo->meta_prod++;
377 meta->gso_size = 0;
378 meta->size = 0;
379 meta->id = req->id;
380
381 npo->copy_off = 0;
382 npo->copy_gref = req->gref;
383
384 return meta;
385}
386
387/*
388 * Set up the grant operations for this fragment. If it's a flipping
389 * interface, we also set up the unmap request from here.
390 */
391static void netbk_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
392 struct netrx_pending_operations *npo,
393 struct page *page, unsigned long size,
394 unsigned long offset, int *head)
395{
396 struct gnttab_copy *copy_gop;
397 struct netbk_rx_meta *meta;
398 /*
Wei Liue34c0242011-12-06 02:04:50 +0000399 * These variables are used iff get_page_ext returns true,
Ian Campbellf942dc22011-03-15 00:06:18 +0000400 * in which case they are guaranteed to be initialized.
401 */
402 unsigned int uninitialized_var(group), uninitialized_var(idx);
403 int foreign = get_page_ext(page, &group, &idx);
404 unsigned long bytes;
405
406 /* Data must not cross a page boundary. */
407 BUG_ON(size + offset > PAGE_SIZE);
408
409 meta = npo->meta + npo->meta_prod - 1;
410
411 while (size > 0) {
412 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
413
414 if (start_new_rx_buffer(npo->copy_off, size, *head)) {
415 /*
416 * Netfront requires there to be some data in the head
417 * buffer.
418 */
419 BUG_ON(*head);
420
421 meta = get_next_rx_buffer(vif, npo);
422 }
423
424 bytes = size;
425 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
426 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
427
428 copy_gop = npo->copy + npo->copy_prod++;
429 copy_gop->flags = GNTCOPY_dest_gref;
430 if (foreign) {
431 struct xen_netbk *netbk = &xen_netbk[group];
432 struct pending_tx_info *src_pend;
433
434 src_pend = &netbk->pending_tx_info[idx];
435
436 copy_gop->source.domid = src_pend->vif->domid;
437 copy_gop->source.u.ref = src_pend->req.gref;
438 copy_gop->flags |= GNTCOPY_source_gref;
439 } else {
440 void *vaddr = page_address(page);
441 copy_gop->source.domid = DOMID_SELF;
442 copy_gop->source.u.gmfn = virt_to_mfn(vaddr);
443 }
444 copy_gop->source.offset = offset;
445 copy_gop->dest.domid = vif->domid;
446
447 copy_gop->dest.offset = npo->copy_off;
448 copy_gop->dest.u.ref = npo->copy_gref;
449 copy_gop->len = bytes;
450
451 npo->copy_off += bytes;
452 meta->size += bytes;
453
454 offset += bytes;
455 size -= bytes;
456
457 /* Leave a gap for the GSO descriptor. */
458 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
459 vif->rx.req_cons++;
460
461 *head = 0; /* There must be something in this buffer now. */
462
463 }
464}
465
466/*
467 * Prepare an SKB to be transmitted to the frontend.
468 *
469 * This function is responsible for allocating grant operations, meta
470 * structures, etc.
471 *
472 * It returns the number of meta structures consumed. The number of
473 * ring slots used is always equal to the number of meta slots used
474 * plus the number of GSO descriptors used. Currently, we use either
475 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
476 * frontend-side LRO).
477 */
478static int netbk_gop_skb(struct sk_buff *skb,
479 struct netrx_pending_operations *npo)
480{
481 struct xenvif *vif = netdev_priv(skb->dev);
482 int nr_frags = skb_shinfo(skb)->nr_frags;
483 int i;
484 struct xen_netif_rx_request *req;
485 struct netbk_rx_meta *meta;
486 unsigned char *data;
487 int head = 1;
488 int old_meta_prod;
489
490 old_meta_prod = npo->meta_prod;
491
492 /* Set up a GSO prefix descriptor, if necessary */
493 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
494 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
495 meta = npo->meta + npo->meta_prod++;
496 meta->gso_size = skb_shinfo(skb)->gso_size;
497 meta->size = 0;
498 meta->id = req->id;
499 }
500
501 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
502 meta = npo->meta + npo->meta_prod++;
503
504 if (!vif->gso_prefix)
505 meta->gso_size = skb_shinfo(skb)->gso_size;
506 else
507 meta->gso_size = 0;
508
509 meta->size = 0;
510 meta->id = req->id;
511 npo->copy_off = 0;
512 npo->copy_gref = req->gref;
513
514 data = skb->data;
515 while (data < skb_tail_pointer(skb)) {
516 unsigned int offset = offset_in_page(data);
517 unsigned int len = PAGE_SIZE - offset;
518
519 if (data + len > skb_tail_pointer(skb))
520 len = skb_tail_pointer(skb) - data;
521
522 netbk_gop_frag_copy(vif, skb, npo,
523 virt_to_page(data), len, offset, &head);
524 data += len;
525 }
526
527 for (i = 0; i < nr_frags; i++) {
528 netbk_gop_frag_copy(vif, skb, npo,
Ian Campbellea066ad2011-10-05 00:28:46 +0000529 skb_frag_page(&skb_shinfo(skb)->frags[i]),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000530 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Ian Campbellf942dc22011-03-15 00:06:18 +0000531 skb_shinfo(skb)->frags[i].page_offset,
532 &head);
533 }
534
535 return npo->meta_prod - old_meta_prod;
536}
537
538/*
539 * This is a twin to netbk_gop_skb. Assume that netbk_gop_skb was
540 * used to set up the operations on the top of
541 * netrx_pending_operations, which have since been done. Check that
542 * they didn't give any errors and advance over them.
543 */
544static int netbk_check_gop(struct xenvif *vif, int nr_meta_slots,
545 struct netrx_pending_operations *npo)
546{
547 struct gnttab_copy *copy_op;
548 int status = XEN_NETIF_RSP_OKAY;
549 int i;
550
551 for (i = 0; i < nr_meta_slots; i++) {
552 copy_op = npo->copy + npo->copy_cons++;
553 if (copy_op->status != GNTST_okay) {
554 netdev_dbg(vif->dev,
555 "Bad status %d from copy to DOM%d.\n",
556 copy_op->status, vif->domid);
557 status = XEN_NETIF_RSP_ERROR;
558 }
559 }
560
561 return status;
562}
563
564static void netbk_add_frag_responses(struct xenvif *vif, int status,
565 struct netbk_rx_meta *meta,
566 int nr_meta_slots)
567{
568 int i;
569 unsigned long offset;
570
571 /* No fragments used */
572 if (nr_meta_slots <= 1)
573 return;
574
575 nr_meta_slots--;
576
577 for (i = 0; i < nr_meta_slots; i++) {
578 int flags;
579 if (i == nr_meta_slots - 1)
580 flags = 0;
581 else
582 flags = XEN_NETRXF_more_data;
583
584 offset = 0;
585 make_rx_response(vif, meta[i].id, status, offset,
586 meta[i].size, flags);
587 }
588}
589
590struct skb_cb_overlay {
591 int meta_slots_used;
592};
593
594static void xen_netbk_rx_action(struct xen_netbk *netbk)
595{
596 struct xenvif *vif = NULL, *tmp;
597 s8 status;
598 u16 irq, flags;
599 struct xen_netif_rx_response *resp;
600 struct sk_buff_head rxq;
601 struct sk_buff *skb;
602 LIST_HEAD(notify);
603 int ret;
604 int nr_frags;
605 int count;
606 unsigned long offset;
607 struct skb_cb_overlay *sco;
608
609 struct netrx_pending_operations npo = {
610 .copy = netbk->grant_copy_op,
611 .meta = netbk->meta,
612 };
613
614 skb_queue_head_init(&rxq);
615
616 count = 0;
617
618 while ((skb = skb_dequeue(&netbk->rx_queue)) != NULL) {
619 vif = netdev_priv(skb->dev);
620 nr_frags = skb_shinfo(skb)->nr_frags;
621
622 sco = (struct skb_cb_overlay *)skb->cb;
623 sco->meta_slots_used = netbk_gop_skb(skb, &npo);
624
625 count += nr_frags + 1;
626
627 __skb_queue_tail(&rxq, skb);
628
629 /* Filled the batch queue? */
630 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
631 break;
632 }
633
634 BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
635
636 if (!npo.copy_prod)
637 return;
638
639 BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
640 ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, &netbk->grant_copy_op,
641 npo.copy_prod);
642 BUG_ON(ret != 0);
643
644 while ((skb = __skb_dequeue(&rxq)) != NULL) {
645 sco = (struct skb_cb_overlay *)skb->cb;
646
647 vif = netdev_priv(skb->dev);
648
649 if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
650 resp = RING_GET_RESPONSE(&vif->rx,
651 vif->rx.rsp_prod_pvt++);
652
653 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
654
655 resp->offset = netbk->meta[npo.meta_cons].gso_size;
656 resp->id = netbk->meta[npo.meta_cons].id;
657 resp->status = sco->meta_slots_used;
658
659 npo.meta_cons++;
660 sco->meta_slots_used--;
661 }
662
663
664 vif->dev->stats.tx_bytes += skb->len;
665 vif->dev->stats.tx_packets++;
666
667 status = netbk_check_gop(vif, sco->meta_slots_used, &npo);
668
669 if (sco->meta_slots_used == 1)
670 flags = 0;
671 else
672 flags = XEN_NETRXF_more_data;
673
674 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
675 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
676 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
677 /* remote but checksummed. */
678 flags |= XEN_NETRXF_data_validated;
679
680 offset = 0;
681 resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
682 status, offset,
683 netbk->meta[npo.meta_cons].size,
684 flags);
685
686 if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
687 struct xen_netif_extra_info *gso =
688 (struct xen_netif_extra_info *)
689 RING_GET_RESPONSE(&vif->rx,
690 vif->rx.rsp_prod_pvt++);
691
692 resp->flags |= XEN_NETRXF_extra_info;
693
694 gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
695 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
696 gso->u.gso.pad = 0;
697 gso->u.gso.features = 0;
698
699 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
700 gso->flags = 0;
701 }
702
703 netbk_add_frag_responses(vif, status,
704 netbk->meta + npo.meta_cons + 1,
705 sco->meta_slots_used);
706
707 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
708 irq = vif->irq;
709 if (ret && list_empty(&vif->notify_list))
710 list_add_tail(&vif->notify_list, &notify);
711
712 xenvif_notify_tx_completion(vif);
713
714 xenvif_put(vif);
715 npo.meta_cons += sco->meta_slots_used;
716 dev_kfree_skb(skb);
717 }
718
719 list_for_each_entry_safe(vif, tmp, &notify, notify_list) {
720 notify_remote_via_irq(vif->irq);
721 list_del_init(&vif->notify_list);
722 }
723
724 /* More work to do? */
725 if (!skb_queue_empty(&netbk->rx_queue) &&
726 !timer_pending(&netbk->net_timer))
727 xen_netbk_kick_thread(netbk);
728}
729
730void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
731{
732 struct xen_netbk *netbk = vif->netbk;
733
734 skb_queue_tail(&netbk->rx_queue, skb);
735
736 xen_netbk_kick_thread(netbk);
737}
738
739static void xen_netbk_alarm(unsigned long data)
740{
741 struct xen_netbk *netbk = (struct xen_netbk *)data;
742 xen_netbk_kick_thread(netbk);
743}
744
745static int __on_net_schedule_list(struct xenvif *vif)
746{
747 return !list_empty(&vif->schedule_list);
748}
749
750/* Must be called with net_schedule_list_lock held */
751static void remove_from_net_schedule_list(struct xenvif *vif)
752{
753 if (likely(__on_net_schedule_list(vif))) {
754 list_del_init(&vif->schedule_list);
755 xenvif_put(vif);
756 }
757}
758
759static struct xenvif *poll_net_schedule_list(struct xen_netbk *netbk)
760{
761 struct xenvif *vif = NULL;
762
763 spin_lock_irq(&netbk->net_schedule_list_lock);
764 if (list_empty(&netbk->net_schedule_list))
765 goto out;
766
767 vif = list_first_entry(&netbk->net_schedule_list,
768 struct xenvif, schedule_list);
769 if (!vif)
770 goto out;
771
772 xenvif_get(vif);
773
774 remove_from_net_schedule_list(vif);
775out:
776 spin_unlock_irq(&netbk->net_schedule_list_lock);
777 return vif;
778}
779
780void xen_netbk_schedule_xenvif(struct xenvif *vif)
781{
782 unsigned long flags;
783 struct xen_netbk *netbk = vif->netbk;
784
785 if (__on_net_schedule_list(vif))
786 goto kick;
787
788 spin_lock_irqsave(&netbk->net_schedule_list_lock, flags);
789 if (!__on_net_schedule_list(vif) &&
790 likely(xenvif_schedulable(vif))) {
791 list_add_tail(&vif->schedule_list, &netbk->net_schedule_list);
792 xenvif_get(vif);
793 }
794 spin_unlock_irqrestore(&netbk->net_schedule_list_lock, flags);
795
796kick:
797 smp_mb();
798 if ((nr_pending_reqs(netbk) < (MAX_PENDING_REQS/2)) &&
799 !list_empty(&netbk->net_schedule_list))
800 xen_netbk_kick_thread(netbk);
801}
802
803void xen_netbk_deschedule_xenvif(struct xenvif *vif)
804{
805 struct xen_netbk *netbk = vif->netbk;
806 spin_lock_irq(&netbk->net_schedule_list_lock);
807 remove_from_net_schedule_list(vif);
808 spin_unlock_irq(&netbk->net_schedule_list_lock);
809}
810
811void xen_netbk_check_rx_xenvif(struct xenvif *vif)
812{
813 int more_to_do;
814
815 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
816
817 if (more_to_do)
818 xen_netbk_schedule_xenvif(vif);
819}
820
821static void tx_add_credit(struct xenvif *vif)
822{
823 unsigned long max_burst, max_credit;
824
825 /*
826 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
827 * Otherwise the interface can seize up due to insufficient credit.
828 */
829 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
830 max_burst = min(max_burst, 131072UL);
831 max_burst = max(max_burst, vif->credit_bytes);
832
833 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
834 max_credit = vif->remaining_credit + vif->credit_bytes;
835 if (max_credit < vif->remaining_credit)
836 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
837
838 vif->remaining_credit = min(max_credit, max_burst);
839}
840
841static void tx_credit_callback(unsigned long data)
842{
843 struct xenvif *vif = (struct xenvif *)data;
844 tx_add_credit(vif);
845 xen_netbk_check_rx_xenvif(vif);
846}
847
848static void netbk_tx_err(struct xenvif *vif,
849 struct xen_netif_tx_request *txp, RING_IDX end)
850{
851 RING_IDX cons = vif->tx.req_cons;
852
853 do {
854 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Ian Campbell42671f12013-02-06 23:41:38 +0000855 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000856 break;
857 txp = RING_GET_REQUEST(&vif->tx, cons++);
858 } while (1);
859 vif->tx.req_cons = cons;
860 xen_netbk_check_rx_xenvif(vif);
861 xenvif_put(vif);
862}
863
Ian Campbellbe7254f2013-02-06 23:41:35 +0000864static void netbk_fatal_tx_err(struct xenvif *vif)
865{
866 netdev_err(vif->dev, "fatal error; disabling device\n");
867 xenvif_carrier_off(vif);
868 xenvif_put(vif);
869}
870
Ian Campbellf942dc22011-03-15 00:06:18 +0000871static int netbk_count_requests(struct xenvif *vif,
872 struct xen_netif_tx_request *first,
873 struct xen_netif_tx_request *txp,
874 int work_to_do)
875{
876 RING_IDX cons = vif->tx.req_cons;
877 int frags = 0;
878
879 if (!(first->flags & XEN_NETTXF_more_data))
880 return 0;
881
882 do {
883 if (frags >= work_to_do) {
Ian Campbellbe7254f2013-02-06 23:41:35 +0000884 netdev_err(vif->dev, "Need more frags\n");
885 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000886 return -frags;
887 }
888
889 if (unlikely(frags >= MAX_SKB_FRAGS)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +0000890 netdev_err(vif->dev, "Too many frags\n");
891 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000892 return -frags;
893 }
894
895 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + frags),
896 sizeof(*txp));
897 if (txp->size > first->size) {
Ian Campbellbe7254f2013-02-06 23:41:35 +0000898 netdev_err(vif->dev, "Frag is bigger than frame.\n");
899 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000900 return -frags;
901 }
902
903 first->size -= txp->size;
904 frags++;
905
906 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +0000907 netdev_err(vif->dev, "txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000908 txp->offset, txp->size);
Ian Campbellbe7254f2013-02-06 23:41:35 +0000909 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000910 return -frags;
911 }
912 } while ((txp++)->flags & XEN_NETTXF_more_data);
913 return frags;
914}
915
916static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
917 struct sk_buff *skb,
Ian Campbellea066ad2011-10-05 00:28:46 +0000918 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000919{
920 struct page *page;
921 page = alloc_page(GFP_KERNEL|__GFP_COLD);
922 if (!page)
923 return NULL;
924 set_page_ext(page, netbk, pending_idx);
925 netbk->mmap_pages[pending_idx] = page;
926 return page;
927}
928
929static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
930 struct xenvif *vif,
931 struct sk_buff *skb,
932 struct xen_netif_tx_request *txp,
933 struct gnttab_copy *gop)
934{
935 struct skb_shared_info *shinfo = skb_shinfo(skb);
936 skb_frag_t *frags = shinfo->frags;
Ian Campbellea066ad2011-10-05 00:28:46 +0000937 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000938 int i, start;
939
940 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000941 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000942
943 for (i = start; i < shinfo->nr_frags; i++, txp++) {
944 struct page *page;
945 pending_ring_idx_t index;
946 struct pending_tx_info *pending_tx_info =
947 netbk->pending_tx_info;
948
949 index = pending_index(netbk->pending_cons++);
950 pending_idx = netbk->pending_ring[index];
951 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
952 if (!page)
Ian Campbell90ffc8f2013-02-06 23:41:37 +0000953 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000954
Ian Campbellf942dc22011-03-15 00:06:18 +0000955 gop->source.u.ref = txp->gref;
956 gop->source.domid = vif->domid;
957 gop->source.offset = txp->offset;
958
959 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
960 gop->dest.domid = DOMID_SELF;
961 gop->dest.offset = txp->offset;
962
963 gop->len = txp->size;
964 gop->flags = GNTCOPY_source_gref;
965
966 gop++;
967
968 memcpy(&pending_tx_info[pending_idx].req, txp, sizeof(*txp));
969 xenvif_get(vif);
970 pending_tx_info[pending_idx].vif = vif;
Ian Campbellea066ad2011-10-05 00:28:46 +0000971 frag_set_pending_idx(&frags[i], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000972 }
973
974 return gop;
Ian Campbell90ffc8f2013-02-06 23:41:37 +0000975err:
976 /* Unwind, freeing all pages and sending error responses. */
977 while (i-- > start) {
978 xen_netbk_idx_release(netbk, frag_get_pending_idx(&frags[i]),
979 XEN_NETIF_RSP_ERROR);
980 }
981 /* The head too, if necessary. */
982 if (start)
983 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
984
985 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000986}
987
988static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
989 struct sk_buff *skb,
990 struct gnttab_copy **gopp)
991{
992 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000993 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000994 struct skb_shared_info *shinfo = skb_shinfo(skb);
995 int nr_frags = shinfo->nr_frags;
996 int i, err, start;
997
998 /* Check status of header. */
999 err = gop->status;
Matthew Daley33eb2602013-02-06 23:41:36 +00001000 if (unlikely(err))
1001 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001002
1003 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +00001004 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001005
1006 for (i = start; i < nr_frags; i++) {
1007 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001008
Ian Campbellea066ad2011-10-05 00:28:46 +00001009 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001010
1011 /* Check error status: if okay then remember grant handle. */
1012 newerr = (++gop)->status;
1013 if (likely(!newerr)) {
1014 /* Had a previous error? Invalidate this fragment. */
1015 if (unlikely(err))
Matthew Daley33eb2602013-02-06 23:41:36 +00001016 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001017 continue;
1018 }
1019
1020 /* Error on this fragment: respond to client with an error. */
Matthew Daley33eb2602013-02-06 23:41:36 +00001021 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001022
1023 /* Not the first error? Preceding frags already invalidated. */
1024 if (err)
1025 continue;
1026
1027 /* First error: invalidate header and preceding fragments. */
1028 pending_idx = *((u16 *)skb->data);
Matthew Daley33eb2602013-02-06 23:41:36 +00001029 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001030 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001031 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Matthew Daley33eb2602013-02-06 23:41:36 +00001032 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001033 }
1034
1035 /* Remember the error: invalidate all subsequent fragments. */
1036 err = newerr;
1037 }
1038
1039 *gopp = gop + 1;
1040 return err;
1041}
1042
1043static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
1044{
1045 struct skb_shared_info *shinfo = skb_shinfo(skb);
1046 int nr_frags = shinfo->nr_frags;
1047 int i;
1048
1049 for (i = 0; i < nr_frags; i++) {
1050 skb_frag_t *frag = shinfo->frags + i;
1051 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001052 struct page *page;
1053 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001054
Ian Campbellea066ad2011-10-05 00:28:46 +00001055 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001056
1057 txp = &netbk->pending_tx_info[pending_idx].req;
Ian Campbellea066ad2011-10-05 00:28:46 +00001058 page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
1059 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001060 skb->len += txp->size;
1061 skb->data_len += txp->size;
1062 skb->truesize += txp->size;
1063
1064 /* Take an extra reference to offset xen_netbk_idx_release */
1065 get_page(netbk->mmap_pages[pending_idx]);
Matthew Daley33eb2602013-02-06 23:41:36 +00001066 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001067 }
1068}
1069
1070static int xen_netbk_get_extras(struct xenvif *vif,
1071 struct xen_netif_extra_info *extras,
1072 int work_to_do)
1073{
1074 struct xen_netif_extra_info extra;
1075 RING_IDX cons = vif->tx.req_cons;
1076
1077 do {
1078 if (unlikely(work_to_do-- <= 0)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001079 netdev_err(vif->dev, "Missing extra info\n");
1080 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001081 return -EBADR;
1082 }
1083
1084 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1085 sizeof(extra));
1086 if (unlikely(!extra.type ||
1087 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1088 vif->tx.req_cons = ++cons;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001089 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001090 "Invalid extra type: %d\n", extra.type);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001091 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001092 return -EINVAL;
1093 }
1094
1095 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1096 vif->tx.req_cons = ++cons;
1097 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1098
1099 return work_to_do;
1100}
1101
1102static int netbk_set_skb_gso(struct xenvif *vif,
1103 struct sk_buff *skb,
1104 struct xen_netif_extra_info *gso)
1105{
1106 if (!gso->u.gso.size) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001107 netdev_err(vif->dev, "GSO size must not be zero.\n");
1108 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001109 return -EINVAL;
1110 }
1111
1112 /* Currently only TCPv4 S.O. is supported. */
1113 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001114 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1115 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001116 return -EINVAL;
1117 }
1118
1119 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1120 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1121
1122 /* Header must be checked, and gso_segs computed. */
1123 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1124 skb_shinfo(skb)->gso_segs = 0;
1125
1126 return 0;
1127}
1128
1129static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1130{
1131 struct iphdr *iph;
1132 unsigned char *th;
1133 int err = -EPROTO;
1134 int recalculate_partial_csum = 0;
1135
1136 /*
1137 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1138 * peers can fail to set NETRXF_csum_blank when sending a GSO
1139 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1140 * recalculate the partial checksum.
1141 */
1142 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1143 vif->rx_gso_checksum_fixup++;
1144 skb->ip_summed = CHECKSUM_PARTIAL;
1145 recalculate_partial_csum = 1;
1146 }
1147
1148 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1149 if (skb->ip_summed != CHECKSUM_PARTIAL)
1150 return 0;
1151
1152 if (skb->protocol != htons(ETH_P_IP))
1153 goto out;
1154
1155 iph = (void *)skb->data;
1156 th = skb->data + 4 * iph->ihl;
1157 if (th >= skb_tail_pointer(skb))
1158 goto out;
1159
1160 skb->csum_start = th - skb->head;
1161 switch (iph->protocol) {
1162 case IPPROTO_TCP:
1163 skb->csum_offset = offsetof(struct tcphdr, check);
1164
1165 if (recalculate_partial_csum) {
1166 struct tcphdr *tcph = (struct tcphdr *)th;
1167 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1168 skb->len - iph->ihl*4,
1169 IPPROTO_TCP, 0);
1170 }
1171 break;
1172 case IPPROTO_UDP:
1173 skb->csum_offset = offsetof(struct udphdr, check);
1174
1175 if (recalculate_partial_csum) {
1176 struct udphdr *udph = (struct udphdr *)th;
1177 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1178 skb->len - iph->ihl*4,
1179 IPPROTO_UDP, 0);
1180 }
1181 break;
1182 default:
1183 if (net_ratelimit())
1184 netdev_err(vif->dev,
1185 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1186 iph->protocol);
1187 goto out;
1188 }
1189
1190 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
1191 goto out;
1192
1193 err = 0;
1194
1195out:
1196 return err;
1197}
1198
1199static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1200{
1201 unsigned long now = jiffies;
1202 unsigned long next_credit =
1203 vif->credit_timeout.expires +
1204 msecs_to_jiffies(vif->credit_usec / 1000);
1205
1206 /* Timer could already be pending in rare cases. */
1207 if (timer_pending(&vif->credit_timeout))
1208 return true;
1209
1210 /* Passed the point where we can replenish credit? */
1211 if (time_after_eq(now, next_credit)) {
1212 vif->credit_timeout.expires = now;
1213 tx_add_credit(vif);
1214 }
1215
1216 /* Still too big to send right now? Set a callback. */
1217 if (size > vif->remaining_credit) {
1218 vif->credit_timeout.data =
1219 (unsigned long)vif;
1220 vif->credit_timeout.function =
1221 tx_credit_callback;
1222 mod_timer(&vif->credit_timeout,
1223 next_credit);
1224
1225 return true;
1226 }
1227
1228 return false;
1229}
1230
1231static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
1232{
1233 struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
1234 struct sk_buff *skb;
1235 int ret;
1236
1237 while (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1238 !list_empty(&netbk->net_schedule_list)) {
1239 struct xenvif *vif;
1240 struct xen_netif_tx_request txreq;
1241 struct xen_netif_tx_request txfrags[MAX_SKB_FRAGS];
1242 struct page *page;
1243 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1244 u16 pending_idx;
1245 RING_IDX idx;
1246 int work_to_do;
1247 unsigned int data_len;
1248 pending_ring_idx_t index;
1249
1250 /* Get a netif from the list with work to do. */
1251 vif = poll_net_schedule_list(netbk);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001252 /* This can sometimes happen because the test of
1253 * list_empty(net_schedule_list) at the top of the
1254 * loop is unlocked. Just go back and have another
1255 * look.
1256 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001257 if (!vif)
1258 continue;
1259
Ian Campbellbe7254f2013-02-06 23:41:35 +00001260 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1261 XEN_NETIF_TX_RING_SIZE) {
1262 netdev_err(vif->dev,
1263 "Impossible number of requests. "
1264 "req_prod %d, req_cons %d, size %ld\n",
1265 vif->tx.sring->req_prod, vif->tx.req_cons,
1266 XEN_NETIF_TX_RING_SIZE);
1267 netbk_fatal_tx_err(vif);
1268 continue;
1269 }
1270
Ian Campbellf942dc22011-03-15 00:06:18 +00001271 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1272 if (!work_to_do) {
1273 xenvif_put(vif);
1274 continue;
1275 }
1276
1277 idx = vif->tx.req_cons;
1278 rmb(); /* Ensure that we see the request before we copy it. */
1279 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1280
1281 /* Credit-based scheduling. */
1282 if (txreq.size > vif->remaining_credit &&
1283 tx_credit_exceeded(vif, txreq.size)) {
1284 xenvif_put(vif);
1285 continue;
1286 }
1287
1288 vif->remaining_credit -= txreq.size;
1289
1290 work_to_do--;
1291 vif->tx.req_cons = ++idx;
1292
1293 memset(extras, 0, sizeof(extras));
1294 if (txreq.flags & XEN_NETTXF_extra_info) {
1295 work_to_do = xen_netbk_get_extras(vif, extras,
1296 work_to_do);
1297 idx = vif->tx.req_cons;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001298 if (unlikely(work_to_do < 0))
Ian Campbellf942dc22011-03-15 00:06:18 +00001299 continue;
Ian Campbellf942dc22011-03-15 00:06:18 +00001300 }
1301
1302 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001303 if (unlikely(ret < 0))
Ian Campbellf942dc22011-03-15 00:06:18 +00001304 continue;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001305
Ian Campbellf942dc22011-03-15 00:06:18 +00001306 idx += ret;
1307
1308 if (unlikely(txreq.size < ETH_HLEN)) {
1309 netdev_dbg(vif->dev,
1310 "Bad packet size: %d\n", txreq.size);
1311 netbk_tx_err(vif, &txreq, idx);
1312 continue;
1313 }
1314
1315 /* No crossing a page as the payload mustn't fragment. */
1316 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001317 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001318 "txreq.offset: %x, size: %u, end: %lu\n",
1319 txreq.offset, txreq.size,
1320 (txreq.offset&~PAGE_MASK) + txreq.size);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001321 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001322 continue;
1323 }
1324
1325 index = pending_index(netbk->pending_cons);
1326 pending_idx = netbk->pending_ring[index];
1327
1328 data_len = (txreq.size > PKT_PROT_LEN &&
1329 ret < MAX_SKB_FRAGS) ?
1330 PKT_PROT_LEN : txreq.size;
1331
1332 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1333 GFP_ATOMIC | __GFP_NOWARN);
1334 if (unlikely(skb == NULL)) {
1335 netdev_dbg(vif->dev,
1336 "Can't allocate a skb in start_xmit.\n");
1337 netbk_tx_err(vif, &txreq, idx);
1338 break;
1339 }
1340
1341 /* Packets passed to netif_rx() must have some headroom. */
1342 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1343
1344 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1345 struct xen_netif_extra_info *gso;
1346 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1347
1348 if (netbk_set_skb_gso(vif, skb, gso)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001349 /* Failure in netbk_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001350 kfree_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001351 continue;
1352 }
1353 }
1354
1355 /* XXX could copy straight to head */
1356 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
1357 if (!page) {
1358 kfree_skb(skb);
1359 netbk_tx_err(vif, &txreq, idx);
1360 continue;
1361 }
1362
Ian Campbellf942dc22011-03-15 00:06:18 +00001363 gop->source.u.ref = txreq.gref;
1364 gop->source.domid = vif->domid;
1365 gop->source.offset = txreq.offset;
1366
1367 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1368 gop->dest.domid = DOMID_SELF;
1369 gop->dest.offset = txreq.offset;
1370
1371 gop->len = txreq.size;
1372 gop->flags = GNTCOPY_source_gref;
1373
1374 gop++;
1375
1376 memcpy(&netbk->pending_tx_info[pending_idx].req,
1377 &txreq, sizeof(txreq));
1378 netbk->pending_tx_info[pending_idx].vif = vif;
1379 *((u16 *)skb->data) = pending_idx;
1380
1381 __skb_put(skb, data_len);
1382
1383 skb_shinfo(skb)->nr_frags = ret;
1384 if (data_len < txreq.size) {
1385 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001386 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1387 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001388 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001389 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1390 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001391 }
1392
1393 __skb_queue_tail(&netbk->tx_queue, skb);
1394
1395 netbk->pending_cons++;
1396
1397 request_gop = xen_netbk_get_requests(netbk, vif,
1398 skb, txfrags, gop);
1399 if (request_gop == NULL) {
1400 kfree_skb(skb);
1401 netbk_tx_err(vif, &txreq, idx);
1402 continue;
1403 }
1404 gop = request_gop;
1405
1406 vif->tx.req_cons = idx;
1407 xen_netbk_check_rx_xenvif(vif);
1408
1409 if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
1410 break;
1411 }
1412
1413 return gop - netbk->tx_copy_ops;
1414}
1415
1416static void xen_netbk_tx_submit(struct xen_netbk *netbk)
1417{
1418 struct gnttab_copy *gop = netbk->tx_copy_ops;
1419 struct sk_buff *skb;
1420
1421 while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
1422 struct xen_netif_tx_request *txp;
1423 struct xenvif *vif;
1424 u16 pending_idx;
1425 unsigned data_len;
1426
1427 pending_idx = *((u16 *)skb->data);
1428 vif = netbk->pending_tx_info[pending_idx].vif;
1429 txp = &netbk->pending_tx_info[pending_idx].req;
1430
1431 /* Check the remap error code. */
1432 if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1433 netdev_dbg(vif->dev, "netback grant failed.\n");
1434 skb_shinfo(skb)->nr_frags = 0;
1435 kfree_skb(skb);
1436 continue;
1437 }
1438
1439 data_len = skb->len;
1440 memcpy(skb->data,
1441 (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
1442 data_len);
1443 if (data_len < txp->size) {
1444 /* Append the packet payload as a fragment. */
1445 txp->offset += data_len;
1446 txp->size -= data_len;
1447 } else {
1448 /* Schedule a response immediately. */
Matthew Daley33eb2602013-02-06 23:41:36 +00001449 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001450 }
1451
1452 if (txp->flags & XEN_NETTXF_csum_blank)
1453 skb->ip_summed = CHECKSUM_PARTIAL;
1454 else if (txp->flags & XEN_NETTXF_data_validated)
1455 skb->ip_summed = CHECKSUM_UNNECESSARY;
1456
1457 xen_netbk_fill_frags(netbk, skb);
1458
1459 /*
1460 * If the initial fragment was < PKT_PROT_LEN then
1461 * pull through some bytes from the other fragments to
1462 * increase the linear region to PKT_PROT_LEN bytes.
1463 */
1464 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1465 int target = min_t(int, skb->len, PKT_PROT_LEN);
1466 __pskb_pull_tail(skb, target - skb_headlen(skb));
1467 }
1468
1469 skb->dev = vif->dev;
1470 skb->protocol = eth_type_trans(skb, skb->dev);
1471
1472 if (checksum_setup(vif, skb)) {
1473 netdev_dbg(vif->dev,
1474 "Can't setup checksum in net_tx_action\n");
1475 kfree_skb(skb);
1476 continue;
1477 }
1478
1479 vif->dev->stats.rx_bytes += skb->len;
1480 vif->dev->stats.rx_packets++;
1481
1482 xenvif_receive_skb(vif, skb);
1483 }
1484}
1485
1486/* Called after netfront has transmitted */
1487static void xen_netbk_tx_action(struct xen_netbk *netbk)
1488{
1489 unsigned nr_gops;
1490 int ret;
1491
1492 nr_gops = xen_netbk_tx_build_gops(netbk);
1493
1494 if (nr_gops == 0)
1495 return;
1496 ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1497 netbk->tx_copy_ops, nr_gops);
1498 BUG_ON(ret);
1499
1500 xen_netbk_tx_submit(netbk);
1501
1502}
1503
Matthew Daley33eb2602013-02-06 23:41:36 +00001504static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
1505 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001506{
1507 struct xenvif *vif;
1508 struct pending_tx_info *pending_tx_info;
1509 pending_ring_idx_t index;
1510
1511 /* Already complete? */
1512 if (netbk->mmap_pages[pending_idx] == NULL)
1513 return;
1514
1515 pending_tx_info = &netbk->pending_tx_info[pending_idx];
1516
1517 vif = pending_tx_info->vif;
1518
Matthew Daley33eb2602013-02-06 23:41:36 +00001519 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001520
1521 index = pending_index(netbk->pending_prod++);
1522 netbk->pending_ring[index] = pending_idx;
1523
1524 xenvif_put(vif);
1525
1526 netbk->mmap_pages[pending_idx]->mapping = 0;
1527 put_page(netbk->mmap_pages[pending_idx]);
1528 netbk->mmap_pages[pending_idx] = NULL;
1529}
1530
1531static void make_tx_response(struct xenvif *vif,
1532 struct xen_netif_tx_request *txp,
1533 s8 st)
1534{
1535 RING_IDX i = vif->tx.rsp_prod_pvt;
1536 struct xen_netif_tx_response *resp;
1537 int notify;
1538
1539 resp = RING_GET_RESPONSE(&vif->tx, i);
1540 resp->id = txp->id;
1541 resp->status = st;
1542
1543 if (txp->flags & XEN_NETTXF_extra_info)
1544 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1545
1546 vif->tx.rsp_prod_pvt = ++i;
1547 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1548 if (notify)
1549 notify_remote_via_irq(vif->irq);
1550}
1551
1552static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1553 u16 id,
1554 s8 st,
1555 u16 offset,
1556 u16 size,
1557 u16 flags)
1558{
1559 RING_IDX i = vif->rx.rsp_prod_pvt;
1560 struct xen_netif_rx_response *resp;
1561
1562 resp = RING_GET_RESPONSE(&vif->rx, i);
1563 resp->offset = offset;
1564 resp->flags = flags;
1565 resp->id = id;
1566 resp->status = (s16)size;
1567 if (st < 0)
1568 resp->status = (s16)st;
1569
1570 vif->rx.rsp_prod_pvt = ++i;
1571
1572 return resp;
1573}
1574
1575static inline int rx_work_todo(struct xen_netbk *netbk)
1576{
1577 return !skb_queue_empty(&netbk->rx_queue);
1578}
1579
1580static inline int tx_work_todo(struct xen_netbk *netbk)
1581{
1582
1583 if (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1584 !list_empty(&netbk->net_schedule_list))
1585 return 1;
1586
1587 return 0;
1588}
1589
1590static int xen_netbk_kthread(void *data)
1591{
1592 struct xen_netbk *netbk = data;
1593 while (!kthread_should_stop()) {
1594 wait_event_interruptible(netbk->wq,
1595 rx_work_todo(netbk) ||
1596 tx_work_todo(netbk) ||
1597 kthread_should_stop());
1598 cond_resched();
1599
1600 if (kthread_should_stop())
1601 break;
1602
1603 if (rx_work_todo(netbk))
1604 xen_netbk_rx_action(netbk);
1605
1606 if (tx_work_todo(netbk))
1607 xen_netbk_tx_action(netbk);
1608 }
1609
1610 return 0;
1611}
1612
1613void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
1614{
David Vrabelc9d63692011-09-29 16:53:31 +01001615 if (vif->tx.sring)
1616 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1617 vif->tx.sring);
1618 if (vif->rx.sring)
1619 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1620 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001621}
1622
1623int xen_netbk_map_frontend_rings(struct xenvif *vif,
1624 grant_ref_t tx_ring_ref,
1625 grant_ref_t rx_ring_ref)
1626{
David Vrabelc9d63692011-09-29 16:53:31 +01001627 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001628 struct xen_netif_tx_sring *txs;
1629 struct xen_netif_rx_sring *rxs;
1630
1631 int err = -ENOMEM;
1632
David Vrabelc9d63692011-09-29 16:53:31 +01001633 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1634 tx_ring_ref, &addr);
1635 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001636 goto err;
1637
David Vrabelc9d63692011-09-29 16:53:31 +01001638 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001639 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1640
David Vrabelc9d63692011-09-29 16:53:31 +01001641 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1642 rx_ring_ref, &addr);
1643 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001644 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001645
David Vrabelc9d63692011-09-29 16:53:31 +01001646 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001647 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1648
David Vrabelc9d63692011-09-29 16:53:31 +01001649 vif->rx_req_cons_peek = 0;
1650
Ian Campbellf942dc22011-03-15 00:06:18 +00001651 return 0;
1652
1653err:
1654 xen_netbk_unmap_frontend_rings(vif);
1655 return err;
1656}
1657
1658static int __init netback_init(void)
1659{
1660 int i;
1661 int rc = 0;
1662 int group;
1663
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001664 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001665 return -ENODEV;
1666
1667 xen_netbk_group_nr = num_online_cpus();
1668 xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
Joe Perchese404dec2012-01-29 12:56:23 +00001669 if (!xen_netbk)
Ian Campbellf942dc22011-03-15 00:06:18 +00001670 return -ENOMEM;
Ian Campbellf942dc22011-03-15 00:06:18 +00001671
1672 for (group = 0; group < xen_netbk_group_nr; group++) {
1673 struct xen_netbk *netbk = &xen_netbk[group];
1674 skb_queue_head_init(&netbk->rx_queue);
1675 skb_queue_head_init(&netbk->tx_queue);
1676
1677 init_timer(&netbk->net_timer);
1678 netbk->net_timer.data = (unsigned long)netbk;
1679 netbk->net_timer.function = xen_netbk_alarm;
1680
1681 netbk->pending_cons = 0;
1682 netbk->pending_prod = MAX_PENDING_REQS;
1683 for (i = 0; i < MAX_PENDING_REQS; i++)
1684 netbk->pending_ring[i] = i;
1685
1686 init_waitqueue_head(&netbk->wq);
1687 netbk->task = kthread_create(xen_netbk_kthread,
1688 (void *)netbk,
1689 "netback/%u", group);
1690
1691 if (IS_ERR(netbk->task)) {
Wei Liu6b84bd12011-12-05 06:57:44 +00001692 printk(KERN_ALERT "kthread_create() fails at netback\n");
Ian Campbellf942dc22011-03-15 00:06:18 +00001693 del_timer(&netbk->net_timer);
1694 rc = PTR_ERR(netbk->task);
1695 goto failed_init;
1696 }
1697
1698 kthread_bind(netbk->task, group);
1699
1700 INIT_LIST_HEAD(&netbk->net_schedule_list);
1701
1702 spin_lock_init(&netbk->net_schedule_list_lock);
1703
1704 atomic_set(&netbk->netfront_count, 0);
1705
1706 wake_up_process(netbk->task);
1707 }
1708
1709 rc = xenvif_xenbus_init();
1710 if (rc)
1711 goto failed_init;
1712
1713 return 0;
1714
1715failed_init:
1716 while (--group >= 0) {
1717 struct xen_netbk *netbk = &xen_netbk[group];
1718 for (i = 0; i < MAX_PENDING_REQS; i++) {
1719 if (netbk->mmap_pages[i])
1720 __free_page(netbk->mmap_pages[i]);
1721 }
1722 del_timer(&netbk->net_timer);
1723 kthread_stop(netbk->task);
1724 }
1725 vfree(xen_netbk);
1726 return rc;
1727
1728}
1729
1730module_init(netback_init);
1731
1732MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001733MODULE_ALIAS("xen-backend:vif");