#include <time.h>
#include <errno.h>
#include <unistd.h>
+#include <signal.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <arpa/inet.h>
+#include <dlfcn.h>
#define STEP_COUNT 60
#define STEP_TIME 1
+#define TIMEOUT 10
+
+#define PID_PATH "/var/run/luci-bwc.pid"
#define DB_PATH "/var/lib/luci-bwc"
#define DB_IF_FILE DB_PATH "/if/%s"
+#define DB_RD_FILE DB_PATH "/radio/%s"
+#define DB_CN_FILE DB_PATH "/connections"
#define DB_LD_FILE DB_PATH "/load"
#define IF_SCAN_PATTERN \
- " %[^ :]:%" SCNu64 " %" SCNu64 \
+ " %[^ :]:%u %u" \
" %*d %*d %*d %*d %*d %*d" \
- " %" SCNu64 " %" SCNu64
+ " %u %u"
#define LD_SCAN_PATTERN \
"%f %f %f"
+struct file_map {
+ int fd;
+ int size;
+ char *mmap;
+};
+
struct traffic_entry {
- uint64_t time;
- uint64_t rxb;
- uint64_t rxp;
- uint64_t txb;
- uint64_t txp;
+ uint32_t time;
+ uint32_t rxb;
+ uint32_t rxp;
+ uint32_t txb;
+ uint32_t txp;
+};
+
+struct conn_entry {
+ uint32_t time;
+ uint32_t udp;
+ uint32_t tcp;
+ uint32_t other;
};
struct load_entry {
- uint64_t time;
+ uint32_t time;
uint16_t load1;
uint16_t load5;
uint16_t load15;
};
+struct radio_entry {
+ uint32_t time;
+ uint16_t rate;
+ uint8_t rssi;
+ uint8_t noise;
+};
-static uint64_t htonll(uint64_t value)
+static int readpid(void)
{
- int num = 1;
+ int fd;
+ int pid = -1;
+ char buf[9] = { 0 };
- if (*(char *)&num == 1)
- return htonl((uint32_t)(value & 0xFFFFFFFF)) |
- htonl((uint32_t)(value >> 32));
+ if ((fd = open(PID_PATH, O_RDONLY)) > -1)
+ {
+ if (read(fd, buf, sizeof(buf)))
+ {
+ buf[8] = 0;
+ pid = atoi(buf);
+ }
- return value;
+ close(fd);
+ }
+
+ return pid;
}
-#define ntohll htonll
+static int writepid(void)
+{
+ int fd;
+ int wlen;
+ char buf[9] = { 0 };
+
+ if ((fd = open(PID_PATH, O_WRONLY | O_CREAT | O_TRUNC, 0600)) > -1)
+ {
+ wlen = snprintf(buf, sizeof(buf), "%i", getpid());
+ write(fd, buf, wlen);
+ close(fd);
+
+ return 0;
+ }
+
+ return -1;
+}
+
+static int timeout = TIMEOUT;
+static int countdown = -1;
+
+static void reset_countdown(int sig)
+{
+ countdown = timeout;
+
+}
+
+
+static char *progname;
+static int prognamelen;
+
+static int (*iw_get_rate)(const char *, int *) = NULL;
+static int (*iw_get_rssi)(const char *, int *) = NULL;
+static int (*iw_get_noise)(const char *, int *) = NULL;
static int init_directory(char *path)
return 0;
}
+static int init_file(char *path, int esize)
+{
+ int i, file;
+ char buf[sizeof(struct traffic_entry)] = { 0 };
+
+ if (init_directory(path))
+ return -1;
+
+ if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
+ {
+ for (i = 0; i < STEP_COUNT; i++)
+ {
+ if (write(file, buf, esize) < 0)
+ break;
+ }
+
+ close(file);
+
+ return 0;
+ }
+
+ return -1;
+}
+
+static inline uint32_t timeof(void *entry)
+{
+ return ntohl(((struct traffic_entry *)entry)->time);
+}
+
static int update_file(const char *path, void *entry, int esize)
{
int rv = -1;
if ((map != NULL) && (map != MAP_FAILED))
{
- memmove(map, map + esize, esize * (STEP_COUNT-1));
- memcpy(map + esize * (STEP_COUNT-1), entry, esize);
+ if (timeof(entry) > timeof(map + esize * (STEP_COUNT-1)))
+ {
+ memmove(map, map + esize, esize * (STEP_COUNT-1));
+ memcpy(map + esize * (STEP_COUNT-1), entry, esize);
+ }
munmap(map, esize * STEP_COUNT);
return rv;
}
+static int mmap_file(const char *path, int esize, struct file_map *m)
+{
+ m->fd = -1;
+ m->size = -1;
+ m->mmap = NULL;
+
+ if ((m->fd = open(path, O_RDONLY)) >= 0)
+ {
+ m->size = STEP_COUNT * esize;
+ m->mmap = mmap(NULL, m->size, PROT_READ,
+ MAP_SHARED | MAP_LOCKED, m->fd, 0);
+
+ if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
+ return 0;
+ }
+
+ return -1;
+}
-static int init_ifstat(const char *ifname)
+static void umap_file(struct file_map *m)
{
- int i, file;
- char path[1024];
- struct traffic_entry e = { 0 };
+ if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
+ munmap(m->mmap, m->size);
- snprintf(path, sizeof(path), DB_IF_FILE, ifname);
+ if (m->fd > -1)
+ close(m->fd);
+}
- if (init_directory(path))
- return -1;
+static void * iwinfo_open(void)
+{
+ return dlopen("/usr/lib/libiwinfo.so", RTLD_LAZY);
+}
- if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
+static int iwinfo_update(
+ void *iw, const char *ifname, uint16_t *rate, uint8_t *rssi, uint8_t *noise
+) {
+ int (*probe)(const char *);
+ int val;
+
+ if (!iw_get_rate)
{
- for (i = 0; i < STEP_COUNT; i++)
+ if ((probe = dlsym(iw, "nl80211_probe")) != NULL && probe(ifname))
{
- if (write(file, &e, sizeof(struct traffic_entry)) < 0)
- break;
+ iw_get_rate = dlsym(iw, "nl80211_get_bitrate");
+ iw_get_rssi = dlsym(iw, "nl80211_get_signal");
+ iw_get_noise = dlsym(iw, "nl80211_get_noise");
+ }
+ else if ((probe = dlsym(iw, "madwifi_probe")) != NULL && probe(ifname))
+ {
+ iw_get_rate = dlsym(iw, "madwifi_get_bitrate");
+ iw_get_rssi = dlsym(iw, "madwifi_get_signal");
+ iw_get_noise = dlsym(iw, "madwifi_get_noise");
+ }
+ else if ((probe = dlsym(iw, "wl_probe")) != NULL && probe(ifname))
+ {
+ iw_get_rate = dlsym(iw, "wl_get_bitrate");
+ iw_get_rssi = dlsym(iw, "wl_get_signal");
+ iw_get_noise = dlsym(iw, "wl_get_noise");
+ }
+ else
+ {
+ return 0;
}
+ }
- close(file);
+ *rate = (iw_get_rate && !iw_get_rate(ifname, &val)) ? val : 0;
+ *rssi = (iw_get_rssi && !iw_get_rssi(ifname, &val)) ? val : 0;
+ *noise = (iw_get_noise && !iw_get_noise(ifname, &val)) ? val : 0;
- return 0;
- }
+ return 1;
+}
- return -1;
+static void iwinfo_close(void *iw)
+{
+ void (*do_close)(void);
+
+ if ((do_close = dlsym(iw, "nl80211_close")) != NULL) do_close();
+ if ((do_close = dlsym(iw, "madwifi_close")) != NULL) do_close();
+ if ((do_close = dlsym(iw, "wl_close")) != NULL) do_close();
+ if ((do_close = dlsym(iw, "wext_close")) != NULL) do_close();
+ if ((do_close = dlsym(iw, "iwinfo_close")) != NULL) do_close();
+
+ dlclose(iw);
}
+
static int update_ifstat(
- const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
+ const char *ifname, uint32_t rxb, uint32_t rxp, uint32_t txb, uint32_t txp
) {
char path[1024];
if (stat(path, &s))
{
- if (init_ifstat(ifname))
+ if (init_file(path, sizeof(struct traffic_entry)))
{
fprintf(stderr, "Failed to init %s: %s\n",
path, strerror(errno));
}
}
- e.time = htonll(time(NULL));
- e.rxb = htonll(rxb);
- e.rxp = htonll(rxp);
- e.txb = htonll(txb);
- e.txp = htonll(txp);
+ e.time = htonl(time(NULL));
+ e.rxb = htonl(rxb);
+ e.rxp = htonl(rxp);
+ e.txb = htonl(txb);
+ e.txp = htonl(txp);
return update_file(path, &e, sizeof(struct traffic_entry));
}
-static int init_ldstat(void)
-{
- int i, file;
+static int update_radiostat(
+ const char *ifname, uint16_t rate, uint8_t rssi, uint8_t noise
+) {
char path[1024];
- struct load_entry e = { 0 };
- snprintf(path, sizeof(path), DB_LD_FILE);
+ struct stat s;
+ struct radio_entry e;
- if (init_directory(path))
- return -1;
+ snprintf(path, sizeof(path), DB_RD_FILE, ifname);
- if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
+ if (stat(path, &s))
{
- for (i = 0; i < STEP_COUNT; i++)
+ if (init_file(path, sizeof(struct radio_entry)))
{
- if (write(file, &e, sizeof(struct load_entry)) < 0)
- break;
+ fprintf(stderr, "Failed to init %s: %s\n",
+ path, strerror(errno));
+
+ return -1;
}
+ }
- close(file);
+ e.time = htonl(time(NULL));
+ e.rate = htons(rate);
+ e.rssi = rssi;
+ e.noise = noise;
- return 0;
+ return update_file(path, &e, sizeof(struct radio_entry));
+}
+
+static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
+{
+ char path[1024];
+
+ struct stat s;
+ struct conn_entry e;
+
+ snprintf(path, sizeof(path), DB_CN_FILE);
+
+ if (stat(path, &s))
+ {
+ if (init_file(path, sizeof(struct conn_entry)))
+ {
+ fprintf(stderr, "Failed to init %s: %s\n",
+ path, strerror(errno));
+
+ return -1;
+ }
}
- return -1;
+ e.time = htonl(time(NULL));
+ e.udp = htonl(udp);
+ e.tcp = htonl(tcp);
+ e.other = htonl(other);
+
+ return update_file(path, &e, sizeof(struct conn_entry));
}
static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
if (stat(path, &s))
{
- if (init_ldstat())
+ if (init_file(path, sizeof(struct load_entry)))
{
fprintf(stderr, "Failed to init %s: %s\n",
path, strerror(errno));
}
}
- e.time = htonll(time(NULL));
+ e.time = htonl(time(NULL));
e.load1 = htons(load1);
e.load5 = htons(load5);
e.load15 = htons(load15);
return update_file(path, &e, sizeof(struct load_entry));
}
-static int run_daemon(int nofork)
+static int run_daemon(void)
{
FILE *info;
- uint64_t rxb, txb, rxp, txp;
+ uint32_t rxb, txb, rxp, txp;
+ uint32_t udp, tcp, other;
+ uint16_t rate;
+ uint8_t rssi, noise;
float lf1, lf5, lf15;
char line[1024];
char ifname[16];
+ int i;
+ void *iw;
+ struct sigaction sa;
+ struct stat s;
+ const char *ipc = stat("/proc/net/nf_conntrack", &s)
+ ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
- if (!nofork)
+ switch (fork())
{
- switch (fork())
- {
- case -1:
- perror("fork()");
- return -1;
+ case -1:
+ perror("fork()");
+ return -1;
- case 0:
- if (chdir("/") < 0)
- {
- perror("chdir()");
- exit(1);
- }
+ case 0:
+ if (chdir("/") < 0)
+ {
+ perror("chdir()");
+ exit(1);
+ }
- close(0);
- close(1);
- close(2);
- break;
+ close(0);
+ close(1);
+ close(2);
+ break;
- default:
- exit(0);
- }
+ default:
+ return 0;
+ }
+
+ /* setup USR1 signal handler to reset timer */
+ sa.sa_handler = reset_countdown;
+ sa.sa_flags = SA_RESTART;
+ sigemptyset(&sa.sa_mask);
+ sigaction(SIGUSR1, &sa, NULL);
+
+ /* write pid */
+ if (writepid())
+ {
+ fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
+ return 1;
}
+ /* initialize iwinfo */
+ iw = iwinfo_open();
/* go */
- while (1)
+ for (reset_countdown(0); countdown >= 0; countdown--)
{
+ /* alter progname for ps, top */
+ memset(progname, 0, prognamelen);
+ snprintf(progname, prognamelen, "luci-bwc %d", countdown);
+
if ((info = fopen("/proc/net/dev", "r")) != NULL)
{
while (fgets(line, sizeof(line), info))
fclose(info);
}
+ if (iw)
+ {
+ for (i = 0; i < 5; i++)
+ {
+#define iwinfo_checkif(pattern) \
+ do { \
+ snprintf(ifname, sizeof(ifname), pattern, i); \
+ if (iwinfo_update(iw, ifname, &rate, &rssi, &noise)) \
+ { \
+ update_radiostat(ifname, rate, rssi, noise); \
+ continue; \
+ } \
+ } while(0)
+
+ iwinfo_checkif("wlan%d");
+ iwinfo_checkif("ath%d");
+ iwinfo_checkif("wl%d");
+ }
+ }
+
+ if ((info = fopen(ipc, "r")) != NULL)
+ {
+ udp = 0;
+ tcp = 0;
+ other = 0;
+
+ while (fgets(line, sizeof(line), info))
+ {
+ if (strstr(line, "TIME_WAIT"))
+ continue;
+
+ if (strstr(line, "src=127.0.0.1 ") &&
+ strstr(line, "dst=127.0.0.1 "))
+ continue;
+
+ if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
+ {
+ if (!strcmp(ifname, "tcp"))
+ tcp++;
+ else if (!strcmp(ifname, "udp"))
+ udp++;
+ else
+ other++;
+ }
+ }
+
+ update_cnstat(udp, tcp, other);
+
+ fclose(info);
+ }
+
if ((info = fopen("/proc/loadavg", "r")) != NULL)
{
if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
sleep(STEP_TIME);
}
+
+ unlink(PID_PATH);
+
+ if (iw)
+ iwinfo_close(iw);
+
+ return 0;
}
-static int run_dump_ifname(const char *ifname)
+static void check_daemon(void)
{
- int rv = 1;
+ int pid;
- int i, file;
- int entrysize = sizeof(struct traffic_entry);
- int mapsize = STEP_COUNT * entrysize;
+ if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
+ {
+ /* daemon ping failed, try to start it up */
+ if (run_daemon())
+ {
+ fprintf(stderr,
+ "Failed to ping daemon and unable to start it up: %s\n",
+ strerror(errno));
- char path[1024];
- char *map;
+ exit(1);
+ }
+ }
+ else if (kill(pid, SIGUSR1))
+ {
+ fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
+ exit(2);
+ }
+}
+static int run_dump_ifname(const char *ifname)
+{
+ int i;
+ char path[1024];
+ struct file_map m;
struct traffic_entry *e;
+ check_daemon();
snprintf(path, sizeof(path), DB_IF_FILE, ifname);
- if ((file = open(path, O_RDONLY)) >= 0)
+ if (mmap_file(path, sizeof(struct traffic_entry), &m))
{
- map = mmap(NULL, mapsize, PROT_READ, MAP_SHARED | MAP_LOCKED, file, 0);
+ fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
+ return 1;
+ }
- if ((map != NULL) && (map != MAP_FAILED))
- {
- for (i = 0; i < mapsize; i += entrysize)
- {
- e = (struct traffic_entry *) &map[i];
+ for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
+ {
+ e = (struct traffic_entry *) &m.mmap[i];
- if (!e->time)
- continue;
+ if (!e->time)
+ continue;
- printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64
- ", %" PRIu64 ", %" PRIu64 " ]%s\n",
- ntohll(e->time),
- ntohll(e->rxb), ntohll(e->rxp),
- ntohll(e->txb), ntohll(e->txp),
- ((i + entrysize) < mapsize) ? "," : "");
- }
+ printf("[ %u, %u, %" PRIu32
+ ", %u, %u ]%s\n",
+ ntohl(e->time),
+ ntohl(e->rxb), ntohl(e->rxp),
+ ntohl(e->txb), ntohl(e->txp),
+ ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
+ }
- munmap(map, mapsize);
- rv = 0;
- }
+ umap_file(&m);
- close(file);
- }
- else
+ return 0;
+}
+
+static int run_dump_radio(const char *ifname)
+{
+ int i;
+ char path[1024];
+ struct file_map m;
+ struct radio_entry *e;
+
+ check_daemon();
+ snprintf(path, sizeof(path), DB_RD_FILE, ifname);
+
+ if (mmap_file(path, sizeof(struct radio_entry), &m))
{
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
+ return 1;
}
- return rv;
-}
+ for (i = 0; i < m.size; i += sizeof(struct radio_entry))
+ {
+ e = (struct radio_entry *) &m.mmap[i];
-static int run_dump_load(void)
-{
- int rv = 1;
+ if (!e->time)
+ continue;
- int i, file;
- int entrysize = sizeof(struct load_entry);
- int mapsize = STEP_COUNT * entrysize;
+ printf("[ %u, %d, %d, %d ]%s\n",
+ ntohl(e->time),
+ e->rate, e->rssi, e->noise,
+ ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
+ }
+
+ umap_file(&m);
+
+ return 0;
+}
+static int run_dump_conns(void)
+{
+ int i;
char path[1024];
- char *map;
+ struct file_map m;
+ struct conn_entry *e;
- struct load_entry *e;
+ check_daemon();
+ snprintf(path, sizeof(path), DB_CN_FILE);
- snprintf(path, sizeof(path), DB_LD_FILE);
+ if (mmap_file(path, sizeof(struct conn_entry), &m))
+ {
+ fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
+ return 1;
+ }
- if ((file = open(path, O_RDONLY)) >= 0)
+ for (i = 0; i < m.size; i += sizeof(struct conn_entry))
{
- map = mmap(NULL, mapsize, PROT_READ, MAP_SHARED | MAP_LOCKED, file, 0);
+ e = (struct conn_entry *) &m.mmap[i];
- if ((map != NULL) && (map != MAP_FAILED))
- {
- for (i = 0; i < mapsize; i += entrysize)
- {
- e = (struct load_entry *) &map[i];
+ if (!e->time)
+ continue;
- if (!e->time)
- continue;
+ printf("[ %u, %u, %u, %u ]%s\n",
+ ntohl(e->time), ntohl(e->udp),
+ ntohl(e->tcp), ntohl(e->other),
+ ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
+ }
- printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
- ntohll(e->time),
- ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
- ((i + entrysize) < mapsize) ? "," : "");
- }
+ umap_file(&m);
- munmap(map, mapsize);
- rv = 0;
- }
+ return 0;
+}
- close(file);
- }
- else
+static int run_dump_load(void)
+{
+ int i;
+ char path[1024];
+ struct file_map m;
+ struct load_entry *e;
+
+ check_daemon();
+ snprintf(path, sizeof(path), DB_LD_FILE);
+
+ if (mmap_file(path, sizeof(struct load_entry), &m))
{
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
+ return 1;
}
- return rv;
+ for (i = 0; i < m.size; i += sizeof(struct load_entry))
+ {
+ e = (struct load_entry *) &m.mmap[i];
+
+ if (!e->time)
+ continue;
+
+ printf("[ %u, %u, %u, %u ]%s\n",
+ ntohl(e->time),
+ ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
+ ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
+ }
+
+ umap_file(&m);
+
+ return 0;
}
int main(int argc, char *argv[])
{
int opt;
- int daemon = 0;
- int nofork = 0;
- int iprint = 0;
- int lprint = 0;
- char *ifname = NULL;
- while ((opt = getopt(argc, argv, "dfi:l")) > -1)
+ progname = argv[0];
+ prognamelen = -1;
+
+ for (opt = 0; opt < argc; opt++)
+ prognamelen += 1 + strlen(argv[opt]);
+
+ while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
{
switch (opt)
{
- case 'd':
- daemon = 1;
+ case 't':
+ timeout = atoi(optarg);
break;
- case 'f':
- nofork = 1;
+ case 'i':
+ if (optarg)
+ return run_dump_ifname(optarg);
break;
- case 'i':
- iprint = 1;
- ifname = optarg;
+ case 'r':
+ if (optarg)
+ return run_dump_radio(optarg);
break;
+ case 'c':
+ return run_dump_conns();
+
case 'l':
- lprint = 1;
- break;
+ return run_dump_load();
default:
break;
}
}
- if (daemon)
- return run_daemon(nofork);
-
- else if (iprint && ifname)
- return run_dump_ifname(ifname);
-
- else if (lprint)
- return run_dump_load();
-
- else
- fprintf(stderr,
- "Usage:\n"
- " %s -d [-f]\n"
- " %s -i ifname\n"
- " %s -l\n",
- argv[0], argv[0], argv[0]
- );
+ fprintf(stderr,
+ "Usage:\n"
+ " %s [-t timeout] -i ifname\n"
+ " %s [-t timeout] -r radiodev\n"
+ " %s [-t timeout] -c\n"
+ " %s [-t timeout] -l\n",
+ argv[0], argv[0], argv[0], argv[0]
+ );
return 1;
}