OpenStack GRE 网络网络介绍

发布时间: 更新时间: 总字数:2656 阅读时间:6m 作者:IP:上海 网址

OpenStack 的网络配置复杂多样,本文讲述的流程只符合以下场景: 1. 网络类型为 GRE 隧道; 2. 单独的网络控制节点;

流程介绍

下面是简单的流程图

OpenStack GRE 隧道网络数据流转全流程示意图

下面是各个部分包含的 PORT

GRE 隧道网络计算节点与网络节点各网桥端口连接拓扑图

下面章节的名称中会包含图中关键点的编号:

  • 计算节点:实例网络 (A,B,C)

所有发出的数据包都是从实例的 eth0 开始的,它连接到 tap 设备,tap 设备连接到 Linux 的网桥设备 qbr。从图中可以看出 tap 设备没有直接连接到集成网桥 br-int,而是通过 qbr 中转了一下,这时为什么了?主要是因为 OVS 的网桥 br-int 没有设置 iptables 规则的功能,但 OpenStack 又想要(或必须)提供安全组服务,那么就借助了 Linux Bridge 的功能。虽说 OVS 的 br-int 网桥和 LinuxBridge 都是二层桥,但是为了功能相互弥补,就同时出现了。

通过在计算节点上查看防火墙的规则,可以发现很多规则都是和 tap 设备相关的

bash
# iptables -S | greptap7c7ae61e-05
-A quantum-openvswi-FORWARD -m physdev --physdev-out tap7c7ae61e-05--physdev-is-bridged -j quantum-openvswi-sg-chain
-A quantum-openvswi-FORWARD -m physdev --physdev-in tap7c7ae61e-05--physdev-is-bridged -j quantum-openvswi-sg-chain
-A quantum-openvswi-INPUT -m physdev --physdev-in tap7c7ae61e-05--physdev-is-bridged -j quantum-openvswi-o7c7ae61e-0
-A quantum-openvswi-sg-chain -m physdev --physdev-out tap7c7ae61e-05--physdev-is-bridged -j quantum-openvswi-i7c7ae61e-0
-A quantum-openvswi-sg-chain -m physdev --physdev-in tap7c7ae61e-05--physdev-is-bridged -j quantum-openvswi-o7c7ae61e-0

quantum-openvswi-sg-chain 是由 neutron-managed security groups 产生的,quantum-openvswi-o7c7ae61e-0 主要用来控制实例发出的 outbound 消息规则,quantum-openvswi-i7c7ae61e-0chain 主要用来控制从外部到实例的 inbound 消息 规则

  • 计算节点: 集成网桥 br-int (D,E)

集成网桥(br-int)负责执行从实例接受或发出的流量中 vlan 标示的拆封工作,此时结算节点上 br-int 的情况应该类似于

bash
#ovs-vsctl show
Bridge br-int
    Port"qvo7c7ae61e-05"
        tag: 1
        Interface "qvo7c7ae61e-05"
    Port patch-tun
        Interface patch-tun
            type: patch
            options:{peer=patch-int}
    Port br-int
        Interface br-int
            type: internal

接口 qvo7c7ae61e-05 与 qvb7c7ae61e-05 是一对接口,它们负责将 Linux 网桥的流量传输到 br-int 上,qvo 上的 tag:1 是一个接入端口,这个端口被挂到了 VLAN1 上,从实例发出的流量会被赋上 VLAN ID 1 的标示,发往该实例的流量也先被拆掉 VLAN 1 的标示,每个不同的网络会被赋予不同的 VLAN ID.

接口 patch-tun 把 br-int 连接到隧道桥( br-tun)上.

  • 计算节点: 隧道桥 br-tun(F,G)

隧道桥主要负责把 br-int 中带有 vlan 标示的流量转换到 GRE 隧道中,实际的转换工作主要是由 br-tun 中 OpenFlow 规则来完成的,在创建实例之前 openflow 的规则大概如下:

bash
#ovs-ofctl dump-flows br-tun
NXST_FLOW reply (xid=0x4):
 cookie=0x0, duration=871.283s, table=0,n_packets=4, n_bytes=300, idle_age=862, priority=1 actions=drop

目前只有一条规则,这条规则会让 br-tun 丢掉所有的流量,当你在计算节点上启动一个实例后,openflow 的规则会被修改成下面的样子:

bash
#ovs-ofctl dump-flows br-run
NXST_FLOW reply (xid=0x4):
 cookie=0x0, duration=422.158s, table=0,n_packets=2, n_bytes=120, idle_age=55,priority=3,tun_id=0x2,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=mod_vlan_vid:1,output:1

 cookie=0x0, duration=421.948s, table=0,n_packets=64, n_bytes=8337, idle_age=31,priority=3,tun_id=0x2,dl_dst=fa:16:3e:dd:c1:62 actions=mod_vlan_vid:1,NORMAL

 cookie=0x0, duration=422.357s, table=0,n_packets=82, n_bytes=10443, idle_age=31, priority=4,in_port=1,dl_vlan=1actions=set_tunnel:0x2,NORMAL

 cookie=0x0, duration=1502.657s, table=0,n_packets=8, n_bytes=596, idle_age=423, priority=1 actions=drop

通常,这些规则负责映射 br-int 使用的 VLAN ID 1,以及 GRE 隧道使用的 tunnel id2

第一条规则

text
cookie=0x0, duration=422.158s, table=0,n_packets=2, n_bytes=120, idle_age=55,priority=3,tun_id=0x2,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00actions=mod_vlan_vid:1,output:1

该流表的匹配条件为从隧道接收且 tun_id=0x2 的广播/组播报文,执行动作为将本地 VLAN ID 修改为 1(mod_vlan_vid:1),并转发输出到端口 1(output:1,即 patch-int)。

通过使用 ovs-ofctlshow br-tun 可以看出端口 1 是 patch-int

bash
#ovs-ofctl show br-tun
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000068df4e44a49
n_tables:254, n_buffers:256
capabilities: FLOW_STATS TABLE_STATS PORT_STATS QUEUE_STATSARP_MATCH_IP
actions: OUTPUT SET_VLAN_VID SET_VLAN_PCP STRIP_VLAN SET_DL_SRC SET_DL_DSTSET_NW_SRC SET_NW_DST SET_NW_TOS SET_TP_SRC SET_TP_DST ENQUEUE
 1(patch-int):addr:46:3d:59:17:df:62
     config:     0
     state:      0
     speed: 0 Mbps now, 0 Mbps max
 2(gre-2):addr:a2:5f:a1:92:29:02
     config:     0
     state:      0
     speed: 0 Mbps now, 0 Mbpsmax
 LOCAL(br-tun):addr:06:8d:f4:e4:4a:49
     config:     0
     state:      0
     speed: 0 Mbps now, 0 Mbpsmax
OFPT_GET_CONFIG_REPLY (xid=0x4): frags=normal miss_send_len=0

有一条规则是

text
cookie=0x0, duration=422.357s, table=0,n_packets=82, n_bytes=10443, idle_age=31, priority=4,in_port=1,dl_vlan=1actions=set_tunnel:0x2,NORMAL

匹配的条件是(in_port=1) , VLAN ID 1 (dl_vlan=1) ,如果匹配就在发出 GRE 隧道之前,设置 tunnel id 为 2 (actions=set_tunnel:0x2) .

  • 网络节点:隧道桥(br-tun) (H,I)

当流量通过连接到 br-tun 的 GRE 隧道到达网络节点,该节点上隧道桥的 flowtable 与计算节点上的非常相似

bash
# ovs-ofctl dump-flowsbr-tun
NXST_FLOW reply (xid=0x4):
 cookie=0x0, duration=1239.229s, table=0,n_packets=23, n_bytes=4246, idle_age=15,priority=3,tun_id=0x2,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=mod_vlan_vid:1,output:1

 cookie=0x0, duration=524.477s, table=0,n_packets=15, n_bytes=3498, idle_age=10,priority=3,tun_id=0x2,dl_dst=fa:16:3e:83:69:cc actions=mod_vlan_vid:1,NORMAL

 cookie=0x0, duration=1239.157s, table=0,n_packets=50, n_bytes=4565, idle_age=148,priority=3,tun_id=0x2,dl_dst=fa:16:3e:aa:99:3c actions=mod_vlan_vid:1,NORMAL

 cookie=0x0, duration=1239.304s, table=0,n_packets=76, n_bytes=9419, idle_age=10, priority=4,in_port=1,dl_vlan=1actions=set_tunnel:0x2,NORMAL

 cookie=0x0, duration=1527.016s, table=0,n_packets=12, n_bytes=880, idle_age=527, priority=1 actions=drop

和在计算节点上一样,第一条规则映射 tunnel ID 2 上的多路广播流量到 VLAN 1

As on thecompute host, the first rule maps multicast traffic on tunnel ID 2 to VLAN 1.

第二条规则

text
cookie=0x0, duration=524.477s, table=0,n_packets=15, n_bytes=3498, idle_age=10,priority=3,tun_id=0x2,dl_dst=fa:16:3e:83:69:cc actions=mod_vlan_vid:1,NORMAL

流量在隧道上转向 fa:16:3e:83:69:cc,这是一个运行在网络命令空间的 dnsmasq 进程

text
...matchestraffic on the tunnel destined for the DHCP server at fa:16:3e:83:69:cc. Thisis a dnsmasq process running inside a network namespace, the details of whichwe will examine shortly.

下一条规则

text
cookie=0x0, duration=1239.157s, table=0,n_packets=50, n_bytes=4565, idle_age=148,priority=3,tun_id=0x2,dl_dst=fa:16:3e:aa:99:3c actions=mod_vlan_vid:1,NORMAL

匹配的流量在 tunnel ID2,转向 fa:16:3e:aa:99:3c 上的 router,这个 router 在另外一个网络命名空间

text
...matchestraffic on tunnel ID 2 destined for the router at fa:16:3e:aa:99:3c, which isan interface in another network namespace.

下一条规则

text
cookie=0x0, duration=1239.304s, table=0,n_packets=76, n_bytes=9419, idle_age=10, priority=4,in_port=1,dl_vlan=1actions=set_tunnel:0x2,NORMAL

将 tunnel id 设置为 2

  • 网络节点:整合桥 br-int(IJ)

该桥负责将实例连接到网络服务上,例如路由或者是 DHCP 服务

bash
# ovs-vsctl show
Bridge br-int
    Port patch-tun
        Interface patch-tun
            type: patch
            options:{peer=patch-int}
    Port"tapf14c598d-98"
        tag: 1
        Interface"tapf14c598d-98"
    Port br-int
        Interface br-int
            type: internal
    Port"tapc2d7dd02-56"
        tag: 1
        Interface"tapc2d7dd02-56"

Br-int 通过使用 patch 接口 patch-tun 连接到 br-tun

  • Network host: DHCP server (O,P)

每一个启动 DHCP 的网络都会有一个 DHCP 服务运行在网络节点上,DHCP 服务是一个运行在网络命名空间的 dnsmasq 实例(网络命名空间(network namespace)是一个 Linux kernel 设备,该设备允许创建一个独立于宿主机的网络栈(栈中可以包含接口,路由表,防火墙规则等))

可以通过 ip netns 的命令查看网络命名空间

text
# ipnetns
qdhcp-88b1609c-68e0-49ca-a658-f1edff54a264
qrouter-2d214fde-293c-4d64-8062-797f80ae2d8f

以 qdhcp 开头的记录是私有网络的命名空间,可以通过执行 neutron net-list 查到与之对应的网络记录

以 qrouter 开头的记录是路由的命名空间,可以通过执行 neutron router-list 查到对应的路由记录

可以通过使用 ip netnsexec command 的方式在一个命名空间内执行一些命令,例如想要查看 DHCP 命名空间的接口配置

bash
# ip netns execqdhcp-88b1609c-68e0-49ca-a658-f1edff54a264 ip addr
71: ns-f14c598d-98: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscpfifo_fast state UP qlen 1000
    link/ether fa:16:3e:10:2f:03 brdff:ff:ff:ff:ff:ff
    inet 10.1.0.3/24 brd 10.1.0.255 scopeglobal ns-f14c598d-98
    inet6 fe80::f816:3eff:fe10:2f03/64scope link
       valid_lft forever preferred_lftforever

接口 ns-f14c598d-98 的 MAC 地址是匹配隧道桥中 flowrule 中的 MAC 地址,这个接口通过 tap 设备连接到 bt-int

text
Port "tapf14c598d-98"
     tag: 1
     Interface"tapf14c598d-98"

可以通过 ps - ef 输出的结果中查到与 dnsmasq 关联的命名空间

bash
# ps -fe | grep88b1609c-68e0-49ca-a658-f1edff54a264
nobody   23195     1  0Oct26 ?        00:00:00 dnsmasq--no-hosts --no-resolv --strict-order --bind-interfaces--interface=ns-f14c598d-98 --except-interface=lo--pid-file=/var/lib/quantum/dhcp/88b1609c-68e0-49ca-a658-f1edff54a264/pid--dhcp-hostsfile=/var/lib/quantum/dhcp/88b1609c-68e0-49ca-a658-f1edff54a264/host--dhcp-optsfile=/var/lib/quantum/dhcp/88b1609c-68e0-49ca-a658-f1edff54a264/opts--dhcp-script=/usr/bin/quantum-dhcp-agent-dnsmasq-lease-update --leasefile-ro--dhcp-range=tag0,10.1.0.0,static,120s --conf-file= --domain=openstacklocal
root     23196 23195  0 Oct26 ?        00:00:00 dnsmasq --no-hosts --no-resolv--strict-order --bind-interfaces --interface=ns-f14c598d-98--except-interface=lo--pid-file=/var/lib/quantum/dhcp/88b1609c-68e0-49ca-a658-f1edff54a264/pid--dhcp-hostsfile=/var/lib/quantum/dhcp/88b1609c-68e0-49ca-a658-f1edff54a264/host--dhcp-optsfile=/var/lib/quantum/dhcp/88b1609c-68e0-49ca-a658-f1edff54a264/opts--dhcp-script=/usr/bin/quantum-dhcp-agent-dnsmasq-lease-update --leasefile-ro--dhcp-range=tag0,10.1.0.0,static,120s --conf-file= --domain=openstacklocal
  • 网络节点:路由 (M,N)

Neutron 的路由是一个包含路由表以及 iptables 规则的网络的命名空间,主要负责不同子网间的路由。回到 ip netns 的执行结果

text
# ipnetns
qdhcp-88b1609c-68e0-49ca-a658-f1edff54a264
qrouter-2d214fde-293c-4d64-8062-797f80ae2d8f

通过执行 ip netnsexec 命令可以发现与路由命名空间绑定的接口

bash
# ip netns execqrouter-2d214fde-293c-4d64-8062-797f80ae2d8f ip addr
66: qg-d48b49e0-aa: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscpfifo_fast state UP qlen 1000
    link/ether fa:16:3e:5c:a2:ac brdff:ff:ff:ff:ff:ff
    inet 172.24.4.227/28 brd 172.24.4.239scope global qg-d48b49e0-aa
    inet 172.24.4.228/32 brd 172.24.4.228scope global qg-d48b49e0-aa
    inet6 fe80::f816:3eff:fe5c:a2ac/64scope link
       valid_lft forever preferred_lftforever
68: qr-c2d7dd02-56: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscpfifo_fast state UP qlen 1000
    link/ether fa:16:3e:ea:64:6e brdff:ff:ff:ff:ff:ff
    inet 10.1.0.1/24 brd 10.1.0.255 scopeglobal qr-c2d7dd02-56
    inet6 fe80::f816:3eff:feea:646e/64scope link
       valid_lft forever preferred_lftforever

从图中可以看出 qg-d48b49e0-aa,以及 qr-c2d7dd02-56 主要是用来连接 br-int 以及 br-ex 的

通过观察这个路由的路由表,可以看到有一个外部网络的默认 gateway 指向 qg-d48b49e0-aa

bash
# ip netns execqrouter-2d214fde-293c-4d64-8062-797f80ae2d8f ip route
172.24.4.224/28 dev qg-d48b49e0-aa  protokernel  scope link  src 172.24.4.227
10.1.0.0/24 dev qr-c2d7dd02-56  protokernel  scope link  src 10.1.0.1
default via 172.24.4.225 dev qg-d48b49e0-aa

