• 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 	inet_prefix daddr, saddr;
99 	int family, i, atype, stype, dlen = 0, slen = 0;
100 
101 	if (n->nlmsg_type != genl_family)
102 		return -1;
103 
104 	len -= NLMSG_LENGTH(GENL_HDRLEN);
105 	if (len < 0)
106 		return -1;
107 
108 	ghdr = NLMSG_DATA(n);
109 	if (ghdr->cmd != TCP_METRICS_CMD_GET)
110 		return 0;
111 
112 	parse_rtattr(attrs, TCP_METRICS_ATTR_MAX, (void *) ghdr + GENL_HDRLEN,
113 		     len);
114 
115 	if (attrs[TCP_METRICS_ATTR_ADDR_IPV4]) {
116 		if (f.daddr.family && f.daddr.family != AF_INET)
117 			return 0;
118 		a = attrs[TCP_METRICS_ATTR_ADDR_IPV4];
119 		memcpy(&daddr.data, RTA_DATA(a), 4);
120 		daddr.bytelen = 4;
121 		family = AF_INET;
122 		atype = TCP_METRICS_ATTR_ADDR_IPV4;
123 		dlen = RTA_PAYLOAD(a);
124 	} else if (attrs[TCP_METRICS_ATTR_ADDR_IPV6]) {
125 		if (f.daddr.family && f.daddr.family != AF_INET6)
126 			return 0;
127 		a = attrs[TCP_METRICS_ATTR_ADDR_IPV6];
128 		memcpy(&daddr.data, RTA_DATA(a), 16);
129 		daddr.bytelen = 16;
130 		family = AF_INET6;
131 		atype = TCP_METRICS_ATTR_ADDR_IPV6;
132 		dlen = RTA_PAYLOAD(a);
133 	} else {
134 		return 0;
135 	}
136 
137 	if (attrs[TCP_METRICS_ATTR_SADDR_IPV4]) {
138 		if (f.saddr.family && f.saddr.family != AF_INET)
139 			return 0;
140 		a = attrs[TCP_METRICS_ATTR_SADDR_IPV4];
141 		memcpy(&saddr.data, RTA_DATA(a), 4);
142 		saddr.bytelen = 4;
143 		stype = TCP_METRICS_ATTR_SADDR_IPV4;
144 		slen = RTA_PAYLOAD(a);
145 	} else if (attrs[TCP_METRICS_ATTR_SADDR_IPV6]) {
146 		if (f.saddr.family && f.saddr.family != AF_INET6)
147 			return 0;
148 		a = attrs[TCP_METRICS_ATTR_SADDR_IPV6];
149 		memcpy(&saddr.data, RTA_DATA(a), 16);
150 		saddr.bytelen = 16;
151 		stype = TCP_METRICS_ATTR_SADDR_IPV6;
152 		slen = RTA_PAYLOAD(a);
153 	}
154 
155 	if (f.daddr.family && f.daddr.bitlen >= 0 &&
156 	    inet_addr_match(&daddr, &f.daddr, f.daddr.bitlen))
157 	       return 0;
158 	/* Only check for the source-address if the kernel supports it,
159 	 * meaning slen != 0.
160 	 */
161 	if (slen && f.saddr.family && f.saddr.bitlen >= 0 &&
162 	    inet_addr_match(&saddr, &f.saddr, f.saddr.bitlen))
163 		return 0;
164 
165 	if (f.flushb) {
166 		struct nlmsghdr *fn;
167 
168 		TCPM_REQUEST(req2, 128, TCP_METRICS_CMD_DEL, NLM_F_REQUEST);
169 
170 		addattr_l(&req2.n, sizeof(req2), atype, &daddr.data,
171 			  daddr.bytelen);
172 		if (slen)
173 			addattr_l(&req2.n, sizeof(req2), stype, &saddr.data,
174 				  saddr.bytelen);
175 
176 		if (NLMSG_ALIGN(f.flushp) + req2.n.nlmsg_len > f.flushe) {
177 			if (flush_update())
178 				return -1;
179 		}
180 		fn = (struct nlmsghdr *) (f.flushb + NLMSG_ALIGN(f.flushp));
181 		memcpy(fn, &req2.n, req2.n.nlmsg_len);
182 		fn->nlmsg_seq = ++grth.seq;
183 		f.flushp = (((char *) fn) + req2.n.nlmsg_len) - f.flushb;
184 		f.flushed++;
185 		if (show_stats < 2)
186 			return 0;
187 	}
188 
189 	if (f.cmd & (CMD_DEL | CMD_FLUSH))
190 		fprintf(fp, "Deleted ");
191 
192 	fprintf(fp, "%s",
193 		format_host(family, dlen, &daddr.data));
194 
195 	a = attrs[TCP_METRICS_ATTR_AGE];
196 	if (a) {
197 		unsigned long long val = rta_getattr_u64(a);
198 
199 		fprintf(fp, " age %llu.%03llusec",
200 			val / 1000, val % 1000);
201 	}
202 
203 	a = attrs[TCP_METRICS_ATTR_TW_TS_STAMP];
204 	if (a) {
205 		__s32 val = (__s32) rta_getattr_u32(a);
206 		__u32 tsval;
207 
208 		a = attrs[TCP_METRICS_ATTR_TW_TSVAL];
209 		tsval = a ? rta_getattr_u32(a) : 0;
210 		fprintf(fp, " tw_ts %u/%dsec ago", tsval, val);
211 	}
212 
213 	a = attrs[TCP_METRICS_ATTR_VALS];
214 	if (a) {
215 		struct rtattr *m[TCP_METRIC_MAX + 1 + 1];
216 		unsigned long rtt = 0, rttvar = 0;
217 
218 		parse_rtattr_nested(m, TCP_METRIC_MAX + 1, a);
219 
220 		for (i = 0; i < TCP_METRIC_MAX + 1; i++) {
221 			unsigned long val;
222 
223 			a = m[i + 1];
224 			if (!a)
225 				continue;
226 			if (i != TCP_METRIC_RTT &&
227 			    i != TCP_METRIC_RTT_US &&
228 			    i != TCP_METRIC_RTTVAR &&
229 			    i != TCP_METRIC_RTTVAR_US) {
230 				if (metric_name[i])
231 					fprintf(fp, " %s ", metric_name[i]);
232 				else
233 					fprintf(fp, " metric_%d ", i);
234 			}
235 			val = rta_getattr_u32(a);
236 			switch (i) {
237 			case TCP_METRIC_RTT:
238 				if (!rtt)
239 					rtt = (val * 1000UL) >> 3;
240 				break;
241 			case TCP_METRIC_RTTVAR:
242 				if (!rttvar)
243 					rttvar = (val * 1000UL) >> 2;
244 				break;
245 			case TCP_METRIC_RTT_US:
246 				rtt = val >> 3;
247 				break;
248 			case TCP_METRIC_RTTVAR_US:
249 				rttvar = val >> 2;
250 				break;
251 			case TCP_METRIC_SSTHRESH:
252 			case TCP_METRIC_CWND:
253 			case TCP_METRIC_REORDERING:
254 			default:
255 				fprintf(fp, "%lu", val);
256 				break;
257 			}
258 		}
259 		if (rtt)
260 			fprintf(fp, " rtt %luus", rtt);
261 		if (rttvar)
262 			fprintf(fp, " rttvar %luus", rttvar);
263 	}
264 
265 	a = attrs[TCP_METRICS_ATTR_FOPEN_MSS];
266 	if (a)
267 		fprintf(fp, " fo_mss %u", rta_getattr_u16(a));
268 
269 	a = attrs[TCP_METRICS_ATTR_FOPEN_SYN_DROPS];
270 	if (a) {
271 		__u16 syn_loss = rta_getattr_u16(a);
272 		unsigned long long ts;
273 
274 		a = attrs[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS];
275 		ts = a ? rta_getattr_u64(a) : 0;
276 
277 		fprintf(fp, " fo_syn_drops %u/%llu.%03llusec ago",
278 			syn_loss, ts / 1000, ts % 1000);
279 	}
280 
281 	a = attrs[TCP_METRICS_ATTR_FOPEN_COOKIE];
282 	if (a) {
283 		char cookie[32 + 1];
284 		unsigned char *ptr = RTA_DATA(a);
285 		int i, max = RTA_PAYLOAD(a);
286 
287 		if (max > 16)
288 			max = 16;
289 		cookie[0] = 0;
290 		for (i = 0; i < max; i++)
291 			sprintf(cookie + i + i, "%02x", ptr[i]);
292 		fprintf(fp, " fo_cookie %s", cookie);
293 	}
294 
295 	if (slen) {
296 		fprintf(fp, " source %s",
297 			format_host(family, slen, &saddr.data));
298 	}
299 
300 	fprintf(fp, "\n");
301 
302 	fflush(fp);
303 	return 0;
304 }
305 
tcpm_do_cmd(int cmd,int argc,char ** argv)306 static int tcpm_do_cmd(int cmd, int argc, char **argv)
307 {
308 	TCPM_REQUEST(req, 1024, TCP_METRICS_CMD_GET, NLM_F_REQUEST);
309 	int atype = -1, stype = -1;
310 	int ack;
311 
312 	memset(&f, 0, sizeof(f));
313 	f.daddr.bitlen = -1;
314 	f.daddr.family = preferred_family;
315 	f.saddr.bitlen = -1;
316 	f.saddr.family = preferred_family;
317 
318 	switch (preferred_family) {
319 	case AF_UNSPEC:
320 	case AF_INET:
321 	case AF_INET6:
322 		break;
323 	default:
324 		fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
325 		return -1;
326 	}
327 
328 	for (; argc > 0; argc--, argv++) {
329 		if (strcmp(*argv, "src") == 0 ||
330 		    strcmp(*argv, "source") == 0) {
331 			char *who = *argv;
332 
333 			NEXT_ARG();
334 			if (matches(*argv, "help") == 0)
335 				usage();
336 			if (f.saddr.bitlen >= 0)
337 				duparg2(who, *argv);
338 
339 			get_prefix(&f.saddr, *argv, preferred_family);
340 			if (f.saddr.bytelen && f.saddr.bytelen * 8 == f.saddr.bitlen) {
341 				if (f.saddr.family == AF_INET)
342 					stype = TCP_METRICS_ATTR_SADDR_IPV4;
343 				else if (f.saddr.family == AF_INET6)
344 					stype = TCP_METRICS_ATTR_SADDR_IPV6;
345 			}
346 
347 			if (stype < 0) {
348 				fprintf(stderr, "Error: a specific IP address is expected rather than \"%s\"\n",
349 					*argv);
350 				return -1;
351 			}
352 		} else {
353 			char *who = "address";
354 
355 			if (strcmp(*argv, "addr") == 0 ||
356 			    strcmp(*argv, "address") == 0) {
357 				who = *argv;
358 				NEXT_ARG();
359 			}
360 			if (matches(*argv, "help") == 0)
361 				usage();
362 			if (f.daddr.bitlen >= 0)
363 				duparg2(who, *argv);
364 
365 			get_prefix(&f.daddr, *argv, preferred_family);
366 			if (f.daddr.bytelen && f.daddr.bytelen * 8 == f.daddr.bitlen) {
367 				if (f.daddr.family == AF_INET)
368 					atype = TCP_METRICS_ATTR_ADDR_IPV4;
369 				else if (f.daddr.family == AF_INET6)
370 					atype = TCP_METRICS_ATTR_ADDR_IPV6;
371 			}
372 			if ((CMD_DEL & cmd) && atype < 0) {
373 				fprintf(stderr, "Error: a specific IP address is expected rather than \"%s\"\n",
374 					*argv);
375 				return -1;
376 			}
377 		}
378 		argc--; argv++;
379 	}
380 
381 	if (cmd == CMD_DEL && atype < 0)
382 		missarg("address");
383 
384 	/* flush for exact address ? Single del */
385 	if (cmd == CMD_FLUSH && atype >= 0)
386 		cmd = CMD_DEL;
387 
388 	/* flush for all addresses ? Single del without address */
389 	if (cmd == CMD_FLUSH && f.daddr.bitlen <= 0 &&
390 	    f.saddr.bitlen <= 0 && preferred_family == AF_UNSPEC) {
391 		cmd = CMD_DEL;
392 		req.g.cmd = TCP_METRICS_CMD_DEL;
393 		ack = 1;
394 	} else if (cmd == CMD_DEL) {
395 		req.g.cmd = TCP_METRICS_CMD_DEL;
396 		ack = 1;
397 	} else {	/* CMD_FLUSH, CMD_LIST */
398 		ack = 0;
399 	}
400 
401 	if (genl_init_handle(&grth, TCP_METRICS_GENL_NAME, &genl_family))
402 		exit(1);
403 	req.n.nlmsg_type = genl_family;
404 
405 	if (!(cmd & CMD_FLUSH) && (atype >= 0 || (cmd & CMD_DEL))) {
406 		if (ack)
407 			req.n.nlmsg_flags |= NLM_F_ACK;
408 		if (atype >= 0)
409 			addattr_l(&req.n, sizeof(req), atype, &f.daddr.data,
410 				  f.daddr.bytelen);
411 		if (stype >= 0)
412 			addattr_l(&req.n, sizeof(req), stype, &f.saddr.data,
413 				  f.saddr.bytelen);
414 	} else {
415 		req.n.nlmsg_flags |= NLM_F_DUMP;
416 	}
417 
418 	f.cmd = cmd;
419 	if (cmd & CMD_FLUSH) {
420 		int round = 0;
421 		char flushb[4096-512];
422 
423 		f.flushb = flushb;
424 		f.flushp = 0;
425 		f.flushe = sizeof(flushb);
426 
427 		for (;;) {
428 			req.n.nlmsg_seq = grth.dump = ++grth.seq;
429 			if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
430 				perror("Failed to send flush request");
431 				exit(1);
432 			}
433 			f.flushed = 0;
434 			if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
435 				fprintf(stderr, "Flush terminated\n");
436 				exit(1);
437 			}
438 			if (f.flushed == 0) {
439 				if (round == 0) {
440 					fprintf(stderr, "Nothing to flush.\n");
441 				} else if (show_stats)
442 					printf("*** Flush is complete after %d round%s ***\n",
443 					       round, round > 1 ? "s" : "");
444 				fflush(stdout);
445 				return 0;
446 			}
447 			round++;
448 			if (flush_update() < 0)
449 				exit(1);
450 			if (show_stats) {
451 				printf("\n*** Round %d, deleting %d entries ***\n",
452 				       round, f.flushed);
453 				fflush(stdout);
454 			}
455 		}
456 		return 0;
457 	}
458 
459 	if (ack) {
460 		if (rtnl_talk(&grth, &req.n, NULL, 0) < 0)
461 			return -2;
462 	} else if (atype >= 0) {
463 		if (rtnl_talk(&grth, &req.n, &req.n, sizeof(req)) < 0)
464 			return -2;
465 		if (process_msg(NULL, &req.n, stdout) < 0) {
466 			fprintf(stderr, "Dump terminated\n");
467 			exit(1);
468 		}
469 	} else {
470 		req.n.nlmsg_seq = grth.dump = ++grth.seq;
471 		if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
472 			perror("Failed to send dump request");
473 			exit(1);
474 		}
475 
476 		if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
477 			fprintf(stderr, "Dump terminated\n");
478 			exit(1);
479 		}
480 	}
481 	return 0;
482 }
483 
do_tcp_metrics(int argc,char ** argv)484 int do_tcp_metrics(int argc, char **argv)
485 {
486 	int i;
487 
488 	if (argc < 1)
489 		return tcpm_do_cmd(CMD_LIST, 0, NULL);
490 	for (i = 0; i < ARRAY_SIZE(cmds); i++) {
491 		if (matches(argv[0], cmds[i].name) == 0)
492 			return tcpm_do_cmd(cmds[i].code, argc-1, argv+1);
493 	}
494 	if (matches(argv[0], "help") == 0)
495 		usage();
496 
497 	fprintf(stderr, "Command \"%s\" is unknown, try \"ip tcp_metrics help\".\n",
498 			*argv);
499 	exit(-1);
500 }
501