connectivity/networking/network_monitoring.html · connectivity/networking/network_monitoring.rst 공식 원문 ↗

Zephyr 3.7.0 · Connectivity · Capture

Network Traffic Monitoring

Zephyr에서 capture한 RX·TX packet을 IP tunnel로 Linux host에 전달해 pcap과 Wireshark로 분석합니다.

Official pathconnectivity/networking/network_monitoring.html
Source fileconnectivity/networking/network_monitoring.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Host tunnel 생성부터 Zephyr capture setup·enable·상태 확인·종료와 Wireshark decapsulation까지 전체 절차입니다.

Tunnel

IPv4 · IPv6 · IPIP

Capture

Setup · Enable · Cleanup

Analysis

pcap · Wireshark

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 64EAE089856BB9D1AB15A8A52189C5C477B7B4A5382DA35456A8E0BBB64AB99D
.. _network_monitoring:

Monitor Network Traffic
#######################

.. contents::
    :local:
    :depth: 2

It is useful to be able to monitor the network traffic especially when
debugging a connectivity issues or when developing new protocol support in
Zephyr. This page describes how to set up a way to capture network traffic so
that user is able to use Wireshark or similar tool in remote host to see the
network packets sent or received by a Zephyr device.

See also the :zephyr:code-sample:`net-capture` sample application from the Zephyr
source distribution for configuration options that need to be enabled.

Host Configuration
******************

The instructions here describe how to setup a Linux host to capture Zephyr
network RX and TX traffic. Similar instructions should work also in other
operating systems.
On the Linux Host, fetch the Zephyr ``net-tools`` project, which is located
in a separate Git repository:

.. code-block:: console

   git clone https://github.com/zephyrproject-rtos/net-tools

The ``net-tools`` project provides a configure file to setup IP-to-IP tunnel
interface so that we can transfer monitoring data from Zephyr to host.

In terminal #1, type:

.. code-block:: console

   ./net-setup.sh -c zeth-tunnel.conf

This script will create following IPIP tunnel interfaces:

.. csv-table::
   :header: "Interface name", "Description"
   :widths: auto

   "``zeth-ip6ip``", "IPv6-over-IPv4 tunnel"
   "``zeth-ipip``", "IPv4-over-IPv4 tunnel"
   "``zeth-ipip6``", "IPv4-over-IPv6 tunnel"
   "``zeth-ip6ip6``", "IPv6-over-IPv6 tunnel"

Zephyr will send captured network packets to one of these interfaces.
The actual interface will depend on how the capturing is configured.
You can then use Wireshark to monitor the proper network interface.

After the tunneling interfaces have been created, you can use for example
``net-capture.py`` script from ``net-tools`` project to print or save the
captured network packets. The ``net-capture.py`` provides an UDP listener,
it can print the captured data to screen and optionally can also save the
data to a pcap file.

.. code-block:: console

   $ ./net-capture.py -i zeth-ip6ip -w capture.pcap
   [20210408Z14:33:08.959589] Ether / IP / ICMP 192.0.2.1 > 192.0.2.2 echo-request 0 / Raw
   [20210408Z14:33:08.976178] Ether / IP / ICMP 192.0.2.2 > 192.0.2.1 echo-reply 0 / Raw
   [20210408Z14:33:16.176303] Ether / IPv6 / ICMPv6 Echo Request (id: 0x9feb seq: 0x0)
   [20210408Z14:33:16.195326] Ether / IPv6 / ICMPv6 Echo Reply (id: 0x9feb seq: 0x0)
   [20210408Z14:33:21.194979] Ether / IPv6 / ICMPv6ND_NS / ICMPv6 Neighbor Discovery Option - Source Link-Layer Address 02:00:5e:00:53:3b
   [20210408Z14:33:21.217528] Ether / IPv6 / ICMPv6ND_NA / ICMPv6 Neighbor Discovery Option - Destination Link-Layer Address 00:00:5e:00:53:ff
   [20210408Z14:34:10.245408] Ether / IPv6 / UDP 2001:db8::2:47319 > 2001:db8::1:4242 / Raw
   [20210408Z14:34:10.266542] Ether / IPv6 / UDP 2001:db8::1:4242 > 2001:db8::2:47319 / Raw

The ``net-capture.py`` has following command line options:

.. code-block:: console

   Listen captured network data from Zephyr and save it optionally to pcap file.
   ./net-capture.py \
	-i | --interface <network interface>
		Listen this interface for the data
	[-p | --port <UDP port>]
		UDP port (default is 4242) where the capture data is received
	[-q | --quiet]
		Do not print packet information
	[-t | --type <L2 type of the data>]
		Scapy L2 type name of the UDP payload, default is Ether
	[-w | --write <pcap file name>]
		Write the received data to file in PCAP format