在路由命名空间中的 nat 表格主要负责关联实例以及浮动 IP,例如当把浮动 IP172.24.4.228 绑定到实例上后,NAT 表看起来应该是下面的样子:

bash
# ip netns execqrouter-2d214fde-293c-4d64-8062-797f80ae2d8f iptables -t nat -S
-P PREROUTING ACCEPT
-P POSTROUTING ACCEPT
-P OUTPUT ACCEPT
-N quantum-l3-agent-OUTPUT
-N quantum-l3-agent-POSTROUTING
-N quantum-l3-agent-PREROUTING
-N quantum-l3-agent-float-snat
-N quantum-l3-agent-snat
-N quantum-postrouting-bottom
-A PREROUTING -j quantum-l3-agent-PREROUTING
-A POSTROUTING -j quantum-l3-agent-POSTROUTING
-A POSTROUTING -j quantum-postrouting-bottom
-A OUTPUT -j quantum-l3-agent-OUTPUT
-Aquantum-l3-agent-OUTPUT -d 172.24.4.228/32 -j DNAT --to-destination 10.1.0.2
-A quantum-l3-agent-POSTROUTING ! -i qg-d48b49e0-aa ! -oqg-d48b49e0-aa -m conntrack ! --ctstate DNAT -j ACCEPT
-A quantum-l3-agent-PREROUTING -d 169.254.169.254/32 -p tcp -m tcp --dport 80-j REDIRECT --to-ports 9697
-Aquantum-l3-agent-PREROUTING -d 172.24.4.228/32 -j DNAT --to-destination10.1.0.2
-A quantum-l3-agent-float-snat -s 10.1.0.2/32 -j SNAT--to-source 172.24.4.228
-A quantum-l3-agent-snat -j quantum-l3-agent-float-snat
-A quantum-l3-agent-snat -s 10.1.0.0/24 -j SNAT --to-source 172.24.4.227
-A quantum-postrouting-bottom -j quantum-l3-agent-snat

主要使用 SNAT 以及 DNAT 的规则来映射浮动 IP 以及固定 IP

text
-Aquantum-l3-agent-OUTPUT -d 172.24.4.228/32 -j DNAT --to-destination 10.1.0.2
-A quantum-l3-agent-PREROUTING -d 172.24.4.228/32 -j DNAT --to-destination10.1.0.2
-A quantum-l3-agent-float-snat -s 10.1.0.2/32 -j SNAT--to-source 172.24.4.228

下面的这条规则主要表示所有 outbound 的流量都会被 NAT 从私有网络转换成 172.24.4.227,这条规则允许即使实例没有浮动 Ip 也可以有 outbound 的连接性

text
-Aquantum-l3-agent-snat -s 10.1.0.0/24 -j SNAT --to-source 172.24.4.227
  • 网络节点:外部流量 (K,L)

br-int 通过 qr 设备借助 Linux 网络命名空间 qrouter 连通 br-ex 上的 qg 设备,将数据包交付到 OVS 网桥 br-ex 上.

接下来流量的走向主要依赖于本地的配置

NAT to host address

如果将 public 网络的 gateway 赋到 br-ex 上:

bash
# ip addr add 172.24.4.225/28 devbr-ex

然后你可以创建 forwarding 以及 NAT 的规则,这些规则会引起外部流量从实例重新写到网络控制器的 IP 地址,然后发出去

bash
#iptables -A FORWARD -d 172.24.4.224/28 -j ACCEPT
# iptables -A FORWARD -s 172.24.4.224/28 -j ACCEPT
# iptables -t nat -I POSTROUTING 1 -s 172.24.4.224/28 -j MASQUERADE

Direct network connection

如果你有一个对于公共网络而言作为 gateway 的外部路由,你可以在 br-ex 上添加一个接口。例如 eth2 是在同一个网络中,地址是 172.24.4.225

bash
#ovs-vsctl add-port br-ex eth2

参考

  1. https://www.rdoproject.org/networking/networking-in-too-much-detail/