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