xref: /freebsd/usr.bin/systat/sctp.c (revision 4e579ad047720775ab580b74192c7de8a3386fea)
1 /*-
2  * Copyright (c) 2015
3  * The Regents of the University of California.  All rights reserved.
4  * Michael Tuexen.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 
37 #include <netinet/sctp.h>
38 
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "systat.h"
43 #include "extern.h"
44 #include "mode.h"
45 
46 static struct sctpstat curstat, initstat, oldstat;
47 
48 /*-
49 --0         1         2         3         4         5         6         7
50 --0123456789012345678901234567890123456789012345678901234567890123456789012345
51 00             SCTP Associations                     SCTP Packets
52 01999999999999 associations initiated   999999999999 packets sent
53 02999999999999 associations accepted    999999999999 packets received
54 03999999999999 associations restarted   999999999999 - out of the blue
55 04999999999999 associations terminated  999999999999 - bad vtag
56 05999999999999 associations aborted     999999999999 - bad crc32c
57 06
58 07             SCTP Timers                           SCTP Chunks
59 08999999999999 init timeouts            999999999999 control chunks sent
60 09999999999999 cookie timeouts          999999999999 data chunks sent
61 10999999999999 data timeouts            999999999999 - ordered
62 11999999999999 delayed sack timeouts    999999999999 - unordered
63 12999999999999 shutdown timeouts        999999999999 control chunks received
64 13999999999999 shutdown-ack timeouts    999999999999 data chunks received
65 14999999999999 shutdown guard timeouts  999999999999 - ordered
66 15999999999999 heartbeat timeouts       999999999999 - unordered
67 16999999999999 path MTU timeouts
68 17999999999999 autoclose timeouts                    SCTP user messages
69 18999999999999 asconf timeouts          999999999999 fragmented
70 19999999999999 stream reset timeouts    999999999999 reassembled
71 --0123456789012345678901234567890123456789012345678901234567890123456789012345
72 --0         1         2         3         4         5         6         7
73 */
74 
75 WINDOW *
76 opensctp(void)
77 {
78 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
79 }
80 
81 void
82 closesctp(WINDOW *w)
83 {
84 	if (w != NULL) {
85 		wclear(w);
86 		wrefresh(w);
87 		delwin(w);
88 	}
89 }
90 
91 void
92 labelsctp(void)
93 {
94 	wmove(wnd, 0, 0); wclrtoeol(wnd);
95 #define L(row, str) mvwprintw(wnd, row, 13, str)
96 #define R(row, str) mvwprintw(wnd, row, 51, str);
97 	L(0, "SCTP Associations");		R(0, "SCTP Packets");
98 	L(1, "associations initiated");		R(1, "packets sent");
99 	L(2, "associations accepted");		R(2, "packets received");
100 	L(3, "associations restarted");		R(3, "- out of the blue");
101 	L(4, "associations terminated");	R(4, "- bad vtag");
102 	L(5, "associations aborted");		R(5, "- bad crc32c");
103 
104 	L(7, "SCTP Timers");			R(7, "SCTP Chunks");
105 	L(8, "init timeouts");			R(8, "control chunks sent");
106 	L(9, "cookie timeouts");		R(9, "data chunks sent");
107 	L(10, "data timeouts");			R(10, "- ordered");
108 	L(11, "delayed sack timeouts");		R(11, "- unordered");
109 	L(12, "shutdown timeouts");		R(12, "control chunks received");
110 	L(13, "shutdown-ack timeouts");		R(13, "data chunks received");
111 	L(14, "shutdown guard timeouts");	R(14, "- ordered");
112 	L(15, "heartbeat timeouts");		R(15, "- unordered");
113 	L(16, "path MTU timeouts");
114 	L(17, "autoclose timeouts");		R(17, "SCTP User Messages");
115 	L(18, "asconf timeouts");		R(18, "fragmented");
116 	L(19, "stream reset timeouts");		R(19, "reassembled");
117 #undef L
118 #undef R
119 }
120 
121 static void
122 domode(struct sctpstat *ret)
123 {
124 	const struct sctpstat *sub;
125 	int divisor = 1;
126 
127 	switch(currentmode) {
128 	case display_RATE:
129 		sub = &oldstat;
130 		divisor = (delay > 1000000) ? delay / 1000000 : 1;
131 		break;
132 	case display_DELTA:
133 		sub = &oldstat;
134 		break;
135 	case display_SINCE:
136 		sub = &initstat;
137 		break;
138 	default:
139 		*ret = curstat;
140 		return;
141 	}
142 #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
143 	DO(sctps_currestab);
144 	DO(sctps_activeestab);
145 	DO(sctps_restartestab);
146 	DO(sctps_collisionestab);
147 	DO(sctps_passiveestab);
148 	DO(sctps_aborted);
149 	DO(sctps_shutdown);
150 	DO(sctps_outoftheblue);
151 	DO(sctps_checksumerrors);
152 	DO(sctps_outcontrolchunks);
153 	DO(sctps_outorderchunks);
154 	DO(sctps_outunorderchunks);
155 	DO(sctps_incontrolchunks);
156 	DO(sctps_inorderchunks);
157 	DO(sctps_inunorderchunks);
158 	DO(sctps_fragusrmsgs);
159 	DO(sctps_reasmusrmsgs);
160 	DO(sctps_outpackets);
161 	DO(sctps_inpackets);
162 
163 	DO(sctps_recvpackets);
164 	DO(sctps_recvdatagrams);
165 	DO(sctps_recvpktwithdata);
166 	DO(sctps_recvsacks);
167 	DO(sctps_recvdata);
168 	DO(sctps_recvdupdata);
169 	DO(sctps_recvheartbeat);
170 	DO(sctps_recvheartbeatack);
171 	DO(sctps_recvecne);
172 	DO(sctps_recvauth);
173 	DO(sctps_recvauthmissing);
174 	DO(sctps_recvivalhmacid);
175 	DO(sctps_recvivalkeyid);
176 	DO(sctps_recvauthfailed);
177 	DO(sctps_recvexpress);
178 	DO(sctps_recvexpressm);
179 	DO(sctps_recvswcrc);
180 	DO(sctps_recvhwcrc);
181 
182 	DO(sctps_sendpackets);
183 	DO(sctps_sendsacks);
184 	DO(sctps_senddata);
185 	DO(sctps_sendretransdata);
186 	DO(sctps_sendfastretrans);
187 	DO(sctps_sendmultfastretrans);
188 	DO(sctps_sendheartbeat);
189 	DO(sctps_sendecne);
190 	DO(sctps_sendauth);
191 	DO(sctps_senderrors);
192 	DO(sctps_sendswcrc);
193 	DO(sctps_sendhwcrc);
194 
195 	DO(sctps_pdrpfmbox);
196 	DO(sctps_pdrpfehos);
197 	DO(sctps_pdrpmbda);
198 	DO(sctps_pdrpmbct);
199 	DO(sctps_pdrpbwrpt);
200 	DO(sctps_pdrpcrupt);
201 	DO(sctps_pdrpnedat);
202 	DO(sctps_pdrppdbrk);
203 	DO(sctps_pdrptsnnf);
204 	DO(sctps_pdrpdnfnd);
205 	DO(sctps_pdrpdiwnp);
206 	DO(sctps_pdrpdizrw);
207 	DO(sctps_pdrpbadd);
208 	DO(sctps_pdrpmark);
209 
210 	DO(sctps_timoiterator);
211 	DO(sctps_timodata);
212 	DO(sctps_timowindowprobe);
213 	DO(sctps_timoinit);
214 	DO(sctps_timosack);
215 	DO(sctps_timoshutdown);
216 	DO(sctps_timoheartbeat);
217 	DO(sctps_timocookie);
218 	DO(sctps_timosecret);
219 	DO(sctps_timopathmtu);
220 	DO(sctps_timoshutdownack);
221 	DO(sctps_timoshutdownguard);
222 	DO(sctps_timostrmrst);
223 	DO(sctps_timoearlyfr);
224 	DO(sctps_timoasconf);
225 	DO(sctps_timodelprim);
226 	DO(sctps_timoautoclose);
227 	DO(sctps_timoassockill);
228 	DO(sctps_timoinpkill);
229 
230 	DO(sctps_hdrops);
231 	DO(sctps_badsum);
232 	DO(sctps_noport);
233 	DO(sctps_badvtag);
234 	DO(sctps_badsid);
235 	DO(sctps_nomem);
236 	DO(sctps_fastretransinrtt);
237 	DO(sctps_markedretrans);
238 	DO(sctps_naglesent);
239 	DO(sctps_naglequeued);
240 	DO(sctps_maxburstqueued);
241 	DO(sctps_ifnomemqueued);
242 	DO(sctps_windowprobed);
243 	DO(sctps_lowlevelerr);
244 	DO(sctps_lowlevelerrusr);
245 	DO(sctps_datadropchklmt);
246 	DO(sctps_datadroprwnd);
247 	DO(sctps_ecnereducedcwnd);
248 	DO(sctps_vtagexpress);
249 	DO(sctps_vtagbogus);
250 	DO(sctps_primary_randry);
251 	DO(sctps_cmt_randry);
252 	DO(sctps_slowpath_sack);
253 	DO(sctps_wu_sacks_sent);
254 	DO(sctps_sends_with_flags);
255 	DO(sctps_sends_with_unord);
256 	DO(sctps_sends_with_eof);
257 	DO(sctps_sends_with_abort);
258 	DO(sctps_protocol_drain_calls);
259 	DO(sctps_protocol_drains_done);
260 	DO(sctps_read_peeks);
261 	DO(sctps_cached_chk);
262 	DO(sctps_cached_strmoq);
263 	DO(sctps_left_abandon);
264 	DO(sctps_send_burst_avoid);
265 	DO(sctps_send_cwnd_avoid);
266 	DO(sctps_fwdtsn_map_over);
267 	DO(sctps_queue_upd_ecne);
268 #undef DO
269 }
270 
271 void
272 showsctp(void)
273 {
274 	struct sctpstat stats;
275 
276 	memset(&stats, 0, sizeof stats);
277 	domode(&stats);
278 
279 #define DO(stat, row, col) \
280 	mvwprintw(wnd, row, col, "%12lu", stats.stat)
281 #define	L(row, stat) DO(stat, row, 0)
282 #define	R(row, stat) DO(stat, row, 38)
283 	L(1, sctps_activeestab);	R(1, sctps_outpackets);
284 	L(2, sctps_passiveestab);	R(2, sctps_inpackets);
285 	L(3, sctps_restartestab);	R(3, sctps_outoftheblue);
286 	L(4, sctps_shutdown);		R(4, sctps_badvtag);
287 	L(5, sctps_aborted);		R(5, sctps_checksumerrors);
288 
289 
290 	L(8, sctps_timoinit);		R(8, sctps_outcontrolchunks);
291 	L(9, sctps_timocookie);		R(9, sctps_senddata);
292 	L(10, sctps_timodata);		R(10, sctps_outorderchunks);
293 	L(11, sctps_timosack);		R(11, sctps_outunorderchunks);
294 	L(12, sctps_timoshutdown);	R(12, sctps_incontrolchunks);
295 	L(13, sctps_timoshutdownack);	R(13, sctps_recvdata);
296 	L(14, sctps_timoshutdownguard);	R(14, sctps_inorderchunks);
297 	L(15, sctps_timoheartbeat);	R(15, sctps_inunorderchunks);
298 	L(16, sctps_timopathmtu);
299 	L(17, sctps_timoautoclose);
300 	L(18, sctps_timoasconf);	R(18, sctps_fragusrmsgs);
301 	L(19, sctps_timostrmrst);	R(19, sctps_reasmusrmsgs);
302 #undef DO
303 #undef L
304 #undef R
305 }
306 
307 int
308 initsctp(void)
309 {
310 	size_t len;
311 	const char *name = "net.inet.sctp.stats";
312 
313 	len = 0;
314 	if (sysctlbyname(name, NULL, &len, NULL, 0) < 0) {
315 		error("sysctl getting sctpstat size failed");
316 		return 0;
317 	}
318 	if (len > sizeof curstat) {
319 		error("sctpstat structure has grown--recompile systat!");
320 		return 0;
321 	}
322 	if (sysctlbyname(name, &initstat, &len, NULL, 0) < 0) {
323 		error("sysctl getting sctpstat failed");
324 		return 0;
325 	}
326 	oldstat = initstat;
327 	return 1;
328 }
329 
330 void
331 resetsctp(void)
332 {
333 	size_t len;
334 	const char *name = "net.inet.sctp.stats";
335 
336 	len = sizeof initstat;
337 	if (sysctlbyname(name, &initstat, &len, NULL, 0) < 0) {
338 		error("sysctl getting sctpstat failed");
339 	}
340 	oldstat = initstat;
341 }
342 
343 void
344 fetchsctp(void)
345 {
346 	size_t len;
347 	const char *name = "net.inet.sctp.stats";
348 
349 	oldstat = curstat;
350 	len = sizeof curstat;
351 	if (sysctlbyname(name, &curstat, &len, NULL, 0) < 0) {
352 		error("sysctl getting sctpstat failed");
353 	}
354 	return;
355 }
356