zephyr 连接·网络

 

网络概述、网络协议栈架构

  • 网络概述、网络协议栈架构

Overview 概述

Supported Features 支援功能

The networking IP stack is modular and highly configurable via build-time configuration options. You can minimize system memory consumption by enabling only those network features required by your application. Almost all features can be disabled if not needed.

网络 IP 堆栈是模块化的,并且通过构建时配置选项高度可配置。通过仅启用应用程序所需的那些网络特性,可以最大限度地减少系统内存消耗。如果不需要,几乎所有的特性都可以禁用。

  • IPv6 The support for IPv6 is enabled by default. Various IPv6 sub-options can be enabled or disabled depending on networking needs.

    IPv6默认情况下支持 IPv6。根据网络需要,可以启用或禁用各种 IPv6子选项。

    • Developer can set the number of unicast and multicast IPv6 addresses that are active at the same time.

      开发人员可以同时设置活动的单播和多播 IPv6地址的数量。

    • The IPv6 address for the device can be set either statically or dynamically using SLAAC (Stateless Address Auto Configuration) (RFC 4862).

      可以使用 SLAAC (无状态地址自动配置)(RFC4862)静态或动态地设置设备的 IPv6地址。

    • The system also supports multiple IPv6 prefixes and the maximum IPv6 prefix count can be configured at build time.

      系统还支持多个 IPv6前缀,可以在构建时配置最大 IPv6前缀数。

    • The IPv6 neighbor cache can be disabled if not needed, and its size can be configured at build time.

      如果不需要,可以禁用 IPv6邻居缓存,并且可以在构建时配置其大小。

    • The IPv6 neighbor discovery support (RFC 4861) is enabled by default.

      默认情况下启用 IPv6邻居发现支持(RFC 4861)。

    • Multicast Listener Discovery v2 support (RFC 3810) is enabled by default.

      默认情况下启用多播监听器发现 v2支持(RFC 3810)。

    • IPv6 header compression (6lo) is available for IPv6 connectivity for Bluetooth IPSP (RFC 7668) and IEEE 802.15.4 networks (RFC 4944).

      IPv6报头压缩(6lo)可用于蓝牙 IPSP (RFC 7668)和 IEEE 802.15.4网络(RFC 4944)的 IPv6连接。

  • IPv4 The legacy IPv4 is supported by the networking stack. It cannot be used by IEEE 802.15.4 or Bluetooth IPSP as those network technologies support only IPv6. IPv4 can be used in Ethernet based networks. By default IPv4 support is disabled.

    网络栈支持传统的 IPv4。由于 IEEE 802.15.4或蓝牙 IPSP 只支持 IPv6,因此不能使用。IPv4可用于基于以太网的网络。默认情况下禁用 IPv4支持。

    • DHCP (Dynamic Host Configuration Protocol) client is supported (RFC 2131).

      支持 DHCP (DHCP)客户端(rfc2131)。

    • The IPv4 address can also be configured manually. Static IPv4 addresses are supported by default.

      IPv4地址也可以手动配置。默认情况下支持静态 IPv4地址。

  • Dual stack support. The networking stack allows a developer to configure the system to use both IPv6 and IPv4 at the same time.

    双栈支持。网络栈允许开发人员将系统配置为同时使用 IPv6和 IPv4。

  • UDP User Datagram Protocol (RFC 768) is supported. The developer can send UDP datagrams (client side support) or create a listener to receive UDP packets destined to certain port (server side support).

    支持 UDP 用户数据报协议(rfc768)。开发人员可以发送 UDP 数据报(客户端支持)或创建一个侦听器来接收发往特定端口(服务器端支持)的 UDP 数据包。

  • TCP Transmission Control Protocol (RFC 793) is supported. Both server and client roles can be used the the application. The amount of TCP sockets that are available to applications can be configured at build time.

    支援 TCP 传输控制协议(rfc793)。应用程序可以使用服务器和客户端角色。可以在构建时配置应用程序可用的 TCP 套接字数量。

  • BSD Sockets API Support for a subset of a BSD sockets compatible API is implemented. Both blocking and non-blocking datagram (UDP) and stream (TCP) sockets are supported.

    实现了对 BSD 套接字兼容 API 子集的支持。支持阻塞和非阻塞数据报(UDP)和流(TCP)套接字。

  • Secure Sockets API Experimental support for TLS/DTLS secure protocols and configuration options for sockets API. Secure functions for the implementation are provided by mbedTLS library.

    安全套接字 API 对 TLS/DTLS 安全协议和套接字 API 配置选项的实验性支持。实现的安全函数由 mbedTLS 库提供。

  • MQTT Message Queue Telemetry Transport (ISO/IEC PRF 20922) is supported. A sample MQTT Publisher client application for MQTT v3.1.1 is implemented.

    支持 MQTT 消息队列遥测传输(ISO/IEC PRF 20922)。实现了一个用于 MQTT v3.1.1的示例 MQTT Publisher 客户机应用程序。

  • CoAP Constrained Application Protocol (RFC 7252) is supported. Both CoAP client and CoAP Server sample applications are implemented.

    支持 CoAP 约束应用程序协议(RFC 7252) ,实现了 CoAP 客户端和 CoAP 服务器示例应用程序。

  • LWM2M OMA Lightweight Machine-to-Machine Protocol (LwM2M specification 1.0.2) is supported via the “Bootstrap”, “Client Registration”, “Device Management & Service Enablement” and “Information Reporting” interfaces. The required core LwM2M objects are implemented as well as several IPSO Smart Objects. LwM2M client implements the library as an example.

    LWM2M OMA 轻量级计算机对计算机协议(LwM2M 规范1.0.2)通过“引导”、“客户端注册”、“设备管理和服务支持”和“信息报告”接口得到支持。实现了所需的核心 LwM2M 对象以及几个 IPSO 智能对象。LwM2M 客户端作为示例实现该库。

  • DNS Domain Name Service (RFC 1035) client functionality is supported. Applications can use the DNS API to query domain name information or IP addresses from the DNS server. Both IPv4 (A) and IPv6 (AAAA) records can be queried. Both multicast DNS (mDNS) (RFC 6762) and link-local multicast name resolution (LLMNR) (RFC 4795) are supported.

    支持 DNS 域名服务(RFC 1035)客户端功能。应用程序可以使用 DNS API 从 DNS 服务器查询域名信息或 IP 地址。可以查询 IPv4(A)和 IPv6(AAAA)记录。支持多播 DNS (mDNS)(RFC 6762)和链路本地多播名称解析(LLMNR)(RFC 4795)。

  • Network Management API. Applications can use network management API to listen management events generated by core stack when for example IP address is added to the device, or network interface is coming up etc.

    网络管理 API。应用程序可以使用网络管理 API 来监听由核心堆栈生成的管理事件,例如当 IP 地址被添加到设备或网络接口出现等。

  • Multiple Network Technologies. The Zephyr OS can be configured to support multiple network technologies at the same time simply by enabling them in Kconfig: for example, Ethernet and 802.15.4 support. Note that no automatic IP routing functionality is provided between these technologies. Applications can send data according to their needs to desired network interface.

    多网络技术。Zephyr 操作系统可以配置为同时支持多种网络技术,只需在 Kconfig 启用它们,例如支持以太网和802.15.4。注意,在这些技术之间没有提供自动 IP 路由功能。应用程序可以根据需要将数据发送到所需的网络接口。

  • Minimal Copy Network Buffer Management. It is possible to have minimal copy network data path. This means that the system tries to avoid copying application data when it is sent to the network.

    最小复制网络缓冲区管理。有可能具有最小的复制网络数据路径。这意味着当应用程序数据发送到网络时,系统会尝试避免复制应用程序数据。

  • Virtual LAN support. Virtual LANs (VLANs) allow partitioning of physical ethernet networks into logical networks. See VLAN support for more details.

    支持虚拟局域网。虚拟局域网(VLAN)允许将物理以太网划分为逻辑网络。有关详细信息,请参阅 VLAN 支持。

  • Network traffic classification. The sent and received network packets can be prioritized depending on application needs. See traffic classification for more details.

    网络流量分类。根据应用程序的需要,可以对发送和接收的网络数据包进行优先级排序。有关详细信息,请参阅流量分类。

  • Time Sensitive Networking. The gPTP (generalized Precision Time Protocol) is supported. See gPTP support for more details.

    时间敏感网络。支持 gPTP (通用精密时间协议)。详情请参阅 gPTP 支持。

  • Network shell. The network shell provides helpers for figuring out network status, enabling/disabling features, and issuing commands like ping or DNS resolving. The net-shell is useful when developing network software. See network shell for more details.

    网络外壳。网络 shell 提供了帮助工具,用于计算网络状态、启用/禁用特性以及发出诸如 ping 或 DNS 解析之类的命令。网壳在开发网络软件时非常有用。有关详细信息,请参阅网络 shell。