Instead of the ``net-capture.py`` script, you can for example use ``netcat``
to provide an UDP listener so that the host will not send port unreachable
message to Zephyr:

.. code-block:: console

   nc -l -u 2001:db8:200::2 4242 > /dev/null

The IP address above is the inner tunnel endpoint, and can be changed and
it depends on how the Zephyr is configured. Zephyr will send UDP packets
containing the captured network packets to the configured IP tunnel, so we
need to terminate the network connection like this.

Zephyr Configuration
********************

In this example, we use the ``native_sim`` board. You can also use any other board
that supports networking.

In terminal #3, type:

.. zephyr-app-commands::
   :zephyr-app: samples/net/capture
   :host-os: unix
   :board: native_sim
   :gen-args: -DCONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD=\""gnome-terminal -- screen %s"\"
   :goals: build
   :compact:

To see the Zephyr console and shell, start Zephyr instance like this:

.. code-block:: console

   build/zephyr/zephyr.exe -attach_uart

Any other application can be used too, just make sure that suitable
configuration options are enabled (see ``samples/net/capture/prj.conf`` file
for examples).

The network capture can be configured automatically if needed, but
currently the ``capture`` sample application does not do that. User has to use
``net-shell`` to setup and enable the monitoring.

The network packet monitoring needs to be setup first. The ``net-shell`` has
``net capture setup`` command for doing that. The command syntax is

.. code-block:: console

   net capture setup <remote-ip-addr> <local-ip-addr> <peer-ip-addr>
        <remote> is the (outer) endpoint IP address
        <local> is the (inner) local IP address
        <peer> is the (inner) peer IP address
        Local and Peer IP addresses can have UDP port number in them (optional)
        like 198.0.51.2:9000 or [2001:db8:100::2]:4242

In Zephyr console, type:

.. code-block:: console

   net capture setup 192.0.2.2 2001:db8:200::1 2001:db8:200::2

This command will create the tunneling interface. The ``192.0.2.2`` is the
remote host where the tunnel is terminated. The address is used to select
the local network interface where the tunneling interface is attached to.
The ``2001:db8:200::1`` tells the local IP address for the tunnel,
the ``2001:db8:200::2`` is the peer IP address where the captured network
packets are sent. The port numbers for UDP packet can be given in the
setup command like this for IPv6-over-IPv4 tunnel

.. code-block:: console

   net capture setup 192.0.2.2 [2001:db8:200::1]:9999 [2001:db8:200::2]:9998

and like this for IPv4-over-IPv4 tunnel

.. code-block:: console

   net capture setup 192.0.2.2 198.51.100.1:9999 198.51.100.2:9998

If the port number is omitted, then ``4242`` UDP port is used as a default.

The current monitoring configuration can be checked like this:

.. code-block:: console

   uart:~$ net capture
   Network packet capture disabled
                   Capture  Tunnel
   Device          iface    iface   Local                  Peer
   NET_CAPTURE0    -        1      [2001:db8:200::1]:4242  [2001:db8:200::2]:4242

which will print the current configuration. As we have not yet enabled
monitoring, the ``Capture iface`` is not set.

Then we need to enable the network packet monitoring like this:

.. code-block:: console

   net capture enable 2

The ``2`` tells the network interface which traffic we want to capture. In
this example, the ``2`` is the ``native_sim`` board Ethernet interface.
Note that we send the network traffic to the same interface that we are
monitoring in this example. The monitoring system avoids to capture already
captured network traffic as that would lead to recursion.
You can use ``net iface`` command to see what network interfaces are available.
Note that you cannot capture traffic from the tunnel interface as that would
cause recursion loop.
The captured network traffic can be sent to some other network interface
if configured so. Just set the ``<remote-ip-addr>`` option properly in
``net capture setup`` so that the IP tunnel is attached to desired network
interface.
The capture status can be checked again like this:

.. code-block:: console

   uart:~$ net capture
   Network packet capture enabled
                   Capture  Tunnel
   Device          iface    iface   Local                  Peer
   NET_CAPTURE0    2        1      [2001:db8:200::1]:4242  [2001:db8:200::2]:4242

After enabling the monitoring, the system will send captured (either received
or sent) network packets to the tunnel interface for further processing.

The monitoring can be disabled like this:

.. code-block:: console

   net capture disable

which will turn currently running monitoring off. The monitoring setup can
be cleared like this:

.. code-block:: console

   net capture cleanup

It is not necessary to use ``net-shell`` for configuring the monitoring.
The :ref:`network capture API <net_capture_interface>` functions can be called
by the application if needed.

