網(wǎng)卡虛擬化 Macvlan

拓?fù)浣Y(jié)構(gòu)
image.png
交換機(jī)配置
# 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 所有的端點(diǎn)都直接連接在一起
  • VEPA 虛擬以太網(wǎng)端口聚合器模式。在同一物理接口上的不同macvlan接口之間的數(shù)據(jù)通過物理接口傳輸。macvlan接口之間通信需要交換機(jī)支持發(fā)夾( hairpin)模式,或者有一個(gè)路由器來轉(zhuǎn)發(fā)。macvlan的缺省模式為VEPA
  • Private 不允許同一物理接口上的macvlan實(shí)例之間通信,即使外部交換機(jī)支持發(fā)夾( hairpin)模式。
  • passthru [nopromisc] -這種模式給單個(gè)端點(diǎn)更多的能力,通常在macvtap模式。不允許在同一物理接口上有一個(gè)以上的端點(diǎn)。所有流量將被轉(zhuǎn)發(fā)到這個(gè)端點(diǎn),從而允許virtio來賓更改MAC地址或設(shè)置混雜模式,以便在接口上架橋或在其上創(chuàng)建vlan接口。默認(rèn)情況下,此模式強(qiáng)制底層接口進(jìn)入混雜模式。傳遞nopromisc標(biāo)志可以防止這種情況發(fā)生,因此可以使用標(biāo)準(zhǔn)工具控制promisc標(biāo)志。
  • Source-允許一個(gè)允許的mac地址列表,這是用來匹配源mac地址從接收幀在底層‐ing接口。這允許創(chuàng)建基于mac的VLAN關(guān)聯(lián),而不是基于標(biāo)準(zhǔn)端口或標(biāo)簽。這個(gè)特性對于部署基于802.1xmac的行為很有用,因?yàn)榈讓咏涌诘尿?qū)動程序不允許這樣做。

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中的網(wǎng)卡名字
# 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測試
  • 對端交換機(jī)接口IP 可以通
  • 物理網(wǎng)卡 不通
  • 同物理網(wǎng)卡的 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
...
關(guān)閉veth11,macvlan之間不通
# ip link set veth11 down
//down一個(gè)veth,peer也會down
# ip netns exec ns3 ip link show type veth
19: eth0@if20: <NO-CARRIER,BROADCAST,MULTICAST,UP> 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
不通
恢復(fù)環(huán)境
# ip link set veth11 up 
image.png

2、VEPA模式

此模式下macvlan之間是隔離的,需要交換機(jī)支持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測試
  • 對端交換機(jī)接口IP 通
  • 同物理網(wǎng)卡的 macvlan 不通
  • 物理網(wǎng)卡 不通
# 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報(bào)文
# 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更強(qiáng),阻斷了廣播和組播,即使交換機(jī)開啟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測試
  • 對端交換機(jī)接口IP 通
  • 同物理網(wǎng)卡的 macvlan 不通
  • 物理網(wǎng)卡 不通
# 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
不通
通過抓包可以發(fā)現(xiàn),以看到ARP在重復(fù)的發(fā)送【注意等待ARP超時(shí)后測試】
# 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模式

只能被一個(gè)macvlan接口使用,并且會繼承網(wǎng)卡的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測試
  • 對端交換機(jī)接口IP 通
  • 物理網(wǎng)卡 不通
# 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的報(bào)文,這里是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測試
  • 對端交換機(jī)接口IP 通
  • 物理網(wǎng)卡 不通
# ip netns exec ns1 ping -c 1 192.168.0.1
通
# ip netns exec ns1 ping -c 1 192.168.0.254
不通
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容