blob: c71a73198b0fc15b010de183bb80ac341f003975 [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);
855 if (cons >= end)
856 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)
953 return NULL;
954
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;
975}
976
977static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
978 struct sk_buff *skb,
979 struct gnttab_copy **gopp)
980{
981 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000982 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000983 struct skb_shared_info *shinfo = skb_shinfo(skb);
984 int nr_frags = shinfo->nr_frags;
985 int i, err, start;
986
987 /* Check status of header. */
988 err = gop->status;
Matthew Daley33eb2602013-02-06 23:41:36 +0000989 if (unlikely(err))
990 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000991
992 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000993 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000994
995 for (i = start; i < nr_frags; i++) {
996 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +0000997
Ian Campbellea066ad2011-10-05 00:28:46 +0000998 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000999
1000 /* Check error status: if okay then remember grant handle. */
1001 newerr = (++gop)->status;
1002 if (likely(!newerr)) {
1003 /* Had a previous error? Invalidate this fragment. */
1004 if (unlikely(err))
Matthew Daley33eb2602013-02-06 23:41:36 +00001005 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001006 continue;
1007 }
1008
1009 /* Error on this fragment: respond to client with an error. */
Matthew Daley33eb2602013-02-06 23:41:36 +00001010 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001011
1012 /* Not the first error? Preceding frags already invalidated. */
1013 if (err)
1014 continue;
1015
1016 /* First error: invalidate header and preceding fragments. */
1017 pending_idx = *((u16 *)skb->data);
Matthew Daley33eb2602013-02-06 23:41:36 +00001018 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001019 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001020 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Matthew Daley33eb2602013-02-06 23:41:36 +00001021 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001022 }
1023
1024 /* Remember the error: invalidate all subsequent fragments. */
1025 err = newerr;
1026 }
1027
1028 *gopp = gop + 1;
1029 return err;
1030}
1031
1032static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
1033{
1034 struct skb_shared_info *shinfo = skb_shinfo(skb);
1035 int nr_frags = shinfo->nr_frags;
1036 int i;
1037
1038 for (i = 0; i < nr_frags; i++) {
1039 skb_frag_t *frag = shinfo->frags + i;
1040 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001041 struct page *page;
1042 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001043
Ian Campbellea066ad2011-10-05 00:28:46 +00001044 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001045
1046 txp = &netbk->pending_tx_info[pending_idx].req;
Ian Campbellea066ad2011-10-05 00:28:46 +00001047 page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
1048 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001049 skb->len += txp->size;
1050 skb->data_len += txp->size;
1051 skb->truesize += txp->size;
1052
1053 /* Take an extra reference to offset xen_netbk_idx_release */
1054 get_page(netbk->mmap_pages[pending_idx]);
Matthew Daley33eb2602013-02-06 23:41:36 +00001055 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001056 }
1057}
1058
1059static int xen_netbk_get_extras(struct xenvif *vif,
1060 struct xen_netif_extra_info *extras,
1061 int work_to_do)
1062{
1063 struct xen_netif_extra_info extra;
1064 RING_IDX cons = vif->tx.req_cons;
1065
1066 do {
1067 if (unlikely(work_to_do-- <= 0)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001068 netdev_err(vif->dev, "Missing extra info\n");
1069 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001070 return -EBADR;
1071 }
1072
1073 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1074 sizeof(extra));
1075 if (unlikely(!extra.type ||
1076 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1077 vif->tx.req_cons = ++cons;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001078 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001079 "Invalid extra type: %d\n", extra.type);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001080 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001081 return -EINVAL;
1082 }
1083
1084 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1085 vif->tx.req_cons = ++cons;
1086 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1087
1088 return work_to_do;
1089}
1090
1091static int netbk_set_skb_gso(struct xenvif *vif,
1092 struct sk_buff *skb,
1093 struct xen_netif_extra_info *gso)
1094{
1095 if (!gso->u.gso.size) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001096 netdev_err(vif->dev, "GSO size must not be zero.\n");
1097 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001098 return -EINVAL;
1099 }
1100
1101 /* Currently only TCPv4 S.O. is supported. */
1102 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001103 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1104 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001105 return -EINVAL;
1106 }
1107
1108 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1109 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1110
1111 /* Header must be checked, and gso_segs computed. */
1112 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1113 skb_shinfo(skb)->gso_segs = 0;
1114
1115 return 0;
1116}
1117
1118static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1119{
1120 struct iphdr *iph;
1121 unsigned char *th;
1122 int err = -EPROTO;
1123 int recalculate_partial_csum = 0;
1124
1125 /*
1126 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1127 * peers can fail to set NETRXF_csum_blank when sending a GSO
1128 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1129 * recalculate the partial checksum.
1130 */
1131 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1132 vif->rx_gso_checksum_fixup++;
1133 skb->ip_summed = CHECKSUM_PARTIAL;
1134 recalculate_partial_csum = 1;
1135 }
1136
1137 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1138 if (skb->ip_summed != CHECKSUM_PARTIAL)
1139 return 0;
1140
1141 if (skb->protocol != htons(ETH_P_IP))
1142 goto out;
1143
1144 iph = (void *)skb->data;
1145 th = skb->data + 4 * iph->ihl;
1146 if (th >= skb_tail_pointer(skb))
1147 goto out;
1148
1149 skb->csum_start = th - skb->head;
1150 switch (iph->protocol) {
1151 case IPPROTO_TCP:
1152 skb->csum_offset = offsetof(struct tcphdr, check);
1153
1154 if (recalculate_partial_csum) {
1155 struct tcphdr *tcph = (struct tcphdr *)th;
1156 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1157 skb->len - iph->ihl*4,
1158 IPPROTO_TCP, 0);
1159 }
1160 break;
1161 case IPPROTO_UDP:
1162 skb->csum_offset = offsetof(struct udphdr, check);
1163
1164 if (recalculate_partial_csum) {
1165 struct udphdr *udph = (struct udphdr *)th;
1166 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1167 skb->len - iph->ihl*4,
1168 IPPROTO_UDP, 0);
1169 }
1170 break;
1171 default:
1172 if (net_ratelimit())
1173 netdev_err(vif->dev,
1174 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1175 iph->protocol);
1176 goto out;
1177 }
1178
1179 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
1180 goto out;
1181
1182 err = 0;
1183
1184out:
1185 return err;
1186}
1187
1188static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1189{
1190 unsigned long now = jiffies;
1191 unsigned long next_credit =
1192 vif->credit_timeout.expires +
1193 msecs_to_jiffies(vif->credit_usec / 1000);
1194
1195 /* Timer could already be pending in rare cases. */
1196 if (timer_pending(&vif->credit_timeout))
1197 return true;
1198
1199 /* Passed the point where we can replenish credit? */
1200 if (time_after_eq(now, next_credit)) {
1201 vif->credit_timeout.expires = now;
1202 tx_add_credit(vif);
1203 }
1204
1205 /* Still too big to send right now? Set a callback. */
1206 if (size > vif->remaining_credit) {
1207 vif->credit_timeout.data =
1208 (unsigned long)vif;
1209 vif->credit_timeout.function =
1210 tx_credit_callback;
1211 mod_timer(&vif->credit_timeout,
1212 next_credit);
1213
1214 return true;
1215 }
1216
1217 return false;
1218}
1219
1220static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
1221{
1222 struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
1223 struct sk_buff *skb;
1224 int ret;
1225
1226 while (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1227 !list_empty(&netbk->net_schedule_list)) {
1228 struct xenvif *vif;
1229 struct xen_netif_tx_request txreq;
1230 struct xen_netif_tx_request txfrags[MAX_SKB_FRAGS];
1231 struct page *page;
1232 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1233 u16 pending_idx;
1234 RING_IDX idx;
1235 int work_to_do;
1236 unsigned int data_len;
1237 pending_ring_idx_t index;
1238
1239 /* Get a netif from the list with work to do. */
1240 vif = poll_net_schedule_list(netbk);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001241 /* This can sometimes happen because the test of
1242 * list_empty(net_schedule_list) at the top of the
1243 * loop is unlocked. Just go back and have another
1244 * look.
1245 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001246 if (!vif)
1247 continue;
1248
Ian Campbellbe7254f2013-02-06 23:41:35 +00001249 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1250 XEN_NETIF_TX_RING_SIZE) {
1251 netdev_err(vif->dev,
1252 "Impossible number of requests. "
1253 "req_prod %d, req_cons %d, size %ld\n",
1254 vif->tx.sring->req_prod, vif->tx.req_cons,
1255 XEN_NETIF_TX_RING_SIZE);
1256 netbk_fatal_tx_err(vif);
1257 continue;
1258 }
1259
Ian Campbellf942dc22011-03-15 00:06:18 +00001260 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1261 if (!work_to_do) {
1262 xenvif_put(vif);
1263 continue;
1264 }
1265
1266 idx = vif->tx.req_cons;
1267 rmb(); /* Ensure that we see the request before we copy it. */
1268 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1269
1270 /* Credit-based scheduling. */
1271 if (txreq.size > vif->remaining_credit &&
1272 tx_credit_exceeded(vif, txreq.size)) {
1273 xenvif_put(vif);
1274 continue;
1275 }
1276
1277 vif->remaining_credit -= txreq.size;
1278
1279 work_to_do--;
1280 vif->tx.req_cons = ++idx;
1281
1282 memset(extras, 0, sizeof(extras));
1283 if (txreq.flags & XEN_NETTXF_extra_info) {
1284 work_to_do = xen_netbk_get_extras(vif, extras,
1285 work_to_do);
1286 idx = vif->tx.req_cons;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001287 if (unlikely(work_to_do < 0))
Ian Campbellf942dc22011-03-15 00:06:18 +00001288 continue;
Ian Campbellf942dc22011-03-15 00:06:18 +00001289 }
1290
1291 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001292 if (unlikely(ret < 0))
Ian Campbellf942dc22011-03-15 00:06:18 +00001293 continue;
Ian Campbellbe7254f2013-02-06 23:41:35 +00001294
Ian Campbellf942dc22011-03-15 00:06:18 +00001295 idx += ret;
1296
1297 if (unlikely(txreq.size < ETH_HLEN)) {
1298 netdev_dbg(vif->dev,
1299 "Bad packet size: %d\n", txreq.size);
1300 netbk_tx_err(vif, &txreq, idx);
1301 continue;
1302 }
1303
1304 /* No crossing a page as the payload mustn't fragment. */
1305 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001306 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001307 "txreq.offset: %x, size: %u, end: %lu\n",
1308 txreq.offset, txreq.size,
1309 (txreq.offset&~PAGE_MASK) + txreq.size);
Ian Campbellbe7254f2013-02-06 23:41:35 +00001310 netbk_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001311 continue;
1312 }
1313
1314 index = pending_index(netbk->pending_cons);
1315 pending_idx = netbk->pending_ring[index];
1316
1317 data_len = (txreq.size > PKT_PROT_LEN &&
1318 ret < MAX_SKB_FRAGS) ?
1319 PKT_PROT_LEN : txreq.size;
1320
1321 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1322 GFP_ATOMIC | __GFP_NOWARN);
1323 if (unlikely(skb == NULL)) {
1324 netdev_dbg(vif->dev,
1325 "Can't allocate a skb in start_xmit.\n");
1326 netbk_tx_err(vif, &txreq, idx);
1327 break;
1328 }
1329
1330 /* Packets passed to netif_rx() must have some headroom. */
1331 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1332
1333 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1334 struct xen_netif_extra_info *gso;
1335 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1336
1337 if (netbk_set_skb_gso(vif, skb, gso)) {
Ian Campbellbe7254f2013-02-06 23:41:35 +00001338 /* Failure in netbk_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001339 kfree_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001340 continue;
1341 }
1342 }
1343
1344 /* XXX could copy straight to head */
1345 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
1346 if (!page) {
1347 kfree_skb(skb);
1348 netbk_tx_err(vif, &txreq, idx);
1349 continue;
1350 }
1351
Ian Campbellf942dc22011-03-15 00:06:18 +00001352 gop->source.u.ref = txreq.gref;
1353 gop->source.domid = vif->domid;
1354 gop->source.offset = txreq.offset;
1355
1356 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1357 gop->dest.domid = DOMID_SELF;
1358 gop->dest.offset = txreq.offset;
1359
1360 gop->len = txreq.size;
1361 gop->flags = GNTCOPY_source_gref;
1362
1363 gop++;
1364
1365 memcpy(&netbk->pending_tx_info[pending_idx].req,
1366 &txreq, sizeof(txreq));
1367 netbk->pending_tx_info[pending_idx].vif = vif;
1368 *((u16 *)skb->data) = pending_idx;
1369
1370 __skb_put(skb, data_len);
1371
1372 skb_shinfo(skb)->nr_frags = ret;
1373 if (data_len < txreq.size) {
1374 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001375 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1376 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001377 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001378 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1379 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001380 }
1381
1382 __skb_queue_tail(&netbk->tx_queue, skb);
1383
1384 netbk->pending_cons++;
1385
1386 request_gop = xen_netbk_get_requests(netbk, vif,
1387 skb, txfrags, gop);
1388 if (request_gop == NULL) {
1389 kfree_skb(skb);
1390 netbk_tx_err(vif, &txreq, idx);
1391 continue;
1392 }
1393 gop = request_gop;
1394
1395 vif->tx.req_cons = idx;
1396 xen_netbk_check_rx_xenvif(vif);
1397
1398 if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
1399 break;
1400 }
1401
1402 return gop - netbk->tx_copy_ops;
1403}
1404
1405static void xen_netbk_tx_submit(struct xen_netbk *netbk)
1406{
1407 struct gnttab_copy *gop = netbk->tx_copy_ops;
1408 struct sk_buff *skb;
1409
1410 while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
1411 struct xen_netif_tx_request *txp;
1412 struct xenvif *vif;
1413 u16 pending_idx;
1414 unsigned data_len;
1415
1416 pending_idx = *((u16 *)skb->data);
1417 vif = netbk->pending_tx_info[pending_idx].vif;
1418 txp = &netbk->pending_tx_info[pending_idx].req;
1419
1420 /* Check the remap error code. */
1421 if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1422 netdev_dbg(vif->dev, "netback grant failed.\n");
1423 skb_shinfo(skb)->nr_frags = 0;
1424 kfree_skb(skb);
1425 continue;
1426 }
1427
1428 data_len = skb->len;
1429 memcpy(skb->data,
1430 (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
1431 data_len);
1432 if (data_len < txp->size) {
1433 /* Append the packet payload as a fragment. */
1434 txp->offset += data_len;
1435 txp->size -= data_len;
1436 } else {
1437 /* Schedule a response immediately. */
Matthew Daley33eb2602013-02-06 23:41:36 +00001438 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001439 }
1440
1441 if (txp->flags & XEN_NETTXF_csum_blank)
1442 skb->ip_summed = CHECKSUM_PARTIAL;
1443 else if (txp->flags & XEN_NETTXF_data_validated)
1444 skb->ip_summed = CHECKSUM_UNNECESSARY;
1445
1446 xen_netbk_fill_frags(netbk, skb);
1447
1448 /*
1449 * If the initial fragment was < PKT_PROT_LEN then
1450 * pull through some bytes from the other fragments to
1451 * increase the linear region to PKT_PROT_LEN bytes.
1452 */
1453 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1454 int target = min_t(int, skb->len, PKT_PROT_LEN);
1455 __pskb_pull_tail(skb, target - skb_headlen(skb));
1456 }
1457
1458 skb->dev = vif->dev;
1459 skb->protocol = eth_type_trans(skb, skb->dev);
1460
1461 if (checksum_setup(vif, skb)) {
1462 netdev_dbg(vif->dev,
1463 "Can't setup checksum in net_tx_action\n");
1464 kfree_skb(skb);
1465 continue;
1466 }
1467
1468 vif->dev->stats.rx_bytes += skb->len;
1469 vif->dev->stats.rx_packets++;
1470
1471 xenvif_receive_skb(vif, skb);
1472 }
1473}
1474
1475/* Called after netfront has transmitted */
1476static void xen_netbk_tx_action(struct xen_netbk *netbk)
1477{
1478 unsigned nr_gops;
1479 int ret;
1480
1481 nr_gops = xen_netbk_tx_build_gops(netbk);
1482
1483 if (nr_gops == 0)
1484 return;
1485 ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1486 netbk->tx_copy_ops, nr_gops);
1487 BUG_ON(ret);
1488
1489 xen_netbk_tx_submit(netbk);
1490
1491}
1492
Matthew Daley33eb2602013-02-06 23:41:36 +00001493static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
1494 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001495{
1496 struct xenvif *vif;
1497 struct pending_tx_info *pending_tx_info;
1498 pending_ring_idx_t index;
1499
1500 /* Already complete? */
1501 if (netbk->mmap_pages[pending_idx] == NULL)
1502 return;
1503
1504 pending_tx_info = &netbk->pending_tx_info[pending_idx];
1505
1506 vif = pending_tx_info->vif;
1507
Matthew Daley33eb2602013-02-06 23:41:36 +00001508 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001509
1510 index = pending_index(netbk->pending_prod++);
1511 netbk->pending_ring[index] = pending_idx;
1512
1513 xenvif_put(vif);
1514
1515 netbk->mmap_pages[pending_idx]->mapping = 0;
1516 put_page(netbk->mmap_pages[pending_idx]);
1517 netbk->mmap_pages[pending_idx] = NULL;
1518}
1519
1520static void make_tx_response(struct xenvif *vif,
1521 struct xen_netif_tx_request *txp,
1522 s8 st)
1523{
1524 RING_IDX i = vif->tx.rsp_prod_pvt;
1525 struct xen_netif_tx_response *resp;
1526 int notify;
1527
1528 resp = RING_GET_RESPONSE(&vif->tx, i);
1529 resp->id = txp->id;
1530 resp->status = st;
1531
1532 if (txp->flags & XEN_NETTXF_extra_info)
1533 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1534
1535 vif->tx.rsp_prod_pvt = ++i;
1536 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1537 if (notify)
1538 notify_remote_via_irq(vif->irq);
1539}
1540
1541static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1542 u16 id,
1543 s8 st,
1544 u16 offset,
1545 u16 size,
1546 u16 flags)
1547{
1548 RING_IDX i = vif->rx.rsp_prod_pvt;
1549 struct xen_netif_rx_response *resp;
1550
1551 resp = RING_GET_RESPONSE(&vif->rx, i);
1552 resp->offset = offset;
1553 resp->flags = flags;
1554 resp->id = id;
1555 resp->status = (s16)size;
1556 if (st < 0)
1557 resp->status = (s16)st;
1558
1559 vif->rx.rsp_prod_pvt = ++i;
1560
1561 return resp;
1562}
1563
1564static inline int rx_work_todo(struct xen_netbk *netbk)
1565{
1566 return !skb_queue_empty(&netbk->rx_queue);
1567}
1568
1569static inline int tx_work_todo(struct xen_netbk *netbk)
1570{
1571
1572 if (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1573 !list_empty(&netbk->net_schedule_list))
1574 return 1;
1575
1576 return 0;
1577}
1578
1579static int xen_netbk_kthread(void *data)
1580{
1581 struct xen_netbk *netbk = data;
1582 while (!kthread_should_stop()) {
1583 wait_event_interruptible(netbk->wq,
1584 rx_work_todo(netbk) ||
1585 tx_work_todo(netbk) ||
1586 kthread_should_stop());
1587 cond_resched();
1588
1589 if (kthread_should_stop())
1590 break;
1591
1592 if (rx_work_todo(netbk))
1593 xen_netbk_rx_action(netbk);
1594
1595 if (tx_work_todo(netbk))
1596 xen_netbk_tx_action(netbk);
1597 }
1598
1599 return 0;
1600}
1601
1602void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
1603{
David Vrabelc9d63692011-09-29 16:53:31 +01001604 if (vif->tx.sring)
1605 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1606 vif->tx.sring);
1607 if (vif->rx.sring)
1608 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1609 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001610}
1611
1612int xen_netbk_map_frontend_rings(struct xenvif *vif,
1613 grant_ref_t tx_ring_ref,
1614 grant_ref_t rx_ring_ref)
1615{
David Vrabelc9d63692011-09-29 16:53:31 +01001616 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001617 struct xen_netif_tx_sring *txs;
1618 struct xen_netif_rx_sring *rxs;
1619
1620 int err = -ENOMEM;
1621
David Vrabelc9d63692011-09-29 16:53:31 +01001622 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1623 tx_ring_ref, &addr);
1624 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001625 goto err;
1626
David Vrabelc9d63692011-09-29 16:53:31 +01001627 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001628 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1629
David Vrabelc9d63692011-09-29 16:53:31 +01001630 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1631 rx_ring_ref, &addr);
1632 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001633 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001634
David Vrabelc9d63692011-09-29 16:53:31 +01001635 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001636 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1637
David Vrabelc9d63692011-09-29 16:53:31 +01001638 vif->rx_req_cons_peek = 0;
1639
Ian Campbellf942dc22011-03-15 00:06:18 +00001640 return 0;
1641
1642err:
1643 xen_netbk_unmap_frontend_rings(vif);
1644 return err;
1645}
1646
1647static int __init netback_init(void)
1648{
1649 int i;
1650 int rc = 0;
1651 int group;
1652
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001653 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001654 return -ENODEV;
1655
1656 xen_netbk_group_nr = num_online_cpus();
1657 xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
Joe Perchese404dec2012-01-29 12:56:23 +00001658 if (!xen_netbk)
Ian Campbellf942dc22011-03-15 00:06:18 +00001659 return -ENOMEM;
Ian Campbellf942dc22011-03-15 00:06:18 +00001660
1661 for (group = 0; group < xen_netbk_group_nr; group++) {
1662 struct xen_netbk *netbk = &xen_netbk[group];
1663 skb_queue_head_init(&netbk->rx_queue);
1664 skb_queue_head_init(&netbk->tx_queue);
1665
1666 init_timer(&netbk->net_timer);
1667 netbk->net_timer.data = (unsigned long)netbk;
1668 netbk->net_timer.function = xen_netbk_alarm;
1669
1670 netbk->pending_cons = 0;
1671 netbk->pending_prod = MAX_PENDING_REQS;
1672 for (i = 0; i < MAX_PENDING_REQS; i++)
1673 netbk->pending_ring[i] = i;
1674
1675 init_waitqueue_head(&netbk->wq);
1676 netbk->task = kthread_create(xen_netbk_kthread,
1677 (void *)netbk,
1678 "netback/%u", group);
1679
1680 if (IS_ERR(netbk->task)) {
Wei Liu6b84bd12011-12-05 06:57:44 +00001681 printk(KERN_ALERT "kthread_create() fails at netback\n");
Ian Campbellf942dc22011-03-15 00:06:18 +00001682 del_timer(&netbk->net_timer);
1683 rc = PTR_ERR(netbk->task);
1684 goto failed_init;
1685 }
1686
1687 kthread_bind(netbk->task, group);
1688
1689 INIT_LIST_HEAD(&netbk->net_schedule_list);
1690
1691 spin_lock_init(&netbk->net_schedule_list_lock);
1692
1693 atomic_set(&netbk->netfront_count, 0);
1694
1695 wake_up_process(netbk->task);
1696 }
1697
1698 rc = xenvif_xenbus_init();
1699 if (rc)
1700 goto failed_init;
1701
1702 return 0;
1703
1704failed_init:
1705 while (--group >= 0) {
1706 struct xen_netbk *netbk = &xen_netbk[group];
1707 for (i = 0; i < MAX_PENDING_REQS; i++) {
1708 if (netbk->mmap_pages[i])
1709 __free_page(netbk->mmap_pages[i]);
1710 }
1711 del_timer(&netbk->net_timer);
1712 kthread_stop(netbk->task);
1713 }
1714 vfree(xen_netbk);
1715 return rc;
1716
1717}
1718
1719module_init(netback_init);
1720
1721MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001722MODULE_ALIAS("xen-backend:vif");