Open vSwith模擬網(wǎng)關(guān)實(shí)現(xiàn)不同子網(wǎng)的互通
一.實(shí)驗(yàn)?zāi)康?/strong>
本實(shí)驗(yàn)通過(guò)Mininet構(gòu)建子網(wǎng),并使得OVS作為網(wǎng)關(guān),來(lái)模擬子網(wǎng)間的互通。在實(shí)驗(yàn)的過(guò)程中,我們來(lái)學(xué)習(xí)一下內(nèi)容:OVS構(gòu)建子網(wǎng)過(guò)程。
OVS設(shè)置網(wǎng)關(guān)過(guò)程。
OVS配置流表過(guò)程。
二.實(shí)驗(yàn)準(zhǔn)備
實(shí)驗(yàn)環(huán)境我們使用Mininet進(jìn)行構(gòu)建,建議到Mininet官方下載***的Mininet虛擬機(jī),本實(shí)驗(yàn)中虛擬機(jī)版本是mininet-2.2.1-150420-ubuntu-14.04-server-amd64,或者參考官方文檔中介紹的Native Installation from Source進(jìn)行安裝。
實(shí)驗(yàn)拓?fù)鋱D如下:
三.實(shí)驗(yàn)步驟
1.構(gòu)建網(wǎng)絡(luò)拓?fù)洹?/strong>
我們的目標(biāo)是要讓兩個(gè)不同子網(wǎng)的主機(jī)能相互通信,可以先構(gòu)建出兩個(gè)主機(jī),然后給主機(jī)設(shè)置不同子網(wǎng)。由于Mininet虛擬的主機(jī)默認(rèn)屬于10.0.0.0/24,需要對(duì)主機(jī)網(wǎng)絡(luò)進(jìn)行設(shè)置。
說(shuō)明:
$> 表示Linux命令行的輸入,權(quán)限為root。
mininet> 表示Mininet命令行模式。
創(chuàng)建拓?fù)?/p>
$> mn --topo single,2 --mac
說(shuō)明:參數(shù)--mac是為了創(chuàng)建的host有更簡(jiǎn)單的MAC地址,為后面流表創(chuàng)建提供方便。
查詢(xún)狀態(tài)
mininet> dump
可以看出,默認(rèn)的h1和h2是在同一網(wǎng)段,而且可以相互ping通。
mininet> h1 ping h2 PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data. 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=1.96 ms 64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.412 ms
修改網(wǎng)絡(luò)設(shè)置
mininet> h1 ifconfig h1-eth0 10.0.0.1/24 netmask 255.0.0.0 mininet> h2 ifconfig h2-eth0 20.0.0.1/24 netmask 255.0.0.0
測(cè)試網(wǎng)絡(luò)狀態(tài)如下:
mininet> h1 ping h2 connect: Network is unreachable
由于通過(guò)配置,h1和h2已經(jīng)不在同一網(wǎng)段,當(dāng)h1 ping h2時(shí),包會(huì)轉(zhuǎn)發(fā)到子網(wǎng)的網(wǎng)關(guān)來(lái)處理,但是由于當(dāng)前環(huán)境中沒(méi)有針對(duì)兩個(gè)網(wǎng)段設(shè)置網(wǎng)關(guān),最終狀態(tài)為不可達(dá)。此時(shí)我們可以為主機(jī)設(shè)置網(wǎng)關(guān)。
設(shè)置網(wǎng)關(guān)
mininet> h1 route add default gw 10.0.0.254 h1-eth0 mininet> h2 route add default gw 20.0.0.254 h2-eth0
測(cè)試網(wǎng)絡(luò)狀態(tài)如下:
mininet> h1 ping h2 PING 20.0.0.1 (20.0.0.1) 56(84) bytes of data. From 10.0.0.1 icmp_seq=1 Destination Host Unreachable From 10.0.0.1 icmp_seq=2 Destination Host Unreachable
#p#
為了更直觀(guān)的觀(guān)察當(dāng)前網(wǎng)絡(luò)狀態(tài),在新的Linux窗口使用tcpdump來(lái)查看h1 ping h2過(guò)程中包的信息。如下:
$>tcpdump -vvv -i s1-eth1 tcpdump: WARNING: s1-eth1: no IPv4 address assigned tcpdump: listening on s1-eth1, link-type EN10MB (Ethernet), capture size 65535 bytes 05:38:06.917012 ARP, Ethernet (len 6), IPv4 (len 4), Request who-has 10.0.0.254 tell 10.0.0.1, length 28 05:38:07.917134 ARP, Ethernet (len 6), IPv4 (len 4), Request who-has 10.0.0.254 tell 10.0.0.1, length 28
當(dāng)h1-eth0發(fā)送ARP包請(qǐng)求s1-eth1網(wǎng)關(guān),而當(dāng)前環(huán)境中沒(méi)有網(wǎng)關(guān)設(shè)備,OVS無(wú)法進(jìn)行轉(zhuǎn)發(fā);另外,如果OVS連接了控制器,如OpenDaylight,這可以利用它預(yù)設(shè)的ARP Proxy,讓控制器來(lái)提供目標(biāo)地址信息。因此ICMP封包前,由于獲取不到目標(biāo)地址信息,而導(dǎo)致網(wǎng)絡(luò)不可達(dá)。
2. 設(shè)置OVS為網(wǎng)關(guān)
如果實(shí)驗(yàn)連接了控制器,這可以通過(guò)控制器獲得目標(biāo)地址的信息,這部分可以單獨(dú)通過(guò)另外的實(shí)驗(yàn)進(jìn)行驗(yàn)證。由于試驗(yàn)中沒(méi)有任何控制器,因此需要對(duì)OVS進(jìn)行配置,使得其具有g(shù)ateway的功能,能夠?qū)RP進(jìn)行回復(fù)。
mininet> s1 ifconfig s1:0 10.0.0.254 mininet> s1 ifconfig s1:1 20.0.0.254
測(cè)試網(wǎng)絡(luò)狀態(tài),發(fā)現(xiàn)h1與網(wǎng)關(guān)可以ping通了。
mininet> h1 ping 10.0.0.254 PING 10.0.0.254 (10.0.0.254) 56(84) bytes of data. 64 bytes from 10.0.0.254: icmp_seq=1 ttl=64 time=3.35 ms 64 bytes from 10.0.0.254: icmp_seq=2 ttl=64 time=0.669 ms
從tcpdump窗口觀(guān)察到的信息也顯示相關(guān)信息如下:
10.0.0.1 > 10.0.0.254: ICMP echo request, id 4284, seq 3, length 64 06:01:43.104097 IP (tos 0x0, ttl 64, id 3241, offset 0, flags [none], proto ICMP (1), length 84) 10.0.0.254 > 10.0.0.1: ICMP echo reply, id 4284, seq 3, length 64 06:01:46.114482 ARP, Ethernet (len 6), IPv4 (len 4), Request who-has 10.0.0.1 tell 10.0.0.254, length 28
但是,此時(shí)h1 ping h2還是不會(huì)通的。因?yàn)楫?dāng)前轉(zhuǎn)發(fā)平面沒(méi)有任何流對(duì)發(fā)過(guò)來(lái)的ICMP包做轉(zhuǎn)發(fā)。因此,我們需要添加一些流,使得整個(gè)網(wǎng)絡(luò)最終運(yùn)作起來(lái)。
3. 配置流表
處理ARP請(qǐng)求
當(dāng)網(wǎng)管的ARP流到來(lái)后,將其交給本地的OVS處理。
mininet> sh ovs-ofctl add-flow s1 "table=0,priority=65535,arp,arp_tpa=10.0.0.254 actions=LOCAL" mininet> sh ovs-ofctl add-flow s1 "table=0,priority=65535,arp,arp_tpa=20.0.0.254 actions=LOCAL"
處理ARP應(yīng)答
該應(yīng)答回復(fù)目標(biāo)地址的出口,比如將目標(biāo)網(wǎng)絡(luò)10.0.0.1的包通過(guò)出口端口1抓發(fā)出去。端口信息可以查詢(xún)?nèi)缦拢?/p>
mininet> sh ovs-vsctl -- --columns=name,ofport list Interface name : "s1-eth2" ofport : 2 name : "s1-eth1" ofport : 1 name : "s1" ofport : 65534
處理應(yīng)答的流表如下:
mininet> sh ovs-ofctl add-flow s1 "table=0,priority=1,arp,nw_dst=10.0.0.1,actions=output:1" mininet> sh ovs-ofctl add-flow s1 "table=0,priority=1,arp,nw_dst=20.0.0.1,actions=output:2"
處理了應(yīng)答包后,實(shí)際上網(wǎng)絡(luò)還是不能通。到目前為止,僅對(duì)ARP的包做了相應(yīng)的處理,而ICMP的包為處理。
10.0.0.1 > 20.0.0.1: ICMP echo request, id 5004, seq 1, length 64 06:28:06.932565 IP (tos 0x0, ttl 64, id 52645, offset 0, flags [DF], proto ICMP (1), length 84) 10.0.0.1 > 20.0.0.1: ICMP echo request, id 5004, seq 2, length 64 06:28:07.932469 IP (tos 0x0, ttl 64, id 52646, offset 0, flags [DF], proto ICMP (1), length 84)
ICMP應(yīng)答處理
接下來(lái)需要對(duì)ICMP的請(qǐng)求進(jìn)行處理。為了使得流表表達(dá)更清晰,我們將ICMP路由的處理放在另外一個(gè)table處理。 也就是在table(1)中設(shè)置一個(gè)***優(yōu)先級(jí)的流,將非ARP的包丟給下一個(gè)流表處理。
mininet> sh ovs-ofctl add-flow s1 "table=0,priority=0,actions=resubmit(,1)"
在table(1)中,OVS的角色有點(diǎn)像router,我們需要修改ICMP封包的目標(biāo)MAC地址。
mininet> sh ovs-ofctl add-flow s1 "table=1,icmp,nw_dst=10.0.0.1,actions=mod_dl_dst=00:00:00:00:00:01,output:1" mininet> sh ovs-ofctl add-flow s1 "table=1,icmp,nw_dst=20.0.0.1,actions=mod_dl_dst=00:00:00:00:00:02,output:2"
***,執(zhí)行h1 ping h2,兩個(gè)子網(wǎng)的主機(jī)能順利ping通。
mininet> h1 ping h2 PING 20.0.0.1 (20.0.0.1) 56(84) bytes of data. 64 bytes from 20.0.0.1: icmp_seq=1 ttl=64 time=1.29 ms 64 bytes from 20.0.0.1: icmp_seq=2 ttl=64 time=0.080 ms 64 bytes from 20.0.0.1: icmp_seq=3 ttl=64 time=0.089 ms