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
);
93 err
= seq_open_net(inode
, file
, &muxinfo
->seq_ops
,
94 sizeof(struct kcm_proc_mux_state
));
100 static void kcm_format_mux_header(struct seq_file
*seq
)
102 struct net
*net
= seq_file_net(seq
);
103 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
106 "*** KCM statistics (%d MUX) ****\n",
110 "%-14s %-10s %-16s %-10s %-16s %-8s %-8s %-8s %-8s %s",
122 /* XXX: pdsts header stuff here */
126 static void kcm_format_sock(struct kcm_sock
*kcm
, struct seq_file
*seq
,
130 " kcm-%-7u %-10llu %-16llu %-10llu %-16llu %-8d %-8d %-8d %-8s ",
136 kcm
->sk
.sk_receive_queue
.qlen
,
137 sk_rmem_alloc_get(&kcm
->sk
),
138 kcm
->sk
.sk_write_queue
.qlen
,
142 seq_printf(seq
, "Psck-%u ", kcm
->tx_psock
->index
);
145 seq_puts(seq
, "TxWait ");
147 if (kcm
->tx_wait_more
)
148 seq_puts(seq
, "WMore ");
151 seq_puts(seq
, "RxWait ");
156 static void kcm_format_psock(struct kcm_psock
*psock
, struct seq_file
*seq
,
160 " psock-%-5u %-10llu %-16llu %-10llu %-16llu %-8d %-8d %-8d %-8d ",
162 psock
->stats
.rx_msgs
,
163 psock
->stats
.rx_bytes
,
164 psock
->stats
.tx_msgs
,
165 psock
->stats
.tx_bytes
,
166 psock
->sk
->sk_receive_queue
.qlen
,
167 atomic_read(&psock
->sk
->sk_rmem_alloc
),
168 psock
->sk
->sk_write_queue
.qlen
,
169 atomic_read(&psock
->sk
->sk_wmem_alloc
));
172 seq_puts(seq
, "Done ");
174 if (psock
->tx_stopped
)
175 seq_puts(seq
, "TxStop ");
177 if (psock
->rx_stopped
)
178 seq_puts(seq
, "RxStop ");
181 seq_printf(seq
, "Rsvd-%d ", psock
->tx_kcm
->index
);
183 if (psock
->ready_rx_msg
)
184 seq_puts(seq
, "RdyRx ");
190 kcm_format_mux(struct kcm_mux
*mux
, loff_t idx
, struct seq_file
*seq
)
193 struct kcm_sock
*kcm
;
194 struct kcm_psock
*psock
;
196 /* mux information */
198 "%-6s%-8s %-10llu %-16llu %-10llu %-16llu %-8s %-8s %-8s %-8s ",
206 seq_printf(seq
, "KCMs: %d, Psocks %d\n",
207 mux
->kcm_socks_cnt
, mux
->psocks_cnt
);
209 /* kcm sock information */
211 spin_lock_bh(&mux
->lock
);
212 list_for_each_entry(kcm
, &mux
->kcm_socks
, kcm_sock_list
) {
213 kcm_format_sock(kcm
, seq
, i
, &len
);
217 list_for_each_entry(psock
, &mux
->psocks
, psock_list
) {
218 kcm_format_psock(psock
, seq
, i
, &len
);
221 spin_unlock_bh(&mux
->lock
);
224 static int kcm_seq_show(struct seq_file
*seq
, void *v
)
226 struct kcm_proc_mux_state
*mux_state
;
228 mux_state
= seq
->private;
229 if (v
== SEQ_START_TOKEN
) {
231 kcm_format_mux_header(seq
);
233 kcm_format_mux(v
, mux_state
->idx
, seq
);
239 static const struct file_operations kcm_seq_fops
= {
240 .owner
= THIS_MODULE
,
241 .open
= kcm_seq_open
,
246 static struct kcm_seq_muxinfo kcm_seq_muxinfo
= {
248 .seq_fops
= &kcm_seq_fops
,
250 .show
= kcm_seq_show
,
251 .start
= kcm_seq_start
,
252 .next
= kcm_seq_next
,
253 .stop
= kcm_seq_stop
,
257 static int kcm_proc_register(struct net
*net
, struct kcm_seq_muxinfo
*muxinfo
)
259 struct proc_dir_entry
*p
;
262 p
= proc_create_data(muxinfo
->name
, S_IRUGO
, net
->proc_net
,
263 muxinfo
->seq_fops
, muxinfo
);
268 EXPORT_SYMBOL(kcm_proc_register
);
270 static void kcm_proc_unregister(struct net
*net
,
271 struct kcm_seq_muxinfo
*muxinfo
)
273 remove_proc_entry(muxinfo
->name
, net
->proc_net
);
275 EXPORT_SYMBOL(kcm_proc_unregister
);
277 static int kcm_stats_seq_show(struct seq_file
*seq
, void *v
)
279 struct kcm_psock_stats psock_stats
;
280 struct kcm_mux_stats mux_stats
;
282 struct kcm_psock
*psock
;
283 struct net
*net
= seq
->private;
284 struct kcm_net
*knet
= net_generic(net
, kcm_net_id
);
286 memset(&mux_stats
, 0, sizeof(mux_stats
));
287 memset(&psock_stats
, 0, sizeof(psock_stats
));
289 mutex_lock(&knet
->mutex
);
291 aggregate_mux_stats(&knet
->aggregate_mux_stats
, &mux_stats
);
292 aggregate_psock_stats(&knet
->aggregate_psock_stats
,
295 list_for_each_entry_rcu(mux
, &knet
->mux_list
, kcm_mux_list
) {
296 spin_lock_bh(&mux
->lock
);
297 aggregate_mux_stats(&mux
->stats
, &mux_stats
);
298 aggregate_psock_stats(&mux
->aggregate_psock_stats
,
300 list_for_each_entry(psock
, &mux
->psocks
, psock_list
)
301 aggregate_psock_stats(&psock
->stats
, &psock_stats
);
302 spin_unlock_bh(&mux
->lock
);
305 mutex_unlock(&knet
->mutex
);
308 "%-8s %-10s %-16s %-10s %-16s %-10s %-10s %-10s %-10s %-10s\n",
321 "%-8s %-10llu %-16llu %-10llu %-16llu %-10u %-10u %-10u %-10u %-10u\n",
327 mux_stats
.tx_retries
,
328 mux_stats
.psock_attach
,
329 mux_stats
.psock_unattach_rsvd
,
330 mux_stats
.psock_unattach
,
331 mux_stats
.rx_ready_drops
);
334 "%-8s %-10s %-16s %-10s %-16s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
351 "%-8s %-10llu %-16llu %-10llu %-16llu %-10llu %-10llu %-10u %-10u %-10u %-10u %-10u %-10u %-10u\n",
354 psock_stats
.rx_bytes
,
356 psock_stats
.tx_bytes
,
357 psock_stats
.reserved
,
358 psock_stats
.unreserved
,
359 psock_stats
.rx_aborts
,
360 psock_stats
.rx_mem_fail
,
361 psock_stats
.rx_need_more_hdr
,
362 psock_stats
.rx_bad_hdr_len
,
363 psock_stats
.rx_msg_too_big
,
364 psock_stats
.rx_msg_timeouts
,
365 psock_stats
.tx_aborts
);
370 static int kcm_stats_seq_open(struct inode
*inode
, struct file
*file
)
372 return single_open_net(inode
, file
, kcm_stats_seq_show
);
375 static const struct file_operations kcm_stats_seq_fops
= {
376 .owner
= THIS_MODULE
,
377 .open
= kcm_stats_seq_open
,
380 .release
= single_release_net
,
383 static int kcm_proc_init_net(struct net
*net
)
387 if (!proc_create("kcm_stats", S_IRUGO
, net
->proc_net
,
388 &kcm_stats_seq_fops
)) {
393 err
= kcm_proc_register(net
, &kcm_seq_muxinfo
);
400 remove_proc_entry("kcm_stats", net
->proc_net
);
405 static void kcm_proc_exit_net(struct net
*net
)
407 kcm_proc_unregister(net
, &kcm_seq_muxinfo
);
408 remove_proc_entry("kcm_stats", net
->proc_net
);
411 static struct pernet_operations kcm_net_ops
= {
412 .init
= kcm_proc_init_net
,
413 .exit
= kcm_proc_exit_net
,
416 int __init
kcm_proc_init(void)
418 return register_pernet_subsys(&kcm_net_ops
);
421 void __exit
kcm_proc_exit(void)
423 unregister_pernet_subsys(&kcm_net_ops
);
426 #endif /* CONFIG_PROC_FS */