GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / net / xen-netback / rx.c
1 /*
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  */
29 #include "common.h"
30
31 #include <linux/kthread.h>
32
33 #include <xen/xen.h>
34 #include <xen/events.h>
35
36 static 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         unsigned long flags;
42
43         spin_lock_irqsave(&queue->rx_queue.lock, flags);
44
45         skb = skb_peek(&queue->rx_queue);
46         if (!skb) {
47                 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
48                 return false;
49         }
50
51         needed = DIV_ROUND_UP(skb->len, XEN_PAGE_SIZE);
52         if (skb_is_gso(skb))
53                 needed++;
54         if (skb->sw_hash)
55                 needed++;
56
57         spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
58
59         do {
60                 prod = queue->rx.sring->req_prod;
61                 cons = queue->rx.req_cons;
62
63                 if (prod - cons >= needed)
64                         return true;
65
66                 queue->rx.sring->req_event = prod + 1;
67
68                 /* Make sure event is visible before we check prod
69                  * again.
70                  */
71                 mb();
72         } while (queue->rx.sring->req_prod != prod);
73
74         return false;
75 }
76
77 void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
78 {
79         unsigned long flags;
80
81         spin_lock_irqsave(&queue->rx_queue.lock, flags);
82
83         __skb_queue_tail(&queue->rx_queue, skb);
84
85         queue->rx_queue_len += skb->len;
86         if (queue->rx_queue_len > queue->rx_queue_max) {
87                 struct net_device *dev = queue->vif->dev;
88
89                 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
90         }
91
92         spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
93 }
94
95 static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
96 {
97         struct sk_buff *skb;
98
99         spin_lock_irq(&queue->rx_queue.lock);
100
101         skb = __skb_dequeue(&queue->rx_queue);
102         if (skb) {
103                 queue->rx_queue_len -= skb->len;
104                 if (queue->rx_queue_len < queue->rx_queue_max) {
105                         struct netdev_queue *txq;
106
107                         txq = netdev_get_tx_queue(queue->vif->dev, queue->id);
108                         netif_tx_wake_queue(txq);
109                 }
110         }
111
112         spin_unlock_irq(&queue->rx_queue.lock);
113
114         return skb;
115 }
116
117 static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
118 {
119         struct sk_buff *skb;
120
121         while ((skb = xenvif_rx_dequeue(queue)) != NULL)
122                 kfree_skb(skb);
123 }
124
125 static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
126 {
127         struct sk_buff *skb;
128
129         for (;;) {
130                 skb = skb_peek(&queue->rx_queue);
131                 if (!skb)
132                         break;
133                 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
134                         break;
135                 xenvif_rx_dequeue(queue);
136                 kfree_skb(skb);
137         }
138 }
139
140 static void xenvif_rx_copy_flush(struct xenvif_queue *queue)
141 {
142         unsigned int i;
143         int notify;
144
145         gnttab_batch_copy(queue->rx_copy.op, queue->rx_copy.num);
146
147         for (i = 0; i < queue->rx_copy.num; i++) {
148                 struct gnttab_copy *op;
149
150                 op = &queue->rx_copy.op[i];
151
152                 /* If the copy failed, overwrite the status field in
153                  * the corresponding response.
154                  */
155                 if (unlikely(op->status != GNTST_okay)) {
156                         struct xen_netif_rx_response *rsp;
157
158                         rsp = RING_GET_RESPONSE(&queue->rx,
159                                                 queue->rx_copy.idx[i]);
160                         rsp->status = op->status;
161                 }
162         }
163
164         queue->rx_copy.num = 0;
165
166         /* Push responses for all completed packets. */
167         RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, notify);
168         if (notify)
169                 notify_remote_via_irq(queue->rx_irq);
170
171         __skb_queue_purge(queue->rx_copy.completed);
172 }
173
174 static void xenvif_rx_copy_add(struct xenvif_queue *queue,
175                                struct xen_netif_rx_request *req,
176                                unsigned int offset, void *data, size_t len)
177 {
178         struct gnttab_copy *op;
179         struct page *page;
180         struct xen_page_foreign *foreign;
181
182         if (queue->rx_copy.num == COPY_BATCH_SIZE)
183                 xenvif_rx_copy_flush(queue);
184
185         op = &queue->rx_copy.op[queue->rx_copy.num];
186
187         page = virt_to_page(data);
188
189         op->flags = GNTCOPY_dest_gref;
190
191         foreign = xen_page_foreign(page);
192         if (foreign) {
193                 op->source.domid = foreign->domid;
194                 op->source.u.ref = foreign->gref;
195                 op->flags |= GNTCOPY_source_gref;
196         } else {
197                 op->source.u.gmfn = virt_to_gfn(data);
198                 op->source.domid  = DOMID_SELF;
199         }
200
201         op->source.offset = xen_offset_in_page(data);
202         op->dest.u.ref    = req->gref;
203         op->dest.domid    = queue->vif->domid;
204         op->dest.offset   = offset;
205         op->len           = len;
206
207         queue->rx_copy.idx[queue->rx_copy.num] = queue->rx.req_cons;
208         queue->rx_copy.num++;
209 }
210
211 static unsigned int xenvif_gso_type(struct sk_buff *skb)
212 {
213         if (skb_is_gso(skb)) {
214                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
215                         return XEN_NETIF_GSO_TYPE_TCPV4;
216                 else
217                         return XEN_NETIF_GSO_TYPE_TCPV6;
218         }
219         return XEN_NETIF_GSO_TYPE_NONE;
220 }
221
222 struct xenvif_pkt_state {
223         struct sk_buff *skb;
224         size_t remaining_len;
225         struct sk_buff *frag_iter;
226         int frag; /* frag == -1 => frag_iter->head */
227         unsigned int frag_offset;
228         struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
229         unsigned int extra_count;
230         unsigned int slot;
231 };
232
233 static void xenvif_rx_next_skb(struct xenvif_queue *queue,
234                                struct xenvif_pkt_state *pkt)
235 {
236         struct sk_buff *skb;
237         unsigned int gso_type;
238
239         skb = xenvif_rx_dequeue(queue);
240
241         queue->stats.tx_bytes += skb->len;
242         queue->stats.tx_packets++;
243
244         /* Reset packet state. */
245         memset(pkt, 0, sizeof(struct xenvif_pkt_state));
246
247         pkt->skb = skb;
248         pkt->frag_iter = skb;
249         pkt->remaining_len = skb->len;
250         pkt->frag = -1;
251
252         gso_type = xenvif_gso_type(skb);
253         if ((1 << gso_type) & queue->vif->gso_mask) {
254                 struct xen_netif_extra_info *extra;
255
256                 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
257
258                 extra->u.gso.type = gso_type;
259                 extra->u.gso.size = skb_shinfo(skb)->gso_size;
260                 extra->u.gso.pad = 0;
261                 extra->u.gso.features = 0;
262                 extra->type = XEN_NETIF_EXTRA_TYPE_GSO;
263                 extra->flags = 0;
264
265                 pkt->extra_count++;
266         }
267
268         if (skb->sw_hash) {
269                 struct xen_netif_extra_info *extra;
270
271                 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
272
273                 extra->u.hash.algorithm =
274                         XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ;
275
276                 if (skb->l4_hash)
277                         extra->u.hash.type =
278                                 skb->protocol == htons(ETH_P_IP) ?
279                                 _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP :
280                                 _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
281                 else
282                         extra->u.hash.type =
283                                 skb->protocol == htons(ETH_P_IP) ?
284                                 _XEN_NETIF_CTRL_HASH_TYPE_IPV4 :
285                                 _XEN_NETIF_CTRL_HASH_TYPE_IPV6;
286
287                 *(uint32_t *)extra->u.hash.value = skb_get_hash_raw(skb);
288
289                 extra->type = XEN_NETIF_EXTRA_TYPE_HASH;
290                 extra->flags = 0;
291
292                 pkt->extra_count++;
293         }
294 }
295
296 static void xenvif_rx_complete(struct xenvif_queue *queue,
297                                struct xenvif_pkt_state *pkt)
298 {
299         /* All responses are ready to be pushed. */
300         queue->rx.rsp_prod_pvt = queue->rx.req_cons;
301
302         __skb_queue_tail(queue->rx_copy.completed, pkt->skb);
303 }
304
305 static void xenvif_rx_next_frag(struct xenvif_pkt_state *pkt)
306 {
307         struct sk_buff *frag_iter = pkt->frag_iter;
308         unsigned int nr_frags = skb_shinfo(frag_iter)->nr_frags;
309
310         pkt->frag++;
311         pkt->frag_offset = 0;
312
313         if (pkt->frag >= nr_frags) {
314                 if (frag_iter == pkt->skb)
315                         pkt->frag_iter = skb_shinfo(frag_iter)->frag_list;
316                 else
317                         pkt->frag_iter = frag_iter->next;
318
319                 pkt->frag = -1;
320         }
321 }
322
323 static void xenvif_rx_next_chunk(struct xenvif_queue *queue,
324                                  struct xenvif_pkt_state *pkt,
325                                  unsigned int offset, void **data,
326                                  size_t *len)
327 {
328         struct sk_buff *frag_iter = pkt->frag_iter;
329         void *frag_data;
330         size_t frag_len, chunk_len;
331
332         BUG_ON(!frag_iter);
333
334         if (pkt->frag == -1) {
335                 frag_data = frag_iter->data;
336                 frag_len = skb_headlen(frag_iter);
337         } else {
338                 skb_frag_t *frag = &skb_shinfo(frag_iter)->frags[pkt->frag];
339
340                 frag_data = skb_frag_address(frag);
341                 frag_len = skb_frag_size(frag);
342         }
343
344         frag_data += pkt->frag_offset;
345         frag_len -= pkt->frag_offset;
346
347         chunk_len = min_t(size_t, frag_len, XEN_PAGE_SIZE - offset);
348         chunk_len = min_t(size_t, chunk_len, XEN_PAGE_SIZE -
349                                              xen_offset_in_page(frag_data));
350
351         pkt->frag_offset += chunk_len;
352
353         /* Advance to next frag? */
354         if (frag_len == chunk_len)
355                 xenvif_rx_next_frag(pkt);
356
357         *data = frag_data;
358         *len = chunk_len;
359 }
360
361 static void xenvif_rx_data_slot(struct xenvif_queue *queue,
362                                 struct xenvif_pkt_state *pkt,
363                                 struct xen_netif_rx_request *req,
364                                 struct xen_netif_rx_response *rsp)
365 {
366         unsigned int offset = 0;
367         unsigned int flags;
368
369         do {
370                 size_t len;
371                 void *data;
372
373                 xenvif_rx_next_chunk(queue, pkt, offset, &data, &len);
374                 xenvif_rx_copy_add(queue, req, offset, data, len);
375
376                 offset += len;
377                 pkt->remaining_len -= len;
378
379         } while (offset < XEN_PAGE_SIZE && pkt->remaining_len > 0);
380
381         if (pkt->remaining_len > 0)
382                 flags = XEN_NETRXF_more_data;
383         else
384                 flags = 0;
385
386         if (pkt->slot == 0) {
387                 struct sk_buff *skb = pkt->skb;
388
389                 if (skb->ip_summed == CHECKSUM_PARTIAL)
390                         flags |= XEN_NETRXF_csum_blank |
391                                  XEN_NETRXF_data_validated;
392                 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
393                         flags |= XEN_NETRXF_data_validated;
394
395                 if (pkt->extra_count != 0)
396                         flags |= XEN_NETRXF_extra_info;
397         }
398
399         rsp->offset = 0;
400         rsp->flags = flags;
401         rsp->id = req->id;
402         rsp->status = (s16)offset;
403 }
404
405 static void xenvif_rx_extra_slot(struct xenvif_queue *queue,
406                                  struct xenvif_pkt_state *pkt,
407                                  struct xen_netif_rx_request *req,
408                                  struct xen_netif_rx_response *rsp)
409 {
410         struct xen_netif_extra_info *extra = (void *)rsp;
411         unsigned int i;
412
413         pkt->extra_count--;
414
415         for (i = 0; i < ARRAY_SIZE(pkt->extras); i++) {
416                 if (pkt->extras[i].type) {
417                         *extra = pkt->extras[i];
418
419                         if (pkt->extra_count != 0)
420                                 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
421
422                         pkt->extras[i].type = 0;
423                         return;
424                 }
425         }
426         BUG();
427 }
428
429 void xenvif_rx_skb(struct xenvif_queue *queue)
430 {
431         struct xenvif_pkt_state pkt;
432
433         xenvif_rx_next_skb(queue, &pkt);
434
435         queue->last_rx_time = jiffies;
436
437         do {
438                 struct xen_netif_rx_request *req;
439                 struct xen_netif_rx_response *rsp;
440
441                 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons);
442                 rsp = RING_GET_RESPONSE(&queue->rx, queue->rx.req_cons);
443
444                 /* Extras must go after the first data slot */
445                 if (pkt.slot != 0 && pkt.extra_count != 0)
446                         xenvif_rx_extra_slot(queue, &pkt, req, rsp);
447                 else
448                         xenvif_rx_data_slot(queue, &pkt, req, rsp);
449
450                 queue->rx.req_cons++;
451                 pkt.slot++;
452         } while (pkt.remaining_len > 0 || pkt.extra_count != 0);
453
454         xenvif_rx_complete(queue, &pkt);
455 }
456
457 #define RX_BATCH_SIZE 64
458
459 void xenvif_rx_action(struct xenvif_queue *queue)
460 {
461         struct sk_buff_head completed_skbs;
462         unsigned int work_done = 0;
463
464         __skb_queue_head_init(&completed_skbs);
465         queue->rx_copy.completed = &completed_skbs;
466
467         while (xenvif_rx_ring_slots_available(queue) &&
468                work_done < RX_BATCH_SIZE) {
469                 xenvif_rx_skb(queue);
470                 work_done++;
471         }
472
473         /* Flush any pending copies and complete all skbs. */
474         xenvif_rx_copy_flush(queue);
475 }
476
477 static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
478 {
479         RING_IDX prod, cons;
480
481         prod = queue->rx.sring->req_prod;
482         cons = queue->rx.req_cons;
483
484         return !queue->stalled &&
485                 prod - cons < 1 &&
486                 time_after(jiffies,
487                            queue->last_rx_time + queue->vif->stall_timeout);
488 }
489
490 static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
491 {
492         RING_IDX prod, cons;
493
494         prod = queue->rx.sring->req_prod;
495         cons = queue->rx.req_cons;
496
497         return queue->stalled && prod - cons >= 1;
498 }
499
500 bool xenvif_have_rx_work(struct xenvif_queue *queue, bool test_kthread)
501 {
502         return xenvif_rx_ring_slots_available(queue) ||
503                 (queue->vif->stall_timeout &&
504                  (xenvif_rx_queue_stalled(queue) ||
505                   xenvif_rx_queue_ready(queue))) ||
506                 (test_kthread && kthread_should_stop()) ||
507                 queue->vif->disabled;
508 }
509
510 static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
511 {
512         struct sk_buff *skb;
513         long timeout;
514
515         skb = skb_peek(&queue->rx_queue);
516         if (!skb)
517                 return MAX_SCHEDULE_TIMEOUT;
518
519         timeout = XENVIF_RX_CB(skb)->expires - jiffies;
520         return timeout < 0 ? 0 : timeout;
521 }
522
523 /* Wait until the guest Rx thread has work.
524  *
525  * The timeout needs to be adjusted based on the current head of the
526  * queue (and not just the head at the beginning).  In particular, if
527  * the queue is initially empty an infinite timeout is used and this
528  * needs to be reduced when a skb is queued.
529  *
530  * This cannot be done with wait_event_timeout() because it only
531  * calculates the timeout once.
532  */
533 static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
534 {
535         DEFINE_WAIT(wait);
536
537         if (xenvif_have_rx_work(queue, true))
538                 return;
539
540         for (;;) {
541                 long ret;
542
543                 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
544                 if (xenvif_have_rx_work(queue, true))
545                         break;
546                 if (atomic_fetch_andnot(NETBK_RX_EOI | NETBK_COMMON_EOI,
547                                         &queue->eoi_pending) &
548                     (NETBK_RX_EOI | NETBK_COMMON_EOI))
549                         xen_irq_lateeoi(queue->rx_irq, 0);
550
551                 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
552                 if (!ret)
553                         break;
554         }
555         finish_wait(&queue->wq, &wait);
556 }
557
558 static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
559 {
560         struct xenvif *vif = queue->vif;
561
562         queue->stalled = true;
563
564         /* At least one queue has stalled? Disable the carrier. */
565         spin_lock(&vif->lock);
566         if (vif->stalled_queues++ == 0) {
567                 netdev_info(vif->dev, "Guest Rx stalled");
568                 netif_carrier_off(vif->dev);
569         }
570         spin_unlock(&vif->lock);
571 }
572
573 static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
574 {
575         struct xenvif *vif = queue->vif;
576
577         queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
578         queue->stalled = false;
579
580         /* All queues are ready? Enable the carrier. */
581         spin_lock(&vif->lock);
582         if (--vif->stalled_queues == 0) {
583                 netdev_info(vif->dev, "Guest Rx ready");
584                 netif_carrier_on(vif->dev);
585         }
586         spin_unlock(&vif->lock);
587 }
588
589 int xenvif_kthread_guest_rx(void *data)
590 {
591         struct xenvif_queue *queue = data;
592         struct xenvif *vif = queue->vif;
593
594         if (!vif->stall_timeout)
595                 xenvif_queue_carrier_on(queue);
596
597         for (;;) {
598                 xenvif_wait_for_rx_work(queue);
599
600                 if (kthread_should_stop())
601                         break;
602
603                 /* This frontend is found to be rogue, disable it in
604                  * kthread context. Currently this is only set when
605                  * netback finds out frontend sends malformed packet,
606                  * but we cannot disable the interface in softirq
607                  * context so we defer it here, if this thread is
608                  * associated with queue 0.
609                  */
610                 if (unlikely(vif->disabled && queue->id == 0)) {
611                         xenvif_carrier_off(vif);
612                         break;
613                 }
614
615                 if (!skb_queue_empty(&queue->rx_queue))
616                         xenvif_rx_action(queue);
617
618                 /* If the guest hasn't provided any Rx slots for a
619                  * while it's probably not responsive, drop the
620                  * carrier so packets are dropped earlier.
621                  */
622                 if (vif->stall_timeout) {
623                         if (xenvif_rx_queue_stalled(queue))
624                                 xenvif_queue_carrier_off(queue);
625                         else if (xenvif_rx_queue_ready(queue))
626                                 xenvif_queue_carrier_on(queue);
627                 }
628
629                 /* Queued packets may have foreign pages from other
630                  * domains.  These cannot be queued indefinitely as
631                  * this would starve guests of grant refs and transmit
632                  * slots.
633                  */
634                 xenvif_rx_queue_drop_expired(queue);
635
636                 cond_resched();
637         }
638
639         /* Bin any remaining skbs */
640         xenvif_rx_queue_purge(queue);
641
642         return 0;
643 }