.png)
我正在嘗試建立虛擬介面(類型 MACVLAN),以便介面之間的所有通訊都從主機發送到永恆的預設網關。有許多描述「私有」模式的文章,例如這裡
專用:過濾所有傳入資料包,以便綁定到介面的 MAC VLAN 無法相互通訊(丟棄透過此介面傳入的所有資料包,這些資料包的來源 MAC 位址與 MAC VLAN 介面之一相符)。
我配置了幾個接口,看起來「私有」模式並沒有像宣傳的那樣運作。難道我做錯了什麼?主機是Ubuntu 18.04 Bionic版本。
封包在主機內進行交換,忽略「模式專用」指令。只需 4 個指令即可輕鬆重現。任何幫助,將不勝感激。
root@ubnt-bkp:/home/super# uname -a
Linux ubnt-bkp 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
root@ubnt-bkp:/home/super# ip link add link ens160 address 38:94:ed:99:99:1A ens160.3 type macvlan mode private
root@ubnt-bkp:/home/super# ip link set ens160.3 up
root@ubnt-bkp:/home/super# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 08:4f:a9:99:99:02 brd ff:ff:ff:ff:ff:ff
...
...
18: ens160.3@ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP **mode** **DEFAULT** group default qlen 1000
link/ether 38:94:ed:99:99:1a brd ff:ff:ff:ff:ff:ff
重現問題的命令
root@ubnt-bkp:/home/super# ip link add link ens160 address 38:94:ed:99:99:1B ens160.5 type macvlan mode private
root@ubnt-bkp:/home/super# ip link set ens160.5 up
root@ubnt-bkp:/home/super# dhclient ens160.5
root@ubnt-bkp:/home/super# ip link add link ens160 address 38:94:ed:99:99:1C ens160.6 type macvlan mode private
root@ubnt-bkp:/home/super# ip link set ens160.6 up
root@ubnt-bkp:/home/super# dhclient ens160.6
然後ping
使用-I
選項:
root@ubnt-bkp:/home/super#ping -I <IP address of ens160.5> <IP address of ens160.6>
(ens160
嘗試重現時,替換為您的乙太網路介面名稱,例如 eth0)
答案1
您用於建立 MACVLAN 介面的命令是正確的。
但是,使用ping -I
並不能幫助您驗證這一點。當來源介面和目標介面位於同一主機上(並且在同一命名空間中,如您的情況)時,ping
總是會成功。
那你要如何測試這個呢?透過將 MACVLAN 介面放置在不同的非預設命名空間中。然後ping
從命名空間執行。
假設您從ens160
具有 IP 位址的實體介面 ()開始192.0.2.2/24
。我們也假設您的路由器網關位址是192.0.2.1
。
現在:
# Create namespace named "ns5"
ip netns add ns5
# Create the macvlan interface as usual
ip link add link ens160 address 38:94:ed:99:99:1B ens160.5 type macvlan mode private
# Move the just-created macvlan interface into the "ns5" namespace
ip link set ens160.5 netns ns5
# Set the link up. Observe the "ip netns exec ns5" prefix.
ip netns exec ns5 ip link set ens160.5 up
# Set the IP address for the macvaln interface. Observe the "ip netns exec ns5" prefix.
ip netns exec ns5 ip address add dev ens160.5 192.0.2.205/24
# Repeat above for another macvlan interface, this one in another namespace
# called "ns6"
ip netns add ns6
ip link add link ens160 address 38:94:ed:99:99:1C ens160.6 type macvlan mode private
ip link set ens160.6 netns ns6
ip netns exec ns6 ip link set ens160.6 up
ip netns exec ns6 ip address add dev ens160.6 192.0.2.206/24
從此時起,兩個 macvtap 介面都能夠成功 ping 通網關:
ip netns exec ns5 ping 192.0.2.1
ip netns exec ns6 ping 192.0.2.1
如果您有權存取網關設備,則應該能夠確認 192.0.2.205 和 192.0.2.6 的 ARP 條目顯示 macvlan 介面的 MAC 位址。
但是,由於 macvlan 介面處於private
模式,因此以下命令都會失敗:
# Try to reach ens160.6 from ens160.5
ip netns exec ns5 ping 192.0.2.206
# Try to reach ens160.5 from ens160.6
ip netns exec ns6 ping 192.0.2.205
重複實驗,bridge
而不是private
;您將觀察到最後兩個 ping 命令將會成功。