如何通过 iptables 在两个接口之间路由流量?

如何通过 iptables 在两个接口之间路由流量?

介绍

这个问题的主要话题是喂养被测设备被测设备) 系统通过流量生成器 (霸王龙)。

霸王龙可以从 docker 镜像中使用 - 这里是文档.docker 镜像已经下载霸王龙以及 2 个相互绑定的虚拟接口。

为了更好地理解,这里有一个建筑环境的方案。

在此处输入图片描述

我需要转发所有来自veth0/veth1eth0。我配置了 iptables 规则,但没有成功。我可以跟踪所有生成的数据包的最后一个链是FORWARD

环境配置

创建docker网络

docker network create --driver=bridge --internal --subnet=172.28.0.0/16 docker-local-net
docker network ls # to check network has been created

部署 t-rex 容器 [t-rex 容器;terminal-0;172.28.0.2]

docker pull trexcisco/trex
docker run -d --privileged --memory="4g" --memory-swap="4g" --cpus="2.0" --network=docker-local-net --name=trex-generator -it bash
docker exec -it trex-generator bash

部署 DUT 容器 [dut 集装箱;terminal-1;172.28.0.3]

docker run -d --privileged --memory="4g" --memory-swap="4g" --cpus="2.0" --network=docker-local-net --name=network-log-server -it bash
docker exec -it network-log-server bash

nload eth0 # to monitor network interface activity

注.0被测设备容器已预装工具和软件。

注1t-rex通过以下配置在两个虚拟接口之间路由流量(/etc/trex_cfg.yaml):

 - port_limit    : 2
   version       : 2
   low_end       : true                 #1
   interfaces    : ["veth0", "veth1"]   #2
   port_info     :  # set eh mac addr
                 - ip         : 1.1.1.1
                   default_gw : 2.2.2.2
                 - ip         : 2.2.2.2
                   default_gw : 1.1.1.1

数据包路由配置 [t-rex 容器;terminal-0;172.28.0.2]

# add default route
ip route add default via 172.28.0.3 dev eth0

# iptables table resetting
iptables -F && iptables -X
iptables -Z

# iptables nat table resetting
iptables -t nat -F && iptables -t nat -X
iptables -t nat -Z


# forward all input packets from veth0/veth1 to eth0
iptables -A FORWARD -i veth0 -o eth0 -j ACCEPT
iptables -A FORWARD -i veth1 -o eth0 -j ACCEPT

# NAT usage to route packet to DUT
iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-destination 172.28.0.3
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

测试

tcpdump 在 t-rex 容器内启动以检查 eth0 活动 [t-rex 集装箱;terminal-2;172.28.0.2]

docker exec -it trex-generator bash
tcpdump -i eth0

霸王龙发射 [t-rex 容器;terminal-0;172.28.0.2]

./t-rex-64 -f avl/sfr_delay_10.yaml -c 1 -d 120 -p
# wait few seconds and interrupt the process via ctrl + C

tcpdump 停止 [t-rex 集装箱;terminal-2;172.28.0.2]

...
# ctrl + C
11872 packets captured
11872 packets received by filter
0 packets dropped by kernel

检查 DUT 容器 [dut 集装箱;terminal-1;172.28.0.3]

# nload has not detected any packets

检查 iptables 规则 [t-rex 容器;terminal-0;172.28.0.2]

#iptables -L -nv

Chain INPUT (policy ACCEPT 36332 packets, 3012K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
18419 9208K ACCEPT     all  --  veth0  eth0    0.0.0.0/0            0.0.0.0/0           
18704 9456K ACCEPT     all  --  veth1  eth0    0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT 36332 packets, 3012K bytes)
 pkts bytes target     prot opt in     out     source               destination         

# iptables -t nat -L -nv

Chain PREROUTING (policy ACCEPT 4476 packets, 328K bytes)
 pkts bytes target     prot opt in     out     source               destination         
    2   400 DNAT       all  --  eth0   *       0.0.0.0/0            0.0.0.0/0            to:172.28.0.3

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 13392 packets, 922K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 13392 packets, 922K bytes)
 pkts bytes target     prot opt in     out     source               destination         
 4476  328K MASQUERADE  all  --  *      eth0    0.0.0.0/0            0.0.0.0/0 

问题

  • FORWARD为什么我在链和 tcpdump 报告中看到不同的数据包数量?( FORWARD:18704 + 18419 =37123tcpdump11872

  • 如何正确转发全部包裹来自veth0/veth1eth0

  • 如何正确转发全部数据包通过 NATeth0:172.28.0.2传输吗?172.28.0.3

  • 如果存在其他配置不准确之处(不仅与路由相关),请分享您的想法。

答案1

如何通过 iptables 在两个接口之间路由流量?

你不需要。虽然从技术上来说您可以这样做,但只需使用适当的路由机制,而不要滥用 iptables。

docker 镜像

对于 Docker 容器,您可以使用桥接网络容器之间(在您的示例中,dutt-rex。您可以使用docker工具进行设置。

如果您尝试在使用 docker 时做某事iptables,那么您会得到很多“乐趣”,因为 docker 广泛使用iptables规则,并且您所做的任何事情都可能会破坏 docker 现在或将来版本所做的事情。

我对 t-rex 不太熟悉,也不知道它的 Docker 化应该如何工作,也无法快速地在 Google 上找到相关信息,但我的猜测是它配置为自动选择最多两个桥接网络,然后您可以将两个桥接网络连接到最多两个其他容器。如果不是这样,我宁愿修改 t-rex 配置文件,也不愿尝试摆弄iptables

相关内容