blob: 8e9ade6ccf18ae0055c105e09ca81253db0611cc [file] [log] [blame]
Paul Durrant3254f832016-10-04 10:29:12 +01001/*
2 * Copyright (c) 2016 Citrix Systems Inc.
3 * Copyright (c) 2002-2005, K A Fraser
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation; or, when distributed
8 * separately from the Linux kernel or incorporated into other
9 * software packages, subject to the following license:
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this source file (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use, copy, modify,
14 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
15 * and to permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * IN THE SOFTWARE.
28 */
Paul Durrant3254f832016-10-04 10:29:12 +010029#include "common.h"
30
31#include <linux/kthread.h>
32
33#include <xen/xen.h>
34#include <xen/events.h>
35
36static bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue)
37{
38 RING_IDX prod, cons;
39 struct sk_buff *skb;
40 int needed;
41
42 skb = skb_peek(&queue->rx_queue);
43 if (!skb)
44 return false;
45
46 needed = DIV_ROUND_UP(skb->len, XEN_PAGE_SIZE);
47 if (skb_is_gso(skb))
48 needed++;
49 if (skb->sw_hash)
50 needed++;
51
52 do {
53 prod = queue->rx.sring->req_prod;
54 cons = queue->rx.req_cons;
55
56 if (prod - cons >= needed)
57 return true;
58
59 queue->rx.sring->req_event = prod + 1;
60
61 /* Make sure event is visible before we check prod
62 * again.
63 */
64 mb();
65 } while (queue->rx.sring->req_prod != prod);
66
67 return false;
68}
69
70void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
71{
72 unsigned long flags;
73
74 spin_lock_irqsave(&queue->rx_queue.lock, flags);
75
76 __skb_queue_tail(&queue->rx_queue, skb);
77
78 queue->rx_queue_len += skb->len;
79 if (queue->rx_queue_len > queue->rx_queue_max) {
80 struct net_device *dev = queue->vif->dev;
81
82 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
83 }
84
85 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
86}
87
88static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
89{
90 struct sk_buff *skb;
91
92 spin_lock_irq(&queue->rx_queue.lock);
93
94 skb = __skb_dequeue(&queue->rx_queue);
David Vrabel7c0b1a22016-10-04 10:29:15 +010095 if (skb) {
Paul Durrant3254f832016-10-04 10:29:12 +010096 queue->rx_queue_len -= skb->len;
David Vrabel7c0b1a22016-10-04 10:29:15 +010097 if (queue->rx_queue_len < queue->rx_queue_max) {
98 struct netdev_queue *txq;
99
100 txq = netdev_get_tx_queue(queue->vif->dev, queue->id);
101 netif_tx_wake_queue(txq);
102 }
103 }
Paul Durrant3254f832016-10-04 10:29:12 +0100104
105 spin_unlock_irq(&queue->rx_queue.lock);
106
107 return skb;
108}
109
Paul Durrant3254f832016-10-04 10:29:12 +0100110static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
111{
112 struct sk_buff *skb;
113
114 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
115 kfree_skb(skb);
116}
117
118static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
119{
120 struct sk_buff *skb;
121
122 for (;;) {
123 skb = skb_peek(&queue->rx_queue);
124 if (!skb)
125 break;
126 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
127 break;
128 xenvif_rx_dequeue(queue);
129 kfree_skb(skb);
130 }
131}
132
David Vrabeleb1723a2016-10-04 10:29:14 +0100133static void xenvif_rx_copy_flush(struct xenvif_queue *queue)
Paul Durrant3254f832016-10-04 10:29:12 +0100134{
David Vrabeleb1723a2016-10-04 10:29:14 +0100135 unsigned int i;
David Vrabela37f1222016-10-04 10:29:17 +0100136 int notify;
Paul Durrant3254f832016-10-04 10:29:12 +0100137
David Vrabeleb1723a2016-10-04 10:29:14 +0100138 gnttab_batch_copy(queue->rx_copy.op, queue->rx_copy.num);
Paul Durrant3254f832016-10-04 10:29:12 +0100139
David Vrabeleb1723a2016-10-04 10:29:14 +0100140 for (i = 0; i < queue->rx_copy.num; i++) {
141 struct gnttab_copy *op;
Paul Durrant3254f832016-10-04 10:29:12 +0100142
David Vrabeleb1723a2016-10-04 10:29:14 +0100143 op = &queue->rx_copy.op[i];
Paul Durrant3254f832016-10-04 10:29:12 +0100144
David Vrabeleb1723a2016-10-04 10:29:14 +0100145 /* If the copy failed, overwrite the status field in
146 * the corresponding response.
147 */
148 if (unlikely(op->status != GNTST_okay)) {
149 struct xen_netif_rx_response *rsp;
150
151 rsp = RING_GET_RESPONSE(&queue->rx,
152 queue->rx_copy.idx[i]);
153 rsp->status = op->status;
154 }
155 }
156
157 queue->rx_copy.num = 0;
David Vrabela37f1222016-10-04 10:29:17 +0100158
159 /* Push responses for all completed packets. */
160 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, notify);
161 if (notify)
162 notify_remote_via_irq(queue->rx_irq);
163
164 __skb_queue_purge(queue->rx_copy.completed);
Paul Durrant3254f832016-10-04 10:29:12 +0100165}
166
David Vrabeleb1723a2016-10-04 10:29:14 +0100167static void xenvif_rx_copy_add(struct xenvif_queue *queue,
168 struct xen_netif_rx_request *req,
169 unsigned int offset, void *data, size_t len)
Paul Durrant3254f832016-10-04 10:29:12 +0100170{
David Vrabeleb1723a2016-10-04 10:29:14 +0100171 struct gnttab_copy *op;
172 struct page *page;
Paul Durrant3254f832016-10-04 10:29:12 +0100173 struct xen_page_foreign *foreign;
Paul Durrant3254f832016-10-04 10:29:12 +0100174
David Vrabeleb1723a2016-10-04 10:29:14 +0100175 if (queue->rx_copy.num == COPY_BATCH_SIZE)
176 xenvif_rx_copy_flush(queue);
Paul Durrant3254f832016-10-04 10:29:12 +0100177
David Vrabeleb1723a2016-10-04 10:29:14 +0100178 op = &queue->rx_copy.op[queue->rx_copy.num];
Paul Durrant3254f832016-10-04 10:29:12 +0100179
David Vrabeleb1723a2016-10-04 10:29:14 +0100180 page = virt_to_page(data);
Paul Durrant3254f832016-10-04 10:29:12 +0100181
David Vrabeleb1723a2016-10-04 10:29:14 +0100182 op->flags = GNTCOPY_dest_gref;
Paul Durrant3254f832016-10-04 10:29:12 +0100183
184 foreign = xen_page_foreign(page);
185 if (foreign) {
David Vrabeleb1723a2016-10-04 10:29:14 +0100186 op->source.domid = foreign->domid;
187 op->source.u.ref = foreign->gref;
188 op->flags |= GNTCOPY_source_gref;
Paul Durrant3254f832016-10-04 10:29:12 +0100189 } else {
David Vrabeleb1723a2016-10-04 10:29:14 +0100190 op->source.u.gmfn = virt_to_gfn(data);
191 op->source.domid = DOMID_SELF;
Paul Durrant3254f832016-10-04 10:29:12 +0100192 }
Paul Durrant3254f832016-10-04 10:29:12 +0100193
David Vrabeleb1723a2016-10-04 10:29:14 +0100194 op->source.offset = xen_offset_in_page(data);
195 op->dest.u.ref = req->gref;
196 op->dest.domid = queue->vif->domid;
197 op->dest.offset = offset;
198 op->len = len;
Paul Durrant3254f832016-10-04 10:29:12 +0100199
David Vrabeleb1723a2016-10-04 10:29:14 +0100200 queue->rx_copy.idx[queue->rx_copy.num] = queue->rx.req_cons;
201 queue->rx_copy.num++;
Paul Durrant3254f832016-10-04 10:29:12 +0100202}
203
David Vrabeleb1723a2016-10-04 10:29:14 +0100204static unsigned int xenvif_gso_type(struct sk_buff *skb)
Paul Durrant3254f832016-10-04 10:29:12 +0100205{
Paul Durrant3254f832016-10-04 10:29:12 +0100206 if (skb_is_gso(skb)) {
207 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
David Vrabeleb1723a2016-10-04 10:29:14 +0100208 return XEN_NETIF_GSO_TYPE_TCPV4;
Paul Durrant3254f832016-10-04 10:29:12 +0100209 else
David Vrabeleb1723a2016-10-04 10:29:14 +0100210 return XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant3254f832016-10-04 10:29:12 +0100211 }
David Vrabeleb1723a2016-10-04 10:29:14 +0100212 return XEN_NETIF_GSO_TYPE_NONE;
Paul Durrant3254f832016-10-04 10:29:12 +0100213}
214
David Vrabeleb1723a2016-10-04 10:29:14 +0100215struct xenvif_pkt_state {
Paul Durrant3254f832016-10-04 10:29:12 +0100216 struct sk_buff *skb;
David Vrabeleb1723a2016-10-04 10:29:14 +0100217 size_t remaining_len;
Ross Lagerwall2167ca02016-10-04 10:29:18 +0100218 struct sk_buff *frag_iter;
219 int frag; /* frag == -1 => frag_iter->head */
David Vrabeleb1723a2016-10-04 10:29:14 +0100220 unsigned int frag_offset;
221 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
222 unsigned int extra_count;
223 unsigned int slot;
224};
Paul Durrant3254f832016-10-04 10:29:12 +0100225
David Vrabeleb1723a2016-10-04 10:29:14 +0100226static void xenvif_rx_next_skb(struct xenvif_queue *queue,
227 struct xenvif_pkt_state *pkt)
228{
229 struct sk_buff *skb;
230 unsigned int gso_type;
Paul Durrant3254f832016-10-04 10:29:12 +0100231
David Vrabeleb1723a2016-10-04 10:29:14 +0100232 skb = xenvif_rx_dequeue(queue);
Paul Durrant3254f832016-10-04 10:29:12 +0100233
David Vrabeleb1723a2016-10-04 10:29:14 +0100234 queue->stats.tx_bytes += skb->len;
235 queue->stats.tx_packets++;
Paul Durrant3254f832016-10-04 10:29:12 +0100236
David Vrabeleb1723a2016-10-04 10:29:14 +0100237 /* Reset packet state. */
238 memset(pkt, 0, sizeof(struct xenvif_pkt_state));
Paul Durrant3254f832016-10-04 10:29:12 +0100239
David Vrabeleb1723a2016-10-04 10:29:14 +0100240 pkt->skb = skb;
Ross Lagerwall2167ca02016-10-04 10:29:18 +0100241 pkt->frag_iter = skb;
David Vrabeleb1723a2016-10-04 10:29:14 +0100242 pkt->remaining_len = skb->len;
243 pkt->frag = -1;
244
245 gso_type = xenvif_gso_type(skb);
246 if ((1 << gso_type) & queue->vif->gso_mask) {
247 struct xen_netif_extra_info *extra;
248
249 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
250
251 extra->u.gso.type = gso_type;
252 extra->u.gso.size = skb_shinfo(skb)->gso_size;
253 extra->u.gso.pad = 0;
254 extra->u.gso.features = 0;
255 extra->type = XEN_NETIF_EXTRA_TYPE_GSO;
256 extra->flags = 0;
257
258 pkt->extra_count++;
Paul Durrant3254f832016-10-04 10:29:12 +0100259 }
260
David Vrabeleb1723a2016-10-04 10:29:14 +0100261 if (skb->sw_hash) {
262 struct xen_netif_extra_info *extra;
Paul Durrant3254f832016-10-04 10:29:12 +0100263
David Vrabeleb1723a2016-10-04 10:29:14 +0100264 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
Paul Durrant3254f832016-10-04 10:29:12 +0100265
David Vrabeleb1723a2016-10-04 10:29:14 +0100266 extra->u.hash.algorithm =
267 XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ;
Paul Durrant3254f832016-10-04 10:29:12 +0100268
David Vrabeleb1723a2016-10-04 10:29:14 +0100269 if (skb->l4_hash)
270 extra->u.hash.type =
271 skb->protocol == htons(ETH_P_IP) ?
272 _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP :
273 _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
Paul Durrant3254f832016-10-04 10:29:12 +0100274 else
David Vrabeleb1723a2016-10-04 10:29:14 +0100275 extra->u.hash.type =
276 skb->protocol == htons(ETH_P_IP) ?
277 _XEN_NETIF_CTRL_HASH_TYPE_IPV4 :
278 _XEN_NETIF_CTRL_HASH_TYPE_IPV6;
Paul Durrant3254f832016-10-04 10:29:12 +0100279
David Vrabeleb1723a2016-10-04 10:29:14 +0100280 *(uint32_t *)extra->u.hash.value = skb_get_hash_raw(skb);
281
282 extra->type = XEN_NETIF_EXTRA_TYPE_HASH;
283 extra->flags = 0;
284
285 pkt->extra_count++;
286 }
287}
288
289static void xenvif_rx_complete(struct xenvif_queue *queue,
290 struct xenvif_pkt_state *pkt)
291{
David Vrabela37f1222016-10-04 10:29:17 +0100292 /* All responses are ready to be pushed. */
David Vrabeleb1723a2016-10-04 10:29:14 +0100293 queue->rx.rsp_prod_pvt = queue->rx.req_cons;
David Vrabeleb1723a2016-10-04 10:29:14 +0100294
David Vrabela37f1222016-10-04 10:29:17 +0100295 __skb_queue_tail(queue->rx_copy.completed, pkt->skb);
David Vrabeleb1723a2016-10-04 10:29:14 +0100296}
297
Ross Lagerwall2167ca02016-10-04 10:29:18 +0100298static void xenvif_rx_next_frag(struct xenvif_pkt_state *pkt)
299{
300 struct sk_buff *frag_iter = pkt->frag_iter;
301 unsigned int nr_frags = skb_shinfo(frag_iter)->nr_frags;
302
303 pkt->frag++;
304 pkt->frag_offset = 0;
305
306 if (pkt->frag >= nr_frags) {
307 if (frag_iter == pkt->skb)
308 pkt->frag_iter = skb_shinfo(frag_iter)->frag_list;
309 else
310 pkt->frag_iter = frag_iter->next;
311
312 pkt->frag = -1;
313 }
314}
315
David Vrabeleb1723a2016-10-04 10:29:14 +0100316static void xenvif_rx_next_chunk(struct xenvif_queue *queue,
317 struct xenvif_pkt_state *pkt,
318 unsigned int offset, void **data,
319 size_t *len)
320{
Ross Lagerwall2167ca02016-10-04 10:29:18 +0100321 struct sk_buff *frag_iter = pkt->frag_iter;
David Vrabeleb1723a2016-10-04 10:29:14 +0100322 void *frag_data;
323 size_t frag_len, chunk_len;
324
Ross Lagerwall2167ca02016-10-04 10:29:18 +0100325 BUG_ON(!frag_iter);
326
David Vrabeleb1723a2016-10-04 10:29:14 +0100327 if (pkt->frag == -1) {
Ross Lagerwall2167ca02016-10-04 10:29:18 +0100328 frag_data = frag_iter->data;
329 frag_len = skb_headlen(frag_iter);
David Vrabeleb1723a2016-10-04 10:29:14 +0100330 } else {
Ross Lagerwall2167ca02016-10-04 10:29:18 +0100331 skb_frag_t *frag = &skb_shinfo(frag_iter)->frags[pkt->frag];
David Vrabeleb1723a2016-10-04 10:29:14 +0100332
333 frag_data = skb_frag_address(frag);
334 frag_len = skb_frag_size(frag);
335 }
336
337 frag_data += pkt->frag_offset;
338 frag_len -= pkt->frag_offset;
339
340 chunk_len = min(frag_len, XEN_PAGE_SIZE - offset);
341 chunk_len = min(chunk_len,
342 XEN_PAGE_SIZE - xen_offset_in_page(frag_data));
343
344 pkt->frag_offset += chunk_len;
345
346 /* Advance to next frag? */
Ross Lagerwall2167ca02016-10-04 10:29:18 +0100347 if (frag_len == chunk_len)
348 xenvif_rx_next_frag(pkt);
David Vrabeleb1723a2016-10-04 10:29:14 +0100349
350 *data = frag_data;
351 *len = chunk_len;
352}
353
354static void xenvif_rx_data_slot(struct xenvif_queue *queue,
355 struct xenvif_pkt_state *pkt,
356 struct xen_netif_rx_request *req,
357 struct xen_netif_rx_response *rsp)
358{
359 unsigned int offset = 0;
360 unsigned int flags;
361
362 do {
363 size_t len;
364 void *data;
365
366 xenvif_rx_next_chunk(queue, pkt, offset, &data, &len);
367 xenvif_rx_copy_add(queue, req, offset, data, len);
368
369 offset += len;
370 pkt->remaining_len -= len;
371
372 } while (offset < XEN_PAGE_SIZE && pkt->remaining_len > 0);
373
374 if (pkt->remaining_len > 0)
375 flags = XEN_NETRXF_more_data;
376 else
377 flags = 0;
378
379 if (pkt->slot == 0) {
380 struct sk_buff *skb = pkt->skb;
381
382 if (skb->ip_summed == CHECKSUM_PARTIAL)
Paul Durrant3254f832016-10-04 10:29:12 +0100383 flags |= XEN_NETRXF_csum_blank |
384 XEN_NETRXF_data_validated;
385 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
Paul Durrant3254f832016-10-04 10:29:12 +0100386 flags |= XEN_NETRXF_data_validated;
387
David Vrabeleb1723a2016-10-04 10:29:14 +0100388 if (pkt->extra_count != 0)
389 flags |= XEN_NETRXF_extra_info;
Paul Durrant3254f832016-10-04 10:29:12 +0100390 }
391
David Vrabeleb1723a2016-10-04 10:29:14 +0100392 rsp->offset = 0;
393 rsp->flags = flags;
394 rsp->id = req->id;
395 rsp->status = (s16)offset;
396}
397
398static void xenvif_rx_extra_slot(struct xenvif_queue *queue,
399 struct xenvif_pkt_state *pkt,
400 struct xen_netif_rx_request *req,
401 struct xen_netif_rx_response *rsp)
402{
403 struct xen_netif_extra_info *extra = (void *)rsp;
404 unsigned int i;
405
406 pkt->extra_count--;
407
408 for (i = 0; i < ARRAY_SIZE(pkt->extras); i++) {
409 if (pkt->extras[i].type) {
410 *extra = pkt->extras[i];
411
412 if (pkt->extra_count != 0)
413 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
414
415 pkt->extras[i].type = 0;
416 return;
417 }
418 }
419 BUG();
420}
421
David Vrabel98f6d572016-10-04 10:29:16 +0100422void xenvif_rx_skb(struct xenvif_queue *queue)
David Vrabeleb1723a2016-10-04 10:29:14 +0100423{
424 struct xenvif_pkt_state pkt;
425
426 xenvif_rx_next_skb(queue, &pkt);
427
428 do {
429 struct xen_netif_rx_request *req;
430 struct xen_netif_rx_response *rsp;
431
432 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons);
433 rsp = RING_GET_RESPONSE(&queue->rx, queue->rx.req_cons);
434
435 /* Extras must go after the first data slot */
436 if (pkt.slot != 0 && pkt.extra_count != 0)
437 xenvif_rx_extra_slot(queue, &pkt, req, rsp);
438 else
439 xenvif_rx_data_slot(queue, &pkt, req, rsp);
440
441 queue->rx.req_cons++;
442 pkt.slot++;
443 } while (pkt.remaining_len > 0 || pkt.extra_count != 0);
444
445 xenvif_rx_complete(queue, &pkt);
Paul Durrant3254f832016-10-04 10:29:12 +0100446}
447
David Vrabel98f6d572016-10-04 10:29:16 +0100448#define RX_BATCH_SIZE 64
449
450void xenvif_rx_action(struct xenvif_queue *queue)
451{
David Vrabela37f1222016-10-04 10:29:17 +0100452 struct sk_buff_head completed_skbs;
David Vrabel98f6d572016-10-04 10:29:16 +0100453 unsigned int work_done = 0;
454
David Vrabela37f1222016-10-04 10:29:17 +0100455 __skb_queue_head_init(&completed_skbs);
456 queue->rx_copy.completed = &completed_skbs;
457
David Vrabel98f6d572016-10-04 10:29:16 +0100458 while (xenvif_rx_ring_slots_available(queue) &&
459 work_done < RX_BATCH_SIZE) {
460 xenvif_rx_skb(queue);
461 work_done++;
462 }
David Vrabela37f1222016-10-04 10:29:17 +0100463
464 /* Flush any pending copies and complete all skbs. */
465 xenvif_rx_copy_flush(queue);
David Vrabel98f6d572016-10-04 10:29:16 +0100466}
467
Paul Durrant3254f832016-10-04 10:29:12 +0100468static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
469{
470 RING_IDX prod, cons;
471
472 prod = queue->rx.sring->req_prod;
473 cons = queue->rx.req_cons;
474
475 return !queue->stalled &&
476 prod - cons < 1 &&
477 time_after(jiffies,
478 queue->last_rx_time + queue->vif->stall_timeout);
479}
480
481static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
482{
483 RING_IDX prod, cons;
484
485 prod = queue->rx.sring->req_prod;
486 cons = queue->rx.req_cons;
487
488 return queue->stalled && prod - cons >= 1;
489}
490
491static bool xenvif_have_rx_work(struct xenvif_queue *queue)
492{
493 return xenvif_rx_ring_slots_available(queue) ||
494 (queue->vif->stall_timeout &&
495 (xenvif_rx_queue_stalled(queue) ||
496 xenvif_rx_queue_ready(queue))) ||
497 kthread_should_stop() ||
498 queue->vif->disabled;
499}
500
501static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
502{
503 struct sk_buff *skb;
504 long timeout;
505
506 skb = skb_peek(&queue->rx_queue);
507 if (!skb)
508 return MAX_SCHEDULE_TIMEOUT;
509
510 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
511 return timeout < 0 ? 0 : timeout;
512}
513
514/* Wait until the guest Rx thread has work.
515 *
516 * The timeout needs to be adjusted based on the current head of the
517 * queue (and not just the head at the beginning). In particular, if
518 * the queue is initially empty an infinite timeout is used and this
519 * needs to be reduced when a skb is queued.
520 *
521 * This cannot be done with wait_event_timeout() because it only
522 * calculates the timeout once.
523 */
524static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
525{
526 DEFINE_WAIT(wait);
527
528 if (xenvif_have_rx_work(queue))
529 return;
530
531 for (;;) {
532 long ret;
533
534 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
535 if (xenvif_have_rx_work(queue))
536 break;
537 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
538 if (!ret)
539 break;
540 }
541 finish_wait(&queue->wq, &wait);
542}
543
544static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
545{
546 struct xenvif *vif = queue->vif;
547
548 queue->stalled = true;
549
550 /* At least one queue has stalled? Disable the carrier. */
551 spin_lock(&vif->lock);
552 if (vif->stalled_queues++ == 0) {
553 netdev_info(vif->dev, "Guest Rx stalled");
554 netif_carrier_off(vif->dev);
555 }
556 spin_unlock(&vif->lock);
557}
558
559static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
560{
561 struct xenvif *vif = queue->vif;
562
563 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
564 queue->stalled = false;
565
566 /* All queues are ready? Enable the carrier. */
567 spin_lock(&vif->lock);
568 if (--vif->stalled_queues == 0) {
569 netdev_info(vif->dev, "Guest Rx ready");
570 netif_carrier_on(vif->dev);
571 }
572 spin_unlock(&vif->lock);
573}
574
575int xenvif_kthread_guest_rx(void *data)
576{
577 struct xenvif_queue *queue = data;
578 struct xenvif *vif = queue->vif;
579
580 if (!vif->stall_timeout)
581 xenvif_queue_carrier_on(queue);
582
583 for (;;) {
584 xenvif_wait_for_rx_work(queue);
585
586 if (kthread_should_stop())
587 break;
588
589 /* This frontend is found to be rogue, disable it in
590 * kthread context. Currently this is only set when
591 * netback finds out frontend sends malformed packet,
592 * but we cannot disable the interface in softirq
593 * context so we defer it here, if this thread is
594 * associated with queue 0.
595 */
596 if (unlikely(vif->disabled && queue->id == 0)) {
597 xenvif_carrier_off(vif);
598 break;
599 }
600
601 if (!skb_queue_empty(&queue->rx_queue))
602 xenvif_rx_action(queue);
603
604 /* If the guest hasn't provided any Rx slots for a
605 * while it's probably not responsive, drop the
606 * carrier so packets are dropped earlier.
607 */
608 if (vif->stall_timeout) {
609 if (xenvif_rx_queue_stalled(queue))
610 xenvif_queue_carrier_off(queue);
611 else if (xenvif_rx_queue_ready(queue))
612 xenvif_queue_carrier_on(queue);
613 }
614
615 /* Queued packets may have foreign pages from other
616 * domains. These cannot be queued indefinitely as
617 * this would starve guests of grant refs and transmit
618 * slots.
619 */
620 xenvif_rx_queue_drop_expired(queue);
621
Paul Durrant3254f832016-10-04 10:29:12 +0100622 cond_resched();
623 }
624
625 /* Bin any remaining skbs */
626 xenvif_rx_queue_purge(queue);
627
628 return 0;
629}