主機(jī)雙網(wǎng)卡實(shí)驗(yàn)
實(shí)驗(yàn)?zāi)繕?biāo)
為一臺主機(jī)配置兩張網(wǎng)卡,每張網(wǎng)卡屬于不同的子網(wǎng),使兩個子網(wǎng)的主機(jī)能夠互相ping通。
實(shí)驗(yàn)過程
由于沒有物理環(huán)境,此處使用mininet模擬。
在物理機(jī)上運(yùn)行 ryu-manager 開啟控制器
定義主機(jī)、交換器、控制器:
h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/24' )
h2 = net.addHost( 'h2', mac='00:00:00:00:00:02', ip='10.0.0.2/24' )
h3 = net.addHost( 'h3', mac='00:00:00:00:00:03', ip='20.0.0.1/24' )
s1 = net.addSwitch( 's1', listenPort=6673, mac='00:00:00:00:00:11' )
s2 = net.addSwitch( 's2', listenPort=6674, mac='00:00:00:00:00:12' )
c0 = net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 )
定義 \((s_1,h_1),(s_2,h_3)\) 的連接:
net.addLink(s1, h1, 1, 0)
net.addLink(s2, h3, 1, 0)
將 \(h_2\) 的不同網(wǎng)卡連接到不同的交換機(jī)上:
Link(h2, s1, intfName1='h2-eth0')
Link(h2, s2, intfName1='h2-eth1')
給 h2-eth1 賦 IP
h2.cmd('ifconfig h2-eth1 20.0.0.2 netmask 255.255.255.0')
啟動mininet后,給 \(h_2\) 開啟內(nèi)核路由轉(zhuǎn)發(fā)參數(shù):
h2.cmd('sysctl net.ipv4.ip_forward=1')
給 \(h_1,h_3\) 配置網(wǎng)關(guān):
h1.cmd('route add -net 20.0.0.0 netmask 255.255.255.0 gw 10.0.0.2')
h3.cmd('route add -net 10.0.0.0 netmask 255.255.255.0 gw 20.0.0.2')
啟動mininet后,給交換機(jī)下規(guī)則:
sh ovs-ofctl add-flow s1 in_port=1,actions=output:2
sh ovs-ofctl add-flow s1 in_port=2,actions=output:1
sh ovs-ofctl add-flow s2 in_port=1,actions=output:2
sh ovs-ofctl add-flow s2 in_port=2,actions=output:1
測試一下:
mininet> pingall
*** Ping: testing ping reachability
h1 -> h2 X
h2 -> h1 h3
h3 -> X X
*** Results: 50% dropped (3/6 received)
結(jié)果比較糟糕。
查看此時 \(h_1,h_3\) 的路由表:
mininet> h1 route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 h1-eth0
mininet> h3 route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
20.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 h3-eth0
發(fā)現(xiàn)之前定義的路由表并沒有生效,再次手動添加:
mininet> h1 route add -net 20.0.0.0 netmask 255.255.255.0 gw 10.0.0.2
mininet> h3 route add -net 10.0.0.0 netmask 255.255.255.0 gw 20.0.0.2
再次進(jìn)行測試
mininet> pingall
*** Ping: testing ping reachability
h1 -> h2 h3
h2 -> h1 h3
h3 -> h1 h2
*** Results: 0% dropped (6/6 received)
完美。
附錄
main.py
#!/usr/bin/python
import time
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSKernelSwitch,UserSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import Link, TCLink
def topology():
"Create a network."
net = Mininet( controller=RemoteController, link=TCLink, switch=OVSKernelSwitch )
print "*** Creating nodes ***"
h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/24' )
h2 = net.addHost( 'h2', mac='00:00:00:00:00:02', ip='10.0.0.2/24' )
h3 = net.addHost( 'h3', mac='00:00:00:00:00:03', ip='20.0.0.1/24' )
s1 = net.addSwitch( 's1', listenPort=6673, mac='00:00:00:00:00:11' )
s2 = net.addSwitch( 's2', listenPort=6674, mac='00:00:00:00:00:12' )
c0 = net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 )
print "*** Creating links ***"
net.addLink(s1, h1, 1, 0)
net.addLink(s2, h3, 1, 0)
Link(h2, s1, intfName1='h2-eth0')
Link(h2, s2, intfName1='h2-eth1')
h2.cmd('ifconfig h2-eth1 20.0.0.2 netmask 255.255.255.0')
print "*** Starting network ***"
net.build()
c0.start()
s1.start( [c0] )
s2.start( [c0] )
print "*** Running CLI ***"
CLI( net )
h2.cmd('sysctl net.ipv4.ip_forward=1')
h1.cmd('route add -net 20.0.0.0 netmask 255.255.255.0 gw 10.0.0.2')
h3.cmd('route add -net 10.0.0.0 netmask 255.255.255.0 gw 20.0.0.2')
print "*** Stopping network ***"
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
topology()

浙公網(wǎng)安備 33010602011771號