2 * tcp_metrics.c "ip tcp_metrics/tcpmetrics"
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;
8 * Authors: Julian Anastasov <ja@ssi.bg>, August 2012
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <arpa/inet.h>
19 #include <sys/ioctl.h>
22 #include <linux/genetlink.h>
23 #include <linux/tcp_metrics.h>
26 #include "ip_common.h"
29 static void usage(void)
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");
39 static struct rtnl_handle grth
= { .fd
= -1 };
40 static int genl_family
= -1;
41 static const double usec_per_sec
= 1000000.;
43 #define TCPM_REQUEST(_req, _bufsiz, _cmd, _flags) \
44 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
45 TCP_METRICS_GENL_VERSION, _cmd, _flags)
47 #define CMD_LIST 0x0001 /* list, lst, show */
48 #define CMD_DEL 0x0002 /* delete, remove */
49 #define CMD_FLUSH 0x0004 /* flush */
58 { "delete", CMD_DEL
},
59 { "remove", CMD_DEL
},
60 { "flush", CMD_FLUSH
},
63 static const char *metric_name
[TCP_METRIC_MAX
+ 1] = {
64 [TCP_METRIC_RTT
] = "rtt",
65 [TCP_METRIC_RTTVAR
] = "rttvar",
66 [TCP_METRIC_SSTHRESH
] = "ssthresh",
67 [TCP_METRIC_CWND
] = "cwnd",
68 [TCP_METRIC_REORDERING
] = "reordering",
81 static int flush_update(void)
83 if (rtnl_send_check(&grth
, f
.flushb
, f
.flushp
) < 0) {
84 perror("Failed to send flush request\n");
91 static void print_tcp_metrics(struct rtattr
*a
)
93 struct rtattr
*m
[TCP_METRIC_MAX
+ 1 + 1];
94 unsigned long rtt
= 0, rttvar
= 0;
97 parse_rtattr_nested(m
, TCP_METRIC_MAX
+ 1, a
);
99 for (i
= 0; i
< TCP_METRIC_MAX
+ 1; i
++) {
108 val
= rta_getattr_u32(a
);
113 rtt
= (val
* 1000UL) >> 3;
115 case TCP_METRIC_RTTVAR
:
117 rttvar
= (val
* 1000UL) >> 2;
119 case TCP_METRIC_RTT_US
:
123 case TCP_METRIC_RTTVAR_US
:
127 case TCP_METRIC_SSTHRESH
:
128 case TCP_METRIC_CWND
:
129 case TCP_METRIC_REORDERING
:
130 name
= metric_name
[i
];
134 snprintf(b1
, sizeof(b1
),
140 print_uint(PRINT_JSON
, name
, NULL
, val
);
141 print_string(PRINT_FP
, NULL
, " %s ", name
);
142 print_uint(PRINT_FP
, NULL
, "%u", val
);
146 print_float(PRINT_JSON
, "rtt", NULL
,
147 (double)rtt
/ usec_per_sec
);
148 print_u64(PRINT_FP
, NULL
,
152 print_float(PRINT_JSON
, "rttvar", NULL
,
153 (double) rttvar
/ usec_per_sec
);
154 print_u64(PRINT_FP
, NULL
,
155 " rttvar %luus", rttvar
);
159 static int process_msg(struct nlmsghdr
*n
, void *arg
)
161 FILE *fp
= (FILE *) arg
;
162 struct genlmsghdr
*ghdr
;
163 struct rtattr
*attrs
[TCP_METRICS_ATTR_MAX
+ 1], *a
;
165 int len
= n
->nlmsg_len
;
166 inet_prefix daddr
, saddr
;
169 if (n
->nlmsg_type
!= genl_family
)
172 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
176 ghdr
= NLMSG_DATA(n
);
177 if (ghdr
->cmd
!= TCP_METRICS_CMD_GET
)
180 parse_rtattr(attrs
, TCP_METRICS_ATTR_MAX
, (void *) ghdr
+ GENL_HDRLEN
,
183 if (attrs
[TCP_METRICS_ATTR_ADDR_IPV4
]) {
184 if (f
.daddr
.family
&& f
.daddr
.family
!= AF_INET
)
186 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV4
];
187 daddr
.family
= AF_INET
;
188 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
189 } else if (attrs
[TCP_METRICS_ATTR_ADDR_IPV6
]) {
190 if (f
.daddr
.family
&& f
.daddr
.family
!= AF_INET6
)
192 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV6
];
193 daddr
.family
= AF_INET6
;
194 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
199 if (get_addr_rta(&daddr
, a
, daddr
.family
))
202 if (f
.daddr
.family
&& f
.daddr
.bitlen
>= 0 &&
203 inet_addr_match(&daddr
, &f
.daddr
, f
.daddr
.bitlen
))
206 if (attrs
[TCP_METRICS_ATTR_SADDR_IPV4
]) {
207 if (f
.saddr
.family
&& f
.saddr
.family
!= AF_INET
)
209 a
= attrs
[TCP_METRICS_ATTR_SADDR_IPV4
];
210 saddr
.family
= AF_INET
;
211 stype
= TCP_METRICS_ATTR_SADDR_IPV4
;
212 } else if (attrs
[TCP_METRICS_ATTR_SADDR_IPV6
]) {
213 if (f
.saddr
.family
&& f
.saddr
.family
!= AF_INET6
)
215 a
= attrs
[TCP_METRICS_ATTR_SADDR_IPV6
];
216 saddr
.family
= AF_INET6
;
217 stype
= TCP_METRICS_ATTR_SADDR_IPV6
;
219 saddr
.family
= AF_UNSPEC
;
223 /* Only get/check for the source-address if the kernel supports it. */
225 if (get_addr_rta(&saddr
, a
, saddr
.family
))
228 if (f
.saddr
.family
&& f
.saddr
.bitlen
>= 0 &&
229 inet_addr_match(&saddr
, &f
.saddr
, f
.saddr
.bitlen
))
236 TCPM_REQUEST(req2
, 128, TCP_METRICS_CMD_DEL
, NLM_F_REQUEST
);
238 addattr_l(&req2
.n
, sizeof(req2
), atype
, daddr
.data
,
241 addattr_l(&req2
.n
, sizeof(req2
), stype
, saddr
.data
,
244 if (NLMSG_ALIGN(f
.flushp
) + req2
.n
.nlmsg_len
> f
.flushe
) {
248 fn
= (struct nlmsghdr
*) (f
.flushb
+ NLMSG_ALIGN(f
.flushp
));
249 memcpy(fn
, &req2
.n
, req2
.n
.nlmsg_len
);
250 fn
->nlmsg_seq
= ++grth
.seq
;
251 f
.flushp
= (((char *) fn
) + req2
.n
.nlmsg_len
) - f
.flushb
;
257 open_json_object(NULL
);
258 if (f
.cmd
& (CMD_DEL
| CMD_FLUSH
))
259 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
261 h
= format_host(daddr
.family
, daddr
.bytelen
, daddr
.data
);
262 print_color_string(PRINT_ANY
,
263 ifa_family_color(daddr
.family
),
266 a
= attrs
[TCP_METRICS_ATTR_AGE
];
268 __u64 val
= rta_getattr_u64(a
);
269 double age
= val
/ 1000.;
271 print_float(PRINT_ANY
, "age",
272 " age %.03fsec", age
);
275 a
= attrs
[TCP_METRICS_ATTR_TW_TS_STAMP
];
277 __s32 val
= (__s32
) rta_getattr_u32(a
);
281 a
= attrs
[TCP_METRICS_ATTR_TW_TSVAL
];
282 tsval
= a
? rta_getattr_u32(a
) : 0;
283 snprintf(tw_ts
, sizeof(tw_ts
),
284 "%u/%d", tsval
, val
);
285 print_string(PRINT_ANY
, "tw_ts_stamp",
286 " tw_ts %s ago", tw_ts
);
289 if (attrs
[TCP_METRICS_ATTR_VALS
])
290 print_tcp_metrics(attrs
[TCP_METRICS_ATTR_VALS
]);
292 a
= attrs
[TCP_METRICS_ATTR_FOPEN_MSS
];
294 print_uint(PRINT_ANY
, "fopen_miss", " fo_mss %u",
298 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROPS
];
300 __u16 syn_loss
= rta_getattr_u16(a
);
303 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS
];
304 ts
= a
? rta_getattr_u64(a
) : 0;
306 print_uint(PRINT_ANY
, "fopen_syn_drops",
307 " fo_syn_drops %u", syn_loss
);
308 print_float(PRINT_ANY
, "fopen_syn_drop_ts",
313 a
= attrs
[TCP_METRICS_ATTR_FOPEN_COOKIE
];
316 unsigned char *ptr
= RTA_DATA(a
);
317 int i
, max
= RTA_PAYLOAD(a
);
322 for (i
= 0; i
< max
; i
++)
323 sprintf(cookie
+ i
+ i
, "%02x", ptr
[i
]);
325 print_string(PRINT_ANY
, "fo_cookie",
326 " fo_cookie %s", cookie
);
332 src
= format_host(saddr
.family
, saddr
.bytelen
, saddr
.data
);
333 print_string(PRINT_ANY
, "source",
337 print_string(PRINT_FP
, NULL
, "\n", "");
343 static int tcpm_do_cmd(int cmd
, int argc
, char **argv
)
345 TCPM_REQUEST(req
, 1024, TCP_METRICS_CMD_GET
, NLM_F_REQUEST
);
346 struct nlmsghdr
*answer
;
347 int atype
= -1, stype
= -1;
350 memset(&f
, 0, sizeof(f
));
352 f
.daddr
.family
= preferred_family
;
354 f
.saddr
.family
= preferred_family
;
356 switch (preferred_family
) {
362 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
366 for (; argc
> 0; argc
--, argv
++) {
367 if (strcmp(*argv
, "src") == 0 ||
368 strcmp(*argv
, "source") == 0) {
372 if (matches(*argv
, "help") == 0)
374 if (f
.saddr
.bitlen
>= 0)
377 get_prefix(&f
.saddr
, *argv
, preferred_family
);
378 if (f
.saddr
.bytelen
&& f
.saddr
.bytelen
* 8 == f
.saddr
.bitlen
) {
379 if (f
.saddr
.family
== AF_INET
)
380 stype
= TCP_METRICS_ATTR_SADDR_IPV4
;
381 else if (f
.saddr
.family
== AF_INET6
)
382 stype
= TCP_METRICS_ATTR_SADDR_IPV6
;
386 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
391 char *who
= "address";
393 if (strcmp(*argv
, "addr") == 0 ||
394 strcmp(*argv
, "address") == 0) {
398 if (matches(*argv
, "help") == 0)
400 if (f
.daddr
.bitlen
>= 0)
403 get_prefix(&f
.daddr
, *argv
, preferred_family
);
404 if (f
.daddr
.bytelen
&& f
.daddr
.bytelen
* 8 == f
.daddr
.bitlen
) {
405 if (f
.daddr
.family
== AF_INET
)
406 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
407 else if (f
.daddr
.family
== AF_INET6
)
408 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
410 if ((CMD_DEL
& cmd
) && atype
< 0) {
411 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
419 if (cmd
== CMD_DEL
&& atype
< 0)
422 /* flush for exact address ? Single del */
423 if (cmd
== CMD_FLUSH
&& atype
>= 0)
426 /* flush for all addresses ? Single del without address */
427 if (cmd
== CMD_FLUSH
&& f
.daddr
.bitlen
<= 0 &&
428 f
.saddr
.bitlen
<= 0 && preferred_family
== AF_UNSPEC
) {
430 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
432 } else if (cmd
== CMD_DEL
) {
433 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
435 } else { /* CMD_FLUSH, CMD_LIST */
439 if (genl_init_handle(&grth
, TCP_METRICS_GENL_NAME
, &genl_family
))
441 req
.n
.nlmsg_type
= genl_family
;
443 if (!(cmd
& CMD_FLUSH
) && (atype
>= 0 || (cmd
& CMD_DEL
))) {
445 req
.n
.nlmsg_flags
|= NLM_F_ACK
;
447 addattr_l(&req
.n
, sizeof(req
), atype
, &f
.daddr
.data
,
450 addattr_l(&req
.n
, sizeof(req
), stype
, &f
.saddr
.data
,
453 req
.n
.nlmsg_flags
|= NLM_F_DUMP
;
457 if (cmd
& CMD_FLUSH
) {
459 char flushb
[4096-512];
463 f
.flushe
= sizeof(flushb
);
466 req
.n
.nlmsg_seq
= grth
.dump
= ++grth
.seq
;
467 if (rtnl_send(&grth
, &req
, req
.n
.nlmsg_len
) < 0) {
468 perror("Failed to send flush request");
472 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
473 fprintf(stderr
, "Flush terminated\n");
476 if (f
.flushed
== 0) {
478 fprintf(stderr
, "Nothing to flush.\n");
479 } else if (show_stats
)
480 printf("*** Flush is complete after %d round%s ***\n",
481 round
, round
> 1 ? "s" : "");
486 if (flush_update() < 0)
489 printf("\n*** Round %d, deleting %d entries ***\n",
498 if (rtnl_talk(&grth
, &req
.n
, NULL
) < 0)
500 } else if (atype
>= 0) {
501 if (rtnl_talk(&grth
, &req
.n
, &answer
) < 0)
503 if (process_msg(answer
, stdout
) < 0) {
504 fprintf(stderr
, "Dump terminated\n");
509 req
.n
.nlmsg_seq
= grth
.dump
= ++grth
.seq
;
510 if (rtnl_send(&grth
, &req
, req
.n
.nlmsg_len
) < 0) {
511 perror("Failed to send dump request");
516 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
517 fprintf(stderr
, "Dump terminated\n");
525 int do_tcp_metrics(int argc
, char **argv
)
530 return tcpm_do_cmd(CMD_LIST
, 0, NULL
);
531 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++) {
532 if (matches(argv
[0], cmds
[i
].name
) == 0)
533 return tcpm_do_cmd(cmds
[i
].code
, argc
-1, argv
+1);
535 if (matches(argv
[0], "help") == 0)
538 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tcp_metrics help\".\n",