2 #include <linux/inet.h>
3 #include <linux/list.h>
4 #include <linux/module.h>
6 #include <linux/proc_fs.h>
7 #include <linux/rculist.h>
8 #include <linux/seq_file.h>
9 #include <linux/socket.h>
10 #include <net/inet_sock.h>
12 #include <net/net_namespace.h>
13 #include <net/netns/generic.h>
17 struct kcm_seq_muxinfo
{
19 const struct file_operations
*seq_fops
;
20 const struct seq_operations seq_ops
;
23 static struct kcm_mux
*kcm_get_first(struct seq_file
*seq
)
25 struct net
*net
= seq_file_net(seq
);
26 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
28 return list_first_or_null_rcu(&knet
->mux_list
,
29 struct kcm_mux
, kcm_mux_list
);
32 static struct kcm_mux
*kcm_get_next(struct kcm_mux
*mux
)
34 struct kcm_net
*knet
= mux
->knet
;
36 return list_next_or_null_rcu(&knet
->mux_list
, &mux
->kcm_mux_list
,
37 struct kcm_mux
, kcm_mux_list
);
40 static struct kcm_mux
*kcm_get_idx(struct seq_file
*seq
, loff_t pos
)
42 struct net
*net
= seq_file_net(seq
);
43 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
46 list_for_each_entry_rcu(m
, &knet
->mux_list
, kcm_mux_list
) {
54 static void *kcm_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
58 if (v
== SEQ_START_TOKEN
)
59 p
= kcm_get_first(seq
);
66 static void *kcm_seq_start(struct seq_file
*seq
, loff_t
*pos
)
72 return SEQ_START_TOKEN
;
74 return kcm_get_idx(seq
, *pos
- 1);
77 static void kcm_seq_stop(struct seq_file
*seq
, void *v
)
83 struct kcm_proc_mux_state
{
84 struct seq_net_private p
;
88 static int kcm_seq_open(struct inode
*inode
, struct file
*file
)
90 struct kcm_seq_muxinfo
*muxinfo
= PDE_DATA(inode
);
92 return seq_open_net(inode
, file
, &muxinfo
->seq_ops
,
93 sizeof(struct kcm_proc_mux_state
));
96 static void kcm_format_mux_header(struct seq_file
*seq
)
98 struct net
*net
= seq_file_net(seq
);
99 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
102 "*** KCM statistics (%d MUX) ****\n",
106 "%-14s %-10s %-16s %-10s %-16s %-8s %-8s %-8s %-8s %s",
118 /* XXX: pdsts header stuff here */
122 static void kcm_format_sock(struct kcm_sock
*kcm
, struct seq_file
*seq
,
126 " kcm-%-7u %-10llu %-16llu %-10llu %-16llu %-8d %-8d %-8d %-8s ",
132 kcm
->sk
.sk_receive_queue
.qlen
,
133 sk_rmem_alloc_get(&kcm
->sk
),
134 kcm
->sk
.sk_write_queue
.qlen
,
138 seq_printf(seq
, "Psck-%u ", kcm
->tx_psock
->index
);
141 seq_puts(seq
, "TxWait ");
143 if (kcm
->tx_wait_more
)
144 seq_puts(seq
, "WMore ");
147 seq_puts(seq
, "RxWait ");
152 static void kcm_format_psock(struct kcm_psock
*psock
, struct seq_file
*seq
,
156 " psock-%-5u %-10llu %-16llu %-10llu %-16llu %-8d %-8d %-8d %-8d ",
158 psock
->strp
.stats
.rx_msgs
,
159 psock
->strp
.stats
.rx_bytes
,
160 psock
->stats
.tx_msgs
,
161 psock
->stats
.tx_bytes
,
162 psock
->sk
->sk_receive_queue
.qlen
,
163 atomic_read(&psock
->sk
->sk_rmem_alloc
),
164 psock
->sk
->sk_write_queue
.qlen
,
165 atomic_read(&psock
->sk
->sk_wmem_alloc
));
168 seq_puts(seq
, "Done ");
170 if (psock
->tx_stopped
)
171 seq_puts(seq
, "TxStop ");
173 if (psock
->strp
.rx_stopped
)
174 seq_puts(seq
, "RxStop ");
177 seq_printf(seq
, "Rsvd-%d ", psock
->tx_kcm
->index
);
179 if (!psock
->strp
.rx_paused
&& !psock
->ready_rx_msg
) {
180 if (psock
->sk
->sk_receive_queue
.qlen
) {
181 if (psock
->strp
.rx_need_bytes
)
182 seq_printf(seq
, "RxWait=%u ",
183 psock
->strp
.rx_need_bytes
);
185 seq_printf(seq
, "RxWait ");
188 if (psock
->strp
.rx_paused
)
189 seq_puts(seq
, "RxPause ");
191 if (psock
->ready_rx_msg
)
192 seq_puts(seq
, "RdyRx ");
199 kcm_format_mux(struct kcm_mux
*mux
, loff_t idx
, struct seq_file
*seq
)
202 struct kcm_sock
*kcm
;
203 struct kcm_psock
*psock
;
205 /* mux information */
207 "%-6s%-8s %-10llu %-16llu %-10llu %-16llu %-8s %-8s %-8s %-8s ",
215 seq_printf(seq
, "KCMs: %d, Psocks %d\n",
216 mux
->kcm_socks_cnt
, mux
->psocks_cnt
);
218 /* kcm sock information */
220 spin_lock_bh(&mux
->lock
);
221 list_for_each_entry(kcm
, &mux
->kcm_socks
, kcm_sock_list
) {
222 kcm_format_sock(kcm
, seq
, i
, &len
);
226 list_for_each_entry(psock
, &mux
->psocks
, psock_list
) {
227 kcm_format_psock(psock
, seq
, i
, &len
);
230 spin_unlock_bh(&mux
->lock
);
233 static int kcm_seq_show(struct seq_file
*seq
, void *v
)
235 struct kcm_proc_mux_state
*mux_state
;
237 mux_state
= seq
->private;
238 if (v
== SEQ_START_TOKEN
) {
240 kcm_format_mux_header(seq
);
242 kcm_format_mux(v
, mux_state
->idx
, seq
);
248 static const struct file_operations kcm_seq_fops
= {
249 .owner
= THIS_MODULE
,
250 .open
= kcm_seq_open
,
253 .release
= seq_release_net
,
256 static struct kcm_seq_muxinfo kcm_seq_muxinfo
= {
258 .seq_fops
= &kcm_seq_fops
,
260 .show
= kcm_seq_show
,
261 .start
= kcm_seq_start
,
262 .next
= kcm_seq_next
,
263 .stop
= kcm_seq_stop
,
267 static int kcm_proc_register(struct net
*net
, struct kcm_seq_muxinfo
*muxinfo
)
269 struct proc_dir_entry
*p
;
272 p
= proc_create_data(muxinfo
->name
, S_IRUGO
, net
->proc_net
,
273 muxinfo
->seq_fops
, muxinfo
);
278 EXPORT_SYMBOL(kcm_proc_register
);
280 static void kcm_proc_unregister(struct net
*net
,
281 struct kcm_seq_muxinfo
*muxinfo
)
283 remove_proc_entry(muxinfo
->name
, net
->proc_net
);
285 EXPORT_SYMBOL(kcm_proc_unregister
);
287 static int kcm_stats_seq_show(struct seq_file
*seq
, void *v
)
289 struct kcm_psock_stats psock_stats
;
290 struct kcm_mux_stats mux_stats
;
291 struct strp_aggr_stats strp_stats
;
293 struct kcm_psock
*psock
;
294 struct net
*net
= seq
->private;
295 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
297 memset(&mux_stats
, 0, sizeof(mux_stats
));
298 memset(&psock_stats
, 0, sizeof(psock_stats
));
299 memset(&strp_stats
, 0, sizeof(strp_stats
));
301 mutex_lock(&knet
->mutex
);
303 aggregate_mux_stats(&knet
->aggregate_mux_stats
, &mux_stats
);
304 aggregate_psock_stats(&knet
->aggregate_psock_stats
,
306 aggregate_strp_stats(&knet
->aggregate_strp_stats
,
309 list_for_each_entry_rcu(mux
, &knet
->mux_list
, kcm_mux_list
) {
310 spin_lock_bh(&mux
->lock
);
311 aggregate_mux_stats(&mux
->stats
, &mux_stats
);
312 aggregate_psock_stats(&mux
->aggregate_psock_stats
,
314 aggregate_strp_stats(&mux
->aggregate_strp_stats
,
316 list_for_each_entry(psock
, &mux
->psocks
, psock_list
) {
317 aggregate_psock_stats(&psock
->stats
, &psock_stats
);
318 save_strp_stats(&psock
->strp
, &strp_stats
);
321 spin_unlock_bh(&mux
->lock
);
324 mutex_unlock(&knet
->mutex
);
327 "%-8s %-10s %-16s %-10s %-16s %-10s %-10s %-10s %-10s %-10s\n",
340 "%-8s %-10llu %-16llu %-10llu %-16llu %-10u %-10u %-10u %-10u %-10u\n",
346 mux_stats
.tx_retries
,
347 mux_stats
.psock_attach
,
348 mux_stats
.psock_unattach_rsvd
,
349 mux_stats
.psock_unattach
,
350 mux_stats
.rx_ready_drops
);
353 "%-8s %-10s %-16s %-10s %-16s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
372 "%-8s %-10llu %-16llu %-10llu %-16llu %-10llu %-10llu %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u\n",
377 psock_stats
.tx_bytes
,
378 psock_stats
.reserved
,
379 psock_stats
.unreserved
,
380 strp_stats
.rx_aborts
,
381 strp_stats
.rx_interrupted
,
382 strp_stats
.rx_unrecov_intr
,
383 strp_stats
.rx_mem_fail
,
384 strp_stats
.rx_need_more_hdr
,
385 strp_stats
.rx_bad_hdr_len
,
386 strp_stats
.rx_msg_too_big
,
387 strp_stats
.rx_msg_timeouts
,
388 psock_stats
.tx_aborts
);
393 static int kcm_stats_seq_open(struct inode
*inode
, struct file
*file
)
395 return single_open_net(inode
, file
, kcm_stats_seq_show
);
398 static const struct file_operations kcm_stats_seq_fops
= {
399 .owner
= THIS_MODULE
,
400 .open
= kcm_stats_seq_open
,
403 .release
= single_release_net
,
406 static int kcm_proc_init_net(struct net
*net
)
410 if (!proc_create("kcm_stats", S_IRUGO
, net
->proc_net
,
411 &kcm_stats_seq_fops
)) {
416 err
= kcm_proc_register(net
, &kcm_seq_muxinfo
);
423 remove_proc_entry("kcm_stats", net
->proc_net
);
428 static void kcm_proc_exit_net(struct net
*net
)
430 kcm_proc_unregister(net
, &kcm_seq_muxinfo
);
431 remove_proc_entry("kcm_stats", net
->proc_net
);
434 static struct pernet_operations kcm_net_ops
= {
435 .init
= kcm_proc_init_net
,
436 .exit
= kcm_proc_exit_net
,
439 int __init
kcm_proc_init(void)
441 return register_pernet_subsys(&kcm_net_ops
);
444 void __exit
kcm_proc_exit(void)
446 unregister_pernet_subsys(&kcm_net_ops
);
449 #endif /* CONFIG_PROC_FS */