Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: net: sockets: echo-client: Add VLAN support
Add support for VLANs which are disabled by default. The application can be configured to use the VLANs by setting the IP addresses properly in config file. The VLAN support in this sample application is only meant for testing multiple network interface handling. The application can be compiled like this for VLAN support: cmake -DBOARD=qemu_x86 -DOVERLAY_CONFIG=overlay-vlan.conf .. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
- Loading branch information
Showing
6 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Kconfig - Private config options for echo-client sample app | ||
|
||
# | ||
# Copyright (c) 2018 Intel Corporation | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
mainmenu "Networking echo-client 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_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_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 "$(ZEPHYR_BASE)/Kconfig.zephyr" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
CONFIG_NET_VLAN=y | ||
|
||
# We have one non-vlan interface and two VLAN interfaces | ||
CONFIG_NET_VLAN_COUNT=3 | ||
|
||
# There will be three network interfaces. Note that the addresses are | ||
# selected so that our address ends to .1 and the peer one to .2 | ||
|
||
# First ethernet interface will use these settings | ||
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" | ||
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 ethernet 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" | ||
# VLAN tag for the second interface | ||
CONFIG_NET_SAMPLE_IFACE2_VLAN_TAG=100 | ||
|
||
# Settings for the third network interface | ||
CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR="2001:db8:200::1" | ||
# TEST-NET-3 from RFC 5737 | ||
CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR="203.0.113.1" | ||
# VLAN tag for the second interface | ||
CONFIG_NET_SAMPLE_IFACE3_VLAN_TAG=200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2018 Intel Corporation. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#define SYS_LOG_DOMAIN "echo-client" | ||
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG | ||
#define NET_LOG_ENABLED 1 | ||
|
||
#include <zephyr.h> | ||
|
||
#include <net/ethernet.h> | ||
|
||
/* User data for the interface callback */ | ||
struct ud { | ||
struct net_if *first; | ||
struct net_if *second; | ||
struct net_if *third; | ||
}; | ||
|
||
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(ETHERNET)) { | ||
return; | ||
} | ||
|
||
if (!ud->first) { | ||
ud->first = iface; | ||
return; | ||
} | ||
|
||
if (!ud->second) { | ||
ud->second = iface; | ||
return; | ||
} | ||
|
||
if (!ud->third) { | ||
ud->third = iface; | ||
return; | ||
} | ||
} | ||
|
||
static int setup_iface(struct net_if *iface, const char *ipv6_addr, | ||
const char *ipv4_addr, u16_t vlan_tag) | ||
{ | ||
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) { | ||
NET_ERR("Cannot enable VLAN for tag %d (%d)", vlan_tag, ret); | ||
} | ||
|
||
if (net_addr_pton(AF_INET6, ipv6_addr, &addr6)) { | ||
NET_ERR("Invalid address: %s", ipv6_addr); | ||
return -EINVAL; | ||
} | ||
|
||
ifaddr = net_if_ipv6_addr_add(iface, &addr6, NET_ADDR_MANUAL, 0); | ||
if (!ifaddr) { | ||
NET_ERR("Cannot add %s to interface %p", ipv6_addr, iface); | ||
return -EINVAL; | ||
} | ||
|
||
if (net_addr_pton(AF_INET, ipv4_addr, &addr4)) { | ||
NET_ERR("Invalid address: %s", ipv6_addr); | ||
return -EINVAL; | ||
} | ||
|
||
ifaddr = net_if_ipv4_addr_add(iface, &addr4, NET_ADDR_MANUAL, 0); | ||
if (!ifaddr) { | ||
NET_ERR("Cannot add %s to interface %p", ipv4_addr, iface); | ||
return -EINVAL; | ||
} | ||
|
||
NET_DBG("Interface %p VLAN tag %d setup done.", iface, vlan_tag); | ||
|
||
return 0; | ||
} | ||
|
||
int init_vlan(void) | ||
{ | ||
struct ud ud; | ||
int ret; | ||
|
||
memset(&ud, 0, sizeof(ud)); | ||
|
||
net_if_foreach(iface_cb, &ud); | ||
|
||
/* This sample has two VLANs. For the second one we need to manually | ||
* create IP address for this test. But first the VLAN needs to be | ||
* added to the interface so that IPv6 DAD can work properly. | ||
*/ | ||
ret = setup_iface(ud.second, | ||
CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR, | ||
CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_ADDR, | ||
CONFIG_NET_SAMPLE_IFACE2_VLAN_TAG); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
|
||
ret = setup_iface(ud.third, | ||
CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR, | ||
CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR, | ||
CONFIG_NET_SAMPLE_IFACE3_VLAN_TAG); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
|
||
return 0; | ||
} |