Skip to content

Commit

Permalink
sys: net: dhcpv6: consolidate simple_pd code
Browse files Browse the repository at this point in the history
Reduce code duplication by having the simple PD feature as conditional
configuration within the regular DHCPv6 client code.
  • Loading branch information
OlegHahm committed Feb 4, 2025
1 parent 989c09e commit 94fef5b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 95 deletions.
7 changes: 1 addition & 6 deletions sys/auto_init/auto_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ extern void test_utils_interactive_sync(void);
AUTO_INIT(test_utils_interactive_sync,
AUTO_INIT_PRIO_MOD_TEST_UTILS_INTERACTIVE_SYNC);
#endif
#if IS_USED(MODULE_AUTO_INIT_DHCPV6_CLIENT)
#if IS_USED(MODULE_AUTO_INIT_DHCPV6_CLIENT) || IS_USED(MODULE_GNRC_DHCPV6_CLIENT_SIMPLE_PD)
extern void dhcpv6_client_auto_init(void);
AUTO_INIT(dhcpv6_client_auto_init,
AUTO_INIT_PRIO_MOD_DHCPV6_CLIENT);
Expand All @@ -302,11 +302,6 @@ extern void dhcpv6_relay_auto_init(void);
AUTO_INIT(dhcpv6_relay_auto_init,
AUTO_INIT_PRIO_MOD_DHCPV6_RELAY);
#endif
#if IS_USED(MODULE_GNRC_DHCPV6_CLIENT_SIMPLE_PD)
extern void gnrc_dhcpv6_client_simple_pd_init(void);
AUTO_INIT(gnrc_dhcpv6_client_simple_pd_init,
AUTO_INIT_PRIO_MOD_DHCPV6_CLIENT_SIMPLE_PD);
#endif
#if IS_USED(MODULE_GNRC_IPV6_AUTO_SUBNETS_AUTO_INIT)
extern void gnrc_ipv6_auto_subnets_init(void);
AUTO_INIT(gnrc_ipv6_auto_subnets_init,
Expand Down
6 changes: 0 additions & 6 deletions sys/auto_init/include/auto_init_priorities.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,6 @@ extern "C" {
*/
#define AUTO_INIT_PRIO_MOD_DHCPV6_RELAY 1480
#endif
#ifndef AUTO_INIT_PRIO_MOD_DHCPV6_CLIENT_SIMPLE_PD
/**
* @brief DHCPv6 client simple PD priority
*/
#define AUTO_INIT_PRIO_MOD_DHCPV6_CLIENT_SIMPLE_PD 1490
#endif
#ifndef AUTO_INIT_PRIO_MOD_GNRC_IPV6_AUTO_SUBNETS
/**
* @brief IPv6 auto subnets priority
Expand Down
7 changes: 0 additions & 7 deletions sys/include/net/gnrc/dhcpv6/client/simple_pd.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ extern "C" {
#define CONFIG_GNRC_DHCPV6_CLIENT_6LBR_STATIC_ROUTE
#endif

/**
* @brief Initializes the DHCPv6 client for simple prefix delegation
*
* @note Called by `auto_init` when included
*/
void gnrc_dhcpv6_client_simple_pd_init(void);

#ifdef __cplusplus
}
#endif
Expand Down
34 changes: 34 additions & 0 deletions sys/net/application_layer/dhcpv6/_dhcpv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "byteorder.h"
#include "net/ipv6/addr.h"
#include "net/gnrc/netif.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -349,6 +350,39 @@ typedef struct __attribute__((packed)) {
void dhcpv6_client_dns_rns_conf(const dhcpv6_opt_dns_rns_t *opt,
uint16_t netif);

/**
* @brief Find upstream network interface
*
* Either the one network interface configured at compile-time with @ref
* CONFIG_GNRC_DHCPV6_CLIENT_6LBR_UPSTREAM is picked or the first network
* interface that is not a 6LoWPAN interfaces if
* `CONFIG_GNRC_DHCPV6_CLIENT_6LBR_UPSTREAM` is 0.
*
* @return The upstream network interface.
*/
gnrc_netif_t *_dhcpv6_client_simple_pd_find_upstream_netif(void);

/**
* @brief Configure upstream netif to be in line with configuration script
*
* Set route and link-local address in accordance to
* `dist/tools/ethos/setup_network.sh`.
*
* @note This might not be necessary with a properly set-up DHCPv6 server
* (automatically configures a route for the delegated prefix) and
* upstream router (sends periodic router advertisements).
*
* @param[in] upstream_netif The upstream netif
*/
void _dhcpv6_client_simple_pd_configure_upstream_netif(gnrc_netif_t *upstream_netif);

/**
* @brief Configures all 6LoWPAN interfaces to request a 64-bit prefix
*
* @param[in] upstream_netif The upstream netif

Check failure on line 382 in sys/net/application_layer/dhcpv6/_dhcpv6.h

View workflow job for this annotation

GitHub Actions / static-tests

argument 'upstream_netif' of command @param is not found in the argument list of _dhcpv6_client_simple_pd_request_prefixes(gnrc_netif_t *upstream)
*/
void _dhcpv6_client_simple_pd_request_prefixes(gnrc_netif_t *upstream);

Check failure on line 384 in sys/net/application_layer/dhcpv6/_dhcpv6.h

View workflow job for this annotation

GitHub Actions / static-tests

The following parameter of _dhcpv6_client_simple_pd_request_prefixes(gnrc_netif_t *upstream) is not documented:

#ifdef __cplusplus
}
#endif
Expand Down
24 changes: 22 additions & 2 deletions sys/net/application_layer/dhcpv6/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static event_t request = { .handler = _request };
static event_t renew = { .handler = _renew };
static event_t rebind = { .handler = _rebind };

