Networking

為什麼我無法在橋接模式下的兩個兄弟 macvlan 之間 ping?

  • August 1, 2020

在連接到我家 LAN 的 Ubuntu 20.04 機器上,我在我的乙太網設備下創建了 macvlan:

$ sudo ip link add macvlan1 link enp37s0 type macvlan mode bridge
$ sudo dhclient macvlan1
$ sudo ip link add macvlan2 link enp37s0 type macvlan mode bridge
$ sudo dhclient macvlan2

偶然地,他們從 DHCP 獲得了以下地址:

  • macvlan1:192.168.0.40
  • macvlan2:192.168.0.41

我可以從他們每個人 ping 我的路由器:

$ ping 192.168.0.1 -I macvlan1
PING 192.168.0.1 (192.168.0.1) from 192.168.0.40 macvlan1: 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.713 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=1.25 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=64 time=1.20 ms
^C
--- 192.168.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2034ms
rtt min/avg/max/mdev = 0.713/1.052/1.245/0.240 ms

$ ping 192.168.0.1 -I macvlan2
PING 192.168.0.1 (192.168.0.1) from 192.168.0.41 macvlan2: 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=1.15 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=1.13 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=64 time=1.07 ms
64 bytes from 192.168.0.1: icmp_seq=4 ttl=64 time=0.548 ms
64 bytes from 192.168.0.1: icmp_seq=5 ttl=64 time=0.619 ms
^C
--- 192.168.0.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4030ms
rtt min/avg/max/mdev = 0.548/0.903/1.148/0.263 ms

由於它們是在橋接模式下創建的,因此虛擬設備應該相互連接。但我無法從另一個 ping 通:

$ ping 192.168.0.40 -I macvlan2
PING 192.168.0.40 (192.168.0.40) from 192.168.0.41 macvlan2: 56(84) bytes of data.
^C
--- 192.168.0.40 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4128ms

$ ping 192.168.0.41 -I macvlan1
PING 192.168.0.41 (192.168.0.41) from 192.168.0.40 macvlan1: 56(84) bytes of data.
^C
--- 192.168.0.41 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2039ms

為什麼我不能從另一個 ping 通?如何解決這個問題?

Linux 核心檢測具有本地源地址的傳入數據包,將其視為路由錯誤(特定電腦發出的數據包不應返回),並丟棄它們以防止網路氾濫。

嘗試將每個 macvlan 放入不同的網路命名空間(畢竟,macvlan 被設計為從網路命名空間使用)。然後它應該工作。

引用自:https://unix.stackexchange.com/questions/602384