# 创建macvlan 网卡 ip link add link eth0 name mac1@eth0 type macvlan mode bridge ip link add link eth0 name mac2@eth0 type macvlan mode bridge
# 创建命名空间 ip netns add ns1 ip netns add ns2
# 把虚拟网卡移入到命名空间 ip link set mac1@eth0 netns ns1 ip link set mac2@eth0 netns ns2
# 进入netns为虚拟网卡分配ip地址 ip netns exec ns1 ip addr add 192.168.0.100/24 dev mac1@eth0 ip netns exec ns2 ip addr add 192.168.0.200/24 dev mac2@eth0
# 设置虚拟网卡状态为up ip netns exec ns1 ip link set mac1@eth0 up ip netns exec ns2 ip link set mac2@eth0 up
# 创建macvlan网卡,不移入到命名空间,并且添加路由 ip link add link eth0 name mac3@eth0 type macvlan mode bridge ip addr add 192.168.0.150/24 dev mac3@eth0 ip link set mac3@eth0 up
ip route add 192.168.0.100/32 dev mac3@eth0 ip route add 192.168.0.200/32 dev mac3@eth0
# ping ns1中的网卡 [root@master ~]# ip netns exec ns2 ping -c 5 192.168.0.100 PING 192.168.0.100 (192.168.0.100) 56(84) bytes of data. 64 bytes from 192.168.0.100: icmp_seq=1 ttl=64 time=0.090 ms 64 bytes from 192.168.0.100: icmp_seq=2 ttl=64 time=0.044 ms 64 bytes from 192.168.0.100: icmp_seq=3 ttl=64 time=0.052 ms 64 bytes from 192.168.0.100: icmp_seq=4 ttl=64 time=0.080 ms 64 bytes from 192.168.0.100: icmp_seq=5 ttl=64 time=0.053 ms
--- 192.168.0.100 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4088ms rtt min/avg/max/mdev = 0.044/0.063/0.090/0.020 ms
# 能够ping通宿主机上的mac3@eth0虚拟网卡 [root@master ~]# ip netns exec ns2 ping -c 5 192.168.0.150 PING 192.168.0.150 (192.168.0.150) 56(84) bytes of data. 64 bytes from 192.168.0.150: icmp_seq=1 ttl=64 time=0.063 ms 64 bytes from 192.168.0.150: icmp_seq=2 ttl=64 time=0.071 ms 64 bytes from 192.168.0.150: icmp_seq=3 ttl=64 time=0.062 ms 64 bytes from 192.168.0.150: icmp_seq=4 ttl=64 time=0.079 ms 64 bytes from 192.168.0.150: icmp_seq=5 ttl=64 time=0.090 ms
--- 192.168.0.150 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4104ms rtt min/avg/max/mdev = 0.062/0.073/0.090/0.010 ms
macvlan 环境清理
1 2 3
ip netns del ns1 ip netns del ns2 ip link del mac3@eth0
ipvlan
IPVlan 和 macvlan 类似,都是从一个主机接口虚拟出多个虚拟网络接口。一个重要的区别就是所有的虚拟接口都有相同的 macv 地址,而拥有不同的 ip 地址。因为所有的虚拟接口要共享 mac 地址,所以有些需要注意的地方:
DHCP 协议分配 ip 的时候一般会用 mac 地址作为机器的标识。这个情况下,客户端动态获取 ip 的时候需要配置唯一的 ClientID 字段,并且 DHCP server 也要正确配置使用该字段作为机器标识,而不是使用 mac 地址
Ipvlan 是 linux kernel 比较新的特性,linux kernel 3.19 开始支持 ipvlan,但是比较稳定推荐的版本是 >=4.2
# 能ping通 192.168.0.200 [root@master ~]# ip netns exec ns1 ping -c 5 192.168.0.200 PING 192.168.0.200 (192.168.0.200) 56(84) bytes of data. 64 bytes from 192.168.0.200: icmp_seq=1 ttl=64 time=0.056 ms 64 bytes from 192.168.0.200: icmp_seq=2 ttl=64 time=0.055 ms 64 bytes from 192.168.0.200: icmp_seq=3 ttl=64 time=0.045 ms 64 bytes from 192.168.0.200: icmp_seq=4 ttl=64 time=0.046 ms 64 bytes from 192.168.0.200: icmp_seq=5 ttl=64 time=0.055 ms
--- 192.168.0.200 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4126ms rtt min/avg/max/mdev = 0.045/0.051/0.056/0.008 ms
# 无法ping通宿主机 [root@master ~]# ip netns exec ns1 ping -c 5 192.168.0.171 PING 192.168.0.171 (192.168.0.171) 56(84) bytes of data.
# 能ping通宿主机上的ipvlan3@eth0 [root@master ~]# ip netns exec ns1 ping -c 5 192.168.0.150 PING 192.168.0.150 (192.168.0.150) 56(84) bytes of data. 64 bytes from 192.168.0.150: icmp_seq=1 ttl=64 time=0.100 ms 64 bytes from 192.168.0.150: icmp_seq=2 ttl=64 time=0.088 ms 64 bytes from 192.168.0.150: icmp_seq=3 ttl=64 time=0.067 ms 64 bytes from 192.168.0.150: icmp_seq=4 ttl=64 time=0.091 ms 64 bytes from 192.168.0.150: icmp_seq=5 ttl=64 time=0.088 ms
--- 192.168.0.150 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4122ms rtt min/avg/max/mdev = 0.067/0.086/0.100/0.016 ms
ipvlan 环境清理
1 2 3
ip netns del ns1 ip netns del ns2 ip link del ipvlan3@eth0
macvtap
MACVTAP 是对 MACVLAN的改进,把 MACVLAN 与 TAP 设备的特点综合一下,使用 MACVLAN 的方式收发数据包,但是收到的包不交给 network stack 处理,而是生成一个 /dev/tapX 文件,交给这个文件。由于 MACVLAN 是工作在 MAC 层的,所以 MACVTAP 也只能工作在 MAC 层。