#ifdef MODULE_AUTO_INIT_DHCPV6_CLIENT
#if defined(MODULE_AUTO_INIT_DHCPV6_CLIENT)
static char _thread_stack[DHCPV6_CLIENT_STACK_SIZE];
static void *_thread(void *args);
static kernel_pid_t _thread_pid;
Expand All @@ -144,10 +144,30 @@ void dhcpv6_client_auto_init(void)
static void *_thread(void *args)
{
(void)args;
pid_t netif_pid = SOCK_ADDR_ANY_NETIF;
#if defined(MODULE_GNRC_DHCPV6_CLIENT_SIMPLE_PD)
gnrc_netif_t *upstream_netif = _dhcpv6_client_simple_pd_find_upstream_netif();
if (upstream_netif == NULL) {
LOG_ERROR("DHCPv6: No upstream interface found!\n");
return NULL;
}
_dhcpv6_client_simple_pd_configure_upstream_netif(upstream_netif);
netif_pid = ((gnrc_netif_t*)upstream_netif)->pid;
#endif
event_queue_t auto_init_event_queue;
/* initialize client event queue */
event_queue_init(&auto_init_event_queue);
dhcpv6_client_init(&auto_init_event_queue, SOCK_ADDR_ANY_NETIF);
/* initialize DHCPv6 client on border interface */
dhcpv6_client_init(&auto_init_event_queue, netif_pid);
#if defined(MODULE_GNRC_DHCPV6_CLIENT_SIMPLE_PD)
/* configure client to request prefix delegation for WPAN interfaces */
_dhcpv6_client_simple_pd_request_prefixes(upstream_netif);
/* set client configuration mode to stateful */
dhcpv6_client_set_conf_mode(DHCPV6_CLIENT_CONF_MODE_STATEFUL);
#endif
/* start DHCPv6 client */
dhcpv6_client_start();
/* start event loop of DHCPv6 client */
event_loop(&auto_init_event_queue); /* never returns */
return NULL;
}
Expand Down
77 changes: 3 additions & 74 deletions sys/net/gnrc/application_layer/dhcpv6/client_simple_pd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,7 @@

#include "net/gnrc/dhcpv6/client/simple_pd.h"

