网卡虚拟化 Macvlan

拓扑结构
image.png
交换机配置
# ip link add name br0 type bridge
# ip link add name veth11 type veth  peer veth12
# ip link set veth11 master br0
# ip addr add 192.168.0.1/24 dev br0
# ip link set veth11 up
# ip link set veth12 up
# ip link set br0 up
添加namespace ns1和ns2
# ip netns add ns1
# ip netns add ns2
# ip netns add ns3
# ip netns exec ns1  ip link set lo up
# ip netns exec ns2  ip link set lo up
# ip netns exec ns3  ip link set lo up
  • Bridge 所有的端点都直接连接在一起
  • VEPA 虚拟以太网端口聚合器模式。在同一物理接口上的不同macvlan接口之间的数据通过物理接口传输。macvlan接口之间通信需要交换机支持发夹( hairpin)模式,或者有一个路由器来转发。macvlan的缺省模式为VEPA
  • Private 不允许同一物理接口上的macvlan实例之间通信,即使外部交换机支持发夹( hairpin)模式。
  • passthru [nopromisc] -这种模式给单个端点更多的能力,通常在macvtap模式。不允许在同一物理接口上有一个以上的端点。所有流量将被转发到这个端点,从而允许virtio来宾更改MAC地址或设置混杂模式,以便在接口上架桥或在其上创建vlan接口。默认情况下,此模式强制底层接口进入混杂模式。传递nopromisc标志可以防止这种情况发生,因此可以使用标准工具控制promisc标志。
  • Source-允许一个允许的mac地址列表,这是用来匹配源mac地址从接收帧在底层‐ing接口。这允许创建基于mac的VLAN关联,而不是基于标准端口或标签。这个特性对于部署基于802.1xmac的行为很有用,因为底层接口的驱动程序不允许这样做。

1、bridge模式

  • Macvlan之间都通
  • 广播洪泛到所有接口
  • 与父接口不通
  • 父接口down,macvlan接口也会down
添加macvlan link veth12.1和veth12.2
# ip link add veth12.1 link veth12 type macvlan mode bridge
# ip link add veth12.2 link veth12 type macvlan mode bridge
将macvlan接口添加到ns1和ns2
# ip link set netns ns1 veth12.1
# ip link set netns ns2 veth12.2
# ip link set netns ns3 veth12

修改namespac中的网卡名字
# ip netns exec ns1 ip link set veth12.1 name eth0
# ip netns exec ns2 ip link set veth12.2 name eth0
# ip netns exec ns3 ip link set veth12 name eth0
添加macvlan接口的ip地址
# ip netns exec ns1 ip addr add  192.168.0.11/24 dev eth0
# ip netns exec ns2 ip addr add  192.168.0.12/24 dev eth0
# ip netns exec ns3 ip addr add  192.168.0.254/24 dev eth0
up接口
# ip netns exec ns1 ip link set eth0 up
# ip netns exec ns2 ip link set eth0 up
# ip netns exec ns3 ip link set eth0 up
ping测试
  • 对端交换机接口IP 可以通
  • 物理网卡 不通
  • 同物理网卡的 macvlan 可以通
# ip netns exec ns1 ping -c 1 192.168.0.1
通
# ip netns exec ns1 ping -c 1 192.168.0.254
不通
# ip netns exec ns1 ping -c 1 192.168.0.12
通
查看mac地址情况
# ip netns exec ns1 ip link | grep ether
    link/ether 62:92:4b:81:85:fe brd ff:ff:ff:ff:ff:ff link-netns ns3
# ip netns exec ns2 ip link | grep ether
    link/ether 76:ed:9e:2e:5f:a4 brd ff:ff:ff:ff:ff:ff link-netns ns3
# bridge fdb
...
62:92:4b:81:85:fe dev dev veth11 master br0
7e:fc:bf:1f:ce:58 dev dev veth11 master br0
...
关闭veth11,macvlan之间不通
# ip link set veth11 down
//down一个veth,peer也会down
# ip netns exec ns3 ip link show type veth
19: eth0@if20:  mtu 1500 qdisc noqueue state LOWERLAYERDOWN mode DEFAULT group default qlen 1000
    link/ether 7e:fc:bf:1f:ce:58 brd ff:ff:ff:ff:ff:ff link-netnsid 0
# ip netns exec ns1 ping -c 1 192.168.0.12
不通
恢复环境
# ip link set veth11 up 
image.png

2、VEPA模式

