• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * tcp_metrics.c	"ip tcp_metrics/tcpmetrics"
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		version 2 as published by the Free Software Foundation;
7  *
8  * Authors:	Julian Anastasov <ja@ssi.bg>, August 2012
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <arpa/inet.h>
19 #include <sys/ioctl.h>
20 #include <linux/if.h>
21 
22 #include <linux/genetlink.h>
23 #include <linux/tcp_metrics.h>
24 
25 #include "utils.h"
26 #include "ip_common.h"
27 #include "libgenl.h"
28 
usage(void)29 static void usage(void)
30 {
31 	fprintf(stderr, "Usage: ip tcp_metrics/tcpmetrics { COMMAND | help }\n");
32 	fprintf(stderr, "       ip tcp_metrics { show | flush } SELECTOR\n");
33 	fprintf(stderr, "       ip tcp_metrics delete [ address ] ADDRESS\n");
34 	fprintf(stderr, "SELECTOR := [ [ address ] PREFIX ]\n");
35 	exit(-1);
36 }
37 
38 /* netlink socket */
39 static struct rtnl_handle grth = { .fd = -1 };
40 static int genl_family = -1;
41 
42 #define TCPM_REQUEST(_req, _bufsiz, _cmd, _flags) \
43 	GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
44 		     TCP_METRICS_GENL_VERSION, _cmd, _flags)
45 
46 #define CMD_LIST	0x0001	/* list, lst, show		*/
47 #define CMD_DEL		0x0002	/* delete, remove		*/
48 #define CMD_FLUSH	0x0004	/* flush			*/
49 
50 static struct {
51 	char	*name;
52 	int	code;
53 } cmds[] = {
54 	{	"list",		CMD_LIST	},
55 	{	"lst",		CMD_LIST	},
56 	{	"show",		CMD_LIST	},
57 	{	"delete",	CMD_DEL		},
58 	{	"remove",	CMD_DEL		},
59 	{	"flush",	CMD_FLUSH	},
60 };
61 
62 static char *metric_name[TCP_METRIC_MAX + 1] = {
63 	[TCP_METRIC_RTT]		= "rtt",
64 	[TCP_METRIC_RTTVAR]		= "rttvar",
65 	[TCP_METRIC_SSTHRESH]		= "ssthresh",
66 	[TCP_METRIC_CWND]		= "cwnd",
67 	[TCP_METRIC_REORDERING]		= "reordering",
68 };
69 
70 static struct
71 {
72 	int flushed;
73 	char *flushb;
74 	int flushp;
75 	int flushe;
76 	int cmd;
77 	inet_prefix daddr;
78 	inet_prefix saddr;
79 } f;
80 
flush_update(void)81 static int flush_update(void)
82 {
83 	if (rtnl_send_check(&grth, f.flushb, f.flushp) < 0) {
84 		perror("Failed to send flush request\n");
85 		return -1;
86 	}
87 	f.flushp = 0;
88 	return 0;
89 }
90 
process_msg(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)91 static int process_msg(const struct sockaddr_nl *who, struct nlmsghdr *n,
92 		       void *arg)
93 {
94 	FILE *fp = (FILE *) arg;
95 	struct genlmsghdr *ghdr;
96 	struct rtattr *attrs[TCP_METRICS_ATTR_MAX + 1], *a;
97 	int len = n->nlmsg_len;
98 	char abuf[256];
99 	inet_prefix daddr, saddr;
100 	int family, i, atype, stype, dlen = 0, slen = 0;
101 
102 	if (n->nlmsg_type != genl_family)
103 		return -1;
104 
105 	len -= NLMSG_LENGTH(GENL_HDRLEN);
106 	if (len < 0)
107 		return -1;
108 
109 	ghdr = NLMSG_DATA(n);
110 	if (ghdr->cmd != TCP_METRICS_CMD_GET)
111 		return 0;
112 
113 	parse_rtattr(attrs, TCP_METRICS_ATTR_MAX, (void *) ghdr + GENL_HDRLEN,
114 		     len);
115 
116 	a = attrs[TCP_METRICS_ATTR_ADDR_IPV4];
117 	if (a) {
118 		if (f.daddr.family && f.daddr.family != AF_INET)
119 			return 0;
120 		memcpy(&daddr.data, RTA_DATA(a), 4);
121 		daddr.bytelen = 4;
122 		family = AF_INET;
123 		atype = TCP_METRICS_ATTR_ADDR_IPV4;
124 		dlen = RTA_PAYLOAD(a);
125 	} else {
126 		a = attrs[TCP_METRICS_ATTR_ADDR_IPV6];
127 		if (a) {
128 			if (f.daddr.family && f.daddr.family != AF_INET6)
129 				return 0;
130 			memcpy(&daddr.data, RTA_DATA(a), 16);
131 			daddr.bytelen = 16;
132 			family = AF_INET6;
133 			atype = TCP_METRICS_ATTR_ADDR_IPV6;
134 			dlen = RTA_PAYLOAD(a);
135 		} else
136 			return 0;
137 	}
138 
139 	a = attrs[TCP_METRICS_ATTR_SADDR_IPV4];
140 	if (a) {
141 		if (f.saddr.family && f.saddr.family != AF_INET)
142 			return 0;
143 		memcpy(&saddr.data, RTA_DATA(a), 4);
144 		saddr.bytelen = 4;
145 		stype = TCP_METRICS_ATTR_SADDR_IPV4;
146 		slen = RTA_PAYLOAD(a);
147 	} else {
148 		a = attrs[TCP_METRICS_ATTR_SADDR_IPV6];
149 		if (a) {
150 			if (f.saddr.family && f.saddr.family != AF_INET6)
151 				return 0;
152 			memcpy(&saddr.data, RTA_DATA(a), 16);
153 			saddr.bytelen = 16;
154 			stype = TCP_METRICS_ATTR_SADDR_IPV6;
155 			slen = RTA_PAYLOAD(a);
156 		}
157 	}
158 
159 	if (f.daddr.family && f.daddr.bitlen >= 0 &&
160 	    inet_addr_match(&daddr, &f.daddr, f.daddr.bitlen))
161 	       return 0;
162 	/* Only check for the source-address if the kernel supports it,
163 	 * meaning slen != 0.
164 	 */
165 	if (slen && f.saddr.family && f.saddr.bitlen >= 0 &&
166 	    inet_addr_match(&saddr, &f.saddr, f.saddr.bitlen))
167 		return 0;
168 
169 	if (f.flushb) {
170 		struct nlmsghdr *fn;
171 		TCPM_REQUEST(req2, 128, TCP_METRICS_CMD_DEL, NLM_F_REQUEST);
172 
173 		addattr_l(&req2.n, sizeof(req2), atype, &daddr.data,
174 			  daddr.bytelen);
175 		if (slen)
176 			addattr_l(&req2.n, sizeof(req2), stype, &saddr.data,
177 				  saddr.bytelen);
178 
179 		if (NLMSG_ALIGN(f.flushp) + req2.n.nlmsg_len > f.flushe) {
180 			if (flush_update())
181 				return -1;
182 		}
183 		fn = (struct nlmsghdr *) (f.flushb + NLMSG_ALIGN(f.flushp));
184 		memcpy(fn, &req2.n, req2.n.nlmsg_len);
185 		fn->nlmsg_seq = ++grth.seq;
186 		f.flushp = (((char *) fn) + req2.n.nlmsg_len) - f.flushb;
187 		f.flushed++;
188 		if (show_stats < 2)
189 			return 0;
190 	}
191 
192 	if (f.cmd & (CMD_DEL | CMD_FLUSH))
193 		fprintf(fp, "Deleted ");
194 
195 	fprintf(fp, "%s",
196 		format_host(family, dlen, &daddr.data, abuf, sizeof(abuf)));
197 
198 	a = attrs[TCP_METRICS_ATTR_AGE];
199 	if (a) {
200 		unsigned long long val = rta_getattr_u64(a);
201 
202 		fprintf(fp, " age %llu.%03llusec",
203 			val / 1000, val % 1000);
204 	}
205 
206 	a = attrs[TCP_METRICS_ATTR_TW_TS_STAMP];
207 	if (a) {
208 		__s32 val = (__s32) rta_getattr_u32(a);
209 		__u32 tsval;
210 
211 		a = attrs[TCP_METRICS_ATTR_TW_TSVAL];
212 		tsval = a ? rta_getattr_u32(a) : 0;
213 		fprintf(fp, " tw_ts %u/%dsec ago", tsval, val);
214 	}
215 
216 	a = attrs[TCP_METRICS_ATTR_VALS];
217 	if (a) {
218 		struct rtattr *m[TCP_METRIC_MAX + 1 + 1];
219 		unsigned long rtt = 0, rttvar = 0;
220 
221 		parse_rtattr_nested(m, TCP_METRIC_MAX + 1, a);
222 
223 		for (i = 0; i < TCP_METRIC_MAX + 1; i++) {
224 			unsigned long val;
225 
226 			a = m[i + 1];
227 			if (!a)
228 				continue;
229 			if (i != TCP_METRIC_RTT &&
230 			    i != TCP_METRIC_RTT_US &&
231 			    i != TCP_METRIC_RTTVAR &&
232 			    i != TCP_METRIC_RTTVAR_US) {
233 				if (metric_name[i])
234 					fprintf(fp, " %s ", metric_name[i]);
235 				else
236 					fprintf(fp, " metric_%d ", i);
237 			}
238 			val = rta_getattr_u32(a);
239 			switch (i) {
240 			case TCP_METRIC_RTT:
241 				if (!rtt)
242 					rtt = (val * 1000UL) >> 3;
243 				break;
244 			case TCP_METRIC_RTTVAR:
245 				if (!rttvar)
246 					rttvar = (val * 1000UL) >> 2;
247 				break;
248 			case TCP_METRIC_RTT_US:
249 				rtt = val >> 3;
250 				break;
251 			case TCP_METRIC_RTTVAR_US:
252 				rttvar = val >> 2;
253 				break;
254 			case TCP_METRIC_SSTHRESH:
255 			case TCP_METRIC_CWND:
256 			case TCP_METRIC_REORDERING:
257 			default:
258 				fprintf(fp, "%lu", val);
259 				break;
260 			}
261 		}
262 		if (rtt)
263 			fprintf(fp, " rtt %luus", rtt);
264 		if (rttvar)
265 			fprintf(fp, " rttvar %luus", rttvar);
266 	}
267 
268 	a = attrs[TCP_METRICS_ATTR_FOPEN_MSS];
269 	if (a)
270 		fprintf(fp, " fo_mss %u", rta_getattr_u16(a));
271 
272 	a = attrs[TCP_METRICS_ATTR_FOPEN_SYN_DROPS];
273 	if (a) {
274 		__u16 syn_loss = rta_getattr_u16(a);
275 		unsigned long long ts;
276 
277 		a = attrs[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS];
278 		ts = a ? rta_getattr_u64(a) : 0;
279 
280 		fprintf(fp, " fo_syn_drops %u/%llu.%03llusec ago",
281 			syn_loss, ts / 1000, ts % 1000);
282 	}
283 
284 	a = attrs[TCP_METRICS_ATTR_FOPEN_COOKIE];
285 	if (a) {
286 		char cookie[32 + 1];
287 		unsigned char *ptr = RTA_DATA(a);
288 		int i, max = RTA_PAYLOAD(a);
289 
290 		if (max > 16)
291 			max = 16;
292 		cookie[0] = 0;
293 		for (i = 0; i < max; i++)
294 			sprintf(cookie + i + i, "%02x", ptr[i]);
295 		fprintf(fp, " fo_cookie %s", cookie);
296 	}
297 
298 	if (slen) {
299 		fprintf(fp, " source %s",
300 			format_host(family, slen, &saddr.data, abuf,
301 				    sizeof(abuf)));
302 	}
303 
304 	fprintf(fp, "\n");
305 
306 	fflush(fp);
307 	return 0;
308 }
309 
tcpm_do_cmd(int cmd,int argc,char ** argv)310 static int tcpm_do_cmd(int cmd, int argc, char **argv)
311 {
312 	TCPM_REQUEST(req, 1024, TCP_METRICS_CMD_GET, NLM_F_REQUEST);
313 	int atype = -1, stype = -1;
314 	int ack;
315 
316 	memset(&f, 0, sizeof(f));
317 	f.daddr.bitlen = -1;
318 	f.daddr.family = preferred_family;
319 	f.saddr.bitlen = -1;
320 	f.saddr.family = preferred_family;
321 
322 	switch (preferred_family) {
323 	case AF_UNSPEC:
324 	case AF_INET:
325 	case AF_INET6:
326 		break;
327 	default:
328 		fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
329 		return -1;
330 	}
331 
332 	for (; argc > 0; argc--, argv++) {
333 		if (strcmp(*argv, "src") == 0 ||
334 		    strcmp(*argv, "source") == 0) {
335 			char *who = *argv;
336 			NEXT_ARG();
337 			if (matches(*argv, "help") == 0)
338 				usage();
339 			if (f.saddr.bitlen >= 0)
340 				duparg2(who, *argv);
341 
342 			get_prefix(&f.saddr, *argv, preferred_family);
343 			if (f.saddr.bytelen && f.saddr.bytelen * 8 == f.saddr.bitlen) {
344 				if (f.saddr.family == AF_INET)
345 					stype = TCP_METRICS_ATTR_SADDR_IPV4;
346 				else if (f.saddr.family == AF_INET6)
347 					stype = TCP_METRICS_ATTR_SADDR_IPV6;
348 			}
349 
350 			if (stype < 0) {
351 				fprintf(stderr, "Error: a specific IP address is expected rather than \"%s\"\n",
352 					*argv);
353 				return -1;
354 			}
355 		} else {
356 			char *who = "address";
357 			if (strcmp(*argv, "addr") == 0 ||
358 			    strcmp(*argv, "address") == 0) {
359 				who = *argv;
360 				NEXT_ARG();
361 			}
362 			if (matches(*argv, "help") == 0)
363 				usage();
364 			if (f.daddr.bitlen >= 0)
365 				duparg2(who, *argv);
366 
367 			get_prefix(&f.daddr, *argv, preferred_family);
368 			if (f.daddr.bytelen && f.daddr.bytelen * 8 == f.daddr.bitlen) {
369 				if (f.daddr.family == AF_INET)
370 					atype = TCP_METRICS_ATTR_ADDR_IPV4;
371 				else if (f.daddr.family == AF_INET6)
372 					atype = TCP_METRICS_ATTR_ADDR_IPV6;
373 			}
374 			if ((CMD_DEL & cmd) && atype < 0) {
375 				fprintf(stderr, "Error: a specific IP address is expected rather than \"%s\"\n",
376 					*argv);
377 				return -1;
378 			}
379 		}
380 		argc--; argv++;
381 	}
382 
383 	if (cmd == CMD_DEL && atype < 0)
384 		missarg("address");
385 
386 	/* flush for exact address ? Single del */
387 	if (cmd == CMD_FLUSH && atype >= 0)
388 		cmd = CMD_DEL;
389 
390 	/* flush for all addresses ? Single del without address */
391 	if (cmd == CMD_FLUSH && f.daddr.bitlen <= 0 &&
392 	    f.saddr.bitlen <= 0 && preferred_family == AF_UNSPEC) {
393 		cmd = CMD_DEL;
394 		req.g.cmd = TCP_METRICS_CMD_DEL;
395 		ack = 1;
396 	} else if (cmd == CMD_DEL) {
397 		req.g.cmd = TCP_METRICS_CMD_DEL;
398 		ack = 1;
399 	} else {	/* CMD_FLUSH, CMD_LIST */
400 		ack = 0;
401 	}
402 
403 	if (genl_family < 0) {
404 		if (rtnl_open_byproto(&grth, 0, NETLINK_GENERIC) < 0) {
405 			fprintf(stderr, "Cannot open generic netlink socket\n");
406 			exit(1);
407 		}
408 		genl_family = genl_resolve_family(&grth,
409 						  TCP_METRICS_GENL_NAME);
410 		if (genl_family < 0)
411 			exit(1);
412 		req.n.nlmsg_type = genl_family;
413 	}
414 
415 	if (!(cmd & CMD_FLUSH) && (atype >= 0 || (cmd & CMD_DEL))) {
416 		if (ack)
417 			req.n.nlmsg_flags |= NLM_F_ACK;
418 		if (atype >= 0)
419 			addattr_l(&req.n, sizeof(req), atype, &f.daddr.data,
420 				  f.daddr.bytelen);
421 		if (stype >= 0)
422 			addattr_l(&req.n, sizeof(req), stype, &f.saddr.data,
423 				  f.saddr.bytelen);
424 	} else {
425 		req.n.nlmsg_flags |= NLM_F_DUMP;
426 	}
427 
428 	f.cmd = cmd;
429 	if (cmd & CMD_FLUSH) {
430 		int round = 0;
431 		char flushb[4096-512];
432 
433 		f.flushb = flushb;
434 		f.flushp = 0;
435 		f.flushe = sizeof(flushb);
436 
437 		for (;;) {
438 			req.n.nlmsg_seq = grth.dump = ++grth.seq;
439 			if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
440 				perror("Failed to send flush request");
441 				exit(1);
442 			}
443 			f.flushed = 0;
444 			if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
445 				fprintf(stderr, "Flush terminated\n");
446 				exit(1);
447 			}
448 			if (f.flushed == 0) {
449 				if (round == 0) {
450 					fprintf(stderr, "Nothing to flush.\n");
451 				} else if (show_stats)
452 					printf("*** Flush is complete after %d round%s ***\n",
453 					       round, round > 1 ? "s" : "");
454 				fflush(stdout);
455 				return 0;
456 			}
457 			round++;
458 			if (flush_update() < 0)
459 				exit(1);
460 			if (show_stats) {
461 				printf("\n*** Round %d, deleting %d entries ***\n",
462 				       round, f.flushed);
463 				fflush(stdout);
464 			}
465 		}
466 		return 0;
467 	}
468 
469 	if (ack) {
470 		if (rtnl_talk(&grth, &req.n, NULL, 0) < 0)
471 			return -2;
472 	} else if (atype >= 0) {
473 		if (rtnl_talk(&grth, &req.n, &req.n, sizeof(req)) < 0)
474 			return -2;
475 		if (process_msg(NULL, &req.n, stdout) < 0) {
476 			fprintf(stderr, "Dump terminated\n");
477 			exit(1);
478 		}
479 	} else {
480 		req.n.nlmsg_seq = grth.dump = ++grth.seq;
481 		if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
482 			perror("Failed to send dump request");
483 			exit(1);
484 		}
485 
486 		if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
487 			fprintf(stderr, "Dump terminated\n");
488 			exit(1);
489 		}
490 	}
491 	return 0;
492 }
493 
do_tcp_metrics(int argc,char ** argv)494 int do_tcp_metrics(int argc, char **argv)
495 {
496 	int i;
497 
498 	if (argc < 1)
499 		return tcpm_do_cmd(CMD_LIST, 0, NULL);
500 	for (i = 0; i < ARRAY_SIZE(cmds); i++) {
501 		if (matches(argv[0], cmds[i].name) == 0)
502 			return tcpm_do_cmd(cmds[i].code, argc-1, argv+1);
503 	}
504 	if (matches(argv[0], "help") == 0)
505 		usage();
506 
507 	fprintf(stderr, "Command \"%s\" is unknown, "
508 			"try \"ip tcp_metrics help\".\n", *argv);
509 	exit(-1);
510 }
511