Skip to content

Commit

Permalink
net/fib: Added network prefix flag to indicate a networ destination
Browse files Browse the repository at this point in the history
  • Loading branch information
BytesGalore authored and BytesGalore committed Jul 27, 2015
1 parent 4efdb99 commit 399dfe2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
5 changes: 5 additions & 0 deletions sys/include/net/ng_fib.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ typedef struct fib_destination_set_entry_t {
*/
#define FIB_LIFETIME_NO_EXPIRE (0xFFFFFFFF)

/**
* @brief flag to identify if the FIB-Entry is a net prefix (MSB == 1)
*/
#define FIB_FLAG_NET_PREFIX (1<<31)

/**
* @brief initializes all FIB entries with 0
*/
Expand Down
24 changes: 15 additions & 9 deletions sys/net/network_layer/fib/fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,16 @@ static int fib_find_entry(uint8_t *dst, size_t dst_size,
}
else {
/* we try to find the most fitting prefix */
if (ret_comp == 1) {
if ((ret_comp == 1)
&& (fib_table[i].global_flags & FIB_FLAG_NET_PREFIX)
&& match_size > prefix_size) {
entry_arr[0] = &(fib_table[i]);
/* we could find a better one so we move on */
ret = 0;

prefix_size = match_size;
match_size = dst_size<<3;
count = 1;
}
match_size = dst_size<<3;
}
}
}
Expand Down Expand Up @@ -639,18 +640,23 @@ static void fib_print_adress(universal_address_container_t *entry)
void fib_print_routes(void)
{
mutex_lock(&mtx_access);
printf("%-32s %-6s %-32s %-6s %-16s Interface\n"
, "Destination", "Flags", "Next Hop", "Flags", "Expires");
printf("%-32s %-12s %-32s %-12s %-12s %-2s\n"
, "Destination", "Flags", "Next Hop", "Flags", "Expires", "Interface");

timex_t now;
vtimer_now(&now);

for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) {
if (fib_table[i].lifetime.seconds != 0 || fib_table[i].lifetime.microseconds != 0) {
fib_print_adress(fib_table[i].global);
printf(" 0x%04"PRIx32" ", fib_table[i].global_flags);
printf(" 0x%"PRIx32" ", fib_table[i].global_flags);
if(fib_table[i].global_flags & FIB_FLAG_NET_PREFIX) {
printf("N ");
} else {
printf("H ");
}
fib_print_adress(fib_table[i].next_hop);
printf(" 0x%04"PRIx32" ", fib_table[i].next_hop_flags);
printf(" 0x%010"PRIx32" ", fib_table[i].next_hop_flags);

if ((fib_table[i].lifetime.seconds != FIB_LIFETIME_NO_EXPIRE)
|| (fib_table[i].lifetime.microseconds != FIB_LIFETIME_NO_EXPIRE)) {
Expand All @@ -663,14 +669,14 @@ void fib_print_routes(void)
printf("%-16s ", "EXPIRED");
}
else {
printf("%"PRIu32".%05"PRIu32, tm.seconds, tm.microseconds);
printf("%"PRIu32".%"PRIu32, tm.seconds, tm.microseconds);
}
}
else {
printf("%-16s ", "NEVER");
}

printf("%d\n", (int)fib_table[i].iface_id);
printf("\t %d\n", (int)fib_table[i].iface_id);
}
}

Expand Down
30 changes: 21 additions & 9 deletions tests/unittests/tests-fib/tests-fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,17 +475,20 @@ 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(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x12,
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1,
(FIB_FLAG_NET_PREFIX | 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(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123,
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1,
(FIB_FLAG_NET_PREFIX | 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(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x1234,
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1,
(FIB_FLAG_NET_PREFIX | 0x1234),
(uint8_t *)addr_nxt, add_buf_size - 1, 0x34, 100000);

memset(addr_lookup, 0, add_buf_size);
Expand Down Expand Up @@ -584,11 +587,13 @@ 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(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123,
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1,
(FIB_FLAG_NET_PREFIX | 0x123),
(uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000);

addr_dst[14] = (char)0x3c; /* 0011 1100 */
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123,
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1,
(FIB_FLAG_NET_PREFIX | 0x123),
(uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000);

memset(addr_nxt, 0, add_buf_size);
Expand All @@ -604,14 +609,15 @@ static void test_fib_16_prefix_match(void)
addr_lookup[14] = (char)0x34; /* 0011 0100 */
add_buf_size = 16;

fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123,
(uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000);
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1,
(FIB_FLAG_NET_PREFIX | 0x123),
(uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000);

memset(addr_nxt, 0, add_buf_size);

ret = fib_get_next_hop(&iface_id,
(uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags,
(uint8_t *)addr_lookup, add_buf_size - 1, 0x123);
(uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags,
(uint8_t *)addr_lookup, add_buf_size - 1, 0x123);

TEST_ASSERT_EQUAL_INT(-EHOSTUNREACH, ret);

Expand Down Expand Up @@ -659,6 +665,9 @@ static void test_fib_17_get_entry_set(void)
fib_destination_set_entry_t arr_dst[arr_size];
char prefix[addr_buf_size];
memset(prefix,0, addr_buf_size);
/* cppcheck: prefix is set to all 0 before adding an address
*/
/* cppcheck-suppress redundantCopy */
snprintf(prefix, addr_buf_size, "Test address 1");

int ret = fib_get_destination_set((uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size);
Expand All @@ -669,6 +678,9 @@ static void test_fib_17_get_entry_set(void)
arr_size = 20;

memset(prefix,0, addr_buf_size);
/* cppcheck: prefix is set to all 0 before adding an address
*/
/* cppcheck-suppress redundantCopy */
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);
Expand Down

0 comments on commit 399dfe2

Please sign in to comment.