README.rst
1.. _eth-native-posix-sample:
2
3Native Posix Ethernet
4#####################
5
6Overview
7********
8
9The eth_native_posix sample application for Zephyr creates a **zeth** network
10interface to the host system. One can communicate with Zephyr via this network
11interface.
12
13The source code for this sample application can be found at:
14:zephyr_file:`samples/net/eth_native_posix`.
15
16Building And Running
17********************
18
19To build the eth_native_posix sample application, follow the steps
20below.
21
22.. zephyr-app-commands::
23 :zephyr-app: samples/net/eth_native_posix
24 :host-os: unix
25 :board: native_posix
26 :conf: <config file to use>
27 :goals: build
28 :compact:
29
30Normally one needs extra privileges to create and configure the TAP device in
31the host system. If the user has set the
32:kconfig:`CONFIG_ETH_NATIVE_POSIX_STARTUP_AUTOMATIC` option (this is disabled
33by default), then the user needs to use ``sudo`` to execute the Zephyr process
34with admin privileges, like this:
35
36.. code-block:: console
37
38 sudo --preserve-env=ZEPHYR_BASE make -Cbuild run
39
40If the ``sudo --preserve-env=ZEPHYR_BASE`` gives an error,
41just use ``sudo --preserve-env`` instead.
42
43If the :kconfig:`CONFIG_ETH_NATIVE_POSIX_STARTUP_AUTOMATIC` option
44is not enabled (this is the default), then the user should
45execute the ``net-setup.sh`` script from Zephyr `net-tools`_ repository.
46The script should be run before executing the Zephyr process. The script
47will create the zeth interface and set up IP addresses and routes.
48While running ``net-setup.sh`` requires root access, afterwards Zephyr
49process can be run as a non-root user.
50
51You can run the ``net-setup.sh`` script like this::
52
53 cd net-tools
54 sudo ./net-setup.sh
55
56or::
57
58 sudo ./net-setup.sh --config ./zeth-vlan.conf
59
60See also other command line options by typing ``net-setup.sh --help``.
61
62When the network interface is set up manually, you can leave the wireshark
63to monitor the interface, and then start and stop the zephyr process without
64stopping the wireshark.
65
66Setting things manually works the same as working with SLIP connectivity
67in QEMU.
68
69If you want to connect two Zephyr instances together, you can do it like this:
70
71Create two Zephyr config files prj1.conf and prj2.conf. You can use
72:zephyr_file:`samples/net/eth_native_posix/prj.conf` as a base.
73
74Set prj1.conf IP address configuration like this:
75
76.. code-block:: console
77
78 CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8:100::1"
79 CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8:100::2"
80 CONFIG_NET_CONFIG_MY_IPV4_ADDR="198.51.100.1"
81 CONFIG_NET_CONFIG_PEER_IPV4_ADDR="198.51.100.2"
82 CONFIG_NET_CONFIG_MY_IPV4_GW="203.0.113.1"
83 CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC=n
84 CONFIG_ETH_NATIVE_POSIX_MAC_ADDR="00:00:5e:00:53:64"
85 CONFIG_ETH_NATIVE_POSIX_SETUP_SCRIPT="echo"
86 CONFIG_ETH_NATIVE_POSIX_DRV_NAME="zeth.1"
87
88Set prj2.conf IP address configuration like this:
89
90.. code-block:: console
91
92 CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8:200::1"
93 CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8:200::2"
94 CONFIG_NET_CONFIG_MY_IPV4_ADDR="203.0.113.1"
95 CONFIG_NET_CONFIG_PEER_IPV4_ADDR="203.0.113.2"
96 CONFIG_NET_CONFIG_MY_IPV4_GW="198.51.100.1"
97 CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC=n
98 CONFIG_ETH_NATIVE_POSIX_MAC_ADDR="00:00:5e:00:53:c8"
99 CONFIG_ETH_NATIVE_POSIX_SETUP_SCRIPT="echo"
100 CONFIG_ETH_NATIVE_POSIX_DRV_NAME="zeth.2"
101
102Then compile and run two Zephyr instances
103(if ``sudo --preserve-env=ZEPHYR_BASE`` gives an error,
104just use ``sudo --preserve-env`` instead):
105
106.. code-block:: console
107
108 cmake -DCONF_FILE=prj1.conf -DBOARD=native_posix -Bbuild1/native_posix .
109 make -s -C build1/native_posix
110 sudo --preserve-env=ZEPHYR_BASE make -s -C build1/native_posix run
111
112.. code-block:: console
113
114 cmake -DCONF_FILE=prj2.conf -DBOARD=native_posix -Bbuild2/native_posix .
115 make -s -C build2/native_posix
116 sudo --preserve-env=ZEPHYR_BASE make -s -C build2/native_posix run
117
118Bridge the two Zephyr instances together:
119
120.. code-block:: console
121
122 sudo brctl addbr zeth-br
123 sudo brctl addif zeth-br zeth.1
124 sudo brctl addif zeth-br zeth.2
125 sudo ifconfig zeth-br up
126
127After this, you are able to ping device 1 from device 2 in net-shell:
128
129.. code-block:: console
130
131 # In device 1
132 net ping 2001:db8:200::1
133 net ping 203.0.113.1
134
135.. code-block:: console
136
137 # In device 2
138 net ping 2001:db8:100::1
139 net ping 198.51.100.1
140
141Note that in this setup you cannot access these two Zephyr devices from
142your host. If you want to do that, then you could create a new network
143interface with proper IP addresses and add that interface to the Zephyr
144bridge.
145
146.. _`net-tools`: https://github.com/zephyrproject-rtos/net-tools
147