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