主机访问外网
Vmware+Ubuntu14.04+mininet中的host如何访问外网
1.虚拟机设置
第一步:两个网络适配器,NAT桥接模式
第二步 打开虚拟网络编辑器,设置子网IP
第三步:NAT设置:设置网关IP
第四步 DHCP设置:起始ip和结束ip随便取一个范围
以上配置已经为Ubuntu配置好了网络。
2.启动Ubuntu,查看网卡信息
测试ping通互联网
3.路由
ping -I ens38 baidu.com
4.配置ens38(第二个网卡)
我们把ens38的IP设置为:0.0.0.0,这样这个闲置的网卡资源就可以被用来桥接到mininet网络中的交换机上
查看IP地址时ens38已经看不到IP地址了
5.打开控制器
ryu控制器:
先运行simple_monitor_13.py(ryu/ryu/app)
:ryu-manager simple_monitor_13.py
再运行gui_topology.py(ryu/ryu/app/gui_topology)
:ryu-manager gui_topology.py
floodlight控制器:java -jar target/floodlight.jar
6.编写python脚本创建mininet网络
mininet/mininet/examples:xffTopo.py
#!/usr/bin/python
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
from mininet.node import OVSSwitch, OVSController, Controller, RemoteController
from mininet.topo import Topo
class MyTopo( Topo ):
# "this topo is used for Scheme_1"
def __init__( self ):
"Create custom topo."
# Initialize topology
Topo.__init__( self )
# Add hosts
h1 = self.addHost( 'h1' , ip="10.0.0.1/24", mac="00:00:00:00:00:01", defaultRoute="via 10.0.0.254")
h2 = self.addHost( 'h2' , ip="10.0.0.2/24", mac="00:00:00:00:00:02", defaultRoute="via 10.0.0.254")
h3 = self.addHost( 'h3' , ip="10.0.0.3/24", mac="00:00:00:00:00:03", defaultRoute="via 10.0.0.254")
h4 = self.addHost( 'h4' , ip="10.0.0.4/24", mac="00:00:00:00:00:04", defaultRoute="via 10.0.0.254")
# Add switches
s1 = self.addSwitch( 's1' )
s2 = self.addSwitch( 's2' )
s3 = self.addSwitch( 's3' )
# Add links
self.addLink( s1, s2 )
self.addLink( s1, s3 )
self.addLink( s2, h1 )
self.addLink( s2, h2 )
self.addLink( s3, h3 )
self.addLink( s3, h4 )
# 检查eth1或者其他指定的网卡资源是不是已经被占用
def checkIntf( intf ):
"Make sure intf exists and is not configured."
if ( ' %s:' % intf ) not in quietRun( 'ip link show' ):
error( 'Error:', intf, 'does not exist!\n' )
exit( 1 )
ips = re.findall( r'\d+\.\d+\.\d+\.\d+', quietRun( 'ifconfig ' + intf ) )
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 'ens38'
info( '*** Connecting to hw intf: %s' % intfName )
info( '*** Checking', intfName, '\n' )
checkIntf( intfName )
info( '*** Creating network\n' )
net = Mininet( topo=MyTopo(),controller=None)
#关键函数,创建mininet网络,指定拓扑和控制器。这里的控制器在后面添加进去
switch = net.switches[ 0 ] # 取第一个交换机与eth1桥接
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' )
c0 = RemoteController( 'c0', ip='127.0.0.1', port=6653 )
net.addController(c0)
net.start()
CLI( net )
net.stop()
topos = {'mytopo':(lambda:MyTopo())}
sudo mn --controller=remote --custom ./xffTopo.py --topo mytopo
通过上面的脚本运行后,在floodlight web UI中可以看到创建了如下拓扑
打开h1的独立窗口
ping不同,G。。。
论文学习
搭建拓扑
c0:127.0.0.1:6653
s1:0000000000000001
h1:10.0.0.1
h2:10.0.0.2
h3:10.0.0.3(作为传输媒介与互联网直接相连)
文件保存在:home/mininet/examples:xff_net_topo.mn
ryu控制器
先运行simple_monitor_13.py
zxd@zxd-virtual-machine:~/ryu/ryu/app$ ryu-manager simple_monitor_13.py
新增一台主机连接互联网的python代码
from mininet.cli import CLI
from mininet.log import lg, info
from mininet.topolib import TreeNet
if __name__ == '__main__':
lg.setLogLevel( 'info')
net = TreeNet( depth=1, fanout=4, waitConnected=True )
# Add NAT connectivity
net.addNAT().configDefault()
net.start()
info( "*** Hosts are running and should have internet connectivity\n" )
info( "*** Type 'exit' or control-D to shut down network\n" )
CLI( net )
# Shut down NAT
net.stop()