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