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