From 824b45a808d13d5033d5a7bfe9d66c961ac23e67 Mon Sep 17 00:00:00 2001 From: emanuele-f Date: Sun, 5 May 2019 21:47:50 +0200 Subject: [PATCH] Add linux termination handler and print stats on shutdown --- edge.c | 29 +++++++++++++++++++++++++++- edge_utils.c | 54 +++++++++++++++++++++++++++++++++++----------------- n2n.h | 1 + 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/edge.c b/edge.c index ac07d74..6a42bd3 100644 --- a/edge.c +++ b/edge.c @@ -541,9 +541,29 @@ static void daemonize() { /* *************************************************** */ +static int keep_on_running; + +#ifdef __linux__ + +static void term_handler(int sig) { + static int called = 0; + + if(called) { + traceEvent(TRACE_NORMAL, "Ok I am leaving now"); + _exit(0); + } else { + traceEvent(TRACE_NORMAL, "Shutting down..."); + called = 1; + } + + keep_on_running = 0; +} +#endif + +/* *************************************************** */ + /** Entry point to program from kernel. */ int main(int argc, char* argv[]) { - int keep_on_running = 1; int rc; tuntap_dev tuntap; /* a tuntap device */ n2n_edge_t *eee; /* single instance for this program */ @@ -635,8 +655,15 @@ int main(int argc, char* argv[]) { } #endif +#ifdef __linux__ + signal(SIGTERM, term_handler); + signal(SIGINT, term_handler); +#endif + + keep_on_running = 1; traceEvent(TRACE_NORMAL, "edge started"); rc = run_edge_loop(eee, &keep_on_running); + print_edge_stats(eee); /* Cleanup */ edge_term(eee); diff --git a/edge_utils.c b/edge_utils.c index ed88fa4..cf3f663 100644 --- a/edge_utils.c +++ b/edge_utils.c @@ -76,6 +76,15 @@ int edge_verify_conf(const n2n_edge_conf_t *conf) { /* ************************************** */ +struct n2n_edge_stats { + uint32_t tx_p2p; + uint32_t rx_p2p; + uint32_t tx_sup; + uint32_t rx_sup; +}; + +/* ************************************** */ + struct n2n_edge { n2n_edge_conf_t conf; @@ -105,10 +114,7 @@ struct n2n_edge { time_t start_time; /**< For calculating uptime */ /* Statistics */ - size_t tx_p2p; - size_t rx_p2p; - size_t tx_sup; - size_t rx_sup; + struct n2n_edge_stats stats; }; /* ************************************** */ @@ -328,7 +334,7 @@ static void register_with_new_peer(n2n_edge_t * eee, memcpy(scan->mac_addr, mac, N2N_MAC_SIZE); scan->sock = *peer; - scan->last_seen = time(NULL); /* Don't change this it marks the pending peer for removal. */ + update_peer_seen(scan, time(NULL)); /* Don't change this it marks the pending peer for removal. */ peer_list_add(&(eee->pending_peers), scan); @@ -431,7 +437,7 @@ static void peer_set_p2p_confirmed(n2n_edge_t * eee, (unsigned int)peer_list_size(eee->known_peers)); - scan->last_seen = time(NULL); + update_peer_seen(scan, time(NULL)); } else { @@ -757,12 +763,12 @@ static int handle_PACKET(n2n_edge_t * eee, if(from_supernode) { - ++(eee->rx_sup); + ++(eee->stats.rx_sup); eee->last_sup=now; } else { - ++(eee->rx_p2p); + ++(eee->stats.rx_p2p); eee->last_p2p=now; } @@ -929,10 +935,10 @@ static void readFromMgmtSocket(n2n_edge_t * eee, int * keep_running) { msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len), "paths super:%u,%u p2p:%u,%u\n", - (unsigned int)eee->tx_sup, - (unsigned int)eee->rx_sup, - (unsigned int)eee->tx_p2p, - (unsigned int)eee->rx_p2p); + (unsigned int)eee->stats.tx_sup, + (unsigned int)eee->stats.rx_sup, + (unsigned int)eee->stats.tx_p2p, + (unsigned int)eee->stats.rx_p2p); msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len), "transop |%6u|%6u|\n", @@ -1050,19 +1056,19 @@ static int send_packet(n2n_edge_t * eee, n2n_mac_t dstMac, const uint8_t * pktbuf, size_t pktlen) { - int dest; + int is_p2p; /*ssize_t s; */ n2n_sock_str_t sockbuf; n2n_sock_t destination; /* hexdump(pktbuf, pktlen); */ - dest = find_peer_destination(eee, dstMac, &destination); + is_p2p = find_peer_destination(eee, dstMac, &destination); - if(dest) - ++(eee->tx_p2p); + if(is_p2p) + ++(eee->stats.tx_p2p); else - ++(eee->tx_sup); + ++(eee->stats.tx_sup); traceEvent(TRACE_INFO, "send_packet to %s", sock_to_cstr(sockbuf, &destination)); @@ -1423,6 +1429,20 @@ static void readFromIPSocket(n2n_edge_t * eee, int in_sock) { /* ************************************** */ +void print_edge_stats(const n2n_edge_t *eee) { + const struct n2n_edge_stats *s = &eee->stats; + + traceEvent(TRACE_NORMAL, "**********************************"); + traceEvent(TRACE_NORMAL, "Packet stats:"); + traceEvent(TRACE_NORMAL, " TX P2P: %u pkts", s->tx_p2p); + traceEvent(TRACE_NORMAL, " RX P2P: %u pkts", s->rx_p2p); + traceEvent(TRACE_NORMAL, " TX Supernode: %u pkts", s->tx_sup); + traceEvent(TRACE_NORMAL, " RX Supernode: %u pkts", s->rx_sup); + traceEvent(TRACE_NORMAL, "**********************************"); +} + +/* ************************************** */ + int run_edge_loop(n2n_edge_t * eee, int *keep_running) { size_t numPurged; time_t lastIfaceCheck=0; diff --git a/n2n.h b/n2n.h index 4292a45..2a8a516 100644 --- a/n2n.h +++ b/n2n.h @@ -260,6 +260,7 @@ char* msg_type2str(uint16_t msg_type); void hexdump(const uint8_t * buf, size_t len); void print_n2n_version(); int is_empty_ip_address(const n2n_sock_t * sock); +void print_edge_stats(const n2n_edge_t *eee); /* Sockets */ char* sock_to_cstr( n2n_sock_str_t out,