#if IS_USED(MODULE_AUTO_INIT_DHCPV6_CLIENT)
#error "Module `gnrc_dhcpv6_client_simple_pd` is mutually exclusive to \
`auto_init_dhcpv6_client`"
#endif

static char _stack[DHCPV6_CLIENT_STACK_SIZE];

/**
* @brief Find upstream network interface
*
* Either the one network interface configured at compile-time with @ref
* CONFIG_GNRC_DHCPV6_CLIENT_6LBR_UPSTREAM is picked or the first network
* interface that is not a 6LoWPAN interfaces if
* `CONFIG_GNRC_DHCPV6_CLIENT_6LBR_UPSTREAM` is 0.
*
* @return The upstream network interface.
*/
static gnrc_netif_t *_find_upstream_netif(void)
gnrc_netif_t *_dhcpv6_client_simple_pd_find_upstream_netif(void)
{
gnrc_netif_t *netif = NULL;

Expand All @@ -64,19 +47,7 @@ static gnrc_netif_t *_find_upstream_netif(void)
return NULL;
}

/**
* @brief Configure upstream netif to be in line with configuration script
*
* Set route and link-local address in accordance to
* `dist/tools/ethos/setup_network.sh`.
*
* @note This might not be necessary with a properly set-up DHCPv6 server
* (automatically configures a route for the delegated prefix) and
* upstream router (sends periodic router advertisements).
*
* @param[in] upstream_netif The upstream netif The upstream netif
*/
static void _configure_upstream_netif(gnrc_netif_t *upstream_netif)
void _dhcpv6_client_simple_pd_configure_upstream_netif(gnrc_netif_t *upstream_netif)
{
if (IS_ACTIVE(CONFIG_GNRC_DHCPV6_CLIENT_6LBR_STATIC_ROUTE)) {
ipv6_addr_t addr = {
Expand All @@ -92,13 +63,9 @@ static void _configure_upstream_netif(gnrc_netif_t *upstream_netif)
}
}

/**
* @brief Configures all 6LoWPAN interfaces to request a 64-bit prefix
*/
static void _configure_dhcpv6_client(void)
void _dhcpv6_client_simple_pd_request_prefixes(gnrc_netif_t *upstream)
{
gnrc_netif_t *netif = NULL;
gnrc_netif_t *upstream = _find_upstream_netif();

if (upstream == NULL) {
LOG_ERROR("DHCPv6: No upstream interface found!\n");
Expand All @@ -120,42 +87,4 @@ static void _configure_dhcpv6_client(void)
}
}

/**
* @brief The DHCPv6 client thread
*/
static void *_dhcpv6_cl_simple_pd_thread(void *args)
{
event_queue_t event_queue;
gnrc_netif_t *upstream_netif = _find_upstream_netif();

(void)args;
if (upstream_netif == NULL) {
LOG_ERROR("DHCPv6: No upstream interface found!\n");
return NULL;
}
_configure_upstream_netif(upstream_netif);
/* initialize client event queue */
event_queue_init(&event_queue);
/* initialize DHCPv6 client on border interface */
dhcpv6_client_init(&event_queue, upstream_netif->pid);
/* configure client to request prefix delegation for WPAN interfaces */
_configure_dhcpv6_client();
/* set client configuration mode to stateful */
dhcpv6_client_set_conf_mode(DHCPV6_CLIENT_CONF_MODE_STATEFUL);
/* start DHCPv6 client */
dhcpv6_client_start();
/* start event loop of DHCPv6 client */
event_loop(&event_queue); /* never returns */
return NULL;
}

void gnrc_dhcpv6_client_simple_pd_init(void)
{
/* start DHCPv6 client thread to request prefix for WPAN */
thread_create(_stack, DHCPV6_CLIENT_STACK_SIZE,
DHCPV6_CLIENT_PRIORITY,
0,
_dhcpv6_cl_simple_pd_thread, NULL, "dhcpv6-client");
}

/** @} */

0 comments on commit 94fef5b

Please sign in to comment.