kcm.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <net/strparser.h>
  15. #include <uapi/linux/kcm.h>
  16. extern unsigned int kcm_net_id;
  17. #define KCM_STATS_ADD(stat, count) ((stat) += (count))
  18. #define KCM_STATS_INCR(stat) ((stat)++)
  19. struct kcm_psock_stats {
  20. unsigned long long tx_msgs;
  21. unsigned long long tx_bytes;
  22. unsigned long long reserved;
  23. unsigned long long unreserved;
  24. unsigned int tx_aborts;
  25. };
  26. struct kcm_mux_stats {
  27. unsigned long long rx_msgs;
  28. unsigned long long rx_bytes;
  29. unsigned long long tx_msgs;
  30. unsigned long long tx_bytes;
  31. unsigned int rx_ready_drops;
  32. unsigned int tx_retries;
  33. unsigned int psock_attach;
  34. unsigned int psock_unattach_rsvd;
  35. unsigned int psock_unattach;
  36. };
  37. struct kcm_stats {
  38. unsigned long long rx_msgs;
  39. unsigned long long rx_bytes;
  40. unsigned long long tx_msgs;
  41. unsigned long long tx_bytes;
  42. };
  43. struct kcm_tx_msg {
  44. unsigned int sent;
  45. unsigned int fragidx;
  46. unsigned int frag_offset;
  47. unsigned int msg_flags;
  48. struct sk_buff *frag_skb;
  49. struct sk_buff *last_skb;
  50. };
  51. /* Socket structure for KCM client sockets */
  52. struct kcm_sock {
  53. struct sock sk;
  54. struct kcm_mux *mux;
  55. struct list_head kcm_sock_list;
  56. int index;
  57. u32 done : 1;
  58. struct work_struct done_work;
  59. struct kcm_stats stats;
  60. /* Transmit */
  61. struct kcm_psock *tx_psock;
  62. struct work_struct tx_work;
  63. struct list_head wait_psock_list;
  64. struct sk_buff *seq_skb;
  65. u32 tx_stopped : 1;
  66. /* Don't use bit fields here, these are set under different locks */
  67. bool tx_wait;
  68. bool tx_wait_more;
  69. /* Receive */
  70. struct kcm_psock *rx_psock;
  71. struct list_head wait_rx_list; /* KCMs waiting for receiving */
  72. bool rx_wait;
  73. u32 rx_disabled : 1;
  74. };
  75. struct bpf_prog;
  76. /* Structure for an attached lower socket */
  77. struct kcm_psock {
  78. struct sock *sk;
  79. struct strparser strp;
  80. struct kcm_mux *mux;
  81. int index;
  82. u32 tx_stopped : 1;
  83. u32 done : 1;
  84. u32 unattaching : 1;
  85. void (*save_state_change)(struct sock *sk);
  86. void (*save_data_ready)(struct sock *sk);
  87. void (*save_write_space)(struct sock *sk);
  88. struct list_head psock_list;
  89. struct kcm_psock_stats stats;
  90. /* Receive */
  91. struct list_head psock_ready_list;
  92. struct bpf_prog *bpf_prog;
  93. struct kcm_sock *rx_kcm;
  94. unsigned long long saved_rx_bytes;
  95. unsigned long long saved_rx_msgs;
  96. struct sk_buff *ready_rx_msg;
  97. /* Transmit */
  98. struct kcm_sock *tx_kcm;
  99. struct list_head psock_avail_list;
  100. unsigned long long saved_tx_bytes;
  101. unsigned long long saved_tx_msgs;
  102. };
  103. /* Per net MUX list */
  104. struct kcm_net {
  105. struct mutex mutex;
  106. struct kcm_psock_stats aggregate_psock_stats;
  107. struct kcm_mux_stats aggregate_mux_stats;
  108. struct strp_aggr_stats aggregate_strp_stats;
  109. struct list_head mux_list;
  110. int count;
  111. };
  112. /* Structure for a MUX */
  113. struct kcm_mux {
  114. struct list_head kcm_mux_list;
  115. struct rcu_head rcu;
  116. struct kcm_net *knet;
  117. struct list_head kcm_socks; /* All KCM sockets on MUX */
  118. int kcm_socks_cnt; /* Total KCM socket count for MUX */
  119. struct list_head psocks; /* List of all psocks on MUX */
  120. int psocks_cnt; /* Total attached sockets */
  121. struct kcm_mux_stats stats;
  122. struct kcm_psock_stats aggregate_psock_stats;
  123. struct strp_aggr_stats aggregate_strp_stats;
  124. /* Receive */
  125. spinlock_t rx_lock ____cacheline_aligned_in_smp;
  126. struct list_head kcm_rx_waiters; /* KCMs waiting for receiving */
  127. struct list_head psocks_ready; /* List of psocks with a msg ready */
  128. struct sk_buff_head rx_hold_queue;
  129. /* Transmit */
  130. spinlock_t lock ____cacheline_aligned_in_smp; /* TX and mux locking */
  131. struct list_head psocks_avail; /* List of available psocks */
  132. struct list_head kcm_tx_waiters; /* KCMs waiting for a TX psock */
  133. };
  134. #ifdef CONFIG_PROC_FS
  135. int kcm_proc_init(void);
  136. void kcm_proc_exit(void);
  137. #else
  138. static inline int kcm_proc_init(void) { return 0; }
  139. static inline void kcm_proc_exit(void) { }
  140. #endif
  141. static inline void aggregate_psock_stats(struct kcm_psock_stats *stats,
  142. struct kcm_psock_stats *agg_stats)
  143. {
  144. /* Save psock statistics in the mux when psock is being unattached. */
  145. #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
  146. SAVE_PSOCK_STATS(tx_msgs);
  147. SAVE_PSOCK_STATS(tx_bytes);
  148. SAVE_PSOCK_STATS(reserved);
  149. SAVE_PSOCK_STATS(unreserved);
  150. SAVE_PSOCK_STATS(tx_aborts);
  151. #undef SAVE_PSOCK_STATS
  152. }
  153. static inline void aggregate_mux_stats(struct kcm_mux_stats *stats,
  154. struct kcm_mux_stats *agg_stats)
  155. {
  156. /* Save psock statistics in the mux when psock is being unattached. */
  157. #define SAVE_MUX_STATS(_stat) (agg_stats->_stat += stats->_stat)
  158. SAVE_MUX_STATS(rx_msgs);
  159. SAVE_MUX_STATS(rx_bytes);
  160. SAVE_MUX_STATS(tx_msgs);
  161. SAVE_MUX_STATS(tx_bytes);
  162. SAVE_MUX_STATS(rx_ready_drops);
  163. SAVE_MUX_STATS(psock_attach);
  164. SAVE_MUX_STATS(psock_unattach_rsvd);
  165. SAVE_MUX_STATS(psock_unattach);
  166. #undef SAVE_MUX_STATS
  167. }
  168. #endif /* __NET_KCM_H_ */