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
;
99 inet_prefix daddr
, saddr
;
100 int family
, i
, atype
, stype
, dlen
= 0, slen
= 0;
102 if (n
->nlmsg_type
!= genl_family
)
105 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
109 ghdr
= NLMSG_DATA(n
);
110 if (ghdr
->cmd
!= TCP_METRICS_CMD_GET
)
113 parse_rtattr(attrs
, TCP_METRICS_ATTR_MAX
, (void *) ghdr
+ GENL_HDRLEN
,
116 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV4
];
118 if (f
.daddr
.family
&& f
.daddr
.family
!= AF_INET
)
120 memcpy(&daddr
.data
, RTA_DATA(a
), 4);
123 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
124 dlen
= RTA_PAYLOAD(a
);
126 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV6
];
128 if (f
.daddr
.family
&& f
.daddr
.family
!= AF_INET6
)
130 memcpy(&daddr
.data
, RTA_DATA(a
), 16);
133 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
134 dlen
= RTA_PAYLOAD(a
);
139 a
= attrs
[TCP_METRICS_ATTR_SADDR_IPV4
];
141 if (f
.saddr
.family
&& f
.saddr
.family
!= AF_INET
)
143 memcpy(&saddr
.data
, RTA_DATA(a
), 4);
145 stype
= TCP_METRICS_ATTR_SADDR_IPV4
;
146 slen
= RTA_PAYLOAD(a
);
148 a
= attrs
[TCP_METRICS_ATTR_SADDR_IPV6
];
150 if (f
.saddr
.family
&& f
.saddr
.family
!= AF_INET6
)
152 memcpy(&saddr
.data
, RTA_DATA(a
), 16);
154 stype
= TCP_METRICS_ATTR_SADDR_IPV6
;
155 slen
= RTA_PAYLOAD(a
);
159 if (f
.daddr
.family
&& f
.daddr
.bitlen
>= 0 &&
160 inet_addr_match(&daddr
, &f
.daddr
, f
.daddr
.bitlen
))
162 /* Only check for the source-address if the kernel supports it,
165 if (slen
&& f
.saddr
.family
&& f
.saddr
.bitlen
>= 0 &&
166 inet_addr_match(&saddr
, &f
.saddr
, f
.saddr
.bitlen
))
171 TCPM_REQUEST(req2
, 128, TCP_METRICS_CMD_DEL
, NLM_F_REQUEST
);
173 addattr_l(&req2
.n
, sizeof(req2
), atype
, &daddr
.data
,
176 addattr_l(&req2
.n
, sizeof(req2
), stype
, &saddr
.data
,
179 if (NLMSG_ALIGN(f
.flushp
) + req2
.n
.nlmsg_len
> f
.flushe
) {
183 fn
= (struct nlmsghdr
*) (f
.flushb
+ NLMSG_ALIGN(f
.flushp
));
184 memcpy(fn
, &req2
.n
, req2
.n
.nlmsg_len
);
185 fn
->nlmsg_seq
= ++grth
.seq
;
186 f
.flushp
= (((char *) fn
) + req2
.n
.nlmsg_len
) - f
.flushb
;
192 if (f
.cmd
& (CMD_DEL
| CMD_FLUSH
))
193 fprintf(fp
, "Deleted ");
196 format_host(family
, dlen
, &daddr
.data
, abuf
, sizeof(abuf
)));
198 a
= attrs
[TCP_METRICS_ATTR_AGE
];
200 unsigned long long val
= rta_getattr_u64(a
);
202 fprintf(fp
, " age %llu.%03llusec",
203 val
/ 1000, val
% 1000);
206 a
= attrs
[TCP_METRICS_ATTR_TW_TS_STAMP
];
208 __s32 val
= (__s32
) rta_getattr_u32(a
);
211 a
= attrs
[TCP_METRICS_ATTR_TW_TSVAL
];
212 tsval
= a
? rta_getattr_u32(a
) : 0;
213 fprintf(fp
, " tw_ts %u/%dsec ago", tsval
, val
);
216 a
= attrs
[TCP_METRICS_ATTR_VALS
];
218 struct rtattr
*m
[TCP_METRIC_MAX
+ 1 + 1];
219 unsigned long rtt
= 0, rttvar
= 0;
221 parse_rtattr_nested(m
, TCP_METRIC_MAX
+ 1, a
);
223 for (i
= 0; i
< TCP_METRIC_MAX
+ 1; i
++) {
229 if (i
!= TCP_METRIC_RTT
&&
230 i
!= TCP_METRIC_RTT_US
&&
231 i
!= TCP_METRIC_RTTVAR
&&
232 i
!= TCP_METRIC_RTTVAR_US
) {
234 fprintf(fp
, " %s ", metric_name
[i
]);
236 fprintf(fp
, " metric_%d ", i
);
238 val
= rta_getattr_u32(a
);
242 rtt
= (val
* 1000UL) >> 3;
244 case TCP_METRIC_RTTVAR
:
246 rttvar
= (val
* 1000UL) >> 2;
248 case TCP_METRIC_RTT_US
:
251 case TCP_METRIC_RTTVAR_US
:
254 case TCP_METRIC_SSTHRESH
:
255 case TCP_METRIC_CWND
:
256 case TCP_METRIC_REORDERING
:
258 fprintf(fp
, "%lu", val
);
263 fprintf(fp
, " rtt %luus", rtt
);
265 fprintf(fp
, " rttvar %luus", rttvar
);
268 a
= attrs
[TCP_METRICS_ATTR_FOPEN_MSS
];
270 fprintf(fp
, " fo_mss %u", rta_getattr_u16(a
));
272 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROPS
];
274 __u16 syn_loss
= rta_getattr_u16(a
);
275 unsigned long long ts
;
277 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS
];
278 ts
= a
? rta_getattr_u64(a
) : 0;
280 fprintf(fp
, " fo_syn_drops %u/%llu.%03llusec ago",
281 syn_loss
, ts
/ 1000, ts
% 1000);
284 a
= attrs
[TCP_METRICS_ATTR_FOPEN_COOKIE
];
287 unsigned char *ptr
= RTA_DATA(a
);
288 int i
, max
= RTA_PAYLOAD(a
);
293 for (i
= 0; i
< max
; i
++)
294 sprintf(cookie
+ i
+ i
, "%02x", ptr
[i
]);
295 fprintf(fp
, " fo_cookie %s", cookie
);
299 fprintf(fp
, " source %s",
300 format_host(family
, slen
, &saddr
.data
, abuf
,
310 static int tcpm_do_cmd(int cmd
, int argc
, char **argv
)
312 TCPM_REQUEST(req
, 1024, TCP_METRICS_CMD_GET
, NLM_F_REQUEST
);
313 int atype
= -1, stype
= -1;
316 memset(&f
, 0, sizeof(f
));
318 f
.daddr
.family
= preferred_family
;
320 f
.saddr
.family
= preferred_family
;
322 switch (preferred_family
) {
328 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
332 for (; argc
> 0; argc
--, argv
++) {
333 if (strcmp(*argv
, "src") == 0 ||
334 strcmp(*argv
, "source") == 0) {
337 if (matches(*argv
, "help") == 0)
339 if (f
.saddr
.bitlen
>= 0)
342 get_prefix(&f
.saddr
, *argv
, preferred_family
);
343 if (f
.saddr
.bytelen
&& f
.saddr
.bytelen
* 8 == f
.saddr
.bitlen
) {
344 if (f
.saddr
.family
== AF_INET
)
345 stype
= TCP_METRICS_ATTR_SADDR_IPV4
;
346 else if (f
.saddr
.family
== AF_INET6
)
347 stype
= TCP_METRICS_ATTR_SADDR_IPV6
;
351 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
356 char *who
= "address";
357 if (strcmp(*argv
, "addr") == 0 ||
358 strcmp(*argv
, "address") == 0) {
362 if (matches(*argv
, "help") == 0)
364 if (f
.daddr
.bitlen
>= 0)
367 get_prefix(&f
.daddr
, *argv
, preferred_family
);
368 if (f
.daddr
.bytelen
&& f
.daddr
.bytelen
* 8 == f
.daddr
.bitlen
) {
369 if (f
.daddr
.family
== AF_INET
)
370 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
371 else if (f
.daddr
.family
== AF_INET6
)
372 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
374 if ((CMD_DEL
& cmd
) && atype
< 0) {
375 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
383 if (cmd
== CMD_DEL
&& atype
< 0)
386 /* flush for exact address ? Single del */
387 if (cmd
== CMD_FLUSH
&& atype
>= 0)
390 /* flush for all addresses ? Single del without address */
391 if (cmd
== CMD_FLUSH
&& f
.daddr
.bitlen
<= 0 &&
392 f
.saddr
.bitlen
<= 0 && preferred_family
== AF_UNSPEC
) {
394 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
396 } else if (cmd
== CMD_DEL
) {
397 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
399 } else { /* CMD_FLUSH, CMD_LIST */
403 if (genl_family
< 0) {
404 if (rtnl_open_byproto(&grth
, 0, NETLINK_GENERIC
) < 0) {
405 fprintf(stderr
, "Cannot open generic netlink socket\n");
408 genl_family
= genl_resolve_family(&grth
,
409 TCP_METRICS_GENL_NAME
);
412 req
.n
.nlmsg_type
= genl_family
;
415 if (!(cmd
& CMD_FLUSH
) && (atype
>= 0 || (cmd
& CMD_DEL
))) {
417 req
.n
.nlmsg_flags
|= NLM_F_ACK
;
419 addattr_l(&req
.n
, sizeof(req
), atype
, &f
.daddr
.data
,
422 addattr_l(&req
.n
, sizeof(req
), stype
, &f
.saddr
.data
,
425 req
.n
.nlmsg_flags
|= NLM_F_DUMP
;
429 if (cmd
& CMD_FLUSH
) {
431 char flushb
[4096-512];
435 f
.flushe
= sizeof(flushb
);
438 req
.n
.nlmsg_seq
= grth
.dump
= ++grth
.seq
;
439 if (rtnl_send(&grth
, &req
, req
.n
.nlmsg_len
) < 0) {
440 perror("Failed to send flush request");
444 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
445 fprintf(stderr
, "Flush terminated\n");
448 if (f
.flushed
== 0) {
450 fprintf(stderr
, "Nothing to flush.\n");
451 } else if (show_stats
)
452 printf("*** Flush is complete after %d round%s ***\n",
453 round
, round
> 1 ? "s" : "");
458 if (flush_update() < 0)
461 printf("\n*** Round %d, deleting %d entries ***\n",
470 if (rtnl_talk(&grth
, &req
.n
, NULL
, 0) < 0)
472 } else if (atype
>= 0) {
473 if (rtnl_talk(&grth
, &req
.n
, &req
.n
, sizeof(req
)) < 0)
475 if (process_msg(NULL
, &req
.n
, stdout
) < 0) {
476 fprintf(stderr
, "Dump terminated\n");
480 req
.n
.nlmsg_seq
= grth
.dump
= ++grth
.seq
;
481 if (rtnl_send(&grth
, &req
, req
.n
.nlmsg_len
) < 0) {
482 perror("Failed to send dump request");
486 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
487 fprintf(stderr
, "Dump terminated\n");
494 int do_tcp_metrics(int argc
, char **argv
)
499 return tcpm_do_cmd(CMD_LIST
, 0, NULL
);
500 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++) {
501 if (matches(argv
[0], cmds
[i
].name
) == 0)
502 return tcpm_do_cmd(cmds
[i
].code
, argc
-1, argv
+1);
504 if (matches(argv
[0], "help") == 0)
507 fprintf(stderr
, "Command \"%s\" is unknown, "
508 "try \"ip tcp_metrics help\".\n", *argv
);