2 * Kernel Connection Multiplexor
4 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
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.
14 #include <linux/skbuff.h>
16 #include <uapi/linux/kcm.h>
18 extern unsigned int kcm_net_id
;
20 #define KCM_STATS_ADD(stat, count) ((stat) += (count))
21 #define KCM_STATS_INCR(stat) ((stat)++)
23 struct kcm_psock_stats
{
24 unsigned long long rx_msgs
;
25 unsigned long long rx_bytes
;
26 unsigned long long tx_msgs
;
27 unsigned long long tx_bytes
;
28 unsigned int rx_aborts
;
29 unsigned int rx_mem_fail
;
30 unsigned int rx_need_more_hdr
;
31 unsigned int rx_msg_too_big
;
32 unsigned int rx_bad_hdr_len
;
33 unsigned long long reserved
;
34 unsigned long long unreserved
;
35 unsigned int tx_aborts
;
38 struct kcm_mux_stats
{
39 unsigned long long rx_msgs
;
40 unsigned long long rx_bytes
;
41 unsigned long long tx_msgs
;
42 unsigned long long tx_bytes
;
43 unsigned int rx_ready_drops
;
44 unsigned int tx_retries
;
45 unsigned int psock_attach
;
46 unsigned int psock_unattach_rsvd
;
47 unsigned int psock_unattach
;
51 unsigned long long rx_msgs
;
52 unsigned long long rx_bytes
;
53 unsigned long long tx_msgs
;
54 unsigned long long tx_bytes
;
60 unsigned int frag_offset
;
61 unsigned int msg_flags
;
62 struct sk_buff
*frag_skb
;
63 struct sk_buff
*last_skb
;
73 /* Socket structure for KCM client sockets */
77 struct list_head kcm_sock_list
;
80 struct work_struct done_work
;
82 struct kcm_stats stats
;
85 struct kcm_psock
*tx_psock
;
86 struct work_struct tx_work
;
87 struct list_head wait_psock_list
;
88 struct sk_buff
*seq_skb
;
90 /* Don't use bit fields here, these are set under different locks */
95 struct kcm_psock
*rx_psock
;
96 struct list_head wait_rx_list
; /* KCMs waiting for receiving */
103 /* Structure for an attached lower socket */
114 void (*save_state_change
)(struct sock
*sk
);
115 void (*save_data_ready
)(struct sock
*sk
);
116 void (*save_write_space
)(struct sock
*sk
);
118 struct list_head psock_list
;
120 struct kcm_psock_stats stats
;
123 struct sk_buff
*rx_skb_head
;
124 struct sk_buff
**rx_skb_nextp
;
125 struct sk_buff
*ready_rx_msg
;
126 struct list_head psock_ready_list
;
127 struct work_struct rx_work
;
128 struct delayed_work rx_delayed_work
;
129 struct bpf_prog
*bpf_prog
;
130 struct kcm_sock
*rx_kcm
;
131 unsigned long long saved_rx_bytes
;
132 unsigned long long saved_rx_msgs
;
133 unsigned int rx_need_bytes
;
136 struct kcm_sock
*tx_kcm
;
137 struct list_head psock_avail_list
;
138 unsigned long long saved_tx_bytes
;
139 unsigned long long saved_tx_msgs
;
142 /* Per net MUX list */
145 struct kcm_psock_stats aggregate_psock_stats
;
146 struct kcm_mux_stats aggregate_mux_stats
;
147 struct list_head mux_list
;
151 /* Structure for a MUX */
153 struct list_head kcm_mux_list
;
155 struct kcm_net
*knet
;
157 struct list_head kcm_socks
; /* All KCM sockets on MUX */
158 int kcm_socks_cnt
; /* Total KCM socket count for MUX */
159 struct list_head psocks
; /* List of all psocks on MUX */
160 int psocks_cnt
; /* Total attached sockets */
162 struct kcm_mux_stats stats
;
163 struct kcm_psock_stats aggregate_psock_stats
;
166 spinlock_t rx_lock ____cacheline_aligned_in_smp
;
167 struct list_head kcm_rx_waiters
; /* KCMs waiting for receiving */
168 struct list_head psocks_ready
; /* List of psocks with a msg ready */
169 struct sk_buff_head rx_hold_queue
;
172 spinlock_t lock ____cacheline_aligned_in_smp
; /* TX and mux locking */
173 struct list_head psocks_avail
; /* List of available psocks */
174 struct list_head kcm_tx_waiters
; /* KCMs waiting for a TX psock */
177 #ifdef CONFIG_PROC_FS
178 int kcm_proc_init(void);
179 void kcm_proc_exit(void);
181 static int kcm_proc_init(void) { return 0; }
182 static void kcm_proc_exit(void) { }
185 static inline void aggregate_psock_stats(struct kcm_psock_stats
*stats
,
186 struct kcm_psock_stats
*agg_stats
)
188 /* Save psock statistics in the mux when psock is being unattached. */
190 #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
191 SAVE_PSOCK_STATS(rx_msgs
);
192 SAVE_PSOCK_STATS(rx_bytes
);
193 SAVE_PSOCK_STATS(rx_aborts
);
194 SAVE_PSOCK_STATS(rx_mem_fail
);
195 SAVE_PSOCK_STATS(rx_need_more_hdr
);
196 SAVE_PSOCK_STATS(rx_msg_too_big
);
197 SAVE_PSOCK_STATS(rx_bad_hdr_len
);
198 SAVE_PSOCK_STATS(tx_msgs
);
199 SAVE_PSOCK_STATS(tx_bytes
);
200 SAVE_PSOCK_STATS(reserved
);
201 SAVE_PSOCK_STATS(unreserved
);
202 SAVE_PSOCK_STATS(tx_aborts
);
203 #undef SAVE_PSOCK_STATS
206 static inline void aggregate_mux_stats(struct kcm_mux_stats
*stats
,
207 struct kcm_mux_stats
*agg_stats
)
209 /* Save psock statistics in the mux when psock is being unattached. */
211 #define SAVE_MUX_STATS(_stat) (agg_stats->_stat += stats->_stat)
212 SAVE_MUX_STATS(rx_msgs
);
213 SAVE_MUX_STATS(rx_bytes
);
214 SAVE_MUX_STATS(tx_msgs
);
215 SAVE_MUX_STATS(tx_bytes
);
216 SAVE_MUX_STATS(rx_ready_drops
);
217 SAVE_MUX_STATS(psock_attach
);
218 SAVE_MUX_STATS(psock_unattach_rsvd
);
219 SAVE_MUX_STATS(psock_unattach
);
220 #undef SAVE_MUX_STATS
223 #endif /* __NET_KCM_H_ */