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