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