Update purge logic of communities and edges for supernode

This commit is contained in:
switch_st 2020-07-22 15:49:45 +08:00
parent 0ab9f3229e
commit 81516d8d8c
3 changed files with 37 additions and 18 deletions

View File

@ -35,6 +35,9 @@
#define IFACE_UPDATE_INTERVAL (30) /* sec. How long it usually takes to get an IP lease. */ #define IFACE_UPDATE_INTERVAL (30) /* sec. How long it usually takes to get an IP lease. */
#define TRANSOP_TICK_INTERVAL (10) /* sec */ #define TRANSOP_TICK_INTERVAL (10) /* sec */
#define PURGE_REGISTRATION_FREQUENCY 30
#define REGISTRATION_TIMEOUT 60
#define ETH_FRAMESIZE 14 #define ETH_FRAMESIZE 14
#define IP4_SRCOFFSET 12 #define IP4_SRCOFFSET 12
#define IP4_DSTOFFSET 16 #define IP4_DSTOFFSET 16

View File

@ -22,9 +22,6 @@
#include <assert.h> #include <assert.h>
#define PURGE_REGISTRATION_FREQUENCY 30
#define REGISTRATION_TIMEOUT 60
static const uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; static const uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static const uint8_t multicast_addr[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 }; /* First 3 bytes are meaningful */ static const uint8_t multicast_addr[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 }; /* First 3 bytes are meaningful */
static const uint8_t ipv6_multicast_addr[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; /* First 2 bytes are meaningful */ static const uint8_t ipv6_multicast_addr[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; /* First 2 bytes are meaningful */

View File

@ -30,6 +30,10 @@ static int update_edge(n2n_sn_t *sss,
const n2n_sock_t *sender_sock, const n2n_sock_t *sender_sock,
time_t now); time_t now);
static int purge_expired_communities(n2n_sn_t *sss,
time_t* p_last_purge,
time_t now);
static int process_mgmt(n2n_sn_t *sss, static int process_mgmt(n2n_sn_t *sss,
const struct sockaddr_in *sender_sock, const struct sockaddr_in *sender_sock,
const uint8_t *mgmt_buf, const uint8_t *mgmt_buf,
@ -286,6 +290,35 @@ static int update_edge(n2n_sn_t *sss,
return 0; return 0;
} }
static int purge_expired_communities(n2n_sn_t *sss,
time_t* p_last_purge,
time_t now)
{
struct sn_community *comm, *tmp;
size_t num_reg = 0;
if ((now - (*p_last_purge)) < PURGE_REGISTRATION_FREQUENCY) return 0;
traceEvent(TRACE_DEBUG, "Purging old communities and edges");
HASH_ITER(hh, sss->communities, comm, tmp) {
num_reg += purge_peer_list(&comm->edges, now - REGISTRATION_TIMEOUT);
if ((comm->edges == NULL) && (!sss->lock_communities)) {
traceEvent(TRACE_INFO, "Purging idle community %s", comm->community);
if (NULL != comm->header_encryption_ctx)
/* this should not happen as no 'locked' and thus only communities w/o encrypted header here */
free(comm->header_encryption_ctx);
HASH_DEL(sss->communities, comm);
free(comm);
}
}
(*p_last_purge) = now;
traceEvent(TRACE_DEBUG, "Remove %ld edges", num_reg);
return 0;
}
static int process_mgmt(n2n_sn_t *sss, static int process_mgmt(n2n_sn_t *sss,
const struct sockaddr_in *sender_sock, const struct sockaddr_in *sender_sock,
const uint8_t *mgmt_buf, const uint8_t *mgmt_buf,
@ -759,7 +792,6 @@ int run_sn_loop(n2n_sn_t *sss, int *keep_running)
{ {
uint8_t pktbuf[N2N_SN_PKTBUF_SIZE]; uint8_t pktbuf[N2N_SN_PKTBUF_SIZE];
time_t last_purge_edges = 0; time_t last_purge_edges = 0;
struct sn_community *comm, *tmp;
sss->start_time = time(NULL); sss->start_time = time(NULL);
@ -844,20 +876,7 @@ int run_sn_loop(n2n_sn_t *sss, int *keep_running)
traceEvent(TRACE_DEBUG, "timeout"); traceEvent(TRACE_DEBUG, "timeout");
} }
HASH_ITER(hh, sss->communities, comm, tmp) purge_expired_communities(sss, &last_purge_edges, now);
{
purge_expired_registrations(&comm->edges, &last_purge_edges);
if ((comm->edges == NULL) && (!sss->lock_communities))
{
traceEvent(TRACE_INFO, "Purging idle community %s", comm->community);
if (NULL != comm->header_encryption_ctx)
/* this should not happen as no 'locked' and thus only communities w/o encrypted header here */
free (comm->header_encryption_ctx);
HASH_DEL(sss->communities, comm);
free(comm);
}
}
} /* while */ } /* while */