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",
80 static int flush_update(void)
82 if (rtnl_send_check(&grth
, f
.flushb
, f
.flushp
) < 0) {
83 perror("Failed to send flush request\n");
90 static int process_msg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
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
;
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 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV4
];
117 if (f
.addr
.family
&& f
.addr
.family
!= AF_INET
)
119 memcpy(&addr
.data
, RTA_DATA(a
), 4);
122 atype
= TCP_METRICS_ATTR_ADDR_IPV4
;
124 a
= attrs
[TCP_METRICS_ATTR_ADDR_IPV6
];
126 if (f
.addr
.family
&& f
.addr
.family
!= AF_INET6
)
128 memcpy(&addr
.data
, RTA_DATA(a
), 16);
131 atype
= TCP_METRICS_ATTR_ADDR_IPV6
;
136 if (f
.addr
.family
&& f
.addr
.bitlen
>= 0 &&
137 inet_addr_match(&addr
, &f
.addr
, f
.addr
.bitlen
))
142 TCPM_REQUEST(req2
, 128, TCP_METRICS_CMD_DEL
, NLM_F_REQUEST
);
144 addattr_l(&req2
.n
, sizeof(req2
), atype
, &addr
.data
,
147 if (NLMSG_ALIGN(f
.flushp
) + req2
.n
.nlmsg_len
> f
.flushe
) {
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
;
160 if (f
.cmd
& (CMD_DEL
| CMD_FLUSH
))
161 fprintf(fp
, "Deleted ");
164 format_host(family
, RTA_PAYLOAD(a
), &addr
.data
,
165 abuf
, sizeof(abuf
)));
167 a
= attrs
[TCP_METRICS_ATTR_AGE
];
169 __u64 val
= rta_getattr_u64(a
);
171 fprintf(fp
, " age %llu.%03llusec",
172 val
/ 1000, val
% 1000);
175 a
= attrs
[TCP_METRICS_ATTR_TW_TS_STAMP
];
177 __s32 val
= (__s32
) rta_getattr_u32(a
);
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
);
185 a
= attrs
[TCP_METRICS_ATTR_VALS
];
187 struct rtattr
*m
[TCP_METRIC_MAX
+ 1 + 1];
189 parse_rtattr_nested(m
, TCP_METRIC_MAX
+ 1, a
);
191 for (i
= 0; i
< TCP_METRIC_MAX
+ 1; i
++) {
198 fprintf(fp
, " %s ", metric_name
[i
]);
200 fprintf(fp
, " metric_%d ", i
);
201 val
= rta_getattr_u32(a
);
204 fprintf(fp
, "%lluus", (val
* 1000ULL) >> 3);
206 case TCP_METRIC_RTTVAR
:
207 fprintf(fp
, "%lluus", (val
* 1000ULL) >> 2);
209 case TCP_METRIC_SSTHRESH
:
210 case TCP_METRIC_CWND
:
211 case TCP_METRIC_REORDERING
:
213 fprintf(fp
, "%u", val
);
219 a
= attrs
[TCP_METRICS_ATTR_FOPEN_MSS
];
221 fprintf(fp
, " fo_mss %u", rta_getattr_u16(a
));
223 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROPS
];
225 __u16 syn_loss
= rta_getattr_u16(a
);
228 a
= attrs
[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS
];
229 ts
= a
? rta_getattr_u64(a
) : 0;
231 fprintf(fp
, " fo_syn_drops %u/%llu.%03llusec ago",
232 syn_loss
, ts
/ 1000, ts
% 1000);
235 a
= attrs
[TCP_METRICS_ATTR_FOPEN_COOKIE
];
238 unsigned char *ptr
= RTA_DATA(a
);
239 int i
, max
= RTA_PAYLOAD(a
);
244 for (i
= 0; i
< max
; i
++)
245 sprintf(cookie
+ i
+ i
, "%02x", ptr
[i
]);
246 fprintf(fp
, " fo_cookie %s", cookie
);
255 static int tcpm_do_cmd(int cmd
, int argc
, char **argv
)
257 TCPM_REQUEST(req
, 1024, TCP_METRICS_CMD_GET
, NLM_F_REQUEST
);
261 memset(&f
, 0, sizeof(f
));
263 f
.addr
.family
= preferred_family
;
265 switch (preferred_family
) {
271 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
275 for (; argc
> 0; argc
--, argv
++) {
276 char *who
= "address";
278 if (strcmp(*argv
, "addr") == 0 ||
279 strcmp(*argv
, "address") == 0) {
283 if (matches(*argv
, "help") == 0)
285 if (f
.addr
.bitlen
>= 0)
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
;
295 if ((CMD_DEL
& cmd
) && atype
< 0) {
296 fprintf(stderr
, "Error: a specific IP address is expected rather than \"%s\"\n",
304 if (cmd
== CMD_DEL
&& atype
< 0)
307 /* flush for exact address ? Single del */
308 if (cmd
== CMD_FLUSH
&& atype
>= 0)
311 /* flush for all addresses ? Single del without address */
312 if (cmd
== CMD_FLUSH
&& f
.addr
.bitlen
<= 0 &&
313 preferred_family
== AF_UNSPEC
) {
315 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
317 } else if (cmd
== CMD_DEL
) {
318 req
.g
.cmd
= TCP_METRICS_CMD_DEL
;
320 } else { /* CMD_FLUSH, CMD_LIST */
324 if (genl_family
< 0) {
325 if (rtnl_open_byproto(&grth
, 0, NETLINK_GENERIC
) < 0) {
326 fprintf(stderr
, "Cannot open generic netlink socket\n");
329 genl_family
= genl_resolve_family(&grth
,
330 TCP_METRICS_GENL_NAME
);
333 req
.n
.nlmsg_type
= genl_family
;
336 if (!(cmd
& CMD_FLUSH
) && (atype
>= 0 || (cmd
& CMD_DEL
))) {
338 req
.n
.nlmsg_flags
|= NLM_F_ACK
;
340 addattr_l(&req
.n
, sizeof(req
), atype
, &f
.addr
.data
,
343 req
.n
.nlmsg_flags
|= NLM_F_DUMP
;
347 if (cmd
& CMD_FLUSH
) {
349 char flushb
[4096-512];
353 f
.flushe
= sizeof(flushb
);
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");
362 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
363 fprintf(stderr
, "Flush terminated\n");
366 if (f
.flushed
== 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" : "");
376 if (flush_update() < 0)
379 printf("\n*** Round %d, deleting %d entries ***\n",
388 if (rtnl_talk(&grth
, &req
.n
, 0, 0, NULL
) < 0)
390 } else if (atype
>= 0) {
391 if (rtnl_talk(&grth
, &req
.n
, 0, 0, &req
.n
) < 0)
393 if (process_msg(NULL
, &req
.n
, stdout
) < 0) {
394 fprintf(stderr
, "Dump terminated\n");
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");
404 if (rtnl_dump_filter(&grth
, process_msg
, stdout
) < 0) {
405 fprintf(stderr
, "Dump terminated\n");
412 int do_tcp_metrics(int argc
, char **argv
)
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);
422 if (matches(argv
[0], "help") == 0)
425 fprintf(stderr
, "Command \"%s\" is unknown, "
426 "try \"ip tcp_metrics help\".\n", *argv
);