Additionally these network technologies (link layers) are supported in Zephyr OS v1.7 and later:

此外,Zephyr OS v1.7及更高版本支持这些网络技术(链接层) :

  • IEEE 802.15.4

  • Bluetooth 蓝牙

  • Ethernet 以太网

  • SLIP (IP over serial line). Used for testing with QEMU. It provides ethernet interface to host system (like Linux) and test applications can be run in Linux host and send network data to Zephyr OS device.

    串行线路上的 IP。用于 QEMU 测试。它为主机系统(如 Linux)提供以太网接口,测试应用程序可以在 Linux 主机上运行,并向 Zephyr OS 设备发送网络数据。

Source Tree Layout 源码布局

The networking stack source code tree is organized as follows:

网络堆栈源代码树的组织方式如下:

  • subsys/net/ip/

    This is where the IP stack code is located.

    这是 IP 堆栈代码所在的位置。

  • subsys/net/l2/

    This is where the IP stack layer 2 code is located. This includes generic support for Bluetooth IPSP adaptation, Ethernet, IEEE 802.15.4 and Wi-Fi.

    这是 IP 栈第2层代码所在的位置。这包括对蓝牙 IPSP 适配、以太网、 IEEE 802.15.4和 Wi-Fi 的通用支持。

  • subsys/net/lib/

    Application-level protocols (DNS, MQTT, etc.) and additional stack components (BSD Sockets, etc.).

    应用程序级协议(DNS、 MQTT 等)和其他堆栈组件(BSD 套接字等)。

  • include/net/

    Public API header files. These are the header files applications need to include to use IP networking functionality.

    公共 API 头文件。这些是应用程序使用 IP 网络功能需要包含的头文件。

  • samples/net/

    Sample networking code. This is a good reference to get started with network application development.

    网络代码示例。这是开始网络应用程序开发的一个很好的参考。

  • tests/net/

    Test applications. These applications are used to verify the functionality of the IP stack, but are not the best source for sample code (see samples/net instead).

    测试申请。这些应用程序用于验证 IP 堆栈的功能,但不是示例代码的最佳来源(请参见示例/net)。

Network Stack Architecture 网络栈架构

The Zephyr network stack is a native network stack specifically designed for Zephyr OS. It consists of layers, each meant to provide certain services to other layers. Network stack functionality is highly configurable via Kconfig options.

Zephyr 网络栈是专门为 Zephyr OS 设计的本机网络栈。它由多层组成,每一层都意味着向其他层提供某些服务。网络堆栈功能可以通过 Kconfig 选项高度配置。

High level overview of the network stack

网络堆栈的高级概述

Overview of the network stack architecture

Fig. 30 Network stack overview

图30网络栈概况

The network stack is layered and consists of the following parts:

