diff options
-rw-r--r-- | ffd/ffd.c | 22 | ||||
-rw-r--r-- | ffd/ffd.h | 2 |
2 files changed, 17 insertions, 7 deletions
@@ -167,15 +167,12 @@ static inline ffd_neigh_t* get_neigh(const ffd_iface_t *iface, const eth_addr_t static ffd_neigh_t* get_neigh_create(ffd_iface_t *iface, const eth_addr_t *addr) { ffd_neigh_t *neigh = get_neigh(iface, addr); if (!neigh) { - neigh = malloc(sizeof(ffd_neigh_t)); + neigh = calloc(1, sizeof(ffd_neigh_t)); neigh->next = iface->neigh_list; iface->neigh_list = neigh; - neigh->addr = *addr; - neigh->hello_log = 0; - neigh->hello_interval = 0; - neigh->last_seqno = 0; - neigh->last_hello = (struct timespec){}; + + neigh->txcost = 0xffff; } return neigh; @@ -189,6 +186,17 @@ static void free_neighs(ffd_neigh_t *neigh) { } } +static uint16_t neigh_get_rxcost(ffd_neigh_t *neigh) { + int timediff = timespec_diff(&now, &neigh->last_hello)/10; + int shift = (timediff - neigh->hello_interval/2)/neigh->hello_interval; + int received = __builtin_popcount(neigh->hello_log << shift); + + if (received == 0) + return 0xffff; + else + return (0x1000/received); +} + static void update_netifs() { ffd_iface_t *iface, **cur; @@ -308,7 +316,7 @@ static void handle_tlv_hello(const ffd_tlv_hello_t *tlv_hello, size_t len, const neigh->last_seqno = seqno; neigh->last_hello = now; - fprintf(stderr, "debug: accepted hello, log %04x.\n", neigh->hello_log); + fprintf(stderr, "debug: accepted hello, log %04x, rxcost is %u now.\n", neigh->hello_log, neigh_get_rxcost(neigh)); } static void handle_tlv(ffd_tlv_type_t type, const void *data, size_t len, void *arg) { @@ -50,6 +50,8 @@ typedef struct _ffd_neigh_t { uint16_t hello_interval; uint16_t last_seqno; struct timespec last_hello; + + uint16_t txcost; } ffd_neigh_t; typedef struct _ffd_iface_t { |