kcm.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Kernel Connection Multiplexor
  3. *
  4. * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. */
  10. #ifndef __NET_KCM_H_
  11. #define __NET_KCM_H_
  12. #include <linux/skbuff.h>
  13. #include <net/sock.h>
  14. #include <uapi/linux/kcm.h>
  15. extern unsigned int kcm_net_id;
  16. #define KCM_STATS_ADD(stat, count) ((stat) += (count))
  17. #define KCM_STATS_INCR(stat) ((stat)++)
  18. struct kcm_psock_stats {
  19. unsigned long long rx_msgs;
  20. unsigned long long rx_bytes;
  21. unsigned long long tx_msgs;
  22. unsigned long long tx_bytes;
  23. unsigned int rx_aborts;
  24. unsigned int rx_mem_fail;
  25. unsigned int rx_need_more_hdr;
  26. unsigned int rx_msg_too_big;
  27. unsigned int rx_msg_timeouts;
  28. unsigned int rx_bad_hdr_len;
  29. unsigned long long reserved;
  30. unsigned long long unreserved;
  31. unsigned int tx_aborts;
  32. };
  33. struct kcm_mux_stats {
  34. unsigned long long rx_msgs;
  35. unsigned long long rx_bytes;
  36. unsigned long long tx_msgs;
  37. unsigned long long tx_bytes;
  38. unsigned int rx_ready_drops;
  39. unsigned int tx_retries;
  40. unsigned int psock_attach;
  41. unsigned int psock_unattach_rsvd;
  42. unsigned int psock_unattach;
  43. };
  44. struct kcm_stats {
  45. unsigned long long rx_msgs;
  46. unsigned long long rx_bytes;
  47. unsigned long long tx_msgs;
  48. unsigned long long tx_bytes;
  49. };
  50. struct kcm_tx_msg {
  51. unsigned int sent;
  52. unsigned int fragidx;
  53. unsigned int frag_offset;
  54. unsigned int msg_flags;
  55. struct sk_buff *frag_skb;
  56. struct sk_buff *last_skb;
  57. };
  58. struct kcm_rx_msg {
  59. int full_len;
  60. int accum_len;
  61. int offset;
  62. int early_eaten;
  63. };
  64. /* Socket structure for KCM client sockets */
  65. struct kcm_sock {
  66. struct sock sk;
  67. struct kcm_mux *mux;
  68. struct list_head kcm_sock_list;
  69. int index;
  70. u32 done : 1;
  71. struct work_struct done_work;
  72. struct kcm_stats stats;
  73. /* Transmit */
  74. struct kcm_psock *tx_psock;
  75. struct work_struct tx_work;
  76. struct list_head wait_psock_list;
  77. struct sk_buff *seq_skb;
  78. /* Don't use bit fields here, these are set under different locks */
  79. bool tx_wait;
  80. bool tx_wait_more;
  81. /* Receive */
  82. struct kcm_psock *rx_psock;
  83. struct list_head wait_rx_list; /* KCMs waiting for receiving */
  84. bool rx_wait;
  85. u32 rx_disabled : 1;
  86. };
  87. struct bpf_prog;
  88. /* Structure for an attached lower socket */
  89. struct kcm_psock {
  90. struct sock *sk;
  91. struct kcm_mux *mux;
  92. int index;
  93. u32 tx_stopped : 1;
  94. u32 rx_stopped : 1;
  95. u32 done : 1;
  96. u32 unattaching : 1;
  97. void (*save_state_change)(struct sock *sk);
  98. void (*save_data_ready)(struct sock *sk);
  99. void (*save_write_space)(struct sock *sk);
  100. struct list_head psock_list;
  101. struct kcm_psock_stats stats;
  102. /* Receive */
  103. struct sk_buff *rx_skb_head;
  104. struct sk_buff **rx_skb_nextp;
  105. struct sk_buff *ready_rx_msg;
  106. struct list_head psock_ready_list;
  107. struct work_struct rx_work;
  108. struct delayed_work rx_delayed_work;
  109. struct bpf_prog *bpf_prog;
  110. struct kcm_sock *rx_kcm;
  111. unsigned long long saved_rx_bytes;
  112. unsigned long long saved_rx_msgs;
  113. struct timer_list rx_msg_timer;
  114. unsigned int rx_need_bytes;
  115. /* Transmit */
  116. struct kcm_sock *tx_kcm;
  117. struct list_head psock_avail_list;
  118. unsigned long long saved_tx_bytes;
  119. unsigned long long saved_tx_msgs;
  120. };
  121. /* Per net MUX list */
  122. struct kcm_net {
  123. struct mutex mutex;
  124. struct kcm_psock_stats aggregate_psock_stats;
  125. struct kcm_mux_stats aggregate_mux_stats;
  126. struct list_head mux_list;
  127. int count;
  128. };
  129. /* Structure for a MUX */
  130. struct kcm_mux {
  131. struct list_head kcm_mux_list;
  132. struct rcu_head rcu;
  133. struct kcm_net *knet;
  134. struct list_head kcm_socks; /* All KCM sockets on MUX */
  135. int kcm_socks_cnt; /* Total KCM socket count for MUX */
  136. struct list_head psocks; /* List of all psocks on MUX */
  137. int psocks_cnt; /* Total attached sockets */
  138. struct kcm_mux_stats stats;
  139. struct kcm_psock_stats aggregate_psock_stats;
  140. /* Receive */
  141. spinlock_t rx_lock ____cacheline_aligned_in_smp;
  142. struct list_head kcm_rx_waiters; /* KCMs waiting for receiving */
  143. struct list_head psocks_ready; /* List of psocks with a msg ready */
  144. struct sk_buff_head rx_hold_queue;
  145. /* Transmit */
  146. spinlock_t lock ____cacheline_aligned_in_smp; /* TX and mux locking */
  147. struct list_head psocks_avail; /* List of available psocks */
  148. struct list_head kcm_tx_waiters; /* KCMs waiting for a TX psock */
  149. };
  150. #ifdef CONFIG_PROC_FS
  151. int kcm_proc_init(void);
  152. void kcm_proc_exit(void);
  153. #else
  154. static inline int kcm_proc_init(void) { return 0; }
  155. static inline void kcm_proc_exit(void) { }
  156. #endif
  157. static inline void aggregate_psock_stats(struct kcm_psock_stats *stats,
  158. struct kcm_psock_stats *agg_stats)
  159. {
  160. /* Save psock statistics in the mux when psock is being unattached. */
  161. #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
  162. SAVE_PSOCK_STATS(rx_msgs);
  163. SAVE_PSOCK_STATS(rx_bytes);
  164. SAVE_PSOCK_STATS(rx_aborts);
  165. SAVE_PSOCK_STATS(rx_mem_fail);
  166. SAVE_PSOCK_STATS(rx_need_more_hdr);
  167. SAVE_PSOCK_STATS(rx_msg_too_big);
  168. SAVE_PSOCK_STATS(rx_msg_timeouts);
  169. SAVE_PSOCK_STATS(rx_bad_hdr_len);
  170. SAVE_PSOCK_STATS(tx_msgs);
  171. SAVE_PSOCK_STATS(tx_bytes);
  172. SAVE_PSOCK_STATS(reserved);
  173. SAVE_PSOCK_STATS(unreserved);
  174. SAVE_PSOCK_STATS(tx_aborts);
  175. #undef SAVE_PSOCK_STATS
  176. }
  177. static inline void aggregate_mux_stats(struct kcm_mux_stats *stats,
  178. struct kcm_mux_stats *agg_stats)
  179. {
  180. /* Save psock statistics in the mux when psock is being unattached. */
  181. #define SAVE_MUX_STATS(_stat) (agg_stats->_stat += stats->_stat)
  182. SAVE_MUX_STATS(rx_msgs);
  183. SAVE_MUX_STATS(rx_bytes);
  184. SAVE_MUX_STATS(tx_msgs);
  185. SAVE_MUX_STATS(tx_bytes);
  186. SAVE_MUX_STATS(rx_ready_drops);
  187. SAVE_MUX_STATS(psock_attach);
  188. SAVE_MUX_STATS(psock_unattach_rsvd);
  189. SAVE_MUX_STATS(psock_unattach);
  190. #undef SAVE_MUX_STATS
  191. }
  192. #endif /* __NET_KCM_H_ */