网络栈是分层的,由以下部分组成:

  • Network Application. The network application can either use the provided application-level protocol libraries or access the BSD socket API directly to create a network connection, send or receive data, and close a connection. The application can also use the network management API to configure the network and set related parameters such as network link options, starting a scan (when applicable), listen network configuration events, etc. The network interface API can be used to set IP address to a network interface, taking the network interface down, etc.

    网络应用。网络应用程序可以使用提供的应用程序级协议库,也可以直接访问 BSD 套接字 API 来创建网络连接、发送或接收数据并关闭连接。应用程序还可以使用网络管理 API 来配置网络和设置相关参数,如网络链接选项、启动扫描(如果适用)、监听网络配置事件等。网络接口 API 可以用来将 IP 地址设置为网络接口、关闭网络接口等。

  • Network Protocols. This provides implementations for various protocols such as

    网络协议。这提供了各种协议的实现,例如

    • Application-level network protocols like CoAP, LWM2M, and MQTT. See application protocols chapter for information about them.

      应用程序级网络协议,如 CoAP、 LWM2M 和 MQTT。有关它们的信息,请参阅应用程序协议一章。

    • Core network protocols like IPv6, IPv4, UDP, TCP, ICMPv4, and ICMPv6. You access these protocols by using the BSD socket API.

      核心网络协议,如 IPv6、 IPv4、 UDP、 TCP、 ICMPv4和 ICMPv6。您可以使用 BSD 套接字 API 访问这些协议。

  • Network Interface Abstraction. This provides functionality that is common in all the network interfaces, such as setting network interface down, etc. There can be multiple network interfaces in the system. See network interface overview for more details.

    网络接口抽象。这提供了在所有网络接口中通用的功能,例如设置网络接口,等等。系统中可以有多个网络接口。有关详细信息,请参阅网络接口概述。

  • L2 Network Technologies. This provides a common API for sending and receiving data to and from an actual network device. See L2 overview for more details. These network technologies include Ethernet, IEEE 802.15.4, Bluetooth, CANBUS, etc. Some of these technologies support IPv6 header compression (6Lo), see RFC 6282 for details. For example ARP for IPv4 is done by the Ethernet component.

    L2网络技术。这提供了一个用于向实际网络设备发送和接收数据的通用 API。有关更多细节,请参见 L2概述。这些网络技术包括以太网、 IEEE 802.15.4、蓝牙、 CANBUS 等。其中一些技术支持 IPv6报头压缩(6Lo) ,详情请参阅 RFC 6282。例如,IPv4的 ARP 是由以太网组件完成的。

  • Network Device Drivers. The actual low-level device drivers handle the physical sending or receiving of network packets.

    网络设备驱动程序: 实际的低级设备驱动程序处理网络数据包的物理发送或接收。

Network data flow 网络数据流

An application typically consists of one or more threads that execute the application logic. When using the BSD socket API, the following things will happen.

应用程序通常由一个或多个执行应用程序逻辑的线程组成。当使用 BSD 套接字 API 时,会发生以下情况。

Network RX data flow

Fig. 31 Network RX data flow

图31网络 RX 数据流

Data receiving (RX) 数据接收(RX)

  1. A network data packet is received by a device driver.

    设备驱动程序接收网络数据包。

  2. The device driver allocates enough network buffers to store the received data. The network packet is placed in the proper RX queue (implemented by k_fifo). By default there is only one receive queue in the system, but it is possible to have up to 8 receive queues. These queues will process incoming packets with different priority. See Traffic Classification for more details. The receive queues also act as a way to separate the data processing pipeline (bottom-half) as the device driver is running in an interrupt context and it must do its processing as fast as possible.

    设备驱动程序分配足够的网络缓冲区来存储接收到的数据。网络数据包被放置在正确的 RX 队列中(由 k _ fifo 实现)。默认情况下,系统中只有一个接收队列,但最多可能有8个接收队列。这些队列将处理具有不同优先级的传入数据包。有关详细信息,请参阅流量分类。当设备驱动程序在中断上下文中运行时,接收队列还起到分离数据处理管道(下半部分)的作用,并且它必须尽可能快地完成处理。

  3. The network packet is then passed to the correct L2 driver. The L2 driver can check if the packet is proper and modify it if needed, e.g. strip L2 header and frame check sequence, etc.

    然后将网络数据包传递给正确的 L2驱动程序。L2驱动程序可以检查数据包是否正确,并在需要时对其进行修改,例如剥离 L2报头和帧检查序列等。

  4. The packet is processed by a network interface. The network statistics are collected if enabled by CONFIG_NET_STATISTICS.

    数据包由网络接口处理。如果 CONFIG _ NET _ STATISTICS 启用,则收集网络统计信息。

  5. The packet is then passed to L3 processing. If the packet is IP based, then the L3 layer checks if the packet is a proper IPv6 or IPv4 packet.

    然后数据包被传递给 L3处理。如果数据包是基于 IP 的,那么 L3层检查数据包是否是正确的 IPv6或 IPv4数据包。

  6. A socket handler then finds an active socket to which the network packet belongs and puts it in a queue for that socket, in order to separate the networking code from the application. Typically the application is run in userspace context and the network stack is run in kernel context.

    然后,套接字处理程序找到网络数据包所属的活动套接字,并将其放入该套接字的队列中,以便将网络代码与应用程序分离。通常,应用程序在用户空间上下文中运行,而网络堆栈在内核上下文中运行。

  7. The application will then receive the data and can process it as needed. The application should have used the BSD socket API to create a socket that will receive the data.

    然后应用程序将接收数据,并可以根据需要进行处理。应用程序应该使用 BSD 套接字 API 来创建一个将接收数据的套接字。

Network TX data flow

Fig. 32 Network TX data flow

图32网络 TX 数据流

