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