Skip to content

Commit

Permalink
[SQUASH ME] changed approach to use a prefix match
Browse files Browse the repository at this point in the history
  • Loading branch information
BytesGalore authored and BytesGalore committed May 11, 2015
1 parent 80c6dae commit cdbca42
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 32 deletions.
11 changes: 5 additions & 6 deletions sys/include/net/ng_fib.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,14 @@ int fib_get_next_hop(kernel_pid_t *iface_id,
uint8_t *dst, size_t dst_size, uint32_t dst_flags);

/**
* @brief provides a set of destination addresses matching the bits specified with
* the provided bitmap and type.
* @brief provides a set of destination addresses matching the given prefix
* If the out buffer is insufficient low or passed as NULL,
* the function will continue to count the number of matching entries
* and provide the number to the caller.
*
* @param[in] bitmap the bitmap to mask the relevant bits
* @param[in] type the type holding the significant bits
* @param[out] dst_set the destination addresses matching the type
* @param[in] prefix the destination address
* @param[in] prefix_size the destination address size
* @param[out] dst_set the destination addresses matching the prefix
* @param[in, out] dst_size the number of entries available on in and used on out
*
* @return 0 on success
Expand All @@ -147,7 +146,7 @@ int fib_get_next_hop(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(uint32_t bitmap, uint32_t type,
int fib_get_destination_set(uint8_t *prefix, size_t prefix_size,
fib_destination_set_entry_t *dst_set, size_t* dst_set_size);


Expand Down
14 changes: 14 additions & 0 deletions sys/include/net/ng_fib/ng_universal_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ uint8_t* universal_address_get_address(universal_address_container_t *entry,
int universal_address_compare(universal_address_container_t *entry,
uint8_t *addr, size_t *addr_size);


/**
* @brief determines if the entry equals the provided prefix
*
* @param[in] entry pointer to the universal_address_container_t for compare
* @param[in] prefix pointer to the address for compare
* @param[in] prefix_size the number of bytes used for the address entry
*
* @return 0 if the entries are equal or comperable
* -ENOENT if the given adresses do not match
*/
int universal_address_compare_prefix(universal_address_container_t *entry,
uint8_t *prefix, size_t prefix_size);

/**
* @brief Prints the content of the given entry
*
Expand Down
9 changes: 4 additions & 5 deletions sys/net/network_layer/fib/fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,17 +424,17 @@ int fib_get_next_hop(kernel_pid_t *iface_id,
return 0;
}

int fib_get_destination_set(uint32_t bitmap, uint32_t type,
int fib_get_destination_set(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( (fib_table[i].global_flags & bitmap) == (type & bitmap) ) {

if( (dst_set != NULL) && (found_entries < (*dst_set_size)-1) ) {
if ((fib_table[i].global != NULL) &&
(universal_address_compare_prefix(fib_table[i].global, prefix, prefix_size) == 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(fib_table[i].global,
Expand All @@ -456,7 +456,6 @@ int fib_get_destination_set(uint32_t bitmap, uint32_t type,
mutex_unlock(&mtx_access);

return ret;

}

void fib_init(void)
Expand Down
29 changes: 29 additions & 0 deletions sys/net/network_layer/fib/universal_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,35 @@ int universal_address_compare(universal_address_container_t *entry,
return ret;
}

int universal_address_compare_prefix(universal_address_container_t *entry,
uint8_t *prefix, size_t prefix_size)
{
mutex_lock(&mtx_access);

int ret = -ENOENT;

/* If we have distinct sizes, the prefix is not comperable */
if (entry->address_size != prefix_size) {
mutex_unlock(&mtx_access);
return ret;
}

/* Get the index of the first trailing `0` */
int i = 0;
for( i = prefix_size-1; i >= 0; --i) {
if( prefix[i] != 0 ) {
break;
}
}

if( memcmp(entry->address, prefix, i+1) == 0 ) {
ret = 0;
}

mutex_unlock(&mtx_access);
return ret;
}

void universal_address_init(void)
{
mutex_lock(&mtx_access);
Expand Down
62 changes: 41 additions & 21 deletions tests/unittests/tests-fib/tests-fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,44 +530,64 @@ static void test_fib_14_exact_and_prefix_match(void)
}

/*
* @brief testing receiving an entry set matching specific type bits
* @brief testing receiving an destination address set matching a specific prefix
*/
static void test_fib_15_get_entry_set(void)
{
size_t add_buf_size = 16;
char addr_dst[add_buf_size];
char addr_nxt[add_buf_size];
uint32_t addr_dst_flags = 0;
uint32_t addr_nxt_flags = 0x33333333;
size_t addr_buf_size = 16;
char addr_dst[addr_buf_size];
char addr_nxt[addr_buf_size];

/* fill 20 addresses with distinct flags (0-19) for the destination address */
/* fill 20 addresses */
for (size_t i = 0; i < 20; ++i) {
/* 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 % 11);
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, addr_dst_flags++,
(uint8_t *)addr_nxt, add_buf_size - 1, addr_nxt_flags, 10000);
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(42, (uint8_t *)addr_dst, addr_buf_size - 1, 0x0,
(uint8_t *)addr_nxt, addr_buf_size - 1, 0x0, 100000);
}

size_t arr_size = 10;
size_t arr_size = 20;
fib_destination_set_entry_t arr_dst[arr_size];
/* we search entries iwits the lower 3 significant bits */
uint32_t bitmap = 0x3;
/* an arbitrary type */
uint32_t type = 0x22;
char prefix[addr_buf_size];
memset(prefix,0, addr_buf_size);
snprintf(prefix, addr_buf_size, "Test address 1");

int ret = fib_get_destination_set(bitmap, type, &arr_dst[0], &arr_size);
int ret = fib_get_destination_set((uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size);
TEST_ASSERT_EQUAL_INT(0, ret);

/* we should receive 5 entries 02, 06, 10, 14 and 18 */
TEST_ASSERT_EQUAL_INT(5, arr_size);
/* we should receive 10 entries 10 to 19 */
TEST_ASSERT_EQUAL_INT(10, arr_size);
arr_size = 20;

memset(prefix,0, addr_buf_size);
snprintf(prefix, addr_buf_size, "Test address 0");

ret = fib_get_destination_set((uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size);
TEST_ASSERT_EQUAL_INT(0, ret);

/* we should receive 10 entries 0-9 */
TEST_ASSERT_EQUAL_INT(10, arr_size);
arr_size = 20;

memset(prefix,0, addr_buf_size);
snprintf(prefix, addr_buf_size, "Test address");

ret = fib_get_destination_set((uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size);
printf("arr_size: %d\n", arr_size);
TEST_ASSERT_EQUAL_INT(0, ret);

/* we should receive 20 entries 0-19 */
TEST_ASSERT_EQUAL_INT(20, arr_size);

#if (TEST_FIB_SHOW_OUTPUT == 1)
puts("");
for(size_t i = 0; i < arr_size; ++i) {
printf("%s\n", (char*)arr_dst[i].dest);
for( size_t j = 0; j < arr_dst[i].dest_size; ++j) {
printf("%c", (char)arr_dst[i].dest[j]);
}
puts("");
}
puts("");
#endif
fib_deinit();
}
Expand Down

0 comments on commit cdbca42

Please sign in to comment.