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;
42 #define TCPM_REQUEST(_req, _bufsiz, _cmd, _flags) \
43 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
44 TCP_METRICS_GENL_VERSION, _cmd, _flags)
46 #define CMD_LIST 0x0001 /* list, lst, show */
47 #define CMD_DEL 0x0002 /* delete, remove */
48 #define CMD_FLUSH 0x0004 /* flush */
57 { "delete", CMD_DEL
},
58 { "remove", CMD_DEL
},
59 { "flush", CMD_FLUSH
},
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",
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 int process_msg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
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 inet_prefix daddr
, saddr
;
99 int family
, i
, atype
, stype
, dlen
= 0, slen
= 0;
101 if (n
->nlmsg_type
!= genl_family
)
104 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
108 ghdr
= NLMSG_DATA(n
);
109 if (ghdr
->cmd
!= TCP_METRICS_CMD_GET
)
112 parse_rtattr(attrs
, TCP_METRICS_ATTR_MAX
, (void *) ghdr
+ GENL_HDRLEN
,
115 if (attrs
[TCP_METRICS_ATTR_ADDR_IPV4
]) {
116 if (f
.daddr
.family
&& f
.daddr
.family
!= AF_INET
)
118 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV4
];
119 memcpy(&daddr
.data
, RTA_DATA(a
), 4);
122 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
123 dlen
= RTA_PAYLOAD(a
);
124 } else if (attrs
[TCP_METRICS_ATTR_ADDR_IPV6
]) {
125 if (f
.daddr
.family
&& f
.daddr
.family
!= AF_INET6
)
127 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV6
];
128 memcpy(&daddr
.data
, RTA_DATA(a
), 16);
131 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
132 dlen
= RTA_PAYLOAD(a
);
137 if (attrs
[TCP_METRICS_ATTR_SADDR_IPV4
]) {
138 if (f
.saddr
.family
&& f
.saddr
.family
!= AF_INET
)
140 a
= attrs
[TCP_METRICS_ATTR_SADDR_IPV4
];
141 memcpy(&saddr
.data
, RTA_DATA(a
), 4);
143 stype
= TCP_METRICS_ATTR_SADDR_IPV4
;
144 slen
= RTA_PAYLOAD(a
);
145 } else if (attrs
[TCP_METRICS_ATTR_SADDR_IPV6
]) {
146 if (f
.saddr
.family
&& f
.saddr
.family
!= AF_INET6
)
148 a
= attrs
[TCP_METRICS_ATTR_SADDR_IPV6
];
149 memcpy(&saddr
.data
, RTA_DATA(a
), 16);
151 stype
= TCP_METRICS_ATTR_SADDR_IPV6
;
152 slen
= RTA_PAYLOAD(a
);
155 if (f
.daddr
.family
&& f
.daddr
.bitlen
>= 0 &&
156 inet_addr_match(&daddr
, &f
.daddr
, f
.daddr
.bitlen
))
158 /* Only check for the source-address if the kernel supports it,
161 if (slen
&& f
.saddr
.family
&& f
.saddr
.bitlen
>= 0 &&
162 inet_addr_match(&saddr
, &f
.saddr
, f
.saddr
.bitlen
))
168 TCPM_REQUEST(req2
, 128, TCP_METRICS_CMD_DEL
, NLM_F_REQUEST
);
170 addattr_l(&req2
.n
, sizeof(req2
), atype
, &daddr
.data
,
173 addattr_l(&req2
.n
, sizeof(req2
), stype
, &saddr
.data
,
176 if (NLMSG_ALIGN(f
.flushp
) + req2
.n
.nlmsg_len
> f
.flushe
) {
180 fn
= (struct nlmsghdr
*) (f
.flushb
+ NLMSG_ALIGN(f
.flushp
));
181 memcpy(fn
, &req2
.n
, req2
.n
.nlmsg_len
);
182 fn
->nlmsg_seq
= ++grth
.seq
;
183 f
.flushp
= (((char *) fn
) + req2
.n
.nlmsg_len
) - f
.flushb
;
189 if (f
.cmd
& (CMD_DEL
| CMD_FLUSH
))
190 fprintf(fp
, "Deleted ");
193 format_host(family
, dlen
, &daddr
.data
));
195 a
= attrs
[TCP_METRICS_ATTR_AGE
];
197 unsigned long long val
= rta_getattr_u64(a
);
199 fprintf(fp
, " age %llu.%03llusec",
200 val
/ 1000, val
% 1000);
203 a
= attrs
[TCP_METRICS_ATTR_TW_TS_STAMP
];
205 __s32 val
= (__s32
) rta_getattr_u32(a
);
208 a
= attrs
[TCP_METRICS_ATTR_TW_TSVAL
];
209 tsval
= a
? rta_getattr_u32(a
) : 0;
210 fprintf(fp
, " tw_ts %u/%dsec ago", tsval
, val
);
213 a
= attrs
[TCP_METRICS_ATTR_VALS
];
215 struct rtattr
*m
[TCP_METRIC_MAX
+ 1 + 1];
216 unsigned long rtt
= 0, rttvar
= 0;
218 parse_rtattr_nested(m
, TCP_METRIC_MAX
+ 1, a
);
220 for (i
= 0; i
< TCP_METRIC_MAX
+ 1; i
++) {
226 if (i
!= TCP_METRIC_RTT
&&
227 i
!= TCP_METRIC_RTT_US
&&
228 i
!= TCP_METRIC_RTTVAR
&&
229 i
!= TCP_METRIC_RTTVAR_US
) {
231 fprintf(fp
, " %s ", metric_name
[i
]);
233 fprintf(fp
, " metric_%d ", i
);
235 val
= rta_getattr_u32(a
);
239 rtt
= (val
* 1000UL) >> 3;
241 case TCP_METRIC_RTTVAR
:
243 rttvar
= (val
* 1000UL) >> 2;
245 case TCP_METRIC_RTT_US
:
248 case TCP_METRIC_RTTVAR_US
:
251 case TCP_METRIC_SSTHRESH
:
252 case TCP_METRIC_CWND
:
253 case TCP_METRIC_REORDERING
:
255 fprintf(fp
, "%lu", val
);
260 fprintf(fp
, " rtt %luus", rtt
);
262 fprintf(fp
, " rttvar %luus", rttvar
);
265 a
= attrs
[TCP_METRICS_ATTR_FOPEN_MSS
];
267 fprintf(fp
, " fo_mss %u", rta_getattr_u16(a
));
269 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROPS
];
271 __u16 syn_loss
= rta_getattr_u16(a
);
272 unsigned long long ts
;
274 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS
];
275 ts
= a
? rta_getattr_u64(a
) : 0;
277 fprintf(fp
, " fo_syn_drops %u/%llu.%03llusec ago",
278 syn_loss
, ts
/ 1000, ts
% 1000);
281 a
= attrs
[TCP_METRICS_ATTR_FOPEN_COOKIE
];
284 unsigned char *ptr
= RTA_DATA(a
);
285 int i
, max
= RTA_PAYLOAD(a
);
290 for (i
= 0; i
< max
; i
++)
291 sprintf(cookie
+ i
+ i
, "%02x", ptr
[i
]);
292 fprintf(fp
, " fo_cookie %s", cookie
);
296 fprintf(fp
, " source %s",
297 format_host(family
, slen
, &saddr
.data
));
306 static int tcpm_do_cmd(int cmd
, int argc
, char **argv
)
308 TCPM_REQUEST(req
, 1024, TCP_METRICS_CMD_GET
, NLM_F_REQUEST
);
309 struct nlmsghdr
*answer
;
310 int atype
= -1, stype
= -1;
313 memset(&f
, 0, sizeof(f
));
315 f
.daddr
.family
= preferred_family
;
317 f
.saddr
.family
= preferred_family
;
319 switch (preferred_family
) {
325 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
329 for (; argc
> 0; argc
--, argv
++) {
330 if (strcmp(*argv
, "src") == 0 ||
331 strcmp(*argv
, "source") == 0) {
335 if (matches(*argv
, "help") == 0)
337 if (f
.saddr
.bitlen
>= 0)
340 get_prefix(&f
.saddr
, *argv
, preferred_family
);
341 if (f
.saddr
.bytelen
&& f
.saddr
.bytelen
* 8 == f
.saddr
.bitlen
) {
342 if (f
.saddr
.family
== AF_INET
)
343 stype
= TCP_METRICS_ATTR_SADDR_IPV4
;
344 else if (f
.saddr
.family
== AF_INET6
)
345 stype
= TCP_METRICS_ATTR_SADDR_IPV6
;
349 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
354 char *who
= "address";
356 if (strcmp(*argv
, "addr") == 0 ||
357 strcmp(*argv
, "address") == 0) {
361 if (matches(*argv
, "help") == 0)
363 if (f
.daddr
.bitlen
>= 0)
366 get_prefix(&f
.daddr
, *argv
, preferred_family
);
367 if (f
.daddr
.bytelen
&& f
.daddr
.bytelen
* 8 == f
.daddr
.bitlen
) {
368 if (f
.daddr
.family
== AF_INET
)
369 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
370 else if (f
.daddr
.family
== AF_INET6
)
371 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
373 if ((CMD_DEL
& cmd
) && atype
< 0) {
374 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
382 if (cmd
== CMD_DEL
&& atype
< 0)
385 /* flush for exact address ? Single del */
386 if (cmd
== CMD_FLUSH
&& atype
>= 0)
389 /* flush for all addresses ? Single del without address */
390 if (cmd
== CMD_FLUSH
&& f
.daddr
.bitlen
<= 0 &&
391 f
.saddr
.bitlen
<= 0 && preferred_family
== AF_UNSPEC
) {
393 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
395 } else if (cmd
== CMD_DEL
) {
396 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
398 } else { /* CMD_FLUSH, CMD_LIST */
402 if (genl_init_handle(&grth
, TCP_METRICS_GENL_NAME
, &genl_family
))
404 req
.n
.nlmsg_type
= genl_family
;
406 if (!(cmd
& CMD_FLUSH
) && (atype
>= 0 || (cmd
& CMD_DEL
))) {
408 req
.n
.nlmsg_flags
|= NLM_F_ACK
;
410 addattr_l(&req
.n
, sizeof(req
), atype
, &f
.daddr
.data
,
413 addattr_l(&req
.n
, sizeof(req
), stype
, &f
.saddr
.data
,
416 req
.n
.nlmsg_flags
|= NLM_F_DUMP
;
420 if (cmd
& CMD_FLUSH
) {
422 char flushb
[4096-512];
426 f
.flushe
= sizeof(flushb
);
429 req
.n
.nlmsg_seq
= grth
.dump
= ++grth
.seq
;
430 if (rtnl_send(&grth
, &req
, req
.n
.nlmsg_len
) < 0) {
431 perror("Failed to send flush request");
435 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
436 fprintf(stderr
, "Flush terminated\n");
439 if (f
.flushed
== 0) {
441 fprintf(stderr
, "Nothing to flush.\n");
442 } else if (show_stats
)
443 printf("*** Flush is complete after %d round%s ***\n",
444 round
, round
> 1 ? "s" : "");
449 if (flush_update() < 0)
452 printf("\n*** Round %d, deleting %d entries ***\n",
461 if (rtnl_talk(&grth
, &req
.n
, NULL
) < 0)
463 } else if (atype
>= 0) {
464 if (rtnl_talk(&grth
, &req
.n
, &answer
) < 0)
466 if (process_msg(NULL
, answer
, stdout
) < 0) {
467 fprintf(stderr
, "Dump terminated\n");
472 req
.n
.nlmsg_seq
= grth
.dump
= ++grth
.seq
;
473 if (rtnl_send(&grth
, &req
, req
.n
.nlmsg_len
) < 0) {
474 perror("Failed to send dump request");
478 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
479 fprintf(stderr
, "Dump terminated\n");
486 int do_tcp_metrics(int argc
, char **argv
)
491 return tcpm_do_cmd(CMD_LIST
, 0, NULL
);
492 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++) {
493 if (matches(argv
[0], cmds
[i
].name
) == 0)
494 return tcpm_do_cmd(cmds
[i
].code
, argc
-1, argv
+1);
496 if (matches(argv
[0], "help") == 0)
499 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tcp_metrics help\".\n",