Data sending (TX) 数据发送(TX)

  1. The application should use the BSD socket API when sending the data.

    应用程序在发送数据时应该使用 BSD 套接字 API。

  2. The application data is prepared for sending to kernel space and then copied to internal net_buf structures.

    应用程序数据准备发送到内核空间,然后复制到内部 net _ buf 结构。

  3. Depending on the socket type, a protocol header is added in front of the data. For example, if the socket is a UDP socket, then a UDP header is constructed and placed in front of the data.

    根据套接字类型,在数据前面添加一个协议头。例如,如果套接字是一个 UDP 套接字,则构造一个 UDP 头并将其放在数据前面。

  4. An IP header is added to the network packet for a UDP or TCP packet.

    IP 报头被添加到 UDP 或 TCP 数据包的网络数据包中。

  5. The network stack will check that the network interface is properly set for the network packet, and also will make sure that the network interface is enabled before the data is queued to be sent.

    网络堆栈将检查网络接口是否为网络数据包正确设置,并且还将确保在数据排队发送之前启用网络接口。

  6. The network packet is then classified and placed to the proper transmit queue (implemented by k_fifo). By default there is only one transmit queue in the system, but it is possible to have up to 8 transmit queues. These queues will process the sent packets with different priority. See Traffic Classification for more details. After the transmit packet classification, the packet is checked by the correct L2 layer module. The L2 module will do additional checks for the data and it will also create any L2 headers for the network packet. If everything is ok, the data is given to the network device driver to be sent out.

    然后对网络数据包进行分类,并将其放置到合适的传输队列中(由 k _ fifo 实现)。默认情况下,系统中只有一个传输队列,但是最多可能有8个传输队列。这些队列将处理具有不同优先级的已发送数据包。有关详细信息,请参阅流量分类。在传输分组分类后,由正确的 L2层模块检查分组。L2模块将对数据进行额外的检查,它还将为网络数据包创建任何 L2报头。如果一切正常,则将数据交给网络设备驱动程序发送出去。

  7. The device driver will send the packet to the network.

    设备驱动程序将数据包发送到网络。

Note that in both the TX and RX data paths, the queues (k_fifo’s) form separation points where data is passed from one thread to another. These threads might run in different contexts (kernel vs. userspace) and with different priorities.

请注意,在 TX 和 RX 数据路径中,队列(k _ fifo’s)形成了分离点,数据从一个线程传递到另一个线程。这些线程可能在不同的上下文(内核与用户空间)中运行,并具有不同的优先级。

Network packet processing statistics 网络分组处理统计

See information about network processing statistics here.

在这里查看有关网络处理统计数据的信息。

Network Connectivity API 网络连接 API

Applications should use the BSD socket API defined in include/zephyr/net/socket.h to create a connection, send or receive data, and close a connection. The same API can be used when working with UDP or TCP data. See BSD socket API for more details.

应用程序应该使用 include/zephyr/net/socket.h 中定义的 BSD 套接字 API 来创建连接、发送或接收数据以及关闭连接。在处理 UDP 或 TCP 数据时可以使用相同的 API。有关详细信息,请参阅 BSD 套接字 API。

See Socket Echo Server and Socket Echo Client applications how to create a simple server or client BSD socket based application.

请参阅 Socket Echo Server 和 Socket Echo Client 应用程序如何创建简单的服务器或基于 BSD 套接字的客户端应用程序。

The legacy connectivity API in include/zephyr/net/net_context.h should not be used by applications.

应用程序不应该使用 /zephyr/net/net_context.h 中的遗留连接 API。

Networking with the host system

与主机系统联网

While developing networking software, it is usually necessary to connect and exchange data with the host system like a Linux desktop computer. Depending on what board is used for development, the following options are possible:

在开发网络软件时,通常需要与主机系统(如 Linux 桌面计算机)连接和交换数据。根据用于开发的板块,可以采用以下选项:

  • QEMU using SLIP (Serial Line Internet Protocol).

    使用 SLIP (串行线因特网协议)的 QEMU。

    • Here IP packets are exchanged between Zephyr and the host system via serial port. This is the legacy way of transferring data. It is also quite slow so use it only when necessary. See Networking with QEMU for details.

      在这里,泽弗和主机系统之间通过串口交换 IP 数据包。这是传输数据的传统方式。它也相当慢,所以只有在必要的时候使用它。有关详细信息,请参阅与 QEMU 联网。

  • QEMU using built-in Ethernet driver.

    使用内置以太网驱动程序的 QEMU。

    • Here IP packets are exchanged between Zephyr and the host system via QEMU’s built-in Ethernet driver. Not all QEMU boards support built-in Ethernet so in some cases, you might need to use the SLIP method for host connectivity. See Networking with QEMU Ethernet for details.

      在这里,Zephyr和主机系统之间通过 QEMU 内置的以太网驱动程序交换 IP 数据包。并非所有 QEMU 板都支持内置的以太网,因此在某些情况下,您可能需要使用 SLIP 方法进行主机连接。有关详细信息,请参阅与 QEMU 以太网联网。

  • QEMU using SLIRP (Qemu User Networking).

    使用 SLIRP (QEMU 用户网络)的 QEMU。

    • QEMU User Networking is implemented using “slirp”, which provides a full TCP/IP stack within QEMU and uses that stack to implement a virtual NAT’d network. As this support is built into QEMU, it can be used with any model and requires no admin privileges on the host machine, unlike TAP. However, it has several limitations including performance which makes it less valuable for practical purposes. See Networking with QEMU User for details.

      QEMU 用户网络是使用“ slrp”实现的,它在 QEMU 中提供一个完整的 TCP/IP 协议栈,并使用该协议栈实现一个虚拟 NAT 网络。由于这种支持构建在 QEMU 中,因此可以与任何模型一起使用,并且不需要主机上的管理员特权,这与 TAP 不同。然而,它有一些局限性,包括性能,这使得它在实际应用中的价值较低。有关详细信息,请参阅与 QEMU 用户联网。

  • native_posix board.

    • The Zephyr instance can be executed as a user space process in the host system. This is the most convenient way to debug the Zephyr system as one can attach host debugger directly to the running Zephyr instance. This requires that there is an adaptation driver in Zephyr for interfacing with the host system. An Ethernet driver exists in Zephyr for this purpose. See Networking with native_posix board for details.

      Zephyr 实例可以作为主机系统中的用户空间进程执行。这是调试 Zephyr 系统的最方便的方法,因为可以将主机调试器直接附加到正在运行的 Zephyr 实例。这就要求Zephyr有一个与主机系统接口的适应驱动因素。Zephyr有一个以太网驱动程序用于此目的。有关详细信息,请参阅 Networking with national _ posx board。

  • USB device networking. USB 设备网络

    • Here, the Zephyr instance is run on a real board and the connectivity to the host system is done via USB. See USB Device Networking for details.

      在这里,Zephyr 实例在一个真实的板上运行,与主机系统的连接是通过 USB 完成的。有关详细信息,请参阅 USB 设备网络。

  • Connecting multiple Zephyr instances together. 将多个 Zephyr 实例连接在一起

    • If you have multiple Zephyr instances, either QEMU or native_posix ones, and want to create a connection between them, see Networking with multiple Zephyr instances for details.

      如果您有多个 Zephyr 实例(QEMU 实例或native_posix 实例) ,并且希望在它们之间创建连接,请参阅具有多个 Zephyr 实例的网络以了解详细信息。

  • Simulating IEEE 802.15.4 network between two QEMUs.

    两 QEMU 间 IEEE 802.15.4网络仿真。

    • Here, two Zephyr instances are running and there is IEEE 802.15.4 link layer run over an UART between them. See Networking with QEMU and IEEE 802.15.4 for details.

      这里,两个 Zephyr 实例正在运行,它们之间有一个通过 UART 运行的 IEEE 802.15.4链接层。有关详细信息,请参阅与 QEMU 和 IEEE 802.15.4联网。

