3 Mininet命令延伸实验拓展 命令行创建拓扑 python 脚本定义拓扑 交互式操作

Mininet自定义拓扑三种实现方式:命令行创建、Python脚本编写、交互式界面创建。

Mininet 是一个轻量级软件定义网络和测试平台;它采用轻量级的虚拟化技术使一个单一的系统看起来像一个完整的网络运行想过的内核系统和用户代码,也可简单理解为 SDN 网络系统中的一种基于进程虚拟化平台,它支持 OpenFlow、OpenvSwith 等各种协议,Mininet 也可以模拟一个完整的网络主机、链接和交换机在同一台计算机上且有助于互动开发、测试和演示,尤其是那些使用 OpenFlow 和 SDN 技术;同时也可将此进程虚拟化的平台下代码迁移到真实的环境中。

1 最小的网络拓扑,一个交换机下挂两个主机。

sudo mn --topo minimal

2 每个交换机连接一个主机,交换机间相连接。本例:4个主机,4个交换机。

sudo mn --topo linear,4

3 每个主机都连接到同一个交换机上。本例:3个主机,一个交换机。

sudo mn --topo single,3

4 定义深度和扇出形成基于树的拓扑。本例:深度2,扇出2。

sudo mn --topo tree,fanout=2,depth=2

启动参数总结

-h, --help        show this help message and exit
--switch=SWITCH        [kernel user ovsk]
--host=HOST        [process]
--controller=CONTROLLER        [nox_dump none ref remote nox_pysw]
--topo=TOPO        [tree reversed single linear minimal],arg1,arg2,...argN
-c, --clean         clean and exit
--custom=CUSTOM        read custom topo and node params from .py file
--test=TEST        [cli build pingall pingpair iperf all iperfudp none]
-x, --xterms        spawn xterms for each node
--mac        set MACs equal to DPIDs
--arp        set all-pairs ARP entries
-v VERBOSITY, --verbosity=VERBOSITY        [info warning critical error debug output]
--ip=IP        [ip address as a dotted decimal string for aremote controller]
--port=PORT        [port integer for a listening remote controller]
--innamespace        sw and ctrl in namespace?
--listenport=LISTENPORT        [base port for passive switch listening controller]
--nolistenport        don't use passive listening port
--pre=PRE        [CLI script to run before tests]
--post=POST          [CLI script to run after tests]

自定义拓扑便捷测试

sudo mn --test pingpair,可以直接对主机连通性进行测试,sudo mn --test iperf 启动后直接进行性能测试。

sudo mn --custom ~/mininet/custom/topo-2sw-2host.py --topo mytopo --test pingall

直接进行性能测试

sudo mn --custom ~/mininet/custom/topo-2sw-2host.py --topo mytopo --test iperf

示例结果

*** Creating network
*** Adding controller
*** Adding hosts:
h1 h2
*** Adding switches:
s3 s4
*** Adding links:
(h1, s3) (s3, s4) (s4, h2)
*** Configuring hosts
h1 h2
*** Starting controller
c0
*** Starting 2 switches
s3 s4 ...
*** Waiting for switches to connect
s3 s4
*** Iperf: testing TCP bandwidth between h1 and h2
*** Results: ['52.5 Gbits/sec', '52.6 Gbits/sec']
*** Stopping 1 controllers
c0
*** Stopping 3 links
...
*** Stopping 2 switches
s3 s4
*** Stopping 2 hosts
h1 h2
*** Done
completed in 10.747 seconds

清理配置

进行清理配置操作,适合故障后恢复,以及清理程序意外退出后虚拟网卡占用。

sudo mn -c

python 脚本定义拓扑

用 python 脚本创建与上述对应拓扑,创建完后的文件需要增加可执行权限chmod +x test.py,还是用sudo python test.py执行。

由于 miniet 支持的是 python2 故注释出现中文,需要在首行添加 # -*- coding: utf8 -*-

对应 --topo linear,4

from mininet.net import Mininet
from mininet.topo import LinearTopo
Linear4 = LinearTopo(k=4)    #四个交换机,分别下挂一个主机
net = Mininet(topo=Linear4)
net.start()
net.pingAll()
net.stop()

对应 --topo single,3

from mininet.net import Mininet
from mininet.topo import SingleSwitchTopo
Single3 = SingleSwitchTopo(k=3)   #一个交换机下挂3个主机
net = Mininet(topo=Single3)
net.start()
net.pingAll()
net.stop()

对应 --topo tree,depth=2,fanout=2

from mininet.net import Mininet
from mininet.topolib import TreeTopo
Tree22 = TreeTopo(depth=2,fanout=2)
net = Mininet(topo=Tree22)
net.start()
net.pingAll()
net.stop()

如果是非上述三种类型的拓扑,那么下面介绍一种适合各种拓扑形式的脚本创建模式。本例:1个交换机,2个主机,并且赋予主机IP地址。

