netns可以让一台机器上模拟多个网络设备,是网络虚拟化的重要组成,将不同类型的网络应用隔离。一个net namespace有自己独立的路由表,iptables策略,设备管理。
本文利用netns模拟网络来实现单机上网卡自环上的iperf测试或其他网络测试。
用法
1. 先打开内核的网络转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward //临时方法
sysctl -w net.ipv4.ip_forward=1 //临时方法2
永久方法:修改/etc/sysctl.conf文件
将 net.ipv4.ip_forward=0改为net.ipv4.ip_forward=1
2. 添加2个namespace
[root@localhost ~]# ip netns add ns1
[root@localhost ~]# ip netns add ns2
[root@localhost ~]# ip netns show
ns2
ns1
3. 将enp5s0添加到ns1并查看
[root@localhost ~]# ip link set enp5s0 netns ns1
[root@localhost ~]# ip netns exec ns1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
8: enp5s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 8c:1c:da:45:4c:d9 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]#
4. 将enp6s0添加到ns2并查看
[root@localhost ~]# ip link set enp6s0 netns ns2
[root@localhost ~]# ip netns exec ns2 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
9: enp6s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 8c:1c:da:45:4c:da brd ff:ff:ff:ff:ff:ff
[root@localhost ~]#
5. 给ns1,ns2的2个接口分别配置IP
[root@localhost ~]# ip netns exec ns1 ifconfig enp5s0 7.7.7.7/24
[root@localhost ~]# ip netns exec ns2 ifconfig enp6s0 7.7.7.8/24
6. ping测试
[root@localhost ~]# ip netns exec ns2 ping 7.7.7.7
PING 7.7.7.7 (7.7.7.7) 56(84) bytes of data.
64 bytes from 7.7.7.7: icmp_seq=1 ttl=64 time=0.419 ms
64 bytes from 7.7.7.7: icmp_seq=2 ttl=64 time=0.328 ms
64 bytes from 7.7.7.7: icmp_seq=3 ttl=64 time=0.308 ms
^C
--- 7.7.7.7 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.308/0.351/0.419/0.052 ms
7. 在ns1上运行iperf服务端
[root@localhost ~]# ip netns exec ns1 iperf -s -D -p 1111
Running Iperf Server as a daemon
The Iperf daemon process ID : 2174
8. 在ns2上运行iperf客户端
[root@localhost ~]# ip netns exec ns2 iperf -c 7.7.7.7 -p 1111 -i 1 -R
------------------------------------------------------------
Client connecting to 7.7.7.7, TCP port 1111
TCP window size: 85.0 KByte (default)
------------------------------------------------------------
[ 3] local 7.7.7.8 port 41814 connected with 7.7.7.7 port 1111
[ ID] Interval Transfer Bandwidth
[ 3] 0.0- 1.0 sec 280 MBytes 2.35 Gbits/sec
[ 3] 1.0- 2.0 sec 280 MBytes 2.35 Gbits/sec
[ 3] 2.0- 3.0 sec 281 MBytes 2.36 Gbits/sec
[ 3] 3.0- 4.0 sec 280 MBytes 2.35 Gbits/sec
[ 3] 4.0- 5.0 sec 281 MBytes 2.36 Gbits/sec
[ 3] 5.0- 6.0 sec 281 MBytes 2.36 Gbits/sec
[ 3] 6.0- 7.0 sec 281 MBytes 2.35 Gbits/sec
[ 3] 7.0- 8.0 sec 280 MBytes 2.35 Gbits/sec
[ 3] 8.0- 9.0 sec 280 MBytes 2.35 Gbits/sec
[ 3] 9.0-10.0 sec 281 MBytes 2.36 Gbits/sec
[ 3] 0.0-10.0 sec 2.74 GBytes 2.35 Gbits/sec
[root@localhost ~]#
9. 在ns2上ping大包测试
[root@localhost ~]# ip netns exec ns2 ping 7.7.7.7 -c 100000 -s 65500 -f
PING 7.7.7.7 (7.7.7.7) 65500(65528) bytes of data.
--- 7.7.7.7 ping statistics ---
100000 packets transmitted, 100000 received, 0% packet loss, time 125759ms
rtt min/avg/max/mdev = 0.718/0.963/2.789/0.088 ms, ipg/ewma 1.257/0.980 ms