Monitor Network Traffic 监察网络流量

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.

能够监控网络流量是很有用的,尤其是在调试连接问题或在Zephyr开发新的协议支持时。本页介绍如何设置捕获网络流量的方法,以便用户能够在远程主机中使用 Wireshark 或类似的工具来查看由 Zephyr 设备发送或接收的网络数据包。

See also the Network Packet Capture sample application from the Zephyr source distribution for configuration options that need to be enabled.

另请参阅来自 Zephyr 源发行版的 NetworkPacketCapture 示例应用程序,了解需要启用的配置选项。

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:

这里的说明描述了如何设置一个 Linux 主机来捕获Zephyr网络 RX 和 TX 流量。类似的指令应该也可以在其他操作系统中使用。在 Linux Host 上,获取 Zephyr net-tools 项目,该项目位于一个单独的 Git 存储库中:

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.

Net-tools 项目提供了一个配置文件来设置 IP-to-IP 隧道接口,以便我们可以将监视数据从 Zephyr 传输到主机。

In terminal #1, type: 在终端 # 1中,键入:

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

This script will create following IPIP tunnel interfaces:

该脚本将创建以下 IPIP 隧道接口:

Interface name 接口名称 Description 描述
zeth-ip6ip IPv6-over-IPv4 tunnel IPv6-over-IPv4隧道
zeth-ipip IPv4-over-IPv4 tunnel IPv4-over-IPv4隧道
zeth-ipip6 IPv4-over-IPv6 tunnel IPv4-over-IPv6隧道
zeth-ip6ip6 IPv6-over-IPv6 tunnel IPv6-over-IPv6隧道

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.

Zephyr 将把捕获的网络数据包发送到这些接口之一。实际的接口将取决于如何配置捕获。然后,您可以使用 Wireshark 来监视正确的网络接口。

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.

在创建了隧道接口之后,您可以使用 net-tools 项目中的 net-capture.py 脚本来打印或保存捕获的网络数据包。Py 提供了一个 UDP 监听器,它可以将捕获的数据打印到屏幕上,还可以选择将数据保存到 pcap 文件中。

$ ./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:

Py 有以下命令行选项:

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:

例如,您可以使用 netcat 代替 net-capture.py 脚本提供一个 UDP 侦听器,这样主机就不会向 Zephyr 发送端口无法到达的消息:

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.

上面的 IP 地址是内部隧道端点,可以进行更改,这取决于如何配置 Zephyr。Zephyr 将发送包含捕获的网络数据包的 UDP 数据包到配置的 IP 隧道,因此我们需要像这样终止网络连接。

Zephyr Configuration Zephyr配置

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

在这个例子中,我们使用native_posix 板。您也可以使用任何其他支持网络的板。

In terminal #3, type: 在终端 # 3中,键入:

west build -b native_posix samples/net/capture -- -DCONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD=\""gnome-terminal -- screen %s"\"

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

要查看 Zephyr 控制台和 shell,请像下面这样启动 Zephyr 实例:

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).

任何其他应用程序也可以使用,只需确保启用了合适的配置选项(参见示例/net/catch/prj.conf 文件)。

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.

如果需要,可以自动配置网络捕获,但是目前捕获示例应用程序不这样做。用户必须使用 net-shell 来设置和启用监视。

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

首先需要设置网络数据包监控。Net-shell 具有用于执行此操作的 net 捕获设置命令。命令语法是

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: 在 Zephyr 控制台中,键入:

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

这个命令将创建隧道接口。192.0.2.2是隧道终止的远程主机。地址用于选择隧道接口所连接的本地网络接口。2001: db8:200: : 1表示隧道的本地 IP 地址,2001: db8:200: : 2表示发送捕获的网络数据包的对等 IP 地址。对于 IPv6-over-IPv4隧道,可以在设置命令中给出 UDP 数据包的端口号

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

像这样的 IPv4-over-IPv4隧道

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.

如果省略端口号,则使用4242 UDP 端口作为默认端口。

The current monitoring configuration can be checked like this:

可以这样检查当前的监视配置:

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.

它将打印当前配置。由于我们还没有启用监视,因此没有设置 Capture iface。

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

然后我们需要像这样启用网络数据包监控:

net capture enable 2

The 2 tells the network interface which traffic we want to capture. In this example, the 2 is the native_posix 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:

2告诉网络接口我们想要捕获的流量。在这个示例中,2是本机 _ posx 板载以太网接口。请注意,我们将网络流量发送到本例中监视的相同接口。监视系统避免捕获已经捕获的网络流量,因为这会导致递归。您可以使用 netiface 命令查看可用的网络接口。注意,不能从隧道接口捕获流量,因为这会导致递归循环。如果这样配置,捕获的网络流量可以发送到其他网络接口。只需在网络捕获设置中正确设置 < remote-IP-addr > 选项,以便将 IP 隧道连接到所需的网络接口。可以像下面这样再次检查捕获状态:

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:

监控可以像下面这样禁用:

net capture disable

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

这将关闭当前运行的监视。监视设置可以像这样清除:

net capture cleanup

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

不需要使用 net-shell 来配置监视。如果需要,应用程序可以调用网络捕获 API 函数。

Wireshark Configuration

The Wireshark tool can be used to monitor the captured network traffic in a useful way.

WireSharktool 可以用来以一种有用的方式监视捕获的网络流量。

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.

您可以监视隧道接口或 zeth 接口。为了查看 UDP 数据包中实际捕获的数据,请参阅 WiresharkdecapulatesUDP 文档中的说明。

Networking with native_posix board

使用native_posix 板进行网络连接

This page describes how to set up a virtual network between a (Linux) host and a Zephyr application running in a native_posix board.

本页描述如何在(Linux)主机和运行在 native_posix 板上的 Zephyr 应用程序之间建立虚拟网络。

In this example, the Socket Echo Server sample application from the Zephyr source distribution is run in native_posix board. The Zephyr native_posix board instance is connected to a Linux host using a tuntap device which is modeled in Linux as an Ethernet network interface.

在此示例中,来自 Zephyr 源发行版的 Socket Echo Server 示例应用程序运行在 native_posix 板中。Zephyr native_posix 主板实例使用 tuntap 设备连接到 Linux 主机,该设备在 Linux 中被建模为以太网网络接口。

Prerequisites 先决条件

On the Linux Host, fetch the Zephyr net-tools project, which is located in a separate Git repository:

在 Linux Host 上,获取 Zephyr net-tools 项目,该项目位于一个单独的 Git 存储库中:

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

Basic Setup 基本设定

For the steps below, you will need three terminal windows:

对于下面的步骤,您将需要三个终端窗口:

  • Terminal #1 is terminal window with net-tools being the current directory (cd net-tools)

    终端1号是终端窗口,工作目录是 net-tools (cd net-tools)

  • Terminal #2 is your usual Zephyr development terminal, with the Zephyr environment initialized.

    终端 # 2是您通常使用的 Zephyr 开发终端,已初始化了 Zephyr 环境。

  • Terminal #3 is the console to the running Zephyr native_posix instance (optional).

    终端 # 3是运行 Zephyr national _ posx 实例的控制台(可选)。

