From f5843e7d649f4a7f72cb3fd356bc935457aa492f Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 15 May 2011 00:42:29 +0200 Subject: [PATCH] Add per-node traffic counters. --- src/device.h | 6 +++++- src/linux/device.c | 16 ++++++++++------ src/net.h | 2 +- src/net_packet.c | 15 +++++++++++++-- src/node.h | 5 +++++ 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/device.h b/src/device.h index 5841253..16a2486 100644 --- a/src/device.h +++ b/src/device.h @@ -25,9 +25,13 @@ extern int device_fd; extern char *device; - extern char *iface; +extern uint64_t device_in_packets; +extern uint64_t device_in_bytes; +extern uint64_t device_out_packets; +extern uint64_t device_out_bytes; + extern bool setup_device(void); extern void close_device(void); extern bool read_packet(struct vpn_packet_t *); diff --git a/src/linux/device.c b/src/linux/device.c index 0cfc546..111b98c 100644 --- a/src/linux/device.c +++ b/src/linux/device.c @@ -47,8 +47,10 @@ char *iface = NULL; static char ifrname[IFNAMSIZ]; static char *device_info; -static uint64_t device_total_in = 0; -static uint64_t device_total_out = 0; +uint64_t device_in_packets = 0; +uint64_t device_in_bytes = 0; +uint64_t device_out_packets = 0; +uint64_t device_out_bytes = 0; bool setup_device(void) { struct ifreq ifr; @@ -166,7 +168,8 @@ bool read_packet(vpn_packet_t *packet) { break; } - device_total_in += packet->len; + device_in_packets++; + device_in_bytes += packet->len; ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info); @@ -205,13 +208,14 @@ bool write_packet(vpn_packet_t *packet) { break; } - device_total_out += packet->len; + device_out_packets++; + device_out_bytes += packet->len; return true; } void dump_device_stats(void) { logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device); - logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in); - logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out); + logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_in_bytes); + logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_out_bytes); } diff --git a/src/net.h b/src/net.h index 9b625a0..d5eeb05 100644 --- a/src/net.h +++ b/src/net.h @@ -131,7 +131,7 @@ extern void do_outgoing_connection(struct connection_t *); extern void handle_new_meta_connection(int, short, void *); extern int setup_listen_socket(const sockaddr_t *); extern int setup_vpn_in_socket(const sockaddr_t *); -extern void send_packet(const struct node_t *, vpn_packet_t *); +extern void send_packet(struct node_t *, vpn_packet_t *); extern void receive_tcppacket(struct connection_t *, char *, int); extern void broadcast_packet(const struct node_t *, vpn_packet_t *); extern bool setup_network(void); diff --git a/src/net_packet.c b/src/net_packet.c index ded80a2..7e567e9 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -236,6 +236,9 @@ static void receive_packet(node_t *n, vpn_packet_t *packet) { ifdebug(TRAFFIC) logger(LOG_DEBUG, "Received packet of %d bytes from %s (%s)", packet->len, n->name, n->hostname); + n->in_packets++; + n->in_bytes += packet->len; + route(n, packet); } @@ -502,12 +505,14 @@ end: /* send a packet to the given vpn ip. */ -void send_packet(const node_t *n, vpn_packet_t *packet) { +void send_packet(node_t *n, vpn_packet_t *packet) { node_t *via; if(n == myself) { if(overwrite_mac) memcpy(packet->data, mymac.x, ETH_ALEN); + n->out_packets++; + n->out_bytes += packet->len; write_packet(packet); return; } @@ -521,6 +526,9 @@ void send_packet(const node_t *n, vpn_packet_t *packet) { return; } + n->out_packets++; + n->out_bytes += packet->len; + via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via; if(via != n) @@ -640,6 +648,9 @@ void handle_incoming_vpn_data(int sock, short events, void *data) { void handle_device_data(int sock, short events, void *data) { vpn_packet_t packet; - if(read_packet(&packet)) + if(read_packet(&packet)) { + myself->in_packets++; + myself->in_bytes += packet.len; route(myself, &packet); + } } diff --git a/src/node.h b/src/node.h index 4eb216f..38e0e06 100644 --- a/src/node.h +++ b/src/node.h @@ -77,6 +77,11 @@ typedef struct node_t { length_t maxmtu; /* Probed maximum MTU */ int mtuprobes; /* Number of probes */ struct event mtuevent; /* Probe event */ + + uint64_t in_packets; + uint64_t in_bytes; + uint64_t out_packets; + uint64_t out_bytes; } node_t; extern struct node_t *myself; -- 2.25.1