| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* ifstat.c handy utility to read net interface statistics |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <string.h> |
| 13 |
#include <errno.h> |
| 14 |
#include <time.h> |
| 15 |
#include <sys/time.h> |
| 16 |
#include <fnmatch.h> |
| 17 |
#include <sys/file.h> |
| 18 |
#include <sys/socket.h> |
| 19 |
#include <sys/un.h> |
| 20 |
#include <poll.h> |
| 21 |
#include <sys/wait.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <signal.h> |
| 24 |
#include <math.h> |
| 25 |
#include <getopt.h> |
| 26 |
|
| 27 |
#include <linux/if.h> |
| 28 |
#include <linux/if_link.h> |
| 29 |
|
| 30 |
#include "libnetlink.h" |
| 31 |
#include "json_print.h" |
| 32 |
#include "version.h" |
| 33 |
#include "utils.h" |
| 34 |
|
| 35 |
int dump_zeros; |
| 36 |
int reset_history; |
| 37 |
int ignore_history; |
| 38 |
int no_output; |
| 39 |
int json_output; |
| 40 |
int no_update; |
| 41 |
int scan_interval; |
| 42 |
int time_constant; |
| 43 |
int show_errors; |
| 44 |
double W; |
| 45 |
char **patterns; |
| 46 |
int npatterns; |
| 47 |
bool is_extended; |
| 48 |
int filter_type; |
| 49 |
int sub_type; |
| 50 |
|
| 51 |
char info_source[128]; |
| 52 |
int source_mismatch; |
| 53 |
|
| 54 |
#define MAXS (sizeof(struct rtnl_link_stats64)/sizeof(__u64)) |
| 55 |
#define NO_SUB_TYPE 0xffff |
| 56 |
|
| 57 |
struct ifstat_ent { |
| 58 |
struct ifstat_ent *next; |
| 59 |
char *name; |
| 60 |
int ifindex; |
| 61 |
unsigned long long val[MAXS]; |
| 62 |
double rate[MAXS]; |
| 63 |
__u64 ival[MAXS]; |
| 64 |
}; |
| 65 |
|
| 66 |
static const char *stats[MAXS] = { |
| 67 |
"rx_packets", |
| 68 |
"tx_packets", |
| 69 |
"rx_bytes", |
| 70 |
"tx_bytes", |
| 71 |
"rx_errors", |
| 72 |
"tx_errors", |
| 73 |
"rx_dropped", |
| 74 |
"tx_dropped", |
| 75 |
"multicast", |
| 76 |
"collisions", |
| 77 |
|
| 78 |
"rx_length_errors", |
| 79 |
"rx_over_errors", |
| 80 |
"rx_crc_errors", |
| 81 |
"rx_frame_errors", |
| 82 |
"rx_fifo_errors", |
| 83 |
"rx_missed_errors", |
| 84 |
|
| 85 |
"tx_aborted_errors", |
| 86 |
"tx_carrier_errors", |
| 87 |
"tx_fifo_errors", |
| 88 |
"tx_heartbeat_errors", |
| 89 |
"tx_window_errors", |
| 90 |
|
| 91 |
"rx_compressed", |
| 92 |
"tx_compressed", |
| 93 |
"rx_nohandler", |
| 94 |
|
| 95 |
"rx_otherhost_dropped", |
| 96 |
}; |
| 97 |
|
| 98 |
struct ifstat_ent *kern_db; |
| 99 |
struct ifstat_ent *hist_db; |
| 100 |
|
| 101 |
static int match(const char *id) |
| 102 |
{ |
| 103 |
int i; |
| 104 |
|
| 105 |
if (npatterns == 0) |
| 106 |
return 1; |
| 107 |
|
| 108 |
for (i = 0; i < npatterns; i++) { |
| 109 |
if (!fnmatch(patterns[i], id, FNM_CASEFOLD)) |
| 110 |
return 1; |
| 111 |
} |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
static int get_nlmsg_extended(struct nlmsghdr *m, void *arg) |
| 116 |
{ |
| 117 |
struct if_stats_msg *ifsm = NLMSG_DATA(m); |
| 118 |
struct rtattr *tb[IFLA_STATS_MAX+1]; |
| 119 |
int len = m->nlmsg_len; |
| 120 |
struct ifstat_ent *n; |
| 121 |
|
| 122 |
if (m->nlmsg_type != RTM_NEWSTATS) |
| 123 |
return 0; |
| 124 |
|
| 125 |
len -= NLMSG_LENGTH(sizeof(*ifsm)); |
| 126 |
if (len < 0) { |
| 127 |
errno = EINVAL; |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
parse_rtattr(tb, IFLA_STATS_MAX, IFLA_STATS_RTA(ifsm), len); |
| 132 |
if (tb[filter_type] == NULL) |
| 133 |
return 0; |
| 134 |
|
| 135 |
n = malloc(sizeof(*n)); |
| 136 |
if (!n) { |
| 137 |
errno = ENOMEM; |
| 138 |
return -1; |
| 139 |
} |
| 140 |
|
| 141 |
n->ifindex = ifsm->ifindex; |
| 142 |
n->name = strdup(ll_index_to_name(ifsm->ifindex)); |
| 143 |
if (!n->name) { |
| 144 |
free(n); |
| 145 |
return -1; |
| 146 |
} |
| 147 |
|
| 148 |
if (sub_type == NO_SUB_TYPE) { |
| 149 |
memcpy(&n->val, RTA_DATA(tb[filter_type]), sizeof(n->val)); |
| 150 |
} else { |
| 151 |
struct rtattr *attr; |
| 152 |
|
| 153 |
attr = parse_rtattr_one_nested(sub_type, tb[filter_type]); |
| 154 |
if (attr == NULL) { |
| 155 |
free(n->name); |
| 156 |
free(n); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
memcpy(&n->val, RTA_DATA(attr), sizeof(n->val)); |
| 160 |
} |
| 161 |
memset(&n->rate, 0, sizeof(n->rate)); |
| 162 |
n->next = kern_db; |
| 163 |
kern_db = n; |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
static int get_nlmsg(struct nlmsghdr *m, void *arg) |
| 168 |
{ |
| 169 |
struct ifinfomsg *ifi = NLMSG_DATA(m); |
| 170 |
struct rtattr *tb[IFLA_MAX+1]; |
| 171 |
int len = m->nlmsg_len; |
| 172 |
struct ifstat_ent *n; |
| 173 |
int i; |
| 174 |
|
| 175 |
if (m->nlmsg_type != RTM_NEWLINK) |
| 176 |
return 0; |
| 177 |
|
| 178 |
len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 179 |
if (len < 0) { |
| 180 |
errno = EINVAL; |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
if (!(ifi->ifi_flags&IFF_UP)) |
| 185 |
return 0; |
| 186 |
|
| 187 |
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 188 |
if (tb[IFLA_IFNAME] == NULL) |
| 189 |
return 0; |
| 190 |
|
| 191 |
n = malloc(sizeof(*n)); |
| 192 |
if (!n) { |
| 193 |
errno = ENOMEM; |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
n->ifindex = ifi->ifi_index; |
| 198 |
n->name = strdup(RTA_DATA(tb[IFLA_IFNAME])); |
| 199 |
if (!n->name) { |
| 200 |
free(n); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
|
| 204 |
memset(&n->rate, 0, sizeof(n->rate)); |
| 205 |
|
| 206 |
if (tb[IFLA_STATS64]) { |
| 207 |
memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS64]), sizeof(n->ival)); |
| 208 |
} else if (tb[IFLA_STATS]) { |
| 209 |
__u32 *stats = RTA_DATA(tb[IFLA_STATS]); |
| 210 |
|
| 211 |
/* expand 32 bit values to 64 bit */ |
| 212 |
for (i = 0; i < MAXS; i++) |
| 213 |
n->ival[i] = stats[i]; |
| 214 |
} else { |
| 215 |
/* missing stats? */ |
| 216 |
free(n); |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
for (i = 0; i < MAXS; i++) |
| 221 |
n->val[i] = n->ival[i]; |
| 222 |
n->next = kern_db; |
| 223 |
kern_db = n; |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
static void load_info(void) |
| 228 |
{ |
| 229 |
struct ifstat_ent *db, *n; |
| 230 |
struct rtnl_handle rth; |
| 231 |
__u32 filter_mask; |
| 232 |
|
| 233 |
if (rtnl_open(&rth, 0) < 0) |
| 234 |
exit(1); |
| 235 |
|
| 236 |
if (is_extended) { |
| 237 |
ll_init_map(&rth); |
| 238 |
filter_mask = IFLA_STATS_FILTER_BIT(filter_type); |
| 239 |
if (rtnl_statsdump_req_filter(&rth, AF_UNSPEC, |
| 240 |
filter_mask, NULL, NULL) < 0) { |
| 241 |
perror("Cannot send dump request"); |
| 242 |
exit(1); |
| 243 |
} |
| 244 |
|
| 245 |
if (rtnl_dump_filter(&rth, get_nlmsg_extended, NULL) < 0) { |
| 246 |
perror("Dump terminated\n"); |
| 247 |
exit(1); |
| 248 |
} |
| 249 |
} else { |
| 250 |
if (rtnl_linkdump_req(&rth, AF_INET) < 0) { |
| 251 |
perror("Cannot send dump request"); |
| 252 |
exit(1); |
| 253 |
} |
| 254 |
|
| 255 |
if (rtnl_dump_filter(&rth, get_nlmsg, NULL) < 0) { |
| 256 |
perror("Dump terminated\n"); |
| 257 |
exit(1); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
rtnl_close(&rth); |
| 262 |
|
| 263 |
db = kern_db; |
| 264 |
kern_db = NULL; |
| 265 |
|
| 266 |
while (db) { |
| 267 |
n = db; |
| 268 |
db = db->next; |
| 269 |
n->next = kern_db; |
| 270 |
kern_db = n; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
static void load_raw_table(FILE *fp) |
| 275 |
{ |
| 276 |
char buf[4096]; |
| 277 |
struct ifstat_ent *db = NULL; |
| 278 |
struct ifstat_ent *n; |
| 279 |
|
| 280 |
while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 281 |
char *p; |
| 282 |
char *next; |
| 283 |
int i; |
| 284 |
|
| 285 |
if (buf[0] == '#') { |
| 286 |
buf[strlen(buf)-1] = 0; |
| 287 |
if (info_source[0] && strcmp(info_source, buf+1)) |
| 288 |
source_mismatch = 1; |
| 289 |
strlcpy(info_source, buf+1, sizeof(info_source)); |
| 290 |
continue; |
| 291 |
} |
| 292 |
if ((n = malloc(sizeof(*n))) == NULL) |
| 293 |
abort(); |
| 294 |
|
| 295 |
if (!(p = strchr(buf, ' '))) |
| 296 |
abort(); |
| 297 |
*p++ = 0; |
| 298 |
|
| 299 |
if (sscanf(buf, "%d", &n->ifindex) != 1) |
| 300 |
abort(); |
| 301 |
if (!(next = strchr(p, ' '))) |
| 302 |
abort(); |
| 303 |
*next++ = 0; |
| 304 |
|
| 305 |
n->name = strdup(p); |
| 306 |
p = next; |
| 307 |
|
| 308 |
for (i = 0; i < MAXS; i++) { |
| 309 |
unsigned int rate; |
| 310 |
|
| 311 |
if (!(next = strchr(p, ' '))) |
| 312 |
abort(); |
| 313 |
*next++ = 0; |
| 314 |
if (sscanf(p, "%llu", n->val+i) != 1) |
| 315 |
abort(); |
| 316 |
n->ival[i] = (__u32)n->val[i]; |
| 317 |
p = next; |
| 318 |
if (!(next = strchr(p, ' '))) |
| 319 |
abort(); |
| 320 |
*next++ = 0; |
| 321 |
if (sscanf(p, "%u", &rate) != 1) |
| 322 |
abort(); |
| 323 |
n->rate[i] = rate; |
| 324 |
p = next; |
| 325 |
} |
| 326 |
n->next = db; |
| 327 |
db = n; |
| 328 |
} |
| 329 |
|
| 330 |
while (db) { |
| 331 |
n = db; |
| 332 |
db = db->next; |
| 333 |
n->next = kern_db; |
| 334 |
kern_db = n; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
static void dump_raw_db(FILE *fp, int to_hist) |
| 339 |
{ |
| 340 |
struct ifstat_ent *n, *h; |
| 341 |
|
| 342 |
h = hist_db; |
| 343 |
new_json_obj_plain(json_output); |
| 344 |
if (is_json_context()) { |
| 345 |
open_json_object(NULL); |
| 346 |
open_json_object(info_source); |
| 347 |
} else |
| 348 |
fprintf(fp, "#%s\n", info_source); |
| 349 |
|
| 350 |
for (n = kern_db; n; n = n->next) { |
| 351 |
int i; |
| 352 |
unsigned long long *vals = n->val; |
| 353 |
double *rates = n->rate; |
| 354 |
|
| 355 |
if (!match(n->name)) { |
| 356 |
struct ifstat_ent *h1; |
| 357 |
|
| 358 |
if (!to_hist) |
| 359 |
continue; |
| 360 |
for (h1 = h; h1; h1 = h1->next) { |
| 361 |
if (h1->ifindex == n->ifindex) { |
| 362 |
vals = h1->val; |
| 363 |
rates = h1->rate; |
| 364 |
h = h1->next; |
| 365 |
break; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
if (is_json_context()) { |
| 371 |
open_json_object(n->name); |
| 372 |
|
| 373 |
for (i = 0; i < MAXS && stats[i]; i++) |
| 374 |
print_lluint(PRINT_JSON, stats[i], NULL, vals[i]); |
| 375 |
close_json_object(); |
| 376 |
} else { |
| 377 |
fprintf(fp, "%d %s ", n->ifindex, n->name); |
| 378 |
for (i = 0; i < MAXS; i++) |
| 379 |
fprintf(fp, "%llu %u ", vals[i], |
| 380 |
(unsigned int)rates[i]); |
| 381 |
fprintf(fp, "\n"); |
| 382 |
} |
| 383 |
} |
| 384 |
if (is_json_context()) { |
| 385 |
close_json_object(); |
| 386 |
close_json_object(); |
| 387 |
} |
| 388 |
delete_json_obj_plain(); |
| 389 |
} |
| 390 |
|
| 391 |
/* use communication definitions of meg/kilo etc */ |
| 392 |
static const unsigned long long giga = 1000000000ull; |
| 393 |
static const unsigned long long mega = 1000000; |
| 394 |
static const unsigned long long kilo = 1000; |
| 395 |
|
| 396 |
static void format_rate(FILE *fp, const unsigned long long *vals, |
| 397 |
const double *rates, int i) |
| 398 |
{ |
| 399 |
char temp[64]; |
| 400 |
|
| 401 |
if (vals[i] > giga) |
| 402 |
fprintf(fp, "%7lluM ", vals[i]/mega); |
| 403 |
else if (vals[i] > mega) |
| 404 |
fprintf(fp, "%7lluK ", vals[i]/kilo); |
| 405 |
else |
| 406 |
fprintf(fp, "%8llu ", vals[i]); |
| 407 |
|
| 408 |
if (rates[i] > mega) { |
| 409 |
snprintf(temp, sizeof(temp), "%uM", (unsigned int)(rates[i]/mega)); |
| 410 |
fprintf(fp, "%-6s ", temp); |
| 411 |
} else if (rates[i] > kilo) { |
| 412 |
snprintf(temp, sizeof(temp), "%uK", (unsigned int)(rates[i]/kilo)); |
| 413 |
fprintf(fp, "%-6s ", temp); |
| 414 |
} else |
| 415 |
fprintf(fp, "%-6u ", (unsigned int)rates[i]); |
| 416 |
} |
| 417 |
|
| 418 |
static void format_pair(FILE *fp, const unsigned long long *vals, int i, int k) |
| 419 |
{ |
| 420 |
char temp[64]; |
| 421 |
|
| 422 |
if (vals[i] > giga) |
| 423 |
fprintf(fp, "%7lluM ", vals[i]/mega); |
| 424 |
else if (vals[i] > mega) |
| 425 |
fprintf(fp, "%7lluK ", vals[i]/kilo); |
| 426 |
else |
| 427 |
fprintf(fp, "%8llu ", vals[i]); |
| 428 |
|
| 429 |
if (vals[k] > giga) { |
| 430 |
snprintf(temp, sizeof(temp), "%uM", (unsigned int)(vals[k]/mega)); |
| 431 |
fprintf(fp, "%-6s ", temp); |
| 432 |
} else if (vals[k] > mega) { |
| 433 |
snprintf(temp, sizeof(temp), "%uK", (unsigned int)(vals[k]/kilo)); |
| 434 |
fprintf(fp, "%-6s ", temp); |
| 435 |
} else |
| 436 |
fprintf(fp, "%-6u ", (unsigned int)vals[k]); |
| 437 |
} |
| 438 |
|
| 439 |
static void print_head(FILE *fp) |
| 440 |
{ |
| 441 |
fprintf(fp, "#%s\n", info_source); |
| 442 |
fprintf(fp, "%-15s ", "Interface"); |
| 443 |
|
| 444 |
fprintf(fp, "%8s/%-6s ", "RX Pkts", "Rate"); |
| 445 |
fprintf(fp, "%8s/%-6s ", "TX Pkts", "Rate"); |
| 446 |
fprintf(fp, "%8s/%-6s ", "RX Data", "Rate"); |
| 447 |
fprintf(fp, "%8s/%-6s\n", "TX Data", "Rate"); |
| 448 |
|
| 449 |
if (!show_errors) { |
| 450 |
fprintf(fp, "%-15s ", ""); |
| 451 |
fprintf(fp, "%8s/%-6s ", "RX Errs", "Drop"); |
| 452 |
fprintf(fp, "%8s/%-6s ", "TX Errs", "Drop"); |
| 453 |
fprintf(fp, "%8s/%-6s ", "RX Over", "Rate"); |
| 454 |
fprintf(fp, "%8s/%-6s\n", "TX Coll", "Rate"); |
| 455 |
} else { |
| 456 |
fprintf(fp, "%-15s ", ""); |
| 457 |
fprintf(fp, "%8s/%-6s ", "RX Errs", "Rate"); |
| 458 |
fprintf(fp, "%8s/%-6s ", "RX Drop", "Rate"); |
| 459 |
fprintf(fp, "%8s/%-6s ", "RX Over", "Rate"); |
| 460 |
fprintf(fp, "%8s/%-6s\n", "RX Leng", "Rate"); |
| 461 |
|
| 462 |
fprintf(fp, "%-15s ", ""); |
| 463 |
fprintf(fp, "%8s/%-6s ", "RX Crc", "Rate"); |
| 464 |
fprintf(fp, "%8s/%-6s ", "RX Frm", "Rate"); |
| 465 |
fprintf(fp, "%8s/%-6s ", "RX Fifo", "Rate"); |
| 466 |
fprintf(fp, "%8s/%-6s\n", "RX Miss", "Rate"); |
| 467 |
|
| 468 |
fprintf(fp, "%-15s ", ""); |
| 469 |
fprintf(fp, "%8s/%-6s ", "TX Errs", "Rate"); |
| 470 |
fprintf(fp, "%8s/%-6s ", "TX Drop", "Rate"); |
| 471 |
fprintf(fp, "%8s/%-6s ", "TX Coll", "Rate"); |
| 472 |
fprintf(fp, "%8s/%-6s\n", "TX Carr", "Rate"); |
| 473 |
|
| 474 |
fprintf(fp, "%-15s ", ""); |
| 475 |
fprintf(fp, "%8s/%-6s ", "TX Abrt", "Rate"); |
| 476 |
fprintf(fp, "%8s/%-6s ", "TX Fifo", "Rate"); |
| 477 |
fprintf(fp, "%8s/%-6s ", "TX Hear", "Rate"); |
| 478 |
fprintf(fp, "%8s/%-6s\n", "TX Wind", "Rate"); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
static void print_one_json(const struct ifstat_ent *n, |
| 483 |
const unsigned long long *vals) |
| 484 |
{ |
| 485 |
int i, m = show_errors ? 20 : 10; |
| 486 |
|
| 487 |
open_json_object(n->name); |
| 488 |
|
| 489 |
for (i = 0; i < m && stats[i]; i++) |
| 490 |
print_lluint(PRINT_JSON, stats[i], NULL, vals[i]); |
| 491 |
|
| 492 |
close_json_object(); |
| 493 |
} |
| 494 |
|
| 495 |
static void print_one_if(FILE *fp, const struct ifstat_ent *n, |
| 496 |
const unsigned long long *vals) |
| 497 |
{ |
| 498 |
int i; |
| 499 |
|
| 500 |
fprintf(fp, "%-15s ", n->name); |
| 501 |
for (i = 0; i < 4; i++) |
| 502 |
format_rate(fp, vals, n->rate, i); |
| 503 |
fprintf(fp, "\n"); |
| 504 |
|
| 505 |
if (!show_errors) { |
| 506 |
fprintf(fp, "%-15s ", ""); |
| 507 |
format_pair(fp, vals, 4, 6); |
| 508 |
format_pair(fp, vals, 5, 7); |
| 509 |
format_rate(fp, vals, n->rate, 11); |
| 510 |
format_rate(fp, vals, n->rate, 9); |
| 511 |
fprintf(fp, "\n"); |
| 512 |
} else { |
| 513 |
fprintf(fp, "%-15s ", ""); |
| 514 |
format_rate(fp, vals, n->rate, 4); |
| 515 |
format_rate(fp, vals, n->rate, 6); |
| 516 |
format_rate(fp, vals, n->rate, 11); |
| 517 |
format_rate(fp, vals, n->rate, 10); |
| 518 |
fprintf(fp, "\n"); |
| 519 |
|
| 520 |
fprintf(fp, "%-15s ", ""); |
| 521 |
format_rate(fp, vals, n->rate, 12); |
| 522 |
format_rate(fp, vals, n->rate, 13); |
| 523 |
format_rate(fp, vals, n->rate, 14); |
| 524 |
format_rate(fp, vals, n->rate, 15); |
| 525 |
fprintf(fp, "\n"); |
| 526 |
|
| 527 |
fprintf(fp, "%-15s ", ""); |
| 528 |
format_rate(fp, vals, n->rate, 5); |
| 529 |
format_rate(fp, vals, n->rate, 7); |
| 530 |
format_rate(fp, vals, n->rate, 9); |
| 531 |
format_rate(fp, vals, n->rate, 17); |
| 532 |
fprintf(fp, "\n"); |
| 533 |
|
| 534 |
fprintf(fp, "%-15s ", ""); |
| 535 |
format_rate(fp, vals, n->rate, 16); |
| 536 |
format_rate(fp, vals, n->rate, 18); |
| 537 |
format_rate(fp, vals, n->rate, 19); |
| 538 |
format_rate(fp, vals, n->rate, 20); |
| 539 |
fprintf(fp, "\n"); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
static void dump_kern_db(FILE *fp) |
| 544 |
{ |
| 545 |
struct ifstat_ent *n; |
| 546 |
|
| 547 |
new_json_obj_plain(json_output); |
| 548 |
if (is_json_context()) { |
| 549 |
open_json_object(NULL); |
| 550 |
open_json_object(info_source); |
| 551 |
} else |
| 552 |
print_head(fp); |
| 553 |
|
| 554 |
for (n = kern_db; n; n = n->next) { |
| 555 |
if (!match(n->name)) |
| 556 |
continue; |
| 557 |
|
| 558 |
if (is_json_context()) |
| 559 |
print_one_json(n, n->val); |
| 560 |
else |
| 561 |
print_one_if(fp, n, n->val); |
| 562 |
} |
| 563 |
if (is_json_context()) { |
| 564 |
close_json_object(); |
| 565 |
close_json_object(); |
| 566 |
} |
| 567 |
delete_json_obj_plain(); |
| 568 |
} |
| 569 |
|
| 570 |
static void dump_incr_db(FILE *fp) |
| 571 |
{ |
| 572 |
struct ifstat_ent *n, *h; |
| 573 |
|
| 574 |
h = hist_db; |
| 575 |
new_json_obj_plain(json_output); |
| 576 |
if (is_json_context()) { |
| 577 |
open_json_object(NULL); |
| 578 |
open_json_object(info_source); |
| 579 |
} else |
| 580 |
print_head(fp); |
| 581 |
|
| 582 |
for (n = kern_db; n; n = n->next) { |
| 583 |
int i; |
| 584 |
unsigned long long vals[MAXS]; |
| 585 |
struct ifstat_ent *h1; |
| 586 |
|
| 587 |
memcpy(vals, n->val, sizeof(vals)); |
| 588 |
|
| 589 |
for (h1 = h; h1; h1 = h1->next) { |
| 590 |
if (h1->ifindex == n->ifindex) { |
| 591 |
for (i = 0; i < MAXS; i++) |
| 592 |
vals[i] -= h1->val[i]; |
| 593 |
h = h1->next; |
| 594 |
break; |
| 595 |
} |
| 596 |
} |
| 597 |
if (!match(n->name)) |
| 598 |
continue; |
| 599 |
|
| 600 |
if (is_json_context()) |
| 601 |
print_one_json(n, n->val); |
| 602 |
else |
| 603 |
print_one_if(fp, n, vals); |
| 604 |
} |
| 605 |
|
| 606 |
if (is_json_context()) { |
| 607 |
close_json_object(); |
| 608 |
close_json_object(); |
| 609 |
} |
| 610 |
delete_json_obj_plain(); |
| 611 |
} |
| 612 |
|
| 613 |
static int children; |
| 614 |
|
| 615 |
static void sigchild(int signo) |
| 616 |
{ |
| 617 |
} |
| 618 |
|
| 619 |
static void update_db(int interval) |
| 620 |
{ |
| 621 |
struct ifstat_ent *n, *h; |
| 622 |
|
| 623 |
n = kern_db; |
| 624 |
kern_db = NULL; |
| 625 |
|
| 626 |
load_info(); |
| 627 |
|
| 628 |
h = kern_db; |
| 629 |
kern_db = n; |
| 630 |
|
| 631 |
for (n = kern_db; n; n = n->next) { |
| 632 |
struct ifstat_ent *h1; |
| 633 |
|
| 634 |
for (h1 = h; h1; h1 = h1->next) { |
| 635 |
if (h1->ifindex == n->ifindex) { |
| 636 |
int i; |
| 637 |
|
| 638 |
for (i = 0; i < MAXS; i++) { |
| 639 |
if (h1->ival[i] < n->ival[i]) { |
| 640 |
memset(n->ival, 0, sizeof(n->ival)); |
| 641 |
break; |
| 642 |
} |
| 643 |
} |
| 644 |
for (i = 0; i < MAXS; i++) { |
| 645 |
double sample; |
| 646 |
__u64 incr; |
| 647 |
|
| 648 |
if (is_extended) { |
| 649 |
incr = h1->val[i] - n->val[i]; |
| 650 |
n->val[i] = h1->val[i]; |
| 651 |
} else { |
| 652 |
incr = (__u32) (h1->ival[i] - n->ival[i]); |
| 653 |
n->val[i] += incr; |
| 654 |
n->ival[i] = h1->ival[i]; |
| 655 |
} |
| 656 |
|
| 657 |
sample = (double)(incr*1000)/interval; |
| 658 |
if (interval >= scan_interval) { |
| 659 |
n->rate[i] += W*(sample-n->rate[i]); |
| 660 |
} else if (interval >= 1000) { |
| 661 |
if (interval >= time_constant) { |
| 662 |
n->rate[i] = sample; |
| 663 |
} else { |
| 664 |
double w = W*(double)interval/scan_interval; |
| 665 |
|
| 666 |
n->rate[i] += w*(sample-n->rate[i]); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
while (h != h1) { |
| 672 |
struct ifstat_ent *tmp = h; |
| 673 |
|
| 674 |
h = h->next; |
| 675 |
free(tmp->name); |
| 676 |
free(tmp); |
| 677 |
}; |
| 678 |
h = h1->next; |
| 679 |
free(h1->name); |
| 680 |
free(h1); |
| 681 |
break; |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
#define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000) |
| 688 |
|
| 689 |
|
| 690 |
static void server_loop(int fd) |
| 691 |
{ |
| 692 |
struct timeval snaptime = { 0 }; |
| 693 |
struct pollfd p; |
| 694 |
|
| 695 |
p.fd = fd; |
| 696 |
p.events = p.revents = POLLIN; |
| 697 |
|
| 698 |
snprintf(info_source, sizeof(info_source), "%d.%lu sampling_interval=%d time_const=%d", |
| 699 |
getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000); |
| 700 |
|
| 701 |
load_info(); |
| 702 |
|
| 703 |
for (;;) { |
| 704 |
int status; |
| 705 |
time_t tdiff; |
| 706 |
struct timeval now; |
| 707 |
|
| 708 |
gettimeofday(&now, NULL); |
| 709 |
tdiff = T_DIFF(now, snaptime); |
| 710 |
if (tdiff >= scan_interval) { |
| 711 |
update_db(tdiff); |
| 712 |
snaptime = now; |
| 713 |
tdiff = 0; |
| 714 |
} |
| 715 |
|
| 716 |
if (poll(&p, 1, scan_interval - tdiff) > 0 |
| 717 |
&& (p.revents&POLLIN)) { |
| 718 |
int clnt = accept(fd, NULL, NULL); |
| 719 |
|
| 720 |
if (clnt >= 0) { |
| 721 |
pid_t pid; |
| 722 |
|
| 723 |
if (children >= 5) { |
| 724 |
close(clnt); |
| 725 |
} else if ((pid = fork()) != 0) { |
| 726 |
if (pid > 0) |
| 727 |
children++; |
| 728 |
close(clnt); |
| 729 |
} else { |
| 730 |
FILE *fp = fdopen(clnt, "w"); |
| 731 |
|
| 732 |
if (fp) |
| 733 |
dump_raw_db(fp, 0); |
| 734 |
exit(0); |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
while (children && waitpid(-1, &status, WNOHANG) > 0) |
| 739 |
children--; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
static int verify_forging(int fd) |
| 744 |
{ |
| 745 |
struct ucred cred; |
| 746 |
socklen_t olen = sizeof(cred); |
| 747 |
|
| 748 |
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &olen) || |
| 749 |
olen < sizeof(cred)) |
| 750 |
return -1; |
| 751 |
if (cred.uid == getuid() || cred.uid == 0) |
| 752 |
return 0; |
| 753 |
return -1; |
| 754 |
} |
| 755 |
|
| 756 |
static void xstat_usage(void) |
| 757 |
{ |
| 758 |
fprintf(stderr, |
| 759 |
"Usage: ifstat supported xstats:\n" |
| 760 |
" cpu_hits Counts only packets that went via the CPU.\n"); |
| 761 |
} |
| 762 |
|
| 763 |
struct extended_stats_options_t { |
| 764 |
char *name; |
| 765 |
int id; |
| 766 |
int sub_type; |
| 767 |
}; |
| 768 |
|
| 769 |
/* Note: if one xstat name is subset of another, it should be before it in this |
| 770 |
* list. |
| 771 |
* Name length must be under 64 chars. |
| 772 |
*/ |
| 773 |
static const struct extended_stats_options_t extended_stats_options[] = { |
| 774 |
{"cpu_hits", IFLA_STATS_LINK_OFFLOAD_XSTATS, IFLA_OFFLOAD_XSTATS_CPU_HIT}, |
| 775 |
}; |
| 776 |
|
| 777 |
static const char *get_filter_type(const char *name) |
| 778 |
{ |
| 779 |
int name_len; |
| 780 |
int i; |
| 781 |
|
| 782 |
name_len = strlen(name); |
| 783 |
for (i = 0; i < ARRAY_SIZE(extended_stats_options); i++) { |
| 784 |
const struct extended_stats_options_t *xstat; |
| 785 |
|
| 786 |
xstat = &extended_stats_options[i]; |
| 787 |
if (strncmp(name, xstat->name, name_len) == 0) { |
| 788 |
filter_type = xstat->id; |
| 789 |
sub_type = xstat->sub_type; |
| 790 |
return xstat->name; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
fprintf(stderr, "invalid ifstat extension %s\n", name); |
| 795 |
xstat_usage(); |
| 796 |
return NULL; |
| 797 |
} |
| 798 |
|
| 799 |
static void usage(void) __attribute__((noreturn)); |
| 800 |
|
| 801 |
static void usage(void) |
| 802 |
{ |
| 803 |
fprintf(stderr, |
| 804 |
"Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n" |
| 805 |
" -h, --help this message\n" |
| 806 |
" -a, --ignore ignore history\n" |
| 807 |
" -d, --scan=SECS sample every statistics every SECS\n" |
| 808 |
" -e, --errors show errors\n" |
| 809 |
" -j, --json format output in JSON\n" |
| 810 |
" -n, --nooutput do history only\n" |
| 811 |
" -p, --pretty pretty print\n" |
| 812 |
" -r, --reset reset history\n" |
| 813 |
" -s, --noupdate don't update history\n" |
| 814 |
" -t, --interval=SECS report average over the last SECS\n" |
| 815 |
" -V, --version output version information\n" |
| 816 |
" -z, --zeros show entries with zero activity\n" |
| 817 |
" -x, --extended=TYPE show extended stats of TYPE\n"); |
| 818 |
|
| 819 |
exit(-1); |
| 820 |
} |
| 821 |
|
| 822 |
static const struct option longopts[] = { |
| 823 |
{ "help", 0, 0, 'h' }, |
| 824 |
{ "ignore", 0, 0, 'a' }, |
| 825 |
{ "scan", 1, 0, 'd'}, |
| 826 |
{ "errors", 0, 0, 'e' }, |
| 827 |
{ "nooutput", 0, 0, 'n' }, |
| 828 |
{ "json", 0, 0, 'j' }, |
| 829 |
{ "reset", 0, 0, 'r' }, |
| 830 |
{ "pretty", 0, 0, 'p' }, |
| 831 |
{ "noupdate", 0, 0, 's' }, |
| 832 |
{ "interval", 1, 0, 't' }, |
| 833 |
{ "version", 0, 0, 'V' }, |
| 834 |
{ "zeros", 0, 0, 'z' }, |
| 835 |
{ "extended", 1, 0, 'x'}, |
| 836 |
{ 0 } |
| 837 |
}; |
| 838 |
|
| 839 |
int main(int argc, char *argv[]) |
| 840 |
{ |
| 841 |
char hist_name[128]; |
| 842 |
struct sockaddr_un sun; |
| 843 |
FILE *hist_fp = NULL; |
| 844 |
const char *stats_type = NULL; |
| 845 |
int ch; |
| 846 |
int fd; |
| 847 |
|
| 848 |
is_extended = false; |
| 849 |
while ((ch = getopt_long(argc, argv, "hjpvVzrnasd:t:ex:", |
| 850 |
longopts, NULL)) != EOF) { |
| 851 |
switch (ch) { |
| 852 |
case 'z': |
| 853 |
dump_zeros = 1; |
| 854 |
break; |
| 855 |
case 'r': |
| 856 |
reset_history = 1; |
| 857 |
break; |
| 858 |
case 'a': |
| 859 |
ignore_history = 1; |
| 860 |
break; |
| 861 |
case 's': |
| 862 |
no_update = 1; |
| 863 |
break; |
| 864 |
case 'n': |
| 865 |
no_output = 1; |
| 866 |
break; |
| 867 |
case 'e': |
| 868 |
show_errors = 1; |
| 869 |
break; |
| 870 |
case 'j': |
| 871 |
json_output = 1; |
| 872 |
break; |
| 873 |
case 'p': |
| 874 |
pretty = 1; |
| 875 |
break; |
| 876 |
case 'd': |
| 877 |
scan_interval = atoi(optarg) * 1000; |
| 878 |
if (scan_interval <= 0) { |
| 879 |
fprintf(stderr, "ifstat: invalid scan interval\n"); |
| 880 |
exit(-1); |
| 881 |
} |
| 882 |
break; |
| 883 |
case 't': |
| 884 |
time_constant = atoi(optarg); |
| 885 |
if (time_constant <= 0) { |
| 886 |
fprintf(stderr, "ifstat: invalid time constant divisor\n"); |
| 887 |
exit(-1); |
| 888 |
} |
| 889 |
break; |
| 890 |
case 'x': |
| 891 |
stats_type = optarg; |
| 892 |
is_extended = true; |
| 893 |
break; |
| 894 |
case 'v': |
| 895 |
case 'V': |
| 896 |
printf("ifstat utility, iproute2-%s\n", version); |
| 897 |
exit(0); |
| 898 |
case 'h': |
| 899 |
case '?': |
| 900 |
default: |
| 901 |
usage(); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
argc -= optind; |
| 906 |
argv += optind; |
| 907 |
|
| 908 |
if (stats_type) { |
| 909 |
stats_type = get_filter_type(stats_type); |
| 910 |
if (!stats_type) |
| 911 |
exit(-1); |
| 912 |
} |
| 913 |
|
| 914 |
sun.sun_family = AF_UNIX; |
| 915 |
sun.sun_path[0] = 0; |
| 916 |
snprintf(sun.sun_path + 1, sizeof(sun.sun_path) - 1, "ifstat%d", getuid()); |
| 917 |
|
| 918 |
if (scan_interval > 0) { |
| 919 |
if (time_constant == 0) |
| 920 |
time_constant = 60; |
| 921 |
time_constant *= 1000; |
| 922 |
W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant); |
| 923 |
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 924 |
perror("ifstat: socket"); |
| 925 |
exit(-1); |
| 926 |
} |
| 927 |
if (bind(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) < 0) { |
| 928 |
perror("ifstat: bind"); |
| 929 |
exit(-1); |
| 930 |
} |
| 931 |
if (listen(fd, 5) < 0) { |
| 932 |
perror("ifstat: listen"); |
| 933 |
exit(-1); |
| 934 |
} |
| 935 |
if (daemon(0, 0)) { |
| 936 |
perror("ifstat: daemon"); |
| 937 |
exit(-1); |
| 938 |
} |
| 939 |
signal(SIGPIPE, SIG_IGN); |
| 940 |
signal(SIGCHLD, sigchild); |
| 941 |
server_loop(fd); |
| 942 |
exit(0); |
| 943 |
} |
| 944 |
|
| 945 |
patterns = argv; |
| 946 |
npatterns = argc; |
| 947 |
|
| 948 |
if (getenv("IFSTAT_HISTORY")) |
| 949 |
snprintf(hist_name, sizeof(hist_name), |
| 950 |
"%s", getenv("IFSTAT_HISTORY")); |
| 951 |
else |
| 952 |
if (!stats_type) |
| 953 |
snprintf(hist_name, sizeof(hist_name), |
| 954 |
"%s/.ifstat.u%d", P_tmpdir, getuid()); |
| 955 |
else |
| 956 |
snprintf(hist_name, sizeof(hist_name), |
| 957 |
"%s/.%s_ifstat.u%d", P_tmpdir, stats_type, |
| 958 |
getuid()); |
| 959 |
|
| 960 |
if (reset_history && unlink(hist_name) < 0) { |
| 961 |
perror("ifstat: unlink history file"); |
| 962 |
exit(-1); |
| 963 |
} |
| 964 |
|
| 965 |
if (!ignore_history || !no_update) { |
| 966 |
struct stat stb; |
| 967 |
|
| 968 |
fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600); |
| 969 |
if (fd < 0) { |
| 970 |
perror("ifstat: open history file"); |
| 971 |
exit(-1); |
| 972 |
} |
| 973 |
if ((hist_fp = fdopen(fd, "r+")) == NULL) { |
| 974 |
perror("ifstat: fdopen history file"); |
| 975 |
exit(-1); |
| 976 |
} |
| 977 |
if (flock(fileno(hist_fp), LOCK_EX)) { |
| 978 |
perror("ifstat: flock history file"); |
| 979 |
exit(-1); |
| 980 |
} |
| 981 |
if (fstat(fileno(hist_fp), &stb) != 0) { |
| 982 |
perror("ifstat: fstat history file"); |
| 983 |
exit(-1); |
| 984 |
} |
| 985 |
if (stb.st_nlink != 1 || stb.st_uid != getuid()) { |
| 986 |
fprintf(stderr, "ifstat: something is so wrong with history file, that I prefer not to proceed.\n"); |
| 987 |
exit(-1); |
| 988 |
} |
| 989 |
if (!ignore_history) { |
| 990 |
FILE *tfp; |
| 991 |
long uptime = -1; |
| 992 |
|
| 993 |
if ((tfp = fopen("/proc/uptime", "r")) != NULL) { |
| 994 |
if (fscanf(tfp, "%ld", &uptime) != 1) |
| 995 |
uptime = -1; |
| 996 |
fclose(tfp); |
| 997 |
} |
| 998 |
if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) { |
| 999 |
fprintf(stderr, "ifstat: history is aged out, resetting\n"); |
| 1000 |
if (ftruncate(fileno(hist_fp), 0)) |
| 1001 |
perror("ifstat: ftruncate"); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
load_raw_table(hist_fp); |
| 1006 |
|
| 1007 |
hist_db = kern_db; |
| 1008 |
kern_db = NULL; |
| 1009 |
} |
| 1010 |
|
| 1011 |
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 && |
| 1012 |
(connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0 |
| 1013 |
|| (strcpy(sun.sun_path+1, "ifstat0"), |
| 1014 |
connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0)) |
| 1015 |
&& verify_forging(fd) == 0) { |
| 1016 |
FILE *sfp = fdopen(fd, "r"); |
| 1017 |
|
| 1018 |
if (!sfp) { |
| 1019 |
fprintf(stderr, "ifstat: fdopen failed: %s\n", |
| 1020 |
strerror(errno)); |
| 1021 |
close(fd); |
| 1022 |
} else { |
| 1023 |
load_raw_table(sfp); |
| 1024 |
if (hist_db && source_mismatch) { |
| 1025 |
fprintf(stderr, "ifstat: history is stale, ignoring it.\n"); |
| 1026 |
hist_db = NULL; |
| 1027 |
} |
| 1028 |
fclose(sfp); |
| 1029 |
} |
| 1030 |
} else { |
| 1031 |
if (fd >= 0) |
| 1032 |
close(fd); |
| 1033 |
if (hist_db && info_source[0] && strcmp(info_source, "kernel")) { |
| 1034 |
fprintf(stderr, "ifstat: history is stale, ignoring it.\n"); |
| 1035 |
hist_db = NULL; |
| 1036 |
info_source[0] = 0; |
| 1037 |
} |
| 1038 |
load_info(); |
| 1039 |
if (info_source[0] == 0) |
| 1040 |
strcpy(info_source, "kernel"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (!no_output) { |
| 1044 |
if (ignore_history || hist_db == NULL) |
| 1045 |
dump_kern_db(stdout); |
| 1046 |
else |
| 1047 |
dump_incr_db(stdout); |
| 1048 |
} |
| 1049 |
|
| 1050 |
if (!no_update) { |
| 1051 |
if (ftruncate(fileno(hist_fp), 0)) |
| 1052 |
perror("ifstat: ftruncate"); |
| 1053 |
rewind(hist_fp); |
| 1054 |
|
| 1055 |
json_output = 0; |
| 1056 |
dump_raw_db(hist_fp, 1); |
| 1057 |
fclose(hist_fp); |
| 1058 |
} |
| 1059 |
exit(0); |
| 1060 |
} |