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