2 * rtacct.c Applet to display contents of /proc/net/rt_acct.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
23 #include <sys/socket.h>
36 int reset_history = 0;
37 int ignore_history = 0;
40 int scan_interval = 0;
41 int time_constant = 0;
43 unsigned long magic_number = 0;
46 static int generic_proc_open(const char *env, const char *name)
49 char *p = getenv(env);
51 p = getenv("PROC_ROOT") ? : "/proc";
52 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
55 return open(p, O_RDONLY);
58 static int net_rtacct_open(void)
60 return generic_proc_open("PROC_NET_RTACCT", "net/rt_acct");
63 static __u32 rmap[256/4];
69 unsigned long long val[256*4];
74 static struct rtacct_data kern_db_static;
76 static struct rtacct_data *kern_db = &kern_db_static;
77 static struct rtacct_data *hist_db;
79 static void nread(int fd, char *buf, int tot)
84 int n = read(fd, buf+count, tot-count);
96 static __u32 *read_kern_table(__u32 *tbl)
98 static __u32 *tbl_ptr;
105 fd = open("/dev/mem", O_RDONLY);
107 perror("magic open");
110 tbl_ptr = mmap(NULL, 4096,
114 if ((unsigned long)tbl_ptr == ~0UL) {
115 perror("magic mmap");
122 fd = net_rtacct_open();
124 nread(fd, (char*)tbl, 256*16);
127 memset(tbl, 0, 256*16);
132 static void format_rate(FILE *fp, double rate)
136 if (rate > 1024*1024) {
137 sprintf(temp, "%uM", (unsigned)rint(rate/(1024*1024)));
138 fprintf(fp, " %-10s", temp);
139 } else if (rate > 1024) {
140 sprintf(temp, "%uK", (unsigned)rint(rate/1024));
141 fprintf(fp, " %-10s", temp);
143 fprintf(fp, " %-10u", (unsigned)rate);
146 static void format_count(FILE *fp, unsigned long long val)
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);
153 fprintf(fp, " %10llu", val);
156 static void dump_abs_db(FILE *fp)
162 fprintf(fp, "#%s\n", kern_db->signature);
170 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
178 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
182 for (realm=0; realm<256; realm++) {
184 unsigned long long *val;
187 if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
190 val = &kern_db->val[realm*4];
191 rate = &kern_db->rate[realm*4];
194 !val[0] && !rate[0] &&
195 !val[1] && !rate[1] &&
196 !val[2] && !rate[2] &&
201 memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
207 fprintf(fp, "%-10s", rtnl_rtrealm_n2a(realm, b1, sizeof(b1)));
208 for (i = 0; i < 4; i++)
209 format_count(fp, val[i]);
210 fprintf(fp, "\n%-10s", "");
211 for (i = 0; i < 4; i++)
212 format_rate(fp, rate[i]);
218 static void dump_incr_db(FILE *fp)
224 fprintf(fp, "#%s\n", kern_db->signature);
232 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
240 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
243 for (realm=0; realm<256; realm++) {
246 unsigned long long *val;
248 unsigned long long rval[4];
250 if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
253 val = &kern_db->val[realm*4];
254 rate = &kern_db->rate[realm*4];
256 for (k=0; k<4; k++) {
258 if (rval[k] < hist_db->val[realm*4+k])
261 rval[k] -= hist_db->val[realm*4+k];
268 memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
275 !rval[0] && !rate[0] &&
276 !rval[1] && !rate[1] &&
277 !rval[2] && !rate[2] &&
278 !rval[3] && !rate[3])
282 fprintf(fp, "%-10s", rtnl_rtrealm_n2a(realm, b1, sizeof(b1)));
283 for (i = 0; i < 4; i++)
284 format_count(fp, rval[i]);
285 fprintf(fp, "\n%-10s", "");
286 for (i = 0; i < 4; i++)
287 format_rate(fp, rate[i]);
295 static void sigchild(int signo)
299 /* Server side only: read kernel data, update tables, calculate rates. */
301 static void update_db(int interval)
307 ival = read_kern_table(_ival);
309 for (i=0; i<256*4; i++) {
311 __u32 incr = ival[i] - kern_db->ival[i];
313 if (ival[i] == 0 && incr == 0 &&
314 kern_db->val[i] == 0 && kern_db->rate[i] == 0)
317 kern_db->val[i] += incr;
318 kern_db->ival[i] = ival[i];
319 sample = (double)(incr*1000)/interval;
320 if (interval >= scan_interval) {
321 kern_db->rate[i] += W*(sample-kern_db->rate[i]);
322 } else if (interval >= 1000) {
323 if (interval >= time_constant) {
324 kern_db->rate[i] = sample;
326 double w = W*(double)interval/scan_interval;
327 kern_db->rate[i] += w*(sample-kern_db->rate[i]);
333 static void send_db(int fd)
337 while (tot < sizeof(*kern_db)) {
338 int n = write(fd, ((char*)kern_db) + tot, sizeof(*kern_db)-tot);
350 #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
353 static void pad_kern_table(struct rtacct_data *dat, __u32 *ival)
356 memset(dat->rate, 0, sizeof(dat->rate));
357 if (dat->ival != ival)
358 memcpy(dat->ival, ival, sizeof(dat->ival));
359 for (i=0; i<256*4; i++)
360 dat->val[i] = ival[i];
363 static void server_loop(int fd)
365 struct timeval snaptime = { 0 };
368 p.events = p.revents = POLLIN;
370 sprintf(kern_db->signature,
371 "%u.%lu sampling_interval=%d time_const=%d",
372 (unsigned) getpid(), (unsigned long)random(),
373 scan_interval/1000, time_constant/1000);
375 pad_kern_table(kern_db, read_kern_table(kern_db->ival));
381 gettimeofday(&now, NULL);
382 tdiff = T_DIFF(now, snaptime);
383 if (tdiff >= scan_interval) {
388 if (poll(&p, 1, tdiff + scan_interval) > 0
389 && (p.revents&POLLIN)) {
390 int clnt = accept(fd, NULL, NULL);
395 } else if ((pid = fork()) != 0) {
407 while (children && waitpid(-1, &status, WNOHANG) > 0)
412 static int verify_forging(int fd)
415 socklen_t olen = sizeof(cred);
417 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
420 if (cred.uid == getuid() || cred.uid == 0)
425 static void usage(void) __attribute__((noreturn));
427 static void usage(void)
430 "Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
435 int main(int argc, char *argv[])
438 struct sockaddr_un sun;
442 while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) {
460 scan_interval = 1000*atoi(optarg);
463 if (sscanf(optarg, "%d", &time_constant) != 1 ||
464 time_constant <= 0) {
465 fprintf(stderr, "rtacct: invalid time constant divisor\n");
471 printf("rtacct utility, iproute2-ss%s\n", SNAPSHOT);
474 /* Some secret undocumented option, nobody
475 * is expected to ask about its sense. See?
477 sscanf(optarg, "%lx", &magic_number);
492 if (rtnl_rtrealm_a2n(&realm, argv[0])) {
493 fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]);
496 rmap[realm>>5] |= (1<<(realm&0x1f));
500 memset(rmap, ~0, sizeof(rmap));
501 /* Always suppress zeros. */
505 sun.sun_family = AF_UNIX;
507 sprintf(sun.sun_path+1, "rtacct%d", getuid());
509 if (scan_interval > 0) {
510 if (time_constant == 0)
512 time_constant *= 1000;
513 W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
514 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
515 perror("rtacct: socket");
518 if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
519 perror("rtacct: bind");
522 if (listen(fd, 5) < 0) {
523 perror("rtacct: listen");
527 perror("rtacct: daemon");
530 signal(SIGPIPE, SIG_IGN);
531 signal(SIGCHLD, sigchild);
536 if (getenv("RTACCT_HISTORY"))
537 snprintf(hist_name, sizeof(hist_name), "%s", getenv("RTACCT_HISTORY"));
539 sprintf(hist_name, "/tmp/.rtacct.u%d", getuid());
544 if (!ignore_history || !no_update) {
547 fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
549 perror("rtacct: open history file");
552 if (flock(fd, LOCK_EX)) {
553 perror("rtacct: flock history file");
556 if (fstat(fd, &stb) != 0) {
557 perror("rtacct: fstat history file");
560 if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
561 fprintf(stderr, "rtacct: something is so wrong with history file, that I prefer not to proceed.\n");
564 if (stb.st_size != sizeof(*hist_db))
565 if (write(fd, kern_db, sizeof(*hist_db)) < 0) {
566 perror("rtacct: write history file");
570 hist_db = mmap(NULL, sizeof(*hist_db),
571 PROT_READ|PROT_WRITE,
572 no_update ? MAP_PRIVATE : MAP_SHARED,
575 if ((unsigned long)hist_db == ~0UL) {
580 if (!ignore_history) {
583 if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
584 if (fscanf(tfp, "%ld", &uptime) != 1)
589 if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
590 fprintf(stderr, "rtacct: history is aged out, resetting\n");
591 memset(hist_db, 0, sizeof(*hist_db));
598 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
599 (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
600 || (strcpy(sun.sun_path+1, "rtacct0"),
601 connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
602 && verify_forging(fd) == 0) {
603 nread(fd, (char*)kern_db, sizeof(*kern_db));
604 if (hist_db && hist_db->signature[0] &&
605 strcmp(kern_db->signature, hist_db->signature)) {
606 fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
614 if (hist_db && hist_db->signature[0] &&
615 strcmp(hist_db->signature, "kernel")) {
616 fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
620 pad_kern_table(kern_db, read_kern_table(kern_db->ival));
621 strcpy(kern_db->signature, "kernel");
624 if (ignore_history || hist_db == NULL)
627 dump_incr_db(stdout);