此模式下macvlan之间是隔离的,需要交换机支持hairpin功能才能通信

# ip netns exec ns1 ip link set eth0  type macvlan mode vepa
# ip netns exec ns1 ip link set eth0  type macvlan mode vepa
ping测试
  • 对端交换机接口IP 通
  • 同物理网卡的 macvlan 不通
  • 物理网卡 不通
# ip netns exec ns1 ping -c 1 192.168.0.1
通
# ip netns exec ns1 ping -c 1 192.168.0.12
不通
# ip netns exec ns1 ping -c 1 192.168.0.254
不通
抓包可以看到很多icmp报文
# tcpdump -i veth11 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on veth11, link-type EN10MB (Ethernet), capture size 262144 bytes
05:21:06.824252 IP 192.168.0.11 > 192.168.0.12: ICMP echo request, id 33858, seq 717, length 64
05:21:07.848077 IP 192.168.0.11 > 192.168.0.12: ICMP echo request, id 33858, seq 718, length 64
05:21:08.871927 IP 192.168.0.11 > 192.168.0.12: ICMP echo request, id 33858, seq 719, length 64
image.png

3、private模式

隔离功能比VPEA更强,阻断了广播和组播,即使交换机开启hairpin也无法通信

# ip netns exec ns1 ip link set eth0  type macvlan mode private
# ip netns exec ns1 ip link set eth0  type macvlan mode private
ping测试
  • 对端交换机接口IP 通
  • 同物理网卡的 macvlan 不通
  • 物理网卡 不通
# ip netns exec ns1 ping -c 1 192.168.0.1
通
# ip netns exec ns1 ping -c 1 192.168.0.12
不通
# ip netns exec ns1 ping -c 1 192.168.0.254
不通
通过抓包可以发现,以看到ARP在重复的发送【注意等待ARP超时后测试】
# tcpdump -i veth11
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on veth11, link-type EN10MB (Ethernet), capture size 262144 bytes
05:29:10.280539 ARP, Request who-has 192.168.0.12 tell 192.168.0.11, length 28
05:29:10.280587 ARP, Request who-has 192.168.0.12 tell 192.168.0.11, length 28
05:29:11.304232 ARP, Request who-has 192.168.0.12 tell 192.168.0.11, length 28
image.png

4、passthru模式

只能被一个macvlan接口使用,并且会继承网卡的mac地址
# ip netns exec ns3 ip link | grep ether
    link/ether 7e:fc:bf:1f:ce:58 brd ff:ff:ff:ff:ff:ff link-netnsid 0
# ip netns exec ns2 ip link delete eth0
# ip netns exec ns1 ip link delete eth0
# ip netns exec ns3 ip link add eth0.11 link eth0 type macvlan mode passthru
# ip netns exec ns3 ip link set eth0.11 netns ns1
# ip netns exec ns1 ip link set eth0.11 name eth0
# ip netns exec ns1 ip addr add 192.168.0.11/24 dev eth0
# ip netns exec ns1 ip link | grep ether
    link/ether 7e:fc:bf:1f:ce:58 brd ff:ff:ff:ff:ff:ff link-netns ns3
# ip netns exec ns1 ip link set eth0 up
ping测试
  • 对端交换机接口IP 通
  • 物理网卡 不通
# ip netns exec ns1 ping -c 1 192.168.0.1
通
# ip netns exec ns1 ping -c 1 192.168.0.254
不通
image.png

5、source

只接受指定源mac的报文,这里是br0的mac地址,MAC为新mac
# ip netns exec ns1 ip link delete eth0
# ip netns exec ns3 ip link add eth0.11 link eth0 type macvlan mode source  macaddr add  36:50:43:11:71:c0
# ip netns exec ns3 ip link set eth0.11 netns ns1
# ip netns exec ns1 ip link set eth0.11 name eth0
# ip netns exec ns1 ip addr add 192.168.0.11/24 dev eth0
# ip netns exec ns1 ip link | grep ether
    link/ether 5e:32:45:fa:21:d3 brd ff:ff:ff:ff:ff:ff link-netns ns3
# ip netns exec ns1 ip link set eth0 up
ping测试
  • 对端交换机接口IP 通
  • 物理网卡 不通
# ip netns exec ns1 ping -c 1 192.168.0.1
通
# ip netns exec ns1 ping -c 1 192.168.0.254
不通
image.png

你可能感兴趣的:(网卡虚拟化 Macvlan)