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