1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
7 #include <linux/skb_array.h>
9 struct multicore_worker __percpu
*
10 wg_packet_percpu_multicore_worker_alloc(work_func_t function
, void *ptr
)
13 struct multicore_worker __percpu
*worker
= alloc_percpu(struct multicore_worker
);
18 for_each_possible_cpu(cpu
) {
19 per_cpu_ptr(worker
, cpu
)->ptr
= ptr
;
20 INIT_WORK(&per_cpu_ptr(worker
, cpu
)->work
, function
);
25 int wg_packet_queue_init(struct crypt_queue
*queue
, work_func_t function
,
30 memset(queue
, 0, sizeof(*queue
));
32 ret
= ptr_ring_init(&queue
->ring
, len
, GFP_KERNEL
);
35 queue
->worker
= wg_packet_percpu_multicore_worker_alloc(function
, queue
);
37 ptr_ring_cleanup(&queue
->ring
, NULL
);
43 void wg_packet_queue_free(struct crypt_queue
*queue
, bool purge
)
45 free_percpu(queue
->worker
);
46 WARN_ON(!purge
&& !__ptr_ring_empty(&queue
->ring
));
47 ptr_ring_cleanup(&queue
->ring
, purge
? __skb_array_destroy_skb
: NULL
);
50 #define NEXT(skb) ((skb)->prev)
51 #define STUB(queue) ((struct sk_buff *)&queue->empty)
53 void wg_prev_queue_init(struct prev_queue
*queue
)
55 NEXT(STUB(queue
)) = NULL
;
56 queue
->head
= queue
->tail
= STUB(queue
);
58 atomic_set(&queue
->count
, 0);
60 offsetof(struct sk_buff
, next
) != offsetof(struct prev_queue
, empty
.next
) -
61 offsetof(struct prev_queue
, empty
) ||
62 offsetof(struct sk_buff
, prev
) != offsetof(struct prev_queue
, empty
.prev
) -
63 offsetof(struct prev_queue
, empty
));
66 static void __wg_prev_queue_enqueue(struct prev_queue
*queue
, struct sk_buff
*skb
)
68 WRITE_ONCE(NEXT(skb
), NULL
);
69 WRITE_ONCE(NEXT(xchg_release(&queue
->head
, skb
)), skb
);
72 bool wg_prev_queue_enqueue(struct prev_queue
*queue
, struct sk_buff
*skb
)
74 if (!atomic_add_unless(&queue
->count
, 1, MAX_QUEUED_PACKETS
))
76 __wg_prev_queue_enqueue(queue
, skb
);
80 struct sk_buff
*wg_prev_queue_dequeue(struct prev_queue
*queue
)
82 struct sk_buff
*tail
= queue
->tail
, *next
= smp_load_acquire(&NEXT(tail
));
84 if (tail
== STUB(queue
)) {
89 next
= smp_load_acquire(&NEXT(next
));
93 atomic_dec(&queue
->count
);
96 if (tail
!= READ_ONCE(queue
->head
))
98 __wg_prev_queue_enqueue(queue
, STUB(queue
));
99 next
= smp_load_acquire(&NEXT(tail
));
102 atomic_dec(&queue
->count
);