SDN | Mininet主机与真实网络互通方案实现
背景:
由于Mininet创建的仿真环境的主机都是虚拟节点,默认情况Mininet是没有链接到网络,但是由于存在客户端与Mininet主机通信或者Mininet主机和控制器通信的需求,因此需要提供一种方法将我们创建的Mininet网络链接到网络中。
实验环境:
实验是在Windows系统上,搭建了Mininet虚拟机的环境下进行的,其中:
(1)Windows主机的IP地址为:192.168.205.101
(2)Mininet虚拟机网络环境配置为,虚拟机需配置两张网卡,都通过NAT桥接:
(3)本实验是在Mininet官方示例hwintf.py基础上进行实现,示例代码如下,同时也可以访问https://github.com/SkyLee109/PaperSDN/blob/master/example/hwintf.py来获取。
#!/usr/bin/python """ This example shows how to add an interface (for example a real hardware interface) to a network after the network is created. """ import re import sys from mininet.cli import CLI from mininet.log import setLogLevel, info, error from mininet.net import Mininet from mininet.link import Intf from mininet.topolib import TreeTopo from mininet.util import quietRun def checkIntf( intf ): "Make sure intf exists and is not configured." config = quietRun( 'ifconfig %s 2>/dev/null' % intf, shell=True ) if not config: error( 'Error:', intf, 'does not exist!\n' ) exit( 1 ) ips = re.findall( r'\d+\.\d+\.\d+\.\d+', config ) if ips: error( 'Error:', intf, 'has an IP address,' 'and is probably in use!\n' ) exit( 1 ) if __name__ == '__main__': setLogLevel( 'info' ) # try to get hw intf from the command line; by default, use eth1 intfName = sys.argv[ 1 ] if len( sys.argv ) > 1 else 'eth1' info( '*** Connecting to hw intf: %s' % intfName ) info( '*** Checking', intfName, '\n' ) checkIntf( intfName ) info( '*** Creating network\n' ) net = Mininet( topo=TreeTopo( depth=1, fanout=2 ) ) switch = net.switches[ 0 ] info( '*** Adding hardware interface', intfName, 'to switch', switch.name, '\n' ) _intf = Intf( intfName, node=switch ) info( '*** Note: you may need to reconfigure the interfaces for ' 'the Mininet hosts:\n', net.hosts, '\n' ) net.start() CLI( net ) net.stop()
实验过程:
(1)首先对eth1网卡进行如下设置:
sudo ifconfig eth1 0.0.0.0
此时eth1网卡不再绑定任何IP,否则无法与Mininet虚拟交换机进行绑定。
(2)执行hwintf.py文件,命令如下:
sudo python hwintf.py
执行效果如下所示:
如图所示,此时h1的主机地址为10.0.0.1,为Mininet虚拟局域网地址,无法和宿主机以及外网进行通信,因此需要进一步配置。
(3)以h1为例进行如下配置:
1) h1 ifconfig h1-eth0 192.168.237.142 netmask 255.255.255.0
2) h1 route add default gw 192.168.237.2
此时再次执行h1 ifconfig,并测试h1与Windows主机连通性以及是否可以访问外网,效果如下:
结论:
本文只是在官方示例基础上进行了实验,更加复杂的拓扑环境以及网络配置还需要进一步研究。实验有诸多不足,欢迎批评指正。
您阅读这篇文章共花了: