samples: net: echo_server: Rework the VLAN support

Rework the code to support VLAN properly.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2024-03-17 18:40:13 +02:00 committed by Fabio Baltieri
parent c4a4506b4a
commit 55cbe530df
2 changed files with 26 additions and 21 deletions

View file

@ -1,9 +1,14 @@
CONFIG_NET_VLAN=y CONFIG_NET_VLAN=y
# We have one non-vlan interface and two VLAN interfaces # Allow useful interface assigned to VLAN interface
CONFIG_NET_VLAN_COUNT=3 CONFIG_NET_INTERFACE_NAME_LEN=15
# There will be three network interfaces. # We have one non-vlan interface and two VLAN interfaces
CONFIG_NET_VLAN_COUNT=2
# There will be three network interfaces so allocate enough IPv4 and IPv6 configs.
CONFIG_NET_IF_MAX_IPV4_COUNT=3
CONFIG_NET_IF_MAX_IPV6_COUNT=3
# First ethernet interface will use these settings # First ethernet interface will use these settings
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"

View file

@ -15,14 +15,19 @@ LOG_MODULE_DECLARE(net_echo_server_sample, LOG_LEVEL_DBG);
struct ud { struct ud {
struct net_if *first; struct net_if *first;
struct net_if *second; struct net_if *second;
struct net_if *third; struct net_if *eth;
}; };
static void iface_cb(struct net_if *iface, void *user_data) static void iface_cb(struct net_if *iface, void *user_data)
{ {
struct ud *ud = user_data; struct ud *ud = user_data;
if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) { if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET) && ud->eth == NULL) {
ud->eth = iface;
return;
}
if (net_if_l2(iface) != &NET_L2_GET_NAME(VIRTUAL)) {
return; return;
} }
@ -35,16 +40,11 @@ static void iface_cb(struct net_if *iface, void *user_data)
ud->second = iface; ud->second = iface;
return; return;
} }
if (!ud->third) {
ud->third = iface;
return;
}
} }
static int setup_iface(struct net_if *iface, const char *ipv6_addr, static int setup_iface(struct net_if *iface, struct net_if *vlan,
const char *ipv4_addr, const char *netmask, const char *ipv6_addr, const char *ipv4_addr,
uint16_t vlan_tag) const char *netmask, uint16_t vlan_tag)
{ {
struct net_if_addr *ifaddr; struct net_if_addr *ifaddr;
struct in_addr addr4; struct in_addr addr4;
@ -62,11 +62,11 @@ static int setup_iface(struct net_if *iface, const char *ipv6_addr,
return -EINVAL; return -EINVAL;
} }
ifaddr = net_if_ipv6_addr_add(iface, &addr6, ifaddr = net_if_ipv6_addr_add(vlan, &addr6,
NET_ADDR_MANUAL, 0); NET_ADDR_MANUAL, 0);
if (!ifaddr) { if (!ifaddr) {
LOG_ERR("Cannot add %s to interface %p", LOG_ERR("Cannot add %s to interface %p",
ipv6_addr, iface); ipv6_addr, vlan);
return -EINVAL; return -EINVAL;
} }
} }
@ -77,11 +77,11 @@ static int setup_iface(struct net_if *iface, const char *ipv6_addr,
return -EINVAL; return -EINVAL;
} }
ifaddr = net_if_ipv4_addr_add(iface, &addr4, ifaddr = net_if_ipv4_addr_add(vlan, &addr4,
NET_ADDR_MANUAL, 0); NET_ADDR_MANUAL, 0);
if (!ifaddr) { if (!ifaddr) {
LOG_ERR("Cannot add %s to interface %p", LOG_ERR("Cannot add %s to interface %p",
ipv4_addr, iface); ipv4_addr, vlan);
return -EINVAL; return -EINVAL;
} }
@ -93,11 +93,11 @@ static int setup_iface(struct net_if *iface, const char *ipv6_addr,
return -EINVAL; return -EINVAL;
} }
net_if_ipv4_set_netmask_by_addr(iface, &addr4, &nm); net_if_ipv4_set_netmask_by_addr(vlan, &addr4, &nm);
} }
} }
LOG_DBG("Interface %p VLAN tag %d setup done.", iface, vlan_tag); LOG_DBG("Interface %p VLAN tag %d setup done.", vlan, vlan_tag);
return 0; return 0;
} }
@ -115,7 +115,7 @@ int init_vlan(void)
* create IP address for this test. But first the VLAN needs to be * create IP address for this test. But first the VLAN needs to be
* added to the interface so that IPv6 DAD can work properly. * added to the interface so that IPv6 DAD can work properly.
*/ */
ret = setup_iface(ud.second, ret = setup_iface(ud.eth, ud.first,
CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR, CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR,
CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_ADDR, CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_ADDR,
CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_NETMASK, CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_NETMASK,
@ -124,7 +124,7 @@ int init_vlan(void)
return ret; return ret;
} }
ret = setup_iface(ud.third, ret = setup_iface(ud.eth, ud.second,
CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR, CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR,
CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR, CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR,
CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_NETMASK, CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_NETMASK,