net: if: Add method to set default interface

This complements the Kconfig possibility, and allows setting an
interface as default on runtime. Changing the default interface also
works around limitations when trying to use an offloaded interface
together with a native one.

Signed-off-by: Ole Morten Haaland <omh@icsys.no>
This commit is contained in:
Ole Morten Haaland 2021-11-12 08:22:32 +01:00 committed by Carles Cufí
commit fc6f40964c
2 changed files with 18 additions and 0 deletions

View file

@ -902,6 +902,13 @@ static inline struct net_if_config *net_if_config_get(struct net_if *iface)
*/
void net_if_router_rm(struct net_if_router *router);
/**
* @brief Set the default network interface.
*
* @param iface New default interface, or NULL to revert to the one set by Kconfig.
*/
void net_if_set_default(struct net_if *iface);
/**
* @brief Get the default network interface.
*

View file

@ -44,6 +44,8 @@ static K_MUTEX_DEFINE(lock);
extern struct net_if _net_if_list_start[];
extern struct net_if _net_if_list_end[];
static struct net_if *default_iface;
#if defined(CONFIG_NET_NATIVE_IPV4) || defined(CONFIG_NET_NATIVE_IPV6)
static struct net_if_router routers[CONFIG_NET_MAX_ROUTERS];
static struct k_work_delayable router_timer;
@ -552,6 +554,11 @@ struct net_if *net_if_lookup_by_dev(const struct device *dev)
return NULL;
}
void net_if_set_default(struct net_if *iface)
{
default_iface = iface;
}
struct net_if *net_if_get_default(void)
{
struct net_if *iface = NULL;
@ -560,6 +567,10 @@ struct net_if *net_if_get_default(void)
return NULL;
}
if (default_iface != NULL) {
return default_iface;
}
#if defined(CONFIG_NET_DEFAULT_IF_ETHERNET)
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(ETHERNET));
#endif