samples: net: eth_native_posix: Sample for native networking
This application just enables native host networking and net-shell which allows connectivity from zephyr to host system via tap device. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
0f66426f4a
commit
e4212306cd
6
samples/net/eth_native_posix/CMakeLists.txt
Normal file
6
samples/net/eth_native_posix/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
|
||||
project(NONE)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
|
||||
include($ENV{ZEPHYR_BASE}/samples/net/common/common.cmake)
|
117
samples/net/eth_native_posix/README.rst
Normal file
117
samples/net/eth_native_posix/README.rst
Normal file
|
@ -0,0 +1,117 @@
|
|||
.. _eth-native-posix-sample:
|
||||
|
||||
Native Posix Ethernet
|
||||
#####################
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
The eth_native_posix sample application for Zephyr creates a **zeth** network
|
||||
interface to the host system. One can communicate with Zephyr via this network
|
||||
interface.
|
||||
|
||||
The source code for this sample application can be found at:
|
||||
:file:`samples/net/eth_native_posix`.
|
||||
|
||||
Building And Running
|
||||
********************
|
||||
|
||||
To build the eth_native_posix sample application, follow the steps
|
||||
below.
|
||||
|
||||
.. zephyr-app-commands::
|
||||
:zephyr-app: samples/net/eth_native_posix
|
||||
:board: native_posix
|
||||
:conf: <config file to use>
|
||||
:goals: build
|
||||
:compact:
|
||||
|
||||
Normally one needs extra priviliges to create and configure the TAP device in
|
||||
the host system. Use sudo to execute the Zephyr process with admin privileges,
|
||||
like this:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
sudo --preserve-env=ZEPHYR_BASE make run
|
||||
|
||||
If the ``sudo --preserve-env=ZEPHYR_BASE`` gives an error,
|
||||
just use ``sudo --preserve-env`` instead:
|
||||
|
||||
If you want to connect two Zephyr instances together, you can do it like this:
|
||||
|
||||
Create two Zephyr config files prj1.conf and prj2.conf. You can use
|
||||
:file:`samples/net/eth_native_posix/prj.conf` as a base.
|
||||
|
||||
Set prj1.conf IP address configuration like this:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8:100::1"
|
||||
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8:100::2"
|
||||
CONFIG_NET_APP_MY_IPV4_ADDR="198.51.100.1"
|
||||
CONFIG_NET_APP_PEER_IPV4_ADDR="198.51.100.2"
|
||||
CONFIG_NET_APP_MY_IPV4_GW="203.0.113.1"
|
||||
CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC=n
|
||||
CONFIG_ETH_NATIVE_POSIX_MAC_ADDR="00:00:5e:00:53:64"
|
||||
CONFIG_ETH_NATIVE_POSIX_SETUP_SCRIPT="echo"
|
||||
CONFIG_ETH_NATIVE_POSIX_DRV_NAME="zeth.1"
|
||||
|
||||
Set prj2.conf IP address configuration like this:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8:200::1"
|
||||
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8:200::2"
|
||||
CONFIG_NET_APP_MY_IPV4_ADDR="203.0.113.1"
|
||||
CONFIG_NET_APP_PEER_IPV4_ADDR="203.0.113.2"
|
||||
CONFIG_NET_APP_MY_IPV4_GW="198.51.100.1"
|
||||
CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC=n
|
||||
CONFIG_ETH_NATIVE_POSIX_MAC_ADDR="00:00:5e:00:53:c8"
|
||||
CONFIG_ETH_NATIVE_POSIX_SETUP_SCRIPT="echo"
|
||||
CONFIG_ETH_NATIVE_POSIX_DRV_NAME="zeth.2"
|
||||
|
||||
Then compile and run two Zephyr instances
|
||||
(if ``sudo --preserve-env=ZEPHYR_BASE`` gives an error,
|
||||
just use ``sudo --preserve-env`` instead):
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
mkdir -p build1/native_posix
|
||||
cmake -DCONF_FILE=prj1.conf -DBOARD=native_posix -Bbuild1/native_posix -H.
|
||||
make -s -C build1/native_posix
|
||||
sudo --preserve-env=ZEPHYR_BASE make -s -C build1/native_posix run
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
mkdir -p build2/native_posix
|
||||
cmake -DCONF_FILE=prj2.conf -DBOARD=native_posix -Bbuild2/native_posix -H.
|
||||
make -s -C build2/native_posix
|
||||
sudo --preserve-env=ZEPHYR_BASE make -s -C build2/native_posix run
|
||||
|
||||
Bridge the two Zephyr instances together:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
sudo brctl addbr zeth-br
|
||||
sudo brctl addif zeth-br zeth.1
|
||||
sudo brctl addif zeth-br zeth.2
|
||||
sudo ifconfig zeth-br up
|
||||
|
||||
After this, you are able to ping device 1 from device 2 in net-shell:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
# In device 1
|
||||
net ping 2001:db8:200::1
|
||||
net ping 203.0.113.1
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
# In device 2
|
||||
net ping 2001:db8:100::1
|
||||
net ping 198.51.100.1
|
||||
|
||||
Note that in this setup you cannot access these two Zephyr devices from
|
||||
your host. If you want to do that, then you could create a new network
|
||||
interface with proper IP addresses and add that interface to the Zephyr
|
||||
bridge.
|
81
samples/net/eth_native_posix/net_setup_host
Executable file
81
samples/net/eth_native_posix/net_setup_host
Executable file
|
@ -0,0 +1,81 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# Copyright (c) 2018 Intel Corporation
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# This script is called by native-posix board when TAP network interface
|
||||
# is taken up by Zephyr. The script should setup the host system so that
|
||||
# connectivity will work with Zephyr.
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
-f|--file)
|
||||
CONF_FILE="$2"
|
||||
shift
|
||||
shift;;
|
||||
-i|--interface)
|
||||
# Only first -i option is taken into account. This way
|
||||
# the driver added -i option is ignored if user has specified
|
||||
# the -i option to host setup script command.
|
||||
if [ -z "$IFACE" ]; then
|
||||
IFACE="$2"
|
||||
fi
|
||||
shift
|
||||
shift;;
|
||||
*)
|
||||
shift;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ `id -u` != 0 ]; then
|
||||
echo "Warning: This script will need admin rights to setup \
|
||||
network interface!"
|
||||
fi
|
||||
|
||||
if [ -z "$IFACE" ]; then
|
||||
IFACE="zeth"
|
||||
fi
|
||||
|
||||
if [ -z "$CONF_FILE" ]; then
|
||||
DIR=`dirname $0`
|
||||
CONF_FILE="$DIR/net_setup_host.conf"
|
||||
fi
|
||||
|
||||
if [ -f "$CONF_FILE" ]; then
|
||||
. $CONF_FILE
|
||||
else
|
||||
echo "Warning: config file $CONF_FILE does not exist!"
|
||||
fi
|
||||
|
||||
ip link set dev $IFACE up
|
||||
|
||||
if [ ! -z "$HWADDR" ]; then
|
||||
ip link set dev $IFACE address $HWADDR
|
||||
fi
|
||||
|
||||
if [ ! -z "$IPV6_ADDR_1" ]; then
|
||||
ip -6 address add $IPV6_ADDR_1 dev $IFACE
|
||||
fi
|
||||
|
||||
if [ ! -z "$IPV6_ROUTE_1" ]; then
|
||||
ip -6 route add $IPV6_ROUTE_1 dev $IFACE
|
||||
fi
|
||||
|
||||
if [ ! -z "$IPV4_ADDR_1" ]; then
|
||||
ip address add $IPV4_ADDR_1 dev $IFACE
|
||||
fi
|
||||
|
||||
if [ ! -z "$IPV4_ROUTE_1" ]; then
|
||||
ip route add $IPV4_ROUTE_1 dev $IFACE
|
||||
fi
|
11
samples/net/eth_native_posix/net_setup_host.conf
Normal file
11
samples/net/eth_native_posix/net_setup_host.conf
Normal file
|
@ -0,0 +1,11 @@
|
|||
#
|
||||
# Configuration options for setting up the host network interface
|
||||
#
|
||||
|
||||
HWADDR="00:00:5e:00:53:ff"
|
||||
|
||||
IPV6_ADDR_1="2001:db8::2"
|
||||
IPV6_ROUTE_1="2001:db8::/64"
|
||||
|
||||
IPV4_ADDR_1="192.0.2.2"
|
||||
IPV4_ROUTE_1="192.0.2.0/24"
|
51
samples/net/eth_native_posix/prj.conf
Normal file
51
samples/net/eth_native_posix/prj.conf
Normal file
|
@ -0,0 +1,51 @@
|
|||
CONFIG_NETWORKING=y
|
||||
CONFIG_NET_UDP=y
|
||||
CONFIG_NET_TCP=y
|
||||
CONFIG_NET_IPV6=y
|
||||
CONFIG_NET_IPV4=y
|
||||
#CONFIG_NET_DHCPV4=y
|
||||
|
||||
CONFIG_ENTROPY_GENERATOR=y
|
||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||
CONFIG_INIT_STACKS=y
|
||||
|
||||
CONFIG_SYS_LOG=y
|
||||
CONFIG_SYS_LOG_SHOW_COLOR=y
|
||||
CONFIG_NET_LOG=y
|
||||
CONFIG_SYS_LOG_NET_LEVEL=4
|
||||
|
||||
CONFIG_NET_STATISTICS=y
|
||||
|
||||
CONFIG_NET_PKT_RX_COUNT=32
|
||||
CONFIG_NET_PKT_TX_COUNT=32
|
||||
CONFIG_NET_BUF_RX_COUNT=32
|
||||
CONFIG_NET_BUF_TX_COUNT=32
|
||||
|
||||
CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
|
||||
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=4
|
||||
CONFIG_NET_MAX_CONTEXTS=10
|
||||
|
||||
CONFIG_NET_SHELL=y
|
||||
|
||||
CONFIG_NET_DEBUG_CORE=n
|
||||
CONFIG_NET_DEBUG_L2_ETHERNET=n
|
||||
CONFIG_NET_DEBUG_IF=n
|
||||
CONFIG_NET_DEBUG_IPV6=n
|
||||
CONFIG_NET_DEBUG_NET_PKT=y
|
||||
|
||||
CONFIG_NET_APP_SETTINGS=y
|
||||
CONFIG_NET_APP_NEED_IPV6=y
|
||||
CONFIG_NET_APP_NEED_IPV4=y
|
||||
CONFIG_NET_APP_SERVER=y
|
||||
CONFIG_NET_DEBUG_APP=n
|
||||
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
|
||||
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::2"
|
||||
CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1"
|
||||
CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2"
|
||||
|
||||
CONFIG_NET_L2_ETHERNET=y
|
||||
|
||||
CONFIG_ETH_NATIVE_POSIX=y
|
||||
CONFIG_SYS_LOG_ETHERNET_LEVEL=1
|
||||
CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC=y
|
||||
#CONFIG_ETH_NATIVE_POSIX_MAC_ADDR="00:00:5e:00:53:2a"
|
9
samples/net/eth_native_posix/sample.yaml
Normal file
9
samples/net/eth_native_posix/sample.yaml
Normal file
|
@ -0,0 +1,9 @@
|
|||
common:
|
||||
harness: net
|
||||
tags: net
|
||||
sample:
|
||||
description: Can be used to test native posix connectivity via ethernet.
|
||||
name: Native posix ethernet demo application
|
||||
tests:
|
||||
test_eth_native_posix:
|
||||
platform_whitelist: native_posix
|
18
samples/net/eth_native_posix/src/main.c
Normal file
18
samples/net/eth_native_posix/src/main.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <logging/sys_log.h>
|
||||
#include <errno.h>
|
||||
#include <board.h>
|
||||
|
||||
/* This application itself does nothing as there is net-shell that can be used
|
||||
* to monitor things.
|
||||
*/
|
||||
void main(void)
|
||||
{
|
||||
SYS_LOG_INF("Start application");
|
||||
}
|
Loading…
Reference in a new issue