diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/fastd.h | 36 | ||||
-rw-r--r-- | src/receive.c | 4 | ||||
-rw-r--r-- | src/send.c | 8 | ||||
-rw-r--r-- | src/status.c | 16 |
4 files changed, 36 insertions, 28 deletions
diff --git a/src/fastd.h b/src/fastd.h index 26fd371..94c9fc7 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -149,14 +149,26 @@ struct fastd_socket { fastd_peer_t *peer; /**< If the socket belongs to a single peer (as it was create dynamically when sending a handshake), contains that peer */ }; -/** Some kind of network transfer stratistics */ + +/** Type of a traffic stat counter */ +typedef enum fastd_stat_type { + STAT_RX = 0, /**< Reception statistics (total) */ + STAT_RX_REORDERED, /**< Reception statistics (reordered) */ + STAT_TX, /**< Transmission statistics (OK) */ + STAT_TX_DROPPED, /**< Transmission statistics (dropped because of full queues) */ + STAT_TX_ERROR, /**< Transmission statistics (other errors) */ + STAT_MAX, /**< (Number of defined stat types) */ +} fastd_stat_type_t; + +/** Some kind of network transfer statistics */ struct fastd_stats { #ifdef WITH_STATUS_SOCKET - uint64_t packets; /**< The number of packets transferred */ - uint64_t bytes; /**< The number of bytes transferred */ + uint64_t packets[STAT_MAX]; /**< The number of packets transferred */ + uint64_t bytes[STAT_MAX]; /**< The number of bytes transferred */ #endif }; + /** A data structure keeping track of an unknown addresses that a handshakes was received from recently */ struct fastd_handshake_timeout { fastd_peer_address_t address; /**< An address a handshake was received from */ @@ -288,12 +300,7 @@ struct fastd_context { fastd_socket_t *sock_default_v4; /**< Points to the socket that is used for new outgoing IPv4 connections */ fastd_socket_t *sock_default_v6; /**< Points to the socket that is used for new outgoing IPv6 connections */ - fastd_stats_t rx; /**< Reception statistics (total) */ - fastd_stats_t rx_reordered; /**< Reception statistics (reordered packets) */ - - fastd_stats_t tx; /**< Transmission statistics (OK) */ - fastd_stats_t tx_dropped; /**< Transmission statistics (dropped because of full queues) */ - fastd_stats_t tx_error; /**< Transmission statistics (other errors) */ + fastd_stats_t stats; /**< Traffic statistics */ VECTOR(fastd_peer_eth_addr_t) eth_addrs; /**< Sorted vector of all known ethernet addresses with associated peers and timeouts */ @@ -387,12 +394,13 @@ static inline size_t fastd_max_payload(void) { } /** Adds statistics for a single packet of a given size */ -static inline void fastd_stats_add(UNUSED fastd_stats_t *stats, UNUSED size_t stat_size) { +static inline void fastd_stats_add(UNUSED fastd_peer_t *peer, UNUSED fastd_stat_type_t stat, UNUSED size_t bytes) { #ifdef WITH_STATUS_SOCKET - if (stat_size) { - stats->packets++; - stats->bytes += stat_size; - } + if (!bytes) + return; + + ctx.stats.packets[stat]++; + ctx.stats.bytes[stat] += bytes; #endif } diff --git a/src/receive.c b/src/receive.c index 451e39d..e2a996b 100644 --- a/src/receive.c +++ b/src/receive.c @@ -267,10 +267,10 @@ void fastd_handle_receive(fastd_peer_t *peer, fastd_buffer_t buffer, bool reorde fastd_peer_eth_addr_add(peer, src_addr); } - fastd_stats_add(&ctx.rx, buffer.len); + fastd_stats_add(peer, STAT_RX, buffer.len); if (reordered) - fastd_stats_add(&ctx.rx_reordered, buffer.len); + fastd_stats_add(peer, STAT_RX_REORDERED, buffer.len); fastd_tuntap_write(buffer); @@ -143,23 +143,23 @@ static void send_type(const fastd_socket_t *sock, const fastd_peer_address_t *lo case EWOULDBLOCK: #endif pr_debug2_errno("sendmsg"); - fastd_stats_add(&ctx.tx_dropped, stat_size); + fastd_stats_add(peer, STAT_TX_DROPPED, stat_size); break; case ENETDOWN: case ENETUNREACH: case EHOSTUNREACH: pr_debug_errno("sendmsg"); - fastd_stats_add(&ctx.tx_error, stat_size); + fastd_stats_add(peer, STAT_TX_ERROR, stat_size); break; default: pr_warn_errno("sendmsg"); - fastd_stats_add(&ctx.tx_error, stat_size); + fastd_stats_add(peer, STAT_TX_ERROR, stat_size); } } else { - fastd_stats_add(&ctx.tx, stat_size); + fastd_stats_add(peer, STAT_TX, stat_size); } fastd_buffer_free(buffer); diff --git a/src/status.c b/src/status.c index 8049f59..e36c2af 100644 --- a/src/status.c +++ b/src/status.c @@ -76,11 +76,11 @@ static void * dump_thread(void *p) { /** Dumps a fastd_stats_t as a JSON object */ -static json_object * dump_stats(const fastd_stats_t *stats) { +static json_object * dump_stats(const fastd_stats_t *stats, fastd_stat_type_t type) { struct json_object *ret = json_object_new_object(); - json_object_object_add(ret, "packets", json_object_new_int64(stats->packets)); - json_object_object_add(ret, "bytes", json_object_new_int64(stats->bytes)); + json_object_object_add(ret, "packets", json_object_new_int64(stats->packets[type])); + json_object_object_add(ret, "bytes", json_object_new_int64(stats->bytes[type])); return ret; } @@ -144,12 +144,12 @@ static void dump_status(int fd) { struct json_object *statistics = json_object_new_object(); json_object_object_add(json, "statistics", statistics); - json_object_object_add(statistics, "rx", dump_stats(&ctx.rx)); - json_object_object_add(statistics, "rx_reordered", dump_stats(&ctx.rx_reordered)); + json_object_object_add(statistics, "rx", dump_stats(&ctx.stats, STAT_RX)); + json_object_object_add(statistics, "rx_reordered", dump_stats(&ctx.stats, STAT_RX_REORDERED)); - json_object_object_add(statistics, "tx", dump_stats(&ctx.tx)); - json_object_object_add(statistics, "tx_dropped", dump_stats(&ctx.tx_dropped)); - json_object_object_add(statistics, "tx_error", dump_stats(&ctx.tx_error)); + json_object_object_add(statistics, "tx", dump_stats(&ctx.stats, STAT_TX)); + json_object_object_add(statistics, "tx_dropped", dump_stats(&ctx.stats, STAT_TX_DROPPED)); + json_object_object_add(statistics, "tx_error", dump_stats(&ctx.stats, STAT_TX_ERROR)); struct json_object *peers = json_object_new_object(); json_object_object_add(json, "peers", peers); |