暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Mininet搭建Openflow网络环境

Simble的小站 2018-11-20
620

Mininet是什么

Mininet是一个网络模拟器,可以创建虚拟主机,交换机,控制器和连接的网络。

Mininet的交换机支持OpenFlow,可实现高度灵活的自定义路由和SDN,为开发和测试SDN提供实验环境

Mininet用途

  • 为开发OpenFlow应用程序提供简单而廉价的网络测试平台
  • 允许多个并发开发人员在同一拓扑上独立工作
  • 支持系统级回归测试,这些测试可重复且易于打包
  • 支持复杂的拓扑测试,无需连接物理网络
  • 包括具有拓扑感知和OpenFlow感知的CLI,用于调试或运行网络范围的测试
  • 支持任意自定义拓扑,并包括一组基本的参数化拓扑
  • 可以在没有编程的情况下开箱
  • 提供了一个简单易用的Python API,用于网络创建和实验

安装

Ubuntu16.04

1
复制
apt install mininet
复制

测试

1
复制
sudo mn --test pingall
复制

使用

常用命令

  • 进入mininet命令行模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
复制
root@mininet:~# mn
*** No default OpenFlow controller found for default switch!
*** Falling back to OVS Bridge
*** Creating network
*** Adding controller
*** Adding hosts:
h1 h2
*** Adding switches:
s1
*** Adding links:
(h1, s1) (h2, s1)
*** Configuring hosts
h1 h2
*** Starting controller

*** Starting 1 switches
s1 ...
*** Starting CLI:
mininet>
复制
  • 查看节点
1
2
3
4
复制
mininet> nodes
available nodes are:
h1 h2 s1
mininet>
复制

可以看到当前包含3个节点,包括两个host,一个switch

  • 查看连接状况
1
2
3
4
5
复制
mininet> net
h1 h1-eth0:s1-eth1
h2 h2-eth0:s1-eth2
s1 lo:  s1-eth1:h1-eth0 s1-eth2:h2-eth0
mininet>
复制

h1的eth0与s1的eth1相连
h2的eth0与s1的eth2相连

  • 查看详细信息
1
2
3
4
5
复制
mininet> dump
<Host h1: h1-eth0:10.0.0.1 pid=6896>
<Host h2: h2-eth0:10.0.0.2 pid=6899>
<OVSBridge s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=6905>
mininet>
复制
  • 环境清理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