Wireshark Configuration
***********************

The `Wireshark <https://www.wireshark.org/>`_ tool can be used to monitor the
captured network traffic in a useful way.

You can monitor either the tunnel interfaces or the ``zeth`` interface.
In order to see the actual captured data inside an UDP packet,
see `Wireshark decapsulate UDP`_ document for instructions.

.. _Wireshark decapsulate UDP:
   https://osqa-ask.wireshark.org/questions/28138/decoding-ethernet-encapsulated-in-tcp-or-udp/

Part 3

한국어 전문 번역

Network traffic monitoring

Connectivity 문제를 debug하거나 Zephyr에 새로운 protocol 지원을 개발할 때 network traffic을 감시할 수 있으면 유용합니다.

이 문서는 Zephyr device가 송수신하는 network packet을 remote host의 Wireshark 같은 도구로 볼 수 있도록 traffic capture 환경을 구성하는 방법을 설명합니다.

필요한 configuration option은 Zephyr source distribution의 net capture sample application도 참고하십시오.

Host 설정

다음 지침은 Linux host가 Zephyr network RX·TX traffic을 capture하도록 설정하는 방법입니다. 다른 operating system에도 비슷하게 적용할 수 있습니다.

Linux host에서 별도 Git repository의 Zephyr net-tools project를 내려받습니다.

git clone https://github.com/zephyrproject-rtos/net-tools

Net-tools에는 monitoring data를 Zephyr에서 host로 전송하기 위한 IP-to-IP tunnel interface 설정 file이 들어 있습니다.

Terminal #1에서 다음 명령을 실행합니다.

./net-setup.sh -c zeth-tunnel.conf

Script는 다음 IPIP tunnel interface를 생성합니다.

Interface 이름설명
zeth-ip6ipIPv6-over-IPv4 tunnel
zeth-ipipIPv4-over-IPv4 tunnel
zeth-ipip6IPv4-over-IPv6 tunnel
zeth-ip6ip6IPv6-over-IPv6 tunnel

Zephyr는 capture한 network packet을 이 interface 가운데 하나로 보냅니다. 실제 interface는 capture configuration에 따라 결정되며, Wireshark에서 해당 interface를 선택해 감시할 수 있습니다.

net-capture.py는 UDP listener를 제공해 capture packet을 화면에 출력하고 선택적으로 pcap file에 저장합니다. Tunnel interface를 만든 뒤 다음과 같이 실행할 수 있습니다.

$ ./net-capture.py -i zeth-ip6ip -w capture.pcap
[20210408Z14:33:08.959589] Ether / IP / ICMP 192.0.2.1 > 192.0.2.2 echo-request 0 / Raw
[20210408Z14:33:08.976178] Ether / IP / ICMP 192.0.2.2 > 192.0.2.1 echo-reply 0 / Raw
[20210408Z14:33:16.176303] Ether / IPv6 / ICMPv6 Echo Request (id: 0x9feb seq: 0x0)
[20210408Z14:33:16.195326] Ether / IPv6 / ICMPv6 Echo Reply (id: 0x9feb seq: 0x0)
[20210408Z14:33:21.194979] Ether / IPv6 / ICMPv6ND_NS / ICMPv6 Neighbor Discovery Option - Source Link-Layer Address 02:00:5e:00:53:3b
[20210408Z14:33:21.217528] Ether / IPv6 / ICMPv6ND_NA / ICMPv6 Neighbor Discovery Option - Destination Link-Layer Address 00:00:5e:00:53:ff
[20210408Z14:34:10.245408] Ether / IPv6 / UDP 2001:db8::2:47319 > 2001:db8::1:4242 / Raw
[20210408Z14:34:10.266542] Ether / IPv6 / UDP 2001:db8::1:4242 > 2001:db8::2:47319 / Raw

net-capture.py command-line option은 다음과 같습니다.

  Listen captured network data from Zephyr and save it optionally to pcap file.
  ./net-capture.py \
-i | --interface <network interface>
	Listen this interface for the data
[-p | --port <UDP port>]
	UDP port (default is 4242) where the capture data is received
[-q | --quiet]
	Do not print packet information
[-t | --type <L2 type of the data>]
	Scapy L2 type name of the UDP payload, default is Ether
[-w | --write <pcap file name>]
	Write the received data to file in PCAP format

net-capture.py 대신 netcat을 UDP listener로 실행해 host가 Zephyr에 port unreachable message를 보내지 않게 할 수도 있습니다.

nc -l -u 2001:db8:200::2 4242 > /dev/null

