bb2edf6db896ffe5e05d1333260481a19d37229e
[platform/kernel/linux-rpi.git] / net / rxrpc / proc.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* /proc/net/ support for AF_RXRPC
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #include <linux/module.h>
9 #include <net/sock.h>
10 #include <net/af_rxrpc.h>
11 #include "ar-internal.h"
12
13 static const char *const rxrpc_conn_states[RXRPC_CONN__NR_STATES] = {
14         [RXRPC_CONN_UNUSED]                     = "Unused  ",
15         [RXRPC_CONN_CLIENT]                     = "Client  ",
16         [RXRPC_CONN_SERVICE_PREALLOC]           = "SvPrealc",
17         [RXRPC_CONN_SERVICE_UNSECURED]          = "SvUnsec ",
18         [RXRPC_CONN_SERVICE_CHALLENGING]        = "SvChall ",
19         [RXRPC_CONN_SERVICE]                    = "SvSecure",
20         [RXRPC_CONN_REMOTELY_ABORTED]           = "RmtAbort",
21         [RXRPC_CONN_LOCALLY_ABORTED]            = "LocAbort",
22 };
23
24 /*
25  * generate a list of extant and dead calls in /proc/net/rxrpc_calls
26  */
27 static void *rxrpc_call_seq_start(struct seq_file *seq, loff_t *_pos)
28         __acquires(rcu)
29 {
30         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
31
32         rcu_read_lock();
33         return seq_list_start_head_rcu(&rxnet->calls, *_pos);
34 }
35
36 static void *rxrpc_call_seq_next(struct seq_file *seq, void *v, loff_t *pos)
37 {
38         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
39
40         return seq_list_next_rcu(v, &rxnet->calls, pos);
41 }
42
43 static void rxrpc_call_seq_stop(struct seq_file *seq, void *v)
44         __releases(rcu)
45 {
46         rcu_read_unlock();
47 }
48
49 static int rxrpc_call_seq_show(struct seq_file *seq, void *v)
50 {
51         struct rxrpc_local *local;
52         struct rxrpc_sock *rx;
53         struct rxrpc_peer *peer;
54         struct rxrpc_call *call;
55         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
56         unsigned long timeout = 0;
57         rxrpc_seq_t acks_hard_ack;
58         char lbuff[50], rbuff[50];
59         u64 wtmp;
60
61         if (v == &rxnet->calls) {
62                 seq_puts(seq,
63                          "Proto Local                                          "
64                          " Remote                                         "
65                          " SvID ConnID   CallID   End Use State    Abort   "
66                          " DebugId  TxSeq    TW RxSeq    RW RxSerial RxTimo\n");
67                 return 0;
68         }
69
70         call = list_entry(v, struct rxrpc_call, link);
71
72         rx = rcu_dereference(call->socket);
73         if (rx) {
74                 local = READ_ONCE(rx->local);
75                 if (local)
76                         sprintf(lbuff, "%pISpc", &local->srx.transport);
77                 else
78                         strcpy(lbuff, "no_local");
79         } else {
80                 strcpy(lbuff, "no_socket");
81         }
82
83         peer = call->peer;
84         if (peer)
85                 sprintf(rbuff, "%pISpc", &peer->srx.transport);
86         else
87                 strcpy(rbuff, "no_connection");
88
89         if (call->state != RXRPC_CALL_SERVER_PREALLOC) {
90                 timeout = READ_ONCE(call->expect_rx_by);
91                 timeout -= jiffies;
92         }
93
94         acks_hard_ack = READ_ONCE(call->acks_hard_ack);
95         wtmp   = atomic64_read_acquire(&call->ackr_window);
96         seq_printf(seq,
97                    "UDP   %-47.47s %-47.47s %4x %08x %08x %s %3u"
98                    " %-8.8s %08x %08x %08x %02x %08x %02x %08x %06lx\n",
99                    lbuff,
100                    rbuff,
101                    call->service_id,
102                    call->cid,
103                    call->call_id,
104                    rxrpc_is_service_call(call) ? "Svc" : "Clt",
105                    refcount_read(&call->ref),
106                    rxrpc_call_states[call->state],
107                    call->abort_code,
108                    call->debug_id,
109                    acks_hard_ack, READ_ONCE(call->tx_top) - acks_hard_ack,
110                    lower_32_bits(wtmp), upper_32_bits(wtmp) - lower_32_bits(wtmp),
111                    call->rx_serial,
112                    timeout);
113
114         return 0;
115 }
116
117 const struct seq_operations rxrpc_call_seq_ops = {
118         .start  = rxrpc_call_seq_start,
119         .next   = rxrpc_call_seq_next,
120         .stop   = rxrpc_call_seq_stop,
121         .show   = rxrpc_call_seq_show,
122 };
123
124 /*
125  * generate a list of extant virtual connections in /proc/net/rxrpc_conns
126  */
127 static void *rxrpc_connection_seq_start(struct seq_file *seq, loff_t *_pos)
128         __acquires(rxnet->conn_lock)
129 {
130         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
131
132         read_lock(&rxnet->conn_lock);
133         return seq_list_start_head(&rxnet->conn_proc_list, *_pos);
134 }
135
136 static void *rxrpc_connection_seq_next(struct seq_file *seq, void *v,
137                                        loff_t *pos)
138 {
139         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
140
141         return seq_list_next(v, &rxnet->conn_proc_list, pos);
142 }
143
144 static void rxrpc_connection_seq_stop(struct seq_file *seq, void *v)
145         __releases(rxnet->conn_lock)
146 {
147         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
148
149         read_unlock(&rxnet->conn_lock);
150 }
151
152 static int rxrpc_connection_seq_show(struct seq_file *seq, void *v)
153 {
154         struct rxrpc_connection *conn;
155         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
156         char lbuff[50], rbuff[50];
157
158         if (v == &rxnet->conn_proc_list) {
159                 seq_puts(seq,
160                          "Proto Local                                          "
161                          " Remote                                         "
162                          " SvID ConnID   End Use State    Key     "
163                          " Serial   ISerial  CallId0  CallId1  CallId2  CallId3\n"
164                          );
165                 return 0;
166         }
167
168         conn = list_entry(v, struct rxrpc_connection, proc_link);
169         if (conn->state == RXRPC_CONN_SERVICE_PREALLOC) {
170                 strcpy(lbuff, "no_local");
171                 strcpy(rbuff, "no_connection");
172                 goto print;
173         }
174
175         sprintf(lbuff, "%pISpc", &conn->local->srx.transport);
176
177         sprintf(rbuff, "%pISpc", &conn->peer->srx.transport);
178 print:
179         seq_printf(seq,
180                    "UDP   %-47.47s %-47.47s %4x %08x %s %3u"
181                    " %s %08x %08x %08x %08x %08x %08x %08x\n",
182                    lbuff,
183                    rbuff,
184                    conn->service_id,
185                    conn->proto.cid,
186                    rxrpc_conn_is_service(conn) ? "Svc" : "Clt",
187                    refcount_read(&conn->ref),
188                    rxrpc_conn_states[conn->state],
189                    key_serial(conn->key),
190                    atomic_read(&conn->serial),
191                    conn->hi_serial,
192                    conn->channels[0].call_id,
193                    conn->channels[1].call_id,
194                    conn->channels[2].call_id,
195                    conn->channels[3].call_id);
196
197         return 0;
198 }
199
200 const struct seq_operations rxrpc_connection_seq_ops = {
201         .start  = rxrpc_connection_seq_start,
202         .next   = rxrpc_connection_seq_next,
203         .stop   = rxrpc_connection_seq_stop,
204         .show   = rxrpc_connection_seq_show,
205 };
206
207 /*
208  * generate a list of extant virtual peers in /proc/net/rxrpc/peers
209  */
210 static int rxrpc_peer_seq_show(struct seq_file *seq, void *v)
211 {
212         struct rxrpc_peer *peer;
213         time64_t now;
214         char lbuff[50], rbuff[50];
215
216         if (v == SEQ_START_TOKEN) {
217                 seq_puts(seq,
218                          "Proto Local                                          "
219                          " Remote                                         "
220                          " Use SST   MTU LastUse      RTT      RTO\n"
221                          );
222                 return 0;
223         }
224
225         peer = list_entry(v, struct rxrpc_peer, hash_link);
226
227         sprintf(lbuff, "%pISpc", &peer->local->srx.transport);
228
229         sprintf(rbuff, "%pISpc", &peer->srx.transport);
230
231         now = ktime_get_seconds();
232         seq_printf(seq,
233                    "UDP   %-47.47s %-47.47s %3u"
234                    " %3u %5u %6llus %8u %8u\n",
235                    lbuff,
236                    rbuff,
237                    refcount_read(&peer->ref),
238                    peer->cong_ssthresh,
239                    peer->mtu,
240                    now - peer->last_tx_at,
241                    peer->srtt_us >> 3,
242                    jiffies_to_usecs(peer->rto_j));
243
244         return 0;
245 }
246
247 static void *rxrpc_peer_seq_start(struct seq_file *seq, loff_t *_pos)
248         __acquires(rcu)
249 {
250         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
251         unsigned int bucket, n;
252         unsigned int shift = 32 - HASH_BITS(rxnet->peer_hash);
253         void *p;
254
255         rcu_read_lock();
256
257         if (*_pos >= UINT_MAX)
258                 return NULL;
259
260         n = *_pos & ((1U << shift) - 1);
261         bucket = *_pos >> shift;
262         for (;;) {
263                 if (bucket >= HASH_SIZE(rxnet->peer_hash)) {
264                         *_pos = UINT_MAX;
265                         return NULL;
266                 }
267                 if (n == 0) {
268                         if (bucket == 0)
269                                 return SEQ_START_TOKEN;
270                         *_pos += 1;
271                         n++;
272                 }
273
274                 p = seq_hlist_start_rcu(&rxnet->peer_hash[bucket], n - 1);
275                 if (p)
276                         return p;
277                 bucket++;
278                 n = 1;
279                 *_pos = (bucket << shift) | n;
280         }
281 }
282
283 static void *rxrpc_peer_seq_next(struct seq_file *seq, void *v, loff_t *_pos)
284 {
285         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
286         unsigned int bucket, n;
287         unsigned int shift = 32 - HASH_BITS(rxnet->peer_hash);
288         void *p;
289
290         if (*_pos >= UINT_MAX)
291                 return NULL;
292
293         bucket = *_pos >> shift;
294
295         p = seq_hlist_next_rcu(v, &rxnet->peer_hash[bucket], _pos);
296         if (p)
297                 return p;
298
299         for (;;) {
300                 bucket++;
301                 n = 1;
302                 *_pos = (bucket << shift) | n;
303
304                 if (bucket >= HASH_SIZE(rxnet->peer_hash)) {
305                         *_pos = UINT_MAX;
306                         return NULL;
307                 }
308                 if (n == 0) {
309                         *_pos += 1;
310                         n++;
311                 }
312
313                 p = seq_hlist_start_rcu(&rxnet->peer_hash[bucket], n - 1);
314                 if (p)
315                         return p;
316         }
317 }
318
319 static void rxrpc_peer_seq_stop(struct seq_file *seq, void *v)
320         __releases(rcu)
321 {
322         rcu_read_unlock();
323 }
324
325
326 const struct seq_operations rxrpc_peer_seq_ops = {
327         .start  = rxrpc_peer_seq_start,
328         .next   = rxrpc_peer_seq_next,
329         .stop   = rxrpc_peer_seq_stop,
330         .show   = rxrpc_peer_seq_show,
331 };
332
333 /*
334  * Generate a list of extant virtual local endpoints in /proc/net/rxrpc/locals
335  */
336 static int rxrpc_local_seq_show(struct seq_file *seq, void *v)
337 {
338         struct rxrpc_local *local;
339         char lbuff[50];
340
341         if (v == SEQ_START_TOKEN) {
342                 seq_puts(seq,
343                          "Proto Local                                          "
344                          " Use Act\n");
345                 return 0;
346         }
347
348         local = hlist_entry(v, struct rxrpc_local, link);
349
350         sprintf(lbuff, "%pISpc", &local->srx.transport);
351
352         seq_printf(seq,
353                    "UDP   %-47.47s %3u %3u\n",
354                    lbuff,
355                    refcount_read(&local->ref),
356                    atomic_read(&local->active_users));
357
358         return 0;
359 }
360
361 static void *rxrpc_local_seq_start(struct seq_file *seq, loff_t *_pos)
362         __acquires(rcu)
363 {
364         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
365         unsigned int n;
366
367         rcu_read_lock();
368
369         if (*_pos >= UINT_MAX)
370                 return NULL;
371
372         n = *_pos;
373         if (n == 0)
374                 return SEQ_START_TOKEN;
375
376         return seq_hlist_start_rcu(&rxnet->local_endpoints, n - 1);
377 }
378
379 static void *rxrpc_local_seq_next(struct seq_file *seq, void *v, loff_t *_pos)
380 {
381         struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
382
383         if (*_pos >= UINT_MAX)
384                 return NULL;
385
386         return seq_hlist_next_rcu(v, &rxnet->local_endpoints, _pos);
387 }
388
389 static void rxrpc_local_seq_stop(struct seq_file *seq, void *v)
390         __releases(rcu)
391 {
392         rcu_read_unlock();
393 }
394
395 const struct seq_operations rxrpc_local_seq_ops = {
396         .start  = rxrpc_local_seq_start,
397         .next   = rxrpc_local_seq_next,
398         .stop   = rxrpc_local_seq_stop,
399         .show   = rxrpc_local_seq_show,
400 };
401
402 /*
403  * Display stats in /proc/net/rxrpc/stats
404  */
405 int rxrpc_stats_show(struct seq_file *seq, void *v)
406 {
407         struct rxrpc_net *rxnet = rxrpc_net(seq_file_single_net(seq));
408
409         seq_printf(seq,
410                    "Data     : send=%u sendf=%u\n",
411                    atomic_read(&rxnet->stat_tx_data_send),
412                    atomic_read(&rxnet->stat_tx_data_send_frag));
413         seq_printf(seq,
414                    "Data-Tx  : nr=%u retrans=%u\n",
415                    atomic_read(&rxnet->stat_tx_data),
416                    atomic_read(&rxnet->stat_tx_data_retrans));
417         seq_printf(seq,
418                    "Data-Rx  : nr=%u reqack=%u jumbo=%u\n",
419                    atomic_read(&rxnet->stat_rx_data),
420                    atomic_read(&rxnet->stat_rx_data_reqack),
421                    atomic_read(&rxnet->stat_rx_data_jumbo));
422         seq_printf(seq,
423                    "Ack      : fill=%u send=%u skip=%u\n",
424                    atomic_read(&rxnet->stat_tx_ack_fill),
425                    atomic_read(&rxnet->stat_tx_ack_send),
426                    atomic_read(&rxnet->stat_tx_ack_skip));
427         seq_printf(seq,
428                    "Ack-Tx   : req=%u dup=%u oos=%u exw=%u nos=%u png=%u prs=%u dly=%u idl=%u\n",
429                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_REQUESTED]),
430                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_DUPLICATE]),
431                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_OUT_OF_SEQUENCE]),
432                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_EXCEEDS_WINDOW]),
433                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_NOSPACE]),
434                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_PING]),
435                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_PING_RESPONSE]),
436                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_DELAY]),
437                    atomic_read(&rxnet->stat_tx_acks[RXRPC_ACK_IDLE]));
438         seq_printf(seq,
439                    "Ack-Rx   : req=%u dup=%u oos=%u exw=%u nos=%u png=%u prs=%u dly=%u idl=%u\n",
440                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_REQUESTED]),
441                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_DUPLICATE]),
442                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_OUT_OF_SEQUENCE]),
443                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_EXCEEDS_WINDOW]),
444                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_NOSPACE]),
445                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_PING]),
446                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_PING_RESPONSE]),
447                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_DELAY]),
448                    atomic_read(&rxnet->stat_rx_acks[RXRPC_ACK_IDLE]));
449         seq_printf(seq,
450                    "Why-Req-A: acklost=%u already=%u mrtt=%u ortt=%u\n",
451                    atomic_read(&rxnet->stat_why_req_ack[rxrpc_reqack_ack_lost]),
452                    atomic_read(&rxnet->stat_why_req_ack[rxrpc_reqack_already_on]),
453                    atomic_read(&rxnet->stat_why_req_ack[rxrpc_reqack_more_rtt]),
454                    atomic_read(&rxnet->stat_why_req_ack[rxrpc_reqack_old_rtt]));
455         seq_printf(seq,
456                    "Why-Req-A: nolast=%u retx=%u slows=%u smtxw=%u\n",
457                    atomic_read(&rxnet->stat_why_req_ack[rxrpc_reqack_no_srv_last]),
458                    atomic_read(&rxnet->stat_why_req_ack[rxrpc_reqack_retrans]),
459                    atomic_read(&rxnet->stat_why_req_ack[rxrpc_reqack_slow_start]),
460                    atomic_read(&rxnet->stat_why_req_ack[rxrpc_reqack_small_txwin]));
461         seq_printf(seq,
462                    "Buffers  : txb=%u rxb=%u\n",
463                    atomic_read(&rxrpc_nr_txbuf),
464                    atomic_read(&rxrpc_n_rx_skbs));
465         return 0;
466 }
467
468 /*
469  * Clear stats if /proc/net/rxrpc/stats is written to.
470  */
471 int rxrpc_stats_clear(struct file *file, char *buf, size_t size)
472 {
473         struct seq_file *m = file->private_data;
474         struct rxrpc_net *rxnet = rxrpc_net(seq_file_single_net(m));
475
476         if (size > 1 || (size == 1 && buf[0] != '\n'))
477                 return -EINVAL;
478
479         atomic_set(&rxnet->stat_tx_data, 0);
480         atomic_set(&rxnet->stat_tx_data_retrans, 0);
481         atomic_set(&rxnet->stat_tx_data_send, 0);
482         atomic_set(&rxnet->stat_tx_data_send_frag, 0);
483         atomic_set(&rxnet->stat_rx_data, 0);
484         atomic_set(&rxnet->stat_rx_data_reqack, 0);
485         atomic_set(&rxnet->stat_rx_data_jumbo, 0);
486
487         atomic_set(&rxnet->stat_tx_ack_fill, 0);
488         atomic_set(&rxnet->stat_tx_ack_send, 0);
489         atomic_set(&rxnet->stat_tx_ack_skip, 0);
490         memset(&rxnet->stat_tx_acks, 0, sizeof(rxnet->stat_tx_acks));
491         memset(&rxnet->stat_rx_acks, 0, sizeof(rxnet->stat_rx_acks));
492
493         memset(&rxnet->stat_why_req_ack, 0, sizeof(rxnet->stat_why_req_ack));
494         return size;
495 }