diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-09-03 21:53:45 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-09-03 21:53:45 +0200 |
commit | 02c1cb4ce21dca6cc63545cd5bd7ed621ed61529 (patch) | |
tree | 7c953fbb9c062936b66f63a8d95b5702da7ece8e | |
parent | f0fa4e0c9085e0b4476b144185040ce9fbdc11e3 (diff) | |
download | fastd-02c1cb4ce21dca6cc63545cd5bd7ed621ed61529.tar fastd-02c1cb4ce21dca6cc63545cd5bd7ed621ed61529.zip |
Add packet/byte statistics
-rw-r--r-- | src/fastd.c | 15 | ||||
-rw-r--r-- | src/fastd.h | 13 | ||||
-rw-r--r-- | src/printf.c | 4 | ||||
-rw-r--r-- | src/protocol_ec25519_fhmqvc.c | 4 | ||||
-rw-r--r-- | src/send.c | 21 | ||||
-rw-r--r-- | src/types.h | 2 |
6 files changed, 49 insertions, 10 deletions
diff --git a/src/fastd.c b/src/fastd.c index 6ce10fc..e5c1364 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -246,6 +246,9 @@ void fastd_handle_receive(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer fastd_peer_eth_addr_add(ctx, peer, src_addr); } + ctx->rx.packets++; + ctx->rx.bytes += buffer.len; + fastd_tuntap_write(ctx, buffer); if (ctx->conf->mode == MODE_TAP && ctx->conf->forward) { @@ -360,8 +363,12 @@ static void delete_peers(fastd_context_t *ctx) { } } -static void dump_peers(fastd_context_t *ctx) { - pr_info(ctx, "dumping peers..."); +static void dump_state(fastd_context_t *ctx) { + pr_info(ctx, "TX stats: %U packet(s), %U byte(s); dropped: %U packet(s), %U byte(s); error: %U packet(s), %U byte(s)", + ctx->tx.packets, ctx->tx.bytes, ctx->tx_dropped.packets, ctx->tx_dropped.bytes, ctx->tx_error.packets, ctx->tx_error.bytes); + pr_info(ctx, "RX stats: %U packet(s), %U byte(s)", ctx->rx.packets, ctx->rx.bytes); + + pr_info(ctx, "dumping peers:"); fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { @@ -385,7 +392,7 @@ static void dump_peers(fastd_context_t *ctx) { } } - pr_info(ctx, "peer dump finished."); + pr_info(ctx, "dump finished."); } static inline void update_time(fastd_context_t *ctx) { @@ -849,7 +856,7 @@ int main(int argc, char *argv[]) { if (dump) { dump = false; - dump_peers(&ctx); + dump_state(&ctx); } pthread_sigmask(SIG_SETMASK, &oldset, NULL); diff --git a/src/fastd.h b/src/fastd.h index dcb35b4..1fe639e 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -161,6 +161,11 @@ struct fastd_peer_group { unsigned n_connections; }; +struct fastd_stats { + uint64_t packets; + uint64_t bytes; +}; + struct fastd_config { struct timespec long_ago; @@ -294,6 +299,12 @@ struct fastd_context { fastd_socket_t *sock_default_v4; fastd_socket_t *sock_default_v6; + fastd_stats_t rx; + + fastd_stats_t tx; + fastd_stats_t tx_dropped; + fastd_stats_t tx_error; + #ifdef USE_CRYPTO_AES128CTR fastd_crypto_aes128ctr_context_t *crypto_aes128ctr; #endif @@ -316,7 +327,7 @@ struct fastd_string_stack { }; -void fastd_send(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer); +void fastd_send(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer, size_t stat_size); void fastd_send_all(fastd_context_t *ctx, fastd_peer_t *source_peer, fastd_buffer_t buffer); void fastd_send_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer); void fastd_receive(fastd_context_t *ctx, fastd_socket_t *sock); diff --git a/src/printf.c b/src/printf.c index deb48c0..6feb72a 100644 --- a/src/printf.c +++ b/src/printf.c @@ -125,6 +125,10 @@ int fastd_vsnprintf(const fastd_context_t *ctx, char *buffer, size_t size, const buffer += snprintf_safe(buffer, buffer_end-buffer, "%u", va_arg(ap, unsigned int)); break; + case 'U': + buffer += snprintf_safe(buffer, buffer_end-buffer, "%llu", (unsigned long long)va_arg(ap, uint64_t)); + break; + case 's': buffer += snprintf_safe(buffer, buffer_end-buffer, "%s", va_arg(ap, char*)); break; diff --git a/src/protocol_ec25519_fhmqvc.c b/src/protocol_ec25519_fhmqvc.c index 73fbc5e..62e9303 100644 --- a/src/protocol_ec25519_fhmqvc.c +++ b/src/protocol_ec25519_fhmqvc.c @@ -826,13 +826,15 @@ static void protocol_handle_recv(fastd_context_t *ctx, fastd_peer_t *peer, fastd } static void session_send(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer, protocol_session_t *session) { + size_t stat_size = buffer.len; + fastd_buffer_t send_buffer; if (!session->method->encrypt(ctx, peer, session->method_state, &send_buffer, buffer)) { fastd_buffer_free(buffer); return; } - fastd_send(ctx, peer->sock, &peer->local_address, &peer->address, send_buffer); + fastd_send(ctx, peer->sock, &peer->local_address, &peer->address, send_buffer, stat_size); peer->last_send = ctx->now; } @@ -64,7 +64,14 @@ static inline void add_pktinfo(struct msghdr *msg, const fastd_peer_address_t *l } } -static void send_type(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, uint8_t packet_type, fastd_buffer_t buffer) { +static inline void count_stat(fastd_stats_t *stats, size_t stat_size) { + if (stat_size) { + stats->packets++; + stats->bytes += stat_size; + } +} + +static void send_type(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, uint8_t packet_type, fastd_buffer_t buffer, size_t stat_size) { if (!sock) exit_bug(ctx, "send: sock == NULL"); @@ -122,28 +129,34 @@ static void send_type(fastd_context_t *ctx, const fastd_socket_t *sock, const fa case EWOULDBLOCK: #endif pr_debug2_errno(ctx, "sendmsg"); + count_stat(&ctx->tx_dropped, stat_size); break; case ENETDOWN: case ENETUNREACH: case EHOSTUNREACH: pr_debug_errno(ctx, "sendmsg"); + count_stat(&ctx->tx_error, stat_size); break; default: pr_warn_errno(ctx, "sendmsg"); + count_stat(&ctx->tx_error, stat_size); } } + else { + count_stat(&ctx->tx, stat_size); + } fastd_buffer_free(buffer); } -void fastd_send(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer) { - send_type(ctx, sock, local_addr, remote_addr, PACKET_DATA, buffer); +void fastd_send(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer, size_t stat_size) { + send_type(ctx, sock, local_addr, remote_addr, PACKET_DATA, buffer, stat_size); } void fastd_send_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer) { - send_type(ctx, sock, local_addr, remote_addr, PACKET_HANDSHAKE, buffer); + send_type(ctx, sock, local_addr, remote_addr, PACKET_HANDSHAKE, buffer, 0); } void fastd_send_all(fastd_context_t *ctx, fastd_peer_t *source_peer, fastd_buffer_t buffer) { diff --git a/src/types.h b/src/types.h index 3af287a..e817f2e 100644 --- a/src/types.h +++ b/src/types.h @@ -81,6 +81,7 @@ typedef enum fastd_loglevel { LL_DEBUG2, } fastd_loglevel_t; + typedef struct fastd_buffer fastd_buffer_t; typedef union fastd_peer_address fastd_peer_address_t; @@ -94,6 +95,7 @@ typedef struct fastd_peer fastd_peer_t; typedef struct fastd_peer_eth_addr fastd_peer_eth_addr_t; typedef struct fastd_remote_config fastd_remote_config_t; typedef struct fastd_remote fastd_remote_t; +typedef struct fastd_stats fastd_stats_t; typedef struct fastd_log_file fastd_log_file_t; typedef struct fastd_log_fd fastd_log_fd_t; |