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(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
162 FILE *fp
= (FILE *) arg
;
163 struct genlmsghdr
*ghdr
;
164 struct rtattr
*attrs
[TCP_METRICS_ATTR_MAX
+ 1], *a
;
166 int len
= n
->nlmsg_len
;
167 inet_prefix daddr
, saddr
;
170 if (n
->nlmsg_type
!= genl_family
)
173 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
177 ghdr
= NLMSG_DATA(n
);
178 if (ghdr
->cmd
!= TCP_METRICS_CMD_GET
)
181 parse_rtattr(attrs
, TCP_METRICS_ATTR_MAX
, (void *) ghdr
+ GENL_HDRLEN
,
184 if (attrs
[TCP_METRICS_ATTR_ADDR_IPV4
]) {
185 if (f
.daddr
.family
&& f
.daddr
.family
!= AF_INET
)
187 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV4
];
188 daddr
.family
= AF_INET
;
189 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
190 } else if (attrs
[TCP_METRICS_ATTR_ADDR_IPV6
]) {
191 if (f
.daddr
.family
&& f
.daddr
.family
!= AF_INET6
)
193 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV6
];
194 daddr
.family
= AF_INET6
;
195 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
200 if (get_addr_rta(&daddr
, a
, daddr
.family
))
203 if (f
.daddr
.family
&& f
.daddr
.bitlen
>= 0 &&
204 inet_addr_match(&daddr
, &f
.daddr
, f
.daddr
.bitlen
))
207 if (attrs
[TCP_METRICS_ATTR_SADDR_IPV4
]) {
208 if (f
.saddr
.family
&& f
.saddr
.family
!= AF_INET
)
210 a
= attrs
[TCP_METRICS_ATTR_SADDR_IPV4
];
211 saddr
.family
= AF_INET
;
212 stype
= TCP_METRICS_ATTR_SADDR_IPV4
;
213 } else if (attrs
[TCP_METRICS_ATTR_SADDR_IPV6
]) {
214 if (f
.saddr
.family
&& f
.saddr
.family
!= AF_INET6
)
216 a
= attrs
[TCP_METRICS_ATTR_SADDR_IPV6
];
217 saddr
.family
= AF_INET6
;
218 stype
= TCP_METRICS_ATTR_SADDR_IPV6
;
220 saddr
.family
= AF_UNSPEC
;
224 /* Only get/check for the source-address if the kernel supports it. */
226 if (get_addr_rta(&saddr
, a
, saddr
.family
))
229 if (f
.saddr
.family
&& f
.saddr
.bitlen
>= 0 &&
230 inet_addr_match(&saddr
, &f
.saddr
, f
.saddr
.bitlen
))
237 TCPM_REQUEST(req2
, 128, TCP_METRICS_CMD_DEL
, NLM_F_REQUEST
);
239 addattr_l(&req2
.n
, sizeof(req2
), atype
, daddr
.data
,
242 addattr_l(&req2
.n
, sizeof(req2
), stype
, saddr
.data
,
245 if (NLMSG_ALIGN(f
.flushp
) + req2
.n
.nlmsg_len
> f
.flushe
) {
249 fn
= (struct nlmsghdr
*) (f
.flushb
+ NLMSG_ALIGN(f
.flushp
));
250 memcpy(fn
, &req2
.n
, req2
.n
.nlmsg_len
);
251 fn
->nlmsg_seq
= ++grth
.seq
;
252 f
.flushp
= (((char *) fn
) + req2
.n
.nlmsg_len
) - f
.flushb
;
258 open_json_object(NULL
);
259 if (f
.cmd
& (CMD_DEL
| CMD_FLUSH
))
260 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
262 h
= format_host(daddr
.family
, daddr
.bytelen
, daddr
.data
);
263 print_color_string(PRINT_ANY
,
264 ifa_family_color(daddr
.family
),
267 a
= attrs
[TCP_METRICS_ATTR_AGE
];
269 __u64 val
= rta_getattr_u64(a
);
270 double age
= val
/ 1000.;
272 print_float(PRINT_ANY
, "age",
273 " age %.03fsec", age
);
276 a
= attrs
[TCP_METRICS_ATTR_TW_TS_STAMP
];
278 __s32 val
= (__s32
) rta_getattr_u32(a
);
282 a
= attrs
[TCP_METRICS_ATTR_TW_TSVAL
];
283 tsval
= a
? rta_getattr_u32(a
) : 0;
284 snprintf(tw_ts
, sizeof(tw_ts
),
285 "%u/%d", tsval
, val
);
286 print_string(PRINT_ANY
, "tw_ts_stamp",
287 " tw_ts %s ago", tw_ts
);
290 if (attrs
[TCP_METRICS_ATTR_VALS
])
291 print_tcp_metrics(attrs
[TCP_METRICS_ATTR_VALS
]);
293 a
= attrs
[TCP_METRICS_ATTR_FOPEN_MSS
];
295 print_uint(PRINT_ANY
, "fopen_miss", " fo_mss %u",
299 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROPS
];
301 __u16 syn_loss
= rta_getattr_u16(a
);
304 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS
];
305 ts
= a
? rta_getattr_u64(a
) : 0;
307 print_uint(PRINT_ANY
, "fopen_syn_drops",
308 " fo_syn_drops %u", syn_loss
);
309 print_float(PRINT_ANY
, "fopen_syn_drop_ts",
314 a
= attrs
[TCP_METRICS_ATTR_FOPEN_COOKIE
];
317 unsigned char *ptr
= RTA_DATA(a
);
318 int i
, max
= RTA_PAYLOAD(a
);
323 for (i
= 0; i
< max
; i
++)
324 sprintf(cookie
+ i
+ i
, "%02x", ptr
[i
]);
326 print_string(PRINT_ANY
, "fo_cookie",
327 " fo_cookie %s", cookie
);
333 src
= format_host(saddr
.family
, saddr
.bytelen
, saddr
.data
);
334 print_string(PRINT_ANY
, "source",
338 print_string(PRINT_FP
, NULL
, "\n", "");
344 static int tcpm_do_cmd(int cmd
, int argc
, char **argv
)
346 TCPM_REQUEST(req
, 1024, TCP_METRICS_CMD_GET
, NLM_F_REQUEST
);
347 struct nlmsghdr
*answer
;
348 int atype
= -1, stype
= -1;
351 memset(&f
, 0, sizeof(f
));
353 f
.daddr
.family
= preferred_family
;
355 f
.saddr
.family
= preferred_family
;
357 switch (preferred_family
) {
363 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
367 for (; argc
> 0; argc
--, argv
++) {
368 if (strcmp(*argv
, "src") == 0 ||
369 strcmp(*argv
, "source") == 0) {
373 if (matches(*argv
, "help") == 0)
375 if (f
.saddr
.bitlen
>= 0)
378 get_prefix(&f
.saddr
, *argv
, preferred_family
);
379 if (f
.saddr
.bytelen
&& f
.saddr
.bytelen
* 8 == f
.saddr
.bitlen
) {
380 if (f
.saddr
.family
== AF_INET
)
381 stype
= TCP_METRICS_ATTR_SADDR_IPV4
;
382 else if (f
.saddr
.family
== AF_INET6
)
383 stype
= TCP_METRICS_ATTR_SADDR_IPV6
;
387 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
392 char *who
= "address";
394 if (strcmp(*argv
, "addr") == 0 ||
395 strcmp(*argv
, "address") == 0) {
399 if (matches(*argv
, "help") == 0)
401 if (f
.daddr
.bitlen
>= 0)
404 get_prefix(&f
.daddr
, *argv
, preferred_family
);
405 if (f
.daddr
.bytelen
&& f
.daddr
.bytelen
* 8 == f
.daddr
.bitlen
) {
406 if (f
.daddr
.family
== AF_INET
)
407 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
408 else if (f
.daddr
.family
== AF_INET6
)
409 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
411 if ((CMD_DEL
& cmd
) && atype
< 0) {
412 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
420 if (cmd
== CMD_DEL
&& atype
< 0)
423 /* flush for exact address ? Single del */
424 if (cmd
== CMD_FLUSH
&& atype
>= 0)
427 /* flush for all addresses ? Single del without address */
428 if (cmd
== CMD_FLUSH
&& f
.daddr
.bitlen
<= 0 &&
429 f
.saddr
.bitlen
<= 0 && preferred_family
== AF_UNSPEC
) {
431 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
433 } else if (cmd
== CMD_DEL
) {
434 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
436 } else { /* CMD_FLUSH, CMD_LIST */
440 if (genl_init_handle(&grth
, TCP_METRICS_GENL_NAME
, &genl_family
))
442 req
.n
.nlmsg_type
= genl_family
;
444 if (!(cmd
& CMD_FLUSH
) && (atype
>= 0 || (cmd
& CMD_DEL
))) {
446 req
.n
.nlmsg_flags
|= NLM_F_ACK
;
448 addattr_l(&req
.n
, sizeof(req
), atype
, &f
.daddr
.data
,
451 addattr_l(&req
.n
, sizeof(req
), stype
, &f
.saddr
.data
,
454 req
.n
.nlmsg_flags
|= NLM_F_DUMP
;
458 if (cmd
& CMD_FLUSH
) {
460 char flushb
[4096-512];
464 f
.flushe
= sizeof(flushb
);
467 req
.n
.nlmsg_seq
= grth
.dump
= ++grth
.seq
;
468 if (rtnl_send(&grth
, &req
, req
.n
.nlmsg_len
) < 0) {
469 perror("Failed to send flush request");
473 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
474 fprintf(stderr
, "Flush terminated\n");
477 if (f
.flushed
== 0) {
479 fprintf(stderr
, "Nothing to flush.\n");
480 } else if (show_stats
)
481 printf("*** Flush is complete after %d round%s ***\n",
482 round
, round
> 1 ? "s" : "");
487 if (flush_update() < 0)
490 printf("\n*** Round %d, deleting %d entries ***\n",
499 if (rtnl_talk(&grth
, &req
.n
, NULL
) < 0)
501 } else if (atype
>= 0) {
502 if (rtnl_talk(&grth
, &req
.n
, &answer
) < 0)
504 if (process_msg(NULL
, answer
, stdout
) < 0) {
505 fprintf(stderr
, "Dump terminated\n");
510 req
.n
.nlmsg_seq
= grth
.dump
= ++grth
.seq
;
511 if (rtnl_send(&grth
, &req
, req
.n
.nlmsg_len
) < 0) {
512 perror("Failed to send dump request");
517 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
518 fprintf(stderr
, "Dump terminated\n");
526 int do_tcp_metrics(int argc
, char **argv
)
531 return tcpm_do_cmd(CMD_LIST
, 0, NULL
);
532 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++) {
533 if (matches(argv
[0], cmds
[i
].name
) == 0)
534 return tcpm_do_cmd(cmds
[i
].code
, argc
-1, argv
+1);
536 if (matches(argv
[0], "help") == 0)
539 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tcp_metrics help\".\n",