[Step 1 - Create Ethernet interface

步骤1-创建以太网接口

Before starting native_posix with network emulation, a network interface should be created.

在使用网络模拟启动 national _ posx 之前,应该创建一个网络接口。

In terminal #1, type: 在终端 # 1中,键入:

./net-setup.sh

You can tweak the behavior of the net-setup.sh script. See various options by running net-setup.sh like this:

您可以调整 net-setup.sh 脚本的行为:

./net-setup.sh --help

Step 2 - Start app in native_posix board

步骤2-在native_posx 板中启动应用程序

Build and start the echo_server sample application.

构建并启动 echo _ server 示例应用程序。

In terminal #2, type: 在终端 # 2中,键入:

west build -b native_posix samples/net/sockets/echo_server
west build -t run

Step 3 - Connect to console (optional)

步骤3-连接到控制台(可选)

The console window should be launched automatically when the Zephyr instance is started but if it does not show up, you can manually connect to the console. The native_posix board will print a string like this when it starts:

当 Zephyr 实例启动时,控制台窗口应该自动启动,但是如果没有显示,您可以手动连接到控制台。当它启动时,本机 _ posx 板将打印如下字符串:

UART connected to pseudotty: /dev/pts/5

You can manually connect to it like this:

你可以像这样手动连接:

screen /dev/pts/5

Socket Echo Client Socket

Echo 客户端

Overview 概述

The echo-client sample application for Zephyr implements a UDP/TCP client that will send IPv4 or IPv6 packets, wait for the data to be sent back, and then verify it matches the data that was sent.

Zephyr 的 echo-client 示例应用程序实现了一个 UDP/TCP 客户机,该客户机将发送 IPv4或 IPv6数据包,等待数据被发送回来,然后验证它与发送的数据是否匹配。

The source code for this sample application can be found at: samples/net/sockets/echo_client.

这个示例应用程序的源代码可以在 sample/net/sockets/echo_client 找到。

Requirements 规定

Building and Running 构建与运行

There are multiple ways to use this application. One of the most common usage scenario is to run echo-client application inside QEMU. This is described in Networking with QEMU.

使用此应用程序有多种方式。最常见的使用场景之一是在 QEMU 中运行 echo-client 应用程序。这在与 QEMU 联网中进行了描述。

There are configuration files for different boards and setups in the echo-client directory:

在 echo-client 目录中有不同主板和设置的配置文件:

  • prj.conf Generic config file, normally you should use this.

    prj.conf 通用配置文件,通常您应该使用这个。

  • overlay-ot.conf This overlay config enables support for OpenThread.

    overlay-ot.conf 此覆盖配置支持 OpenThread。

  • overlay-802154.conf This overlay config enables support for native IEEE 802.15.4 connectivity. Note, that by default IEEE 802.15.4 L2 uses unacknowledged communication. To improve connection reliability, acknowledgments can be enabled with shell command: ieee802154 ack set.

    overlay-802154.conf此覆盖配置支持本机 IEEE 802.15.4连接。注意,在默认情况下,IEEE 802.15.4 L2使用未确认通信。为了提高连接的可靠性,可以使用 shell 命令 ieee802154 ack set 启用确认。

  • overlay-bt.conf This overlay config enables support for Bluetooth IPSP connectivity.

    overlay-bt.conf此覆盖配置支持蓝牙 IPSP 连接。

  • overlay-qemu_802154.conf This overlay config enables support for two QEMU’s when simulating IEEE 802.15.4 network that are connected together.

    overlay-qemu_802154.conf这个覆盖配置在模拟连接在一起的 IEEE 802.15.4网络时支持两个 QEMU。

  • overlay-tls.conf This overlay config enables support for TLS.

    overlay-tls.conf 此覆盖配置支持 TLS。

Build echo-client sample application like this:

构建 echo-client 示例应用程序如下:

west build -b <board to use> samples/net/sockets/echo_client -- -DCONF_FILE=<config file to use>

Example building for the nrf52840dk_nrf52840 with OpenThread support:

具有 OpenThread 支持的 nrf52840dk _ nrf52840的示例构建:

west build -b nrf52840dk_nrf52840 samples/net/sockets/echo_client -- -DCONF_FILE="prj.conf overlay-ot.conf"
west build -t run

Example building for the IEEE 802.15.4 RF2XX transceiver:

IEEE 802.15.4 RF2XX 收发器的示例构建:

west build -b [atsamr21_xpro | sam4s_xplained | sam_v71_xult] samples/net/sockets/echo_client -- -DOVERLAY_CONFIG=overlay-802154.conf
west flash	

In a terminal window you can check if communication is happen:

在终端窗口中,你可以检查通讯是否发生:

$ minicom -D /dev/ttyACM1

Enabling TLS support 启用 TLS 支持

Enable TLS support in the sample by building the project with the overlay-tls.conf overlay file enabled, for example, using these commands:

通过使用叠加 TLS 构建项目,在示例中启用 TLS 支持。例如,使用以下命令启用了 conf 覆盖文件:

west build -b qemu_x86 samples/net/sockets/echo_client -- -DCONF_FILE="prj.conf overlay-tls.conf"

An alternative way is to specify -DOVERLAY_CONFIG=overlay-tls.conf when running west build or cmake.

另一种方法是在运行 west build 或 cmake 时指定 -DOVERLAY_CONFIG = overlay-tls.conf

The certificate and private key used by the sample can be found in the sample’s src directory. The default certificates used by Socket Echo Client and Socket Echo Server enable establishing a secure connection between the samples.

示例使用的证书和私钥可以在示例的 src 目录中找到。Socket Echo Client 和 Socket Echo Server 使用的默认证书支持在示例之间建立安全连接。

SOCKS5 proxy support SOCKS5代理支持

It is also possible to connect to the echo-server through a SOCKS5 proxy. To enable it, use -DOVERLAY_CONFIG=overlay-socks5.conf when running west build or cmake.

还可以通过 SOCKS5代理连接到回送服务器。要启用它,请使用-DOVERLAY _ CONFIG = overlay-socks5。在运行西部构建或 cmake 时使用 conf。

By default, to make the testing easier, the proxy is expected to run on the same host as the echo-server in Linux host.

默认情况下,为了使测试更容易,代理需要与 Linux 主机中的 echo-server 运行在同一主机上。

To start a proxy server, for example a builtin SOCKS server support in ssh can be used (-D option). Use the following command to run it on your host with the default port:

要启动代理服务器,例如可以使用 ssh 中的内置 SOCKS 服务器支持(- D 选项)。使用以下命令在主机上以默认端口运行它:

For IPv4 proxy server: 对于 IPv4代理伺服器:

$ ssh -N -D 0.0.0.0:1080 localhost

For IPv6 proxy server: 对于 IPv6代理伺服器:

$ ssh -N -D [::]:1080 localhost

Run both commands if you are testing IPv4 and IPv6.

如果您正在测试 IPv4和 IPv6,请运行这两个命令。

To connect to a proxy server that is not running under the same IP as the echo-server or uses a different port number, modify the following values in echo_client/src/tcp.c.

若要连接到不在与 echo-server 相同的 IP 下运行或使用不同端口号的代理服务器,请修改 echo _ client/src/tcp.c 中的以下值。

#define SOCKS5_PROXY_V4_ADDR IPV4_ADDR
#define SOCKS5_PROXY_V6_ADDR IPV6_ADDR
#define SOCKS5_PROXY_PORT    1080

Running echo-server in Linux Host

在 Linux 主机中运行 echo 服务器

There is one useful testing scenario that can be used with Linux host. Here echo-client is run in QEMU and echo-server is run in Linux host.

有一个可以与 Linux 主机一起使用的有用的测试场景。在这里,echo-client 在 QEMU 中运行,echo-server 在 Linux 主机中运行。

To use QEMU for testing, follow the Networking with QEMU guide.

若要使用 QEMU 进行测试,请遵循 QEMU 联网指南。

In a terminal window: 在终端窗口中:

$ sudo ./echo-server -i tap0

Run echo-client application in QEMU:

在 QEMU 中运行 echo-client 应用程序:

west build -b qemu_x86 samples/net/sockets/echo_client -- -DCONF_FILE="prj.conf overlay-linux.conf"
west build -t run

Note that echo-server must be running in the Linux host terminal window before you start the echo-client application in QEMU. Exit QEMU by pressing CTRL+A x.

注意,在 QEMU 中启动 echo-client 应用程序之前,echo-server 必须在 Linux 主机终端窗口中运行。按 CTRL + Ax 退出 QEMU。

You can verify TLS communication with a Linux host as well. See https://github.com/zephyrproject-rtos/net-tools documentation for information on how to test TLS with Linux host samples.

您还可以验证与 Linux 主机的 TLS 通信。有关如何使用 Linux 主机示例测试 TLS 的信息,请参阅 https://github.com/zephyrproject-rtos/net-tools 文档。

See the Socket Echo Server documentation for an alternate way of running, with the echo-client on the Linux host and the echo-server in QEMU.

有关运行 Linux 主机上的 Echo-client 和 QEMU 中的 Echo-Server 的替代方法,请参阅 Socket Echo Server 文档。

Socket Echo Server Socket

Echo 服务器

Overview 概述

The echo-server sample application for Zephyr implements a UDP/TCP server that complements the echo-client sample application: the echo-server listens for incoming IPv4 or IPv6 packets (sent by the echo client) and simply sends them back.

Zephyr 的 echo-server 示例应用程序实现了一个 UDP/TCP 服务器,该服务器补充了 echo-client 示例应用程序: echo-server 侦听传入的 IPv4或 IPv6数据包(由 echo 客户机发送) ,并简单地将它们发送回去。

The source code for this sample application can be found at: samples/net/sockets/echo_server.

这个示例应用程序的源代码可以在 sample/net/sockets/echo_server 找到。

Requirements 规定

Building and Running 构建与运行

There are multiple ways to use this application. One of the most common usage scenario is to run echo-server application inside QEMU. This is described in Networking with QEMU.

使用此应用程序有多种方式。最常见的使用场景之一是在 QEMU 中运行 echo-server 应用程序。这在与 QEMU 联网中进行了描述。

There are configuration files for different boards and setups in the echo-server directory:

在 echo-server 目录中有不同主板和设置的配置文件:

  • prj.conf Generic config file, normally you should use this.

    prj.conf 通用配置文件,通常您应该使用这个。

  • overlay-ot.conf This overlay config enables support for OpenThread.

    overlay-ot.conf 此覆盖配置支持 OpenThread。

  • overlay-802154.conf This overlay config enables support for native IEEE 802.15.4 connectivity. Note, that by default IEEE 802.15.4 L2 uses unacknowledged communication. To improve connection reliability, acknowledgments can be enabled with shell command: ieee802154 ack set.

    overlay-802154.conf 此覆盖配置支持本机 IEEE 802.15.4连接。注意,在默认情况下,IEEE 802.15.4 L2使用未确认通信。为了提高连接的可靠性,可以使用 shell 命令 ieee802154 ack set 启用确认。

  • overlay-bt.conf This overlay config enables support for Bluetooth IPSP connectivity.

    overlay-bt.conf 此覆盖配置支持蓝牙 IPSP 连接。

  • overlay-qemu_802154.conf This overlay config enables support for two QEMU’s when simulating IEEE 802.15.4 network that are connected together.

    overlay-qemu_802154.conf这个覆盖配置在模拟连接在一起的 IEEE 802.15.4网络时支持两个 QEMU。

  • overlay-ppp.conf This overlay config enables support for PPP (Point-to-Point Protocol).

    overlay-ppp.conf 此覆盖配置支持 PPP (点对点协议)。

  • overlay-tls.conf This overlay config enables support for TLS.

    overlay-tls.conf 此覆盖配置支持 TLS。

  • overlay-tunnel.conf This overlay config enables support for IP tunneling.

    overlay-tunnel.conf这个覆盖配置支持 IP 隧道。

Build echo-server sample application like this:

像下面这样构建 echo-server 示例应用程序:

west build -b <board to use> samples/net/sockets/echo_server -- -DCONF_FILE=<config file to use>

Example building for the nrf52840dk_nrf52840 with OpenThread support:

具有 OpenThread 支持的 nrf52840dk _ nrf52840的示例构建:

west build -b nrf52840dk_nrf52840 samples/net/sockets/echo_server -- -DCONF_FILE="prj.conf overlay-ot.conf"
west build -t run

Example building for the atsamr21_xpro with RF2XX driver support:

支持 RF2XX 驱动程序的 atsamr21_xpro 示例构建:

west build -b [atsamr21_xpro | sam4e_xpro | sam_v71_xult] samples/net/sockets/echo_server -- -DOVERLAY_CONFIG=overlay-802154.conf
west flash

In a terminal window you can check if communication is happen:

在终端窗口中,你可以检查通讯是否发生:

$ minicom -D /dev/ttyACM0

Enabling TLS support 启用 TLS 支持

Enable TLS support in the sample by building the project with the overlay-tls.conf overlay file enabled, for example, using these commands:

通过使用overlay-tls.conf叠加文件 TLS 构建项目,在示例中启用 TLS 支持。使用以下命令:

west build -b qemu_x86 samples/net/sockets/echo_server -- -DCONF_FILE="prj.conf overlay-tls.conf"

An alternative way is to specify -DOVERLAY_CONFIG=overlay-tls.conf when running west build or cmake.

另一种方法是在运行 west build 或 cmake 时指定-DOVERLAY_CONFIG = overlay-tls. conf

The certificate used by the sample can be found in the sample’s src directory. The default certificates used by Socket Echo Server and Socket Echo Client enable establishing a secure connection between the samples.

示例使用的证书可以在示例的 src 目录中找到。Socket Echo Server 和 Socket Echo Client 使用的默认证书支持在示例之间建立安全连接。

Running echo-client in Linux Host

在 Linux 主机中运行 echo 客户端

There is one useful testing scenario that can be used with Linux host. Here echo-server is run in QEMU and echo-client is run in Linux host.

有一个可以与 Linux 主机一起使用的有用的测试场景。在这里,echo-server 在 QEMU 中运行,echo-client 在 Linux 主机中运行。

To use QEMU for testing, follow the Networking with QEMU guide.

若要使用 QEMU 进行测试,请遵循 QEMU 联网指南。

Run echo-server application in QEMU:

在 QEMU 中运行 echo-server 应用程序:

west build -b qemu_x86 samples/net/sockets/echo_server
west build -t run

In a terminal window: 在终端窗口中:

$ sudo ./echo-client -i tap0 2001:db8::1

Note that echo-server must be running in QEMU before you start the echo-client application in host terminal window.

请注意,在主机终端窗口中启动 echo-client 应用程序之前,echo-server 必须在 QEMU 中运行。

You can verify TLS communication with a Linux host as well. See https://github.com/zephyrproject-rtos/net-tools documentation for information on how to test TLS with Linux host samples.

您还可以验证与 Linux 主机的 TLS 通信。有关如何使用 Linux 主机示例测试 TLS 的信息,请参阅 https://github.com/zephyrproject-rtos/net-tools 文档。

See the Socket Echo Client documentation for an alternate way of running, with the echo-server on the Linux host and the echo-client in QEMU.

有关运行 Linux 主机上的 Echo-server 和 QEMU 中的 Echo-Client 的替代方法,请参阅 Socket Echo Client 文档。