diff --git a/sys/include/net/fib.h b/sys/include/net/fib.h index a4c51798fa..91230c3030 100644 --- a/sys/include/net/fib.h +++ b/sys/include/net/fib.h @@ -17,6 +17,7 @@ * @brief Types and functions for FIB * * @author Martin Landsmann + * @author Oliver Hahm */ #ifndef FIB_H_ @@ -30,15 +31,6 @@ extern "C" { #endif -#ifndef FIB_MAX_FIB_TABLE_ENTRIES -/** - * @brief maximum number of FIB tables entries handled - * - * Might be overwritten by other modules or the application. - */ -#define FIB_MAX_FIB_TABLE_ENTRIES (20) -#endif - /** * @brief Routing Protocol (RP) message content to request/reply notification */ @@ -72,16 +64,18 @@ typedef struct fib_destination_set_entry_t { /** * @brief initializes all FIB entries with 0 * - * @param[in] table the fib instance to initialize + * @param[in] table the fib instance to initialize + * + * @pre @p table is not NULL and points to a pre-filled struct */ -void fib_init(fib_entry_t table[]); +void fib_init(fib_table_t *table); /** * @brief de-initializes the FIB entries * - * @param[in] table the fib instance to de-initialize + * @param[in] table the fib instance to de-initialize */ -void fib_deinit(fib_entry_t table[]); +void fib_deinit(fib_table_t *table); /** * @brief Registration of a routing protocol handler function @@ -113,7 +107,7 @@ int fib_register_rp(uint8_t *prefix, size_t prefix_addr_type_size); * -ENOMEM if the entry cannot be created due to insufficient RAM * -EFAULT if dst and/or next_hop is not a valid pointer */ -int fib_add_entry(fib_entry_t table[], kernel_pid_t iface_id, uint8_t *dst, +int fib_add_entry(fib_table_t *table, kernel_pid_t iface_id, uint8_t *dst, size_t dst_size, uint32_t dst_flags, uint8_t *next_hop, size_t next_hop_size, uint32_t next_hop_flags, uint32_t lifetime); @@ -133,18 +127,18 @@ int fib_add_entry(fib_entry_t table[], kernel_pid_t iface_id, uint8_t *dst, * -ENOMEM if the entry cannot be updated due to insufficient RAM * -EFAULT if dst and/or next_hop is not a valid pointer */ -int fib_update_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size, - uint8_t *next_hop, size_t next_hop_size, uint32_t next_hop_flags, - uint32_t lifetime); +int fib_update_entry(fib_table_t *table, uint8_t *dst, size_t dst_size, + uint8_t *next_hop, size_t next_hop_size, + uint32_t next_hop_flags, uint32_t lifetime); /** * @brief removes an entry from the corresponding FIB table * - * @param[in] table the fib table containing the entry to remove - * @param[in] dst the destination address - * @param[in] dst_size the destination address size + * @param[in] table the fib table containing the entry to remove + * @param[in] dst the destination address + * @param[in] dst_size the destination address size */ -void fib_remove_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size); +void fib_remove_entry(fib_table_t *table, uint8_t *dst, size_t dst_size); /** * @brief provides a next hop for a given destination @@ -166,9 +160,10 @@ void fib_remove_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size); * -EFAULT if dst and/or next_hop is not a valid pointer * -EINVAL if one of the other passed out pointers is NULL */ -int fib_get_next_hop(fib_entry_t table[], kernel_pid_t *iface_id, - uint8_t *next_hop, size_t *next_hop_size, uint32_t* next_hop_flags, - uint8_t *dst, size_t dst_size, uint32_t dst_flags); +int fib_get_next_hop(fib_table_t *table, kernel_pid_t *iface_id, + uint8_t *next_hop, size_t *next_hop_size, + uint32_t* next_hop_flags, uint8_t *dst, size_t dst_size, + uint32_t dst_flags); /** * @brief provides a set of destination addresses matching the given prefix @@ -189,16 +184,17 @@ int fib_get_next_hop(fib_entry_t table[], kernel_pid_t *iface_id, * The actual needed size is stored then in dst_set_size, * however the required size may change in between calls. */ -int fib_get_destination_set(fib_entry_t table[], uint8_t *prefix, size_t prefix_size, - fib_destination_set_entry_t *dst_set, size_t* dst_set_size); - +int fib_get_destination_set(fib_table_t *table, uint8_t *prefix, + size_t prefix_size, + fib_destination_set_entry_t *dst_set, + size_t *dst_set_size); /** * @brief returns the actual number of used FIB entries * - * @param[in] table the fib instance to check + * @param[in] table the fib instance to check */ -int fib_get_num_used_entries(fib_entry_t table[]); +int fib_get_num_used_entries(fib_table_t *table); /** * @brief Prints the kernel_pid_t for all registered RRPs @@ -208,22 +204,22 @@ void fib_print_notify_rrp(void); /** * @brief Prints the FIB content (does not print the entries) * - * @param[in] table the fib instance to print + * @param[in] table the fib instance to print */ -void fib_print_fib_table(fib_entry_t table[]); +void fib_print_fib_table(fib_table_t *table); /** * @brief Prints the FIB content * - * @param[in] table the fib instance to print + * @param[in] table the fib instance to print */ -void fib_print_routes(fib_entry_t table[]); +void fib_print_routes(fib_table_t *table); #if FIB_DEVEL_HELPER /** * @brief get the point in time at which the entry for destination dst expires. * - * @param[in] table the fib instance to check + * @param[in] table the fib instance to check * @param[out] lifetime pointer where the expiration time is written on success * @param[in] dst the destination address * @param[in] dst_size the destination address size @@ -231,7 +227,9 @@ void fib_print_routes(fib_entry_t table[]); * @return 0 on success: entry for dst found and lifetime copied * -EHOSTUNREACH if no fitting entry is available */ -int fib_devel_get_lifetime(fib_entry_t table[], timex_t *lifetime, uint8_t *dst, size_t dst_size); +int fib_devel_get_lifetime(fib_table_t *table, timex_t *lifetime, uint8_t *dst, + size_t dst_size); + #endif #ifdef __cplusplus diff --git a/sys/include/net/fib/table.h b/sys/include/net/fib/table.h index 1986f4b9ab..262e079025 100644 --- a/sys/include/net/fib/table.h +++ b/sys/include/net/fib/table.h @@ -28,8 +28,8 @@ extern "C" { #endif /** -* @brief Container descriptor for a FIB entry -*/ + * @brief Container descriptor for a FIB entry + */ typedef struct fib_entry_t { /** interface ID */ kernel_pid_t iface_id; @@ -45,6 +45,14 @@ typedef struct fib_entry_t { struct universal_address_container_t *next_hop; } fib_entry_t; +/** + * @brief Meta information about the FIB table + */ +typedef struct { + fib_entry_t *entries; /**< array holding the FIB entries */ + size_t size; /**< number of entries in this table */ +} fib_table_t; + #ifdef __cplusplus } #endif diff --git a/sys/include/net/gnrc/ipv6.h b/sys/include/net/gnrc/ipv6.h index 3d63a9c367..b265097205 100644 --- a/sys/include/net/gnrc/ipv6.h +++ b/sys/include/net/gnrc/ipv6.h @@ -22,6 +22,7 @@ * @brief Definitions for IPv6 * * @author Martine Lenders + * @author Oliver Hahm */ @@ -78,12 +79,18 @@ extern "C" { extern kernel_pid_t gnrc_ipv6_pid; #ifdef MODULE_FIB + +/** + * @brief Maximum number of entries in the IPv6 FIB table. + */ +#define GNRC_IPV6_FIB_TABLE_SIZE (20) + /** * @brief The forwarding information base (FIB) for the IPv6 stack. * * @see @ref net_fib */ -extern fib_entry_t gnrc_ipv6_fib_table[FIB_MAX_FIB_TABLE_ENTRIES]; +extern fib_table_t gnrc_ipv6_fib_table; #endif /** diff --git a/sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c b/sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c index 5e429a2baa..37347db8d1 100644 --- a/sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c +++ b/sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c @@ -45,10 +45,15 @@ static char _stack[GNRC_IPV6_STACK_SIZE]; #ifdef MODULE_FIB #include "net/fib.h" #include "net/fib/table.h" +/** + * @brief buffer to store the entries in the IPv6 forwarding table + */ +static fib_entry_t _fib_entries[GNRC_IPV6_FIB_TABLE_SIZE]; + /** * @brief the IPv6 forwarding table */ -fib_entry_t gnrc_ipv6_fib_table[FIB_MAX_FIB_TABLE_ENTRIES]; +fib_table_t gnrc_ipv6_fib_table; #endif #if ENABLE_DEBUG @@ -80,7 +85,9 @@ kernel_pid_t gnrc_ipv6_init(void) } #ifdef MODULE_FIB - fib_init(gnrc_ipv6_fib_table); + gnrc_ipv6_fib_table.entries = _fib_entries; + gnrc_ipv6_fib_table.size = GNRC_IPV6_FIB_TABLE_SIZE; + fib_init(&gnrc_ipv6_fib_table); #endif return gnrc_ipv6_pid; diff --git a/sys/net/gnrc/network_layer/ndp/node/gnrc_ndp_node.c b/sys/net/gnrc/network_layer/ndp/node/gnrc_ndp_node.c index a3698537c8..9b37b4d02d 100644 --- a/sys/net/gnrc/network_layer/ndp/node/gnrc_ndp_node.c +++ b/sys/net/gnrc/network_layer/ndp/node/gnrc_ndp_node.c @@ -78,8 +78,8 @@ kernel_pid_t gnrc_ndp_node_next_hop_l2addr(uint8_t *l2addr, uint8_t *l2addr_len, ipv6_addr_t next_hop_actual; /* FIB copies address into this variable */ if ((next_hop_ip == NULL) && - (fib_get_next_hop(gnrc_ipv6_fib_table, &iface, next_hop_actual.u8, &next_hop_size, - &next_hop_flags, (uint8_t *)dst, + (fib_get_next_hop(&gnrc_ipv6_fib_table, &iface, next_hop_actual.u8, + &next_hop_size, &next_hop_flags, (uint8_t *)dst, sizeof(ipv6_addr_t), 0) >= 0) && (next_hop_size == sizeof(ipv6_addr_t))) { next_hop_ip = &next_hop_actual; diff --git a/sys/net/gnrc/routing/rpl/gnrc_rpl_control_messages.c b/sys/net/gnrc/routing/rpl/gnrc_rpl_control_messages.c index 95afa25861..3fdeb9fca1 100644 --- a/sys/net/gnrc/routing/rpl/gnrc_rpl_control_messages.c +++ b/sys/net/gnrc/routing/rpl/gnrc_rpl_control_messages.c @@ -307,7 +307,7 @@ bool _parse_options(int msg_type, gnrc_rpl_dodag_t *dodag, gnrc_rpl_opt_t *opt, first_target = target; } - fib_add_entry(gnrc_ipv6_fib_table, if_id, target->target.u8, + fib_add_entry(&gnrc_ipv6_fib_table, if_id, target->target.u8, sizeof(ipv6_addr_t), AF_INET6, src->u8, sizeof(ipv6_addr_t), AF_INET6, (dodag->default_lifetime * dodag->lifetime_unit) * @@ -328,10 +328,12 @@ a preceding RPL TARGET DAO option\n"); } do { - fib_update_entry(gnrc_ipv6_fib_table, first_target->target.u8, + fib_update_entry(&gnrc_ipv6_fib_table, + first_target->target.u8, sizeof(ipv6_addr_t), src->u8, sizeof(ipv6_addr_t), AF_INET6, - (transit->path_lifetime * dodag->lifetime_unit * SEC_IN_MS)); + (transit->path_lifetime * + dodag->lifetime_unit * SEC_IN_MS)); first_target = (gnrc_rpl_opt_target_t *) (((uint8_t *) (first_target)) + sizeof(gnrc_rpl_opt_t) + first_target->length); } @@ -530,7 +532,8 @@ void gnrc_rpl_send_DAO(gnrc_rpl_dodag_t *dodag, ipv6_addr_t *destination, uint8_ /* find prefix for my address */ ipv6_addr_t prefix; ipv6_addr_init_prefix(&prefix, me, me_netif->prefix_len); - fib_get_destination_set(gnrc_ipv6_fib_table, prefix.u8, sizeof(ipv6_addr_t), fib_dest_set, &dst_size); + fib_get_destination_set(&gnrc_ipv6_fib_table, prefix.u8, + sizeof(ipv6_addr_t), fib_dest_set, &dst_size); int size = sizeof(icmpv6_hdr_t) + sizeof(gnrc_rpl_dao_t) + (sizeof(gnrc_rpl_opt_target_t) * (dst_size + 1)) + sizeof(gnrc_rpl_opt_transit_t); diff --git a/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c b/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c index 185f1d1281..7fb585934f 100644 --- a/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c +++ b/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c @@ -264,12 +264,11 @@ bool gnrc_rpl_parent_add_by_addr(gnrc_rpl_dodag_t *dodag, ipv6_addr_t *addr, gnr DEBUG("RPL: no interface found for the parent addres\n"); return false; } - if (fib_add_entry(gnrc_ipv6_fib_table, if_id, def.u8, + if (fib_add_entry(&gnrc_ipv6_fib_table, if_id, def.u8, sizeof(ipv6_addr_t), AF_INET6, dodag->parents->addr.u8, sizeof(ipv6_addr_t), - AF_INET6, - (dodag->default_lifetime * dodag->lifetime_unit) * - SEC_IN_MS) != 0) { + AF_INET6, (dodag->default_lifetime * + dodag->lifetime_unit) * SEC_IN_MS) != 0) { DEBUG("RPL: error adding parent to FIB\n"); gnrc_rpl_parent_remove(*parent); return false; @@ -304,7 +303,7 @@ bool gnrc_rpl_parent_remove(gnrc_rpl_parent_t *parent) { if (parent == parent->dodag->parents) { ipv6_addr_t def = { .u64 = {{0}, {0}} }; - fib_remove_entry(gnrc_ipv6_fib_table, def.u8, sizeof(ipv6_addr_t)); + fib_remove_entry(&gnrc_ipv6_fib_table, def.u8, sizeof(ipv6_addr_t)); } gnrc_rpl_dodag_t *dodag = parent->dodag; LL_DELETE(dodag->parents, parent); @@ -321,7 +320,7 @@ void gnrc_rpl_local_repair(gnrc_rpl_dodag_t *dodag) if (dodag->parents) { gnrc_rpl_dodag_remove_all_parents(dodag); ipv6_addr_t def = IPV6_ADDR_UNSPECIFIED; - fib_remove_entry(gnrc_ipv6_fib_table, def.u8, sizeof(ipv6_addr_t)); + fib_remove_entry(&gnrc_ipv6_fib_table, def.u8, sizeof(ipv6_addr_t)); } if (dodag->my_rank != GNRC_RPL_INFINITE_RANK) { @@ -349,9 +348,11 @@ void gnrc_rpl_parent_update(gnrc_rpl_dodag_t *dodag, gnrc_rpl_parent_t *parent) ipv6_addr_t all_RPL_nodes = GNRC_RPL_ALL_NODES_ADDR; kernel_pid_t if_id; if ((if_id = gnrc_ipv6_netif_find_by_addr(NULL, &all_RPL_nodes)) != KERNEL_PID_UNDEF) { - fib_add_entry(gnrc_ipv6_fib_table, if_id, def.u8, sizeof(ipv6_addr_t), AF_INET6, - dodag->parents->addr.u8, sizeof(ipv6_addr_t), AF_INET6, - (dodag->default_lifetime * dodag->lifetime_unit) * SEC_IN_MS); + fib_add_entry(&gnrc_ipv6_fib_table, if_id, def.u8, + sizeof(ipv6_addr_t), AF_INET6, + dodag->parents->addr.u8, sizeof(ipv6_addr_t), + AF_INET6, (dodag->default_lifetime * + dodag->lifetime_unit) * SEC_IN_MS); } } } @@ -404,7 +405,7 @@ static gnrc_rpl_parent_t *_gnrc_rpl_find_preferred_parent(gnrc_rpl_dodag_t *doda } trickle_reset_timer(&dodag->trickle); - fib_remove_entry(gnrc_ipv6_fib_table, def.u8, sizeof(ipv6_addr_t)); + fib_remove_entry(&gnrc_ipv6_fib_table, def.u8, sizeof(ipv6_addr_t)); ipv6_addr_t all_RPL_nodes = GNRC_RPL_ALL_NODES_ADDR; kernel_pid_t if_id = gnrc_ipv6_netif_find_by_addr(NULL, &all_RPL_nodes); @@ -414,10 +415,10 @@ static gnrc_rpl_parent_t *_gnrc_rpl_find_preferred_parent(gnrc_rpl_dodag_t *doda return NULL; } - fib_add_entry(gnrc_ipv6_fib_table, if_id, def.u8, sizeof(ipv6_addr_t), - AF_INET6, dodag->parents->addr.u8, sizeof(ipv6_addr_t), - AF_INET6, - (dodag->default_lifetime * dodag->lifetime_unit) * SEC_IN_MS); + fib_add_entry(&gnrc_ipv6_fib_table, if_id, def.u8, sizeof(ipv6_addr_t), + AF_INET6, dodag->parents->addr.u8, sizeof(ipv6_addr_t), + AF_INET6, (dodag->default_lifetime * + dodag->lifetime_unit) * SEC_IN_MS); } return dodag->parents; diff --git a/sys/net/network_layer/fib/fib.c b/sys/net/network_layer/fib/fib.c index 4a94cf3897..a7bb3611e5 100644 --- a/sys/net/network_layer/fib/fib.c +++ b/sys/net/network_layer/fib/fib.c @@ -14,6 +14,7 @@ * @brief Functions to manage FIB entries * * @author Martin Landsmann + * @author Oliver Hahm * * @} */ @@ -92,6 +93,7 @@ static void fib_ms_to_timex(uint32_t ms, timex_t *timex) /** * @brief returns pointer to the entry for the given destination address * + * @param[in] table the FIB table to search in * @param[in] dst the destination address * @param[in] dst_size the destination address size * @param[out] entry_arr the array to scribe the found match @@ -102,9 +104,8 @@ static void fib_ms_to_timex(uint32_t ms, timex_t *timex) * 1 if we found the exact address next-hop * -EHOSTUNREACH if no fitting next-hop is available */ -static int fib_find_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size, - fib_entry_t **entry_arr, size_t *entry_arr_size) -{ +static int fib_find_entry(fib_table_t *table, uint8_t *dst, size_t dst_size, + fib_entry_t **entry_arr, size_t *entry_arr_size) { timex_t now; vtimer_now(&now); @@ -121,39 +122,39 @@ static int fib_find_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size, } } - for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { + for (size_t i = 0; i < table->size; ++i) { /* autoinvalidate if the entry lifetime is not set to not expire */ - if ((table[i].lifetime.seconds != FIB_LIFETIME_NO_EXPIRE) - || (table[i].lifetime.microseconds != FIB_LIFETIME_NO_EXPIRE)) { + if ((table->entries[i].lifetime.seconds != FIB_LIFETIME_NO_EXPIRE) + || (table->entries[i].lifetime.microseconds != FIB_LIFETIME_NO_EXPIRE)) { /* check if the lifetime expired */ - if (timex_cmp(now, table[i].lifetime) > -1) { + if (timex_cmp(now, table->entries[i].lifetime) > -1) { /* remove this entry if its lifetime expired */ - table[i].lifetime.seconds = 0; - table[i].lifetime.microseconds = 0; - table[i].global_flags = 0; - table[i].next_hop_flags = 0; - table[i].iface_id = KERNEL_PID_UNDEF; + table->entries[i].lifetime.seconds = 0; + table->entries[i].lifetime.microseconds = 0; + table->entries[i].global_flags = 0; + table->entries[i].next_hop_flags = 0; + table->entries[i].iface_id = KERNEL_PID_UNDEF; - if (table[i].global != NULL) { - universal_address_rem(table[i].global); - table[i].global = NULL; + if (table->entries[i].global != NULL) { + universal_address_rem(table->entries[i].global); + table->entries[i].global = NULL; } - if (table[i].next_hop != NULL) { - universal_address_rem(table[i].next_hop); - table[i].next_hop = NULL; + if (table->entries[i].next_hop != NULL) { + universal_address_rem(table->entries[i].next_hop); + table->entries[i].next_hop = NULL; } } } - if ((prefix_size < (dst_size<<3)) && (table[i].global != NULL)) { + if ((prefix_size < (dst_size<<3)) && (table->entries[i].global != NULL)) { - int ret_comp = universal_address_compare(table[i].global, dst, &match_size); + int ret_comp = universal_address_compare(table->entries[i].global, dst, &match_size); /* If we found an exact match */ if (ret_comp == 0 || (is_all_zeros_addr && match_size == 0)) { - entry_arr[0] = &(table[i]); + entry_arr[0] = &(table->entries[i]); *entry_arr_size = 1; /* we will not find a better one so we return */ return 1; @@ -161,7 +162,7 @@ static int fib_find_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size, else { /* we try to find the most fitting prefix */ if (ret_comp == 1) { - entry_arr[0] = &(table[i]); + entry_arr[0] = &(table->entries[i]); /* we could find a better one so we move on */ ret = 0; @@ -189,8 +190,8 @@ static int fib_find_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size, * @return 0 if the entry has been updated * -ENOMEM if the entry cannot be updated due to insufficient RAM */ -static int fib_upd_entry(fib_entry_t *entry, - uint8_t *next_hop, size_t next_hop_size, uint32_t next_hop_flags, +static int fib_upd_entry(fib_entry_t *entry, uint8_t *next_hop, + size_t next_hop_size, uint32_t next_hop_flags, uint32_t lifetime) { universal_address_container_t *container = universal_address_add(next_hop, next_hop_size); @@ -217,6 +218,7 @@ static int fib_upd_entry(fib_entry_t *entry, /** * @brief creates a new FIB entry with the provided parameters * + * @param[in] table the FIB table to create the entry in * @param[in] iface_id the interface ID * @param[in] dst the destination address * @param[in] dst_size the destination address size @@ -229,32 +231,33 @@ static int fib_upd_entry(fib_entry_t *entry, * @return 0 on success * -ENOMEM if no new entry can be created */ -static int fib_create_entry(fib_entry_t table[], kernel_pid_t iface_id, +static int fib_create_entry(fib_table_t *table, kernel_pid_t iface_id, uint8_t *dst, size_t dst_size, uint32_t dst_flags, - uint8_t *next_hop, size_t next_hop_size, uint32_t next_hop_flags, - uint32_t lifetime) + uint8_t *next_hop, size_t next_hop_size, uint32_t + next_hop_flags, uint32_t lifetime) { - for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { - if (table[i].lifetime.seconds == 0 && table[i].lifetime.microseconds == 0) { + for (size_t i = 0; i < table->size; ++i) { + if ((table->entries[i].lifetime.seconds == 0) && + (table->entries[i].lifetime.microseconds == 0)) { - table[i].global = universal_address_add(dst, dst_size); + table->entries[i].global = universal_address_add(dst, dst_size); - if (table[i].global != NULL) { - table[i].global_flags = dst_flags; - table[i].next_hop = universal_address_add(next_hop, next_hop_size); - table[i].next_hop_flags = next_hop_flags; + if (table->entries[i].global != NULL) { + table->entries[i].global_flags = dst_flags; + table->entries[i].next_hop = universal_address_add(next_hop, next_hop_size); + table->entries[i].next_hop_flags = next_hop_flags; } - if (table[i].next_hop != NULL) { + if (table->entries[i].next_hop != NULL) { /* everything worked fine */ - table[i].iface_id = iface_id; + table->entries[i].iface_id = iface_id; if (lifetime < FIB_LIFETIME_NO_EXPIRE) { - fib_ms_to_timex(lifetime, &table[i].lifetime); + fib_ms_to_timex(lifetime, &table->entries[i].lifetime); } else { - table[i].lifetime.seconds = FIB_LIFETIME_NO_EXPIRE; - table[i].lifetime.microseconds = FIB_LIFETIME_NO_EXPIRE; + table->entries[i].lifetime.seconds = FIB_LIFETIME_NO_EXPIRE; + table->entries[i].lifetime.microseconds = FIB_LIFETIME_NO_EXPIRE; } return 0; @@ -340,10 +343,10 @@ static int fib_signal_rp(uint8_t *dst, size_t dst_size, uint32_t dst_flags) return ret; } -int fib_add_entry(fib_entry_t table[], kernel_pid_t iface_id, uint8_t *dst, - size_t dst_size, uint32_t dst_flags, uint8_t *next_hop, - size_t next_hop_size, uint32_t next_hop_flags, - uint32_t lifetime) +int fib_add_entry(fib_table_t *table, + kernel_pid_t iface_id, uint8_t *dst, size_t dst_size, + uint32_t dst_flags, uint8_t *next_hop, size_t next_hop_size, + uint32_t next_hop_flags, uint32_t lifetime) { mutex_lock(&mtx_access); DEBUG("[fib_add_entry]\n"); @@ -371,9 +374,9 @@ int fib_add_entry(fib_entry_t table[], kernel_pid_t iface_id, uint8_t *dst, return ret; } -int fib_update_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size, - uint8_t *next_hop, size_t next_hop_size, uint32_t next_hop_flags, - uint32_t lifetime) +int fib_update_entry(fib_table_t *table, uint8_t *dst, size_t dst_size, + uint8_t *next_hop, size_t next_hop_size, + uint32_t next_hop_flags, uint32_t lifetime) { mutex_lock(&mtx_access); DEBUG("[fib_update_entry]\n"); @@ -403,7 +406,7 @@ int fib_update_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size, return ret; } -void fib_remove_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size) +void fib_remove_entry(fib_table_t *table, uint8_t *dst, size_t dst_size) { mutex_lock(&mtx_access); DEBUG("[fib_remove_entry]\n"); @@ -426,9 +429,10 @@ void fib_remove_entry(fib_entry_t table[], uint8_t *dst, size_t dst_size) mutex_unlock(&mtx_access); } -int fib_get_next_hop(fib_entry_t table[], kernel_pid_t *iface_id, - uint8_t *next_hop, size_t *next_hop_size, uint32_t *next_hop_flags, - uint8_t *dst, size_t dst_size, uint32_t dst_flags) +int fib_get_next_hop(fib_table_t *table, kernel_pid_t *iface_id, + uint8_t *next_hop, size_t *next_hop_size, + uint32_t *next_hop_flags, uint8_t *dst, size_t dst_size, + uint32_t dst_flags) { mutex_lock(&mtx_access); DEBUG("[fib_get_next_hop]\n"); @@ -478,20 +482,22 @@ int fib_get_next_hop(fib_entry_t table[], kernel_pid_t *iface_id, return 0; } -int fib_get_destination_set(fib_entry_t table[], uint8_t *prefix, size_t prefix_size, - fib_destination_set_entry_t *dst_set, size_t* dst_set_size) +int fib_get_destination_set(fib_table_t *table, uint8_t *prefix, + size_t prefix_size, + fib_destination_set_entry_t *dst_set, + size_t* dst_set_size) { mutex_lock(&mtx_access); int ret = -EHOSTUNREACH; size_t found_entries = 0; - for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { - if ((table[i].global != NULL) && - (universal_address_compare_prefix(table[i].global, prefix, prefix_size<<3) >= 0)) { + for (size_t i = 0; i < table->size; ++i) { + if ((table->entries[i].global != NULL) && + (universal_address_compare_prefix(table->entries[i].global, prefix, prefix_size<<3) >= 0)) { if( (dst_set != NULL) && (found_entries < *dst_set_size) ) { /* set the size to full byte usage */ dst_set[found_entries].dest_size = sizeof(dst_set[found_entries].dest); - universal_address_get_address(table[i].global, + universal_address_get_address(table->entries[i].global, dst_set[found_entries].dest, &dst_set[found_entries].dest_size); } @@ -512,7 +518,7 @@ int fib_get_destination_set(fib_entry_t table[], uint8_t *prefix, size_t prefix_ return ret; } -void fib_init(fib_entry_t table[]) +void fib_init(fib_table_t *table) { DEBUG("[fib_init] hello. Initializing some stuff.\n"); mutex_lock(&mtx_access); @@ -522,21 +528,21 @@ void fib_init(fib_entry_t table[]) prefix_rp[i] = NULL; } - for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { - table[i].iface_id = 0; - table[i].lifetime.seconds = 0; - table[i].lifetime.microseconds = 0; - table[i].global_flags = 0; - table[i].global = NULL; - table[i].next_hop_flags = 0; - table[i].next_hop = NULL; + for (size_t i = 0; i < table->size; ++i) { + table->entries[i].iface_id = 0; + table->entries[i].lifetime.seconds = 0; + table->entries[i].lifetime.microseconds = 0; + table->entries[i].global_flags = 0; + table->entries[i].global = NULL; + table->entries[i].next_hop_flags = 0; + table->entries[i].next_hop = NULL; } universal_address_init(); mutex_unlock(&mtx_access); } -void fib_deinit(fib_entry_t table[]) +void fib_deinit(fib_table_t *table) { DEBUG("[fib_deinit] hello. De-Initializing stuff.\n"); mutex_lock(&mtx_access); @@ -548,14 +554,14 @@ void fib_deinit(fib_entry_t table[]) notify_rp_pos = 0; - for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { - table[i].iface_id = 0; - table[i].lifetime.seconds = 0; - table[i].lifetime.microseconds = 0; - table[i].global_flags = 0; - table[i].global = NULL; - table[i].next_hop_flags = 0; - table[i].next_hop = NULL; + for (size_t i = 0; i < table->size; ++i) { + table->entries[i].iface_id = 0; + table->entries[i].lifetime.seconds = 0; + table->entries[i].lifetime.microseconds = 0; + table->entries[i].global_flags = 0; + table->entries[i].global = NULL; + table->entries[i].next_hop_flags = 0; + table->entries[i].next_hop = NULL; } universal_address_reset(); @@ -587,13 +593,13 @@ int fib_register_rp(uint8_t *prefix, size_t prefix_addr_type_size) return 0; } -int fib_get_num_used_entries(fib_entry_t table[]) +int fib_get_num_used_entries(fib_table_t *table) { mutex_lock(&mtx_access); size_t used_entries = 0; - for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { - used_entries += (size_t)(table[i].global != NULL); + for (size_t i = 0; i < table->size; ++i) { + used_entries += (size_t)(table->entries[i].global != NULL); } mutex_unlock(&mtx_access); @@ -613,18 +619,17 @@ void fib_print_notify_rp(void) mutex_unlock(&mtx_access); } -void fib_print_fib_table(fib_entry_t table[]) +void fib_print_fib_table(fib_table_t *table) { mutex_lock(&mtx_access); - for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { - printf("[fib_print_table] %d) iface_id: %d, global: %p, next hop: %p, lifetime: %d.%d\n", \ - (int)i, \ - (int)table[i].iface_id, \ - (void *)table[i].global, \ - (void *)table[i].next_hop, \ - (int)table[i].lifetime.seconds, \ - (int)table[i].lifetime.microseconds); + for (size_t i = 0; i < table->size; ++i) { + printf("[fib_print_table] %d) iface_id: %d, global: %p, next hop: %p, lifetime: %d.%d\n", + (int)i, (int)table->entries[i].iface_id, + (void *)table->entries[i].global, + (void *)table->entries[i].next_hop, + (int)table->entries[i].lifetime.seconds, + (int)table->entries[i].lifetime.microseconds); } mutex_unlock(&mtx_access); @@ -661,7 +666,7 @@ static void fib_print_address(universal_address_container_t *entry) } } -void fib_print_routes(fib_entry_t table[]) +void fib_print_routes(fib_table_t *table) { mutex_lock(&mtx_access); printf("%-" FIB_ADDR_PRINT_LENS "s %-6s %-" FIB_ADDR_PRINT_LENS "s %-6s %-16s Interface\n" @@ -670,17 +675,17 @@ void fib_print_routes(fib_entry_t table[]) timex_t now; vtimer_now(&now); - for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { - if (table[i].lifetime.seconds != 0 || table[i].lifetime.microseconds != 0) { - fib_print_address(table[i].global); - printf(" 0x%04"PRIx32" ", table[i].global_flags); - fib_print_address(table[i].next_hop); - printf(" 0x%04"PRIx32" ", table[i].next_hop_flags); + for (size_t i = 0; i < table->size; ++i) { + if (table->entries[i].lifetime.seconds != 0 || table->entries[i].lifetime.microseconds != 0) { + fib_print_address(table->entries[i].global); + printf(" 0x%04"PRIx32" ", table->entries[i].global_flags); + fib_print_address(table->entries[i].next_hop); + printf(" 0x%04"PRIx32" ", table->entries[i].next_hop_flags); - if ((table[i].lifetime.seconds != FIB_LIFETIME_NO_EXPIRE) - || (table[i].lifetime.microseconds != FIB_LIFETIME_NO_EXPIRE)) { + if ((table->entries[i].lifetime.seconds != FIB_LIFETIME_NO_EXPIRE) + || (table->entries[i].lifetime.microseconds != FIB_LIFETIME_NO_EXPIRE)) { - timex_t tm = timex_sub(table[i].lifetime, now); + timex_t tm = timex_sub(table->entries[i].lifetime, now); /* we must interpret the values as signed */ if ((int32_t)tm.seconds < 0 @@ -695,7 +700,7 @@ void fib_print_routes(fib_entry_t table[]) printf("%-16s ", "NEVER"); } - printf("%d\n", (int)table[i].iface_id); + printf("%d\n", (int)table->entries[i].iface_id); } } @@ -703,7 +708,8 @@ void fib_print_routes(fib_entry_t table[]) } #if FIB_DEVEL_HELPER -int fib_devel_get_lifetime(fib_entry_t table[], timex_t *lifetime, uint8_t *dst, size_t dst_size) +int fib_devel_get_lifetime(fib_table_t *table, timex_t *lifetime, uint8_t *dst, + size_t dst_size) { size_t count = 1; fib_entry_t *entry[count]; diff --git a/sys/shell/commands/sc_fib.c b/sys/shell/commands/sc_fib.c index 563aafcd29..5debb6fdbc 100644 --- a/sys/shell/commands/sc_fib.c +++ b/sys/shell/commands/sc_fib.c @@ -105,14 +105,15 @@ static void _fib_add(const char *dest, const char *next, kernel_pid_t pid, uint3 nxt_flags = AF_INET; } - fib_add_entry(gnrc_ipv6_fib_table, pid, dst, dst_size, dst_flags, nxt, nxt_size, nxt_flags, lifetime); + fib_add_entry(&gnrc_ipv6_fib_table, pid, dst, dst_size, dst_flags, nxt, + nxt_size, nxt_flags, lifetime); } int _fib_route_handler(int argc, char **argv) { /* e.g. fibroute right now dont care about the adress/protocol family */ if (argc == 1) { - fib_print_routes(gnrc_ipv6_fib_table); + fib_print_routes(&gnrc_ipv6_fib_table); return 0; } @@ -139,13 +140,14 @@ int _fib_route_handler(int argc, char **argv) /* e.g. fibroute del */ if (argc == 3) { if (inet_pton(AF_INET6, argv[2], tmp_ipv6_dst)) { - fib_remove_entry(gnrc_ipv6_fib_table, tmp_ipv6_dst, IN6ADDRSZ); + fib_remove_entry(&gnrc_ipv6_fib_table, tmp_ipv6_dst, IN6ADDRSZ); } else if (inet_pton(AF_INET, argv[2], tmp_ipv4_dst)) { - fib_remove_entry(gnrc_ipv6_fib_table, tmp_ipv4_dst, INADDRSZ); + fib_remove_entry(&gnrc_ipv6_fib_table, tmp_ipv4_dst, INADDRSZ); } else { - fib_remove_entry(gnrc_ipv6_fib_table, (uint8_t *)argv[2], (strlen(argv[2]))); + fib_remove_entry(&gnrc_ipv6_fib_table, (uint8_t *)argv[2], + (strlen(argv[2]))); } return 0; diff --git a/tests/unittests/tests-fib/tests-fib.c b/tests/unittests/tests-fib/tests-fib.c index 61280c3604..52edd5384a 100644 --- a/tests/unittests/tests-fib/tests-fib.c +++ b/tests/unittests/tests-fib/tests-fib.c @@ -19,7 +19,9 @@ #include "net/fib.h" #include "universal_address.h" -static fib_entry_t test_fib_table[20]; +#define TEST_FIB_TABLE_SIZE (20) +static fib_entry_t _entries[TEST_FIB_TABLE_SIZE]; +static fib_table_t test_fib_table = { _entries, TEST_FIB_TABLE_SIZE }; /* * @brief helper to fill FIB with unique entries @@ -37,8 +39,10 @@ static void _fill_FIB_unique(size_t entries) snprintf(addr_dst, add_buf_size, "Test address %02d", (int)i); snprintf(addr_nxt, add_buf_size, "Test address %02d", entries + i); /* the terminating \0 is unnecessary here */ - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, addr_dst_flags, - (uint8_t *)addr_nxt, add_buf_size - 1, addr_nxt_flags, 10000); + fib_add_entry(&test_fib_table, 42, + (uint8_t *)addr_dst, add_buf_size - 1, addr_dst_flags, + (uint8_t *)addr_nxt, add_buf_size - 1, addr_nxt_flags, + 10000); } } @@ -58,8 +62,10 @@ static void _fill_FIB_multiple(size_t entries, size_t modulus) /* construct "addresses" for the FIB */ snprintf(addr_dst, add_buf_size, "Test address %02d", (int)i); snprintf(addr_nxt, add_buf_size, "Test address %02d", i % modulus); - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, addr_dst_flags, - (uint8_t *)addr_nxt, add_buf_size - 1, addr_nxt_flags, 10000); + fib_add_entry(&test_fib_table, 42, + (uint8_t *)addr_dst, add_buf_size - 1, addr_dst_flags, + (uint8_t *)addr_nxt, add_buf_size - 1, addr_nxt_flags, + 10000); } } @@ -71,14 +77,14 @@ static void test_fib_01_fill_unique_entries(void) { _fill_FIB_unique(20); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(40, universal_address_get_num_used_entries()); - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -91,14 +97,14 @@ static void test_fib_02_fill_multiple_entries(void) _fill_FIB_multiple(entries, 11); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -113,26 +119,26 @@ static void test_fib_03_removing_all_entries(void) size_t entries = 20; _fill_FIB_unique(entries); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(40, universal_address_get_num_used_entries()); for (size_t i = 0; i < entries; ++i) { /* construct "addresses" to remove */ snprintf(addr_dst, add_buf_size, "Test address %02d", (int)i); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); } - TEST_ASSERT_EQUAL_INT(0, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(0, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(0, universal_address_get_num_used_entries()); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -147,24 +153,24 @@ static void test_fib_04_remove_lower_half(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); for (size_t i = 0; i < entries / 2; ++i) { /* construct "addresses" to remove */ snprintf(addr_dst, add_buf_size, "Test address %02d", (int)i); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); } #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - TEST_ASSERT_EQUAL_INT(10, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(10, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(19, universal_address_get_num_used_entries()); - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -179,24 +185,25 @@ static void test_fib_05_remove_upper_half(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); for (size_t i = 0; i < entries / 2; ++i) { /* construct "addresses" to remove */ snprintf(addr_dst, add_buf_size, "Test address %02d", ((entries / 2) + i)); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); } #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - TEST_ASSERT_EQUAL_INT(10, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(10, fib_get_num_used_entries(&test_fib_table)); + TEST_ASSERT_EQUAL_INT(10, universal_address_get_num_used_entries()); - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -213,20 +220,20 @@ static void test_fib_06_remove_one_entry(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst,add_buf_size - 1); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - TEST_ASSERT_EQUAL_INT(19, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(19, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -242,23 +249,23 @@ static void test_fib_07_remove_one_entry_multiple_times(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - TEST_ASSERT_EQUAL_INT(19, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(19, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(19, universal_address_get_num_used_entries()); - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -273,22 +280,22 @@ static void test_fib_08_remove_unknown(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, add_buf_size - 1); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -307,26 +314,29 @@ static void test_fib_09_update_entry(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - fib_update_entry(test_fib_table, (uint8_t *)addr_dst13, add_buf_size - 1, - (uint8_t *)addr_nxt2, add_buf_size - 1, 0x99, 9999); - fib_update_entry(test_fib_table, (uint8_t *)addr_dst07, add_buf_size - 1, - (uint8_t *)addr_nxt77, add_buf_size - 1, 0x77, 7777); + fib_update_entry(&test_fib_table, (uint8_t *)addr_dst13, + add_buf_size - 1, (uint8_t *)addr_nxt2, add_buf_size - 1, + 0x99, 9999); + fib_update_entry(&test_fib_table, (uint8_t *)addr_dst07, + add_buf_size - 1, (uint8_t *)addr_nxt77, add_buf_size - 1, + 0x77, 7777); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* * @brief filling the FIB with entries and adding an additional one (not fitting) -* It is expected to have 20 FIB entries and to receive FPC_ERROR on adding an additional one +* It is expected to have 20 FIB entries and to receive FPC_ERROR on adding an +* additional one */ static void test_fib_10_add_exceed(void) { @@ -337,23 +347,24 @@ static void test_fib_10_add_exceed(void) size_t entries = 20; _fill_FIB_unique(entries); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(40, universal_address_get_num_used_entries()); - int ret = fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, 0x98, - (uint8_t *)addr_nxt, add_buf_size - 1, 0x99, 9999); + int ret = fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size - 1, 0x98, (uint8_t *)addr_nxt, + add_buf_size - 1, 0x99, 9999); TEST_ASSERT_EQUAL_INT(-ENOMEM, ret); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(40, universal_address_get_num_used_entries()); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -372,26 +383,26 @@ static void test_fib_11_get_next_hop_success(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - int ret = fib_get_next_hop(test_fib_table, &iface_id, + int ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, (uint8_t *)addr_dst, add_buf_size - 1, 0x13); TEST_ASSERT_EQUAL_INT(0, ret); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); ret = strncmp(addr_expect, addr_nxt, add_buf_size); TEST_ASSERT_EQUAL_INT(0, ret); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -408,21 +419,21 @@ static void test_fib_12_get_next_hop_fail(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - int ret = fib_get_next_hop(test_fib_table, &iface_id, - (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, + int ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, + &add_buf_size, &next_hop_flags, (uint8_t *)addr_dst, add_buf_size - 1, 0x99); TEST_ASSERT_EQUAL_INT(-EHOSTUNREACH, ret); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -440,23 +451,23 @@ static void test_fib_13_get_next_hop_fail_on_buffer_size(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(test_fib_table)); + TEST_ASSERT_EQUAL_INT(20, fib_get_num_used_entries(&test_fib_table)); TEST_ASSERT_EQUAL_INT(20, universal_address_get_num_used_entries()); - int ret = fib_get_next_hop(test_fib_table, &iface_id, - (uint8_t *)addr_nxt, &add_buf_size_nxt, &next_hop_flags, + int ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, + &add_buf_size_nxt, &next_hop_flags, (uint8_t *)addr_dst, add_buf_size - 1, 0x13); TEST_ASSERT_EQUAL_INT(-ENOBUFS, ret); TEST_ASSERT_EQUAL_INT(add_buf_size_nxt, add_buf_size - 1); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -477,23 +488,26 @@ static void test_fib_14_exact_and_prefix_match(void) snprintf(addr_dst, add_buf_size, "Test addr12"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 12); - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, 0x12, - (uint8_t *)addr_nxt, add_buf_size - 1, 0x12, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size - 1, 0x12, (uint8_t *)addr_nxt, add_buf_size - 1, + 0x12, 100000); snprintf(addr_dst, add_buf_size, "Test addr123"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 23); - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123, - (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - 1, + 0x23, 100000); snprintf(addr_dst, add_buf_size, "Test addr1234"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 34); - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, 0x1234, - (uint8_t *)addr_nxt, add_buf_size - 1, 0x34, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size - 1, 0x1234, (uint8_t *)addr_nxt, add_buf_size - 1, + 0x34, 100000); memset(addr_lookup, 0, add_buf_size); /* exact match */ snprintf(addr_lookup, add_buf_size, "Test addr123"); - int ret = fib_get_next_hop(test_fib_table, &iface_id, + int ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, (uint8_t *)addr_lookup, add_buf_size - 1, 0x123); @@ -514,7 +528,7 @@ static void test_fib_14_exact_and_prefix_match(void) */ /* cppcheck-suppress redundantCopy */ snprintf(addr_lookup, add_buf_size, "Test addr124"); - ret = fib_get_next_hop(test_fib_table, &iface_id, + ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, (uint8_t *)addr_lookup, add_buf_size - 1, 0x124); @@ -526,12 +540,12 @@ static void test_fib_14_exact_and_prefix_match(void) TEST_ASSERT_EQUAL_INT(0, ret); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } static void test_fib_15_get_lifetime(void) @@ -544,11 +558,14 @@ static void test_fib_15_get_lifetime(void) uint32_t addr_dst_flags = 0x77777777; uint32_t addr_nxt_flags = 0x77777777; - TEST_ASSERT_EQUAL_INT(0, fib_add_entry(test_fib_table, iface_id, (uint8_t *)addr_dst, add_buf_size - 1, + TEST_ASSERT_EQUAL_INT(0, fib_add_entry(&test_fib_table, + iface_id, (uint8_t *)addr_dst, add_buf_size - 1, addr_dst_flags, (uint8_t *)addr_nxt, add_buf_size - 1, addr_nxt_flags, 1000)); - TEST_ASSERT_EQUAL_INT(0, fib_devel_get_lifetime(test_fib_table, &lifetime, (uint8_t *)addr_dst, add_buf_size - 1)); + TEST_ASSERT_EQUAL_INT(0, fib_devel_get_lifetime(&test_fib_table, &lifetime, + (uint8_t *)addr_dst, + add_buf_size - 1)); /* assuming some ms passed during these operations... */ vtimer_now(&now); @@ -558,7 +575,7 @@ static void test_fib_15_get_lifetime(void) /* make sure lifetime hasn't grown magically either */ TEST_ASSERT_EQUAL_INT(-1, timex_cmp(lifetime, cmp_max_lifetime)); - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -586,16 +603,18 @@ static void test_fib_16_prefix_match(void) addr_dst[14] = (char)0x80; /* 1000 0000 */ addr_lookup[14] = (char)0x87; /* 1000 0111 */ - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123, - (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - 1, + 0x23, 100000); addr_dst[14] = (char)0x3c; /* 0011 1100 */ - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123, - (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - 1, + 0x23, 100000); memset(addr_nxt, 0, add_buf_size); - int ret = fib_get_next_hop(test_fib_table, &iface_id, + int ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, (uint8_t *)addr_lookup, add_buf_size - 1, 0x123); @@ -606,12 +625,13 @@ static void test_fib_16_prefix_match(void) addr_lookup[14] = (char)0x34; /* 0011 0100 */ add_buf_size = 16; - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123, - (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - + 1, 0x23, 100000); memset(addr_nxt, 0, add_buf_size); - ret = fib_get_next_hop(test_fib_table, &iface_id, + ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, (uint8_t *)addr_lookup, add_buf_size - 1, 0x123); @@ -623,19 +643,19 @@ static void test_fib_16_prefix_match(void) memset(addr_nxt, 0, add_buf_size); - ret = fib_get_next_hop(test_fib_table, &iface_id, + ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, (uint8_t *)addr_lookup, add_buf_size - 1, 0x123); TEST_ASSERT_EQUAL_INT(0, ret); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } @@ -653,7 +673,8 @@ static void test_fib_17_get_entry_set(void) /* construct "addresses" for the FIB */ snprintf(addr_dst, addr_buf_size, "Test address %02d", (int)i); snprintf(addr_nxt, addr_buf_size, "Test address %02d", i % 11); - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, addr_buf_size - 1, 0x0, + fib_add_entry(&test_fib_table, 42, + (uint8_t *)addr_dst, addr_buf_size - 1, 0x0, (uint8_t *)addr_nxt, addr_buf_size - 1, 0x0, 100000); } @@ -663,7 +684,9 @@ static void test_fib_17_get_entry_set(void) memset(prefix,0, addr_buf_size); snprintf(prefix, addr_buf_size, "Test address 1"); - int ret = fib_get_destination_set(test_fib_table, (uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size); + int ret = fib_get_destination_set(&test_fib_table, + (uint8_t *)prefix, addr_buf_size-1, + &arr_dst[0], &arr_size); TEST_ASSERT_EQUAL_INT(0, ret); /* we should receive 10 entries 10 to 19 */ @@ -673,7 +696,9 @@ static void test_fib_17_get_entry_set(void) memset(prefix,0, addr_buf_size); snprintf(prefix, addr_buf_size, "Test address 0"); - ret = fib_get_destination_set(test_fib_table, (uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size); + ret = fib_get_destination_set(&test_fib_table, + (uint8_t *)prefix, addr_buf_size-1, + &arr_dst[0], &arr_size); TEST_ASSERT_EQUAL_INT(0, ret); /* we should receive 20 entries 0-19 */ @@ -683,7 +708,9 @@ static void test_fib_17_get_entry_set(void) memset(prefix,0, addr_buf_size); snprintf(prefix, addr_buf_size, "Test address"); - ret = fib_get_destination_set(test_fib_table, (uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size); + ret = fib_get_destination_set(&test_fib_table, + (uint8_t *)prefix, addr_buf_size-1, + &arr_dst[0], &arr_size); TEST_ASSERT_EQUAL_INT(0, ret); /* we should receive 20 entries 0-19 */ @@ -699,7 +726,7 @@ static void test_fib_17_get_entry_set(void) } #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -718,12 +745,12 @@ static void test_fib_18_get_next_hop_invalid_parameters(void) size_t entries = 20; _fill_FIB_multiple(entries, 11); - int ret = fib_get_next_hop(test_fib_table, NULL, NULL, NULL, NULL,NULL, - add_buf_size - 1, 0x13); + int ret = fib_get_next_hop(&test_fib_table, NULL, NULL, + NULL, NULL,NULL, add_buf_size - 1, 0x13); TEST_ASSERT_EQUAL_INT(-EINVAL, ret); - ret = fib_get_next_hop(test_fib_table, &iface_id, + ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, (uint8_t *)addr_dst, add_buf_size - 1, 0x13); @@ -733,12 +760,12 @@ static void test_fib_18_get_next_hop_invalid_parameters(void) TEST_ASSERT_EQUAL_INT(0, ret); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -767,13 +794,15 @@ static void test_fib_19_default_gateway(void) } /* add a default gateway entry */ - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size, 0x123, - (uint8_t *)addr_nxt, add_buf_size, 0x23, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size, 0x123, (uint8_t *)addr_nxt, add_buf_size, 0x23, + 100000); /* check if it matches all */ - int ret = fib_get_next_hop(test_fib_table, &iface_id, - (uint8_t *)addr_nxt_hop, &add_buf_size, &next_hop_flags, - (uint8_t *)addr_lookup, add_buf_size, 0x123); + int ret = fib_get_next_hop(&test_fib_table, &iface_id, + (uint8_t *)addr_nxt_hop, &add_buf_size, + &next_hop_flags, (uint8_t *)addr_lookup, + add_buf_size, 0x123); TEST_ASSERT_EQUAL_INT(0, ret); TEST_ASSERT_EQUAL_INT(0, memcmp(addr_nxt, addr_nxt_hop, add_buf_size)); @@ -786,11 +815,12 @@ static void test_fib_19_default_gateway(void) } /* change the default gateway entry */ - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size, 0x123, - (uint8_t *)addr_nxt, add_buf_size, 0x24, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size, 0x123, (uint8_t *)addr_nxt, add_buf_size, 0x24, + 100000); /* and check again if it matches all */ - ret = fib_get_next_hop(test_fib_table, &iface_id, + ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt_hop, &add_buf_size, &next_hop_flags, (uint8_t *)addr_lookup, add_buf_size, 0x123); @@ -798,12 +828,12 @@ static void test_fib_19_default_gateway(void) TEST_ASSERT_EQUAL_INT(0, memcmp(addr_nxt, addr_nxt_hop, add_buf_size)); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } /* @@ -840,18 +870,21 @@ static void test_fib_20_replace_prefix(void) } /* add a prefix entry */ - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size, 0x123, - (uint8_t *)addr_nxt, add_buf_size, 0x23, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size, 0x123, (uint8_t *)addr_nxt, add_buf_size, 0x23, + 100000); /* check if it matches */ - int ret = fib_get_next_hop(test_fib_table, &iface_id, - (uint8_t *)addr_nxt_hop, &add_buf_size, &next_hop_flags, - (uint8_t *)addr_lookup, add_buf_size, 0x123); + int ret = fib_get_next_hop(&test_fib_table, &iface_id, + (uint8_t *)addr_nxt_hop, &add_buf_size, + &next_hop_flags, (uint8_t *)addr_lookup, + add_buf_size, 0x123); TEST_ASSERT_EQUAL_INT(0, ret); TEST_ASSERT_EQUAL_INT(0, memcmp(addr_nxt, addr_nxt_hop, add_buf_size)); - fib_remove_entry(test_fib_table, (uint8_t *)addr_dst, add_buf_size); + fib_remove_entry(&test_fib_table, (uint8_t *)addr_dst, + add_buf_size); memset(addr_nxt_hop, 0, add_buf_size); @@ -866,11 +899,12 @@ static void test_fib_20_replace_prefix(void) } /* change the prefix entry */ - fib_add_entry(test_fib_table, 42, (uint8_t *)addr_dst, add_buf_size, 0x123, - (uint8_t *)addr_nxt, add_buf_size, 0x24, 100000); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, + add_buf_size, 0x123, (uint8_t *)addr_nxt, add_buf_size, 0x24, + 100000); /* and check again if it matches */ - ret = fib_get_next_hop(test_fib_table, &iface_id, + ret = fib_get_next_hop(&test_fib_table, &iface_id, (uint8_t *)addr_nxt_hop, &add_buf_size, &next_hop_flags, (uint8_t *)addr_lookup, add_buf_size, 0x123); @@ -878,17 +912,17 @@ static void test_fib_20_replace_prefix(void) TEST_ASSERT_EQUAL_INT(0, memcmp(addr_nxt, addr_nxt_hop, add_buf_size)); #if (TEST_FIB_SHOW_OUTPUT == 1) - fib_print_fib_table(test_fib_table); + fib_print_fib_table(&test_fib_table); puts(""); universal_address_print_table(); puts(""); #endif - fib_deinit(test_fib_table); + fib_deinit(&test_fib_table); } Test *tests_fib_tests(void) { - fib_init(test_fib_table); + fib_init(&test_fib_table); EMB_UNIT_TESTFIXTURES(fixtures) { new_TestFixture(test_fib_01_fill_unique_entries), new_TestFixture(test_fib_02_fill_multiple_entries),