blob: 1fac92543094f1b8e3cd6cc2eac42d925fbca070 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tom Herbertcd6e1112016-03-07 14:11:07 -08002#include <linux/in.h>
3#include <linux/inet.h>
4#include <linux/list.h>
5#include <linux/module.h>
6#include <linux/net.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>
12#include <net/kcm.h>
13#include <net/net_namespace.h>
14#include <net/netns/generic.h>
15#include <net/tcp.h>
16
17#ifdef CONFIG_PROC_FS
18struct kcm_seq_muxinfo {
19 char *name;
20 const struct file_operations *seq_fops;
21 const struct seq_operations seq_ops;
22};
23
24static struct kcm_mux *kcm_get_first(struct seq_file *seq)
25{
26 struct net *net = seq_file_net(seq);
27 struct kcm_net *knet = net_generic(net, kcm_net_id);
28
29 return list_first_or_null_rcu(&knet->mux_list,
30 struct kcm_mux, kcm_mux_list);
31}
32
33static struct kcm_mux *kcm_get_next(struct kcm_mux *mux)
34{
35 struct kcm_net *knet = mux->knet;
36
37 return list_next_or_null_rcu(&knet->mux_list, &mux->kcm_mux_list,
38 struct kcm_mux, kcm_mux_list);
39}
40
41static struct kcm_mux *kcm_get_idx(struct seq_file *seq, loff_t pos)
42{
43 struct net *net = seq_file_net(seq);
44 struct kcm_net *knet = net_generic(net, kcm_net_id);
45 struct kcm_mux *m;
46
47 list_for_each_entry_rcu(m, &knet->mux_list, kcm_mux_list) {
48 if (!pos)
49 return m;
50 --pos;
51 }
52 return NULL;
53}
54
55static void *kcm_seq_next(struct seq_file *seq, void *v, loff_t *pos)
56{
57 void *p;
58
59 if (v == SEQ_START_TOKEN)
60 p = kcm_get_first(seq);
61 else
62 p = kcm_get_next(v);
63 ++*pos;
64 return p;
65}
66
67static void *kcm_seq_start(struct seq_file *seq, loff_t *pos)
68 __acquires(rcu)
69{
70 rcu_read_lock();
71
72 if (!*pos)
73 return SEQ_START_TOKEN;
74 else
75 return kcm_get_idx(seq, *pos - 1);
76}
77
78static void kcm_seq_stop(struct seq_file *seq, void *v)
79 __releases(rcu)
80{
81 rcu_read_unlock();
82}
83
84struct kcm_proc_mux_state {
85 struct seq_net_private p;
86 int idx;
87};
88
89static int kcm_seq_open(struct inode *inode, struct file *file)
90{
91 struct kcm_seq_muxinfo *muxinfo = PDE_DATA(inode);
Tom Herbertcd6e1112016-03-07 14:11:07 -080092
Colin Ian King0a58f472016-07-22 19:04:12 +010093 return seq_open_net(inode, file, &muxinfo->seq_ops,
Tom Herbertcd6e1112016-03-07 14:11:07 -080094 sizeof(struct kcm_proc_mux_state));
Tom Herbertcd6e1112016-03-07 14:11:07 -080095}
96
97static void kcm_format_mux_header(struct seq_file *seq)
98{
99 struct net *net = seq_file_net(seq);
100 struct kcm_net *knet = net_generic(net, kcm_net_id);
101
102 seq_printf(seq,
103 "*** KCM statistics (%d MUX) ****\n",
104 knet->count);
105
106 seq_printf(seq,
107 "%-14s %-10s %-16s %-10s %-16s %-8s %-8s %-8s %-8s %s",
108 "Object",
109 "RX-Msgs",
110 "RX-Bytes",
111 "TX-Msgs",
112 "TX-Bytes",
113 "Recv-Q",
114 "Rmem",
115 "Send-Q",
116 "Smem",
117 "Status");
118
119 /* XXX: pdsts header stuff here */
120 seq_puts(seq, "\n");
121}
122
123static void kcm_format_sock(struct kcm_sock *kcm, struct seq_file *seq,
124 int i, int *len)
125{
126 seq_printf(seq,
127 " kcm-%-7u %-10llu %-16llu %-10llu %-16llu %-8d %-8d %-8d %-8s ",
128 kcm->index,
129 kcm->stats.rx_msgs,
130 kcm->stats.rx_bytes,
131 kcm->stats.tx_msgs,
132 kcm->stats.tx_bytes,
133 kcm->sk.sk_receive_queue.qlen,
134 sk_rmem_alloc_get(&kcm->sk),
135 kcm->sk.sk_write_queue.qlen,
136 "-");
137
138 if (kcm->tx_psock)
139 seq_printf(seq, "Psck-%u ", kcm->tx_psock->index);
140
141 if (kcm->tx_wait)
142 seq_puts(seq, "TxWait ");
143
144 if (kcm->tx_wait_more)
145 seq_puts(seq, "WMore ");
146
147 if (kcm->rx_wait)
148 seq_puts(seq, "RxWait ");
149
150 seq_puts(seq, "\n");
151}
152
153static void kcm_format_psock(struct kcm_psock *psock, struct seq_file *seq,
154 int i, int *len)
155{
156 seq_printf(seq,
157 " psock-%-5u %-10llu %-16llu %-10llu %-16llu %-8d %-8d %-8d %-8d ",
158 psock->index,
Tom Herbertbbb03022017-07-28 16:22:43 -0700159 psock->strp.stats.msgs,
160 psock->strp.stats.bytes,
Tom Herbertcd6e1112016-03-07 14:11:07 -0800161 psock->stats.tx_msgs,
162 psock->stats.tx_bytes,
163 psock->sk->sk_receive_queue.qlen,
164 atomic_read(&psock->sk->sk_rmem_alloc),
165 psock->sk->sk_write_queue.qlen,
Reshetova, Elena14afee42017-06-30 13:08:00 +0300166 refcount_read(&psock->sk->sk_wmem_alloc));
Tom Herbertcd6e1112016-03-07 14:11:07 -0800167
168 if (psock->done)
169 seq_puts(seq, "Done ");
170
171 if (psock->tx_stopped)
172 seq_puts(seq, "TxStop ");
173
Tom Herbertbbb03022017-07-28 16:22:43 -0700174 if (psock->strp.stopped)
Tom Herbertcd6e1112016-03-07 14:11:07 -0800175 seq_puts(seq, "RxStop ");
176
177 if (psock->tx_kcm)
178 seq_printf(seq, "Rsvd-%d ", psock->tx_kcm->index);
179
Tom Herbertbbb03022017-07-28 16:22:43 -0700180 if (!psock->strp.paused && !psock->ready_rx_msg) {
Tom Herbert1616b382016-08-23 11:55:31 -0700181 if (psock->sk->sk_receive_queue.qlen) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700182 if (psock->strp.need_bytes)
Tom Herbert1616b382016-08-23 11:55:31 -0700183 seq_printf(seq, "RxWait=%u ",
Tom Herbertbbb03022017-07-28 16:22:43 -0700184 psock->strp.need_bytes);
Tom Herbert1616b382016-08-23 11:55:31 -0700185 else
186 seq_printf(seq, "RxWait ");
187 }
188 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700189 if (psock->strp.paused)
Tom Herbert1616b382016-08-23 11:55:31 -0700190 seq_puts(seq, "RxPause ");
191
192 if (psock->ready_rx_msg)
193 seq_puts(seq, "RdyRx ");
194 }
Tom Herbertcd6e1112016-03-07 14:11:07 -0800195
196 seq_puts(seq, "\n");
197}
198
199static void
200kcm_format_mux(struct kcm_mux *mux, loff_t idx, struct seq_file *seq)
201{
202 int i, len;
203 struct kcm_sock *kcm;
204 struct kcm_psock *psock;
205
206 /* mux information */
207 seq_printf(seq,
208 "%-6s%-8s %-10llu %-16llu %-10llu %-16llu %-8s %-8s %-8s %-8s ",
209 "mux", "",
210 mux->stats.rx_msgs,
211 mux->stats.rx_bytes,
212 mux->stats.tx_msgs,
213 mux->stats.tx_bytes,
214 "-", "-", "-", "-");
215
216 seq_printf(seq, "KCMs: %d, Psocks %d\n",
217 mux->kcm_socks_cnt, mux->psocks_cnt);
218
219 /* kcm sock information */
220 i = 0;
221 spin_lock_bh(&mux->lock);
222 list_for_each_entry(kcm, &mux->kcm_socks, kcm_sock_list) {
223 kcm_format_sock(kcm, seq, i, &len);
224 i++;
225 }
226 i = 0;
227 list_for_each_entry(psock, &mux->psocks, psock_list) {
228 kcm_format_psock(psock, seq, i, &len);
229 i++;
230 }
231 spin_unlock_bh(&mux->lock);
232}
233
234static int kcm_seq_show(struct seq_file *seq, void *v)
235{
236 struct kcm_proc_mux_state *mux_state;
237
238 mux_state = seq->private;
239 if (v == SEQ_START_TOKEN) {
240 mux_state->idx = 0;
241 kcm_format_mux_header(seq);
242 } else {
243 kcm_format_mux(v, mux_state->idx, seq);
244 mux_state->idx++;
245 }
246 return 0;
247}
248
249static const struct file_operations kcm_seq_fops = {
Tom Herbertcd6e1112016-03-07 14:11:07 -0800250 .open = kcm_seq_open,
251 .read = seq_read,
252 .llseek = seq_lseek,
Jiri Slabyd19af0a2016-06-20 11:36:28 +0200253 .release = seq_release_net,
Tom Herbertcd6e1112016-03-07 14:11:07 -0800254};
255
256static struct kcm_seq_muxinfo kcm_seq_muxinfo = {
257 .name = "kcm",
258 .seq_fops = &kcm_seq_fops,
259 .seq_ops = {
260 .show = kcm_seq_show,
261 .start = kcm_seq_start,
262 .next = kcm_seq_next,
263 .stop = kcm_seq_stop,
264 }
265};
266
267static int kcm_proc_register(struct net *net, struct kcm_seq_muxinfo *muxinfo)
268{
269 struct proc_dir_entry *p;
270 int rc = 0;
271
Joe Perchesd6444062018-03-23 15:54:38 -0700272 p = proc_create_data(muxinfo->name, 0444, net->proc_net,
Tom Herbertcd6e1112016-03-07 14:11:07 -0800273 muxinfo->seq_fops, muxinfo);
274 if (!p)
275 rc = -ENOMEM;
276 return rc;
277}
278EXPORT_SYMBOL(kcm_proc_register);
279
280static void kcm_proc_unregister(struct net *net,
281 struct kcm_seq_muxinfo *muxinfo)
282{
283 remove_proc_entry(muxinfo->name, net->proc_net);
284}
285EXPORT_SYMBOL(kcm_proc_unregister);
286
287static int kcm_stats_seq_show(struct seq_file *seq, void *v)
288{
289 struct kcm_psock_stats psock_stats;
290 struct kcm_mux_stats mux_stats;
Tom Herbert9b738962016-08-15 14:51:02 -0700291 struct strp_aggr_stats strp_stats;
Tom Herbertcd6e1112016-03-07 14:11:07 -0800292 struct kcm_mux *mux;
293 struct kcm_psock *psock;
294 struct net *net = seq->private;
295 struct kcm_net *knet = net_generic(net, kcm_net_id);
296
297 memset(&mux_stats, 0, sizeof(mux_stats));
298 memset(&psock_stats, 0, sizeof(psock_stats));
Tom Herbert9b738962016-08-15 14:51:02 -0700299 memset(&strp_stats, 0, sizeof(strp_stats));
Tom Herbertcd6e1112016-03-07 14:11:07 -0800300
301 mutex_lock(&knet->mutex);
302
303 aggregate_mux_stats(&knet->aggregate_mux_stats, &mux_stats);
304 aggregate_psock_stats(&knet->aggregate_psock_stats,
305 &psock_stats);
Tom Herbert9b738962016-08-15 14:51:02 -0700306 aggregate_strp_stats(&knet->aggregate_strp_stats,
307 &strp_stats);
Tom Herbertcd6e1112016-03-07 14:11:07 -0800308
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,
313 &psock_stats);
Tom Herbert9b738962016-08-15 14:51:02 -0700314 aggregate_strp_stats(&mux->aggregate_strp_stats,
315 &strp_stats);
316 list_for_each_entry(psock, &mux->psocks, psock_list) {
Tom Herbertcd6e1112016-03-07 14:11:07 -0800317 aggregate_psock_stats(&psock->stats, &psock_stats);
Tom Herbert9b738962016-08-15 14:51:02 -0700318 save_strp_stats(&psock->strp, &strp_stats);
319 }
320
Tom Herbertcd6e1112016-03-07 14:11:07 -0800321 spin_unlock_bh(&mux->lock);
322 }
323
324 mutex_unlock(&knet->mutex);
325
326 seq_printf(seq,
327 "%-8s %-10s %-16s %-10s %-16s %-10s %-10s %-10s %-10s %-10s\n",
328 "MUX",
329 "RX-Msgs",
330 "RX-Bytes",
331 "TX-Msgs",
332 "TX-Bytes",
333 "TX-Retries",
334 "Attach",
335 "Unattach",
336 "UnattchRsvd",
337 "RX-RdyDrops");
338
339 seq_printf(seq,
340 "%-8s %-10llu %-16llu %-10llu %-16llu %-10u %-10u %-10u %-10u %-10u\n",
341 "",
342 mux_stats.rx_msgs,
343 mux_stats.rx_bytes,
344 mux_stats.tx_msgs,
345 mux_stats.tx_bytes,
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);
351
352 seq_printf(seq,
Tom Herbert9b738962016-08-15 14:51:02 -0700353 "%-8s %-10s %-16s %-10s %-16s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
Tom Herbertcd6e1112016-03-07 14:11:07 -0800354 "Psock",
355 "RX-Msgs",
356 "RX-Bytes",
357 "TX-Msgs",
358 "TX-Bytes",
359 "Reserved",
360 "Unreserved",
361 "RX-Aborts",
Tom Herbert9b738962016-08-15 14:51:02 -0700362 "RX-Intr",
363 "RX-Unrecov",
Tom Herbertcd6e1112016-03-07 14:11:07 -0800364 "RX-MemFail",
365 "RX-NeedMor",
366 "RX-BadLen",
Tom Herbert7ced95e2016-03-07 14:11:10 -0800367 "RX-TooBig",
Tom Herbert29152a32016-03-07 14:11:11 -0800368 "RX-Timeout",
Tom Herbertcd6e1112016-03-07 14:11:07 -0800369 "TX-Aborts");
370
371 seq_printf(seq,
Tom Herbert9b738962016-08-15 14:51:02 -0700372 "%-8s %-10llu %-16llu %-10llu %-16llu %-10llu %-10llu %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u\n",
Tom Herbertcd6e1112016-03-07 14:11:07 -0800373 "",
Tom Herbertbbb03022017-07-28 16:22:43 -0700374 strp_stats.msgs,
375 strp_stats.bytes,
Tom Herbertcd6e1112016-03-07 14:11:07 -0800376 psock_stats.tx_msgs,
377 psock_stats.tx_bytes,
378 psock_stats.reserved,
379 psock_stats.unreserved,
Tom Herbertbbb03022017-07-28 16:22:43 -0700380 strp_stats.aborts,
381 strp_stats.interrupted,
382 strp_stats.unrecov_intr,
383 strp_stats.mem_fail,
384 strp_stats.need_more_hdr,
385 strp_stats.bad_hdr_len,
386 strp_stats.msg_too_big,
387 strp_stats.msg_timeouts,
Tom Herbertcd6e1112016-03-07 14:11:07 -0800388 psock_stats.tx_aborts);
389
390 return 0;
391}
392
393static int kcm_stats_seq_open(struct inode *inode, struct file *file)
394{
395 return single_open_net(inode, file, kcm_stats_seq_show);
396}
397
398static const struct file_operations kcm_stats_seq_fops = {
Tom Herbertcd6e1112016-03-07 14:11:07 -0800399 .open = kcm_stats_seq_open,
400 .read = seq_read,
401 .llseek = seq_lseek,
402 .release = single_release_net,
403};
404
405static int kcm_proc_init_net(struct net *net)
406{
407 int err;
408
Joe Perchesd6444062018-03-23 15:54:38 -0700409 if (!proc_create("kcm_stats", 0444, net->proc_net,
Tom Herbertcd6e1112016-03-07 14:11:07 -0800410 &kcm_stats_seq_fops)) {
411 err = -ENOMEM;
412 goto out_kcm_stats;
413 }
414
415 err = kcm_proc_register(net, &kcm_seq_muxinfo);
416 if (err)
417 goto out_kcm;
418
419 return 0;
420
421out_kcm:
422 remove_proc_entry("kcm_stats", net->proc_net);
423out_kcm_stats:
424 return err;
425}
426
427static void kcm_proc_exit_net(struct net *net)
428{
429 kcm_proc_unregister(net, &kcm_seq_muxinfo);
430 remove_proc_entry("kcm_stats", net->proc_net);
431}
432
433static struct pernet_operations kcm_net_ops = {
434 .init = kcm_proc_init_net,
435 .exit = kcm_proc_exit_net,
Tom Herbertcd6e1112016-03-07 14:11:07 -0800436};
437
438int __init kcm_proc_init(void)
439{
440 return register_pernet_subsys(&kcm_net_ops);
441}
442
443void __exit kcm_proc_exit(void)
444{
445 unregister_pernet_subsys(&kcm_net_ops);
446}
447
448#endif /* CONFIG_PROC_FS */