Skip to content

Commit fceb30d

Browse files
jukkarkartben
authored andcommitted
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 <[email protected]>
1 parent 6dd2390 commit fceb30d

File tree

3 files changed

+8
-196
lines changed

3 files changed

+8
-196
lines changed

samples/net/vlan/Kconfig

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,6 @@
55

66
mainmenu "Networking VLAN sample application"
77

8-
config NET_SAMPLE_IFACE2_MY_IPV6_ADDR
9-
string "My IPv6 address for second interface"
10-
help
11-
The value depends on your network setup.
12-
13-
config NET_SAMPLE_IFACE2_MY_IPV4_ADDR
14-
string "My IPv4 address for second interface"
15-
help
16-
The value depends on your network setup.
17-
18-
config NET_SAMPLE_IFACE2_MY_IPV4_NETMASK
19-
string "My IPv4 netmask for second interface"
20-
help
21-
The value depends on your network setup.
22-
23-
config NET_SAMPLE_IFACE2_VLAN_TAG
24-
int "VLAN tag for second interface"
25-
default 100
26-
range 0 4094
27-
depends on NET_VLAN
28-
help
29-
Set VLAN (virtual LAN) tag (id) that is used in the sample
30-
application.
31-
32-
config NET_SAMPLE_IFACE3_MY_IPV6_ADDR
33-
string "My IPv6 address for third interface"
34-
help
35-
The value depends on your network setup.
36-
37-
config NET_SAMPLE_IFACE3_MY_IPV4_ADDR
38-
string "My IPv4 address for third interface"
39-
help
40-
The value depends on your network setup.
41-
42-
config NET_SAMPLE_IFACE3_MY_IPV4_NETMASK
43-
string "My IPv4 netmask for third interface"
44-
help
45-
The value depends on your network setup.
46-
47-
config NET_SAMPLE_IFACE3_VLAN_TAG
48-
int "VLAN tag for third interface"
49-
default 200
50-
range 0 4094
51-
depends on NET_VLAN
52-
help
53-
Set VLAN (virtual LAN) tag (id) that is used in the sample
54-
application.
55-
568
config NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN
579
int "IPv6 address prefix length for the interfaces"
5810
default 64
@@ -61,4 +13,5 @@ config NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN
6113
Set the IPv6 address prefix length (netmask) for the interfaces
6214
that is used in the sample application.
6315

16+
source "samples/net/common/Kconfig"
6417
source "Kconfig.zephyr"

samples/net/vlan/prj.conf

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CONFIG_NET_DHCPV4=n
66
CONFIG_NET_UDP=y
77
CONFIG_NET_TCP=y
88
CONFIG_NET_STATISTICS=y
9+
CONFIG_POSIX_API=y
910

1011
CONFIG_TEST_RANDOM_GENERATOR=y
1112

@@ -41,21 +42,13 @@ CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
4142
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
4243
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
4344

44-
# Second VLAN supported network interface will have these settings
45-
CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR="2001:db8:100::1"
45+
# First VLAN interface will have these settings
4646
# TEST-NET-2 from RFC 5737
47-
CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_ADDR="198.51.100.1"
48-
CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_NETMASK="255.255.255.0"
49-
# VLAN tag for the second interface
50-
CONFIG_NET_SAMPLE_IFACE2_VLAN_TAG=100
47+
CONFIG_NET_SAMPLE_COMMON_VLAN_SETUP_1="100;2001:db8:100::1/64,198.51.100.1/24"
5148

52-
# Settings for the third VLAN supported network interface
53-
CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR="2001:db8:200::1"
49+
# Second VLAN interface will have these settings
5450
# TEST-NET-3 from RFC 5737
55-
CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR="203.0.113.1"
56-
CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_NETMASK="255.255.255.0"
57-
# VLAN tag for the second interface
58-
CONFIG_NET_SAMPLE_IFACE3_VLAN_TAG=200
51+
CONFIG_NET_SAMPLE_COMMON_VLAN_SETUP_2="200;2001:db8:200::1/64,203.0.113.1/24"
5952

6053
# Logging
6154
CONFIG_LOG=y

samples/net/vlan/src/main.c

Lines changed: 2 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -15,144 +15,10 @@ LOG_MODULE_REGISTER(net_vlan_sample, LOG_LEVEL_DBG);
1515
#include <zephyr/net/net_if.h>
1616
#include <zephyr/net/ethernet.h>
1717

