]> Git Repo - linux.git/blob - drivers/net/xen-netback/netback.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[linux.git] / drivers / net / xen-netback / netback.c
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 #include <linux/highmem.h>
41
42 #include <net/tcp.h>
43
44 #include <xen/xen.h>
45 #include <xen/events.h>
46 #include <xen/interface/memory.h>
47
48 #include <asm/xen/hypercall.h>
49 #include <asm/xen/page.h>
50
51 /* Provide an option to disable split event channels at load time as
52  * event channels are limited resource. Split event channels are
53  * enabled by default.
54  */
55 bool separate_tx_rx_irq = 1;
56 module_param(separate_tx_rx_irq, bool, 0644);
57
58 /* When guest ring is filled up, qdisc queues the packets for us, but we have
59  * to timeout them, otherwise other guests' packets can get stuck there
60  */
61 unsigned int rx_drain_timeout_msecs = 10000;
62 module_param(rx_drain_timeout_msecs, uint, 0444);
63 unsigned int rx_drain_timeout_jiffies;
64
65 unsigned int xenvif_max_queues;
66 module_param_named(max_queues, xenvif_max_queues, uint, 0644);
67 MODULE_PARM_DESC(max_queues,
68                  "Maximum number of queues per virtual interface");
69
70 /*
71  * This is the maximum slots a skb can have. If a guest sends a skb
72  * which exceeds this limit it is considered malicious.
73  */
74 #define FATAL_SKB_SLOTS_DEFAULT 20
75 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
76 module_param(fatal_skb_slots, uint, 0444);
77
78 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
79                                u8 status);
80
81 static void make_tx_response(struct xenvif_queue *queue,
82                              struct xen_netif_tx_request *txp,
83                              s8       st);
84
85 static inline int tx_work_todo(struct xenvif_queue *queue);
86 static inline int rx_work_todo(struct xenvif_queue *queue);
87
88 static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
89                                              u16      id,
90                                              s8       st,
91                                              u16      offset,
92                                              u16      size,
93                                              u16      flags);
94
95 static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
96                                        u16 idx)
97 {
98         return page_to_pfn(queue->mmap_pages[idx]);
99 }
100
101 static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
102                                          u16 idx)
103 {
104         return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
105 }
106
107 #define callback_param(vif, pending_idx) \
108         (vif->pending_tx_info[pending_idx].callback_struct)
109
110 /* Find the containing VIF's structure from a pointer in pending_tx_info array
111  */
112 static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
113 {
114         u16 pending_idx = ubuf->desc;
115         struct pending_tx_info *temp =
116                 container_of(ubuf, struct pending_tx_info, callback_struct);
117         return container_of(temp - pending_idx,
118                             struct xenvif_queue,
119                             pending_tx_info[0]);
120 }
121
122 /* This is a miniumum size for the linear area to avoid lots of
123  * calls to __pskb_pull_tail() as we set up checksum offsets. The
124  * value 128 was chosen as it covers all IPv4 and most likely
125  * IPv6 headers.
126  */
127 #define PKT_PROT_LEN 128
128
129 static u16 frag_get_pending_idx(skb_frag_t *frag)
130 {
131         return (u16)frag->page_offset;
132 }
133
134 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
135 {
136         frag->page_offset = pending_idx;
137 }
138
139 static inline pending_ring_idx_t pending_index(unsigned i)
140 {
141         return i & (MAX_PENDING_REQS-1);
142 }
143
144 bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
145 {
146         RING_IDX prod, cons;
147
148         do {
149                 prod = queue->rx.sring->req_prod;
150                 cons = queue->rx.req_cons;
151
152                 if (prod - cons >= needed)
153                         return true;
154
155                 queue->rx.sring->req_event = prod + 1;
156
157                 /* Make sure event is visible before we check prod
158                  * again.
159                  */
160                 mb();
161         } while (queue->rx.sring->req_prod != prod);
162
163         return false;
164 }
165
166 /*
167  * Returns true if we should start a new receive buffer instead of
168  * adding 'size' bytes to a buffer which currently contains 'offset'
169  * bytes.
170  */
171 static bool start_new_rx_buffer(int offset, unsigned long size, int head)
172 {
173         /* simple case: we have completely filled the current buffer. */
174         if (offset == MAX_BUFFER_OFFSET)
175                 return true;
176
177         /*
178          * complex case: start a fresh buffer if the current frag
179          * would overflow the current buffer but only if:
180          *     (i)   this frag would fit completely in the next buffer
181          * and (ii)  there is already some data in the current buffer
182          * and (iii) this is not the head buffer.
183          *
184          * Where:
185          * - (i) stops us splitting a frag into two copies
186          *   unless the frag is too large for a single buffer.
187          * - (ii) stops us from leaving a buffer pointlessly empty.
188          * - (iii) stops us leaving the first buffer
189          *   empty. Strictly speaking this is already covered
190          *   by (ii) but is explicitly checked because
191          *   netfront relies on the first buffer being
192          *   non-empty and can crash otherwise.
193          *
194          * This means we will effectively linearise small
195          * frags but do not needlessly split large buffers
196          * into multiple copies tend to give large frags their
197          * own buffers as before.
198          */
199         BUG_ON(size > MAX_BUFFER_OFFSET);
200         if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head)
201                 return true;
202
203         return false;
204 }
205
206 struct netrx_pending_operations {
207         unsigned copy_prod, copy_cons;
208         unsigned meta_prod, meta_cons;
209         struct gnttab_copy *copy;
210         struct xenvif_rx_meta *meta;
211         int copy_off;
212         grant_ref_t copy_gref;
213 };
214
215 static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
216                                                  struct netrx_pending_operations *npo)
217 {
218         struct xenvif_rx_meta *meta;
219         struct xen_netif_rx_request *req;
220
221         req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
222
223         meta = npo->meta + npo->meta_prod++;
224         meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
225         meta->gso_size = 0;
226         meta->size = 0;
227         meta->id = req->id;
228
229         npo->copy_off = 0;
230         npo->copy_gref = req->gref;
231
232         return meta;
233 }
234
235 /*
236  * Set up the grant operations for this fragment. If it's a flipping
237  * interface, we also set up the unmap request from here.
238  */
239 static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
240                                  struct netrx_pending_operations *npo,
241                                  struct page *page, unsigned long size,
242                                  unsigned long offset, int *head,
243                                  struct xenvif_queue *foreign_queue,
244                                  grant_ref_t foreign_gref)
245 {
246         struct gnttab_copy *copy_gop;
247         struct xenvif_rx_meta *meta;
248         unsigned long bytes;
249         int gso_type = XEN_NETIF_GSO_TYPE_NONE;
250
251         /* Data must not cross a page boundary. */
252         BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
253
254         meta = npo->meta + npo->meta_prod - 1;
255
256         /* Skip unused frames from start of page */
257         page += offset >> PAGE_SHIFT;
258         offset &= ~PAGE_MASK;
259
260         while (size > 0) {
261                 BUG_ON(offset >= PAGE_SIZE);
262                 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
263
264                 bytes = PAGE_SIZE - offset;
265
266                 if (bytes > size)
267                         bytes = size;
268
269                 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
270                         /*
271                          * Netfront requires there to be some data in the head
272                          * buffer.
273                          */
274                         BUG_ON(*head);
275
276                         meta = get_next_rx_buffer(queue, npo);
277                 }
278
279                 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
280                         bytes = MAX_BUFFER_OFFSET - npo->copy_off;
281
282                 copy_gop = npo->copy + npo->copy_prod++;
283                 copy_gop->flags = GNTCOPY_dest_gref;
284                 copy_gop->len = bytes;
285
286                 if (foreign_queue) {
287                         copy_gop->source.domid = foreign_queue->vif->domid;
288                         copy_gop->source.u.ref = foreign_gref;
289                         copy_gop->flags |= GNTCOPY_source_gref;
290                 } else {
291                         copy_gop->source.domid = DOMID_SELF;
292                         copy_gop->source.u.gmfn =
293                                 virt_to_mfn(page_address(page));
294                 }
295                 copy_gop->source.offset = offset;
296
297                 copy_gop->dest.domid = queue->vif->domid;
298                 copy_gop->dest.offset = npo->copy_off;
299                 copy_gop->dest.u.ref = npo->copy_gref;
300
301                 npo->copy_off += bytes;
302                 meta->size += bytes;
303
304                 offset += bytes;
305                 size -= bytes;
306
307                 /* Next frame */
308                 if (offset == PAGE_SIZE && size) {
309                         BUG_ON(!PageCompound(page));
310                         page++;
311                         offset = 0;
312                 }
313
314                 /* Leave a gap for the GSO descriptor. */
315                 if (skb_is_gso(skb)) {
316                         if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
317                                 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
318                         else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
319                                 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
320                 }
321
322                 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
323                         queue->rx.req_cons++;
324
325                 *head = 0; /* There must be something in this buffer now. */
326
327         }
328 }
329
330 /*
331  * Find the grant ref for a given frag in a chain of struct ubuf_info's
332  * skb: the skb itself
333  * i: the frag's number
334  * ubuf: a pointer to an element in the chain. It should not be NULL
335  *
336  * Returns a pointer to the element in the chain where the page were found. If
337  * not found, returns NULL.
338  * See the definition of callback_struct in common.h for more details about
339  * the chain.
340  */
341 static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb,
342                                                 const int i,
343                                                 const struct ubuf_info *ubuf)
344 {
345         struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf);
346
347         do {
348                 u16 pending_idx = ubuf->desc;
349
350                 if (skb_shinfo(skb)->frags[i].page.p ==
351                     foreign_queue->mmap_pages[pending_idx])
352                         break;
353                 ubuf = (struct ubuf_info *) ubuf->ctx;
354         } while (ubuf);
355
356         return ubuf;
357 }
358
359 /*
360  * Prepare an SKB to be transmitted to the frontend.
361  *
362  * This function is responsible for allocating grant operations, meta
363  * structures, etc.
364  *
365  * It returns the number of meta structures consumed. The number of
366  * ring slots used is always equal to the number of meta slots used
367  * plus the number of GSO descriptors used. Currently, we use either
368  * zero GSO descriptors (for non-GSO packets) or one descriptor (for
369  * frontend-side LRO).
370  */
371 static int xenvif_gop_skb(struct sk_buff *skb,
372                           struct netrx_pending_operations *npo,
373                           struct xenvif_queue *queue)
374 {
375         struct xenvif *vif = netdev_priv(skb->dev);
376         int nr_frags = skb_shinfo(skb)->nr_frags;
377         int i;
378         struct xen_netif_rx_request *req;
379         struct xenvif_rx_meta *meta;
380         unsigned char *data;
381         int head = 1;
382         int old_meta_prod;
383         int gso_type;
384         const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
385         const struct ubuf_info *const head_ubuf = ubuf;
386
387         old_meta_prod = npo->meta_prod;
388
389         gso_type = XEN_NETIF_GSO_TYPE_NONE;
390         if (skb_is_gso(skb)) {
391                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
392                         gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
393                 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
394                         gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
395         }
396
397         /* Set up a GSO prefix descriptor, if necessary */
398         if ((1 << gso_type) & vif->gso_prefix_mask) {
399                 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
400                 meta = npo->meta + npo->meta_prod++;
401                 meta->gso_type = gso_type;
402                 meta->gso_size = skb_shinfo(skb)->gso_size;
403                 meta->size = 0;
404                 meta->id = req->id;
405         }
406
407         req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
408         meta = npo->meta + npo->meta_prod++;
409
410         if ((1 << gso_type) & vif->gso_mask) {
411                 meta->gso_type = gso_type;
412                 meta->gso_size = skb_shinfo(skb)->gso_size;
413         } else {
414                 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
415                 meta->gso_size = 0;
416         }
417
418         meta->size = 0;
419         meta->id = req->id;
420         npo->copy_off = 0;
421         npo->copy_gref = req->gref;
422
423         data = skb->data;
424         while (data < skb_tail_pointer(skb)) {
425                 unsigned int offset = offset_in_page(data);
426                 unsigned int len = PAGE_SIZE - offset;
427
428                 if (data + len > skb_tail_pointer(skb))
429                         len = skb_tail_pointer(skb) - data;
430
431                 xenvif_gop_frag_copy(queue, skb, npo,
432                                      virt_to_page(data), len, offset, &head,
433                                      NULL,
434                                      0);
435                 data += len;
436         }
437
438         for (i = 0; i < nr_frags; i++) {
439                 /* This variable also signals whether foreign_gref has a real
440                  * value or not.
441                  */
442                 struct xenvif_queue *foreign_queue = NULL;
443                 grant_ref_t foreign_gref;
444
445                 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
446                         (ubuf->callback == &xenvif_zerocopy_callback)) {
447                         const struct ubuf_info *const startpoint = ubuf;
448
449                         /* Ideally ubuf points to the chain element which
450                          * belongs to this frag. Or if frags were removed from
451                          * the beginning, then shortly before it.
452                          */
453                         ubuf = xenvif_find_gref(skb, i, ubuf);
454
455                         /* Try again from the beginning of the list, if we
456                          * haven't tried from there. This only makes sense in
457                          * the unlikely event of reordering the original frags.
458                          * For injected local pages it's an unnecessary second
459                          * run.
460                          */
461                         if (unlikely(!ubuf) && startpoint != head_ubuf)
462                                 ubuf = xenvif_find_gref(skb, i, head_ubuf);
463
464                         if (likely(ubuf)) {
465                                 u16 pending_idx = ubuf->desc;
466
467                                 foreign_queue = ubuf_to_queue(ubuf);
468                                 foreign_gref =
469                                         foreign_queue->pending_tx_info[pending_idx].req.gref;
470                                 /* Just a safety measure. If this was the last
471                                  * element on the list, the for loop will
472                                  * iterate again if a local page were added to
473                                  * the end. Using head_ubuf here prevents the
474                                  * second search on the chain. Or the original
475                                  * frags changed order, but that's less likely.
476                                  * In any way, ubuf shouldn't be NULL.
477                                  */
478                                 ubuf = ubuf->ctx ?
479                                         (struct ubuf_info *) ubuf->ctx :
480                                         head_ubuf;
481                         } else
482                                 /* This frag was a local page, added to the
483                                  * array after the skb left netback.
484                                  */
485                                 ubuf = head_ubuf;
486                 }
487                 xenvif_gop_frag_copy(queue, skb, npo,
488                                      skb_frag_page(&skb_shinfo(skb)->frags[i]),
489                                      skb_frag_size(&skb_shinfo(skb)->frags[i]),
490                                      skb_shinfo(skb)->frags[i].page_offset,
491                                      &head,
492                                      foreign_queue,
493                                      foreign_queue ? foreign_gref : UINT_MAX);
494         }
495
496         return npo->meta_prod - old_meta_prod;
497 }
498
499 /*
500  * This is a twin to xenvif_gop_skb.  Assume that xenvif_gop_skb was
501  * used to set up the operations on the top of
502  * netrx_pending_operations, which have since been done.  Check that
503  * they didn't give any errors and advance over them.
504  */
505 static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
506                             struct netrx_pending_operations *npo)
507 {
508         struct gnttab_copy     *copy_op;
509         int status = XEN_NETIF_RSP_OKAY;
510         int i;
511
512         for (i = 0; i < nr_meta_slots; i++) {
513                 copy_op = npo->copy + npo->copy_cons++;
514                 if (copy_op->status != GNTST_okay) {
515                         netdev_dbg(vif->dev,
516                                    "Bad status %d from copy to DOM%d.\n",
517                                    copy_op->status, vif->domid);
518                         status = XEN_NETIF_RSP_ERROR;
519                 }
520         }
521
522         return status;
523 }
524
525 static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
526                                       struct xenvif_rx_meta *meta,
527                                       int nr_meta_slots)
528 {
529         int i;
530         unsigned long offset;
531
532         /* No fragments used */
533         if (nr_meta_slots <= 1)
534                 return;
535
536         nr_meta_slots--;
537
538         for (i = 0; i < nr_meta_slots; i++) {
539                 int flags;
540                 if (i == nr_meta_slots - 1)
541                         flags = 0;
542                 else
543                         flags = XEN_NETRXF_more_data;
544
545                 offset = 0;
546                 make_rx_response(queue, meta[i].id, status, offset,
547                                  meta[i].size, flags);
548         }
549 }
550
551 struct xenvif_rx_cb {
552         int meta_slots_used;
553 };
554
555 #define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
556
557 void xenvif_kick_thread(struct xenvif_queue *queue)
558 {
559         wake_up(&queue->wq);
560 }
561
562 static void xenvif_rx_action(struct xenvif_queue *queue)
563 {
564         s8 status;
565         u16 flags;
566         struct xen_netif_rx_response *resp;
567         struct sk_buff_head rxq;
568         struct sk_buff *skb;
569         LIST_HEAD(notify);
570         int ret;
571         unsigned long offset;
572         bool need_to_notify = false;
573
574         struct netrx_pending_operations npo = {
575                 .copy  = queue->grant_copy_op,
576                 .meta  = queue->meta,
577         };
578
579         skb_queue_head_init(&rxq);
580
581         while ((skb = skb_dequeue(&queue->rx_queue)) != NULL) {
582                 RING_IDX max_slots_needed;
583                 RING_IDX old_req_cons;
584                 RING_IDX ring_slots_used;
585                 int i;
586
587                 /* We need a cheap worse case estimate for the number of
588                  * slots we'll use.
589                  */
590
591                 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
592                                                 skb_headlen(skb),
593                                                 PAGE_SIZE);
594                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
595                         unsigned int size;
596                         unsigned int offset;
597
598                         size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
599                         offset = skb_shinfo(skb)->frags[i].page_offset;
600
601                         /* For a worse-case estimate we need to factor in
602                          * the fragment page offset as this will affect the
603                          * number of times xenvif_gop_frag_copy() will
604                          * call start_new_rx_buffer().
605                          */
606                         max_slots_needed += DIV_ROUND_UP(offset + size,
607                                                          PAGE_SIZE);
608                 }
609
610                 /* To avoid the estimate becoming too pessimal for some
611                  * frontends that limit posted rx requests, cap the estimate
612                  * at MAX_SKB_FRAGS.
613                  */
614                 if (max_slots_needed > MAX_SKB_FRAGS)
615                         max_slots_needed = MAX_SKB_FRAGS;
616
617                 /* We may need one more slot for GSO metadata */
618                 if (skb_is_gso(skb) &&
619                    (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
620                     skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
621                         max_slots_needed++;
622
623                 /* If the skb may not fit then bail out now */
624                 if (!xenvif_rx_ring_slots_available(queue, max_slots_needed)) {
625                         skb_queue_head(&queue->rx_queue, skb);
626                         need_to_notify = true;
627                         queue->rx_last_skb_slots = max_slots_needed;
628                         break;
629                 } else
630                         queue->rx_last_skb_slots = 0;
631
632                 old_req_cons = queue->rx.req_cons;
633                 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
634                 ring_slots_used = queue->rx.req_cons - old_req_cons;
635
636                 BUG_ON(ring_slots_used > max_slots_needed);
637
638                 __skb_queue_tail(&rxq, skb);
639         }
640
641         BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
642
643         if (!npo.copy_prod)
644                 goto done;
645
646         BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
647         gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
648
649         while ((skb = __skb_dequeue(&rxq)) != NULL) {
650
651                 if ((1 << queue->meta[npo.meta_cons].gso_type) &
652                     queue->vif->gso_prefix_mask) {
653                         resp = RING_GET_RESPONSE(&queue->rx,
654                                                  queue->rx.rsp_prod_pvt++);
655
656                         resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
657
658                         resp->offset = queue->meta[npo.meta_cons].gso_size;
659                         resp->id = queue->meta[npo.meta_cons].id;
660                         resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
661
662                         npo.meta_cons++;
663                         XENVIF_RX_CB(skb)->meta_slots_used--;
664                 }
665
666
667                 queue->stats.tx_bytes += skb->len;
668                 queue->stats.tx_packets++;
669
670                 status = xenvif_check_gop(queue->vif,
671                                           XENVIF_RX_CB(skb)->meta_slots_used,
672                                           &npo);
673
674                 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
675                         flags = 0;
676                 else
677                         flags = XEN_NETRXF_more_data;
678
679                 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
680                         flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
681                 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
682                         /* remote but checksummed. */
683                         flags |= XEN_NETRXF_data_validated;
684
685                 offset = 0;
686                 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
687                                         status, offset,
688                                         queue->meta[npo.meta_cons].size,
689                                         flags);
690
691                 if ((1 << queue->meta[npo.meta_cons].gso_type) &
692                     queue->vif->gso_mask) {
693                         struct xen_netif_extra_info *gso =
694                                 (struct xen_netif_extra_info *)
695                                 RING_GET_RESPONSE(&queue->rx,
696                                                   queue->rx.rsp_prod_pvt++);
697
698                         resp->flags |= XEN_NETRXF_extra_info;
699
700                         gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
701                         gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
702                         gso->u.gso.pad = 0;
703                         gso->u.gso.features = 0;
704
705                         gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
706                         gso->flags = 0;
707                 }
708
709                 xenvif_add_frag_responses(queue, status,
710                                           queue->meta + npo.meta_cons + 1,
711                                           XENVIF_RX_CB(skb)->meta_slots_used);
712
713                 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
714
715                 need_to_notify |= !!ret;
716
717                 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
718                 dev_kfree_skb(skb);
719         }
720
721 done:
722         if (need_to_notify)
723                 notify_remote_via_irq(queue->rx_irq);
724 }
725
726 void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
727 {
728         int more_to_do;
729
730         RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
731
732         if (more_to_do)
733                 napi_schedule(&queue->napi);
734 }
735
736 static void tx_add_credit(struct xenvif_queue *queue)
737 {
738         unsigned long max_burst, max_credit;
739
740         /*
741          * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
742          * Otherwise the interface can seize up due to insufficient credit.
743          */
744         max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
745         max_burst = min(max_burst, 131072UL);
746         max_burst = max(max_burst, queue->credit_bytes);
747
748         /* Take care that adding a new chunk of credit doesn't wrap to zero. */
749         max_credit = queue->remaining_credit + queue->credit_bytes;
750         if (max_credit < queue->remaining_credit)
751                 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
752
753         queue->remaining_credit = min(max_credit, max_burst);
754 }
755
756 static void tx_credit_callback(unsigned long data)
757 {
758         struct xenvif_queue *queue = (struct xenvif_queue *)data;
759         tx_add_credit(queue);
760         xenvif_napi_schedule_or_enable_events(queue);
761 }
762
763 static void xenvif_tx_err(struct xenvif_queue *queue,
764                           struct xen_netif_tx_request *txp, RING_IDX end)
765 {
766         RING_IDX cons = queue->tx.req_cons;
767         unsigned long flags;
768
769         do {
770                 spin_lock_irqsave(&queue->response_lock, flags);
771                 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
772                 spin_unlock_irqrestore(&queue->response_lock, flags);
773                 if (cons == end)
774                         break;
775                 txp = RING_GET_REQUEST(&queue->tx, cons++);
776         } while (1);
777         queue->tx.req_cons = cons;
778 }
779
780 static void xenvif_fatal_tx_err(struct xenvif *vif)
781 {
782         netdev_err(vif->dev, "fatal error; disabling device\n");
783         vif->disabled = true;
784         /* Disable the vif from queue 0's kthread */
785         if (vif->queues)
786                 xenvif_kick_thread(&vif->queues[0]);
787 }
788
789 static int xenvif_count_requests(struct xenvif_queue *queue,
790                                  struct xen_netif_tx_request *first,
791                                  struct xen_netif_tx_request *txp,
792                                  int work_to_do)
793 {
794         RING_IDX cons = queue->tx.req_cons;
795         int slots = 0;
796         int drop_err = 0;
797         int more_data;
798
799         if (!(first->flags & XEN_NETTXF_more_data))
800                 return 0;
801
802         do {
803                 struct xen_netif_tx_request dropped_tx = { 0 };
804
805                 if (slots >= work_to_do) {
806                         netdev_err(queue->vif->dev,
807                                    "Asked for %d slots but exceeds this limit\n",
808                                    work_to_do);
809                         xenvif_fatal_tx_err(queue->vif);
810                         return -ENODATA;
811                 }
812
813                 /* This guest is really using too many slots and
814                  * considered malicious.
815                  */
816                 if (unlikely(slots >= fatal_skb_slots)) {
817                         netdev_err(queue->vif->dev,
818                                    "Malicious frontend using %d slots, threshold %u\n",
819                                    slots, fatal_skb_slots);
820                         xenvif_fatal_tx_err(queue->vif);
821                         return -E2BIG;
822                 }
823
824                 /* Xen network protocol had implicit dependency on
825                  * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
826                  * the historical MAX_SKB_FRAGS value 18 to honor the
827                  * same behavior as before. Any packet using more than
828                  * 18 slots but less than fatal_skb_slots slots is
829                  * dropped
830                  */
831                 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
832                         if (net_ratelimit())
833                                 netdev_dbg(queue->vif->dev,
834                                            "Too many slots (%d) exceeding limit (%d), dropping packet\n",
835                                            slots, XEN_NETBK_LEGACY_SLOTS_MAX);
836                         drop_err = -E2BIG;
837                 }
838
839                 if (drop_err)
840                         txp = &dropped_tx;
841
842                 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
843                        sizeof(*txp));
844
845                 /* If the guest submitted a frame >= 64 KiB then
846                  * first->size overflowed and following slots will
847                  * appear to be larger than the frame.
848                  *
849                  * This cannot be fatal error as there are buggy
850                  * frontends that do this.
851                  *
852                  * Consume all slots and drop the packet.
853                  */
854                 if (!drop_err && txp->size > first->size) {
855                         if (net_ratelimit())
856                                 netdev_dbg(queue->vif->dev,
857                                            "Invalid tx request, slot size %u > remaining size %u\n",
858                                            txp->size, first->size);
859                         drop_err = -EIO;
860                 }
861
862                 first->size -= txp->size;
863                 slots++;
864
865                 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
866                         netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
867                                  txp->offset, txp->size);
868                         xenvif_fatal_tx_err(queue->vif);
869                         return -EINVAL;
870                 }
871
872                 more_data = txp->flags & XEN_NETTXF_more_data;
873
874                 if (!drop_err)
875                         txp++;
876
877         } while (more_data);
878
879         if (drop_err) {
880                 xenvif_tx_err(queue, first, cons + slots);
881                 return drop_err;
882         }
883
884         return slots;
885 }
886
887
888 struct xenvif_tx_cb {
889         u16 pending_idx;
890 };
891
892 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
893
894 static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
895                                           u16 pending_idx,
896                                           struct xen_netif_tx_request *txp,
897                                           struct gnttab_map_grant_ref *mop)
898 {
899         queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
900         gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
901                           GNTMAP_host_map | GNTMAP_readonly,
902                           txp->gref, queue->vif->domid);
903
904         memcpy(&queue->pending_tx_info[pending_idx].req, txp,
905                sizeof(*txp));
906 }
907
908 static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
909 {
910         struct sk_buff *skb =
911                 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
912                           GFP_ATOMIC | __GFP_NOWARN);
913         if (unlikely(skb == NULL))
914                 return NULL;
915
916         /* Packets passed to netif_rx() must have some headroom. */
917         skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
918
919         /* Initialize it here to avoid later surprises */
920         skb_shinfo(skb)->destructor_arg = NULL;
921
922         return skb;
923 }
924
925 static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
926                                                         struct sk_buff *skb,
927                                                         struct xen_netif_tx_request *txp,
928                                                         struct gnttab_map_grant_ref *gop)
929 {
930         struct skb_shared_info *shinfo = skb_shinfo(skb);
931         skb_frag_t *frags = shinfo->frags;
932         u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
933         int start;
934         pending_ring_idx_t index;
935         unsigned int nr_slots, frag_overflow = 0;
936
937         /* At this point shinfo->nr_frags is in fact the number of
938          * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
939          */
940         if (shinfo->nr_frags > MAX_SKB_FRAGS) {
941                 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
942                 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
943                 shinfo->nr_frags = MAX_SKB_FRAGS;
944         }
945         nr_slots = shinfo->nr_frags;
946
947         /* Skip first skb fragment if it is on same page as header fragment. */
948         start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
949
950         for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
951              shinfo->nr_frags++, txp++, gop++) {
952                 index = pending_index(queue->pending_cons++);
953                 pending_idx = queue->pending_ring[index];
954                 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
955                 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
956         }
957
958         if (frag_overflow) {
959                 struct sk_buff *nskb = xenvif_alloc_skb(0);
960                 if (unlikely(nskb == NULL)) {
961                         if (net_ratelimit())
962                                 netdev_err(queue->vif->dev,
963                                            "Can't allocate the frag_list skb.\n");
964                         return NULL;
965                 }
966
967                 shinfo = skb_shinfo(nskb);
968                 frags = shinfo->frags;
969
970                 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
971                      shinfo->nr_frags++, txp++, gop++) {
972                         index = pending_index(queue->pending_cons++);
973                         pending_idx = queue->pending_ring[index];
974                         xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
975                         frag_set_pending_idx(&frags[shinfo->nr_frags],
976                                              pending_idx);
977                 }
978
979                 skb_shinfo(skb)->frag_list = nskb;
980         }
981
982         return gop;
983 }
984
985 static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
986                                            u16 pending_idx,
987                                            grant_handle_t handle)
988 {
989         if (unlikely(queue->grant_tx_handle[pending_idx] !=
990                      NETBACK_INVALID_HANDLE)) {
991                 netdev_err(queue->vif->dev,
992                            "Trying to overwrite active handle! pending_idx: %x\n",
993                            pending_idx);
994                 BUG();
995         }
996         queue->grant_tx_handle[pending_idx] = handle;
997 }
998
999 static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
1000                                              u16 pending_idx)
1001 {
1002         if (unlikely(queue->grant_tx_handle[pending_idx] ==
1003                      NETBACK_INVALID_HANDLE)) {
1004                 netdev_err(queue->vif->dev,
1005                            "Trying to unmap invalid handle! pending_idx: %x\n",
1006                            pending_idx);
1007                 BUG();
1008         }
1009         queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
1010 }
1011
1012 static int xenvif_tx_check_gop(struct xenvif_queue *queue,
1013                                struct sk_buff *skb,
1014                                struct gnttab_map_grant_ref **gopp_map,
1015                                struct gnttab_copy **gopp_copy)
1016 {
1017         struct gnttab_map_grant_ref *gop_map = *gopp_map;
1018         u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
1019         struct skb_shared_info *shinfo = skb_shinfo(skb);
1020         int nr_frags = shinfo->nr_frags;
1021         int i, err;
1022         struct sk_buff *first_skb = NULL;
1023
1024         /* Check status of header. */
1025         err = (*gopp_copy)->status;
1026         (*gopp_copy)++;
1027         if (unlikely(err)) {
1028                 if (net_ratelimit())
1029                         netdev_dbg(queue->vif->dev,
1030                                    "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
1031                                    (*gopp_copy)->status,
1032                                    pending_idx,
1033                                    (*gopp_copy)->source.u.ref);
1034                 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
1035         }
1036
1037 check_frags:
1038         for (i = 0; i < nr_frags; i++, gop_map++) {
1039                 int j, newerr;
1040
1041                 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
1042
1043                 /* Check error status: if okay then remember grant handle. */
1044                 newerr = gop_map->status;
1045
1046                 if (likely(!newerr)) {
1047                         xenvif_grant_handle_set(queue,
1048                                                 pending_idx,
1049                                                 gop_map->handle);
1050                         /* Had a previous error? Invalidate this fragment. */
1051                         if (unlikely(err))
1052                                 xenvif_idx_unmap(queue, pending_idx);
1053                         continue;
1054                 }
1055
1056                 /* Error on this fragment: respond to client with an error. */
1057                 if (net_ratelimit())
1058                         netdev_dbg(queue->vif->dev,
1059                                    "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
1060                                    i,
1061                                    gop_map->status,
1062                                    pending_idx,
1063                                    gop_map->ref);
1064                 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
1065
1066                 /* Not the first error? Preceding frags already invalidated. */
1067                 if (err)
1068                         continue;
1069                 /* First error: invalidate preceding fragments. */
1070                 for (j = 0; j < i; j++) {
1071                         pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1072                         xenvif_idx_unmap(queue, pending_idx);
1073                 }
1074
1075                 /* Remember the error: invalidate all subsequent fragments. */
1076                 err = newerr;
1077         }
1078
1079         if (skb_has_frag_list(skb)) {
1080                 first_skb = skb;
1081                 skb = shinfo->frag_list;
1082                 shinfo = skb_shinfo(skb);
1083                 nr_frags = shinfo->nr_frags;
1084
1085                 goto check_frags;
1086         }
1087
1088         /* There was a mapping error in the frag_list skb. We have to unmap
1089          * the first skb's frags
1090          */
1091         if (first_skb && err) {
1092                 int j;
1093                 shinfo = skb_shinfo(first_skb);
1094                 for (j = 0; j < shinfo->nr_frags; j++) {
1095                         pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1096                         xenvif_idx_unmap(queue, pending_idx);
1097                 }
1098         }
1099
1100         *gopp_map = gop_map;
1101         return err;
1102 }
1103
1104 static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
1105 {
1106         struct skb_shared_info *shinfo = skb_shinfo(skb);
1107         int nr_frags = shinfo->nr_frags;
1108         int i;
1109         u16 prev_pending_idx = INVALID_PENDING_IDX;
1110
1111         for (i = 0; i < nr_frags; i++) {
1112                 skb_frag_t *frag = shinfo->frags + i;
1113                 struct xen_netif_tx_request *txp;
1114                 struct page *page;
1115                 u16 pending_idx;
1116
1117                 pending_idx = frag_get_pending_idx(frag);
1118
1119                 /* If this is not the first frag, chain it to the previous*/
1120                 if (prev_pending_idx == INVALID_PENDING_IDX)
1121                         skb_shinfo(skb)->destructor_arg =
1122                                 &callback_param(queue, pending_idx);
1123                 else
1124                         callback_param(queue, prev_pending_idx).ctx =
1125                                 &callback_param(queue, pending_idx);
1126
1127                 callback_param(queue, pending_idx).ctx = NULL;
1128                 prev_pending_idx = pending_idx;
1129
1130                 txp = &queue->pending_tx_info[pending_idx].req;
1131                 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
1132                 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1133                 skb->len += txp->size;
1134                 skb->data_len += txp->size;
1135                 skb->truesize += txp->size;
1136
1137                 /* Take an extra reference to offset network stack's put_page */
1138                 get_page(queue->mmap_pages[pending_idx]);
1139         }
1140         /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1141          * overlaps with "index", and "mapping" is not set. I think mapping
1142          * should be set. If delivered to local stack, it would drop this
1143          * skb in sk_filter unless the socket has the right to use it.
1144          */
1145         skb->pfmemalloc = false;
1146 }
1147
1148 static int xenvif_get_extras(struct xenvif_queue *queue,
1149                                 struct xen_netif_extra_info *extras,
1150                                 int work_to_do)
1151 {
1152         struct xen_netif_extra_info extra;
1153         RING_IDX cons = queue->tx.req_cons;
1154
1155         do {
1156                 if (unlikely(work_to_do-- <= 0)) {
1157                         netdev_err(queue->vif->dev, "Missing extra info\n");
1158                         xenvif_fatal_tx_err(queue->vif);
1159                         return -EBADR;
1160                 }
1161
1162                 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
1163                        sizeof(extra));
1164                 if (unlikely(!extra.type ||
1165                              extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1166                         queue->tx.req_cons = ++cons;
1167                         netdev_err(queue->vif->dev,
1168                                    "Invalid extra type: %d\n", extra.type);
1169                         xenvif_fatal_tx_err(queue->vif);
1170                         return -EINVAL;
1171                 }
1172
1173                 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1174                 queue->tx.req_cons = ++cons;
1175         } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1176
1177         return work_to_do;
1178 }
1179
1180 static int xenvif_set_skb_gso(struct xenvif *vif,
1181                               struct sk_buff *skb,
1182                               struct xen_netif_extra_info *gso)
1183 {
1184         if (!gso->u.gso.size) {
1185                 netdev_err(vif->dev, "GSO size must not be zero.\n");
1186                 xenvif_fatal_tx_err(vif);
1187                 return -EINVAL;
1188         }
1189
1190         switch (gso->u.gso.type) {
1191         case XEN_NETIF_GSO_TYPE_TCPV4:
1192                 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1193                 break;
1194         case XEN_NETIF_GSO_TYPE_TCPV6:
1195                 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1196                 break;
1197         default:
1198                 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1199                 xenvif_fatal_tx_err(vif);
1200                 return -EINVAL;
1201         }
1202
1203         skb_shinfo(skb)->gso_size = gso->u.gso.size;
1204         /* gso_segs will be calculated later */
1205
1206         return 0;
1207 }
1208
1209 static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
1210 {
1211         bool recalculate_partial_csum = false;
1212
1213         /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1214          * peers can fail to set NETRXF_csum_blank when sending a GSO
1215          * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1216          * recalculate the partial checksum.
1217          */
1218         if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1219                 queue->stats.rx_gso_checksum_fixup++;
1220                 skb->ip_summed = CHECKSUM_PARTIAL;
1221                 recalculate_partial_csum = true;
1222         }
1223
1224         /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1225         if (skb->ip_summed != CHECKSUM_PARTIAL)
1226                 return 0;
1227
1228         return skb_checksum_setup(skb, recalculate_partial_csum);
1229 }
1230
1231 static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
1232 {
1233         u64 now = get_jiffies_64();
1234         u64 next_credit = queue->credit_window_start +
1235                 msecs_to_jiffies(queue->credit_usec / 1000);
1236
1237         /* Timer could already be pending in rare cases. */
1238         if (timer_pending(&queue->credit_timeout))
1239                 return true;
1240
1241         /* Passed the point where we can replenish credit? */
1242         if (time_after_eq64(now, next_credit)) {
1243                 queue->credit_window_start = now;
1244                 tx_add_credit(queue);
1245         }
1246
1247         /* Still too big to send right now? Set a callback. */
1248         if (size > queue->remaining_credit) {
1249                 queue->credit_timeout.data     =
1250                         (unsigned long)queue;
1251                 queue->credit_timeout.function =
1252                         tx_credit_callback;
1253                 mod_timer(&queue->credit_timeout,
1254                           next_credit);
1255                 queue->credit_window_start = next_credit;
1256
1257                 return true;
1258         }
1259
1260         return false;
1261 }
1262
1263 static void xenvif_tx_build_gops(struct xenvif_queue *queue,
1264                                      int budget,
1265                                      unsigned *copy_ops,
1266                                      unsigned *map_ops)
1267 {
1268         struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
1269         struct sk_buff *skb;
1270         int ret;
1271
1272         while (skb_queue_len(&queue->tx_queue) < budget) {
1273                 struct xen_netif_tx_request txreq;
1274                 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
1275                 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1276                 u16 pending_idx;
1277                 RING_IDX idx;
1278                 int work_to_do;
1279                 unsigned int data_len;
1280                 pending_ring_idx_t index;
1281
1282                 if (queue->tx.sring->req_prod - queue->tx.req_cons >
1283                     XEN_NETIF_TX_RING_SIZE) {
1284                         netdev_err(queue->vif->dev,
1285                                    "Impossible number of requests. "
1286                                    "req_prod %d, req_cons %d, size %ld\n",
1287                                    queue->tx.sring->req_prod, queue->tx.req_cons,
1288                                    XEN_NETIF_TX_RING_SIZE);
1289                         xenvif_fatal_tx_err(queue->vif);
1290                         break;
1291                 }
1292
1293                 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
1294                 if (!work_to_do)
1295                         break;
1296
1297                 idx = queue->tx.req_cons;
1298                 rmb(); /* Ensure that we see the request before we copy it. */
1299                 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
1300
1301                 /* Credit-based scheduling. */
1302                 if (txreq.size > queue->remaining_credit &&
1303                     tx_credit_exceeded(queue, txreq.size))
1304                         break;
1305
1306                 queue->remaining_credit -= txreq.size;
1307
1308                 work_to_do--;
1309                 queue->tx.req_cons = ++idx;
1310
1311                 memset(extras, 0, sizeof(extras));
1312                 if (txreq.flags & XEN_NETTXF_extra_info) {
1313                         work_to_do = xenvif_get_extras(queue, extras,
1314                                                        work_to_do);
1315                         idx = queue->tx.req_cons;
1316                         if (unlikely(work_to_do < 0))
1317                                 break;
1318                 }
1319
1320                 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
1321                 if (unlikely(ret < 0))
1322                         break;
1323
1324                 idx += ret;
1325
1326                 if (unlikely(txreq.size < ETH_HLEN)) {
1327                         netdev_dbg(queue->vif->dev,
1328                                    "Bad packet size: %d\n", txreq.size);
1329                         xenvif_tx_err(queue, &txreq, idx);
1330                         break;
1331                 }
1332
1333                 /* No crossing a page as the payload mustn't fragment. */
1334                 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1335                         netdev_err(queue->vif->dev,
1336                                    "txreq.offset: %x, size: %u, end: %lu\n",
1337                                    txreq.offset, txreq.size,
1338                                    (txreq.offset&~PAGE_MASK) + txreq.size);
1339                         xenvif_fatal_tx_err(queue->vif);
1340                         break;
1341                 }
1342
1343                 index = pending_index(queue->pending_cons);
1344                 pending_idx = queue->pending_ring[index];
1345
1346                 data_len = (txreq.size > PKT_PROT_LEN &&
1347                             ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
1348                         PKT_PROT_LEN : txreq.size;
1349
1350                 skb = xenvif_alloc_skb(data_len);
1351                 if (unlikely(skb == NULL)) {
1352                         netdev_dbg(queue->vif->dev,
1353                                    "Can't allocate a skb in start_xmit.\n");
1354                         xenvif_tx_err(queue, &txreq, idx);
1355                         break;
1356                 }
1357
1358                 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1359                         struct xen_netif_extra_info *gso;
1360                         gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1361
1362                         if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
1363                                 /* Failure in xenvif_set_skb_gso is fatal. */
1364                                 kfree_skb(skb);
1365                                 break;
1366                         }
1367                 }
1368
1369                 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
1370
1371                 __skb_put(skb, data_len);
1372                 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1373                 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1374                 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
1375
1376                 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
1377                         virt_to_mfn(skb->data);
1378                 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1379                 queue->tx_copy_ops[*copy_ops].dest.offset =
1380                         offset_in_page(skb->data);
1381
1382                 queue->tx_copy_ops[*copy_ops].len = data_len;
1383                 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
1384
1385                 (*copy_ops)++;
1386
1387                 skb_shinfo(skb)->nr_frags = ret;
1388                 if (data_len < txreq.size) {
1389                         skb_shinfo(skb)->nr_frags++;
1390                         frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1391                                              pending_idx);
1392                         xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
1393                         gop++;
1394                 } else {
1395                         frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1396                                              INVALID_PENDING_IDX);
1397                         memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
1398                                sizeof(txreq));
1399                 }
1400
1401                 queue->pending_cons++;
1402
1403                 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
1404                 if (request_gop == NULL) {
1405                         kfree_skb(skb);
1406                         xenvif_tx_err(queue, &txreq, idx);
1407                         break;
1408                 }
1409                 gop = request_gop;
1410
1411                 __skb_queue_tail(&queue->tx_queue, skb);
1412
1413                 queue->tx.req_cons = idx;
1414
1415                 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1416                     (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
1417                         break;
1418         }
1419
1420         (*map_ops) = gop - queue->tx_map_ops;
1421         return;
1422 }
1423
1424 /* Consolidate skb with a frag_list into a brand new one with local pages on
1425  * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1426  */
1427 static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
1428 {
1429         unsigned int offset = skb_headlen(skb);
1430         skb_frag_t frags[MAX_SKB_FRAGS];
1431         int i;
1432         struct ubuf_info *uarg;
1433         struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1434
1435         queue->stats.tx_zerocopy_sent += 2;
1436         queue->stats.tx_frag_overflow++;
1437
1438         xenvif_fill_frags(queue, nskb);
1439         /* Subtract frags size, we will correct it later */
1440         skb->truesize -= skb->data_len;
1441         skb->len += nskb->len;
1442         skb->data_len += nskb->len;
1443
1444         /* create a brand new frags array and coalesce there */
1445         for (i = 0; offset < skb->len; i++) {
1446                 struct page *page;
1447                 unsigned int len;
1448
1449                 BUG_ON(i >= MAX_SKB_FRAGS);
1450                 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
1451                 if (!page) {
1452                         int j;
1453                         skb->truesize += skb->data_len;
1454                         for (j = 0; j < i; j++)
1455                                 put_page(frags[j].page.p);
1456                         return -ENOMEM;
1457                 }
1458
1459                 if (offset + PAGE_SIZE < skb->len)
1460                         len = PAGE_SIZE;
1461                 else
1462                         len = skb->len - offset;
1463                 if (skb_copy_bits(skb, offset, page_address(page), len))
1464                         BUG();
1465
1466                 offset += len;
1467                 frags[i].page.p = page;
1468                 frags[i].page_offset = 0;
1469                 skb_frag_size_set(&frags[i], len);
1470         }
1471         /* swap out with old one */
1472         memcpy(skb_shinfo(skb)->frags,
1473                frags,
1474                i * sizeof(skb_frag_t));
1475         skb_shinfo(skb)->nr_frags = i;
1476         skb->truesize += i * PAGE_SIZE;
1477
1478         /* remove traces of mapped pages and frag_list */
1479         skb_frag_list_init(skb);
1480         uarg = skb_shinfo(skb)->destructor_arg;
1481         uarg->callback(uarg, true);
1482         skb_shinfo(skb)->destructor_arg = NULL;
1483
1484         skb_shinfo(nskb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1485         kfree_skb(nskb);
1486
1487         return 0;
1488 }
1489
1490 static int xenvif_tx_submit(struct xenvif_queue *queue)
1491 {
1492         struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1493         struct gnttab_copy *gop_copy = queue->tx_copy_ops;
1494         struct sk_buff *skb;
1495         int work_done = 0;
1496
1497         while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
1498                 struct xen_netif_tx_request *txp;
1499                 u16 pending_idx;
1500                 unsigned data_len;
1501
1502                 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
1503                 txp = &queue->pending_tx_info[pending_idx].req;
1504
1505                 /* Check the remap error code. */
1506                 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
1507                         skb_shinfo(skb)->nr_frags = 0;
1508                         kfree_skb(skb);
1509                         continue;
1510                 }
1511
1512                 data_len = skb->len;
1513                 callback_param(queue, pending_idx).ctx = NULL;
1514                 if (data_len < txp->size) {
1515                         /* Append the packet payload as a fragment. */
1516                         txp->offset += data_len;
1517                         txp->size -= data_len;
1518                 } else {
1519                         /* Schedule a response immediately. */
1520                         xenvif_idx_release(queue, pending_idx,
1521                                            XEN_NETIF_RSP_OKAY);
1522                 }
1523
1524                 if (txp->flags & XEN_NETTXF_csum_blank)
1525                         skb->ip_summed = CHECKSUM_PARTIAL;
1526                 else if (txp->flags & XEN_NETTXF_data_validated)
1527                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1528
1529                 xenvif_fill_frags(queue, skb);
1530
1531                 if (unlikely(skb_has_frag_list(skb))) {
1532                         if (xenvif_handle_frag_list(queue, skb)) {
1533                                 if (net_ratelimit())
1534                                         netdev_err(queue->vif->dev,
1535                                                    "Not enough memory to consolidate frag_list!\n");
1536                                 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1537                                 kfree_skb(skb);
1538                                 continue;
1539                         }
1540                 }
1541
1542                 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
1543                         int target = min_t(int, skb->len, PKT_PROT_LEN);
1544                         __pskb_pull_tail(skb, target - skb_headlen(skb));
1545                 }
1546
1547                 skb->dev      = queue->vif->dev;
1548                 skb->protocol = eth_type_trans(skb, skb->dev);
1549                 skb_reset_network_header(skb);
1550
1551                 if (checksum_setup(queue, skb)) {
1552                         netdev_dbg(queue->vif->dev,
1553                                    "Can't setup checksum in net_tx_action\n");
1554                         /* We have to set this flag to trigger the callback */
1555                         if (skb_shinfo(skb)->destructor_arg)
1556                                 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1557                         kfree_skb(skb);
1558                         continue;
1559                 }
1560
1561                 skb_probe_transport_header(skb, 0);
1562
1563                 /* If the packet is GSO then we will have just set up the
1564                  * transport header offset in checksum_setup so it's now
1565                  * straightforward to calculate gso_segs.
1566                  */
1567                 if (skb_is_gso(skb)) {
1568                         int mss = skb_shinfo(skb)->gso_size;
1569                         int hdrlen = skb_transport_header(skb) -
1570                                 skb_mac_header(skb) +
1571                                 tcp_hdrlen(skb);
1572
1573                         skb_shinfo(skb)->gso_segs =
1574                                 DIV_ROUND_UP(skb->len - hdrlen, mss);
1575                 }
1576
1577                 queue->stats.rx_bytes += skb->len;
1578                 queue->stats.rx_packets++;
1579
1580                 work_done++;
1581
1582                 /* Set this flag right before netif_receive_skb, otherwise
1583                  * someone might think this packet already left netback, and
1584                  * do a skb_copy_ubufs while we are still in control of the
1585                  * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1586                  */
1587                 if (skb_shinfo(skb)->destructor_arg) {
1588                         skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1589                         queue->stats.tx_zerocopy_sent++;
1590                 }
1591
1592                 netif_receive_skb(skb);
1593         }
1594
1595         return work_done;
1596 }
1597
1598 void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1599 {
1600         unsigned long flags;
1601         pending_ring_idx_t index;
1602         struct xenvif_queue *queue = ubuf_to_queue(ubuf);
1603
1604         /* This is the only place where we grab this lock, to protect callbacks
1605          * from each other.
1606          */
1607         spin_lock_irqsave(&queue->callback_lock, flags);
1608         do {
1609                 u16 pending_idx = ubuf->desc;
1610                 ubuf = (struct ubuf_info *) ubuf->ctx;
1611                 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
1612                         MAX_PENDING_REQS);
1613                 index = pending_index(queue->dealloc_prod);
1614                 queue->dealloc_ring[index] = pending_idx;
1615                 /* Sync with xenvif_tx_dealloc_action:
1616                  * insert idx then incr producer.
1617                  */
1618                 smp_wmb();
1619                 queue->dealloc_prod++;
1620         } while (ubuf);
1621         wake_up(&queue->dealloc_wq);
1622         spin_unlock_irqrestore(&queue->callback_lock, flags);
1623
1624         if (likely(zerocopy_success))
1625                 queue->stats.tx_zerocopy_success++;
1626         else
1627                 queue->stats.tx_zerocopy_fail++;
1628 }
1629
1630 static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
1631 {
1632         struct gnttab_unmap_grant_ref *gop;
1633         pending_ring_idx_t dc, dp;
1634         u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1635         unsigned int i = 0;
1636
1637         dc = queue->dealloc_cons;
1638         gop = queue->tx_unmap_ops;
1639
1640         /* Free up any grants we have finished using */
1641         do {
1642                 dp = queue->dealloc_prod;
1643
1644                 /* Ensure we see all indices enqueued by all
1645                  * xenvif_zerocopy_callback().
1646                  */
1647                 smp_rmb();
1648
1649                 while (dc != dp) {
1650                         BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
1651                         pending_idx =
1652                                 queue->dealloc_ring[pending_index(dc++)];
1653
1654                         pending_idx_release[gop-queue->tx_unmap_ops] =
1655                                 pending_idx;
1656                         queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1657                                 queue->mmap_pages[pending_idx];
1658                         gnttab_set_unmap_op(gop,
1659                                             idx_to_kaddr(queue, pending_idx),
1660                                             GNTMAP_host_map,
1661                                             queue->grant_tx_handle[pending_idx]);
1662                         xenvif_grant_handle_reset(queue, pending_idx);
1663                         ++gop;
1664                 }
1665
1666         } while (dp != queue->dealloc_prod);
1667
1668         queue->dealloc_cons = dc;
1669
1670         if (gop - queue->tx_unmap_ops > 0) {
1671                 int ret;
1672                 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
1673                                         NULL,
1674                                         queue->pages_to_unmap,
1675                                         gop - queue->tx_unmap_ops);
1676                 if (ret) {
1677                         netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1678                                    gop - queue->tx_unmap_ops, ret);
1679                         for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
1680                                 if (gop[i].status != GNTST_okay)
1681                                         netdev_err(queue->vif->dev,
1682                                                    " host_addr: %llx handle: %x status: %d\n",
1683                                                    gop[i].host_addr,
1684                                                    gop[i].handle,
1685                                                    gop[i].status);
1686                         }
1687                         BUG();
1688                 }
1689         }
1690
1691         for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1692                 xenvif_idx_release(queue, pending_idx_release[i],
1693                                    XEN_NETIF_RSP_OKAY);
1694 }
1695
1696
1697 /* Called after netfront has transmitted */
1698 int xenvif_tx_action(struct xenvif_queue *queue, int budget)
1699 {
1700         unsigned nr_mops, nr_cops = 0;
1701         int work_done, ret;
1702
1703         if (unlikely(!tx_work_todo(queue)))
1704                 return 0;
1705
1706         xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
1707
1708         if (nr_cops == 0)
1709                 return 0;
1710
1711         gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
1712         if (nr_mops != 0) {
1713                 ret = gnttab_map_refs(queue->tx_map_ops,
1714                                       NULL,
1715                                       queue->pages_to_map,
1716                                       nr_mops);
1717                 BUG_ON(ret);
1718         }
1719
1720         work_done = xenvif_tx_submit(queue);
1721
1722         return work_done;
1723 }
1724
1725 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
1726                                u8 status)
1727 {
1728         struct pending_tx_info *pending_tx_info;
1729         pending_ring_idx_t index;
1730         unsigned long flags;
1731
1732         pending_tx_info = &queue->pending_tx_info[pending_idx];
1733         spin_lock_irqsave(&queue->response_lock, flags);
1734         make_tx_response(queue, &pending_tx_info->req, status);
1735         index = pending_index(queue->pending_prod);
1736         queue->pending_ring[index] = pending_idx;
1737         /* TX shouldn't use the index before we give it back here */
1738         mb();
1739         queue->pending_prod++;
1740         spin_unlock_irqrestore(&queue->response_lock, flags);
1741 }
1742
1743
1744 static void make_tx_response(struct xenvif_queue *queue,
1745                              struct xen_netif_tx_request *txp,
1746                              s8       st)
1747 {
1748         RING_IDX i = queue->tx.rsp_prod_pvt;
1749         struct xen_netif_tx_response *resp;
1750         int notify;
1751
1752         resp = RING_GET_RESPONSE(&queue->tx, i);
1753         resp->id     = txp->id;
1754         resp->status = st;
1755
1756         if (txp->flags & XEN_NETTXF_extra_info)
1757                 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1758
1759         queue->tx.rsp_prod_pvt = ++i;
1760         RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
1761         if (notify)
1762                 notify_remote_via_irq(queue->tx_irq);
1763 }
1764
1765 static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
1766                                              u16      id,
1767                                              s8       st,
1768                                              u16      offset,
1769                                              u16      size,
1770                                              u16      flags)
1771 {
1772         RING_IDX i = queue->rx.rsp_prod_pvt;
1773         struct xen_netif_rx_response *resp;
1774
1775         resp = RING_GET_RESPONSE(&queue->rx, i);
1776         resp->offset     = offset;
1777         resp->flags      = flags;
1778         resp->id         = id;
1779         resp->status     = (s16)size;
1780         if (st < 0)
1781                 resp->status = (s16)st;
1782
1783         queue->rx.rsp_prod_pvt = ++i;
1784
1785         return resp;
1786 }
1787
1788 void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
1789 {
1790         int ret;
1791         struct gnttab_unmap_grant_ref tx_unmap_op;
1792
1793         gnttab_set_unmap_op(&tx_unmap_op,
1794                             idx_to_kaddr(queue, pending_idx),
1795                             GNTMAP_host_map,
1796                             queue->grant_tx_handle[pending_idx]);
1797         xenvif_grant_handle_reset(queue, pending_idx);
1798
1799         ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1800                                 &queue->mmap_pages[pending_idx], 1);
1801         if (ret) {
1802                 netdev_err(queue->vif->dev,
1803                            "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1804                            ret,
1805                            pending_idx,
1806                            tx_unmap_op.host_addr,
1807                            tx_unmap_op.handle,
1808                            tx_unmap_op.status);
1809                 BUG();
1810         }
1811
1812         xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_OKAY);
1813 }
1814
1815 static inline int rx_work_todo(struct xenvif_queue *queue)
1816 {
1817         return (!skb_queue_empty(&queue->rx_queue) &&
1818                xenvif_rx_ring_slots_available(queue, queue->rx_last_skb_slots)) ||
1819                queue->rx_queue_purge;
1820 }
1821
1822 static inline int tx_work_todo(struct xenvif_queue *queue)
1823 {
1824         if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
1825                 return 1;
1826
1827         return 0;
1828 }
1829
1830 static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
1831 {
1832         return queue->dealloc_cons != queue->dealloc_prod;
1833 }
1834
1835 void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
1836 {
1837         if (queue->tx.sring)
1838                 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1839                                         queue->tx.sring);
1840         if (queue->rx.sring)
1841                 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1842                                         queue->rx.sring);
1843 }
1844
1845 int xenvif_map_frontend_rings(struct xenvif_queue *queue,
1846                               grant_ref_t tx_ring_ref,
1847                               grant_ref_t rx_ring_ref)
1848 {
1849         void *addr;
1850         struct xen_netif_tx_sring *txs;
1851         struct xen_netif_rx_sring *rxs;
1852
1853         int err = -ENOMEM;
1854
1855         err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1856                                      tx_ring_ref, &addr);
1857         if (err)
1858                 goto err;
1859
1860         txs = (struct xen_netif_tx_sring *)addr;
1861         BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
1862
1863         err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1864                                      rx_ring_ref, &addr);
1865         if (err)
1866                 goto err;
1867
1868         rxs = (struct xen_netif_rx_sring *)addr;
1869         BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
1870
1871         return 0;
1872
1873 err:
1874         xenvif_unmap_frontend_rings(queue);
1875         return err;
1876 }
1877
1878 static void xenvif_start_queue(struct xenvif_queue *queue)
1879 {
1880         if (xenvif_schedulable(queue->vif))
1881                 xenvif_wake_queue(queue);
1882 }
1883
1884 int xenvif_kthread_guest_rx(void *data)
1885 {
1886         struct xenvif_queue *queue = data;
1887         struct sk_buff *skb;
1888
1889         while (!kthread_should_stop()) {
1890                 wait_event_interruptible(queue->wq,
1891                                          rx_work_todo(queue) ||
1892                                          queue->vif->disabled ||
1893                                          kthread_should_stop());
1894
1895                 /* This frontend is found to be rogue, disable it in
1896                  * kthread context. Currently this is only set when
1897                  * netback finds out frontend sends malformed packet,
1898                  * but we cannot disable the interface in softirq
1899                  * context so we defer it here, if this thread is
1900                  * associated with queue 0.
1901                  */
1902                 if (unlikely(queue->vif->disabled && netif_carrier_ok(queue->vif->dev) && queue->id == 0))
1903                         xenvif_carrier_off(queue->vif);
1904
1905                 if (kthread_should_stop())
1906                         break;
1907
1908                 if (queue->rx_queue_purge) {
1909                         skb_queue_purge(&queue->rx_queue);
1910                         queue->rx_queue_purge = false;
1911                 }
1912
1913                 if (!skb_queue_empty(&queue->rx_queue))
1914                         xenvif_rx_action(queue);
1915
1916                 if (skb_queue_empty(&queue->rx_queue) &&
1917                     xenvif_queue_stopped(queue)) {
1918                         del_timer_sync(&queue->wake_queue);
1919                         xenvif_start_queue(queue);
1920                 }
1921
1922                 cond_resched();
1923         }
1924
1925         /* Bin any remaining skbs */
1926         while ((skb = skb_dequeue(&queue->rx_queue)) != NULL)
1927                 dev_kfree_skb(skb);
1928
1929         return 0;
1930 }
1931
1932 int xenvif_dealloc_kthread(void *data)
1933 {
1934         struct xenvif_queue *queue = data;
1935
1936         while (!kthread_should_stop()) {
1937                 wait_event_interruptible(queue->dealloc_wq,
1938                                          tx_dealloc_work_todo(queue) ||
1939                                          kthread_should_stop());
1940                 if (kthread_should_stop())
1941                         break;
1942
1943                 xenvif_tx_dealloc_action(queue);
1944                 cond_resched();
1945         }
1946
1947         /* Unmap anything remaining*/
1948         if (tx_dealloc_work_todo(queue))
1949                 xenvif_tx_dealloc_action(queue);
1950
1951         return 0;
1952 }
1953
1954 static int __init netback_init(void)
1955 {
1956         int rc = 0;
1957
1958         if (!xen_domain())
1959                 return -ENODEV;
1960
1961         /* Allow as many queues as there are CPUs, by default */
1962         xenvif_max_queues = num_online_cpus();
1963
1964         if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1965                 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1966                         fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1967                 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1968         }
1969
1970         rc = xenvif_xenbus_init();
1971         if (rc)
1972                 goto failed_init;
1973
1974         rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs);
1975
1976         return 0;
1977
1978 failed_init:
1979         return rc;
1980 }
1981
1982 module_init(netback_init);
1983
1984 static void __exit netback_fini(void)
1985 {
1986         xenvif_xenbus_fini();
1987 }
1988 module_exit(netback_fini);
1989
1990 MODULE_LICENSE("Dual BSD/GPL");
1991 MODULE_ALIAS("xen-backend:vif");
This page took 0.15881 seconds and 4 git commands to generate.