sn_selection.c (n2n-3.0) | : | sn_selection.c (n2n-3.1.1) | ||
---|---|---|---|---|
/** | /** | |||
* (C) 2007-21 - ntop.org and contributors | * (C) 2007-22 - ntop.org and contributors | |||
* | * | |||
* This program is free software; you can redistribute it and/or modify | * This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 3 of the License, or | * the Free Software Foundation; either version 3 of the License, or | |||
* (at your option) any later version. | * (at your option) any later version. | |||
* | * | |||
* This program is distributed in the hope that it will be useful, | * This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | * GNU General Public License for more details. | |||
skipping to change at line 85 | skipping to change at line 85 | |||
* Because this behavior has a cost of switching, the real load is m itigated with a stickyness factor. | * Because this behavior has a cost of switching, the real load is m itigated with a stickyness factor. | |||
* This factor is dynamically calculated basing on network size and prevent that unnecessary switching */ | * This factor is dynamically calculated basing on network size and prevent that unnecessary switching */ | |||
if(peer == eee->curr_sn) { | if(peer == eee->curr_sn) { | |||
sum = HASH_COUNT(eee->known_peers) + HASH_COUNT(eee->pending_pee rs); | sum = HASH_COUNT(eee->known_peers) + HASH_COUNT(eee->pending_pee rs); | |||
peer->selection_criterion = peer->selection_criterion * sum / (s um + 1); | peer->selection_criterion = peer->selection_criterion * sum / (s um + 1); | |||
} | } | |||
break; | break; | |||
} | } | |||
case SN_SELECTION_STRATEGY_RTT: { | case SN_SELECTION_STRATEGY_RTT: { | |||
peer->selection_criterion = (SN_SELECTION_CRITERION_DATA_TYPE)(time_ | peer->selection_criterion = (SN_SELECTION_CRITERION_DATA_TYPE)((uint | |||
stamp() >> 22) - common_data; | 32_t)time_stamp() >> 22) - common_data; | |||
break; | ||||
} | ||||
case SN_SELECTION_STRATEGY_MAC: { | ||||
peer->selection_criterion = 0; | ||||
memcpy(&peer->selection_criterion, /* leftbound, don't mess with poi | ||||
nter arithmetics */ | ||||
peer->mac_addr, | ||||
N2N_MAC_SIZE); | ||||
peer->selection_criterion = be64toh(peer->selection_criterion); | ||||
peer->selection_criterion >>= (sizeof(peer->selection_criterion) - N | ||||
2N_MAC_SIZE) * 8; /* rightbound */ | ||||
break; | break; | |||
} | } | |||
default: { | default: { | |||
// this should never happen | // this should never happen | |||
traceEvent(TRACE_ERROR, "selection_criterion unknown selection strat egy configuration"); | traceEvent(TRACE_ERROR, "selection_criterion unknown selection strat egy configuration"); | |||
break; | break; | |||
} | } | |||
} | } | |||
skipping to change at line 116 | skipping to change at line 126 | |||
tmp = HASH_COUNT(eee->pending_peers); | tmp = HASH_COUNT(eee->pending_peers); | |||
if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED) { | if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED) { | |||
tmp *= 2; | tmp *= 2; | |||
} | } | |||
eee->sn_selection_criterion_common_data = tmp / HASH_COUNT(eee->conf .supernodes); | eee->sn_selection_criterion_common_data = tmp / HASH_COUNT(eee->conf .supernodes); | |||
break; | break; | |||
} | } | |||
case SN_SELECTION_STRATEGY_RTT: { | case SN_SELECTION_STRATEGY_RTT: { | |||
eee->sn_selection_criterion_common_data = (SN_SELECTION_CRITERION_DA | eee->sn_selection_criterion_common_data = (SN_SELECTION_CRITERION_DA | |||
TA_TYPE)(time_stamp() >> 22); | TA_TYPE)((uint32_t)time_stamp() >> 22); | |||
break; | ||||
} | ||||
case SN_SELECTION_STRATEGY_MAC: { | ||||
eee->sn_selection_criterion_common_data = 0; | ||||
break; | break; | |||
} | } | |||
default: { | default: { | |||
// this should never happen | // this should never happen | |||
traceEvent(TRACE_ERROR, "selection_criterion unknown selection strat egy configuration"); | traceEvent(TRACE_ERROR, "selection_criterion unknown selection strat egy configuration"); | |||
break; | break; | |||
} | } | |||
} | } | |||
skipping to change at line 139 | skipping to change at line 154 | |||
/* Return the value of sn_selection_criterion_common_data field. */ | /* Return the value of sn_selection_criterion_common_data field. */ | |||
static SN_SELECTION_CRITERION_DATA_TYPE sn_selection_criterion_common_read (n2n_ edge_t *eee) { | static SN_SELECTION_CRITERION_DATA_TYPE sn_selection_criterion_common_read (n2n_ edge_t *eee) { | |||
return eee->sn_selection_criterion_common_data; | return eee->sn_selection_criterion_common_data; | |||
} | } | |||
/* Function that compare two selection_criterion fields and sorts them in ascend ing order. */ | /* Function that compare two selection_criterion fields and sorts them in ascend ing order. */ | |||
static int sn_selection_criterion_sort (peer_info_t *a, peer_info_t *b) { | static int sn_selection_criterion_sort (peer_info_t *a, peer_info_t *b) { | |||
int ret = 0; | ||||
// comparison function for sorting supernodes in ascending order of their se lection_criterion. | // comparison function for sorting supernodes in ascending order of their se lection_criterion. | |||
return (a->selection_criterion - b->selection_criterion); | if(a->selection_criterion > b->selection_criterion) | |||
ret = 1; | ||||
else if(a->selection_criterion < b->selection_criterion) | ||||
ret = -1; | ||||
return ret; | ||||
} | } | |||
/* Function that sorts peer_list using sn_selection_criterion_sort. */ | /* Function that sorts peer_list using sn_selection_criterion_sort. */ | |||
int sn_selection_sort (peer_info_t **peer_list) { | int sn_selection_sort (peer_info_t **peer_list) { | |||
HASH_SORT(*peer_list, sn_selection_criterion_sort); | HASH_SORT(*peer_list, sn_selection_criterion_sort); | |||
return 0; /* OK */ | return 0; /* OK */ | |||
} | } | |||
skipping to change at line 169 | skipping to change at line 191 | |||
HASH_ITER(hh, sss->communities, comm, tmp_comm) { | HASH_ITER(hh, sss->communities, comm, tmp_comm) { | |||
// number of nodes in the community + the community itself | // number of nodes in the community + the community itself | |||
tmp = HASH_COUNT(comm->edges) + 1; | tmp = HASH_COUNT(comm->edges) + 1; | |||
if(comm->header_encryption == HEADER_ENCRYPTION_ENABLED) { | if(comm->header_encryption == HEADER_ENCRYPTION_ENABLED) { | |||
// double-count encrypted communities (and their nodes): they exert more load on supernode | // double-count encrypted communities (and their nodes): they exert more load on supernode | |||
tmp *= 2; | tmp *= 2; | |||
} | } | |||
data += tmp; | data += tmp; | |||
} | } | |||
return htobe32(data); | return htobe64(data); | |||
} | } | |||
/* Convert selection_criterion field in a string for management port output. */ | /* Convert selection_criterion field in a string for management port output. */ | |||
extern char * sn_selection_criterion_str (n2n_edge_t *eee, selection_criterion_s tr_t out, peer_info_t *peer) { | extern char * sn_selection_criterion_str (n2n_edge_t *eee, selection_criterion_s tr_t out, peer_info_t *peer) { | |||
int chars = 0; | int chars = 0; | |||
if(NULL == out) { | if(NULL == out) { | |||
return NULL; | return NULL; | |||
} | } | |||
memset(out, 0, SN_SELECTION_CRITERION_BUF_SIZE); | memset(out, 0, SN_SELECTION_CRITERION_BUF_SIZE); | |||
// keep off the super-big values (used for "bad" or "good" or "undetermined" supernodes, | // keep off the super-big values (used for "bad" or "good" or "undetermined" supernodes, | |||
// easier to sort to the end of the list). | // easier to sort to the end of the list). | |||
// Alternatively, typecast to (int16_t) and check for greater or equal zero | // Alternatively, typecast to (int16_t) and check for greater or equal zero | |||
if(peer->selection_criterion < (UINT32_MAX >> 2)) { | if(peer->selection_criterion < (UINT64_MAX >> 2)) { | |||
switch(eee->conf.sn_selection_strategy) { | switch(eee->conf.sn_selection_strategy) { | |||
case SN_SELECTION_STRATEGY_LOAD: { | case SN_SELECTION_STRATEGY_LOAD: { | |||
chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "load = % 8ld", peer->selection_criterion); | chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "load = % 8ld", peer->selection_criterion); | |||
break; | break; | |||
} | } | |||
case SN_SELECTION_STRATEGY_RTT: { | case SN_SELECTION_STRATEGY_RTT: { | |||
chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "rtt = %6 ld ms", peer->selection_criterion); | chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "rtt = %6 ld ms", peer->selection_criterion); | |||
break; | break; | |||
} | } | |||
case SN_SELECTION_STRATEGY_MAC: { | ||||
chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "%s", ((i | ||||
nt64_t)peer->selection_criterion > 0 ? | ||||
(( | ||||
peer == eee->curr_sn) ? "active" : "standby") : "")); | ||||
break; | ||||
} | ||||
default: { | default: { | |||
// this should never happen | // this should never happen | |||
traceEvent(TRACE_ERROR, "selection_criterion unknown selection s trategy configuration"); | traceEvent(TRACE_ERROR, "selection_criterion unknown selection s trategy configuration"); | |||
break; | break; | |||
} | } | |||
} | } | |||
// this test is to make "-Wformat-truncation" less sad | // this test is to make "-Wformat-truncation" less sad | |||
if(chars > SN_SELECTION_CRITERION_BUF_SIZE) { | if(chars > SN_SELECTION_CRITERION_BUF_SIZE) { | |||
traceEvent(TRACE_ERROR, "selection_criterion buffer overflow"); | traceEvent(TRACE_ERROR, "selection_criterion buffer overflow"); | |||
End of changes. 8 change blocks. | ||||
8 lines changed or deleted | 40 lines changed or added |