samples: net: vlan: Refactor VLAN support
Move the common VLAN setup code to samples/net/common directory so that other network samples can utilize that too. Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
This commit is contained in:
parent
6dd239098b
commit
fceb30d935
@ -5,54 +5,6 @@
|
||||
|
||||
mainmenu "Networking VLAN sample application"
|
||||
|
||||
config NET_SAMPLE_IFACE2_MY_IPV6_ADDR
|
||||
string "My IPv6 address for second interface"
|
||||
help
|
||||
The value depends on your network setup.
|
||||
|
||||
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.
|
||||
|
||||
config NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN
|
||||
int "IPv6 address prefix length for the interfaces"
|
||||
default 64
|
||||
@ -61,4 +13,5 @@ config NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN
|
||||
Set the IPv6 address prefix length (netmask) for the interfaces
|
||||
that is used in the sample application.
|
||||
|
||||
source "samples/net/common/Kconfig"
|
||||
source "Kconfig.zephyr"
|
||||
|
||||
@ -6,6 +6,7 @@ CONFIG_NET_DHCPV4=n
|
||||
CONFIG_NET_UDP=y
|
||||
CONFIG_NET_TCP=y
|
||||
CONFIG_NET_STATISTICS=y
|
||||
CONFIG_POSIX_API=y
|
||||
|
||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||
|
||||
@ -41,21 +42,13 @@ 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"
|
||||
|
||||
# Second VLAN supported network interface will have these settings
|
||||
CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR="2001:db8:100::1"
|
||||
# First VLAN interface will have these settings
|
||||
# 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
|
||||
CONFIG_NET_SAMPLE_COMMON_VLAN_SETUP_1="100;2001:db8:100::1/64,198.51.100.1/24"
|
||||
|
||||
# Settings for the third VLAN supported network interface
|
||||
CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR="2001:db8:200::1"
|
||||
# Second VLAN interface will have these settings
|
||||
# TEST-NET-3 from RFC 5737
|
||||
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
|
||||
CONFIG_NET_SAMPLE_COMMON_VLAN_SETUP_2="200;2001:db8:200::1/64,203.0.113.1/24"
|
||||
|
||||
# Logging
|
||||
CONFIG_LOG=y
|
||||
|
||||
@ -15,144 +15,10 @@ LOG_MODULE_REGISTER(net_vlan_sample, LOG_LEVEL_DBG);
|
||||
#include <zephyr/net/net_if.h>
|
||||
#include <zephyr/net/ethernet.h>
|
||||
|
||||
struct ud {
|
||||
struct net_if *first;
|
||||
struct net_if *second;
|
||||
};
|
||||
|
||||
static void iface_cb(struct net_if *iface, void *user_data)
|
||||
{
|
||||
struct ud *ud = user_data;
|
||||
|
||||
if (net_if_l2(iface) != &NET_L2_GET_NAME(VIRTUAL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ud->first == NULL) {
|
||||
ud->first = iface;
|
||||
return;
|
||||
}
|
||||
|
||||
ud->second = iface;
|
||||
}
|
||||
|
||||
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_addr *ifaddr;
|
||||
struct in_addr addr4;
|
||||
struct in6_addr addr6, netaddr6;
|
||||
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;
|
||||
}
|
||||
|
||||
net_ipv6_addr_prefix_mask((uint8_t *)&addr6,
|
||||
(uint8_t *)&netaddr6,
|
||||
CONFIG_NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN);
|
||||
|
||||
if (!net_if_ipv6_prefix_add(vlan, &netaddr6,
|
||||
CONFIG_NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN,
|
||||
(uint32_t)0xffffffff)) {
|
||||
LOG_ERR("Cannot add %s with prefix_len %d to interface %p",
|
||||
ipv6_addr,
|
||||
CONFIG_NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN,
|
||||
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;
|
||||
|
||||
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(ETHERNET));
|
||||
if (!iface) {
|
||||
LOG_ERR("No ethernet interfaces found.");
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
memset(&ud, 0, sizeof(ud));
|
||||
|
||||
net_if_foreach(iface_cb, &ud);
|
||||
|
||||
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) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
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) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Bring up the VLAN interface automatically */
|
||||
net_if_up(ud.first);
|
||||
net_if_up(ud.second);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#include "net_sample_common.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
init_app();
|
||||
init_vlan();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user