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