]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/tcp_metrics.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / tcp_metrics.c
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
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
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
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
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 struct nlmsghdr *answer;
310 int atype = -1, stype = -1;
311 int ack;
312
313 memset(&f, 0, sizeof(f));
314 f.daddr.bitlen = -1;
315 f.daddr.family = preferred_family;
316 f.saddr.bitlen = -1;
317 f.saddr.family = preferred_family;
318
319 switch (preferred_family) {
320 case AF_UNSPEC:
321 case AF_INET:
322 case AF_INET6:
323 break;
324 default:
325 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
326 return -1;
327 }
328
329 for (; argc > 0; argc--, argv++) {
330 if (strcmp(*argv, "src") == 0 ||
331 strcmp(*argv, "source") == 0) {
332 char *who = *argv;
333
334 NEXT_ARG();
335 if (matches(*argv, "help") == 0)
336 usage();
337 if (f.saddr.bitlen >= 0)
338 duparg2(who, *argv);
339
340 get_prefix(&f.saddr, *argv, preferred_family);
341 if (f.saddr.bytelen && f.saddr.bytelen * 8 == f.saddr.bitlen) {
342 if (f.saddr.family == AF_INET)
343 stype = TCP_METRICS_ATTR_SADDR_IPV4;
344 else if (f.saddr.family == AF_INET6)
345 stype = TCP_METRICS_ATTR_SADDR_IPV6;
346 }
347
348 if (stype < 0) {
349 fprintf(stderr, "Error: a specific IP address is expected rather than \"%s\"\n",
350 *argv);
351 return -1;
352 }
353 } else {
354 char *who = "address";
355
356 if (strcmp(*argv, "addr") == 0 ||
357 strcmp(*argv, "address") == 0) {
358 who = *argv;
359 NEXT_ARG();
360 }
361 if (matches(*argv, "help") == 0)
362 usage();
363 if (f.daddr.bitlen >= 0)
364 duparg2(who, *argv);
365
366 get_prefix(&f.daddr, *argv, preferred_family);
367 if (f.daddr.bytelen && f.daddr.bytelen * 8 == f.daddr.bitlen) {
368 if (f.daddr.family == AF_INET)
369 atype = TCP_METRICS_ATTR_ADDR_IPV4;
370 else if (f.daddr.family == AF_INET6)
371 atype = TCP_METRICS_ATTR_ADDR_IPV6;
372 }
373 if ((CMD_DEL & cmd) && atype < 0) {
374 fprintf(stderr, "Error: a specific IP address is expected rather than \"%s\"\n",
375 *argv);
376 return -1;
377 }
378 }
379 argc--; argv++;
380 }
381
382 if (cmd == CMD_DEL && atype < 0)
383 missarg("address");
384
385 /* flush for exact address ? Single del */
386 if (cmd == CMD_FLUSH && atype >= 0)
387 cmd = CMD_DEL;
388
389 /* flush for all addresses ? Single del without address */
390 if (cmd == CMD_FLUSH && f.daddr.bitlen <= 0 &&
391 f.saddr.bitlen <= 0 && preferred_family == AF_UNSPEC) {
392 cmd = CMD_DEL;
393 req.g.cmd = TCP_METRICS_CMD_DEL;
394 ack = 1;
395 } else if (cmd == CMD_DEL) {
396 req.g.cmd = TCP_METRICS_CMD_DEL;
397 ack = 1;
398 } else { /* CMD_FLUSH, CMD_LIST */
399 ack = 0;
400 }
401
402 if (genl_init_handle(&grth, TCP_METRICS_GENL_NAME, &genl_family))
403 exit(1);
404 req.n.nlmsg_type = genl_family;
405
406 if (!(cmd & CMD_FLUSH) && (atype >= 0 || (cmd & CMD_DEL))) {
407 if (ack)
408 req.n.nlmsg_flags |= NLM_F_ACK;
409 if (atype >= 0)
410 addattr_l(&req.n, sizeof(req), atype, &f.daddr.data,
411 f.daddr.bytelen);
412 if (stype >= 0)
413 addattr_l(&req.n, sizeof(req), stype, &f.saddr.data,
414 f.saddr.bytelen);
415 } else {
416 req.n.nlmsg_flags |= NLM_F_DUMP;
417 }
418
419 f.cmd = cmd;
420 if (cmd & CMD_FLUSH) {
421 int round = 0;
422 char flushb[4096-512];
423
424 f.flushb = flushb;
425 f.flushp = 0;
426 f.flushe = sizeof(flushb);
427
428 for (;;) {
429 req.n.nlmsg_seq = grth.dump = ++grth.seq;
430 if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
431 perror("Failed to send flush request");
432 exit(1);
433 }
434 f.flushed = 0;
435 if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
436 fprintf(stderr, "Flush terminated\n");
437 exit(1);
438 }
439 if (f.flushed == 0) {
440 if (round == 0) {
441 fprintf(stderr, "Nothing to flush.\n");
442 } else if (show_stats)
443 printf("*** Flush is complete after %d round%s ***\n",
444 round, round > 1 ? "s" : "");
445 fflush(stdout);
446 return 0;
447 }
448 round++;
449 if (flush_update() < 0)
450 exit(1);
451 if (show_stats) {
452 printf("\n*** Round %d, deleting %d entries ***\n",
453 round, f.flushed);
454 fflush(stdout);
455 }
456 }
457 return 0;
458 }
459
460 if (ack) {
461 if (rtnl_talk(&grth, &req.n, NULL) < 0)
462 return -2;
463 } else if (atype >= 0) {
464 if (rtnl_talk(&grth, &req.n, &answer) < 0)
465 return -2;
466 if (process_msg(NULL, answer, stdout) < 0) {
467 fprintf(stderr, "Dump terminated\n");
468 exit(1);
469 }
470 free(answer);
471 } else {
472 req.n.nlmsg_seq = grth.dump = ++grth.seq;
473 if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
474 perror("Failed to send dump request");
475 exit(1);
476 }
477
478 if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
479 fprintf(stderr, "Dump terminated\n");
480 exit(1);
481 }
482 }
483 return 0;
484 }
485
486 int do_tcp_metrics(int argc, char **argv)
487 {
488 int i;
489
490 if (argc < 1)
491 return tcpm_do_cmd(CMD_LIST, 0, NULL);
492 for (i = 0; i < ARRAY_SIZE(cmds); i++) {
493 if (matches(argv[0], cmds[i].name) == 0)
494 return tcpm_do_cmd(cmds[i].code, argc-1, argv+1);
495 }
496 if (matches(argv[0], "help") == 0)
497 usage();
498
499 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tcp_metrics help\".\n",
500 *argv);
501 exit(-1);
502 }