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 Aug 18, 2015
1 parent b68213a commit 5a90bb4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
5 changes: 5 additions & 0 deletions sys/include/net/fib.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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 (1UL<<31)

/**
* @brief initializes all FIB entries with 0
*/
Expand Down
31 changes: 19 additions & 12 deletions sys/net/network_layer/fib/fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,17 @@ 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) {
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;
if ((ret_comp == 1)
&& (fib_table[i].global_flags & FIB_FLAG_NET_PREFIX)) {
if ((prefix_size == 0) || (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;
count = 1;
}
}
match_size = dst_size<<3;
}
}
}
Expand Down Expand Up @@ -682,9 +684,14 @@ void fib_print_routes(void)
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_address(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_address(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 @@ -697,14 +704,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
36 changes: 24 additions & 12 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 Expand Up @@ -765,7 +777,7 @@ static void test_fib_19_default_gateway(void)
}

/* add a default gateway entry */
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size, 0x123,
fib_add_entry(42, (uint8_t *)addr_dst, 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 @@ -838,7 +850,7 @@ static void test_fib_20_replace_prefix(void)
}

/* add a prefix entry */
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size, 0x123,
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size, (FIB_FLAG_NET_PREFIX | 0x123),
(uint8_t *)addr_nxt, add_buf_size, 0x23, 100000);

/* check if it matches */
Expand All @@ -864,7 +876,7 @@ static void test_fib_20_replace_prefix(void)
}

/* change the prefix entry */
fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size, 0x123,
fib_add_entry(42, (uint8_t *)addr_dst, 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 5a90bb4

Please sign in to comment.