blob: d8ffbf84e3dfb58cf28038337753c288e1f32132 [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);
David Vrabel320c1502013-02-14 03:18:57 +0000886 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000887 }
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);
David Vrabel320c1502013-02-14 03:18:57 +0000892 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000893 }
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);
David Vrabel320c1502013-02-14 03:18:57 +0000900 return -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000901 }
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);
David Vrabel320c1502013-02-14 03:18:57 +0000910 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000911 }
912 } while ((txp++)->flags & XEN_NETTXF_more_data);
913 return frags;
914}
915
916static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
Ian Campbellea066ad2011-10-05 00:28:46 +0000917 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000918{
919 struct page *page;
920 page = alloc_page(GFP_KERNEL|__GFP_COLD);
921 if (!page)
922 return NULL;
923 set_page_ext(page, netbk, pending_idx);
924 netbk->mmap_pages[pending_idx] = page;
925 return page;
926}
927
928static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
929 struct xenvif *vif,
930 struct sk_buff *skb,
931 struct xen_netif_tx_request *txp,
932 struct gnttab_copy *gop)
933{
934 struct skb_shared_info *shinfo = skb_shinfo(skb);
935 skb_frag_t *frags = shinfo->frags;
Ian Campbellea066ad2011-10-05 00:28:46 +0000936 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000937 int i, start;
938
939 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000940 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000941
942 for (i = start; i < shinfo->nr_frags; i++, txp++) {
943 struct page *page;
944 pending_ring_idx_t index;
945 struct pending_tx_info *pending_tx_info =
946 netbk->pending_tx_info;
947
948 index = pending_index(netbk->pending_cons++);
949 pending_idx = netbk->pending_ring[index];
Wei Liubaff3c82013-03-25 01:08:20 +0000950 page = xen_netbk_alloc_page(netbk, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000951 if (!page)
Ian Campbell90ffc8f2013-02-06 23:41:37 +0000952 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000953
Ian Campbellf942dc22011-03-15 00:06:18 +0000954 gop->source.u.ref = txp->gref;
955 gop->source.domid = vif->domid;
956 gop->source.offset = txp->offset;
957
958 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
959 gop->dest.domid = DOMID_SELF;
960 gop->dest.offset = txp->offset;
961
962 gop->len = txp->size;
963 gop->flags = GNTCOPY_source_gref;
964
965 gop++;
966
967 memcpy(&pending_tx_info[pending_idx].req, txp, sizeof(*txp));
968 xenvif_get(vif);
969 pending_tx_info[pending_idx].vif = vif;
Ian Campbellea066ad2011-10-05 00:28:46 +0000970 frag_set_pending_idx(&frags[i], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000971 }
972
973 return gop;
Ian Campbell90ffc8f2013-02-06 23:41:37 +0000974err:
975 /* Unwind, freeing all pages and sending error responses. */
976 while (i-- > start) {
977 xen_netbk_idx_release(netbk, frag_get_pending_idx(&frags[i]),
978 XEN_NETIF_RSP_ERROR);
979 }
980 /* The head too, if necessary. */
981 if (start)
982 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
983
984 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000985}
986
987static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
988 struct sk_buff *skb,
989 struct gnttab_copy **gopp)
990{
991 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000992 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000993 struct skb_shared_info *shinfo = skb_shinfo(skb);
994 int nr_frags = shinfo->nr_frags;
995 int i, err, start;
996
997 /* Check status of header. */
998 err = gop->status;
Matthew Daley33eb2602013-02-06 23:41:36 +0000999 if (unlikely(err))
1000 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001001
1002 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +00001003 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001004
1005 for (i = start; i < nr_frags; i++) {
1006 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001007
Ian Campbellea066ad2011-10-05 00:28:46 +00001008 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001009
1010 /* Check error status: if okay then remember grant handle. */
1011 newerr = (++gop)->status;
1012 if (likely(!newerr)) {
1013 /* Had a previous error? Invalidate this fragment. */
1014 if (unlikely(err))
Matthew Daley33eb2602013-02-06 23:41:36 +00001015 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001016 continue;
1017 }
1018
1019 /* Error on this fragment: respond to client with an error. */
Matthew Daley33eb2602013-02-06 23:41:36 +00001020 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001021
1022 /* Not the first error? Preceding frags already invalidated. */
1023 if (err)
1024 continue;
1025
1026 /* First error: invalidate header and preceding fragments. */
1027 pending_idx = *((u16 *)skb->data);
Matthew Daley33eb2602013-02-06 23:41:36 +00001028 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001029 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001030 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Matthew Daley33eb2602013-02-06 23:41:36 +00001031 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001032 }
1033
1034 /* Remember the error: invalidate all subsequent fragments. */
1035 err = newerr;
1036 }
1037
1038 *gopp = gop + 1;
1039 return err;
1040}
1041
1042static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
1043{
1044 struct skb_shared_info *shinfo = skb_shinfo(skb);
1045 int nr_frags = shinfo->nr_frags;
1046 int i;
1047
1048 for (i = 0; i < nr_frags; i++) {
1049 skb_frag_t *frag = shinfo->frags + i;
1050 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001051 struct page *page;
1052 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001053
Ian Campbellea066ad2011-10-05 00:28:46 +00001054 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001055
1056 txp = &netbk->pending_tx_info[pending_idx].req;
Ian Campbellea066ad2011-10-05 00:28:46 +00001057 page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
1058 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001059 skb->len += txp->size;
1060 skb->data_len += txp->size;
1061 skb->truesize += txp->size;
1062
1063 /* Take an extra reference to offset xen_netbk_idx_release */
1064 get_page(netbk->mmap_pages[pending_idx]);
Matthew Daley33eb2602013-02-06 23:41:36 +00001065 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001066 }
1067}
1068
1069static int xen_netbk_get_extras(struct xenvif *vif,
1070 struct xen_netif_extra_info *extras,
1071 int work_to_do)
1072{
1073 struct xen_netif_extra_info extra;
1074 RING_IDX cons = vif->tx.req_cons;
1075
1076 do {
1077 if (unlikely(work_to_do-- <= 0)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001078 netdev_err(vif->dev, "Missing extra info\n");
1079 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001080 return -EBADR;
1081 }
1082
1083 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1084 sizeof(extra));
1085 if (unlikely(!extra.type ||
1086 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1087 vif->tx.req_cons = ++cons;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001088 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001089 "Invalid extra type: %d\n", extra.type);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001090 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001091 return -EINVAL;
1092 }
1093
1094 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1095 vif->tx.req_cons = ++cons;
1096 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1097
1098 return work_to_do;
1099}
1100
1101static int netbk_set_skb_gso(struct xenvif *vif,
1102 struct sk_buff *skb,
1103 struct xen_netif_extra_info *gso)
1104{
1105 if (!gso->u.gso.size) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001106 netdev_err(vif->dev, "GSO size must not be zero.\n");
1107 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001108 return -EINVAL;
1109 }
1110
1111 /* Currently only TCPv4 S.O. is supported. */
1112 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001113 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1114 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001115 return -EINVAL;
1116 }
1117
1118 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1119 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1120
1121 /* Header must be checked, and gso_segs computed. */
1122 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1123 skb_shinfo(skb)->gso_segs = 0;
1124
1125 return 0;
1126}
1127
1128static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1129{
1130 struct iphdr *iph;
1131 unsigned char *th;
1132 int err = -EPROTO;
1133 int recalculate_partial_csum = 0;
1134
1135 /*
1136 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1137 * peers can fail to set NETRXF_csum_blank when sending a GSO
1138 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1139 * recalculate the partial checksum.
1140 */
1141 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1142 vif->rx_gso_checksum_fixup++;
1143 skb->ip_summed = CHECKSUM_PARTIAL;
1144 recalculate_partial_csum = 1;
1145 }
1146
1147 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1148 if (skb->ip_summed != CHECKSUM_PARTIAL)
1149 return 0;
1150
1151 if (skb->protocol != htons(ETH_P_IP))
1152 goto out;
1153
1154 iph = (void *)skb->data;
1155 th = skb->data + 4 * iph->ihl;
1156 if (th >= skb_tail_pointer(skb))
1157 goto out;
1158
1159 skb->csum_start = th - skb->head;
1160 switch (iph->protocol) {
1161 case IPPROTO_TCP:
1162 skb->csum_offset = offsetof(struct tcphdr, check);
1163
1164 if (recalculate_partial_csum) {
1165 struct tcphdr *tcph = (struct tcphdr *)th;
1166 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1167 skb->len - iph->ihl*4,
1168 IPPROTO_TCP, 0);
1169 }
1170 break;
1171 case IPPROTO_UDP:
1172 skb->csum_offset = offsetof(struct udphdr, check);
1173
1174 if (recalculate_partial_csum) {
1175 struct udphdr *udph = (struct udphdr *)th;
1176 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1177 skb->len - iph->ihl*4,
1178 IPPROTO_UDP, 0);
1179 }
1180 break;
1181 default:
1182 if (net_ratelimit())
1183 netdev_err(vif->dev,
1184 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1185 iph->protocol);
1186 goto out;
1187 }
1188
1189 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
1190 goto out;
1191
1192 err = 0;
1193
1194out:
1195 return err;
1196}
1197
1198static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1199{
Wei Liu2b58df72013-10-28 12:07:57 +00001200 u64 now = get_jiffies_64();
1201 u64 next_credit = vif->credit_window_start +
Ian Campbellf942dc22011-03-15 00:06:18 +00001202 msecs_to_jiffies(vif->credit_usec / 1000);
1203
1204 /* Timer could already be pending in rare cases. */
1205 if (timer_pending(&vif->credit_timeout))
1206 return true;
1207
1208 /* Passed the point where we can replenish credit? */
Wei Liu2b58df72013-10-28 12:07:57 +00001209 if (time_after_eq64(now, next_credit)) {
1210 vif->credit_window_start = now;
Ian Campbellf942dc22011-03-15 00:06:18 +00001211 tx_add_credit(vif);
1212 }
1213
1214 /* Still too big to send right now? Set a callback. */
1215 if (size > vif->remaining_credit) {
1216 vif->credit_timeout.data =
1217 (unsigned long)vif;
1218 vif->credit_timeout.function =
1219 tx_credit_callback;
1220 mod_timer(&vif->credit_timeout,
1221 next_credit);
Wei Liu2b58df72013-10-28 12:07:57 +00001222 vif->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001223
1224 return true;
1225 }
1226
1227 return false;
1228}
1229
1230static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
1231{
1232 struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
1233 struct sk_buff *skb;
1234 int ret;
1235
1236 while (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1237 !list_empty(&netbk->net_schedule_list)) {
1238 struct xenvif *vif;
1239 struct xen_netif_tx_request txreq;
1240 struct xen_netif_tx_request txfrags[MAX_SKB_FRAGS];
1241 struct page *page;
1242 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1243 u16 pending_idx;
1244 RING_IDX idx;
1245 int work_to_do;
1246 unsigned int data_len;
1247 pending_ring_idx_t index;
1248
1249 /* Get a netif from the list with work to do. */
1250 vif = poll_net_schedule_list(netbk);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001251 /* This can sometimes happen because the test of
1252 * list_empty(net_schedule_list) at the top of the
1253 * loop is unlocked. Just go back and have another
1254 * look.
1255 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001256 if (!vif)
1257 continue;
1258
Ian Campbellbe7254f2013-02-06 23:41:35 +00001259 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1260 XEN_NETIF_TX_RING_SIZE) {
1261 netdev_err(vif->dev,
1262 "Impossible number of requests. "
1263 "req_prod %d, req_cons %d, size %ld\n",
1264 vif->tx.sring->req_prod, vif->tx.req_cons,
1265 XEN_NETIF_TX_RING_SIZE);
1266 netbk_fatal_tx_err(vif);
1267 continue;
1268 }
1269
Ian Campbellf942dc22011-03-15 00:06:18 +00001270 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1271 if (!work_to_do) {
1272 xenvif_put(vif);
1273 continue;
1274 }
1275
1276 idx = vif->tx.req_cons;
1277 rmb(); /* Ensure that we see the request before we copy it. */
1278 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1279
1280 /* Credit-based scheduling. */
1281 if (txreq.size > vif->remaining_credit &&
1282 tx_credit_exceeded(vif, txreq.size)) {
1283 xenvif_put(vif);
1284 continue;
1285 }
1286
1287 vif->remaining_credit -= txreq.size;
1288
1289 work_to_do--;
1290 vif->tx.req_cons = ++idx;
1291
1292 memset(extras, 0, sizeof(extras));
1293 if (txreq.flags & XEN_NETTXF_extra_info) {
1294 work_to_do = xen_netbk_get_extras(vif, extras,
1295 work_to_do);
1296 idx = vif->tx.req_cons;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001297 if (unlikely(work_to_do < 0))
Ian Campbellf942dc22011-03-15 00:06:18 +00001298 continue;
Ian Campbellf942dc22011-03-15 00:06:18 +00001299 }
1300
1301 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001302 if (unlikely(ret < 0))
Ian Campbellf942dc22011-03-15 00:06:18 +00001303 continue;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001304
Ian Campbellf942dc22011-03-15 00:06:18 +00001305 idx += ret;
1306
1307 if (unlikely(txreq.size < ETH_HLEN)) {
1308 netdev_dbg(vif->dev,
1309 "Bad packet size: %d\n", txreq.size);
1310 netbk_tx_err(vif, &txreq, idx);
1311 continue;
1312 }
1313
1314 /* No crossing a page as the payload mustn't fragment. */
1315 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001316 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001317 "txreq.offset: %x, size: %u, end: %lu\n",
1318 txreq.offset, txreq.size,
1319 (txreq.offset&~PAGE_MASK) + txreq.size);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001320 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001321 continue;
1322 }
1323
1324 index = pending_index(netbk->pending_cons);
1325 pending_idx = netbk->pending_ring[index];
1326
1327 data_len = (txreq.size > PKT_PROT_LEN &&
1328 ret < MAX_SKB_FRAGS) ?
1329 PKT_PROT_LEN : txreq.size;
1330
1331 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1332 GFP_ATOMIC | __GFP_NOWARN);
1333 if (unlikely(skb == NULL)) {
1334 netdev_dbg(vif->dev,
1335 "Can't allocate a skb in start_xmit.\n");
1336 netbk_tx_err(vif, &txreq, idx);
1337 break;
1338 }
1339
1340 /* Packets passed to netif_rx() must have some headroom. */
1341 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1342
1343 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1344 struct xen_netif_extra_info *gso;
1345 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1346
1347 if (netbk_set_skb_gso(vif, skb, gso)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001348 /* Failure in netbk_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001349 kfree_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001350 continue;
1351 }
1352 }
1353
1354 /* XXX could copy straight to head */
Wei Liubaff3c82013-03-25 01:08:20 +00001355 page = xen_netbk_alloc_page(netbk, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001356 if (!page) {
1357 kfree_skb(skb);
1358 netbk_tx_err(vif, &txreq, idx);
1359 continue;
1360 }
1361
Ian Campbellf942dc22011-03-15 00:06:18 +00001362 gop->source.u.ref = txreq.gref;
1363 gop->source.domid = vif->domid;
1364 gop->source.offset = txreq.offset;
1365
1366 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1367 gop->dest.domid = DOMID_SELF;
1368 gop->dest.offset = txreq.offset;
1369
1370 gop->len = txreq.size;
1371 gop->flags = GNTCOPY_source_gref;
1372
1373 gop++;
1374
1375 memcpy(&netbk->pending_tx_info[pending_idx].req,
1376 &txreq, sizeof(txreq));
1377 netbk->pending_tx_info[pending_idx].vif = vif;
1378 *((u16 *)skb->data) = pending_idx;
1379
1380 __skb_put(skb, data_len);
1381
1382 skb_shinfo(skb)->nr_frags = ret;
1383 if (data_len < txreq.size) {
1384 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001385 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1386 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001387 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001388 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1389 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001390 }
1391
1392 __skb_queue_tail(&netbk->tx_queue, skb);
1393
1394 netbk->pending_cons++;
1395
1396 request_gop = xen_netbk_get_requests(netbk, vif,
1397 skb, txfrags, gop);
1398 if (request_gop == NULL) {
1399 kfree_skb(skb);
1400 netbk_tx_err(vif, &txreq, idx);
1401 continue;
1402 }
1403 gop = request_gop;
1404
1405 vif->tx.req_cons = idx;
1406 xen_netbk_check_rx_xenvif(vif);
1407
1408 if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
1409 break;
1410 }
1411
1412 return gop - netbk->tx_copy_ops;
1413}
1414
1415static void xen_netbk_tx_submit(struct xen_netbk *netbk)
1416{
1417 struct gnttab_copy *gop = netbk->tx_copy_ops;
1418 struct sk_buff *skb;
1419
1420 while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
1421 struct xen_netif_tx_request *txp;
1422 struct xenvif *vif;
1423 u16 pending_idx;
1424 unsigned data_len;
1425
1426 pending_idx = *((u16 *)skb->data);
1427 vif = netbk->pending_tx_info[pending_idx].vif;
1428 txp = &netbk->pending_tx_info[pending_idx].req;
1429
1430 /* Check the remap error code. */
1431 if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1432 netdev_dbg(vif->dev, "netback grant failed.\n");
1433 skb_shinfo(skb)->nr_frags = 0;
1434 kfree_skb(skb);
1435 continue;
1436 }
1437
1438 data_len = skb->len;
1439 memcpy(skb->data,
1440 (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
1441 data_len);
1442 if (data_len < txp->size) {
1443 /* Append the packet payload as a fragment. */
1444 txp->offset += data_len;
1445 txp->size -= data_len;
1446 } else {
1447 /* Schedule a response immediately. */
Matthew Daley33eb2602013-02-06 23:41:36 +00001448 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001449 }
1450
1451 if (txp->flags & XEN_NETTXF_csum_blank)
1452 skb->ip_summed = CHECKSUM_PARTIAL;
1453 else if (txp->flags & XEN_NETTXF_data_validated)
1454 skb->ip_summed = CHECKSUM_UNNECESSARY;
1455
1456 xen_netbk_fill_frags(netbk, skb);
1457
1458 /*
1459 * If the initial fragment was < PKT_PROT_LEN then
1460 * pull through some bytes from the other fragments to
1461 * increase the linear region to PKT_PROT_LEN bytes.
1462 */
1463 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1464 int target = min_t(int, skb->len, PKT_PROT_LEN);
1465 __pskb_pull_tail(skb, target - skb_headlen(skb));
1466 }
1467
1468 skb->dev = vif->dev;
1469 skb->protocol = eth_type_trans(skb, skb->dev);
1470
1471 if (checksum_setup(vif, skb)) {
1472 netdev_dbg(vif->dev,
1473 "Can't setup checksum in net_tx_action\n");
1474 kfree_skb(skb);
1475 continue;
1476 }
1477
1478 vif->dev->stats.rx_bytes += skb->len;
1479 vif->dev->stats.rx_packets++;
1480
1481 xenvif_receive_skb(vif, skb);
1482 }
1483}
1484
1485/* Called after netfront has transmitted */
1486static void xen_netbk_tx_action(struct xen_netbk *netbk)
1487{
1488 unsigned nr_gops;
1489 int ret;
1490
1491 nr_gops = xen_netbk_tx_build_gops(netbk);
1492
1493 if (nr_gops == 0)
1494 return;
1495 ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1496 netbk->tx_copy_ops, nr_gops);
1497 BUG_ON(ret);
1498
1499 xen_netbk_tx_submit(netbk);
1500
1501}
1502
Matthew Daley33eb2602013-02-06 23:41:36 +00001503static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
1504 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001505{
1506 struct xenvif *vif;
1507 struct pending_tx_info *pending_tx_info;
1508 pending_ring_idx_t index;
1509
1510 /* Already complete? */
1511 if (netbk->mmap_pages[pending_idx] == NULL)
1512 return;
1513
1514 pending_tx_info = &netbk->pending_tx_info[pending_idx];
1515
1516 vif = pending_tx_info->vif;
1517
Matthew Daley33eb2602013-02-06 23:41:36 +00001518 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001519
1520 index = pending_index(netbk->pending_prod++);
1521 netbk->pending_ring[index] = pending_idx;
1522
1523 xenvif_put(vif);
1524
1525 netbk->mmap_pages[pending_idx]->mapping = 0;
1526 put_page(netbk->mmap_pages[pending_idx]);
1527 netbk->mmap_pages[pending_idx] = NULL;
1528}
1529
1530static void make_tx_response(struct xenvif *vif,
1531 struct xen_netif_tx_request *txp,
1532 s8 st)
1533{
1534 RING_IDX i = vif->tx.rsp_prod_pvt;
1535 struct xen_netif_tx_response *resp;
1536 int notify;
1537
1538 resp = RING_GET_RESPONSE(&vif->tx, i);
1539 resp->id = txp->id;
1540 resp->status = st;
1541
1542 if (txp->flags & XEN_NETTXF_extra_info)
1543 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1544
1545 vif->tx.rsp_prod_pvt = ++i;
1546 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1547 if (notify)
1548 notify_remote_via_irq(vif->irq);
1549}
1550
1551static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1552 u16 id,
1553 s8 st,
1554 u16 offset,
1555 u16 size,
1556 u16 flags)
1557{
1558 RING_IDX i = vif->rx.rsp_prod_pvt;
1559 struct xen_netif_rx_response *resp;
1560
1561 resp = RING_GET_RESPONSE(&vif->rx, i);
1562 resp->offset = offset;
1563 resp->flags = flags;
1564 resp->id = id;
1565 resp->status = (s16)size;
1566 if (st < 0)
1567 resp->status = (s16)st;
1568
1569 vif->rx.rsp_prod_pvt = ++i;
1570
1571 return resp;
1572}
1573
1574static inline int rx_work_todo(struct xen_netbk *netbk)
1575{
1576 return !skb_queue_empty(&netbk->rx_queue);
1577}
1578
1579static inline int tx_work_todo(struct xen_netbk *netbk)
1580{
1581
1582 if (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1583 !list_empty(&netbk->net_schedule_list))
1584 return 1;
1585
1586 return 0;
1587}
1588
1589static int xen_netbk_kthread(void *data)
1590{
1591 struct xen_netbk *netbk = data;
1592 while (!kthread_should_stop()) {
1593 wait_event_interruptible(netbk->wq,
1594 rx_work_todo(netbk) ||
1595 tx_work_todo(netbk) ||
1596 kthread_should_stop());
1597 cond_resched();
1598
1599 if (kthread_should_stop())
1600 break;
1601
1602 if (rx_work_todo(netbk))
1603 xen_netbk_rx_action(netbk);
1604
1605 if (tx_work_todo(netbk))
1606 xen_netbk_tx_action(netbk);
1607 }
1608
1609 return 0;
1610}
1611
1612void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
1613{
David Vrabelc9d63692011-09-29 16:53:31 +01001614 if (vif->tx.sring)
1615 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1616 vif->tx.sring);
1617 if (vif->rx.sring)
1618 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1619 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001620}
1621
1622int xen_netbk_map_frontend_rings(struct xenvif *vif,
1623 grant_ref_t tx_ring_ref,
1624 grant_ref_t rx_ring_ref)
1625{
David Vrabelc9d63692011-09-29 16:53:31 +01001626 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001627 struct xen_netif_tx_sring *txs;
1628 struct xen_netif_rx_sring *rxs;
1629
1630 int err = -ENOMEM;
1631
David Vrabelc9d63692011-09-29 16:53:31 +01001632 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1633 tx_ring_ref, &addr);
1634 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001635 goto err;
1636
David Vrabelc9d63692011-09-29 16:53:31 +01001637 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001638 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1639
David Vrabelc9d63692011-09-29 16:53:31 +01001640 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1641 rx_ring_ref, &addr);
1642 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001643 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001644
David Vrabelc9d63692011-09-29 16:53:31 +01001645 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001646 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1647
David Vrabelc9d63692011-09-29 16:53:31 +01001648 vif->rx_req_cons_peek = 0;
1649
Ian Campbellf942dc22011-03-15 00:06:18 +00001650 return 0;
1651
1652err:
1653 xen_netbk_unmap_frontend_rings(vif);
1654 return err;
1655}
1656
1657static int __init netback_init(void)
1658{
1659 int i;
1660 int rc = 0;
1661 int group;
1662
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001663 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001664 return -ENODEV;
1665
1666 xen_netbk_group_nr = num_online_cpus();
1667 xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
Joe Perchese404dec2012-01-29 12:56:23 +00001668 if (!xen_netbk)
Ian Campbellf942dc22011-03-15 00:06:18 +00001669 return -ENOMEM;
Ian Campbellf942dc22011-03-15 00:06:18 +00001670
1671 for (group = 0; group < xen_netbk_group_nr; group++) {
1672 struct xen_netbk *netbk = &xen_netbk[group];
1673 skb_queue_head_init(&netbk->rx_queue);
1674 skb_queue_head_init(&netbk->tx_queue);
1675
1676 init_timer(&netbk->net_timer);
1677 netbk->net_timer.data = (unsigned long)netbk;
1678 netbk->net_timer.function = xen_netbk_alarm;
1679
1680 netbk->pending_cons = 0;
1681 netbk->pending_prod = MAX_PENDING_REQS;
1682 for (i = 0; i < MAX_PENDING_REQS; i++)
1683 netbk->pending_ring[i] = i;
1684
1685 init_waitqueue_head(&netbk->wq);
1686 netbk->task = kthread_create(xen_netbk_kthread,
1687 (void *)netbk,
1688 "netback/%u", group);
1689
1690 if (IS_ERR(netbk->task)) {
Wei Liu6b84bd12011-12-05 06:57:44 +00001691 printk(KERN_ALERT "kthread_create() fails at netback\n");
Ian Campbellf942dc22011-03-15 00:06:18 +00001692 del_timer(&netbk->net_timer);
1693 rc = PTR_ERR(netbk->task);
1694 goto failed_init;
1695 }
1696
1697 kthread_bind(netbk->task, group);
1698
1699 INIT_LIST_HEAD(&netbk->net_schedule_list);
1700
1701 spin_lock_init(&netbk->net_schedule_list_lock);
1702
1703 atomic_set(&netbk->netfront_count, 0);
1704
1705 wake_up_process(netbk->task);
1706 }
1707
1708 rc = xenvif_xenbus_init();
1709 if (rc)
1710 goto failed_init;
1711
1712 return 0;
1713
1714failed_init:
1715 while (--group >= 0) {
1716 struct xen_netbk *netbk = &xen_netbk[group];
1717 for (i = 0; i < MAX_PENDING_REQS; i++) {
1718 if (netbk->mmap_pages[i])
1719 __free_page(netbk->mmap_pages[i]);
1720 }
1721 del_timer(&netbk->net_timer);
1722 kthread_stop(netbk->task);
1723 }
1724 vfree(xen_netbk);
1725 return rc;
1726
1727}
1728
1729module_init(netback_init);
1730
1731MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001732MODULE_ALIAS("xen-backend:vif");