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