Part 1: 基础
- $表明Linux普通环境
- mininet>表示MIninet环境
- #表明LinuxRoot环境
启动参数
$ sudo mn -h //显示启动mininet时的参数
结果如下:
-h, –help
show this help message and exit
–switch=SWITCH
default|ivs|lxbr|ovs|ovsbr|ovsk|user[,param=value...] ovs=OVSSwitch default=OVSSwitch ovsk=OVSSwitch lxbr=LinuxBridge user=UserSwitch ivs=IVSSwitch ovsbr=OVSBridge
–host=HOST
cfs|proc|rt[,param=value...] rt=CPULimitedHost{'sched': 'rt'} proc=Host cfs=CPULimitedHost{'sched': 'cfs'}
–controller=CONTROLLER
default|none|nox|ovsc|ref|remote|ryu[,param=value...] ovsc=OVSController none=NullController remote=RemoteController default=DefaultController nox=NOX ryu=Ryu ref=Controller
–link=LINK
default|ovs|tc|tcu[,param=value...] default=Link ovs=OVSLink tcu=TCULink tc=TCLink
–topo=TOPO
linear|minimal|reversed|single|torus|tree[,param=value ...] linear=LinearTopo torus=TorusTopo tree=TreeTopo single=SingleSwitchTopo reversed=SingleSwitchReversedTopo minimal=MinimalTopo
-c, –clean
clean and exit
–custom=CUSTOM
read custom classes or params from .py file(s)
–test=TESTS
cli|build|pingall|pingpair|iperf|all|iperfudp|none|pin gpair|iperfudp|pingall|iperfUDP
-x, –xterms
spawn xterms for each node
-i IPBASE, –ipbase=IPBASE
base IP address for hosts
–mac
automatically set host MACs
–arp
set all-pairs ARP entries
-v VERBOSITY, –verbosity=VERBOSITY
info|warning|critical|error|debug|output
–innamespace
sw and ctrl in namespace?
–listenport=LISTENPORT
base port for passive switch listening
–nolistenport
don't use passive listening port
–pre=PRE
CLI script to run before tests
–post=POST
CLI script to run after tests
–pin
pin hosts to CPU cores (requires --host cfs or --host rt)
–nat
[option=val...] adds a NAT to the topology that connects Mininet hosts to the physical network. Warning: This may route any traffic on the machine that uses Mininet's IP subnet into the Mininet network. If you need to change Mininet's IP subnet, see the --ipbase option.
–version
prints the version and exits
–cluster=server1,server2…
run on multiple servers (experimental!)
–placement=block|random
node placement for --cluster (experimental!)
启动wireshark
$ wireshark &
Mininet虚拟机内置的Wireshark可以解析openflow协议的数据包。在wireshark的过滤文本框中输入“of”并应用,即可只显示openflow的数据包。抓包接口选择“lo”,并开始抓包。
与主机、交换机交互
$ sudo mn
默认启动拓扑为minimal的网络:主机h1和h2,交换机s1,控制器。这四项各为一个进程,其中控制器可以是远程的,通过controller参数设置。
Commands | Meanings |
---|---|
mininet>help | 显示mininet里的命令 |
miminet>nodes | 显示所有节点 |
mininet>net | 显示所有链接 |
mininet>dump | 显示所有节点内信息 |
当mininet中的第一个参数是节点时,表示该命令是在该节点上运行,例如:
mininet>h1 ifconfig
测试连通性
mininet>h1 ping -c 3 h2 or mininet>pingall
关闭mininet
$ sudo mn -c
退出mininet并清理内存。
Part 2:高级启动选项
改变topo大小和类型
$ sudo mn --topo linear,4 //线性topo,4个主机
链接多样化
$ sudo mn --link tc,bw=10,delay=10ms //每条链接时延为10ms
调整冗余度
$ sudo mn -v debug //冗余级别为debug
自定义拓扑结构
#topo-2sw-2host.py
from mininet.topo import Topo
class MyTopo( Topo ):
"Simple topology example."
def __init__( self ):
"Create custom topo."
# Initialize topology
Topo.__init__( self )
# Add hosts and switches
leftHost = self.addHost( 'h1' )
rightHost = self.addHost( 'h2' )
leftSwitch = self.addSwitch( 's3' )
rightSwitch = self.addSwitch( 's4' )
# Add links
self.addLink( leftHost, leftSwitch )
self.addLink( leftSwitch, rightSwitch )
self.addLink( rightSwitch, rightHost )
topos = { 'mytopo': ( lambda: MyTopo() ) }
$ sudo mn --custom topo-2sw-2host.py --topo mytopo --test pingall
命名空间
默认情况下,主机各自在自己的命名空间,而控制器和交换机则在root空间。将交换机放在自己的命名空间,可以用–innamespace
$ sudo mn --innamespace --switch user
Mininet命令行命令
$ mininet> help
Python解释器
如果以py开头,则会用python执行。
mininet>py locals() #显示可获取的局部变量
mininet>py dir(s1) #显示一个节点的方法和属性
mininet>py help(h1) #获取一个基点的方法的在线文档(按q退出)
mininet>py h1.IP() #求变量的值
显示xterm
mininet>xterm h1,s1
python API
构建topo
API | description |
---|---|
Topo | Mininet 拓扑结构的基类 |
.build(n) | 脱坡类中需要重写的函数 |
.addSwitch(string) | 向拓扑结构中添加交换价 |
.addHost(String) | 向拓扑结构中添加主机 |
.addLink(string1,string2) | 添加链接 |
Mininet | 创建和管理网络的主类 |
.start() | 启动网络 |
.pingAll() | 测试网络连通性 |
.stop() | 终止网络 |
.hosts | 网络中的所有节点 |
dumpNodeConnections() | 显示一组节点的connection |
setLogLevel | 显示显示级别,’info’、’debug’、’output’等,推荐’info’ |
设置性能参数
API | description |
---|---|
addHost(name,cpu=f) | 添加占用f的CPU资源的主机 |
addLink(node1,node2,bw,delay,max_queue_size,loss) | 在node1和node2间添加带宽、时延、队列长度和丢包率的链接 |
net.get(name) | 通过名字获取节点 |
在主机中运行程序
h1 = net.get('h1')
result = h1.cmd('ifconfig')
print result
主机配置方法
API | description |
---|---|
.IP() | 返回IP地址 |
.MAC() | 返回MAC地址 |
.setARP() | 向主机的ARP缓存中添加静态ARP项 |
.setIP() | 设置主机IP |
.setMAC() | 设置主机MAC() |
以上方法中,若不声明具体接口,则使用主机默认接口。
CLI接口
CLI即可可以显示窗口,运行各种命令。
from mininet.topo import SingleSwitchTopo
from mininet.net import Mininet
from mininet.cli import CLI
net = Mininet(SingleSwitchTopo(2))
net.start()
CLI(net)
net.stop()
自定义文件
sudo mn --custom mytopo.py --topo mytopo,3
需要在mytopo.py文件中定义字典,且必须满足如下要求:
option | dict name | key: value |
---|---|---|
–topo | topos | ‘short name’: Topo constructor |
–switch | switches | ‘short name’: Switch constructor |
–host | hosts | ‘short name’: Host constructor |
–controller | controllers | ‘short name’: Controller constructor |
–link | links | ‘short name’: Link constructor |
–test | test | ‘short name’: test function to call with Mininet object |