背景
Mininet是由一些虛擬的終端節點(end-hosts)、交換機、路由器連接而成的一個網絡仿真器,它采用輕量級的虛擬化技術使得系統可以和真實網絡相媲美。但是默認的情況下,Mininet是沒有鏈接到網絡,這通常來說是一個好事,隔離了外界網絡的干擾,但是有時我們是需要我們創建的網絡鏈接到網絡中。
環境
- Ubuntu14.04LTS x86_64 in VM
- Internet:DHCP/NAT
過程
在Mininet 2.2 以及更新的版本中,可以使用 --nat 標簽
sudo mn --nat ...
值得注意的是,默認情況下,所有的這些方法將重路由(reroute)來自Mininet服務器或者VM的本地的流量,並將重定向到Mininet的IP子網當中(默認為10.0.0.0/8)。如果正在使用的LAN中相同的網段,則可能會造成斷開鏈接。可以使用--ipbase來更改這個子網。
我們同樣可以通過調用Python API包 Mininet.addNAT() 來達到NAT。
net = Mininet( topo=... )
net.addNAT().configDefault()
net.start()
...
同時你也可以建立NAT拓撲。
class NatTopo( Topo ): def build( self, natIP='10.0.0.254' ): self.hopts = { 'defaultRoute': 'via ' + natIP } hosts = [ self.addHost( h ) for h in 'h1', 'h2' ] s1 = self.addSwitch( 's1' ) for h in hosts: self.addLink( s1, h ) nat1 = self.addNode( 'nat1', cls=NAT, ip=natIP, inNamespace=False ) self.addLink( nat1, s1 )
或者
def Natted( topoClass ): "Return a customized Topo class based on topoClass" class NattedTopo( topoClass ): "Customized topology with attached NAT" def build( self, *args, **kwargs ): """Build topo with NAT attachment natIP: local IP address of NAT node for routing (10.0.0.254) connect: switch to connect (s1)""" self.natIP = kwargs.pop( 'natIP', '10.0.0.254') self.connect = kwargs.pop( 'connect', 's1' ) self.hopts.update( defaultRoute='via ' + self.natIP ) super( NattedTopo, self ).build( *args, **kwargs ) nat1 = self.addNode( 'nat1', cls=NAT, ip=self.natIP, inNamespace=False ) self.addLink( self.connect, nat1 ) return NattedTopo def natted( topoClass, *args, **kwargs ): "Create and invoke natted version of topoClass" topoClass = Natted( topoClass ) return topoClass( *args, **kwargs ) topo = natted( TreeTopo, depth=2, fanout=2 ) net = Mininet( topo=topo )
具體的實現方式可以參考Mininet的Github https://github.com/mininet/mininet/blob/master/examples/nat.py
但是這種的實現方法會造成在Mininet中增加了一個nat0主機以達到鏈接Internet的目的。
另一種實現的方法是將交換機鏈接到eth0端口從而實現鏈接網絡的目的。

Minitnet自定義拓撲:https://github.com/huang362432/Mininet-connect-to-Net/blob/master/topo.py
通過運行py文件建立網絡拓撲
sudo ./topo.py
或者可以通過建立簡單的Mininet自定義拓撲https://github.com/huang362432/Mininet-connect-to-Net/blob/master/mytopo.py,然后向S1交換機添加eth0端口
sudo ovs-vsctl add-port s1 eth0
此時主機已經可以實現DHCP功能,可以自動分配IP,並且每次運行分配的IP不同。但是從Net卻不能鏈接Ubuntu,這時候還需要更改
ifconfig s1 <ip/netmask> ifconfig eth0 0.0.0.0 route add default gw <nat_ip> s1 route del default gw <nat_ip> eth0
目的是把原來處理數據包的eth0改為s1處理數據包,所以相關的route ,ip都得全部重設。
參考http://roan.logdown.com/posts/195601-sdn-lab3-mininet-connect-to-real-internet