from mininet.net import Mininet
net = Mininet()
# Creating nodes in the network.
c0 = net.addController()
h0 = net.addHost('h0')
s0 = net.addSwitch('s0')
h1 = net.addHost('h1')
# Creating links between nodes in network
net.addLink(h0, s0)
net.addLink(h1, s0)
# Configuration of IP addresses in interfaces
h0.setIP('192.168.1.1', 24)
h1.setIP('192.168.1.2', 24)
net.start()
net.pingAll()
net.stop()

除了可以通过Python脚本创建基本的拓扑以外,还能在此基础上对性能进行限制。观察下面给出的脚本文件,addHost()语法可以对主机cpu进行设置,以百分数的形式;addLink()语法可以设置带宽bw、延迟delay、最大队列的大小max_queue_size、损耗率loss。

from mininet.net import Mininet
from mininet.node import CPULimitedHost
from mininet.link import TCLink
net = Mininet(host=CPULimitedHost, link=TCLink)
c0 = net.addController()
s0 = net.addSwitch('s0')
h0 = net.addHost('h0')
h1 = net.addHost('h1', cpu=0.5)
h2 = net.addHost('h1', cpu=0.5)
net.addLink(s0, h0, bw=10, delay='5ms',
max_queue_size=1000, loss=10, use_htb=True)
net.addLink(s0, h1)
net.addLink(s0, h2)
net.start()
net.pingAll()
net.stop()

展示网络信息

展示函数可关注mininet/util.py中,dump开头的函数。

def dumpNodeConnections( nodes ):
    "Dump connections to/from nodes."
    
def dumpNetConnections( net ):
    "Dump connections in network"
    
def dumpPorts( switches ):
    "dump interface to openflow port mappings for each switch"

demo

import mininet.util

info("Dump connections to/from nodes:
")
mininet.util.dumpNodeConnections(net.hosts)
mininet.util.dumpNodeConnections(net.switches)
mininet.util.dumpNodeConnections(net.controllers)

info("Dump all connections in network:
")
mininet.util.dumpNetConnections(net)

info("Dump interface to openflow port mappings for each switch:
")
mininet.util.dumpPorts(net.switches)

pyton 脚本中调起 CLI

添加代码

from mininet.cli import CLI
CLI(net)

交互式操作

CLI中可以进行原生命令和py单行脚本结合的操作。

sudo mn   进入minite的CLI

查看全部链路信息
mininet> net
h1 h1-eth0:s1-eth1
h2 h2-eth0:s1-eth2
s1 lo:  s1-eth1:h1-eth0 s1-eth2:h2-eth0
c0

查看全部节点
mininet> nodes
available nodes are:
c0 h1 h2 s1

查看各节点的信息
mininet> dump
<Host h1: h1-eth0:10.0.0.1 pid=3371>
<Host h2: h2-eth0:10.0.0.2 pid=3373>
<OVSSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=3378>
<Controller c0: 127.0.0.1:6653 pid=3364>

添加主机h3
mininet> py net.addHost('h3')
<Host h3:  pid=3831>

添加link
mininet> py net.addLink(s1, net.get('h3'))
<mininet.link.Link object at 0x7fb6d7d02390>


mininet> dump
<Host h1: h1-eth0:10.0.0.1 pid=3371>
<Host h2: h2-eth0:10.0.0.2 pid=3373>
<Host h3: h3-eth0:None pid=3831>
<OVSSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None,s1-eth3:None pid=3378>
<Controller c0: 127.0.0.1:6653 pid=3364>


给交换机s1添加端口eth3用于连接h3
mininet> py s1.attach('s1-eth3')

给h3赋予ip 10.0.0.3
mininet> py net.get('h3').cmd('ifconfig h3-eth0 10.3')

h1 ping h3
mininet> h1 ping -c1 10.3
mininet> h1 ping -c1 h3
mininet> h1 ping h3

常用命令总结

help: 默认列出所有命令文档,后面加命令名将介绍该命令用法 dump打印节点信息
gterm: 给定节点上开启gnome-terminal。注:可能导致mn崩溃 xterm给定节点上开启xterm
intfs:列出所有的网络接口
iperf:两个节点之间进行简单的iperf  TCP测试
iperfudp:两个节点之间用制定带宽udp进行测试
net:显示网络链接情况
noecho:运行交互式窗口,关闭回应(echoing)
pingpair:在前两个主机之间互ping测试
source:从外部文件中读入命令
dpctl:在所有交换机上用dptcl执行相关命令,本地为tcp   127.0.0.1:6634
link:禁用或启用两个节点之间的链路
nodes:列出所有的节点信息
pingall:所有host节点之间互ping
py:执行python表达式
sh:运行外部shell命令
quit/exit:退出

参考: