KVM Bridge 僅單向轉送封包

KVM Bridge 僅單向轉送封包

我已經使用 virsh/libvirt/kvm 配置了 Linux VM,但橋接網路出現問題。 libvirt 的預設 NAT 網路運作正常,但我需要橋接到公共介面。我已使用 virsh 刪除了預設網絡,並將 virsh 中的虛擬機器網絡設定更改為:

    <interface type='bridge'>
      <mac address='02:00:00:c1:8f:95'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    </interface>

在主機系統上我配置了網橋:

# brctl show br0
bridge name     bridge id               STP enabled     interfaces
br0             8000.a0423f494574       yes             enp97s0f0
                                                        vnet0

# brctl showstp br0
br0
 bridge id              8000.a0423f494574
 designated root        8000.a0423f494574
 root port                 0                    path cost                  0
 max age                  20.00                 bridge max age            20.00
 hello time                2.00                 bridge hello time          2.00
 forward delay            15.00                 bridge forward delay      15.00
 ageing time             300.00
 hello timer               0.86                 tcn timer                  0.00
 topology change timer     0.00                 gc timer                 240.41
 flags


enp97s0f0 (1)
 port id                8001                    state                forwarding
 designated root        8000.a0423f494574       path cost                100
 designated bridge      8000.a0423f494574       message age timer          0.00
 designated port        8001                    forward delay timer        0.00
 designated cost           0                    hold timer                 0.00
 flags

vnet0 (2)
 port id                8002                    state                forwarding
 designated root        8000.a0423f494574       path cost                100
 designated bridge      8000.a0423f494574       message age timer          0.00
 designated port        8002                    forward delay timer        0.00
 designated cost           0                    hold timer                 0.00

其中 enp97s0f0 是公用網路卡,vnet0 是 libvirt 使用的虛擬網路卡。

我已經在虛擬機器上設定了 IP 和網關,但沒有收到 ping 回覆。在從虛擬機器 ping 到 1.1.1.1 cloudflare dns 期間,我在 vnet0 和 br0 上的主機上完成了 tcpdump。結果如下:

# tcpdump -i vnet0 -n host VM.VM.VM.VM
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on vnet0, link-type EN10MB (Ethernet), capture size 262144 bytes
15:13:50.863273 IP VM.VM.VM.VM > 1.1.1.1: ICMP echo request, id 2016, seq 80, length 64
15:13:51.887267 IP VM.VM.VM.VM > 1.1.1.1: ICMP echo request, id 2016, seq 81, length 64
15:13:52.911271 IP VM.VM.VM.VM > 1.1.1.1: ICMP echo request, id 2016, seq 82, length 64
15:13:53.935270 IP VM.VM.VM.VM > 1.1.1.1: ICMP echo request, id 2016, seq 83, length 64
# tcpdump -i br0 -n host VM.VM.VM.VM
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on br0, link-type EN10MB (Ethernet), capture size 262144 bytes
15:12:33.039228 IP VM.VM.VM.VM > 1.1.1.1: ICMP echo request, id 2016, seq 4, length 64
15:12:33.045522 IP 1.1.1.1 > VM.VM.VM.VM: ICMP echo reply, id 2016, seq 4, length 64
15:12:33.045532 IP 1.1.1.1 > VM.VM.VM.VM: ICMP echo reply, id 2016, seq 4, length 64
15:12:34.063111 IP VM.VM.VM.VM > 1.1.1.1: ICMP echo request, id 2016, seq 5, length 64
15:12:34.069501 IP 1.1.1.1 > VM.VM.VM.VM: ICMP echo reply, id 2016, seq 5, length 64
15:12:34.069510 IP 1.1.1.1 > VM.VM.VM.VM: ICMP echo reply, id 2016, seq 5, length 64
15:12:35.087281 IP VM.VM.VM.VM > 1.1.1.1: ICMP echo request, id 2016, seq 6, length 64
15:12:35.093640 IP 1.1.1.1 > VM.VM.VM.VM: ICMP echo reply, id 2016, seq 6, length 64
15:12:35.093649 IP 1.1.1.1 > VM.VM.VM.VM: ICMP echo reply, id 2016, seq 6, length 64

所以我可以看到,虛擬機器正在透過 vnet0 發送 ping 請求,網橋 br0 正在將此請求轉發到 1.1.1.1,而 1.1.1.1 正在將回覆傳送回網橋。然後封包消失並且永遠不會到達虛擬機器介面。

這就是為什麼我認為這個問題可能與主機上的某種資料包過濾有關,但我檢查了一堆變量,一切看起來都正常:

/proc/sys/net/ipv4/ip_forward : 1
/proc/sys/net/ipv4/conf/br0/forwarding : 1
/proc/sys/net/ipv4/conf/vnet0/forwarding : 1
/proc/sys/net/ipv4/conf/enp97s0f0/forwarding : 1
/proc/sys/net/bridge/bridge-nf-call-iptables: No such file or directory
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
# ebtables -L
Bridge table: filter

Bridge chain: INPUT, entries: 0, policy: ACCEPT

Bridge chain: FORWARD, entries: 0, policy: ACCEPT

Bridge chain: OUTPUT, entries: 0, policy: ACCEPT

# nft list ruleset
table bridge filter {
        chain INPUT {
                type filter hook input priority filter; policy accept;
        }

        chain FORWARD {
                type filter hook forward priority filter; policy accept;
        }

        chain OUTPUT {
                type filter hook output priority filter; policy accept;
        }
}
table ip filter {
        chain INPUT {
                type filter hook input priority filter; policy accept;
        }

        chain FORWARD {
                type filter hook forward priority filter; policy accept;
        }

        chain OUTPUT {
                type filter hook output priority filter; policy accept;
        }
}
[root@HOST ~] # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp97s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000
    link/ether a0:42:3f:49:45:74 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether a0:42:3f:49:45:74 brd ff:ff:ff:ff:ff:ff
    inet xxx.xxx.xxx.xxx/24 scope global dynamic noprefixroute br0
       valid_lft 71705sec preferred_lft 71705sec
    inet6 fe80::6559:22b2:dccd:3b24/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
4: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UNKNOWN group default qlen 1000
    link/ether fe:00:00:c1:8f:95 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc00:ff:fec1:8f95/64 scope link
       valid_lft forever preferred_lft forever
[root@VM ~]# ip addr show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp1s0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 02:00:00:c1:8f:95 brd ff:ff:ff:ff:ff:ff
    inet VM.VM.VM.VM/24 scope global enp1s0
       valid_lft forever preferred_lft forever
[root@HOST ~]# bridge link
2: enp97s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 100
8: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 100
[root@HOST ~]# bridge fdb show
fc:bd:67:ff:ec:6a dev enp97s0f0 master br0
fc:bd:67:ff:ec:31 dev enp97s0f0 master br0
2c:dd:e9:0d:9d:31 dev enp97s0f0 master br0
fe:ed:de:ad:be:ef dev enp97s0f0 master br0
a0:42:3f:49:45:74 dev enp97s0f0 vlan 1 master br0 permanent
a0:42:3f:49:45:74 dev enp97s0f0 master br0 permanent
01:00:5e:00:00:01 dev enp97s0f0 self permanent
33:33:00:00:00:01 dev enp97s0f0 self permanent
01:00:5e:00:00:01 dev br0 self permanent
33:33:00:00:00:01 dev br0 self permanent
33:33:ff:cd:3b:24 dev br0 self permanent
01:00:5e:00:00:6a dev br0 self permanent
33:33:00:00:00:6a dev br0 self permanent
02:00:00:c1:8f:95 dev vnet0 master br0
fe:00:00:c1:8f:95 dev vnet0 vlan 1 master br0 permanent
fe:00:00:c1:8f:95 dev vnet0 master br0 permanent
33:33:00:00:00:01 dev vnet0 self permanent
01:00:5e:00:00:01 dev vnet0 self permanent
33:33:ff:c1:8f:95 dev vnet0 self permanent
[root@HOST ~]# bridge vlan show
port              vlan-id
enp97s0f0         1 PVID Egress Untagged
vnet0             1 PVID Egress Untagged

主機系統是 Centos8,核心為 5.13.12-1.el8.elrepo.x86_64。 VM系統預設安裝Centos8。

編輯:

如果我在主機 br0 和虛擬機器 enp1s0 介面上配置專用網路 IP,則虛擬機器能夠 ping 主機,主機也能夠 ping 虛擬機器。但仍無法 ping 通本地網路以外的任何內容。

答案1

透過在主機上建立到虛擬機器 IP 位址的路由來解決:

route add -host VM.VM.VM.VM dev br0

相關內容