From a8135bbdd5e4a5903d6f86918ae4f851a1e94c77 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 28 Mar 2024 13:52:29 +0200 Subject: [PATCH] samples: net: vlan: Support all three network interfaces The VLAN sample application only had configuration support for the Ethernet interface and one VLAN interface, and left the one VLAN interface unconfigured. Fix this by adding config support for all three network interfaces created by the sample. Signed-off-by: Jukka Rissanen --- samples/net/vlan/Kconfig | 57 +++++++++++++------ samples/net/vlan/prj.conf | 24 +++++--- samples/net/vlan/src/main.c | 107 +++++++++++++++++++++++++----------- 3 files changed, 130 insertions(+), 58 deletions(-) diff --git a/samples/net/vlan/Kconfig b/samples/net/vlan/Kconfig index 92ef899d276..22ef6b07f8b 100644 --- a/samples/net/vlan/Kconfig +++ b/samples/net/vlan/Kconfig @@ -5,31 +5,52 @@ mainmenu "Networking VLAN sample application" -config SAMPLE_VLAN_TAG - int "Virtual lan tag used in VLAN sample app" - default 100 - range 0 4094 - depends on NET_VLAN - help - Set virtual lan tag (id) that is used in VLAN sample application. - -config SAMPLE_VLAN_TAG_2 - int "Second VLAN tag used in VLAN sample app" - default 200 - range 0 4094 - depends on NET_VLAN - help - Set virtual lan tag (id) that is used in VLAN sample application. - -config SAMPLE_IPV6_ADDR_2 +config NET_SAMPLE_IFACE2_MY_IPV6_ADDR string "My IPv6 address for second interface" help The value depends on your network setup. -config SAMPLE_IPV4_ADDR_2 +config NET_SAMPLE_IFACE2_MY_IPV4_ADDR string "My IPv4 address for second interface" help The value depends on your network setup. +config NET_SAMPLE_IFACE2_MY_IPV4_NETMASK + string "My IPv4 netmask for second interface" + help + The value depends on your network setup. + +config NET_SAMPLE_IFACE2_VLAN_TAG + int "VLAN tag for second interface" + default 100 + range 0 4094 + depends on NET_VLAN + help + Set VLAN (virtual LAN) tag (id) that is used in the sample + application. + +config NET_SAMPLE_IFACE3_MY_IPV6_ADDR + string "My IPv6 address for third interface" + help + The value depends on your network setup. + +config NET_SAMPLE_IFACE3_MY_IPV4_ADDR + string "My IPv4 address for third interface" + help + The value depends on your network setup. + +config NET_SAMPLE_IFACE3_MY_IPV4_NETMASK + string "My IPv4 netmask for third interface" + help + The value depends on your network setup. + +config NET_SAMPLE_IFACE3_VLAN_TAG + int "VLAN tag for third interface" + default 200 + range 0 4094 + depends on NET_VLAN + help + Set VLAN (virtual LAN) tag (id) that is used in the sample + application. source "Kconfig.zephyr" diff --git a/samples/net/vlan/prj.conf b/samples/net/vlan/prj.conf index 0fb5b752dfd..ffce904005f 100644 --- a/samples/net/vlan/prj.conf +++ b/samples/net/vlan/prj.conf @@ -19,7 +19,7 @@ CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=1 CONFIG_NET_MAX_CONTEXTS=10 CONFIG_NET_IF_MAX_IPV6_COUNT=3 -CONFIG_NET_IF_MAX_IPV6_COUNT=3 +CONFIG_NET_IF_MAX_IPV4_COUNT=3 CONFIG_INIT_STACKS=y CONFIG_PRINTK=y @@ -38,19 +38,25 @@ CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" -# VLAN tag for the first interface -CONFIG_SAMPLE_VLAN_TAG=100 +# Second VLAN supported network interface will have these settings +CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR="2001:db8:100::1" +# TEST-NET-2 from RFC 5737 +CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_ADDR="198.51.100.1" +CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_NETMASK="255.255.255.0" +# VLAN tag for the second interface +CONFIG_NET_SAMPLE_IFACE2_VLAN_TAG=100 -# Settings for the second network interface -CONFIG_SAMPLE_IPV6_ADDR_2="2001:db8:200::1" -CONFIG_SAMPLE_VLAN_TAG_2=200 +# Settings for the third VLAN supported network interface +CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR="2001:db8:200::1" # TEST-NET-3 from RFC 5737 -CONFIG_SAMPLE_IPV4_ADDR_2="203.0.113.1" +CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR="203.0.113.1" +CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_NETMASK="255.255.255.0" +# VLAN tag for the second interface +CONFIG_NET_SAMPLE_IFACE3_VLAN_TAG=200 # Logging CONFIG_LOG=y -# VLAN settings. Note that currently SLIP only supports one VLAN tag, -# and that is enforced by Kconfig file. +# VLAN settings. CONFIG_NET_VLAN=y CONFIG_NET_VLAN_COUNT=2 diff --git a/samples/net/vlan/src/main.c b/samples/net/vlan/src/main.c index 4f7483c1d25..7b7736f71a2 100644 --- a/samples/net/vlan/src/main.c +++ b/samples/net/vlan/src/main.c @@ -36,12 +36,69 @@ static void iface_cb(struct net_if *iface, void *user_data) ud->second = iface; } -static int init_app(void) +static int setup_iface(struct net_if *iface, struct net_if *vlan, + const char *ipv6_addr, const char *ipv4_addr, + const char *netmask, uint16_t vlan_tag) { - struct net_if *iface; struct net_if_addr *ifaddr; struct in_addr addr4; struct in6_addr addr6; + int ret; + + ret = net_eth_vlan_enable(iface, vlan_tag); + if (ret < 0) { + LOG_ERR("Cannot enable VLAN for tag %d (%d)", vlan_tag, ret); + } + + if (IS_ENABLED(CONFIG_NET_IPV6)) { + if (net_addr_pton(AF_INET6, ipv6_addr, &addr6)) { + LOG_ERR("Invalid address: %s", ipv6_addr); + return -EINVAL; + } + + ifaddr = net_if_ipv6_addr_add(vlan, &addr6, + NET_ADDR_MANUAL, 0); + if (!ifaddr) { + LOG_ERR("Cannot add %s to interface %p", + ipv6_addr, vlan); + return -EINVAL; + } + } + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + if (net_addr_pton(AF_INET, ipv4_addr, &addr4)) { + LOG_ERR("Invalid address: %s", ipv4_addr); + return -EINVAL; + } + + ifaddr = net_if_ipv4_addr_add(vlan, &addr4, + NET_ADDR_MANUAL, 0); + if (!ifaddr) { + LOG_ERR("Cannot add %s to interface %p", + ipv4_addr, vlan); + return -EINVAL; + } + + if (netmask && netmask[0]) { + struct in_addr nm; + + if (net_addr_pton(AF_INET, netmask, &nm)) { + LOG_ERR("Invalid netmask: %s", ipv4_addr); + return -EINVAL; + } + + net_if_ipv4_set_netmask_by_addr(vlan, &addr4, &nm); + } + } + + LOG_DBG("Interface %p VLAN tag %d setup done.", vlan, vlan_tag); + + return 0; +} + +static int init_app(void) +{ + struct net_if *iface; struct ud ud; int ret; @@ -51,43 +108,31 @@ static int init_app(void) return -ENOENT; } + memset(&ud, 0, sizeof(ud)); + net_if_foreach(iface_cb, &ud); - ret = net_eth_vlan_enable(iface, CONFIG_SAMPLE_VLAN_TAG); + ret = setup_iface(iface, ud.first, + CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR, + CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_ADDR, + CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_NETMASK, + CONFIG_NET_SAMPLE_IFACE2_VLAN_TAG); if (ret < 0) { - LOG_ERR("Cannot enable VLAN for tag %d (%d)", - CONFIG_SAMPLE_VLAN_TAG, ret); + return ret; } - ret = net_eth_vlan_enable(iface, CONFIG_SAMPLE_VLAN_TAG_2); + ret = setup_iface(iface, ud.second, + CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR, + CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR, + CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_NETMASK, + CONFIG_NET_SAMPLE_IFACE3_VLAN_TAG); if (ret < 0) { - LOG_ERR("Cannot enable VLAN for tag %d (%d)", - CONFIG_SAMPLE_VLAN_TAG_2, ret); + return ret; } - if (net_addr_pton(AF_INET6, CONFIG_SAMPLE_IPV6_ADDR_2, &addr6)) { - LOG_ERR("Invalid address: %s", CONFIG_SAMPLE_IPV6_ADDR_2); - return -EINVAL; - } - - ifaddr = net_if_ipv6_addr_add(ud.second, &addr6, NET_ADDR_MANUAL, 0); - if (!ifaddr) { - LOG_ERR("Cannot add %s to interface %p", - CONFIG_SAMPLE_IPV6_ADDR_2, ud.second); - return -EINVAL; - } - - if (net_addr_pton(AF_INET, CONFIG_SAMPLE_IPV4_ADDR_2, &addr4)) { - LOG_ERR("Invalid address: %s", CONFIG_SAMPLE_IPV4_ADDR_2); - return -EINVAL; - } - - ifaddr = net_if_ipv4_addr_add(ud.second, &addr4, NET_ADDR_MANUAL, 0); - if (!ifaddr) { - LOG_ERR("Cannot add %s to interface %p", - CONFIG_SAMPLE_IPV4_ADDR_2, ud.second); - return -EINVAL; - } + /* Bring up the VLAN interface automatically */ + net_if_up(ud.first); + net_if_up(ud.second); return ret; }