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