queueing.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. 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, 0, sizeof(skb->headers));
  72. if (encapsulating) {
  73. skb->l4_hash = l4_hash;
  74. skb->sw_hash = sw_hash;
  75. skb->hash = hash;
  76. }
  77. skb->queue_mapping = 0;
  78. skb->nohdr = 0;
  79. skb->peeked = 0;
  80. skb->mac_len = 0;
  81. skb->dev = NULL;
  82. #ifdef CONFIG_NET_SCHED
  83. skb->tc_index = 0;
  84. #endif
  85. skb_reset_redirect(skb);
  86. skb->hdr_len = skb_headroom(skb);
  87. skb_reset_mac_header(skb);
  88. skb_reset_network_header(skb);
  89. skb_reset_transport_header(skb);
  90. skb_probe_transport_header(skb);
  91. skb_reset_inner_headers(skb);
  92. }
  93. static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
  94. {
  95. unsigned int cpu = *stored_cpu, cpu_index, i;
  96. if (unlikely(cpu == nr_cpumask_bits ||
  97. !cpumask_test_cpu(cpu, cpu_online_mask))) {
  98. cpu_index = id % cpumask_weight(cpu_online_mask);
  99. cpu = cpumask_first(cpu_online_mask);
  100. for (i = 0; i < cpu_index; ++i)
  101. cpu = cpumask_next(cpu, cpu_online_mask);
  102. *stored_cpu = cpu;
  103. }
  104. return cpu;
  105. }
  106. /* This function is racy, in the sense that it's called while last_cpu is
  107. * unlocked, so it could return the same CPU twice. Adding locking or using
  108. * atomic sequence numbers is slower though, and the consequences of racing are
  109. * harmless, so live with it.
  110. */
  111. static inline int wg_cpumask_next_online(int *last_cpu)
  112. {
  113. int cpu = cpumask_next(*last_cpu, cpu_online_mask);
  114. if (cpu >= nr_cpu_ids)
  115. cpu = cpumask_first(cpu_online_mask);
  116. *last_cpu = cpu;
  117. return cpu;
  118. }
  119. void wg_prev_queue_init(struct prev_queue *queue);
  120. /* Multi producer */
  121. bool wg_prev_queue_enqueue(struct prev_queue *queue, struct sk_buff *skb);
  122. /* Single consumer */
  123. struct sk_buff *wg_prev_queue_dequeue(struct prev_queue *queue);
  124. /* Single consumer */
  125. static inline struct sk_buff *wg_prev_queue_peek(struct prev_queue *queue)
  126. {
  127. if (queue->peeked)
  128. return queue->peeked;
  129. queue->peeked = wg_prev_queue_dequeue(queue);
  130. return queue->peeked;
  131. }
  132. /* Single consumer */
  133. static inline void wg_prev_queue_drop_peeked(struct prev_queue *queue)
  134. {
  135. queue->peeked = NULL;
  136. }
  137. static inline int wg_queue_enqueue_per_device_and_peer(
  138. struct crypt_queue *device_queue, struct prev_queue *peer_queue,
  139. struct sk_buff *skb, struct workqueue_struct *wq)
  140. {
  141. int cpu;
  142. atomic_set_release(&PACKET_CB(skb)->state, PACKET_STATE_UNCRYPTED);
  143. /* We first queue this up for the peer ingestion, but the consumer
  144. * will wait for the state to change to CRYPTED or DEAD before.
  145. */
  146. if (unlikely(!wg_prev_queue_enqueue(peer_queue, skb)))
  147. return -ENOSPC;
  148. /* Then we queue it up in the device queue, which consumes the
  149. * packet as soon as it can.
  150. */
  151. cpu = wg_cpumask_next_online(&device_queue->last_cpu);
  152. if (unlikely(ptr_ring_produce_bh(&device_queue->ring, skb)))
  153. return -EPIPE;
  154. queue_work_on(cpu, wq, &per_cpu_ptr(device_queue->worker, cpu)->work);
  155. return 0;
  156. }
  157. static inline void wg_queue_enqueue_per_peer_tx(struct sk_buff *skb, enum packet_state state)
  158. {
  159. /* We take a reference, because as soon as we call atomic_set, the
  160. * peer can be freed from below us.
  161. */
  162. struct wg_peer *peer = wg_peer_get(PACKET_PEER(skb));
  163. atomic_set_release(&PACKET_CB(skb)->state, state);
  164. queue_work_on(wg_cpumask_choose_online(&peer->serial_work_cpu, peer->internal_id),
  165. peer->device->packet_crypt_wq, &peer->transmit_packet_work);
  166. wg_peer_put(peer);
  167. }
  168. static inline void wg_queue_enqueue_per_peer_rx(struct sk_buff *skb, enum packet_state state)
  169. {
  170. /* We take a reference, because as soon as we call atomic_set, the
  171. * peer can be freed from below us.
  172. */
  173. struct wg_peer *peer = wg_peer_get(PACKET_PEER(skb));
  174. atomic_set_release(&PACKET_CB(skb)->state, state);
  175. napi_schedule(&peer->napi);
  176. wg_peer_put(peer);
  177. }
  178. #ifdef DEBUG
  179. bool wg_packet_counter_selftest(void);
  180. #endif
  181. #endif /* _WG_QUEUEING_H */