]> git.proxmox.com Git - mirror_iproute2.git/blame - misc/rtacct.c
replace SNAPSHOT with auto-generated version string
[mirror_iproute2.git] / misc / rtacct.c
CommitLineData
aba5acdf
SH
1/*
2 * rtacct.c Applet to display contents of /proc/net/rt_acct.
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 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <string.h>
18#include <errno.h>
19#include <time.h>
20#include <sys/time.h>
aba5acdf
SH
21#include <sys/file.h>
22#include <sys/socket.h>
23#include <sys/un.h>
24#include <sys/poll.h>
25#include <sys/wait.h>
26#include <sys/stat.h>
27#include <sys/mman.h>
28#include <signal.h>
29#include <math.h>
30
31#include "rt_names.h"
32
fbef6555 33#include "version.h"
aba5acdf 34
acd1e437
SH
35int reset_history;
36int ignore_history;
37int no_output;
38int no_update;
39int scan_interval;
40int time_constant;
41int dump_zeros;
42unsigned long magic_number;
aba5acdf
SH
43double W;
44
4ffc44ca 45static int generic_proc_open(const char *env, const char *name)
aba5acdf
SH
46{
47 char store[1024];
48 char *p = getenv(env);
acd1e437 49
aba5acdf
SH
50 if (!p) {
51 p = getenv("PROC_ROOT") ? : "/proc";
52 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
53 p = store;
54 }
4ffc44ca 55 return open(p, O_RDONLY);
aba5acdf
SH
56}
57
d1f28cf1 58static int net_rtacct_open(void)
aba5acdf
SH
59{
60 return generic_proc_open("PROC_NET_RTACCT", "net/rt_acct");
61}
62
d1f28cf1 63static __u32 rmap[256/4];
aba5acdf 64
acd1e437 65struct rtacct_data {
aba5acdf
SH
66 __u32 ival[256*4];
67
68 unsigned long long val[256*4];
69 double rate[256*4];
737f15f6 70 char signature[128];
aba5acdf
SH
71};
72
d1f28cf1 73static struct rtacct_data kern_db_static;
aba5acdf 74
d1f28cf1
SH
75static struct rtacct_data *kern_db = &kern_db_static;
76static struct rtacct_data *hist_db;
aba5acdf 77
d1f28cf1 78static void nread(int fd, char *buf, int tot)
aba5acdf
SH
79{
80 int count = 0;
81
82 while (count < tot) {
83 int n = read(fd, buf+count, tot-count);
acd1e437 84
aba5acdf
SH
85 if (n < 0) {
86 if (errno == EINTR)
87 continue;
88 exit(-1);
89 }
90 if (n == 0)
91 exit(-1);
92 count += n;
93 }
94}
95
d1f28cf1 96static __u32 *read_kern_table(__u32 *tbl)
aba5acdf
SH
97{
98 static __u32 *tbl_ptr;
99 int fd;
100
101 if (magic_number) {
102 if (tbl_ptr != NULL)
103 return tbl_ptr;
104
105 fd = open("/dev/mem", O_RDONLY);
106 if (fd < 0) {
107 perror("magic open");
108 exit(-1);
109 }
110 tbl_ptr = mmap(NULL, 4096,
111 PROT_READ,
112 MAP_SHARED,
113 fd, magic_number);
114 if ((unsigned long)tbl_ptr == ~0UL) {
115 perror("magic mmap");
116 exit(-1);
117 }
118 close(fd);
119 return tbl_ptr;
120 }
121
122 fd = net_rtacct_open();
123 if (fd >= 0) {
acd1e437 124 nread(fd, (char *)tbl, 256*16);
aba5acdf
SH
125 close(fd);
126 } else {
127 memset(tbl, 0, 256*16);
128 }
129 return tbl;
130}
131
d1f28cf1 132static void format_rate(FILE *fp, double rate)
aba5acdf
SH
133{
134 char temp[64];
135
136 if (rate > 1024*1024) {
acd1e437 137 sprintf(temp, "%uM", (unsigned int)rint(rate/(1024*1024)));
aba5acdf
SH
138 fprintf(fp, " %-10s", temp);
139 } else if (rate > 1024) {
acd1e437 140 sprintf(temp, "%uK", (unsigned int)rint(rate/1024));
aba5acdf
SH
141 fprintf(fp, " %-10s", temp);
142 } else
acd1e437 143 fprintf(fp, " %-10u", (unsigned int)rate);
aba5acdf
SH
144}
145
d1f28cf1 146static void format_count(FILE *fp, unsigned long long val)
aba5acdf
SH
147{
148 if (val > 1024*1024*1024)
149 fprintf(fp, " %10lluM", val/(1024*1024));
150 else if (val > 1024*1024)
151 fprintf(fp, " %10lluK", val/1024);
152 else
153 fprintf(fp, " %10llu", val);
154}
155
d1f28cf1 156static void dump_abs_db(FILE *fp)
aba5acdf
SH
157{
158 int realm;
159 char b1[16];
160
161 if (!no_output) {
162 fprintf(fp, "#%s\n", kern_db->signature);
163 fprintf(fp,
acd1e437
SH
164"%-10s %-10s "
165"%-10s %-10s "
166"%-10s \n"
ae665a52 167 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
aba5acdf 168 fprintf(fp,
acd1e437
SH
169"%-10s %-10s "
170"%-10s %-10s "
171"%-10s \n"
ae665a52 172 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
aba5acdf
SH
173
174 }
175
acd1e437 176 for (realm = 0; realm < 256; realm++) {
aba5acdf
SH
177 int i;
178 unsigned long long *val;
179 double *rate;
180
181 if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
182 continue;
183
184 val = &kern_db->val[realm*4];
185 rate = &kern_db->rate[realm*4];
186
187 if (!dump_zeros &&
188 !val[0] && !rate[0] &&
189 !val[1] && !rate[1] &&
190 !val[2] && !rate[2] &&
191 !val[3] && !rate[3])
192 continue;
193
194 if (hist_db) {
195 memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
196 }
197
198 if (no_output)
199 continue;
200
201 fprintf(fp, "%-10s", rtnl_rtrealm_n2a(realm, b1, sizeof(b1)));
202 for (i = 0; i < 4; i++)
ae665a52 203 format_count(fp, val[i]);
aba5acdf
SH
204 fprintf(fp, "\n%-10s", "");
205 for (i = 0; i < 4; i++)
ae665a52 206 format_rate(fp, rate[i]);
aba5acdf
SH
207 fprintf(fp, "\n");
208 }
209}
210
211
d1f28cf1 212static void dump_incr_db(FILE *fp)
aba5acdf
SH
213{
214 int k, realm;
215 char b1[16];
216
217 if (!no_output) {
218 fprintf(fp, "#%s\n", kern_db->signature);
219 fprintf(fp,
acd1e437
SH
220"%-10s %-10s "
221"%-10s %-10s "
222"%-10s \n"
ae665a52 223 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
aba5acdf 224 fprintf(fp,
acd1e437
SH
225"%-10s %-10s "
226"%-10s %-10s "
227"%-10s \n"
ae665a52 228 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
aba5acdf
SH
229 }
230
acd1e437 231 for (realm = 0; realm < 256; realm++) {
aba5acdf
SH
232 int ovfl = 0;
233 int i;
234 unsigned long long *val;
235 double *rate;
236 unsigned long long rval[4];
237
238 if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
239 continue;
240
241 val = &kern_db->val[realm*4];
242 rate = &kern_db->rate[realm*4];
243
acd1e437 244 for (k = 0; k < 4; k++) {
aba5acdf
SH
245 rval[k] = val[k];
246 if (rval[k] < hist_db->val[realm*4+k])
247 ovfl = 1;
248 else
249 rval[k] -= hist_db->val[realm*4+k];
250 }
251 if (ovfl) {
acd1e437 252 for (k = 0; k < 4; k++)
aba5acdf
SH
253 rval[k] = val[k];
254 }
255 if (hist_db) {
256 memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
257 }
258
259 if (no_output)
260 continue;
261
262 if (!dump_zeros &&
263 !rval[0] && !rate[0] &&
264 !rval[1] && !rate[1] &&
265 !rval[2] && !rate[2] &&
266 !rval[3] && !rate[3])
267 continue;
268
269
270 fprintf(fp, "%-10s", rtnl_rtrealm_n2a(realm, b1, sizeof(b1)));
271 for (i = 0; i < 4; i++)
ae665a52 272 format_count(fp, rval[i]);
aba5acdf
SH
273 fprintf(fp, "\n%-10s", "");
274 for (i = 0; i < 4; i++)
ae665a52 275 format_rate(fp, rate[i]);
aba5acdf
SH
276 fprintf(fp, "\n");
277 }
278}
279
280
281static int children;
282
d1f28cf1 283static void sigchild(int signo)
aba5acdf
SH
284{
285}
286
ae665a52 287/* Server side only: read kernel data, update tables, calculate rates. */
aba5acdf 288
d1f28cf1 289static void update_db(int interval)
aba5acdf
SH
290{
291 int i;
292 __u32 *ival;
293 __u32 _ival[256*4];
294
295 ival = read_kern_table(_ival);
296
acd1e437 297 for (i = 0; i < 256*4; i++) {
aba5acdf
SH
298 double sample;
299 __u32 incr = ival[i] - kern_db->ival[i];
300
301 if (ival[i] == 0 && incr == 0 &&
302 kern_db->val[i] == 0 && kern_db->rate[i] == 0)
303 continue;
304
305 kern_db->val[i] += incr;
306 kern_db->ival[i] = ival[i];
307 sample = (double)(incr*1000)/interval;
308 if (interval >= scan_interval) {
309 kern_db->rate[i] += W*(sample-kern_db->rate[i]);
310 } else if (interval >= 1000) {
311 if (interval >= time_constant) {
312 kern_db->rate[i] = sample;
313 } else {
314 double w = W*(double)interval/scan_interval;
acd1e437 315
aba5acdf
SH
316 kern_db->rate[i] += w*(sample-kern_db->rate[i]);
317 }
318 }
319 }
320}
321
d1f28cf1 322static void send_db(int fd)
aba5acdf
SH
323{
324 int tot = 0;
325
326 while (tot < sizeof(*kern_db)) {
acd1e437
SH
327 int n = write(fd, ((char *)kern_db) + tot, sizeof(*kern_db)-tot);
328
aba5acdf
SH
329 if (n < 0) {
330 if (errno == EINTR)
331 continue;
332 return;
333 }
334 tot += n;
335 }
336}
337
338
339
acd1e437 340#define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
aba5acdf
SH
341
342
d1f28cf1 343static void pad_kern_table(struct rtacct_data *dat, __u32 *ival)
aba5acdf
SH
344{
345 int i;
acd1e437 346
aba5acdf
SH
347 memset(dat->rate, 0, sizeof(dat->rate));
348 if (dat->ival != ival)
349 memcpy(dat->ival, ival, sizeof(dat->ival));
acd1e437 350 for (i = 0; i < 256*4; i++)
aba5acdf
SH
351 dat->val[i] = ival[i];
352}
353
d1f28cf1 354static void server_loop(int fd)
aba5acdf 355{
737f15f6 356 struct timeval snaptime = { 0 };
aba5acdf 357 struct pollfd p;
acd1e437 358
aba5acdf
SH
359 p.fd = fd;
360 p.events = p.revents = POLLIN;
361
ae665a52 362 sprintf(kern_db->signature,
737f15f6 363 "%u.%lu sampling_interval=%d time_const=%d",
acd1e437 364 (unsigned int) getpid(), (unsigned long)random(),
737f15f6 365 scan_interval/1000, time_constant/1000);
aba5acdf
SH
366
367 pad_kern_table(kern_db, read_kern_table(kern_db->ival));
368
369 for (;;) {
370 int status;
371 int tdiff;
372 struct timeval now;
acd1e437 373
aba5acdf
SH
374 gettimeofday(&now, NULL);
375 tdiff = T_DIFF(now, snaptime);
376 if (tdiff >= scan_interval) {
377 update_db(tdiff);
378 snaptime = now;
379 tdiff = 0;
380 }
381 if (poll(&p, 1, tdiff + scan_interval) > 0
382 && (p.revents&POLLIN)) {
383 int clnt = accept(fd, NULL, NULL);
acd1e437 384
aba5acdf
SH
385 if (clnt >= 0) {
386 pid_t pid;
acd1e437 387
aba5acdf
SH
388 if (children >= 5) {
389 close(clnt);
390 } else if ((pid = fork()) != 0) {
acd1e437 391 if (pid > 0)
aba5acdf
SH
392 children++;
393 close(clnt);
394 } else {
395 if (tdiff > 0)
396 update_db(tdiff);
397 send_db(clnt);
398 exit(0);
399 }
400 }
401 }
402 while (children && waitpid(-1, &status, WNOHANG) > 0)
403 children--;
404 }
405}
406
d1f28cf1 407static int verify_forging(int fd)
aba5acdf
SH
408{
409 struct ucred cred;
737f15f6
SH
410 socklen_t olen = sizeof(cred);
411
acd1e437 412 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &olen) ||
aba5acdf
SH
413 olen < sizeof(cred))
414 return -1;
415 if (cred.uid == getuid() || cred.uid == 0)
416 return 0;
417 return -1;
418}
419
420static void usage(void) __attribute__((noreturn));
421
422static void usage(void)
423{
424 fprintf(stderr,
425"Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
426 );
427 exit(-1);
428}
429
430int main(int argc, char *argv[])
431{
432 char hist_name[128];
433 struct sockaddr_un sun;
434 int ch;
435 int fd;
436
437 while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) {
acd1e437 438 switch (ch) {
aba5acdf
SH
439 case 'z':
440 dump_zeros = 1;
441 break;
442 case 'r':
443 reset_history = 1;
444 break;
445 case 'a':
446 ignore_history = 1;
447 break;
448 case 's':
449 no_update = 1;
450 break;
451 case 'n':
452 no_output = 1;
453 break;
454 case 'd':
455 scan_interval = 1000*atoi(optarg);
456 break;
457 case 't':
458 if (sscanf(optarg, "%d", &time_constant) != 1 ||
459 time_constant <= 0) {
460 fprintf(stderr, "rtacct: invalid time constant divisor\n");
461 exit(-1);
462 }
463 break;
464 case 'v':
465 case 'V':
fbef6555 466 printf("rtacct utility, iproute2-%s\n", version);
aba5acdf
SH
467 exit(0);
468 case 'M':
469 /* Some secret undocumented option, nobody
470 * is expected to ask about its sense. See?
471 */
472 sscanf(optarg, "%lx", &magic_number);
473 break;
474 case 'h':
475 case '?':
476 default:
477 usage();
478 }
479 }
480
481 argc -= optind;
482 argv += optind;
483
484 if (argc) {
485 while (argc > 0) {
486 __u32 realm;
acd1e437 487
aba5acdf
SH
488 if (rtnl_rtrealm_a2n(&realm, argv[0])) {
489 fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]);
490 exit(-1);
491 }
492 rmap[realm>>5] |= (1<<(realm&0x1f));
493 argc--; argv++;
494 }
495 } else {
496 memset(rmap, ~0, sizeof(rmap));
497 /* Always suppress zeros. */
498 dump_zeros = 0;
499 }
500
501 sun.sun_family = AF_UNIX;
502 sun.sun_path[0] = 0;
503 sprintf(sun.sun_path+1, "rtacct%d", getuid());
504
505 if (scan_interval > 0) {
506 if (time_constant == 0)
507 time_constant = 60;
508 time_constant *= 1000;
509 W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
510 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
511 perror("rtacct: socket");
512 exit(-1);
513 }
acd1e437 514 if (bind(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
aba5acdf
SH
515 perror("rtacct: bind");
516 exit(-1);
517 }
518 if (listen(fd, 5) < 0) {
519 perror("rtacct: listen");
520 exit(-1);
521 }
a7a9ddbb
MF
522 if (daemon(0, 0)) {
523 perror("rtacct: daemon");
524 exit(-1);
525 }
aba5acdf
SH
526 signal(SIGPIPE, SIG_IGN);
527 signal(SIGCHLD, sigchild);
528 server_loop(fd);
529 exit(0);
530 }
531
532 if (getenv("RTACCT_HISTORY"))
8988b02e 533 snprintf(hist_name, sizeof(hist_name), "%s", getenv("RTACCT_HISTORY"));
aba5acdf
SH
534 else
535 sprintf(hist_name, "/tmp/.rtacct.u%d", getuid());
536
537 if (reset_history)
538 unlink(hist_name);
539
540 if (!ignore_history || !no_update) {
541 struct stat stb;
542
543 fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
544 if (fd < 0) {
545 perror("rtacct: open history file");
546 exit(-1);
547 }
548 if (flock(fd, LOCK_EX)) {
549 perror("rtacct: flock history file");
550 exit(-1);
551 }
552 if (fstat(fd, &stb) != 0) {
553 perror("rtacct: fstat history file");
554 exit(-1);
555 }
556 if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
557 fprintf(stderr, "rtacct: something is so wrong with history file, that I prefer not to proceed.\n");
558 exit(-1);
559 }
560 if (stb.st_size != sizeof(*hist_db))
8988b02e
SH
561 if (write(fd, kern_db, sizeof(*hist_db)) < 0) {
562 perror("rtacct: write history file");
563 exit(-1);
564 }
aba5acdf
SH
565
566 hist_db = mmap(NULL, sizeof(*hist_db),
567 PROT_READ|PROT_WRITE,
568 no_update ? MAP_PRIVATE : MAP_SHARED,
569 fd, 0);
570
571 if ((unsigned long)hist_db == ~0UL) {
572 perror("mmap");
573 exit(-1);
574 }
575
576 if (!ignore_history) {
577 FILE *tfp;
9a230771 578 long uptime = -1;
acd1e437 579
aba5acdf
SH
580 if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
581 if (fscanf(tfp, "%ld", &uptime) != 1)
582 uptime = -1;
583 fclose(tfp);
584 }
585
586 if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
587 fprintf(stderr, "rtacct: history is aged out, resetting\n");
588 memset(hist_db, 0, sizeof(*hist_db));
589 }
590 }
591
592 close(fd);
593 }
594
595 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
acd1e437 596 (connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0
aba5acdf 597 || (strcpy(sun.sun_path+1, "rtacct0"),
acd1e437 598 connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
aba5acdf 599 && verify_forging(fd) == 0) {
acd1e437 600 nread(fd, (char *)kern_db, sizeof(*kern_db));
aba5acdf
SH
601 if (hist_db && hist_db->signature[0] &&
602 strcmp(kern_db->signature, hist_db->signature)) {
603 fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
604 hist_db = NULL;
605 }
606 close(fd);
607 } else {
608 if (fd >= 0)
609 close(fd);
610
611 if (hist_db && hist_db->signature[0] &&
612 strcmp(hist_db->signature, "kernel")) {
613 fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
614 hist_db = NULL;
615 }
616
617 pad_kern_table(kern_db, read_kern_table(kern_db->ival));
618 strcpy(kern_db->signature, "kernel");
619 }
620
621 if (ignore_history || hist_db == NULL)
622 dump_abs_db(stdout);
623 else
624 dump_incr_db(stdout);
625
626 exit(0);
627}