1.. _networking_with_native_sim_eth_bridge:
2
3Ethernet bridge with native_sim board
4#####################################
5
6.. contents::
7    :local:
8    :depth: 2
9
10This document describes how to set up a bridged Ethernet network between a (Linux) host
11and a Zephyr application running in a :ref:`native_sim <native_sim>` board.
12
13This setup is useful when testing the Ethernet bridging feature that can be enabled with
14:kconfig:option:`CONFIG_NET_ETHERNET_BRIDGE` Kconfig option. In this setup, the net-tools
15configuration creates two host network interfaces ``zeth0`` and ``zeth1`` and connects them
16to Zephyr's :ref:`native_sim <native_sim>` application.
17
18First create the host interfaces. In this example two interfaces are created.
19
20.. code-block:: console
21
22   cd $ZEPHYR_BASE/../tools/net-tools
23   ./net-setup.sh -c zeth-multiface.conf -i zeth0 -t 2
24
25The ``-c`` tells which configuration file to use, where ``zeth-multiface.conf`` is tailored
26for generating multiple network interfaces in the host.
27The ``-i`` option tells what is the first host interface name. The ``-t`` tells how
28many network interfaces to create.
29
30Example output of the host interfaces:
31
32.. code-block:: console
33
34   zeth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
35          inet 192.0.2.2  netmask 255.255.255.255  broadcast 0.0.0.0
36          inet6 2001:db8::2  prefixlen 128  scopeid 0x0<global>
37          inet6 fe80::200:5eff:fe00:5300  prefixlen 64  scopeid 0x20<link>
38          ether 00:00:5e:00:53:00  txqueuelen 1000  (Ethernet)
39          RX packets 33  bytes 2408 (2.4 KB)
40          RX errors 0  dropped 0  overruns 0  frame 0
41          TX packets 49  bytes 4092 (4.0 KB)
42          TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
43
44   zeth1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
45          inet 198.51.100.1  netmask 255.255.255.255  broadcast 0.0.0.0
46          inet6 fe80::200:5eff:fe00:5301  prefixlen 64  scopeid 0x20<link>
47          inet6 2001:db8:2::1  prefixlen 128  scopeid 0x0<global>
48          ether 00:00:5e:00:53:01  txqueuelen 1000  (Ethernet)
49          RX packets 21  bytes 1340 (1.3 KB)
50          RX errors 0  dropped 0  overruns 0  frame 0
51          TX packets 45  bytes 3916 (3.9 KB)
52          TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
53
54Then create a sample and enable Ethernet bridging support. In this example we create
55:zephyr:code-sample:`sockets-echo-server` sample application.
56
57.. code-block:: console
58
59   west build -p -b native_sim -d ../build/echo-server \
60      samples/net/sockets/echo_server -- \
61      -DCONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD="\"gnome-terminal -- screen %s\"" \
62      -DCONFIG_NET_ETHERNET_BRIDGE=y \
63      -DCONFIG_NET_ETHERNET_BRIDGE_SHELL=y \
64      -DCONFIG_ETH_NATIVE_POSIX_INTERFACE_COUNT=2 \
65      -DCONFIG_NET_IF_MAX_IPV6_COUNT=2 \
66      -DCONFIG_NET_IF_MAX_IPV4_COUNT=2
67   ../build/echo-server/zephyr/zephyr.exe -attach_uart
68
69This will create and run :zephyr:code-sample:`sockets-echo-server` with bridging enabled but
70not yet configured. To configure the bridging, you either need to use the bridge shell or call the
71bridging API directly from the application. We setup the bridging using the bridge shell like
72this:
73
74.. code-block:: console
75
76   net bridge addif 1 3 2
77   net iface up 1
78
79In the above example, the bridge interface index is 1, and interfaces 2 and 3 are
80Ethernet interfaces which are linked to interfaces ``zeth0`` and ``zeth1`` in the host side.
81
82The network interfaces look like this in Zephyr's side:
83
84.. code-block:: console
85
86   net iface
87   Hostname: zephyr
88
89   Interface bridge0 (0x8090ebc) (Virtual) [1]
90   ==================================
91   Virtual name : <enabled>
92   No attached network interface.
93   Link addr : 3B:DB:31:0F:CC:B6
94   MTU       : 1500
95   Flags     : NO_AUTO_START
96   Device    : BRIDGE_0 (0x8088354)
97   Promiscuous mode : disabled
98   IPv6 not enabled for this interface.
99   IPv4 not enabled for this interface.
100
101   Interface eth0 (0x8090fcc) (Ethernet) [2]
102   ===================================
103   Link addr : 02:00:5E:00:53:D2
104   MTU       : 1500
105   Flags     : AUTO_START,IPv4,IPv6
106   Device    : zeth0 (0x808837c)
107   Promiscuous mode : disabled
108   Ethernet capabilities supported:
109           TXTIME
110           Promiscuous mode
111   Ethernet PHY device: <none> (0)
112   IPv6 unicast addresses (max 3):
113           fe80::5eff:fe00:53d2 autoconf preferred infinite
114           2001:db8::1 manual preferred infinite
115   IPv6 multicast addresses (max 4):
116           ff02::1
117           ff02::1:ff00:53d2
118           ff02::1:ff00:1
119   IPv6 prefixes (max 2):
120           <none>
121   IPv6 hop limit           : 64
122   IPv6 base reachable time : 30000
123   IPv6 reachable time      : 18476
124   IPv6 retransmit timer    : 0
125   IPv4 unicast addresses (max 1):
126           192.0.2.1/255.255.255.0 manual preferred infinite
127   IPv4 multicast addresses (max 2):
128           224.0.0.1
129   IPv4 gateway : 0.0.0.0
130
131   Interface eth1 (0x80910dc) (Ethernet) [3]
132   ===================================
133   Link addr : 02:00:5E:00:53:87
134   MTU       : 1500
135   Flags     : AUTO_START,IPv4,IPv6
136   Device    : zeth1 (0x8088368)
137   Promiscuous mode : disabled
138   Ethernet capabilities supported:
139           TXTIME
140           Promiscuous mode
141   Ethernet PHY device: <none> (0)
142   IPv6 unicast addresses (max 3):
143           fe80::5eff:fe00:5387 autoconf preferred infinite
144   IPv6 multicast addresses (max 4):
145           ff02::1
146           ff02::1:ff00:5387
147   IPv6 prefixes (max 2):
148           <none>
149   IPv6 hop limit           : 64
150   IPv6 base reachable time : 30000
151   IPv6 reachable time      : 25158
152   IPv6 retransmit timer    : 0
153   IPv4 unicast addresses (max 1):
154           <none>
155   IPv4 multicast addresses (max 2):
156           224.0.0.1
157   IPv4 gateway : 0.0.0.0
158
159The ``net bridge`` command will show the current status of the bridging:
160
161.. code-block:: console
162
163   net bridge
164   Bridge Status   Config   Interfaces
165   1      up       ok       2 3
166
167The ``addif`` command adds Ethernet interfaces 2 and 3 to the bridge interface 1.
168After the ``addif`` command, the bridging is still disabled because the bridge interface
169is not up by default. The ``net iface up`` command will turn on bridging.
170
171If you have wireshark running in host side and monitoring ``zeth0`` and ``zeth1``,
172you should see the same network traffic in both host interfaces.
173
174Note that interface index numbers are not fixed, the bridge and Ethernet interface index
175values might be different in your setup.
176
177The bridging can be disabled by taking the bridge interface down, and the Ethernet interfaces
178can be removed from the bridge using ``delif`` command.
179
180.. code-block:: console
181
182   net iface down 1
183   net bridge delif 1 2 3
184