]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/tcp_metrics.c
ip: allow to specify mode for sit tunnels
[mirror_iproute2.git] / ip / tcp_metrics.c
CommitLineData
ea63a69b
JA
1/*
2 * tcp_metrics.c "ip tcp_metrics/tcpmetrics"
3 *
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;
7 *
8 * Authors: Julian Anastasov <ja@ssi.bg>, August 2012
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
15#include <errno.h>
16#include <sys/types.h>
17#include <sys/socket.h>
18#include <arpa/inet.h>
19#include <sys/ioctl.h>
20#include <linux/if.h>
21
22#include <linux/genetlink.h>
23#include <linux/tcp_metrics.h>
24
25#include "utils.h"
26#include "ip_common.h"
27#include "libgenl.h"
28
29static void usage(void)
30{
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");
35 exit(-1);
36}
37
38/* netlink socket */
39static struct rtnl_handle grth = { .fd = -1 };
40static int genl_family = -1;
41
42#define TCPM_REQUEST(_req, _bufsiz, _cmd, _flags) \
43 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
44 TCP_METRICS_GENL_VERSION, _cmd, _flags)
45
46#define CMD_LIST 0x0001 /* list, lst, show */
47#define CMD_DEL 0x0002 /* delete, remove */
48#define CMD_FLUSH 0x0004 /* flush */
49
50static struct {
51 char *name;
52 int code;
53} cmds[] = {
54 { "list", CMD_LIST },
55 { "lst", CMD_LIST },
56 { "show", CMD_LIST },
57 { "delete", CMD_DEL },
58 { "remove", CMD_DEL },
59 { "flush", CMD_FLUSH },
60};
61
62static 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",
68};
69
70static struct
71{
72 int flushed;
73 char *flushb;
74 int flushp;
75 int flushe;
76 int cmd;
77 inet_prefix addr;
78} f;
79
80static int flush_update(void)
81{
82 if (rtnl_send_check(&grth, f.flushb, f.flushp) < 0) {
83 perror("Failed to send flush request\n");
84 return -1;
85 }
86 f.flushp = 0;
87 return 0;
88}
89
90static int process_msg(const struct sockaddr_nl *who, struct nlmsghdr *n,
91 void *arg)
92{
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;
97 char abuf[256];
98 inet_prefix addr;
99 int family, i, atype;
100
101 if (n->nlmsg_type != genl_family)
102 return -1;
103
104 len -= NLMSG_LENGTH(GENL_HDRLEN);
105 if (len < 0)
106 return -1;
107
108 ghdr = NLMSG_DATA(n);
109 if (ghdr->cmd != TCP_METRICS_CMD_GET)
110 return 0;
111
112 parse_rtattr(attrs, TCP_METRICS_ATTR_MAX, (void *) ghdr + GENL_HDRLEN,
113 len);
114
115 a = attrs[TCP_METRICS_ATTR_ADDR_IPV4];
116 if (a) {
117 if (f.addr.family && f.addr.family != AF_INET)
118 return 0;
119 memcpy(&addr.data, RTA_DATA(a), 4);
120 addr.bytelen = 4;
121 family = AF_INET;
122 atype = TCP_METRICS_ATTR_ADDR_IPV4;
123 } else {
124 a = attrs[TCP_METRICS_ATTR_ADDR_IPV6];
125 if (a) {
126 if (f.addr.family && f.addr.family != AF_INET6)
127 return 0;
128 memcpy(&addr.data, RTA_DATA(a), 16);
129 addr.bytelen = 16;
130 family = AF_INET6;
131 atype = TCP_METRICS_ATTR_ADDR_IPV6;
132 } else
133 return 0;
134 }
135
136 if (f.addr.family && f.addr.bitlen >= 0 &&
137 inet_addr_match(&addr, &f.addr, f.addr.bitlen))
138 return 0;
139
140 if (f.flushb) {
141 struct nlmsghdr *fn;
142 TCPM_REQUEST(req2, 128, TCP_METRICS_CMD_DEL, NLM_F_REQUEST);
143
144 addattr_l(&req2.n, sizeof(req2), atype, &addr.data,
145 addr.bytelen);
146
147 if (NLMSG_ALIGN(f.flushp) + req2.n.nlmsg_len > f.flushe) {
148 if (flush_update())
149 return -1;
150 }
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;
155 f.flushed++;
156 if (show_stats < 2)
157 return 0;
158 }
159
160 if (f.cmd & (CMD_DEL | CMD_FLUSH))
161 fprintf(fp, "Deleted ");
162
163 fprintf(fp, "%s",
164 format_host(family, RTA_PAYLOAD(a), &addr.data,
165 abuf, sizeof(abuf)));
166
167 a = attrs[TCP_METRICS_ATTR_AGE];
168 if (a) {
a55a8fd8 169 unsigned long long val = rta_getattr_u64(a);
ea63a69b
JA
170
171 fprintf(fp, " age %llu.%03llusec",
172 val / 1000, val % 1000);
173 }
174
175 a = attrs[TCP_METRICS_ATTR_TW_TS_STAMP];
176 if (a) {
177 __s32 val = (__s32) rta_getattr_u32(a);
178 __u32 tsval;
179
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);
183 }
184
185 a = attrs[TCP_METRICS_ATTR_VALS];
186 if (a) {
187 struct rtattr *m[TCP_METRIC_MAX + 1 + 1];
188
189 parse_rtattr_nested(m, TCP_METRIC_MAX + 1, a);
190
191 for (i = 0; i < TCP_METRIC_MAX + 1; i++) {
a55a8fd8 192 unsigned long val;
ea63a69b
JA
193
194 a = m[i + 1];
195 if (!a)
196 continue;
197 if (metric_name[i])
198 fprintf(fp, " %s ", metric_name[i]);
199 else
200 fprintf(fp, " metric_%d ", i);
a55a8fd8 201
ea63a69b
JA
202 val = rta_getattr_u32(a);
203 switch (i) {
204 case TCP_METRIC_RTT:
a55a8fd8 205 fprintf(fp, "%luus", (val * 1000UL) >> 3);
ea63a69b
JA
206 break;
207 case TCP_METRIC_RTTVAR:
a55a8fd8 208 fprintf(fp, "%luus", (val * 1000UL) >> 2);
ea63a69b
JA
209 break;
210 case TCP_METRIC_SSTHRESH:
211 case TCP_METRIC_CWND:
212 case TCP_METRIC_REORDERING:
213 default:
a55a8fd8 214 fprintf(fp, "%lu", val);
ea63a69b
JA
215 break;
216 }
217 }
218 }
219
220 a = attrs[TCP_METRICS_ATTR_FOPEN_MSS];
221 if (a)
222 fprintf(fp, " fo_mss %u", rta_getattr_u16(a));
223
224 a = attrs[TCP_METRICS_ATTR_FOPEN_SYN_DROPS];
225 if (a) {
226 __u16 syn_loss = rta_getattr_u16(a);
a55a8fd8 227 unsigned long long ts;
ea63a69b
JA
228
229 a = attrs[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS];
230 ts = a ? rta_getattr_u64(a) : 0;
231
232 fprintf(fp, " fo_syn_drops %u/%llu.%03llusec ago",
233 syn_loss, ts / 1000, ts % 1000);
234 }
235
236 a = attrs[TCP_METRICS_ATTR_FOPEN_COOKIE];
237 if (a) {
238 char cookie[32 + 1];
239 unsigned char *ptr = RTA_DATA(a);
240 int i, max = RTA_PAYLOAD(a);
241
242 if (max > 16)
243 max = 16;
244 cookie[0] = 0;
245 for (i = 0; i < max; i++)
246 sprintf(cookie + i + i, "%02x", ptr[i]);
247 fprintf(fp, " fo_cookie %s", cookie);
248 }
249
250 fprintf(fp, "\n");
251
252 fflush(fp);
253 return 0;
254}
255
256static int tcpm_do_cmd(int cmd, int argc, char **argv)
257{
258 TCPM_REQUEST(req, 1024, TCP_METRICS_CMD_GET, NLM_F_REQUEST);
259 int atype = -1;
260 int ack;
261
262 memset(&f, 0, sizeof(f));
263 f.addr.bitlen = -1;
264 f.addr.family = preferred_family;
265
266 switch (preferred_family) {
267 case AF_UNSPEC:
268 case AF_INET:
269 case AF_INET6:
270 break;
271 default:
14645ec2 272 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
ea63a69b
JA
273 return -1;
274 }
275
276 for (; argc > 0; argc--, argv++) {
277 char *who = "address";
278
279 if (strcmp(*argv, "addr") == 0 ||
280 strcmp(*argv, "address") == 0) {
281 who = *argv;
282 NEXT_ARG();
283 }
284 if (matches(*argv, "help") == 0)
285 usage();
286 if (f.addr.bitlen >= 0)
287 duparg2(who, *argv);
288
289 get_prefix(&f.addr, *argv, preferred_family);
290 if (f.addr.bytelen && f.addr.bytelen * 8 == f.addr.bitlen) {
291 if (f.addr.family == AF_INET)
292 atype = TCP_METRICS_ATTR_ADDR_IPV4;
293 else if (f.addr.family == AF_INET6)
294 atype = TCP_METRICS_ATTR_ADDR_IPV6;
295 }
296 if ((CMD_DEL & cmd) && atype < 0) {
297 fprintf(stderr, "Error: a specific IP address is expected rather than \"%s\"\n",
298 *argv);
299 return -1;
300 }
301
302 argc--; argv++;
303 }
304
305 if (cmd == CMD_DEL && atype < 0)
306 missarg("address");
307
308 /* flush for exact address ? Single del */
309 if (cmd == CMD_FLUSH && atype >= 0)
310 cmd = CMD_DEL;
311
312 /* flush for all addresses ? Single del without address */
313 if (cmd == CMD_FLUSH && f.addr.bitlen <= 0 &&
314 preferred_family == AF_UNSPEC) {
315 cmd = CMD_DEL;
316 req.g.cmd = TCP_METRICS_CMD_DEL;
317 ack = 1;
318 } else if (cmd == CMD_DEL) {
319 req.g.cmd = TCP_METRICS_CMD_DEL;
320 ack = 1;
321 } else { /* CMD_FLUSH, CMD_LIST */
322 ack = 0;
323 }
324
325 if (genl_family < 0) {
326 if (rtnl_open_byproto(&grth, 0, NETLINK_GENERIC) < 0) {
327 fprintf(stderr, "Cannot open generic netlink socket\n");
328 exit(1);
329 }
330 genl_family = genl_resolve_family(&grth,
331 TCP_METRICS_GENL_NAME);
332 if (genl_family < 0)
333 exit(1);
334 req.n.nlmsg_type = genl_family;
335 }
336
337 if (!(cmd & CMD_FLUSH) && (atype >= 0 || (cmd & CMD_DEL))) {
338 if (ack)
339 req.n.nlmsg_flags |= NLM_F_ACK;
340 if (atype >= 0)
341 addattr_l(&req.n, sizeof(req), atype, &f.addr.data,
342 f.addr.bytelen);
343 } else {
344 req.n.nlmsg_flags |= NLM_F_DUMP;
345 }
346
347 f.cmd = cmd;
348 if (cmd & CMD_FLUSH) {
349 int round = 0;
350 char flushb[4096-512];
351
352 f.flushb = flushb;
353 f.flushp = 0;
354 f.flushe = sizeof(flushb);
355
356 for (;;) {
357 req.n.nlmsg_seq = grth.dump = ++grth.seq;
358 if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
359 perror("Failed to send flush request");
360 exit(1);
361 }
362 f.flushed = 0;
363 if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
364 fprintf(stderr, "Flush terminated\n");
365 exit(1);
366 }
367 if (f.flushed == 0) {
368 if (round == 0) {
369 fprintf(stderr, "Nothing to flush.\n");
370 } else if (show_stats)
371 printf("*** Flush is complete after %d round%s ***\n",
372 round, round > 1 ? "s" : "");
373 fflush(stdout);
374 return 0;
375 }
376 round++;
377 if (flush_update() < 0)
378 exit(1);
379 if (show_stats) {
380 printf("\n*** Round %d, deleting %d entries ***\n",
381 round, f.flushed);
382 fflush(stdout);
383 }
384 }
385 return 0;
386 }
387
388 if (ack) {
389 if (rtnl_talk(&grth, &req.n, 0, 0, NULL) < 0)
390 return -2;
391 } else if (atype >= 0) {
392 if (rtnl_talk(&grth, &req.n, 0, 0, &req.n) < 0)
393 return -2;
394 if (process_msg(NULL, &req.n, stdout) < 0) {
395 fprintf(stderr, "Dump terminated\n");
396 exit(1);
397 }
398 } else {
399 req.n.nlmsg_seq = grth.dump = ++grth.seq;
400 if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
401 perror("Failed to send dump request");
402 exit(1);
403 }
404
405 if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
406 fprintf(stderr, "Dump terminated\n");
407 exit(1);
408 }
409 }
410 return 0;
411}
412
413int do_tcp_metrics(int argc, char **argv)
414{
415 int i;
416
417 if (argc < 1)
418 return tcpm_do_cmd(CMD_LIST, 0, NULL);
419 for (i = 0; i < ARRAY_SIZE(cmds); i++) {
420 if (matches(argv[0], cmds[i].name) == 0)
421 return tcpm_do_cmd(cmds[i].code, argc-1, argv+1);
422 }
423 if (matches(argv[0], "help") == 0)
424 usage();
425
426 fprintf(stderr, "Command \"%s\" is unknown, "
427 "try \"ip tcp_metrics help\".\n", *argv);
428 exit(-1);
429}
430