Skip to content

Commit

Permalink
net/fib: Added network prefix flag to indicate a network destination
Browse files Browse the repository at this point in the history
  • Loading branch information
BytesGalore authored and BytesGalore committed Sep 29, 2015
1 parent 669043c commit 01afb92
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
5 changes: 5 additions & 0 deletions sys/include/net/fib.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ typedef struct fib_destination_set_entry_t {
*/
#define FIB_LIFETIME_NO_EXPIRE (0xFFFFFFFFffffffff)

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

/**
* @brief initializes all FIB entries with 0
*
Expand Down
25 changes: 17 additions & 8 deletions sys/net/network_layer/fib/fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,18 @@ static int fib_find_entry(fib_table_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->entries[i]);
/* we could find a better one so we move on */
ret = 0;

prefix_size = match_size;
match_size = dst_size<<3;
count = 1;
if ((ret_comp == 1)
&& (table->entries[i].global_flags & FIB_FLAG_NET_PREFIX)) {
if ((prefix_size == 0) || (match_size > prefix_size)) {
entry_arr[0] = &(table->entries[i]);
/* we could find a better one so we move on */
ret = 0;

prefix_size = match_size;
count = 1;
}
}
match_size = dst_size<<3;
}
}
}
Expand Down Expand Up @@ -653,6 +656,12 @@ void fib_print_routes(fib_table_t *table)
if (table->entries[i].lifetime != 0) {
fib_print_address(table->entries[i].global);
printf(" 0x%04"PRIx32" ", table->entries[i].global_flags);
if(table->entries[i].global_flags & FIB_FLAG_NET_PREFIX) {
printf("N ");
} else {
printf("H ");
}

fib_print_address(table->entries[i].next_hop);
printf(" 0x%04"PRIx32" ", table->entries[i].next_hop_flags);

Expand Down
34 changes: 25 additions & 9 deletions tests/unittests/tests-fib/tests-fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,20 +488,24 @@ 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,
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(&test_fib_table, 42, (uint8_t *)addr_dst,
add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - 1,
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(&test_fib_table, 42, (uint8_t *)addr_dst,
add_buf_size - 1, 0x1234, (uint8_t *)addr_nxt, add_buf_size - 1,
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 @@ -605,12 +609,14 @@ static void test_fib_16_prefix_match(void)
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,
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(&test_fib_table, 42, (uint8_t *)addr_dst,
add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - 1,
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 @@ -627,7 +633,8 @@ static void test_fib_16_prefix_match(void)
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 -
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 Down Expand Up @@ -683,6 +690,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(&test_fib_table,
Expand All @@ -695,6 +705,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(&test_fib_table,
Expand Down Expand Up @@ -796,7 +809,8 @@ 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,
add_buf_size, (FIB_FLAG_NET_PREFIX | 0x123),
(uint8_t *)addr_nxt, add_buf_size, 0x23,
100000);

/* check if it matches all */
Expand Down Expand Up @@ -872,7 +886,8 @@ 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,
add_buf_size, (FIB_FLAG_NET_PREFIX | 0x123),
(uint8_t *)addr_nxt, add_buf_size, 0x23,
100000);

/* check if it matches */
Expand Down Expand Up @@ -901,7 +916,8 @@ 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,
add_buf_size, (FIB_FLAG_NET_PREFIX | 0x123),
(uint8_t *)addr_nxt, add_buf_size, 0x24,
100000);

/* and check again if it matches */
Expand Down

0 comments on commit 01afb92

Please sign in to comment.