위 IP address는 inner tunnel endpoint입니다. Zephyr configuration에 따라 바꿀 수 있습니다. Zephyr는 capture packet을 포함한 UDP packet을 설정된 IP tunnel로 보내므로 host가 이 network connection을 terminate해야 합니다.

Zephyr 설정

이 예제는 native_sim board를 사용하지만 networking을 지원하는 다른 board도 사용할 수 있습니다.

Terminal #3에서 capture sample을 west build -b native_sim samples/net/capture로 build하고, CONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD gen-arg로 gnome-terminal과 screen 자동 연결 command를 지정할 수 있습니다.

Zephyr console과 shell을 보려면 다음처럼 instance를 시작합니다.

build/zephyr/zephyr.exe -attach_uart

samples/net/capture/prj.conf의 예처럼 적절한 configuration option을 켜면 다른 application도 사용할 수 있습니다.

Network capture는 자동 구성할 수도 있지만 현재 capture sample은 자동 구성하지 않습니다. 사용자가 net-shell로 monitoring을 setup하고 enable해야 합니다.

net capture setup command로 먼저 network packet monitoring을 설정합니다. Syntax는 다음과 같습니다.

net capture setup <remote-ip-addr> <local-ip-addr> <peer-ip-addr>
     <remote> is the (outer) endpoint IP address
     <local> is the (inner) local IP address
     <peer> is the (inner) peer IP address
     Local and Peer IP addresses can have UDP port number in them (optional)
     like 198.0.51.2:9000 or [2001:db8:100::2]:4242

Zephyr console에서 다음 명령을 입력합니다.

net capture setup 192.0.2.2 2001:db8:200::1 2001:db8:200::2

192.0.2.2는 tunnel을 terminate하는 remote host이며 tunnel을 연결할 local network interface를 선택하는 데 사용됩니다. 2001:db8:200::1은 tunnel local IP, 2001:db8:200::2는 capture packet을 보낼 peer IP입니다.

IPv6-over-IPv4 tunnel에서는 setup command에 UDP port를 다음처럼 넣을 수 있습니다.

net capture setup 192.0.2.2 [2001:db8:200::1]:9999 [2001:db8:200::2]:9998

IPv4-over-IPv4 tunnel은 다음과 같습니다.

net capture setup 192.0.2.2 198.51.100.1:9999 198.51.100.2:9998

Port number를 생략하면 기본 UDP port 4242를 사용합니다.

현재 monitoring configuration은 다음처럼 확인합니다.

uart:~$ net capture
Network packet capture disabled
                Capture  Tunnel
Device          iface    iface   Local                  Peer
NET_CAPTURE0    -        1      [2001:db8:200::1]:4242  [2001:db8:200::2]:4242

아직 monitoring을 활성화하지 않았으므로 Capture iface가 설정되지 않았습니다.

다음 명령으로 network packet monitoring을 활성화합니다.

net capture enable 2

2는 capture할 traffic의 network interface 번호이며 이 예에서는 native_sim board의 Ethernet interface입니다.

이 예는 감시 중인 interface로 capture traffic도 전송합니다. Monitoring system은 이미 capture한 traffic을 다시 capture하면 recursion이 생기므로 이를 자동으로 피합니다.

net iface command로 사용 가능한 network interface를 확인할 수 있습니다. Recursion loop를 일으키므로 tunnel interface 자체의 traffic은 capture할 수 없습니다.

net capture setup의 remote-ip-addr를 올바르게 지정해 IP tunnel을 원하는 interface에 연결하면 capture traffic을 다른 network interface로 보낼 수도 있습니다.

활성화 뒤 capture status는 다음처럼 표시됩니다.

uart:~$ net capture
Network packet capture enabled
                Capture  Tunnel
Device          iface    iface   Local                  Peer
NET_CAPTURE0    2        1      [2001:db8:200::1]:4242  [2001:db8:200::2]:4242

Monitoring을 활성화하면 system은 수신 또는 송신한 capture packet을 후속 처리를 위해 tunnel interface로 보냅니다.

Monitoring은 다음 명령으로 비활성화합니다.

net capture disable

현재 실행 중인 monitoring이 중지됩니다. Setup 자체는 다음 명령으로 지웁니다.

net capture cleanup

Monitoring configuration에 net-shell이 반드시 필요한 것은 아닙니다. Application이 network capture API를 직접 호출해도 됩니다.

Wireshark 설정

Wireshark를 사용하면 capture한 network traffic을 편리하게 분석할 수 있습니다.

zeth-ip6ip 같은 tunnel interface 또는 zeth interface를 감시할 수 있습니다. UDP packet 안에 encapsulate된 실제 capture data를 보려면 Wireshark의 UDP decapsulation 절차를 적용하십시오.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.