iT邦幫忙

0

Mininet (Rapid Prototyping for Software Defined Networks)-3-simple network topology example

  • 分享至 

  • xImage
  •  

In this tutorial, i will show you how to create a simple network topology including Hub, Router and Switch.

First experimental topology:
https://ithelp.ithome.com.tw/upload/images/20200510/20126989iGW852ExMO.png
in this experiment, you need to type this command to install:

sudo apt-get install bridge utils

Base command:

brctl addbr br0       //add bridge(BRCTL = bridge control)
brctl addif br0 eth0  //Which current interface to add to the bridge (addif = add interface)

Here is my topology.py:

 #!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink

if '__main__' == __name__ :
 net = Mininet(link=TCLink)
 h1 = net.addHost('h1', ip="192.168.10.1/24", mac="00:00:00:00:00:01")
 h2 = net.addHost('h2', ip="192.168.10.2/24", mac="00:00:00:00:00:02")
 h3 = net.addHost('h3', ip="192.168.20.1/24", mac="00:00:00:00:00:03")
 r0 = net.addHost('r0')
 s0 = net.addHost('s0')
 net.addLink(h1,s0)
 net.addLink(h2,s0)
 net.addLink(s0,r0)
 net.addLink(r0,h3)
 net.build()
 r0.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
 r0.cmd('ifconfig r0-eth0 192.168.10.254 netmask 255.255.255.0')
 r0.cmd('ifconfig r0-eth1 192.168.20.254 netmask 255.255.255.0')
 h1.cmd("ip route add default via 192.168.10.254 dev h1-eth0")
 h2.cmd("ip rotue add default via 192.168.10.254 dev h2-eth0")
 h3.cmd("ip route add default via 192.168.20.254 dev h3-eth0")
 s0.cmd("brctl addbr br0")
 s0.cmd("brctl addif br0 s0-eth0")
 s0.cmd("brctl addif br0 s0-eth1")
 s0.cmd("brctl addif br0 s0-eth2")
 s0.cmd("ifconfig br0 up")
 CLI(net)
 net.stop()

echo 1 > /proc/sys/net/ipv4/ip_forward --> mean enable routing function.
ip rotue add default via 192.168.10.254 dev h2-eth0 -->setting default router.

The execution result:

parallels@parallels-vm:~/Desktop/shell$ sudo python topology.py 
mininet> net
h1 h1-eth0:s0-eth0
h2 h2-eth0:s0-eth1
h3 h3-eth0:r0-eth1
r0 r0-eth0:s0-eth2 r0-eth1:h3-eth0
s0 s0-eth0:h1-eth0 s0-eth1:h2-eth0 s0-eth2:r0-eth0
mininet> nodes
available nodes are: 
h1 h2 h3 r0 s0
mininet> links
h1-eth0<->s0-eth0 (OK OK) 
h2-eth0<->s0-eth1 (OK OK) 
s0-eth2<->r0-eth0 (OK OK) 
r0-eth1<->h3-eth0 (OK OK) 

Second experimental topology:
continuation experiment 1, i added h4 which opened hub function.
Here is my topology.py:

#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink

if '__main__' == __name__ :
 net = Mininet(link=TCLink)
 h1 = net.addHost('h1', ip="192.168.10.1/24", mac="00:00:00:00:00:01")
 h2 = net.addHost('h2', ip="192.168.10.2/24", mac="00:00:00:00:00:02")
 h3 = net.addHost('h3', ip="192.168.20.1/24", mac="00:00:00:00:00:03")
 h4 = net.addHost('h4', ip="192.168.10.3/24", mac="00:00:00:00:00:04")
 r0 = net.addHost('r0')
 s0 = net.addHost('s0')
 net.addLink(h1, s0)
 net.addLink(h2, s0)
 net.addLink(s0, r0)
 net.addLink(r0, h3)
 net.addLink(s0, h4)
 net.build()
 r0.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
 r0.cmd('ifconfig r0-eth0 192.168.10.254 netmask 255.255.255.0')
 r0.cmd('ifconfig r0-eth1 192.168.20.254 netmask 255.255.255.0')
 h1.cmd("ip route add default via 192.168.10.254 dev h1-eth0")
 h2.cmd("ip rotue add default via 192.168.10.254 dev h2-eth0")
 h3.cmd("ip route add default via 192.168.20.254 dev h3-eth0")
 h4.cmd("ip route add default via 192.168.10.254 dev h4-eth0")
 s0.cmd("brctl addbr br0")
 s0.cmd("brctl addif br0 s0-eth0")
 s0.cmd("brctl addif br0 s0-eth1")
 s0.cmd("brctl addif br0 s0-eth2")
 s0.cmd("brctl addif br0 s0-eth3")
 s0.cmd("brctl setageing br0 0")
 s0.cmd("ifconfig br0 up")
 CLI(net)
 net.stop()

https://ithelp.ithome.com.tw/upload/images/20200510/20126989c5npkoH0Xj.png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言