18-
struct ud {
19-
struct net_if *first;
20-
struct net_if *second;
21-
};
22-
23-
static void iface_cb(struct net_if *iface, void *user_data)
24-
{
25-
struct ud *ud = user_data;
26-
27-
if (net_if_l2(iface) != &NET_L2_GET_NAME(VIRTUAL)) {
28-
return;
29-
}
30-
31-
if (ud->first == NULL) {
32-
ud->first = iface;
33-
return;
34-
}
35-
36-
ud->second = iface;
37-
}
38-
39-
static int setup_iface(struct net_if *iface, struct net_if *vlan,
40-
const char *ipv6_addr, const char *ipv4_addr,
41-
const char *netmask, uint16_t vlan_tag)
42-
{
43-
struct net_if_addr *ifaddr;
44-
struct in_addr addr4;
45-
struct in6_addr addr6, netaddr6;
46-
int ret;
47-
48-
ret = net_eth_vlan_enable(iface, vlan_tag);
49-
if (ret < 0) {
50-
LOG_ERR("Cannot enable VLAN for tag %d (%d)", vlan_tag, ret);
51-
}
52-
53-
if (IS_ENABLED(CONFIG_NET_IPV6)) {
54-
if (net_addr_pton(AF_INET6, ipv6_addr, &addr6)) {
55-
LOG_ERR("Invalid address: %s", ipv6_addr);
56-
return -EINVAL;
57-
}
58-
59-
ifaddr = net_if_ipv6_addr_add(vlan, &addr6,
60-
NET_ADDR_MANUAL, 0);
61-
if (!ifaddr) {
62-
LOG_ERR("Cannot add %s to interface %p",
63-
ipv6_addr, vlan);
64-
return -EINVAL;
65-
}
66-
67-
net_ipv6_addr_prefix_mask((uint8_t *)&addr6,
68-
(uint8_t *)&netaddr6,
69-
CONFIG_NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN);
70-
71-
if (!net_if_ipv6_prefix_add(vlan, &netaddr6,
72-
CONFIG_NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN,
73-
(uint32_t)0xffffffff)) {
74-
LOG_ERR("Cannot add %s with prefix_len %d to interface %p",
75-
ipv6_addr,
76-
CONFIG_NET_SAMPLE_IFACE_MY_IPV6_PREFIXLEN,
77-
vlan);
78-
return -EINVAL;
79-
}
80-
}
81-
82-
if (IS_ENABLED(CONFIG_NET_IPV4)) {
83-
if (net_addr_pton(AF_INET, ipv4_addr, &addr4)) {
84-
LOG_ERR("Invalid address: %s", ipv4_addr);
85-
return -EINVAL;
86-
}
87-
88-
ifaddr = net_if_ipv4_addr_add(vlan, &addr4,
89-
NET_ADDR_MANUAL, 0);
90-
if (!ifaddr) {
91-
LOG_ERR("Cannot add %s to interface %p",
92-
ipv4_addr, vlan);
93-
return -EINVAL;
94-
}
95-
96-
if (netmask && netmask[0]) {
97-
struct in_addr nm;
98-
99-
if (net_addr_pton(AF_INET, netmask, &nm)) {
100-
LOG_ERR("Invalid netmask: %s", ipv4_addr);
101-
return -EINVAL;
102-
}
103-
104-
net_if_ipv4_set_netmask_by_addr(vlan, &addr4, &nm);
105-
}
106-
}
107-
108-
LOG_DBG("Interface %p VLAN tag %d setup done.", vlan, vlan_tag);
109-
110-
return 0;
111-
}
112-
113-
static int init_app(void)
114-
{
115-
struct net_if *iface;
116-
struct ud ud;
117-
int ret;
118-
119-
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(ETHERNET));
120-
if (!iface) {
121-
LOG_ERR("No ethernet interfaces found.");
122-
return -ENOENT;
123-
}
124-
125-
memset(&ud, 0, sizeof(ud));
126-
127-
net_if_foreach(iface_cb, &ud);
128-
129-
ret = setup_iface(iface, ud.first,
130-
CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR,
131-
CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_ADDR,
132-
CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_NETMASK,
133-
CONFIG_NET_SAMPLE_IFACE2_VLAN_TAG);
134-
if (ret < 0) {
135-
return ret;
136-
}
137-
138-
ret = setup_iface(iface, ud.second,
139-
CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR,
140-
CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR,
141-
CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_NETMASK,
142-
CONFIG_NET_SAMPLE_IFACE3_VLAN_TAG);
143-
if (ret < 0) {
144-
return ret;
145-
}
146-
147-
/* Bring up the VLAN interface automatically */
148-
net_if_up(ud.first);
149-
net_if_up(ud.second);
150-
151-
return ret;
152-
}
18+
#include "net_sample_common.h"
15319

15420
int main(void)
15521
{
156-
init_app();
22+
init_vlan();
15723
return 0;
15824
}

0 commit comments

Comments
 (0)