1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/inet.h>
4 #include <linux/list.h>
5 #include <linux/module.h>
7 #include <linux/proc_fs.h>
8 #include <linux/rculist.h>
9 #include <linux/seq_file.h>
10 #include <linux/socket.h>
11 #include <net/inet_sock.h>
13 #include <net/net_namespace.h>
14 #include <net/netns/generic.h>
18 static struct kcm_mux
*kcm_get_first(struct seq_file
*seq
)
20 struct net
*net
= seq_file_net(seq
);
21 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
23 return list_first_or_null_rcu(&knet
->mux_list
,
24 struct kcm_mux
, kcm_mux_list
);
27 static struct kcm_mux
*kcm_get_next(struct kcm_mux
*mux
)
29 struct kcm_net
*knet
= mux
->knet
;
31 return list_next_or_null_rcu(&knet
->mux_list
, &mux
->kcm_mux_list
,
32 struct kcm_mux
, kcm_mux_list
);
35 static struct kcm_mux
*kcm_get_idx(struct seq_file
*seq
, loff_t pos
)
37 struct net
*net
= seq_file_net(seq
);
38 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
41 list_for_each_entry_rcu(m
, &knet
->mux_list
, kcm_mux_list
) {
49 static void *kcm_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
53 if (v
== SEQ_START_TOKEN
)
54 p
= kcm_get_first(seq
);
61 static void *kcm_seq_start(struct seq_file
*seq
, loff_t
*pos
)
67 return SEQ_START_TOKEN
;
69 return kcm_get_idx(seq
, *pos
- 1);
72 static void kcm_seq_stop(struct seq_file
*seq
, void *v
)
78 struct kcm_proc_mux_state
{
79 struct seq_net_private p
;
83 static void kcm_format_mux_header(struct seq_file
*seq
)
85 struct net
*net
= seq_file_net(seq
);
86 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
89 "*** KCM statistics (%d MUX) ****\n",
93 "%-14s %-10s %-16s %-10s %-16s %-8s %-8s %-8s %-8s %s",
105 /* XXX: pdsts header stuff here */
109 static void kcm_format_sock(struct kcm_sock
*kcm
, struct seq_file
*seq
,
113 " kcm-%-7u %-10llu %-16llu %-10llu %-16llu %-8d %-8d %-8d %-8s ",
119 kcm
->sk
.sk_receive_queue
.qlen
,
120 sk_rmem_alloc_get(&kcm
->sk
),
121 kcm
->sk
.sk_write_queue
.qlen
,
125 seq_printf(seq
, "Psck-%u ", kcm
->tx_psock
->index
);
128 seq_puts(seq
, "TxWait ");
130 if (kcm
->tx_wait_more
)
131 seq_puts(seq
, "WMore ");
134 seq_puts(seq
, "RxWait ");
139 static void kcm_format_psock(struct kcm_psock
*psock
, struct seq_file
*seq
,
143 " psock-%-5u %-10llu %-16llu %-10llu %-16llu %-8d %-8d %-8d %-8d ",
145 psock
->strp
.stats
.msgs
,
146 psock
->strp
.stats
.bytes
,
147 psock
->stats
.tx_msgs
,
148 psock
->stats
.tx_bytes
,
149 psock
->sk
->sk_receive_queue
.qlen
,
150 atomic_read(&psock
->sk
->sk_rmem_alloc
),
151 psock
->sk
->sk_write_queue
.qlen
,
152 refcount_read(&psock
->sk
->sk_wmem_alloc
));
155 seq_puts(seq
, "Done ");
157 if (psock
->tx_stopped
)
158 seq_puts(seq
, "TxStop ");
160 if (psock
->strp
.stopped
)
161 seq_puts(seq
, "RxStop ");
164 seq_printf(seq
, "Rsvd-%d ", psock
->tx_kcm
->index
);
166 if (!psock
->strp
.paused
&& !psock
->ready_rx_msg
) {
167 if (psock
->sk
->sk_receive_queue
.qlen
) {
168 if (psock
->strp
.need_bytes
)
169 seq_printf(seq
, "RxWait=%u ",
170 psock
->strp
.need_bytes
);
172 seq_printf(seq
, "RxWait ");
175 if (psock
->strp
.paused
)
176 seq_puts(seq
, "RxPause ");
178 if (psock
->ready_rx_msg
)
179 seq_puts(seq
, "RdyRx ");
186 kcm_format_mux(struct kcm_mux
*mux
, loff_t idx
, struct seq_file
*seq
)
189 struct kcm_sock
*kcm
;
190 struct kcm_psock
*psock
;
192 /* mux information */
194 "%-6s%-8s %-10llu %-16llu %-10llu %-16llu %-8s %-8s %-8s %-8s ",
202 seq_printf(seq
, "KCMs: %d, Psocks %d\n",
203 mux
->kcm_socks_cnt
, mux
->psocks_cnt
);
205 /* kcm sock information */
207 spin_lock_bh(&mux
->lock
);
208 list_for_each_entry(kcm
, &mux
->kcm_socks
, kcm_sock_list
) {
209 kcm_format_sock(kcm
, seq
, i
, &len
);
213 list_for_each_entry(psock
, &mux
->psocks
, psock_list
) {
214 kcm_format_psock(psock
, seq
, i
, &len
);
217 spin_unlock_bh(&mux
->lock
);
220 static int kcm_seq_show(struct seq_file
*seq
, void *v
)
222 struct kcm_proc_mux_state
*mux_state
;
224 mux_state
= seq
->private;
225 if (v
== SEQ_START_TOKEN
) {
227 kcm_format_mux_header(seq
);
229 kcm_format_mux(v
, mux_state
->idx
, seq
);
235 static const struct seq_operations kcm_seq_ops
= {
236 .show
= kcm_seq_show
,
237 .start
= kcm_seq_start
,
238 .next
= kcm_seq_next
,
239 .stop
= kcm_seq_stop
,
242 static int kcm_stats_seq_show(struct seq_file
*seq
, void *v
)
244 struct kcm_psock_stats psock_stats
;
245 struct kcm_mux_stats mux_stats
;
246 struct strp_aggr_stats strp_stats
;
248 struct kcm_psock
*psock
;
249 struct net
*net
= seq
->private;
250 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
252 memset(&mux_stats
, 0, sizeof(mux_stats
));
253 memset(&psock_stats
, 0, sizeof(psock_stats
));
254 memset(&strp_stats
, 0, sizeof(strp_stats
));
256 mutex_lock(&knet
->mutex
);
258 aggregate_mux_stats(&knet
->aggregate_mux_stats
, &mux_stats
);
259 aggregate_psock_stats(&knet
->aggregate_psock_stats
,
261 aggregate_strp_stats(&knet
->aggregate_strp_stats
,
264 list_for_each_entry(mux
, &knet
->mux_list
, kcm_mux_list
) {
265 spin_lock_bh(&mux
->lock
);
266 aggregate_mux_stats(&mux
->stats
, &mux_stats
);
267 aggregate_psock_stats(&mux
->aggregate_psock_stats
,
269 aggregate_strp_stats(&mux
->aggregate_strp_stats
,
271 list_for_each_entry(psock
, &mux
->psocks
, psock_list
) {
272 aggregate_psock_stats(&psock
->stats
, &psock_stats
);
273 save_strp_stats(&psock
->strp
, &strp_stats
);
276 spin_unlock_bh(&mux
->lock
);
279 mutex_unlock(&knet
->mutex
);
282 "%-8s %-10s %-16s %-10s %-16s %-10s %-10s %-10s %-10s %-10s\n",
295 "%-8s %-10llu %-16llu %-10llu %-16llu %-10u %-10u %-10u %-10u %-10u\n",
301 mux_stats
.tx_retries
,
302 mux_stats
.psock_attach
,
303 mux_stats
.psock_unattach_rsvd
,
304 mux_stats
.psock_unattach
,
305 mux_stats
.rx_ready_drops
);
308 "%-8s %-10s %-16s %-10s %-16s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
327 "%-8s %-10llu %-16llu %-10llu %-16llu %-10llu %-10llu %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u\n",
332 psock_stats
.tx_bytes
,
333 psock_stats
.reserved
,
334 psock_stats
.unreserved
,
336 strp_stats
.interrupted
,
337 strp_stats
.unrecov_intr
,
339 strp_stats
.need_more_hdr
,
340 strp_stats
.bad_hdr_len
,
341 strp_stats
.msg_too_big
,
342 strp_stats
.msg_timeouts
,
343 psock_stats
.tx_aborts
);
348 static int kcm_proc_init_net(struct net
*net
)
350 if (!proc_create_net_single("kcm_stats", 0444, net
->proc_net
,
351 kcm_stats_seq_show
, NULL
))
354 if (!proc_create_net("kcm", 0444, net
->proc_net
, &kcm_seq_ops
,
355 sizeof(struct kcm_proc_mux_state
)))
361 remove_proc_entry("kcm_stats", net
->proc_net
);
366 static void kcm_proc_exit_net(struct net
*net
)
368 remove_proc_entry("kcm", net
->proc_net
);
369 remove_proc_entry("kcm_stats", net
->proc_net
);
372 static struct pernet_operations kcm_net_ops
= {
373 .init
= kcm_proc_init_net
,
374 .exit
= kcm_proc_exit_net
,
377 int __init
kcm_proc_init(void)
379 return register_pernet_subsys(&kcm_net_ops
);
382 void __exit
kcm_proc_exit(void)
384 unregister_pernet_subsys(&kcm_net_ops
);
387 #endif /* CONFIG_PROC_FS */