queueing.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4. */
  5. #ifndef _WG_QUEUEING_H
  6. #define _WG_QUEUEING_H
  7. #include "peer.h"
  8. #include <linux/types.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/ip.h>
  11. #include <linux/ipv6.h>
  12. #include <net/ip_tunnels.h>
  13. struct wg_device;
  14. struct wg_peer;
  15. struct multicore_worker;
  16. struct crypt_queue;
  17. struct prev_queue;
  18. struct sk_buff;
  19. /* queueing.c APIs: */
  20. int wg_packet_queue_init(struct crypt_queue *queue, work_func_t function,
  21. unsigned int len);
  22. void wg_packet_queue_free(struct crypt_queue *queue, bool purge);
  23. struct multicore_worker __percpu *
  24. wg_packet_percpu_multicore_worker_alloc(work_func_t function, void *ptr);
  25. /* receive.c APIs: */
  26. void wg_packet_receive(struct wg_device *wg, struct sk_buff *skb);
  27. void wg_packet_handshake_receive_worker(struct work_struct *work);
  28. /* NAPI poll function: */
  29. int wg_packet_rx_poll(struct napi_struct *napi, int budget);
  30. /* Workqueue worker: */
  31. void wg_packet_decrypt_worker(struct work_struct *work);
  32. /* send.c APIs: */
  33. void wg_packet_send_queued_handshake_initiation(struct wg_peer *peer,
  34. bool is_retry);
  35. void wg_packet_send_handshake_response(struct wg_peer *peer);
  36. void wg_packet_send_handshake_cookie(struct wg_device *wg,
  37. struct sk_buff *initiating_skb,
  38. __le32 sender_index);
  39. void wg_packet_send_keepalive(struct wg_peer *peer);
  40. void wg_packet_purge_staged_packets(struct wg_peer *peer);
  41. void wg_packet_send_staged_packets(struct wg_peer *peer);
  42. /* Workqueue workers: */
  43. void wg_packet_handshake_send_worker(struct work_struct *work);
  44. void wg_packet_tx_worker(struct work_struct *work);
  45. void wg_packet_encrypt_worker(struct work_struct *work);
  46. enum packet_state {
  47. PACKET_STATE_UNCRYPTED,
  48. PACKET_STATE_CRYPTED,
  49. PACKET_STATE_DEAD
  50. };
  51. struct packet_cb {
  52. u64 nonce;
  53. struct noise_keypair *keypair;
  54. atomic_t state;
  55. u32 mtu;
  56. u8 ds;
  57. };
  58. #define PACKET_CB(skb) ((struct packet_cb *)((skb)->cb))
  59. #define PACKET_PEER(skb) (PACKET_CB(skb)->keypair->entry.peer)
  60. static inline bool wg_check_packet_protocol(struct sk_buff *skb)
  61. {
  62. __be16 real_protocol = ip_tunnel_parse_protocol(skb);
  63. return real_protocol && skb->protocol == real_protocol;
  64. }
  65. static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
  66. {
  67. u8 l4_hash = skb->l4_hash;
  68. u8 sw_hash = skb->sw_hash;
  69. u32 hash = skb->hash;
  70. skb_scrub_packet(skb, true);
  71. memset(&skb->headers_start, 0,
  72. offsetof(struct sk_buff, headers_end) -
  73. offsetof(struct sk_buff, headers_start));
  74. if (encapsulating) {
  75. skb->l4_hash = l4_hash;
  76. skb->sw_hash = sw_hash;
  77. skb->hash = hash;
  78. }
  79. skb->queue_mapping = 0;
  80. skb->nohdr = 0;
  81. skb->peeked = 0;
  82. skb->mac_len = 0;
  83. skb->dev = NULL;
  84. #ifdef CONFIG_NET_SCHED
  85. skb->tc_index = 0;
  86. #endif
  87. skb_reset_redirect(skb);
  88. skb->hdr_len = skb_headroom(skb);
  89. skb_reset_mac_header(skb);
  90. skb_reset_network_header(skb);
  91. skb_reset_transport_header(skb);
  92. skb_probe_transport_header(skb);
  93. skb_reset_inner_headers(skb);
  94. }
  95. static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
  96. {
  97. unsigned int cpu = *stored_cpu, cpu_index, i;
  98. if (unlikely(cpu == nr_cpumask_bits ||
  99. !cpumask_test_cpu(cpu, cpu_online_mask))) {
  100. cpu_index = id % cpumask_weight(cpu_online_mask);
  101. cpu = cpumask_first(cpu_online_mask);
  102. for (i = 0; i < cpu_index; ++i)
  103. cpu = cpumask_next(cpu, cpu_online_mask);
  104. *stored_cpu = cpu;
  105. }
  106. return cpu;
  107. }
  108. /* This function is racy, in the sense that next is unlocked, so it could return
  109. * the same CPU twice. A race-free version of this would be to instead store an
  110. * atomic sequence number, do an increment-and-return, and then iterate through
  111. * every possible CPU until we get to that index -- choose_cpu. However that's
  112. * a bit slower, and it doesn't seem like this potential race actually
  113. * introduces any performance loss, so we live with it.
  114. */
  115. static inline int wg_cpumask_next_online(int *next)
  116. {
  117. int cpu = *next;
  118. while (unlikely(!cpumask_test_cpu(cpu, cpu_online_mask)))
  119. cpu = cpumask_next(cpu, cpu_online_mask) % nr_cpumask_bits;
  120. *next = cpumask_next(cpu, cpu_online_mask) % nr_cpumask_bits;
  121. return cpu;
  122. }
  123. void wg_prev_queue_init(struct prev_queue *queue);
  124. /* Multi producer */
  125. bool wg_prev_queue_enqueue(struct prev_queue *queue, struct sk_buff *skb);
  126. /* Single consumer */
  127. struct sk_buff *wg_prev_queue_dequeue(struct prev_queue *queue);
  128. /* Single consumer */
  129. static inline struct sk_buff *wg_prev_queue_peek(struct prev_queue *queue)
  130. {
  131. if (queue->peeked)
  132. return queue->peeked;
  133. queue->peeked = wg_prev_queue_dequeue(queue);
  134. return queue->peeked;
  135. }
  136. /* Single consumer */
  137. static inline void wg_prev_queue_drop_peeked(struct prev_queue *queue)
  138. {
  139. queue->peeked = NULL;
  140. }
  141. static inline int wg_queue_enqueue_per_device_and_peer(
  142. struct crypt_queue *device_queue, struct prev_queue *peer_queue,
  143. struct sk_buff *skb, struct workqueue_struct *wq, int *next_cpu)
  144. {
  145. int cpu;
  146. atomic_set_release(&PACKET_CB(skb)->state, PACKET_STATE_UNCRYPTED);
  147. /* We first queue this up for the peer ingestion, but the consumer
  148. * will wait for the state to change to CRYPTED or DEAD before.
  149. */
  150. if (unlikely(!wg_prev_queue_enqueue(peer_queue, skb)))
  151. return -ENOSPC;
  152. /* Then we queue it up in the device queue, which consumes the
  153. * packet as soon as it can.
  154. */
  155. cpu = wg_cpumask_next_online(next_cpu);
  156. if (unlikely(ptr_ring_produce_bh(&device_queue->ring, skb)))
  157. return -EPIPE;
  158. queue_work_on(cpu, wq, &per_cpu_ptr(device_queue->worker, cpu)->work);
  159. return 0;
  160. }
  161. static inline void wg_queue_enqueue_per_peer_tx(struct sk_buff *skb, enum packet_state state)
  162. {
  163. /* We take a reference, because as soon as we call atomic_set, the
  164. * peer can be freed from below us.
  165. */
  166. struct wg_peer *peer = wg_peer_get(PACKET_PEER(skb));
  167. atomic_set_release(&PACKET_CB(skb)->state, state);
  168. queue_work_on(wg_cpumask_choose_online(&peer->serial_work_cpu, peer->internal_id),
  169. peer->device->packet_crypt_wq, &peer->transmit_packet_work);
  170. wg_peer_put(peer);
  171. }
  172. static inline void wg_queue_enqueue_per_peer_rx(struct sk_buff *skb, enum packet_state state)
  173. {
  174. /* We take a reference, because as soon as we call atomic_set, the
  175. * peer can be freed from below us.
  176. */
  177. struct wg_peer *peer = wg_peer_get(PACKET_PEER(skb));
  178. atomic_set_release(&PACKET_CB(skb)->state, state);
  179. napi_schedule(&peer->napi);
  180. wg_peer_put(peer);
  181. }
  182. #ifdef DEBUG
  183. bool wg_packet_counter_selftest(void);
  184. #endif
  185. #endif /* _WG_QUEUEING_H */