Skip to content

Commit

Permalink
Merge pull request #8083 from miri64/gnrc_ipv6_nib/api/prime-dr
Browse files Browse the repository at this point in the history
gnrc_ipv6_nib: make user added default route the primary one
  • Loading branch information
cgundogan committed Nov 19, 2017
2 parents ba77fd5 + 2f7b55b commit 21a5561
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
6 changes: 6 additions & 0 deletions sys/include/net/gnrc/ipv6/nib/ft.h
Expand Up @@ -62,6 +62,9 @@ int gnrc_ipv6_nib_ft_get(const ipv6_addr_t *dst, gnrc_pktsnip_t *pkt,
/**
* @brief Adds a new route to the forwarding table
*
* If @p dst is the default route, the route will be configured to be the
* default route.
*
* @param[in] dst The destination to the route. May be NULL or `::` for
* default route.
* @param[in] dst_len The prefix length of @p dst in bits. May be 0 for
Expand All @@ -83,6 +86,9 @@ int gnrc_ipv6_nib_ft_add(const ipv6_addr_t *dst, unsigned dst_len,
/**
* @brief Deletes a route from forwarding table.
*
* If @p dst is the default route, the function assures, that the current
* primary default route is removed first.
*
* @param[in] dst The destination of the route. May be NULL or `::` for
* default route.
* @param[in] dst_len The prefix length of @p dst in bits. May be 0 for
Expand Down
2 changes: 1 addition & 1 deletion sys/net/gnrc/network_layer/ipv6/nib/_nib-internal.c
Expand Up @@ -31,7 +31,7 @@
#include "debug.h"

/* pointers for default router selection */
static _nib_dr_entry_t *_prime_def_router = NULL;
_nib_dr_entry_t *_prime_def_router = NULL;
static clist_node_t _next_removable = { NULL };

static _nib_onl_entry_t _nodes[GNRC_IPV6_NIB_NUMOF];
Expand Down
10 changes: 10 additions & 0 deletions sys/net/gnrc/network_layer/ipv6/nib/_nib-internal.h
Expand Up @@ -250,6 +250,16 @@ extern mutex_t _nib_mutex;
*/
extern evtimer_msg_t _nib_evtimer;

/**
* @brief Primary default router.
*
* This value is returned by @ref @_nib_drl_get_dr() when it is not NULL and it
* is reachable. Otherwise it is selected with the [default router selection
* algoritm](https://tools.ietf.org/html/rfc4861#section-6.3.6) by that function.
* Exposed to be settable by @ref net_gnrc_ipv6_nib_ft.
*/
extern _nib_dr_entry_t *_prime_def_router;

/**
* @brief Initializes NIB internally
*/
Expand Down
9 changes: 6 additions & 3 deletions sys/net/gnrc/network_layer/ipv6/nib/nib_ft.c
Expand Up @@ -51,9 +51,12 @@ int gnrc_ipv6_nib_ft_add(const ipv6_addr_t *dst, unsigned dst_len,
if (ptr == NULL) {
res = -ENOMEM;
}
else if (ltime > 0) {
_evtimer_add(ptr, GNRC_IPV6_NIB_RTR_TIMEOUT,
&ptr->rtr_timeout, ltime * MS_PER_SEC);
else {
_prime_def_router = ptr;
if (ltime > 0) {
_evtimer_add(ptr, GNRC_IPV6_NIB_RTR_TIMEOUT,
&ptr->rtr_timeout, ltime * MS_PER_SEC);
}
}
}
#if GNRC_IPV6_NIB_CONF_ROUTER
Expand Down
31 changes: 30 additions & 1 deletion tests/unittests/tests-gnrc_ipv6_nib/tests-gnrc_ipv6_nib-ft.c
Expand Up @@ -568,6 +568,28 @@ static void test_nib_ft_add__success(void)
TEST_ASSERT(!gnrc_ipv6_nib_ft_iter(NULL, 0, &iter_state, &fte));
}

/*
* Creates a default route
* Expected result: a new entry should exist and contain the given prefix,
* interface, and lifetimes and it should be the primary default route
*/
static void test_nib_ft_add__success_dr(void)
{
gnrc_ipv6_nib_ft_t fte;
void *iter_state = NULL;
static const ipv6_addr_t next_hop = { .u64 = { { .u8 = LINK_LOCAL_PREFIX },
{ .u64 = TEST_UINT64 } } };

TEST_ASSERT_EQUAL_INT(0, gnrc_ipv6_nib_ft_add(NULL, 0, &next_hop, IFACE, 0));
TEST_ASSERT(gnrc_ipv6_nib_ft_iter(NULL, 0, &iter_state, &fte));
TEST_ASSERT(ipv6_addr_is_unspecified(&fte.dst));
TEST_ASSERT_EQUAL_INT(0, fte.dst_len);
TEST_ASSERT(ipv6_addr_equal(&fte.next_hop, &next_hop));
TEST_ASSERT_EQUAL_INT(IFACE, fte.iface);
TEST_ASSERT_EQUAL_INT(1, fte.primary);
TEST_ASSERT(!gnrc_ipv6_nib_ft_iter(NULL, 0, &iter_state, &fte));
}

/*
* Creates MAX_NUMOF routes with different destinations of to the different
* next hops and interfaces and then tries to delete one with yet another
Expand Down Expand Up @@ -639,13 +661,19 @@ static void test_nib_ft_iter__empty_def_route_at_beginning(void)
next_hop.u16[0].u16++;
TEST_ASSERT_EQUAL_INT(0, gnrc_ipv6_nib_ft_add(NULL, 0,
&next_hop, IFACE, 0));
/* make first added route the primary default route again */
next_hop.u16[0].u16 -= 2;
TEST_ASSERT_EQUAL_INT(0, gnrc_ipv6_nib_ft_add(NULL, 0,
&next_hop, IFACE, 0));
/* remove primary default route */
gnrc_ipv6_nib_ft_del(NULL, 0);
next_hop.u16[0].u16--;
next_hop.u16[0].u16++;
while (gnrc_ipv6_nib_ft_iter(NULL, 0, &iter_state, &fte)) {
TEST_ASSERT(ipv6_addr_is_unspecified(&fte.dst));
TEST_ASSERT(ipv6_addr_equal(&fte.next_hop, &next_hop));
TEST_ASSERT_EQUAL_INT(0, fte.dst_len);
TEST_ASSERT_EQUAL_INT(IFACE, fte.iface);
TEST_ASSERT(!fte.primary);
count++;
next_hop.u16[0].u16++;
}
Expand Down Expand Up @@ -717,6 +745,7 @@ Test *tests_gnrc_ipv6_nib_ft_tests(void)
new_TestFixture(test_nib_ft_add__success_duplicate),
new_TestFixture(test_nib_ft_add__success_overwrite_unspecified),
new_TestFixture(test_nib_ft_add__success),
new_TestFixture(test_nib_ft_add__success_dr),
new_TestFixture(test_nib_ft_del__unknown),
new_TestFixture(test_nib_ft_del__success),
/* most of gnrc_ipv6_nib_ft_iter() is tested during all the tests above */
Expand Down

0 comments on commit 21a5561

Please sign in to comment.