bridge: Pull ip header into skb->data before looking into ip header.
[linux-2.6-block.git] / drivers / net / xen-netback / netback.c
CommitLineData
f942dc25
IC
1/*
2 * Back-end of the driver for virtual network devices. This portion of the
3 * driver exports a 'unified' network-device interface that can be accessed
4 * by any operating system that implements a compatible front end. A
5 * reference front-end implementation can be found in:
6 * drivers/net/xen-netfront.c
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
ca981633 43#include <xen/xen.h>
f942dc25
IC
44#include <xen/events.h>
45#include <xen/interface/memory.h>
46
47#include <asm/xen/hypercall.h>
48#include <asm/xen/page.h>
49
50struct pending_tx_info {
51 struct xen_netif_tx_request req;
52 struct xenvif *vif;
53};
54typedef unsigned int pending_ring_idx_t;
55
56struct netbk_rx_meta {
57 int id;
58 int size;
59 int gso_size;
60};
61
62#define MAX_PENDING_REQS 256
63
ea066ad1
IC
64/* Discriminate from any valid pending_idx value. */
65#define INVALID_PENDING_IDX 0xFFFF
66
f942dc25
IC
67#define MAX_BUFFER_OFFSET PAGE_SIZE
68
69/* extra field used in struct page */
70union page_ext {
71 struct {
72#if BITS_PER_LONG < 64
73#define IDX_WIDTH 8
74#define GROUP_WIDTH (BITS_PER_LONG - IDX_WIDTH)
75 unsigned int group:GROUP_WIDTH;
76 unsigned int idx:IDX_WIDTH;
77#else
78 unsigned int group, idx;
79#endif
80 } e;
81 void *mapping;
82};
83
84struct xen_netbk {
85 wait_queue_head_t wq;
86 struct task_struct *task;
87
88 struct sk_buff_head rx_queue;
89 struct sk_buff_head tx_queue;
90
91 struct timer_list net_timer;
92
93 struct page *mmap_pages[MAX_PENDING_REQS];
94
95 pending_ring_idx_t pending_prod;
96 pending_ring_idx_t pending_cons;
97 struct list_head net_schedule_list;
98
99 /* Protect the net_schedule_list in netif. */
100 spinlock_t net_schedule_list_lock;
101
102 atomic_t netfront_count;
103
104 struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
105 struct gnttab_copy tx_copy_ops[MAX_PENDING_REQS];
106
107 u16 pending_ring[MAX_PENDING_REQS];
108
109 /*
110 * Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
111 * head/fragment page uses 2 copy operations because it
112 * straddles two buffers in the frontend.
113 */
114 struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
115 struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
116};
117
118static struct xen_netbk *xen_netbk;
119static int xen_netbk_group_nr;
120
121void xen_netbk_add_xenvif(struct xenvif *vif)
122{
123 int i;
124 int min_netfront_count;
125 int min_group = 0;
126 struct xen_netbk *netbk;
127
128 min_netfront_count = atomic_read(&xen_netbk[0].netfront_count);
129 for (i = 0; i < xen_netbk_group_nr; i++) {
130 int netfront_count = atomic_read(&xen_netbk[i].netfront_count);
131 if (netfront_count < min_netfront_count) {
132 min_group = i;
133 min_netfront_count = netfront_count;
134 }
135 }
136
137 netbk = &xen_netbk[min_group];
138
139 vif->netbk = netbk;
140 atomic_inc(&netbk->netfront_count);
141}
142
143void xen_netbk_remove_xenvif(struct xenvif *vif)
144{
145 struct xen_netbk *netbk = vif->netbk;
146 vif->netbk = NULL;
147 atomic_dec(&netbk->netfront_count);
148}
149
150static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx);
151static 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,
ea066ad1 162 u16 idx)
f942dc25
IC
163{
164 return page_to_pfn(netbk->mmap_pages[idx]);
165}
166
167static inline unsigned long idx_to_kaddr(struct xen_netbk *netbk,
ea066ad1 168 u16 idx)
f942dc25
IC
169{
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
ea066ad1
IC
222static 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
f942dc25
IC
232static 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
e26b203e 329 count = DIV_ROUND_UP(skb_headlen(skb), PAGE_SIZE);
f942dc25
IC
330
331 copy_off = skb_headlen(skb) % PAGE_SIZE;
332
333 if (skb_shinfo(skb)->gso_size)
334 count++;
335
336 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
9e903e08 337 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
f942dc25
IC
338 unsigned long bytes;
339 while (size > 0) {
340 BUG_ON(copy_off > MAX_BUFFER_OFFSET);
341
342 if (start_new_rx_buffer(copy_off, size, 0)) {
343 count++;
344 copy_off = 0;
345 }
346
347 bytes = size;
348 if (copy_off + bytes > MAX_BUFFER_OFFSET)
349 bytes = MAX_BUFFER_OFFSET - copy_off;
350
351 copy_off += bytes;
352 size -= bytes;
353 }
354 }
355 return count;
356}
357
358struct netrx_pending_operations {
359 unsigned copy_prod, copy_cons;
360 unsigned meta_prod, meta_cons;
361 struct gnttab_copy *copy;
362 struct netbk_rx_meta *meta;
363 int copy_off;
364 grant_ref_t copy_gref;
365};
366
367static struct netbk_rx_meta *get_next_rx_buffer(struct xenvif *vif,
368 struct netrx_pending_operations *npo)
369{
370 struct netbk_rx_meta *meta;
371 struct xen_netif_rx_request *req;
372
373 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
374
375 meta = npo->meta + npo->meta_prod++;
376 meta->gso_size = 0;
377 meta->size = 0;
378 meta->id = req->id;
379
380 npo->copy_off = 0;
381 npo->copy_gref = req->gref;
382
383 return meta;
384}
385
386/*
387 * Set up the grant operations for this fragment. If it's a flipping
388 * interface, we also set up the unmap request from here.
389 */
390static void netbk_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
391 struct netrx_pending_operations *npo,
392 struct page *page, unsigned long size,
393 unsigned long offset, int *head)
394{
395 struct gnttab_copy *copy_gop;
396 struct netbk_rx_meta *meta;
397 /*
e34c0246 398 * These variables are used iff get_page_ext returns true,
f942dc25
IC
399 * in which case they are guaranteed to be initialized.
400 */
401 unsigned int uninitialized_var(group), uninitialized_var(idx);
402 int foreign = get_page_ext(page, &group, &idx);
403 unsigned long bytes;
404
405 /* Data must not cross a page boundary. */
406 BUG_ON(size + offset > PAGE_SIZE);
407
408 meta = npo->meta + npo->meta_prod - 1;
409
410 while (size > 0) {
411 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
412
413 if (start_new_rx_buffer(npo->copy_off, size, *head)) {
414 /*
415 * Netfront requires there to be some data in the head
416 * buffer.
417 */
418 BUG_ON(*head);
419
420 meta = get_next_rx_buffer(vif, npo);
421 }
422
423 bytes = size;
424 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
425 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
426
427 copy_gop = npo->copy + npo->copy_prod++;
428 copy_gop->flags = GNTCOPY_dest_gref;
429 if (foreign) {
430 struct xen_netbk *netbk = &xen_netbk[group];
431 struct pending_tx_info *src_pend;
432
433 src_pend = &netbk->pending_tx_info[idx];
434
435 copy_gop->source.domid = src_pend->vif->domid;
436 copy_gop->source.u.ref = src_pend->req.gref;
437 copy_gop->flags |= GNTCOPY_source_gref;
438 } else {
439 void *vaddr = page_address(page);
440 copy_gop->source.domid = DOMID_SELF;
441 copy_gop->source.u.gmfn = virt_to_mfn(vaddr);
442 }
443 copy_gop->source.offset = offset;
444 copy_gop->dest.domid = vif->domid;
445
446 copy_gop->dest.offset = npo->copy_off;
447 copy_gop->dest.u.ref = npo->copy_gref;
448 copy_gop->len = bytes;
449
450 npo->copy_off += bytes;
451 meta->size += bytes;
452
453 offset += bytes;
454 size -= bytes;
455
456 /* Leave a gap for the GSO descriptor. */
457 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
458 vif->rx.req_cons++;
459
460 *head = 0; /* There must be something in this buffer now. */
461
462 }
463}
464
465/*
466 * Prepare an SKB to be transmitted to the frontend.
467 *
468 * This function is responsible for allocating grant operations, meta
469 * structures, etc.
470 *
471 * It returns the number of meta structures consumed. The number of
472 * ring slots used is always equal to the number of meta slots used
473 * plus the number of GSO descriptors used. Currently, we use either
474 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
475 * frontend-side LRO).
476 */
477static int netbk_gop_skb(struct sk_buff *skb,
478 struct netrx_pending_operations *npo)
479{
480 struct xenvif *vif = netdev_priv(skb->dev);
481 int nr_frags = skb_shinfo(skb)->nr_frags;
482 int i;
483 struct xen_netif_rx_request *req;
484 struct netbk_rx_meta *meta;
485 unsigned char *data;
486 int head = 1;
487 int old_meta_prod;
488
489 old_meta_prod = npo->meta_prod;
490
491 /* Set up a GSO prefix descriptor, if necessary */
492 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
493 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
494 meta = npo->meta + npo->meta_prod++;
495 meta->gso_size = skb_shinfo(skb)->gso_size;
496 meta->size = 0;
497 meta->id = req->id;
498 }
499
500 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
501 meta = npo->meta + npo->meta_prod++;
502
503 if (!vif->gso_prefix)
504 meta->gso_size = skb_shinfo(skb)->gso_size;
505 else
506 meta->gso_size = 0;
507
508 meta->size = 0;
509 meta->id = req->id;
510 npo->copy_off = 0;
511 npo->copy_gref = req->gref;
512
513 data = skb->data;
514 while (data < skb_tail_pointer(skb)) {
515 unsigned int offset = offset_in_page(data);
516 unsigned int len = PAGE_SIZE - offset;
517
518 if (data + len > skb_tail_pointer(skb))
519 len = skb_tail_pointer(skb) - data;
520
521 netbk_gop_frag_copy(vif, skb, npo,
522 virt_to_page(data), len, offset, &head);
523 data += len;
524 }
525
526 for (i = 0; i < nr_frags; i++) {
527 netbk_gop_frag_copy(vif, skb, npo,
ea066ad1 528 skb_frag_page(&skb_shinfo(skb)->frags[i]),
9e903e08 529 skb_frag_size(&skb_shinfo(skb)->frags[i]),
f942dc25
IC
530 skb_shinfo(skb)->frags[i].page_offset,
531 &head);
532 }
533
534 return npo->meta_prod - old_meta_prod;
535}
536
537/*
538 * This is a twin to netbk_gop_skb. Assume that netbk_gop_skb was
539 * used to set up the operations on the top of
540 * netrx_pending_operations, which have since been done. Check that
541 * they didn't give any errors and advance over them.
542 */
543static int netbk_check_gop(struct xenvif *vif, int nr_meta_slots,
544 struct netrx_pending_operations *npo)
545{
546 struct gnttab_copy *copy_op;
547 int status = XEN_NETIF_RSP_OKAY;
548 int i;
549
550 for (i = 0; i < nr_meta_slots; i++) {
551 copy_op = npo->copy + npo->copy_cons++;
552 if (copy_op->status != GNTST_okay) {
553 netdev_dbg(vif->dev,
554 "Bad status %d from copy to DOM%d.\n",
555 copy_op->status, vif->domid);
556 status = XEN_NETIF_RSP_ERROR;
557 }
558 }
559
560 return status;
561}
562
563static void netbk_add_frag_responses(struct xenvif *vif, int status,
564 struct netbk_rx_meta *meta,
565 int nr_meta_slots)
566{
567 int i;
568 unsigned long offset;
569
570 /* No fragments used */
571 if (nr_meta_slots <= 1)
572 return;
573
574 nr_meta_slots--;
575
576 for (i = 0; i < nr_meta_slots; i++) {
577 int flags;
578 if (i == nr_meta_slots - 1)
579 flags = 0;
580 else
581 flags = XEN_NETRXF_more_data;
582
583 offset = 0;
584 make_rx_response(vif, meta[i].id, status, offset,
585 meta[i].size, flags);
586 }
587}
588
589struct skb_cb_overlay {
590 int meta_slots_used;
591};
592
593static void xen_netbk_rx_action(struct xen_netbk *netbk)
594{
595 struct xenvif *vif = NULL, *tmp;
596 s8 status;
597 u16 irq, flags;
598 struct xen_netif_rx_response *resp;
599 struct sk_buff_head rxq;
600 struct sk_buff *skb;
601 LIST_HEAD(notify);
602 int ret;
603 int nr_frags;
604 int count;
605 unsigned long offset;
606 struct skb_cb_overlay *sco;
607
608 struct netrx_pending_operations npo = {
609 .copy = netbk->grant_copy_op,
610 .meta = netbk->meta,
611 };
612
613 skb_queue_head_init(&rxq);
614
615 count = 0;
616
617 while ((skb = skb_dequeue(&netbk->rx_queue)) != NULL) {
618 vif = netdev_priv(skb->dev);
619 nr_frags = skb_shinfo(skb)->nr_frags;
620
621 sco = (struct skb_cb_overlay *)skb->cb;
622 sco->meta_slots_used = netbk_gop_skb(skb, &npo);
623
624 count += nr_frags + 1;
625
626 __skb_queue_tail(&rxq, skb);
627
628 /* Filled the batch queue? */
629 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
630 break;
631 }
632
633 BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
634
635 if (!npo.copy_prod)
636 return;
637
638 BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
c571898f 639 gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
f942dc25
IC
640
641 while ((skb = __skb_dequeue(&rxq)) != NULL) {
642 sco = (struct skb_cb_overlay *)skb->cb;
643
644 vif = netdev_priv(skb->dev);
645
646 if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
647 resp = RING_GET_RESPONSE(&vif->rx,
648 vif->rx.rsp_prod_pvt++);
649
650 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
651
652 resp->offset = netbk->meta[npo.meta_cons].gso_size;
653 resp->id = netbk->meta[npo.meta_cons].id;
654 resp->status = sco->meta_slots_used;
655
656 npo.meta_cons++;
657 sco->meta_slots_used--;
658 }
659
660
661 vif->dev->stats.tx_bytes += skb->len;
662 vif->dev->stats.tx_packets++;
663
664 status = netbk_check_gop(vif, sco->meta_slots_used, &npo);
665
666 if (sco->meta_slots_used == 1)
667 flags = 0;
668 else
669 flags = XEN_NETRXF_more_data;
670
671 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
672 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
673 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
674 /* remote but checksummed. */
675 flags |= XEN_NETRXF_data_validated;
676
677 offset = 0;
678 resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
679 status, offset,
680 netbk->meta[npo.meta_cons].size,
681 flags);
682
683 if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
684 struct xen_netif_extra_info *gso =
685 (struct xen_netif_extra_info *)
686 RING_GET_RESPONSE(&vif->rx,
687 vif->rx.rsp_prod_pvt++);
688
689 resp->flags |= XEN_NETRXF_extra_info;
690
691 gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
692 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
693 gso->u.gso.pad = 0;
694 gso->u.gso.features = 0;
695
696 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
697 gso->flags = 0;
698 }
699
700 netbk_add_frag_responses(vif, status,
701 netbk->meta + npo.meta_cons + 1,
702 sco->meta_slots_used);
703
704 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
705 irq = vif->irq;
706 if (ret && list_empty(&vif->notify_list))
707 list_add_tail(&vif->notify_list, &notify);
708
709 xenvif_notify_tx_completion(vif);
710
711 xenvif_put(vif);
712 npo.meta_cons += sco->meta_slots_used;
713 dev_kfree_skb(skb);
714 }
715
716 list_for_each_entry_safe(vif, tmp, &notify, notify_list) {
717 notify_remote_via_irq(vif->irq);
718 list_del_init(&vif->notify_list);
719 }
720
721 /* More work to do? */
722 if (!skb_queue_empty(&netbk->rx_queue) &&
723 !timer_pending(&netbk->net_timer))
724 xen_netbk_kick_thread(netbk);
725}
726
727void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
728{
729 struct xen_netbk *netbk = vif->netbk;
730
731 skb_queue_tail(&netbk->rx_queue, skb);
732
733 xen_netbk_kick_thread(netbk);
734}
735
736static void xen_netbk_alarm(unsigned long data)
737{
738 struct xen_netbk *netbk = (struct xen_netbk *)data;
739 xen_netbk_kick_thread(netbk);
740}
741
742static int __on_net_schedule_list(struct xenvif *vif)
743{
744 return !list_empty(&vif->schedule_list);
745}
746
747/* Must be called with net_schedule_list_lock held */
748static void remove_from_net_schedule_list(struct xenvif *vif)
749{
750 if (likely(__on_net_schedule_list(vif))) {
751 list_del_init(&vif->schedule_list);
752 xenvif_put(vif);
753 }
754}
755
756static struct xenvif *poll_net_schedule_list(struct xen_netbk *netbk)
757{
758 struct xenvif *vif = NULL;
759
760 spin_lock_irq(&netbk->net_schedule_list_lock);
761 if (list_empty(&netbk->net_schedule_list))
762 goto out;
763
764 vif = list_first_entry(&netbk->net_schedule_list,
765 struct xenvif, schedule_list);
766 if (!vif)
767 goto out;
768
769 xenvif_get(vif);
770
771 remove_from_net_schedule_list(vif);
772out:
773 spin_unlock_irq(&netbk->net_schedule_list_lock);
774 return vif;
775}
776
777void xen_netbk_schedule_xenvif(struct xenvif *vif)
778{
779 unsigned long flags;
780 struct xen_netbk *netbk = vif->netbk;
781
782 if (__on_net_schedule_list(vif))
783 goto kick;
784
785 spin_lock_irqsave(&netbk->net_schedule_list_lock, flags);
786 if (!__on_net_schedule_list(vif) &&
787 likely(xenvif_schedulable(vif))) {
788 list_add_tail(&vif->schedule_list, &netbk->net_schedule_list);
789 xenvif_get(vif);
790 }
791 spin_unlock_irqrestore(&netbk->net_schedule_list_lock, flags);
792
793kick:
794 smp_mb();
795 if ((nr_pending_reqs(netbk) < (MAX_PENDING_REQS/2)) &&
796 !list_empty(&netbk->net_schedule_list))
797 xen_netbk_kick_thread(netbk);
798}
799
800void xen_netbk_deschedule_xenvif(struct xenvif *vif)
801{
802 struct xen_netbk *netbk = vif->netbk;
803 spin_lock_irq(&netbk->net_schedule_list_lock);
804 remove_from_net_schedule_list(vif);
805 spin_unlock_irq(&netbk->net_schedule_list_lock);
806}
807
808void xen_netbk_check_rx_xenvif(struct xenvif *vif)
809{
810 int more_to_do;
811
812 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
813
814 if (more_to_do)
815 xen_netbk_schedule_xenvif(vif);
816}
817
818static void tx_add_credit(struct xenvif *vif)
819{
820 unsigned long max_burst, max_credit;
821
822 /*
823 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
824 * Otherwise the interface can seize up due to insufficient credit.
825 */
826 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
827 max_burst = min(max_burst, 131072UL);
828 max_burst = max(max_burst, vif->credit_bytes);
829
830 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
831 max_credit = vif->remaining_credit + vif->credit_bytes;
832 if (max_credit < vif->remaining_credit)
833 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
834
835 vif->remaining_credit = min(max_credit, max_burst);
836}
837
838static void tx_credit_callback(unsigned long data)
839{
840 struct xenvif *vif = (struct xenvif *)data;
841 tx_add_credit(vif);
842 xen_netbk_check_rx_xenvif(vif);
843}
844
845static void netbk_tx_err(struct xenvif *vif,
846 struct xen_netif_tx_request *txp, RING_IDX end)
847{
848 RING_IDX cons = vif->tx.req_cons;
849
850 do {
851 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
852 if (cons >= end)
853 break;
854 txp = RING_GET_REQUEST(&vif->tx, cons++);
855 } while (1);
856 vif->tx.req_cons = cons;
857 xen_netbk_check_rx_xenvif(vif);
858 xenvif_put(vif);
859}
860
861static int netbk_count_requests(struct xenvif *vif,
862 struct xen_netif_tx_request *first,
863 struct xen_netif_tx_request *txp,
864 int work_to_do)
865{
866 RING_IDX cons = vif->tx.req_cons;
867 int frags = 0;
868
869 if (!(first->flags & XEN_NETTXF_more_data))
870 return 0;
871
872 do {
873 if (frags >= work_to_do) {
874 netdev_dbg(vif->dev, "Need more frags\n");
875 return -frags;
876 }
877
878 if (unlikely(frags >= MAX_SKB_FRAGS)) {
879 netdev_dbg(vif->dev, "Too many frags\n");
880 return -frags;
881 }
882
883 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + frags),
884 sizeof(*txp));
885 if (txp->size > first->size) {
886 netdev_dbg(vif->dev, "Frags galore\n");
887 return -frags;
888 }
889
890 first->size -= txp->size;
891 frags++;
892
893 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
894 netdev_dbg(vif->dev, "txp->offset: %x, size: %u\n",
895 txp->offset, txp->size);
896 return -frags;
897 }
898 } while ((txp++)->flags & XEN_NETTXF_more_data);
899 return frags;
900}
901
902static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
903 struct sk_buff *skb,
ea066ad1 904 u16 pending_idx)
f942dc25
IC
905{
906 struct page *page;
907 page = alloc_page(GFP_KERNEL|__GFP_COLD);
908 if (!page)
909 return NULL;
910 set_page_ext(page, netbk, pending_idx);
911 netbk->mmap_pages[pending_idx] = page;
912 return page;
913}
914
915static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
916 struct xenvif *vif,
917 struct sk_buff *skb,
918 struct xen_netif_tx_request *txp,
919 struct gnttab_copy *gop)
920{
921 struct skb_shared_info *shinfo = skb_shinfo(skb);
922 skb_frag_t *frags = shinfo->frags;
ea066ad1 923 u16 pending_idx = *((u16 *)skb->data);
f942dc25
IC
924 int i, start;
925
926 /* Skip first skb fragment if it is on same page as header fragment. */
ea066ad1 927 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
f942dc25
IC
928
929 for (i = start; i < shinfo->nr_frags; i++, txp++) {
930 struct page *page;
931 pending_ring_idx_t index;
932 struct pending_tx_info *pending_tx_info =
933 netbk->pending_tx_info;
934
935 index = pending_index(netbk->pending_cons++);
936 pending_idx = netbk->pending_ring[index];
937 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
938 if (!page)
939 return NULL;
940
f942dc25
IC
941 gop->source.u.ref = txp->gref;
942 gop->source.domid = vif->domid;
943 gop->source.offset = txp->offset;
944
945 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
946 gop->dest.domid = DOMID_SELF;
947 gop->dest.offset = txp->offset;
948
949 gop->len = txp->size;
950 gop->flags = GNTCOPY_source_gref;
951
952 gop++;
953
954 memcpy(&pending_tx_info[pending_idx].req, txp, sizeof(*txp));
955 xenvif_get(vif);
956 pending_tx_info[pending_idx].vif = vif;
ea066ad1 957 frag_set_pending_idx(&frags[i], pending_idx);
f942dc25
IC
958 }
959
960 return gop;
961}
962
963static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
964 struct sk_buff *skb,
965 struct gnttab_copy **gopp)
966{
967 struct gnttab_copy *gop = *gopp;
ea066ad1 968 u16 pending_idx = *((u16 *)skb->data);
f942dc25
IC
969 struct pending_tx_info *pending_tx_info = netbk->pending_tx_info;
970 struct xenvif *vif = pending_tx_info[pending_idx].vif;
971 struct xen_netif_tx_request *txp;
972 struct skb_shared_info *shinfo = skb_shinfo(skb);
973 int nr_frags = shinfo->nr_frags;
974 int i, err, start;
975
976 /* Check status of header. */
977 err = gop->status;
978 if (unlikely(err)) {
979 pending_ring_idx_t index;
980 index = pending_index(netbk->pending_prod++);
981 txp = &pending_tx_info[pending_idx].req;
982 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
983 netbk->pending_ring[index] = pending_idx;
984 xenvif_put(vif);
985 }
986
987 /* Skip first skb fragment if it is on same page as header fragment. */
ea066ad1 988 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
f942dc25
IC
989
990 for (i = start; i < nr_frags; i++) {
991 int j, newerr;
992 pending_ring_idx_t index;
993
ea066ad1 994 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
f942dc25
IC
995
996 /* Check error status: if okay then remember grant handle. */
997 newerr = (++gop)->status;
998 if (likely(!newerr)) {
999 /* Had a previous error? Invalidate this fragment. */
1000 if (unlikely(err))
1001 xen_netbk_idx_release(netbk, pending_idx);
1002 continue;
1003 }
1004
1005 /* Error on this fragment: respond to client with an error. */
1006 txp = &netbk->pending_tx_info[pending_idx].req;
1007 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
1008 index = pending_index(netbk->pending_prod++);
1009 netbk->pending_ring[index] = pending_idx;
1010 xenvif_put(vif);
1011
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);
1018 xen_netbk_idx_release(netbk, pending_idx);
1019 for (j = start; j < i; j++) {
5ccb3ea7 1020 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
f942dc25
IC
1021 xen_netbk_idx_release(netbk, pending_idx);
1022 }
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;
ea066ad1
IC
1041 struct page *page;
1042 u16 pending_idx;
f942dc25 1043
ea066ad1 1044 pending_idx = frag_get_pending_idx(frag);
f942dc25
IC
1045
1046 txp = &netbk->pending_tx_info[pending_idx].req;
ea066ad1
IC
1047 page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
1048 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
f942dc25
IC
1049 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]);
1055 xen_netbk_idx_release(netbk, pending_idx);
1056 }
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)) {
1068 netdev_dbg(vif->dev, "Missing extra info\n");
1069 return -EBADR;
1070 }
1071
1072 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1073 sizeof(extra));
1074 if (unlikely(!extra.type ||
1075 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1076 vif->tx.req_cons = ++cons;
1077 netdev_dbg(vif->dev,
1078 "Invalid extra type: %d\n", extra.type);
1079 return -EINVAL;
1080 }
1081
1082 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1083 vif->tx.req_cons = ++cons;
1084 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1085
1086 return work_to_do;
1087}
1088
1089static int netbk_set_skb_gso(struct xenvif *vif,
1090 struct sk_buff *skb,
1091 struct xen_netif_extra_info *gso)
1092{
1093 if (!gso->u.gso.size) {
1094 netdev_dbg(vif->dev, "GSO size must not be zero.\n");
1095 return -EINVAL;
1096 }
1097
1098 /* Currently only TCPv4 S.O. is supported. */
1099 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1100 netdev_dbg(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1101 return -EINVAL;
1102 }
1103
1104 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1105 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1106
1107 /* Header must be checked, and gso_segs computed. */
1108 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1109 skb_shinfo(skb)->gso_segs = 0;
1110
1111 return 0;
1112}
1113
1114static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1115{
1116 struct iphdr *iph;
1117 unsigned char *th;
1118 int err = -EPROTO;
1119 int recalculate_partial_csum = 0;
1120
1121 /*
1122 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1123 * peers can fail to set NETRXF_csum_blank when sending a GSO
1124 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1125 * recalculate the partial checksum.
1126 */
1127 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1128 vif->rx_gso_checksum_fixup++;
1129 skb->ip_summed = CHECKSUM_PARTIAL;
1130 recalculate_partial_csum = 1;
1131 }
1132
1133 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1134 if (skb->ip_summed != CHECKSUM_PARTIAL)
1135 return 0;
1136
1137 if (skb->protocol != htons(ETH_P_IP))
1138 goto out;
1139
1140 iph = (void *)skb->data;
1141 th = skb->data + 4 * iph->ihl;
1142 if (th >= skb_tail_pointer(skb))
1143 goto out;
1144
1145 skb->csum_start = th - skb->head;
1146 switch (iph->protocol) {
1147 case IPPROTO_TCP:
1148 skb->csum_offset = offsetof(struct tcphdr, check);
1149
1150 if (recalculate_partial_csum) {
1151 struct tcphdr *tcph = (struct tcphdr *)th;
1152 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1153 skb->len - iph->ihl*4,
1154 IPPROTO_TCP, 0);
1155 }
1156 break;
1157 case IPPROTO_UDP:
1158 skb->csum_offset = offsetof(struct udphdr, check);
1159
1160 if (recalculate_partial_csum) {
1161 struct udphdr *udph = (struct udphdr *)th;
1162 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1163 skb->len - iph->ihl*4,
1164 IPPROTO_UDP, 0);
1165 }
1166 break;
1167 default:
1168 if (net_ratelimit())
1169 netdev_err(vif->dev,
1170 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1171 iph->protocol);
1172 goto out;
1173 }
1174
1175 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
1176 goto out;
1177
1178 err = 0;
1179
1180out:
1181 return err;
1182}
1183
1184static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1185{
1186 unsigned long now = jiffies;
1187 unsigned long next_credit =
1188 vif->credit_timeout.expires +
1189 msecs_to_jiffies(vif->credit_usec / 1000);
1190
1191 /* Timer could already be pending in rare cases. */
1192 if (timer_pending(&vif->credit_timeout))
1193 return true;
1194
1195 /* Passed the point where we can replenish credit? */
1196 if (time_after_eq(now, next_credit)) {
1197 vif->credit_timeout.expires = now;
1198 tx_add_credit(vif);
1199 }
1200
1201 /* Still too big to send right now? Set a callback. */
1202 if (size > vif->remaining_credit) {
1203 vif->credit_timeout.data =
1204 (unsigned long)vif;
1205 vif->credit_timeout.function =
1206 tx_credit_callback;
1207 mod_timer(&vif->credit_timeout,
1208 next_credit);
1209
1210 return true;
1211 }
1212
1213 return false;
1214}
1215
1216static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
1217{
1218 struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
1219 struct sk_buff *skb;
1220 int ret;
1221
1222 while (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1223 !list_empty(&netbk->net_schedule_list)) {
1224 struct xenvif *vif;
1225 struct xen_netif_tx_request txreq;
1226 struct xen_netif_tx_request txfrags[MAX_SKB_FRAGS];
1227 struct page *page;
1228 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1229 u16 pending_idx;
1230 RING_IDX idx;
1231 int work_to_do;
1232 unsigned int data_len;
1233 pending_ring_idx_t index;
1234
1235 /* Get a netif from the list with work to do. */
1236 vif = poll_net_schedule_list(netbk);
1237 if (!vif)
1238 continue;
1239
1240 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1241 if (!work_to_do) {
1242 xenvif_put(vif);
1243 continue;
1244 }
1245
1246 idx = vif->tx.req_cons;
1247 rmb(); /* Ensure that we see the request before we copy it. */
1248 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1249
1250 /* Credit-based scheduling. */
1251 if (txreq.size > vif->remaining_credit &&
1252 tx_credit_exceeded(vif, txreq.size)) {
1253 xenvif_put(vif);
1254 continue;
1255 }
1256
1257 vif->remaining_credit -= txreq.size;
1258
1259 work_to_do--;
1260 vif->tx.req_cons = ++idx;
1261
1262 memset(extras, 0, sizeof(extras));
1263 if (txreq.flags & XEN_NETTXF_extra_info) {
1264 work_to_do = xen_netbk_get_extras(vif, extras,
1265 work_to_do);
1266 idx = vif->tx.req_cons;
1267 if (unlikely(work_to_do < 0)) {
1268 netbk_tx_err(vif, &txreq, idx);
1269 continue;
1270 }
1271 }
1272
1273 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
1274 if (unlikely(ret < 0)) {
1275 netbk_tx_err(vif, &txreq, idx - ret);
1276 continue;
1277 }
1278 idx += ret;
1279
1280 if (unlikely(txreq.size < ETH_HLEN)) {
1281 netdev_dbg(vif->dev,
1282 "Bad packet size: %d\n", txreq.size);
1283 netbk_tx_err(vif, &txreq, idx);
1284 continue;
1285 }
1286
1287 /* No crossing a page as the payload mustn't fragment. */
1288 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1289 netdev_dbg(vif->dev,
1290 "txreq.offset: %x, size: %u, end: %lu\n",
1291 txreq.offset, txreq.size,
1292 (txreq.offset&~PAGE_MASK) + txreq.size);
1293 netbk_tx_err(vif, &txreq, idx);
1294 continue;
1295 }
1296
1297 index = pending_index(netbk->pending_cons);
1298 pending_idx = netbk->pending_ring[index];
1299
1300 data_len = (txreq.size > PKT_PROT_LEN &&
1301 ret < MAX_SKB_FRAGS) ?
1302 PKT_PROT_LEN : txreq.size;
1303
1304 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1305 GFP_ATOMIC | __GFP_NOWARN);
1306 if (unlikely(skb == NULL)) {
1307 netdev_dbg(vif->dev,
1308 "Can't allocate a skb in start_xmit.\n");
1309 netbk_tx_err(vif, &txreq, idx);
1310 break;
1311 }
1312
1313 /* Packets passed to netif_rx() must have some headroom. */
1314 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1315
1316 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1317 struct xen_netif_extra_info *gso;
1318 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1319
1320 if (netbk_set_skb_gso(vif, skb, gso)) {
1321 kfree_skb(skb);
1322 netbk_tx_err(vif, &txreq, idx);
1323 continue;
1324 }
1325 }
1326
1327 /* XXX could copy straight to head */
1328 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
1329 if (!page) {
1330 kfree_skb(skb);
1331 netbk_tx_err(vif, &txreq, idx);
1332 continue;
1333 }
1334
f942dc25
IC
1335 gop->source.u.ref = txreq.gref;
1336 gop->source.domid = vif->domid;
1337 gop->source.offset = txreq.offset;
1338
1339 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1340 gop->dest.domid = DOMID_SELF;
1341 gop->dest.offset = txreq.offset;
1342
1343 gop->len = txreq.size;
1344 gop->flags = GNTCOPY_source_gref;
1345
1346 gop++;
1347
1348 memcpy(&netbk->pending_tx_info[pending_idx].req,
1349 &txreq, sizeof(txreq));
1350 netbk->pending_tx_info[pending_idx].vif = vif;
1351 *((u16 *)skb->data) = pending_idx;
1352
1353 __skb_put(skb, data_len);
1354
1355 skb_shinfo(skb)->nr_frags = ret;
1356 if (data_len < txreq.size) {
1357 skb_shinfo(skb)->nr_frags++;
ea066ad1
IC
1358 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1359 pending_idx);
f942dc25 1360 } else {
ea066ad1
IC
1361 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1362 INVALID_PENDING_IDX);
f942dc25
IC
1363 }
1364
f942dc25
IC
1365 netbk->pending_cons++;
1366
1367 request_gop = xen_netbk_get_requests(netbk, vif,
1368 skb, txfrags, gop);
1369 if (request_gop == NULL) {
1370 kfree_skb(skb);
1371 netbk_tx_err(vif, &txreq, idx);
1372 continue;
1373 }
1374 gop = request_gop;
1375
1e0b6eac
AL
1376 __skb_queue_tail(&netbk->tx_queue, skb);
1377
f942dc25
IC
1378 vif->tx.req_cons = idx;
1379 xen_netbk_check_rx_xenvif(vif);
1380
1381 if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
1382 break;
1383 }
1384
1385 return gop - netbk->tx_copy_ops;
1386}
1387
1388static void xen_netbk_tx_submit(struct xen_netbk *netbk)
1389{
1390 struct gnttab_copy *gop = netbk->tx_copy_ops;
1391 struct sk_buff *skb;
1392
1393 while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
1394 struct xen_netif_tx_request *txp;
1395 struct xenvif *vif;
1396 u16 pending_idx;
1397 unsigned data_len;
1398
1399 pending_idx = *((u16 *)skb->data);
1400 vif = netbk->pending_tx_info[pending_idx].vif;
1401 txp = &netbk->pending_tx_info[pending_idx].req;
1402
1403 /* Check the remap error code. */
1404 if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1405 netdev_dbg(vif->dev, "netback grant failed.\n");
1406 skb_shinfo(skb)->nr_frags = 0;
1407 kfree_skb(skb);
1408 continue;
1409 }
1410
1411 data_len = skb->len;
1412 memcpy(skb->data,
1413 (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
1414 data_len);
1415 if (data_len < txp->size) {
1416 /* Append the packet payload as a fragment. */
1417 txp->offset += data_len;
1418 txp->size -= data_len;
1419 } else {
1420 /* Schedule a response immediately. */
1421 xen_netbk_idx_release(netbk, pending_idx);
1422 }
1423
1424 if (txp->flags & XEN_NETTXF_csum_blank)
1425 skb->ip_summed = CHECKSUM_PARTIAL;
1426 else if (txp->flags & XEN_NETTXF_data_validated)
1427 skb->ip_summed = CHECKSUM_UNNECESSARY;
1428
1429 xen_netbk_fill_frags(netbk, skb);
1430
1431 /*
1432 * If the initial fragment was < PKT_PROT_LEN then
1433 * pull through some bytes from the other fragments to
1434 * increase the linear region to PKT_PROT_LEN bytes.
1435 */
1436 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1437 int target = min_t(int, skb->len, PKT_PROT_LEN);
1438 __pskb_pull_tail(skb, target - skb_headlen(skb));
1439 }
1440
1441 skb->dev = vif->dev;
1442 skb->protocol = eth_type_trans(skb, skb->dev);
1443
1444 if (checksum_setup(vif, skb)) {
1445 netdev_dbg(vif->dev,
1446 "Can't setup checksum in net_tx_action\n");
1447 kfree_skb(skb);
1448 continue;
1449 }
1450
1451 vif->dev->stats.rx_bytes += skb->len;
1452 vif->dev->stats.rx_packets++;
1453
1454 xenvif_receive_skb(vif, skb);
1455 }
1456}
1457
1458/* Called after netfront has transmitted */
1459static void xen_netbk_tx_action(struct xen_netbk *netbk)
1460{
1461 unsigned nr_gops;
f942dc25
IC
1462
1463 nr_gops = xen_netbk_tx_build_gops(netbk);
1464
1465 if (nr_gops == 0)
1466 return;
f942dc25 1467
c571898f 1468 gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
f942dc25 1469
c571898f 1470 xen_netbk_tx_submit(netbk);
f942dc25
IC
1471}
1472
1473static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx)
1474{
1475 struct xenvif *vif;
1476 struct pending_tx_info *pending_tx_info;
1477 pending_ring_idx_t index;
1478
1479 /* Already complete? */
1480 if (netbk->mmap_pages[pending_idx] == NULL)
1481 return;
1482
1483 pending_tx_info = &netbk->pending_tx_info[pending_idx];
1484
1485 vif = pending_tx_info->vif;
1486
1487 make_tx_response(vif, &pending_tx_info->req, XEN_NETIF_RSP_OKAY);
1488
1489 index = pending_index(netbk->pending_prod++);
1490 netbk->pending_ring[index] = pending_idx;
1491
1492 xenvif_put(vif);
1493
1494 netbk->mmap_pages[pending_idx]->mapping = 0;
1495 put_page(netbk->mmap_pages[pending_idx]);
1496 netbk->mmap_pages[pending_idx] = NULL;
1497}
1498
1499static void make_tx_response(struct xenvif *vif,
1500 struct xen_netif_tx_request *txp,
1501 s8 st)
1502{
1503 RING_IDX i = vif->tx.rsp_prod_pvt;
1504 struct xen_netif_tx_response *resp;
1505 int notify;
1506
1507 resp = RING_GET_RESPONSE(&vif->tx, i);
1508 resp->id = txp->id;
1509 resp->status = st;
1510
1511 if (txp->flags & XEN_NETTXF_extra_info)
1512 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1513
1514 vif->tx.rsp_prod_pvt = ++i;
1515 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1516 if (notify)
1517 notify_remote_via_irq(vif->irq);
1518}
1519
1520static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1521 u16 id,
1522 s8 st,
1523 u16 offset,
1524 u16 size,
1525 u16 flags)
1526{
1527 RING_IDX i = vif->rx.rsp_prod_pvt;
1528 struct xen_netif_rx_response *resp;
1529
1530 resp = RING_GET_RESPONSE(&vif->rx, i);
1531 resp->offset = offset;
1532 resp->flags = flags;
1533 resp->id = id;
1534 resp->status = (s16)size;
1535 if (st < 0)
1536 resp->status = (s16)st;
1537
1538 vif->rx.rsp_prod_pvt = ++i;
1539
1540 return resp;
1541}
1542
1543static inline int rx_work_todo(struct xen_netbk *netbk)
1544{
1545 return !skb_queue_empty(&netbk->rx_queue);
1546}
1547
1548static inline int tx_work_todo(struct xen_netbk *netbk)
1549{
1550
1551 if (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1552 !list_empty(&netbk->net_schedule_list))
1553 return 1;
1554
1555 return 0;
1556}
1557
1558static int xen_netbk_kthread(void *data)
1559{
1560 struct xen_netbk *netbk = data;
1561 while (!kthread_should_stop()) {
1562 wait_event_interruptible(netbk->wq,
1563 rx_work_todo(netbk) ||
1564 tx_work_todo(netbk) ||
1565 kthread_should_stop());
1566 cond_resched();
1567
1568 if (kthread_should_stop())
1569 break;
1570
1571 if (rx_work_todo(netbk))
1572 xen_netbk_rx_action(netbk);
1573
1574 if (tx_work_todo(netbk))
1575 xen_netbk_tx_action(netbk);
1576 }
1577
1578 return 0;
1579}
1580
1581void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
1582{
c9d63699
DV
1583 if (vif->tx.sring)
1584 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1585 vif->tx.sring);
1586 if (vif->rx.sring)
1587 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1588 vif->rx.sring);
f942dc25
IC
1589}
1590
1591int xen_netbk_map_frontend_rings(struct xenvif *vif,
1592 grant_ref_t tx_ring_ref,
1593 grant_ref_t rx_ring_ref)
1594{
c9d63699 1595 void *addr;
f942dc25
IC
1596 struct xen_netif_tx_sring *txs;
1597 struct xen_netif_rx_sring *rxs;
1598
1599 int err = -ENOMEM;
1600
c9d63699
DV
1601 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1602 tx_ring_ref, &addr);
1603 if (err)
f942dc25
IC
1604 goto err;
1605
c9d63699 1606 txs = (struct xen_netif_tx_sring *)addr;
f942dc25
IC
1607 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1608
c9d63699
DV
1609 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1610 rx_ring_ref, &addr);
1611 if (err)
f942dc25 1612 goto err;
f942dc25 1613
c9d63699 1614 rxs = (struct xen_netif_rx_sring *)addr;
f942dc25
IC
1615 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1616
c9d63699
DV
1617 vif->rx_req_cons_peek = 0;
1618
f942dc25
IC
1619 return 0;
1620
1621err:
1622 xen_netbk_unmap_frontend_rings(vif);
1623 return err;
1624}
1625
1626static int __init netback_init(void)
1627{
1628 int i;
1629 int rc = 0;
1630 int group;
1631
2a14b244 1632 if (!xen_domain())
f942dc25
IC
1633 return -ENODEV;
1634
1635 xen_netbk_group_nr = num_online_cpus();
1636 xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
e404decb 1637 if (!xen_netbk)
f942dc25 1638 return -ENOMEM;
f942dc25
IC
1639
1640 for (group = 0; group < xen_netbk_group_nr; group++) {
1641 struct xen_netbk *netbk = &xen_netbk[group];
1642 skb_queue_head_init(&netbk->rx_queue);
1643 skb_queue_head_init(&netbk->tx_queue);
1644
1645 init_timer(&netbk->net_timer);
1646 netbk->net_timer.data = (unsigned long)netbk;
1647 netbk->net_timer.function = xen_netbk_alarm;
1648
1649 netbk->pending_cons = 0;
1650 netbk->pending_prod = MAX_PENDING_REQS;
1651 for (i = 0; i < MAX_PENDING_REQS; i++)
1652 netbk->pending_ring[i] = i;
1653
1654 init_waitqueue_head(&netbk->wq);
1655 netbk->task = kthread_create(xen_netbk_kthread,
1656 (void *)netbk,
1657 "netback/%u", group);
1658
1659 if (IS_ERR(netbk->task)) {
6b84bd16 1660 printk(KERN_ALERT "kthread_create() fails at netback\n");
f942dc25
IC
1661 del_timer(&netbk->net_timer);
1662 rc = PTR_ERR(netbk->task);
1663 goto failed_init;
1664 }
1665
1666 kthread_bind(netbk->task, group);
1667
1668 INIT_LIST_HEAD(&netbk->net_schedule_list);
1669
1670 spin_lock_init(&netbk->net_schedule_list_lock);
1671
1672 atomic_set(&netbk->netfront_count, 0);
1673
1674 wake_up_process(netbk->task);
1675 }
1676
1677 rc = xenvif_xenbus_init();
1678 if (rc)
1679 goto failed_init;
1680
1681 return 0;
1682
1683failed_init:
1684 while (--group >= 0) {
1685 struct xen_netbk *netbk = &xen_netbk[group];
1686 for (i = 0; i < MAX_PENDING_REQS; i++) {
1687 if (netbk->mmap_pages[i])
1688 __free_page(netbk->mmap_pages[i]);
1689 }
1690 del_timer(&netbk->net_timer);
1691 kthread_stop(netbk->task);
1692 }
1693 vfree(xen_netbk);
1694 return rc;
1695
1696}
1697
1698module_init(netback_init);
1699
1700MODULE_LICENSE("Dual BSD/GPL");
f984cec6 1701MODULE_ALIAS("xen-backend:vif");