From af79e547f8635f17218b0ab80a5b41410c7e57ea Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 28 Sep 2012 19:34:49 +0200 Subject: Calculate rxcost --- ffd/ffd.c | 22 +++++++++++++++------- ffd/ffd.h | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ffd/ffd.c b/ffd/ffd.c index 1fc9eed..14d770c 100644 --- a/ffd/ffd.c +++ b/ffd/ffd.c @@ -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) { diff --git a/ffd/ffd.h b/ffd/ffd.h index e29fce6..1ef5494 100644 --- a/ffd/ffd.h +++ b/ffd/ffd.h @@ -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 { -- cgit v1.2.3