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