复制
# mn -c
*** Removing excess controllers/ofprotocols/ofdatapaths/pings/noxes
killall controller ofprotocol ofdatapath ping nox_core lt-nox_core ovs-openflowd ovs-controller udpbwtest mnexec ivs 2> dev/null
killall -9 controller ofprotocol ofdatapath ping nox_core lt-nox_core ovs-openflowd ovs-controller udpbwtest mnexec ivs 2> /dev/null
pkill -9 -f "sudo mnexec"
*** Removing junk from /tmp
rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log
*** Removing old X11 tunnels
*** Removing excess kernel datapaths
ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'
***  Removing OVS datapaths
ovs-vsctl --timeout=1 list-br
ovs-vsctl --timeout=1 list-br
*** Removing all links of the pattern foo-ethX
ip link show | egrep -o '([-_.[:alnum:]]+-eth[[:digit:]]+)'
ip link show
*** Killing stale mininet node processes
pkill -9 -f mininet:
*** Shutting down stale tunnels
pkill -9 -f Tunnel=Ethernet
pkill -9 -f .ssh/mn
rm -f ~/.ssh/mn/*
*** Cleanup complete.
root@mininet:~#
复制
  • 节点命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
复制
mininet> h1 ifconfig
h1-eth0   Link encap:Ethernet  HWaddr a2:5f:da:ed:6a:74
         inet addr:10.0.0.1  Bcast:10.255.255.255  Mask:255.0.0.0
         inet6 addr: fe80::a05f:daff:feed:6a74/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:15 errors:0 dropped:0 overruns:0 frame:0
         TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000
         RX bytes:1206 (1.2 KB)  TX bytes:648 (648.0 B)

lo        Link encap:Local Loopback
         inet addr:127.0.0.1  Mask:255.0.0.0
         inet6 addr: ::1/128 Scope:Host
         UP LOOPBACK RUNNING  MTU:65536  Metric:1
         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1
         RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

mininet>
复制
  • Run http server
1
复制
mininet> h1 python -m SimpleHTTPServer 80 &
复制
  • Http client
1
2
3
4
5
6
7
8
9
10
11
12
复制
mininet> h2 wget h1
--2018-11-07 10:52:29--  http://10.0.0.1/
Connecting to 10.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 370 [text/html]
Saving to: 'index.html'

index.html          100%[===================>]     370  --.-KB/s    in 0s

2018-11-07 10:52:29 (62.3 MB/s) - 'index.html' saved [370/370]

mininet>
复制

拓扑

mininet自带topo

通过mn -h
可以看到Mininet自带的几种topo类型,分别有Linear, minimal, reversed, single, torus和tree类型,但有时候这些类型无法满足需求,需要自定义topo

1
2
3
4
5
6
7
8
9
10
复制
$ mn --help
Usage: mn [options]
(type mn -h for details)

...

 --topo=TOPO           linear|minimal|reversed|single|torus|tree[,param=value
                       ...] linear=LinearTopo torus=TorusTopo tree=TreeTopo
                       single=SingleSwitchTopo
                       reversed=SingleSwitchReversedTopo minimal=MinimalTopo
复制

获取示例

Mininet提供了topo-2sw-2host的示例,可以通过Mininet github的custom目录下获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
复制
"""Custom topology example
Two directly connected switches plus a host for each switch:
  host --- switch --- switch --- host
Adding the 'topos' dict with a key/value pair to generate our newly defined
topology enables one to pass in '--topo=mytopo' from the command line.
"""

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() ) }
复制

自定义topo

如要创建如下topo

1
2
3
4
5
6
7
8
9
10
11
复制
                         +------------+
             +----------+     s3     +---------+
             |          +------------+         |
             |                                 |
        +----+----+                       +----+----+
  +-----+    s1   +-----+           +-----+   s2    +-----+
  |     +---------+     |           |     +---------+     |
  |                     |           |                     |
+--+--+               +--+--+     +--+--+               +--+--+
| h1  |               | h2  |     | h3  |               | h4  |
+-----+               +-----+     +-----+               +-----+
复制

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
复制
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
       h1 = self.addHost( 'h1' )
       h2 = self.addHost( 'h2' )
       h3 = self.addHost( 'h3' )
       h4 = self.addHost( 'h4' )
       s1 = self.addSwitch( 's1' )
       s2 = self.addSwitch( 's2' )
       s3 = self.addSwitch( 's3' )

       # Add links
       self.addLink( h1, s1 )
       self.addLink( s1, h2 )
       self.addLink( s1, s3 )
       self.addLink( s3, s2 )
       self.addLink( h3, s2 )
       self.addLink( s2, h4 )


topos = { 'mytopo': ( lambda: MyTopo() ) }
复制

使用自定义topo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
复制
root@mininet:~# mn --custom ./testtopo.py --topo mytopo --controller=remote,ip=127.0.0.1,port=6633
*** Creating network
*** Adding controller
*** Adding hosts:
h1 h2 h3 h4
*** Adding switches:
s1 s2 s3
*** Adding links:
(h1, s1) (h3, s2) (s1, h2) (s1, s3) (s2, h4) (s3, s2)
*** Configuring hosts
h1 h2 h3 h4
*** Starting controller
c0
*** Starting 3 switches
s1 s2 s3 ...
*** Starting CLI:
mininet>
复制

查看连接状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
复制
mininet> links
h1-eth0<->s1-eth1 (OK OK)
h3-eth0<->s2-eth2 (OK OK)
s1-eth2<->h2-eth0 (OK OK)
s1-eth3<->s3-eth1 (OK OK)
s2-eth3<->h4-eth0 (OK OK)
s3-eth2<->s2-eth1 (OK OK)
mininet> pingall
*** Ping: testing ping reachability
h1 -> h2 h3 h4
h2 -> h1 h3 h4
h3 -> h1 h2 h4
h4 -> h1 h2 h3
*** Results: 0% dropped (12/12 received)
mininet>
复制


文章转载自Simble的小站,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论