• TRex学习之旅十一


    本节中的教程演示了基本的 TRex无状态用例。示例包括常见和中等高级的 TRex 概念。

    port service模式

    正常操作模式下,为了保持数据包的高速处理,TRex 忽略大部分 rx 流量,但计数/统计和处理延迟流除外。

    模式:_

    1. On:所有数据包都被转发到 rx 以由 Client 或 Capture 处理。

    2. Off - 仅转发延迟数据包。在 v2.66 之前,非 TCP UDP 以软件模式转发到 rx,在 v2.66(包括)之后,仅在过滤模式下转发这些数据包以获得更大的灵活性。

    3. Filter [bgp, no_tcp_udp, emu, transport, mdns, dhcp, all] - 在这种情况下,特定的数据包被转发到 rx。一个例子可以是 BIRD 的 BGP 数据包。它仅与软件模式有关,--software 使用过滤器模式,您将能够以高速率运行 TCP/UDP 流量,同时保持路由协议的功能。性能将与“关闭”一样好,唯一的惩罚来自需要在 rx 端处理所有数据包的软件模式。

    过滤模式是v2.66版本新增的

    下面说明了如何处理 rx 数据包。只有一部分被转发到 rx 处理模块,没有一部分被转发回 Python 客户端。

                                                            图1. 正常模式下的端口

    在服务模式下,端口响应 ping 和 ARP 请求,并启用将数据包转发到 Python 控制平面以应用全双工协议(DCHP、IPv6 邻居等)。

    下面说明了如何将数据包转发回 Python 客户端。

                                                            图 2. 服务模式下的端口

    在编写用于仿真的 Python 插件(例如:IPV6 ND/DHCP)以准备设置时,服务模式可能很有用。然后您可以进入正常模式进行高速测试。

    在 Service 和 Normal 模式之间切换的示例
    1. trex>service --help
    2. usage: service [-h] [-p PORTS [PORTS ...] | -a] [--bgp] [--dhcp] [--mdns]
    3. [--emu] [--tran] [--no-tcp-udp] [--all] [--off]
    4. Configures port for service mode. In service mode ports will reply to ARP,
    5. PING and etc.
    6. optional arguments:
    7. -h, --help show this help message and exit
    8. -p PORTS [PORTS ...], --port PORTS [PORTS ...]
    9. A list of ports on which to apply the command
    10. -a Set this flag to apply the command on all available
    11. ports
    12. --bgp filter mode with bgp packets forward to rx
    13. --dhcp filter mode with dhcpv4/dhcpv6 packets forward to rx
    14. --mdns filter mode with mDNS packets forward to rx
    15. --emu filter mode for all emu services rx
    16. --tran filter mode with tcp/udp packets forward to rx
    17. (generated by emu)
    18. --no-tcp-udp filter mode with no_tcp_udp packets forward to rx
    19. --all Allow every filter possible
    20. --off Deactivates services on port(s)
    21. trex>service
    22. Enabling service mode on port(s) [0, 1]: [SUCCESS]
    23. trex(service)>service --off
    24. Disabling service mode on port(s) [0, 1]: [SUCCESS]

                                              服务模式和普通模式切换示例:API

    1. client.set_service_mode(ports = [0, 1], enabled = True)
    2. client.set_service_mode(ports = [0, 1], enabled = False)

    ARP/ICMP 响应

    只有在服务模式下,端口才会回复 ICMP 回显请求和 ARP 请求。

    抓包

    本节仅与服务模式相关。

    在服务模式下,TRex 提供了几种方法来检查和操作 Rx 和 Tx 数据包。

    数据包捕获是通过在服务器端分配一个更快的内存队列来实现的,该队列复制并存储数据包缓冲区。

    每个队列都可以使用以下属性定义:

    • 贮存

    • 它应该捕获 Tx/Rx 上的哪些端口

    • 应该是循环的还是固定

    图3. 数据包捕获架构

    上述架构意味着我们可以在短时间内高速捕获。

    例如,可以将 100 万个数据包的队列分配为循环队列,并以几个 MPPS 的速率处于活动状态。这有效地提供了具有给定过滤器的服务器看到的最后 100 万个数据包的样本。

    BPF 过滤

    在演示如何使用数据包捕获之前,回顾一下过滤是如何完成的会很有帮助。

    每个数据包捕获都分配了一个过滤器(默认情况下,一个匹配任何数据包的过滤器)。可以分配任何遵循Berekely 包过滤器 (BPF)语法规则的过滤器。

    BPF 过滤器被 Linux 内核、TCP 转储等广泛使用。基本上任何tcpdump过滤教程都可以用来为 TRex 定义一个过滤器。

    一些使用BPF的简单示例:

    所有ARPICMP数据包:

    'arp or icmp'

    所有目标端口为 53 的 UDP数据包:

    'udp and dst 53'

    所有数据包VLAN标记为200TCP SYN

    'vlan 200 and tcp[tcpflags] == tcp-syn'

    有关更多示例,请参阅在线提供的 BPF 和 tcpdump 示例。

    BPFJIT

    TRex 服务器使用BPF JIT,一个 BPF 到本地代码的编译版本,以允许非常快速的过滤。所以在TRex中高速过滤是非常可能的。

    之前

    以下是 XL710 的快照,配备 Intel® Xeon® CPU E5-2667 v3 @ 3.20GHz ,在应用 BPF 过滤器之前处理 15.72 mpps。

    1. Global Statistics
    2. connection : localhost, Port 4501 total_tx_L2 : 8.18 Gb/sec
    3. version : v2.28 total_tx_L1 : 10.73 Gb/sec
    4. cpu_util. : 3.31% @ 14 cores (7 per port) total_rx : 8.18 Gb/sec
    5. rx_cpu_util. : 82.0% / 15.72 Mpkt/sec 1 total_pps : 15.97 Mpkt/sec
    6. async_util. : 0.19% / 1.76 KB/sec drop_rate : 0.00 b/sec
    7. queue_full : 0 pkts

    之后

    用一个非命中过滤器来衡量使用 BPF 过滤器的效果:

    1. Global Statistics
    2. connection : localhost, Port 4501 total_tx_L2 : 8.21 Gb/sec
    3. version : v2.28 total_tx_L1 : 10.77 Gb/sec
    4. cpu_util. : 3.37% @ 14 cores (7 per port) total_rx : 8.21 Gb/sec
    5. rx_cpu_util. : 86.4% / 15.63 Mpkt/sec 1 total_pps : 16.03 Mpkt/sec
    6. async_util. : 0.21% / 1.64 KB/sec drop_rate : 0.00 b/sec
    7. queue_full : 0 pkts

    负过滤对 CPU 利用率的影响几乎为零 (<5%)。

    当然,命中过滤器会产生影响,但通常只对一小部分流量产生影响。

    API 使用情况

    使用 Python API 相当简单:

    1. # move port 1 to service mode as we want to capture traffic on it
    2. client.set_service_mode(ports = 1)
    3. # start a capture on port 1 Rx side with a limit, a mode and a *BPF* filter for any UDP with dst port 53
    4. capture = client.start_capture(rx_ports = 1, limit = 100, mode = 'fixed', bpf_filter = 'udp and dst 53')
    5. # execute your code here
    6. # save the packets to a file or to a list (see the Python API docs)
    7. client.stop_capture(capture['id'], '/home/mydir/port_0_rx.pcap')
    8. # exit service mode on port 1
    9. client.set_service_mode(ports = 1, enabled = False)

    控制台使用

    控制台提供了几种灵活的方式来处理数据包捕获

    • 捕获监控

    • 捕获记录

    捕获监控

    捕获监控是一种非持久性方法,用于捕获和显示来自一个或多个端口的 Tx / Rx 的数据包。

    监控有 3 种模式:

    • 低详细 - 将显示每个数据包的短行

    • 高详细 - 每个数据包将显示完整的 Scapy 节目

    • Wireshark Pipe - 使用连接到正在捕获的流量的管道启动 Wireshark

    前两个选项在控制台上显示数据包信息。如果正在监控中等数量的流量,这是理想的选择。但是,如果正在监控大量流量,请考虑使用Wireshark Pipe或Capture Recording方法。

    使用打开详细信息的控制台捕获流量的示例
    1. trex>service 1
    2. Enabling service mode on port(s) [0, 1, 2, 3]: [SUCCESS]
    3. trex(service)>capture monitor start --rx 3 -v 2
    4. Starting stdout capture monitor - verbose: 'high' [SUCCESS]
    5. *** use 'capture monitor stop' to abort capturing... ***
    6. trex(service)>arp -p 3 3
    7. Resolving destination on port(s) [3]: [SUCCESS]
    8. Port 3 - Recieved ARP reply from: 1.1.1.1, hw: 90:e2:ba:ae:88:b8 4
    9. 38.14 [ms]
    10. trex(service)>
    11. #1 Port: 3 -- Rx
    12. Type: ARP, Size: 60 B, TS: 16.98 [sec]
    13. ###[ Ethernet ]###
    14. dst = 90:e2:ba:af:13:89
    15. src = 90:e2:ba:ae:88:b8
    16. type = 0x806
    17. ###[ ARP ]###
    18. hwtype = 0x1
    19. ptype = 0x800
    20. hwlen = 6
    21. plen = 4
    22. op = is-at 5
    23. hwsrc = 90:e2:ba:ae:88:b8
    24. psrc = 1.1.1.1
    25. hwdst = 90:e2:ba:af:13:89
    26. pdst = 4.4.4.4
    27. ###[ Padding ]###
    28. load = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    29. trex(service)>

    说明:

    1、移动到服务模式以允许捕获。

    2、在详细打开 的情况下激活端口3 Rx 侧的捕获监视器。

    3、在端口3 上发送 ARP 请求。

    4、控制台显示返回的数据包。

    5、is-at ARP 响应被捕获。

    使用 Wireshark 管道捕获流量的示例

    1. trex(service)>capture monitor start --rx 3 -f udp -p 1
    2. Starting pipe capture monitor [SUCCESS]
    3. Trying to locate Wireshark [SUCCESS]
    4. Checking permissions on '/usr/bin/dumpcap' [SUCCESS]
    5. Launching '/usr/bin/wireshark -k -i /tmp/tmputa4jf3c' [SUCCESS] 2
    6. Waiting for Wireshark pipe connection [SUCCESS] 3
    7. *** Capture monitoring started *** 4
    8. trex(service)>arp 5
    9. Resolving destination on port(s) [0, 1, 2, 3]: [SUCCESS]
    10. Port 0 - Recieved ARP reply from: 4.4.4.4, hw: 90:e2:ba:af:13:89
    11. Port 1 - Recieved ARP reply from: 3.3.3.3, hw: 90:e2:ba:af:13:88
    12. Port 2 - Recieved ARP reply from: 2.2.2.2, hw: 90:e2:ba:ae:88:b9
    13. Port 3 - Recieved ARP reply from: 1.1.1.1, hw: 90:e2:ba:ae:88:b8

    说明:

    1、使用 Wireshark 管道和 UDP 过滤器 (BPF) 激活监视器。

    2、尝试使用与管道的连接来启动 Wireshark。

    3、控制台被阻塞,直到建立连接。

    4、监视器处于活动状态。

    5、发送 ARP 请求。

                                                             图4. Wireshark 管道

    捕获记录

    除了监控之外,控制台还允许进行简单的录制。记录使您能够定义一个固定大小的队列,然后可以将其保存到 PCAP 文件中。

    将流量捕获到固定大小队列的示例
    1. trex(service)>capture record start --rx 3 --limit 200 1
    2. Starting packet capturing up to 200 packets [SUCCESS]
    3. *** Capturing ID is set to '4' *** 2
    4. *** Please call 'capture record stop --id 4 -o <out.pcap>' when done ***
    5. trex(service)>capture 3
    6. Active Recorders
    7. ID | Status | Packets | Bytes | TX Ports | RX Ports
    8. ------------------------------------------------------------------------------------------------------
    9. 4 | ACTIVE | [0/200] | 0 B | - | 3
    10. trex(service)>start -f stl/imix.py -m 1kpps -p 0 --force 4
    11. Removing all streams from port(s) [0]: [SUCCESS]
    12. Attaching 3 streams to port(s) [0]: [SUCCESS]
    13. Starting traffic on port(s) [0]: [SUCCESS]
    14. 20.42 [ms]
    15. trex(service)>capture 5
    16. Active Recorders
    17. ID | Status | Packets | Bytes | TX Ports | RX Ports
    18. ------------------------------------------------------------------------------------------------------
    19. 4 | ACTIVE | [200/200] | 74.62 KB | - | 3
    20. trex(service)>capture record stop --id 4 -o /tmp/rx_3.pcap 6
    21. Stopping packet capture 4 [SUCCESS]
    22. Writing 200 packets to '/tmp/rx_3.pcap' [SUCCESS]
    23. Removing PCAP capture 4 from server [SUCCESS]
    24. trex(service)>

    说明:

    1、在端口3 Rx 端 启动一个数据包记录,限制为200 个数据包。

    2、使用 ID 4 创建一个新的捕获。

    3、显示捕获状态 - 当前为空。

    4、在连接到端口3的端口0 上启动流量。

    5、显示捕获状态 - 已满。

    6、将 200 个数据包保存到输出文件:/tmp/rx_3.pcap

    使用捕获作为计数器

    数据包捕获的另一个用途是计数。您可以简单地计算命中 BPF 过滤器的数据包,而不是获取数据包。

    例如,要计算源端口为5000的任何UDP数据包,您只需使用正确的 BPF 过滤器附加一个捕获并检查匹配的字段:

    1. trex(service)>capture record start --rx 3 --limit 0 -f udp and src 5000
    2. Starting packet capturing up to 0 packets [SUCCESS]
    3. *** Capturing ID is set to '14' ***
    4. *** Please call 'capture record stop --id 14 -o <out.pcap>' when done ***
    5. trex(service)>capture
    6. Active Recorders
    7. ID | Status | Matched | Packets | Bytes | RX Ports | BPF Filter
    8. -------------------------------------------------------------------------------------------------------
    9. 14 | ACTIVE | 0 | [0/0] | 0 B | 3 | udp and src 5000
    10. trex(service)>

    Matched字段表示有多少数据包与过滤器匹配。

    使用捕获端口进行更快的数据包捕获/数据包注入

    由于轮询性质和 RPC 协议的开销,使用数据包捕获机制在 TRex 客户端 python 端检查数据包通常会产生大约 5000 个数据包/秒的传输速率。

    为了在 TRex 服务器和 TRex 客户端之间进行更快的数据包传输,以及改进数据包的注入,可以使用捕获端口功能。它的运行速度通常比 TRex 捕获快 4 倍。

    此功能使 oneTRex 服务器能够连接到已打开的 ZeroMQ 套接字,该套接字将仅用于发送/接收给定 TRex 端口的原始数据包。在此套接字上推送一些数据将转换为在 TRex 端口上发送的新数据包,而在该端口上接收到的数据包也将通过 ZeroMQ 套接字发送。

    可选地,也可以指定一个 BPF 过滤器来限制从 TRex 服务器发送到 TRex 客户端的数据包。

    这是一个使用示例,它将首先在 TRex 端口 0 上发送一个数据包,然后阻塞直到收到一个 IP 数据包:

    1. import zmq
    2. # Bind our ZeroMQ socket so that the TRex server can connect to it
    3. capture_port = "ipc:///tmp/trex_capture_port"
    4. zmq_context = zmq.Context()
    5. zmq_socket = zmq_context.socket(zmq.PAIR)
    6. zmq_socket.bind(capture_port)
    7. # move port 0 to service mode as we want to start capture port on it
    8. client.set_service_mode(ports = 0)
    9. # start a trex capture port on port 0 with *BPF* filter for any IP packets
    10. client.start_capture_port(port = 0, endpoint = capture_port, bpf_filter = 'ip')
    11. # Send one packet (using scapy here)
    12. zmq_socket.send(bytes(Ether()/IP()/IP()/UDP()))
    13. # Wait until we get an IP packet on TRex port 0 and display it parsed using Scapy
    14. received_packet = zmq_socket.recv()
    15. Ether(received_packet).show2()
    16. # Stop capture port
    17. client.stop_capture_port(port = 0)
    18. # exit service mode on port 0
    19. client.set_service_mode(ports = 0, enabled = False)

    相邻协议

    为了保持高速流量生成,TRex 在预测试阶段处理相邻协议。

    需要运行相邻协议的测试应首先进入服务模式,在 Python 中执行所需的步骤,切换回正常模式,然后开始实际测试。

    ARP

    作为 TRex 的一部分提供的基本相邻协议是 AR​​P。

    示例设置:

                                                               图 5. 路由器 ARP

    1. trex>service #1
    2. Enabling service mode on port(s) [0, 1]: [SUCCESS]
    3. trex(service)>portattr --port 0
    4. port | 0 |
    5. ------------------------------------------
    6. driver | rte_ixgbe_pmd |
    7. description | 82599EB 10-Gigabit |
    8. link status | UP |
    9. link speed | 10 Gb/s |
    10. port status | IDLE |
    11. promiscuous | off |
    12. flow ctrl | none |
    13. -- | |
    14. src IPv4 | - |
    15. src MAC | 00:00:00:01:00:00 |
    16. --- | |
    17. Destination | 00:00:00:01:00:00 |
    18. ARP Resolution | - |
    19. ---- | |
    20. PCI Address | 0000:03:00.0 |
    21. NUMA Node | 0 |
    22. ----- | |
    23. RX Filter Mode | hardware match |
    24. RX Queueing | off |
    25. RX sniffer | off |
    26. Grat ARP | off |
    27. trex(service)>l3 -p -s 1.1.1.1 -d 1.1.1.2 #2
    28. trex(service)>arp -p 0 1 #3
    29. Resolving destination on port(s) [0, 1]: [SUCCESS]
    30. Port 0 - Received ARP reply from: 1.1.1.1, hw: d0:d0:fd:a8:a1:01
    31. Port 1 - Received ARP reply from: 1.1.2.1, hw: d0:d0:fd:a8:a1:02
    32. trex(service)>service --off #4

    说明:

    1、启用服务模式。

    2、设置 IPv4/默认网关。它将解析ARP。

    3、重复 ARP 解析。

    4、退出服务模式。

    要恢复到 MAC 地址模式(无 ARP 解析):

    禁用 L3 模式
    1. trex>l2 -p 0 --dst 00:00:00:01:00:00 #1
    2. trex>portattr --port 0
    3. port | 0 |
    4. ------------------------------------------
    5. driver | rte_ixgbe_pmd |
    6. description | 82599EB 10-Gigabit |
    7. link status | UP |
    8. link speed | 10 Gb/s |
    9. port status | IDLE |
    10. promiscuous | off |
    11. flow ctrl | none |
    12. -- | |
    13. src IPv4 | - |
    14. src MAC | 00:00:00:01:00:00 |
    15. --- | |
    16. Destination | 00:00:00:01:00:00 |
    17. ARP Resolution | - |
    18. ---- | |
    19. PCI Address | 0000:03:00.0 |
    20. NUMA Node | 0 |
    21. ----- | |
    22. RX Filter Mode | hardware match |
    23. RX Queueing | off |
    24. RX sniffer | off |
    25. Grat ARP | off |

    说明:

    1、禁用服务模式。

    Python API:

    1. client.set_service_mode(ports = [0, 1], enabled = True) 1
    2. # configure port 0, 1 to Layer 3 mode
    3. client.set_l3_mode(port = 0, src_ipv4 = '1.1.1.2', dst_ipv4 = '1.1.1.2') 2
    4. client.set_l3_mode(port = 1, src_ipv4 = '1.1.2.2', dst_ipv4 = '1.1.2.1')
    5. # ARP resolve ports 0, 1
    6. c.resolve(ports = [0, 1])
    7. client.set_service_mode(ports = [0, 1], enabled = False) 3

    说明:

    1、启用服务模式。

    2、配置 IPv4 和默认网关。

    3、禁用服务模式。

    ICMP

    TRex 提供的另一个基本协议是 ICMP。在服务模式下,可以从控制台/API ping DUT 甚至是 TRex 端口。

    TRex 控制台
    1. trex(service)>ping --help
    2. usage: ping [-h] --port PORT -d PING_IPV4 [-s PKT_SIZE] [-n COUNT]
    3. pings the server / specific IP
    4. optional arguments:
    5. -h, --help show this help message and exit
    6. --port PORT, -p PORT source port for the action
    7. -d PING_IPV4 which IPv4 to ping
    8. -s PKT_SIZE packet size to use
    9. -n COUNT, --count COUNT
    10. How many times to ping [default is 5]
    11. trex(service)>ping -p 0 -d 1.1.2.2
    12. Pinging 1.1.2.2 from port 0 with 64 bytes of data:
    13. Reply from 1.1.2.2: bytes=64, time=27.72ms, TTL=127
    14. Reply from 1.1.2.2: bytes=64, time=1.40ms, TTL=127
    15. Reply from 1.1.2.2: bytes=64, time=1.31ms, TTL=127
    16. Reply from 1.1.2.2: bytes=64, time=1.78ms, TTL=127
    17. Reply from 1.1.2.2: bytes=64, time=1.95ms, TTL=127

    Python API

    1. # move to service mode
    2. client.set_service_mode(ports = ports, enabled = True)
    3. # configure port 0, 1 to Layer 3 mode
    4. client.set_l3_mode(port = 0, src_ipv4 = '1.1.1.2', dst_ipv4 = '1.1.1.1')
    5. client.set_l3_mode(port = 1, src_ipv4 = '1.1.2.2', dst_ipv4 = '1.1.2.1')
    6. # ping port 1 from port 0 through the router
    7. client.ping_ip(src_port = 0, dst_ipv4 = '1.1.2.2', pkt_size = 64) 1
    8. # disable service mode
    9. client.set_service_mode(enabled = False)

    说明:

    1、检查连通性。

    IPv6 ND 客户端

    当前:TRex 支持扫描网络以查找支持 IPv6 的邻居,并从控制台 ping 附近的设备。
    未来阶段的计划:在 CPP 服务器上添加支持。
    这些方法的优点是它们可以很容易地扩展以在自动化中模拟大量客户端。

    扫描示例:

     ping示例:

    这些实用程序(也可从 API 获得)可以帮助用户配置下一跳。从控制台,可以设置从 scan6 结果中获取的“l2”目标 MAC:

    为了设置自己的 IPv6,我们使用RFC 3513中描述的本地地址。
    对于网络扫描,我们 ping 多播地址 ff02::1 并通过 NS/ND 会话建立连接。

    扫描网络上的附加链接:

    在自动化中使用 IPv6 方法的示例:

    Linux 网络命名空间

    从 v2.50 版本开始,可以将一些 Linux 网络命名空间附加到 TRex 物理接口。命名空间中的每个主机也就是模拟真实客户端的节点。使用这种方法,可以模拟具有许多 Linux 网络设备的网络,每个设备都有自己独立的网络堆栈(例如不同的 ipv4/ipv6/QinQ/Dot1Q/routing/arp 表等)

     

    上图显示了连接到虚拟交换机(在 TRex 内部实现)的两台主机 host4 和 host5。TRex 端口本身有自己的命名空间,有自己的 IPv4 和 IPv6 配置(使用旧 API)。创建时的每个主机都有一个不同的 MAC 地址作为密钥和网络配置(IPv4、IPv6、Dot1Q、QinQ 等)。这些协议由 Linux 内核实现(超时、arp 表、ipv6、每个命名空间的路由)。

    让我们以入口和出口数据包为例

    DUT ARP 请求数据包来自 DUT 到 host4(whois host4 -broadcast)。此数据包到达 TRex Rx 核心(由于 STL 中的服务模式配置以及默认情况下在 ASTF 中)。Rx 核心会将其广播到所有节点(host4、host5、trex-port1) host4 将使用单播 ARP 响应进行应答 tat 将到达 DUT(通过 TRex 交换机实现)

    节点生成的 IPv6 MLD/广播数据包(组播/广播数据包)将仅转发到DUT。DUT 广播/多播数据包将转发到所有节点。

    创建新节点时。它不会发送免费的 arp 多播/混杂模式应该在端口级别启用,否则单播数据包不会到达节点。

    API 功能是

    1. 创建一个新节点并将其关联到一个物理端口(例如端口 0)。

    2. 使用 IPv4 和/或 IPv6 配置节点。

    3. 从端口中删除节点。

    4. 获取统计信息/状态

    这种方法的一个缺点是添加/删除新节点有点慢。创建一个可能需要大约 100 毫秒(由于内核交互)。但是,一旦创建了节点,TRex 将能够毫无问题地处理其中的许多节点,因为流量率不高(仅限多播/广播数据包),TRex 可以通过将此操作拆分为来处理多播突发(DUT→许多节点)很多小操作。

    规模受内核内存和创建时间的限制。

    因为这是一个非常缓慢的操作,所以 API 与我们今天的 API 有点不同。

    差异

    1. reset API不会删除所有节点

    2. 一旦客户端断开/连接,节点信息就不会同步

    3. “服务模式”应该在 STL 中启用(对于 ASTF 不需要)

    4. 需要启用多播/混杂模式

    5. 添加 stack: linux_basedtrex_cfg.yaml 参见Linux Stack

    基于 linux_based 的堆栈配置
    1. - version: 2
    2. interfaces: ['82:00.0', '82:00.1']
    3. stack: linux_based
    4. ...

    完整的 API 可以在命名空间 API中找到

    节点类型

    所有节点 (node==veth) 都由唯一的 MAC 地址标识,但存在一些差异。有 3 种类型的节点:Normal、Shared Namespace和Bird 节点。

                                                                 图 7. 三种类型的节点

    普通节点

    这些是默认类型的节点,创建 1 个命名空间和 1 个 veth 对。添加另一个新节点将创建另一个具有 1 个 veth 对的命名空间。这些节点由 ipv4 和一个默认网关配置(所有流量都转发到默认的单个 veth)

    使用示例

    1. cmds = NSCmds()
    2. MAC = "00:01:02:03:04:05"
    3. cmds.add_node(MAC) 1
    4. c.set_namespace(0, method='set_ipv4', mac=MAC, ipv4="1.1.1.3", dg="1.1.1.2") 2
    5. ...

    说明:

    1、默认节点为普通节点,只指定mac地址

    2、使用 ip 地址和默认网关配置 ipv4

    更多示例可以在这里找到

    共享命名空间节点

    这些是附加到预先创建的命名空间的节点(veth)。通过这种方式,您可以创建多个附加到同一命名空间的 ve​​th。这些节点由 ipv4 和子网掩码配置,还可以为整个命名空间设置默认网关(设置为 veth 之一) Dot1Q/QinQ 的配置是 per veth 而不是 per namespace

    使用示例

    1. cmds = NSCmds()
    2. cmds.add_node(mac = "00:01:02:03:04:05", shared_ns = "my-ns") 1
    3. cmds.add_node(mac = "00:01:02:03:04:06", shared_ns = "my-ns") 2
    4. c.set_namespace(0, method='set_ipv4', mac="00:01:02:03:04:05",
    5. ipv4 = "1.1.1.3",
    6. subnet = 24,
    7. shared_ns = True) 3
    8. c.set_namespace(0, method='set_ipv4', mac="00:01:02:03:04:06",
    9. ipv4 = "1.1.2.3",
    10. subnet = 24,
    11. shared_ns = True)

    说明:

    1、“my-ns”是预先创建的命名空间的名称

    2、在同一命名空间中创建第二个 veth 对

    3、必须指定 shared_ns 参数

    鸟节点

    鸟节点是共享命名空间节点的一个特例。Bird 命名空间在运行带有“--bird-server”标志的 TRex 时创建,这样它们类似于共享命名空间。

    要了解更多关于在 TRex 中使用 bird 的信息,请参阅:

    节点类型差异总结

    表 6. 节点类型差异

    节点类型

    节点:veth 比率

    命名空间创建

    子网掩码

    默认网关

    普通的

    1:1

    在每个节点上创建

    未使用,使用默认网关

    1 代表整个节点

    共享命名空间

    1:很多

    由“add_shared_ns”命令创建,名称由 TRex 确定

    在“set_ipv4/6”处配置

    1 代表整个命名空间

    1:很多

    预先创建,名称由 TRex 确定

    在“set_ipv4/6”处配置

    未使用,使用子网掩码

    注意命名空间不能相互通信,因为它们都通过 TRex 端口将数据包传递到 DUT。

    使用批处理 API 添加一个节点

    在此示例中,将添加一个 MAC:“00:01:02:03:04:05”和 ipv4="1.1.1.3" default_gateway ="1.1.1.2" 并启用 IPv6 的节点。API 需要一批操作。这些操作可以与其他 API(例如流量)并行工作,以验证需要调用wait_for_async_resultsAPI的响应

    1. c = STLClient(verbose_level = 'error')
    2. c.connect()
    3. my_ports=[0,1]
    4. c.reset(ports = my_ports) 1
    5. # move to service mode
    6. c.set_service_mode (ports = my_ports, enabled = True) 2
    7. cmds=NSCmds() 3
    8. MAC="00:01:02:03:04:05"
    9. cmds.add_node(MAC) # add namespace 4
    10. cmds.set_vlan(MAC,[123,123]) # add vlan + QinQ tags
    11. cmds.set_ipv4(MAC,"1.1.1.3","1.1.1.2") # configure ipv4 and default gateway 5
    12. cmds.set_ipv6(MAC,True) # enable ipv6 (auto mode, get src address from the router) 6
    13. # start the batch
    14. c.set_namespace_start( 0, cmds) 7
    15. # wait for the results
    16. res = c.wait_for_async_results(0) 8
    17. # print the results
    18. print(res)

    说明:

    1、重置 API 不会删除旧节点

    2、进入服务模式

    3、定义一个 NSCmds 对象,它将被异步命令填充

    4、添加新节点

    5、配置ipv4

    6、启用 ipv6

    7、为对象提供 set_namespace_start 的所有命令

    8、等待操作完成

    阻塞 API

    1. c = STLClient(verbose_level = 'error')
    2. c.connect()
    3. my_ports=[0,1]
    4. c.reset(ports = my_ports)
    5. # move to service mode
    6. c.set_service_mode (ports = my_ports, enabled = True)
    7. # remove all old name spaces from all the ports
    8. c.namespace_remove_all()
    9. # utility function
    10. MAC="00:01:02:03:04:05"
    11. # each function will block
    12. c.set_namespace(0,method='add_node',mac=MAC) 1
    13. c.set_namespace(0,method='set_vlan',vlans=[123,123])
    14. c.set_namespace(0,method='set_ipv4', mac=MAC, ipv4="1.1.1.3", dg="1.1.1.2")
    15. c.set_namespace(0,method='set_ipv6', mac=MAC, enable= True)

    说明:

    1、无需填充对象 NSCmds。使用命名空间请求的 API 调用 set_namespace API

    统计查询

    1. c = STLClient(verbose_level = 'error')
    2. c.connect()
    3. my_ports=[0,1]
    4. c.reset(ports = my_ports)
    5. # get all active nodes on port 0
    6. r=c.set_namespace(0, method='get_nodes') 1
    7. c.set_namespace (0, method='get_nodes_info', macs_list=r) 2
    8. r=c.set_namespace(0, method='counters_get_meta') 3
    9. r=c.set_namespace(0, method='counters_get_values', zeros=True) 4

    说明:

    1、获取活动节点(每个节点的 mac)

    2、获取每个节点的完整信息

    3、获取统计计数器(名称/类型)

    4、获取计数器值

    Ping 到新节点

    在此设置中有两个端口 1.1.1.1 (port0) <→ 1.1.1.2 (port1)

    这是之前的配置:

    混杂已关闭

    1. trex>portattr -a
    2. Port Status
    3. port | 0 | 1
    4. ----------------+----------------------+---------------------
    5. driver | net_vmxnet3 | net_vmxnet3
    6. description | VMXNET3 Ethernet C | VMXNET3 Ethernet C
    7. link status | UP | UP
    8. link speed | 10 Gb/s | 10 Gb/s
    9. port status | IDLE | IDLE
    10. promiscuous | off | off
    11. multicast | on | on
    12. flow ctrl | N/A | N/A
    13. vxlan fs | N/A | N/A
    14. -- | |
    15. layer mode | IPv4 | IPv4
    16. src IPv4 | 1.1.1.1 | 1.1.1.2
    17. IPv6 | off | off
    18. src MAC | 00:0c:29:b4:e7:e9 | 00:0c:29:b4:e7:11
    19. --- | |
    20. Destination | 1.1.1.2 | 1.1.1.1
    21. ARP Resolution | 00:0c:29:b4:e7:11 | 00:0c:29:b4:e7:e9
    22. ---- | |
    23. VLAN | - | -
    24. ----- | |
    25. PCI Address | 0000:0b:00.0 | 0000:0c:00.0
    26. NUMA Node | 0 | 0
    27. RX Filter Mode | hardware match | hardware match
    28. RX Queueing | off | off
    29. Grat ARP | off | off
    30. ------ | |

    让我们确保我们处于混杂模式和服务模式

    1. trex>portattr --mul on
    2. trex>portattr --prom on
    3. trex>service
    1. trex>portattr -a
    2. Port Status
    3. port | 0 | 1
    4. ----------------+----------------------+---------------------
    5. driver | net_vmxnet3 | net_vmxnet3
    6. description | VMXNET3 Ethernet C | VMXNET3 Ethernet C
    7. link status | UP | UP
    8. link speed | 10 Gb/s | 10 Gb/s
    9. port status | IDLE | IDLE
    10. promiscuous | on | on
    11. multicast | on | on
    12. flow ctrl | N/A | N/A
    13. vxlan fs | N/A | N/A
    14. -- | |
    15. layer mode | IPv4 | IPv4
    16. src IPv4 | 1.1.1.1 | 1.1.1.2
    17. IPv6 | off | off
    18. src MAC | 00:0c:29:b4:e7:e9 | 00:0c:29:b4:e7:11
    19. --- | |
    20. Destination | 1.1.1.2 | 1.1.1.1
    21. ARP Resolution | 00:0c:29:b4:e7:11 | 00:0c:29:b4:e7:e9
    22. ---- | |
    23. VLAN | - | -
    24. ----- | |
    25. PCI Address | 0000:0b:00.0 | 0000:0c:00.0
    26. NUMA Node | 0 | 0
    27. RX Filter Mode | hardware match | hardware match
    28. RX Queueing | off | off
    29. Grat ARP | off | off
    30. ------

    让我们调用这个脚本在端口 0 上添加一个新节点

    1. cmds=NSCmds()
    2. MAC="00:01:02:03:04:05"
    3. cmds.add_node(MAC)
    4. cmds.set_ipv4(MAC,"1.1.1.3","1.1.1.2")
    5. cmds.set_ipv6(MAC,True)
    6. res = c.set_namespace_start( 0, cmds)
    7. res = c.wait_for_async_results(0)

    现在我们可以ping到新节点

    1. trex(service)>l3 -p 1 --src 1.1.1.2 --dst 1.1.1.3
    2. trex(service)>ping -p 1 -d 1.1.1.3
    3. Pinging 1.1.1.3 from port 1 with 64 bytes of data:
    4. Reply from 1.1.1.3: bytes=64, time=32.29ms, TTL=64
    5. Reply from 1.1.1.3: bytes=64, time=3.73ms, TTL=64
    6. Reply from 1.1.1.3: bytes=64, time=3.89ms, TTL=64
    7. Reply from 1.1.1.3: bytes=64, time=2.85ms, TTL=64
    8. Reply from 1.1.1.3: bytes=64, time=2.82ms, TTL=64

    要从 Linux shell 调试它,你可以这样做

    $sudo ip netns show

    这将显示所有网络命名空间

    对于端口 0,名称是trex-a-0-x,其中 X 是命名空间的编号

    查看信息

    1. $sudo ip netns exec trex-a-0-1 ifconfig
    2. trex-a-0-1-L: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 9280
    3. inet 1.1.1.3 netmask 255.255.255.255 broadcast 0.0.0.0
    4. inet6 fe80::201:2ff:fe03:405 prefixlen 64 scopeid 0x20<link>
    5. ether 00:01:02:03:04:05 txqueuelen 1000 (Ethernet)
    6. RX packets 1 bytes 60 (60.0 B)
    7. RX errors 0 dropped 1 overruns 0 frame 0
    8. TX packets 8 bytes 648 (648.0 B)
    9. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    可以使用通常的捕获功能监视和捕获来自/到命名空间的流量

    您将无法 ping 1.1.1.1(原始端口 ip),因为 TRex ping 有一个默认网关

    控制台命令示例

    控制台命令的目标是一次添加/删除一个节点的小规模操作以下示例显示

    1. trex(service)>ns add -p 0 --mac 00:01:02:03:04:05 --src 1.1.1.3 --dst 1.1.1.2 --ipv6
    2. trex(service)>ns show-node -p 0 --mac 00:01:02:03:04:05
    3. {'nodes': [{'ether': {'src': '00:01:02:03:04:05'},
    4. 'ipv4': {'dst': '1.1.1.2', 'src': '1.1.1.3'},
    5. 'ipv6': {'enabled': True, 'src': ''},
    6. 'linux-ns': 'trex-a-0-1',
    7. 'linux-veth-external': 'trex-a-0-1-T',
    8. 'linux-veth-internal': 'trex-a-0-1-L',
    9. 'vlan': {'tags': []}}]}
    10. trex(service)>ns show-nodes -p 0
    11. Setting port 0 in with namespace configuration [SUCCESS]
    12. wait_for_async_results [SUCCESS]
    13. ns nods
    14. node-id | mac
    15. -----------+------------------
    16. 0 | 00:01:02:03:04:05
    17. trex(service)>ns show-counters -p 0
    18. Setting port 0 in with namespace configuration [SUCCESS]
    19. wait_for_async_results [SUCCESS]
    20. ns stats
    21. name | value | help
    22. ---------------------+-------------------+---------------------
    23. rx_unicast_bytes | 60 | rx unicast bytes
    24. tx_multicast_bytes | 648 | tx multicast bytes
    25. rx_unicast_pkts | 1 | rx unicast pkts
    26. tx_multicast_pkts | 8 | tx multicast pkts
    27. rx_multicast_bytes | 480 | rx multicast bytes
    28. rx_multicast_pkts | 8 | rx multicast pkts
  • 相关阅读:
    HTML+CSS+JS:花瓣登录组件
    力扣:718. 最长重复子数组
    Echarts仪表盘3.0
    Hadoop完全分布式的搭建
    Elasticsearch-IndexTemplate和DynamicTemplate 有什么区别
    双线程架构:小程序相比于H5的架构优势
    zk系列三:zookeeper实战之分布式锁实现
    yolov5 优化方法(四)修改bbox损失函数(补充EIOU,SIOU)
    Scala生成ip和Mac
    【干货】ZBrush王者细节操作
  • 原文地址:https://blog.csdn.net/qq_35029061/article/details/125438264