0. 测试环境
硬件环境:还是使用四节点OpenStack部署环境,参见 http://www.cnblogs.com/sammyliu/p/4190843.html
OpenStack配置:
- tenant:三个tenant:demo,tenant-one,tenant-two
- network:三个tenanet公用public network,每个tenant拥有自己的subnet,都有一个router连接自己的subnet到public net
- 虚机:三个虚机,tenant-one一个,tenant-two两个,都在compute node上
1. Neutron节点上的网络组件
使用 http://www.cnblogs.com/sammyliu/p/4201143.html中相同的方法,画出Neutron节点上网络组件图:
可见:
(1). 关于Neutron上的三种Agent的作用:
- Neutron-OVS-Agent:从OVS-Plugin上接收tunnel和tunnel flow的配置,驱动OVS来建立GRE Tunnel
- Neutron-DHCP-Agent:为每一个配置了DHCP的网络/子网配置dnsmasq,也负责把Mac地址/IP地址 信息写入dnsmasq dhcp lease 文件
- Neturon-L3-Agent:设置iptables/routing/NAT表
(2). Neutorn节点上同样有OVS Tunnel bridge br-tun和OVS Integration bridge br-int,多了br-ex来提供外部网络连接,br-ex和物理网卡eth0绑定。这里出现的一个问题是eth0的IP无法ping通,OVS提供的解决方法如下。究其原因,一块物理以太网卡如果作为 OpenvSwitch bridge 的一部分,则它不能拥有 IP 地址,如果有,也会完全不起作用。如果发生了上述情况,可以将 IP 地址绑定至某 OpenvSwitch “internal” 设备来恢复网络访问功能。
ifconfig eth0 0.0.0.0
ifconfig br-ex 192.168.1.19
(3). Neutron使用Linux network namespace来实现tenant之间的网络隔离。本例中有三个network namespace,每个network namspace包括router,dhcp,interface,routing tables,iptable rules等。
root@network:/home/s1# ip netns
qdhcp-d24963da--481e-adf5-fe033d6e0b4e
qrouter-e506f8fe---bd06-32246225aeae
qdhcp-d04a0a06--4d05--3443843bc199
qrouter-33e2b1bf-04cb--9c58-7e03856022c1
qrouter-9ba04071-f32b-435e-8f44-e32936568102
qdhcp-0a4cd030-d951-401a--937b788bea43
(4). Neutron 为每一个 network 分配一个本地的 VLAN ID,每个 network 分配一个 network namespace,该DHCP 通过一个 tap 连接在 br-int 上,该 tap 的 tag 为该 local VLAN ID。H1/H2/H3端口上分布有不同的VLAN ID。
#在存在多个 network 的情况下,br-int 上DHCP namespace 端口的 tag 情况
Port "tap0f45d165-9f"
tag:
Interface "tap0f45d165-9f"
type: internal
Port "tap89874f55-97"
tag:
Interface "tap89874f55-97"
type: internal
Port "tap5522533d-fe"
tag:
Interface "tap5522533d-fe"
type: internal
Port "tap56c9730c-9c"
tag:
Interface "tap56c9730c-9c"
type: internal
Port "tap1fd04a93-09"
tag:
Interface "tap1fd04a93-09"
type: internal
Port "tap777c1047-ed"
tag:
Interface "tap777c1047-ed"
type: internal
Port "tap3fca96e0-c6"
tag:
Interface "tap3fca96e0-c6"
type: internal
1.1 br-tun OpenFlow rules
插播Mac地址的基础知识:
- MAC地址是以太网二层使用的一个48bit(6字节十六进制数)的地址,用来标识设备位置。MAC地址分成两部分,前24位是组织唯一标识符(OUI, Organizationally unique identifier),后24位由厂商自行分配。48bit的MAC地址一般用6字节的十六进制来表示,如XX-XX-XX-XX-XX-XX。
- 广播地址:FF:FF:FF:FF:FF:FF
- 组播地址:MAC组播地址的特征是头8位的最低位是1。例如:80:C2:00:00:00是一个组播地址,表示802.1d网桥多播组。网桥就是使用这个地址,相互之间交换配置信息,运行分布式生成树算法,消除网络拓扑结构中的环路。
- 单播地址:单播地址的特征是头8位的最低位为0。每个网卡出厂时被分配唯一一个单播地址,头24位是设备制造厂商的编号,由IEEE(电气与电子工程师协会)分配,后24位是设备厂商为网卡制定的唯一编号。例如8C-70-5A-29-3A-48 是单播地址的例子 (8C = 1000110)。
root@network:/home/s1# ovs-ofctl dump-flows br-tun
NXST_FLOW reply (xid=0x4):
cookie=0x0, duration=33.236s, table=0, n_packets=0, n_bytes=0, idle_age=33, priority=1,in_port=1 actions=resubmit(,2) //从H1进来的traffic,到table 2
cookie=0x0, duration=32.131s, table=0, n_packets=0, n_bytes=0, idle_age=32, priority=1,in_port=2 actions=resubmit(,3) //从GRE端口进来的traffic,到table 3
cookie=0x0, duration=33.178s, table=0, n_packets=6, n_bytes=480, idle_age=24, priority=0 actions=drop
cookie=0x0, duration=33.121s, table=2, n_packets=0, n_bytes=0, idle_age=33, priority=0,dl_dst=00:00:00:00:00:00/01:00:00:00:00:00 actions=resubmit(,20) //目的地址为单播地址,到table 20
cookie=0x0, duration=33.066s, table=2, n_packets=0, n_bytes=0, idle_age=33, priority=0,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=resubmit(,22) //目的地址为组播(包括广播)地址,到table 22
cookie=0x0, duration=30.614s, table=3, n_packets=0, n_bytes=0, idle_age=30, priority=1,tun_id=0x1 actions=mod_vlan_vid:1,resubmit(,10) //Tunnel 1的traffic,修改VLAN ID 为 1, 再到 table 10
cookie=0x0, duration=29.291s, table=3, n_packets=0, n_bytes=0, idle_age=29, priority=1,tun_id=0x2 actions=mod_vlan_vid:3,resubmit(,10) //Tunnel 2的traffic,修改VLAN ID 为 2, 再到 table 10
cookie=0x0, duration=30.241s, table=3, n_packets=0, n_bytes=0, idle_age=30, priority=1,tun_id=0x3 actions=mod_vlan_vid:2,resubmit(,10) //Tunnel 3的traffic,修改VLAN ID 为 3, 再到 table 10
cookie=0x0, duration=33.001s, table=3, n_packets=0, n_bytes=0, idle_age=33, priority=0 actions=drop
cookie=0x0, duration=32.932s, table=4, n_packets=0, n_bytes=0, idle_age=32, priority=0 actions=drop
cookie=0x0, duration=32.874s, table=10, n_packets=0, n_bytes=0, idle_age=32, priority=1 actions=learn(table=20,hard_timeout=300,priority=1,NXM_OF_VLAN_TCI[0..11],NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],load:0->NXM_OF_VLAN_TCI[],load:NXM_NX_TUN_ID[]->NXM_NX_TUN_ID[],output:NXM_OF_IN_PORT[]),output:1 //学习一条新的规则添加到table 20,发到端口1,进入br-int
cookie=0x0, duration=32.815s, table=20, n_packets=0, n_bytes=0, idle_age=32, priority=0 actions=resubmit(,22) //到table 22
cookie=0x0, duration=29.35s, table=22, n_packets=0, n_bytes=0, idle_age=29, dl_vlan=3 actions=strip_vlan,set_tunnel:0x2,output:2
cookie=0x0, duration=30.293s, table=22, n_packets=0, n_bytes=0, idle_age=30, dl_vlan=2 actions=strip_vlan,set_tunnel:0x3,output:2
cookie=0x0, duration=30.682s, table=22, n_packets=0, n_bytes=0, idle_age=30, dl_vlan=1 actions=strip_vlan,set_tunnel:0x1,output:2 //以上三条rule,根据目的VLAN ID,修改Tunnel ID,并去掉VLAN ID,发到GRE端口,经过GRE Tunnel到compute node
cookie=0x0, duration=32.752s, table=22, n_packets=0, n_bytes=0, idle_age=32, priority=0 actions=drop
总之,br-tun会:
- 把从GRE端口来的traffic设置相应的VLAN ID,发到br-int
- 把从br-int/patch-int来的traffic,去掉VLAN ID,设置相应的Trunne ID,经过GRE端口H1 发到Compute节点
2. Router Server
2.1 以tenant-one (有一个虚机)的router为例,先看看它的interface (略去lo)
root@network:/home/s1# ip netns exec qrouter-33e2b1bf-04cb--9c58-7e03856022c1 ip addr
: qr-d3d3e235-d4: <BROADCAST,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default
link/ether fa::3e:b3::e8 brd ff:ff:ff:ff:ff:ff
inet 10.0.11.1/ brd 10.0.11.255 scope global qr-d3d3e235-d4
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:feb3:6e8/ scope link
valid_lft forever preferred_lft forever
: qg-6c06581b-bd: <BROADCAST,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default
link/ether fa::3e:0b:ac: brd ff:ff:ff:ff:ff:ff
inet 192.168.1.114/ brd 192.168.1.255 scope global qg-6c06581b-bd
valid_lft forever preferred_lft forever
inet 192.168.1.115/ brd 192.168.1.115 scope global qg-6c06581b-bd
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe0b:ac82/ scope link
valid_lft forever preferred_lft forever
可见:
- qg-6c06581b-bd 连接 br-ex
- qr-d3d3e235-d4连接br-int
再看看它的route规则:
root@network:/home/s1# ip netns exec qrouter-33e2b1bf-04cb-4811-9c58-7e03856022c1 route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 qg-6c06581b-bd
//默认路由,所有目的地址不在本网络中的traffic都要通过 qg-d3657c7f-28 interface 发到外网网关192.168.1.1
10.0.11.0 0.0.0.0 255.255.255.0 U 0 0 0 qr-d3d3e235-d4
//目的为本子网内的traffic 经过 qr-d3d3e235-d4 发到子网网关 10.0.11.1
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 qg-6c06581b-bd
//目的为 192.168.1.0/24 的traffic通过 qg-6c06581b-bd 发到网关192.168.1.100
2.2 Neutorn Floating IP 实现原理
Router namespace中的 netfilter NAT 表负责 Neutron Floating IP 的实现。下面是tenant-two (有两个虚机)的router的NAT表:
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
-N neutron-l3-agent-OUTPUT
-N neutron-l3-agent-POSTROUTING
-N neutron-l3-agent-PREROUTING
-N neutron-l3-agent-float-snat
-N neutron-l3-agent-snat
-N neutron-postrouting-bottom
-A PREROUTING -j neutron-l3-agent-PREROUTING
-A OUTPUT -j neutron-l3-agent-OUTPUT
-A POSTROUTING -j neutron-l3-agent-POSTROUTING
-A POSTROUTING -j neutron-postrouting-bottom
-A neutron-l3-agent-OUTPUT -d 192.168.1.118/32 -j DNAT --to-destination 10.0.22.200
-A neutron-l3-agent-OUTPUT -d 192.168.1.117/32 -j DNAT --to-destination 10.0.22.202
-A neutron-l3-agent-POSTROUTING ! -i qg-cba7b139-04 ! -o qg-cba7b139-04 -m conntrack ! --ctstate DNAT -j ACCEPT
-A neutron-l3-agent-PREROUTING -d 169.254.169.254/32 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 9697
-A neutron-l3-agent-PREROUTING -d 192.168.1.118/32 -j DNAT --to-destination 10.0.22.200
-A neutron-l3-agent-PREROUTING -d 192.168.1.117/32 -j DNAT --to-destination 10.0.22.202
-A neutron-l3-agent-float-snat -s 10.0.22.200/32 -j SNAT --to-source 192.168.1.118
-A neutron-l3-agent-float-snat -s 10.0.22.202/32 -j SNAT --to-source 192.168.1.117
-A neutron-l3-agent-snat -j neutron-l3-agent-float-snat
-A neutron-l3-agent-snat -s 10.0.22.0/24 -j SNAT --to-source 192.168.1.116
-A neutron-postrouting-bottom -j neutron-l3-agent-snat
- SNAT (源地址转换) 负责把从虚机来的traffic的 IP源地址 即fixed ip 10.0.22.200/202 转化为 floating ip 192.168.1.118/117,然后该traffic被路由到 br-ex 再到外网
- DNAT (目的地址转换)负责把从外网来的traffic的 IP目的地址 即floating ip 192.168.1.118/117 转化为虚机所使用的 fixed ip 10.0.22.200/202,然后该traffic被路由到br-int 再到虚机
3. DHCP Server
每一个有DHCP的网络都在Neutron节点上有一个DHCP服务,每个DHCP Server都是一个运行在一个network namespace中的dnsmasq进程。 dnsmasq是一个用在Linux上的轻型DNS和DHCP服务,具体见 http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html.
3.1 每个DHCP在neutron host上都有一个process,其ID是qdhcp-<net id>:
nobody 2049 1 0 06:43 ? 00:00:00 dnsmasq --no-hosts --no-resolv --strict-order --bind-interfaces --interface=tap15865c29-9b --except-interface=lo --pid-file=/var/lib/neutron/dhcp/d24963da-5221-481e-adf5-fe033d6e0b4e/pid --dhcp-hostsfile=/var/lib/neutron/dhcp/d24963da-5221-481e-adf5-fe033d6e0b4e/host --addn-hosts=/var/lib/neutron/dhcp/d24963da-5221-481e-adf5-fe033d6e0b4e/addn_hosts --dhcp-optsfile=/var/lib/neutron/dhcp/d24963da-5221-481e-adf5-fe033d6e0b4e/opts --leasefile-ro --dhcp-range=set:tag0,10.0.22.0,static,86400s --dhcp-lease-max=256 --conf-file= --domain=openstacklocal
说明:
1. --interface=tap15865c29-9b: 该process绑定/监听一个TAP设备,即上图中的 H3
2. --dhcp-hostsfile=/var/lib/neutron/dhcp/d24963da-5221-481e-adf5-fe033d6e0b4e/host:
root@network:/home/s1# cat /var/lib/neutron/dhcp/d24963da-5221-481e-adf5-fe033d6e0b4e/host
fa:16:3e:4d:6b:44,host-10-0-22-201.openstacklocal,10.0.22.201 //本子网DHCP Server自己(M3)的Mac地址以及IP
fa:16:3e:79:07:5e,host-10-0-22-1.openstacklocal,10.0.22.1 //本子网Router Server ( N3) 的Mac地址,名字和 IP
fa:16:3e:bf:69:36,host-10-0-22-200.openstacklocal,10.0.22.200 //本子网虚机1的Mac地址,虚机的主机名字,虚机的fixed IP
fa:16:3e:19:65:62,host-10-0-22-202.openstacklocal,10.0.22.202 //本子网虚机2的Mac地址,虚机的主机名字,虚机的fixed IP
fa:16:3e:88:99:c1,host-10-0-0-116.openstacklocal,10.0.0.116 //子网1的DHCP Server (H1)的Mac地址,以及IP地址。那么这里为什么没H2的相应信息?
在虚机的创建过程中,Neutron会把这些信息(应该是从neutron db中拿到一个可用的IP地址)写到该文件中,这样,当虚机使用Mac地址向DHCP Server查询IP地址的时候,dnsmasq会读取该文件把IP地址返回给它。
3.2 DHCP的interface (省去lo)
root@network:/home/s1# ip netns exec qdhcp-0a4cd030-d951-401a-8202-937b788bea43 ip addr
18: tap6356d532-32: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default
link/ether fa:16:3e:88:99:c1 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.116/24 brd 10.0.0.255 scope global tap6356d532-32
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe88:99c1/64 scope link
valid_lft forever preferred_lft forever
root@network:/home/s1# ip netns exec qdhcp-d04a0a06-7206-4d05-9432-3443843bc199 ip addr
17: tap8dfd0bd8-45: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default
link/ether fa:16:3e:82:fd:26 brd ff:ff:ff:ff:ff:ff
inet 10.0.11.101/24 brd 10.0.11.255 scope global tap8dfd0bd8-45
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe82:fd26/64 scope link
valid_lft forever preferred_lft forever
root@network:/home/s1# ip netns exec qdhcp-d24963da--481e-adf5-fe033d6e0b4e ip addr
: tap15865c29-9b: <BROADCAST,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default
link/ether fa:16:3e:4d:6b:44 brd ff:ff:ff:ff:ff:ff
inet 10.0.22.201/24 brd 10.0.22.255 scope global tap15865c29-9b
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe4d:6b44/ scope link
valid_lft forever preferred_lft forever
DHCP使用fix ip range的第一个可用IP地址做为其IP地址。它的interface的MAC地址 fa:16:3e:4d:6b:44 会出现在br-tun的rules里面。
3.3 虚机向DHCP Server申请/查询Fixed IP
具体步骤在下一篇博文中详细描述。