mininet topology

本文介绍了使用Mininet搭建多种网络拓扑的方法,包括简单的主机路由器拓扑、多接口拓扑、环形拓扑等,并详细展示了配置命令及路由设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pre requisite

All script should enble ip_forward.

echo 1 > /proc/sys/net/ipv4/ip_forward

2 hosts and 4 routers topology

 This code is refered from[2]. From this example, I know the difference between “ip route add” and “route add”. [1]
2h4s.py

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import time
#   0   1   2   3   4
#h1--r1--r2--r3--r4--h2
# 10.0.X.0
def rp_disable(host):
    ifaces = host.cmd('ls /proc/sys/net/ipv4/conf')
    ifacelist = ifaces.split()    # default is to split on whitespace
    for iface in ifacelist:
       if iface != 'lo': host.cmd('sysctl net.ipv4.conf.' + iface + '.rp_filter=0')

net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.0.1')
r1 = net.addHost('r1',ip='10.0.0.2')
r2 = net.addHost('r2',ip='10.0.1.2')
r3 = net.addHost('r3',ip='10.0.2.2')
r4 = net.addHost('r4',ip='10.0.3.2')
h2 = net.addHost('h2',ip='10.0.4.2')
c0 = net.addController( 'c0' )

net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0')
net.addLink(r1,r2,intfName1='r1-eth1',intfName2='r2-eth0')
net.addLink(r2,r3,intfName1='r2-eth1',intfName2='r3-eth0')
net.addLink(r3,r4,intfName1='r3-eth1',intfName2='r4-eth0')
net.addLink(r4,h2,intfName1='r4-eth1',intfName2='h2-eth0')
net.build()

h1.setIP('10.0.0.1', intf='h1-eth0')
h1.cmd("ifconfig h1-eth0 10.0.0.1 netmask 255.255.255.0")
h1.cmd("route add default gw 10.0.0.2 dev h1-eth0")

r1.cmd("ifconfig r1-eth0 10.0.0.2/24")
r1.cmd("ifconfig r1-eth1 10.0.1.1/24")
r1.cmd("ip route add to 10.0.4.0/24 via 10.0.1.2")
r2.cmd("ip route add to 10.0.0.0/24 via 10.0.0.1")
r1.cmd('sysctl net.ipv4.ip_forward=1')
rp_disable(r1)

r2.cmd("ifconfig r2-eth0 10.0.1.2/24")
r2.cmd("ifconfig r2-eth1 10.0.2.1/24")
r2.cmd("ip route add to 10.0.4.0/24 via 10.0.2.2")
r2.cmd("ip route add to 10.0.0.0/24 via 10.0.1.1")
r2.cmd('sysctl net.ipv4.ip_forward=1')
rp_disable(r2)

r3.cmd("ifconfig r3-eth0 10.0.2.2/24")
r3.cmd("ifconfig r3-eth1 10.0.3.1/24")
r3.cmd("ip route add to 10.0.4.0/24 via 10.0.3.2")
r3.cmd("ip route add to 10.0.0.0/24 via 10.0.2.1")
r3.cmd('sysctl net.ipv4.ip_forward=1')
rp_disable(r3)

r4.cmd("ifconfig r4-eth0 10.0.3.2/24")
r4.cmd("ifconfig r4-eth1 10.0.4.1/24")
r4.cmd("ip route add to 10.0.4.0/24 via 10.0.4.2")
r4.cmd("ip route add to 10.0.0.0/24 via 10.0.3.1")
r4.cmd('sysctl net.ipv4.ip_forward=1')
rp_disable(r4)

h2.setIP('10.0.4.2', intf='h2-eth0')
h2.cmd("ifconfig h2-eth0 10.0.4.2 netmask 255.255.255.0")
h2.cmd("route add default gw 10.0.4.1")

net.start()
time.sleep(1)
CLI(net)
net.stop()

[1]ip route 和 route命令的区别
[2]routeline topology
[3]An intruction to computer networks

star topology

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import time
#        h1
#         |1      10.0.x.0
#        r1
#       /2   \3   
#      h2   h3
#
net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.1.1')
h2 = net.addHost('h2',ip='10.0.2.2')
h3 = net.addHost('h3',ip='10.0.3.2')
r1 = net.addHost('r1',ip='10.0.1.2')
net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0')
net.addLink(h2,r1,intfName1='h2-eth0',intfName2='r1-eth1')
net.addLink(h3,r1,intfName1='h3-eth0',intfName2='r1-eth2')
net.build()

h1.cmd("ifconfig h1-eth0 10.0.1.1/24")
h1.cmd("route add default gw 10.0.1.2")

h2.cmd("ifconfig h2-eth0 10.0.2.2/24")
h2.cmd("route add default gw 10.0.2.1")

h3.cmd("ifconfig h3-eth0 10.0.3.2/24")
h3.cmd("route add default gw 10.0.3.1")

r1.cmd("ifconfig r1-eth0 10.0.1.2/24")
r1.cmd("ifconfig r1-eth1 10.0.2.1/24")
r1.cmd("ifconfig r1-eth2 10.0.3.1/24")

r1.cmd("ip route add to 10.0.1.0/24 via 10.0.1.1")
r1.cmd("ip route add to 10.0.2.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.3.0/24 via 10.0.3.2")
r1.cmd('sysctl net.ipv4.ip_forward=1')
net.start()
time.sleep(1)
CLI(net)
net.stop()

mininet multi-interface ring topology

 This trivial cost me nearly one afternoon and a night.
 Thanks this blog[1].
 The tips to use:

1 sudo su
2 python 4h.py
3 xterm h1 h3
4 ping X.X.X.X

4h.py

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import time
##https://serverfault.com/questions/417885/configure-gateway-for-two-nics-through-static-routeing
#    ____h2____
#   /          \
# h1           h3
#   \___h4_____/
#  
net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.1.1')
h2 = net.addHost('h2',ip='10.0.1.2')
h3 = net.addHost('h3',ip='10.0.2.2')
h4 = net.addHost('h4',ip='10.0.3.2')
c0 = net.addController( 'c0' )
net.addLink(h1,h2,intfName1='h1-eth0',intfName2='h2-eth0')
net.addLink(h2,h3,intfName1='h2-eth1',intfName2='h3-eth0')
net.addLink(h1,h4,intfName1='h1-eth1',intfName2='h4-eth0')
net.addLink(h4,h3,intfName1='h4-eth1',intfName2='h3-eth1')
net.build()
h1.setIP('10.0.1.1', intf='h1-eth0')
h1.cmd("ifconfig h1-eth0 10.0.1.1 netmask 255.255.255.0")

h1.setIP('10.0.3.1', intf='h1-eth1')
h1.cmd("ifconfig h1-eth1 10.0.3.1 netmask 255.255.255.0")

h1.cmd("ip route flush all proto static scope global")
h1.cmd("ip route add 10.0.1.1/24 dev h1-eth0 table 5000")
h1.cmd("ip route add default via 10.0.1.2 dev h1-eth0 table 5000")

h1.cmd("ip route add 10.0.3.1/24 dev h1-eth1 table 5001")
h1.cmd("ip route add default via 10.0.3.2 dev h1-eth1 table 5001")
h1.cmd("ip rule add from 10.0.1.1 table 5000")
h1.cmd("ip rule add from 10.0.3.1 table 5001")
h1.cmd("route add default gw 10.0.1.2  dev h1-eth0")

h2.setIP('10.0.1.2', intf='h2-eth0')
h2.setIP('10.0.2.1', intf='h2-eth1')
h2.cmd("ifconfig h2-eth0 10.0.1.2/24")
h2.cmd("ifconfig h2-eth1 10.0.2.1/24")
h2.cmd("ip route add 10.0.2.0/24 via 10.0.2.2")
h2.cmd("ip route add 10.0.1.0/24 via 10.0.1.1")
h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")


h4.setIP('10.0.3.2', intf='h4-eth0')
h4.setIP('10.0.4.1', intf='h4-eth1')
h4.cmd("ifconfig h4-eth0 10.0.3.2/24")
h4.cmd("ifconfig h4-eth1 10.0.4.1/24")
h4.cmd("ip route add 10.0.4.0  dev h4-eth1") #via 10.0.4.2
h4.cmd("ip route add 10.0.3.0 via 10.0.3.1")

h4.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")


h3.setIP('10.0.2.2', intf='h3-eth0')
h3.cmd("ifconfig h3-eth0 10.0.2.2 netmask 255.255.255.0")
h3.setIP('10.0.4.2', intf='h3-eth1')
h3.cmd("ifconfig h3-eth1 10.0.4.2 netmask 255.255.255.0")

h3.cmd("ip route flush all proto static scope global")
h3.cmd("ip route add 10.0.2.2/24 dev h3-eth0 table 5000")
h3.cmd("ip route add default via 10.0.2.1 dev h3-eth0 table 5000")

h3.cmd("ip route add 10.0.4.2/24 dev h3-eth1 table 5001")
h3.cmd("ip route add default via 10.0.4.1 dev h3-eth1 table 5001")
h3.cmd("ip rule add from 10.0.2.2 table 5000")
h3.cmd("ip rule add from 10.0.4.2 table 5001")

net.start()
time.sleep(1)
CLI(net)
net.stop()

这里写图片描述
[1]Configure gateway for two NICs through static routeing

3 hosts and 4 routers

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import time
#   0   1   2   3   4
#h1--r1--r2--r3--r4--h2
#             \5
#              h3
# 10.0.X.0
def rp_disable(host):
    ifaces = host.cmd('ls /proc/sys/net/ipv4/conf')
    ifacelist = ifaces.split()    # default is to split on whitespace
    for iface in ifacelist:
       if iface != 'lo': host.cmd('sysctl net.ipv4.conf.' + iface + '.rp_filter=0')

net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.0.1')
r1 = net.addHost('r1',ip='10.0.0.2')
r2 = net.addHost('r2',ip='10.0.1.2')
r3 = net.addHost('r3',ip='10.0.2.2')
r4 = net.addHost('r4',ip='10.0.3.2')
h2 = net.addHost('h2',ip='10.0.4.2')
h3 = net.addHost('h3',ip='10.0.5.2')
c0 = net.addController( 'c0' )

net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0')
net.addLink(r1,r2,intfName1='r1-eth1',intfName2='r2-eth0')
net.addLink(r2,r3,intfName1='r2-eth1',intfName2='r3-eth0')
net.addLink(r3,r4,intfName1='r3-eth1',intfName2='r4-eth0')
net.addLink(r4,h2,intfName1='r4-eth1',intfName2='h2-eth0')
net.addLink(r3,h3,intfName1='r3-eth2',intfName2='h3-eth0')
net.build()

h1.setIP('10.0.0.1', intf='h1-eth0')
h1.cmd("ifconfig h1-eth0 10.0.0.1 netmask 255.255.255.0")
h1.cmd("route add default gw 10.0.0.2")

h3.cmd("ifconfig h3-eth0 10.0.5.2/24")
h3.cmd("route add default gw 10.0.5.1")

r1.cmd("ifconfig r1-eth0 10.0.0.2/24")
r1.cmd("ifconfig r1-eth1 10.0.1.1/24")
r1.cmd("ip route add to 10.0.4.0/24 via 10.0.1.2")
r1.cmd("ip route add to 10.0.0.0/24 via 10.0.0.1")
r1.cmd("ip route add to 10.0.5.0/24 via 10.0.1.2")
r1.cmd('sysctl net.ipv4.ip_forward=1')
#rp_disable(r1)

r2.cmd("ifconfig r2-eth0 10.0.1.2/24")
r2.cmd("ifconfig r2-eth1 10.0.2.1/24")
r2.cmd("ip route add to 10.0.4.0/24 via 10.0.2.2")
r2.cmd("ip route add to 10.0.0.0/24 via 10.0.1.1")
r2.cmd("ip route add to 10.0.5.0/24 via 10.0.2.2")
r2.cmd('sysctl net.ipv4.ip_forward=1')
#rp_disable(r2)

r3.cmd("ifconfig r3-eth0 10.0.2.2/24")
r3.cmd("ifconfig r3-eth1 10.0.3.1/24")
r3.cmd("ifconfig r3-eth2 10.0.5.1/24")
r3.cmd("ip route add to 10.0.4.0/24 via 10.0.3.2")
r3.cmd("ip route add to 10.0.0.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.5.0/24 via 10.0.5.2")
r3.cmd('sysctl net.ipv4.ip_forward=1')
#rp_disable(r3)

r4.cmd("ifconfig r4-eth0 10.0.3.2/24")
r4.cmd("ifconfig r4-eth1 10.0.4.1/24")
r4.cmd("ip route add to 10.0.4.0/24 via 10.0.4.2")
r4.cmd("ip route add to 10.0.0.0/24 via 10.0.3.1")
r4.cmd("ip route add to 10.0.5.0/24 via 10.0.3.1")
r4.cmd('sysctl net.ipv4.ip_forward=1')
#rp_disable(r4)

h2.cmd("ifconfig h2-eth0 10.0.4.2/24")
h2.cmd("route add default gw 10.0.4.1")

net.start()
time.sleep(1)
CLI(net)
net.stop()

##mininet multi interface topology

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.link import TCLink
import time
#    ___r1____
#   /          \0  1
# h1            r3---h2
#  \           /2
#   ---r2-----
max_queue_size = 20  
net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.1.1')
r1 = net.addHost('r1',ip='10.0.1.2')
r2 = net.addHost('r2',ip='10.0.3.2')
r3 = net.addHost('r3',ip='10.0.5.1')
h2 = net.addHost('h2',ip='10.0.5.2')
c0 = net.addController('c0')
net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0',cls=TCLink , bw=2, delay='20ms', max_queue_size=max_queue_size)
net.addLink(r1,r3,intfName1='r1-eth1',intfName2='r3-eth0',cls=TCLink , bw=2, delay='20ms', max_queue_size=max_queue_size)
net.addLink(r3,h2,intfName1='r3-eth1',intfName2='h2-eth0',cls=TCLink , bw=10, delay='10ms', max_queue_size=max_queue_size)
net.addLink(h1,r2,intfName1='h1-eth1',intfName2='r2-eth0',cls=TCLink , bw=2, delay='20ms', max_queue_size=max_queue_size)
net.addLink(r2,r3,intfName1='r2-eth1',intfName2='r3-eth2',cls=TCLink , bw=2, delay='20ms', max_queue_size=max_queue_size)

net.build()

h1.cmd("ifconfig h1-eth0 10.0.1.1/24")
h1.cmd("ifconfig h1-eth1 10.0.3.1/24")
h1.cmd("ip route flush all proto static scope global")
h1.cmd("ip route add 10.0.1.1/24 dev h1-eth0 table 5000")
h1.cmd("ip route add default via 10.0.1.2 dev h1-eth0 table 5000")

h1.cmd("ip route add 10.0.3.1/24 dev h1-eth1 table 5001")
h1.cmd("ip route add default via 10.0.3.2 dev h1-eth1 table 5001")
h1.cmd("ip rule add from 10.0.1.1 table 5000")
h1.cmd("ip rule add from 10.0.3.1 table 5001")
h1.cmd("ip route add default gw 10.0.1.2  dev h1-eth0")
#that be a must or else a tcp client would not know how to route packet out
#h1.cmd("route add default gw 10.0.1.2  dev h1-eth0") would not work for the second part when a tcp client bind a address


r1.cmd("ifconfig r1-eth0 10.0.1.2/24")
r1.cmd("ifconfig r1-eth1 10.0.2.1/24")
r1.cmd("ip route add to 10.0.1.0/24 via 10.0.1.1")
r1.cmd("ip route add to 10.0.2.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.5.0/24 via 10.0.2.2")
r1.cmd('sysctl net.ipv4.ip_forward=1')

r3.cmd("ifconfig r3-eth0 10.0.2.2/24")
r3.cmd("ifconfig r3-eth1 10.0.5.1/24")
r3.cmd("ifconfig r3-eth2 10.0.4.2/24")
r3.cmd("ip route add to 10.0.1.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.2.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.5.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.4.0/24 via 10.0.4.1")
r3.cmd("ip route add to 10.0.3.0/24 via 10.0.4.1")
r3.cmd('sysctl net.ipv4.ip_forward=1')

r2.cmd("ifconfig r2-eth0 10.0.3.2/24")
r2.cmd("ifconfig r2-eth1 10.0.4.1/24")
r2.cmd("ip route add to 10.0.3.0/24 via 10.0.3.1")
r2.cmd("ip route add to 10.0.4.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.5.0/24 via 10.0.4.2")
r3.cmd('sysctl net.ipv4.ip_forward=1')

h2.cmd("ifconfig h2-eth0 10.0.5.2/24")
h2.cmd("route add default gw 10.0.5.1")

net.start()
time.sleep(1)
CLI(net)
net.stop()

 h1.cmd(“route add default gw 10.0.1.2 dev h1-eth0”),this can make ping 10.0.5.2 works ok. If this line is commented out, ping 10.0.5.2 will not work but “ping -I 10.0.1.1 10.0.5.2” works normally.
##ring topology
r4 routes packet from 10.0.1.1 to r5 and packets with source ip 10.0.3.1 to r6. This topology was created for multipath protocol test.
ringTopology.py

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.cli import CLI
import time
import subprocess
import os,signal
import sys
#                  5.0   6.0     7.0
#    ___r1____           ____r5____
#   /          \0 1   0 /1         \  10.0
# h1            r3-----r4            r7---h2
#  \           /2       \____r6____/
#   ---r2-----           8.0    9.0
max_queue_size = 200  
net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.1.1')
r1 = net.addHost('r1',ip='10.0.1.2')
r2 = net.addHost('r2',ip='10.0.3.2')
r3 = net.addHost('r3',ip='10.0.5.1')

r4 = net.addHost('r4',ip='10.0.5.2')
r5 = net.addHost('r5',ip='10.0.6.2')
r6 = net.addHost('r6',ip='10.0.8.2')
r7 = net.addHost('r7',ip='10.0.10.1')
h2 = net.addHost('h2',ip='10.0.10.2')
c0 = net.addController('c0')
net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0',cls=TCLink , bw=500, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r1,r3,intfName1='r1-eth1',intfName2='r3-eth0',cls=TCLink , bw=500, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(h1,r2,intfName1='h1-eth1',intfName2='r2-eth0',cls=TCLink , bw=500, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r2,r3,intfName1='r2-eth1',intfName2='r3-eth2',cls=TCLink , bw=500, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r3,r4,intfName1='r3-eth1',intfName2='r4-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=max_queue_size)
net.addLink(r4,r5,intfName1='r4-eth1',intfName2='r5-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r5,r7,intfName1='r5-eth1',intfName2='r7-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r7,h2,intfName1='r7-eth1',intfName2='h2-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r4,r6,intfName1='r4-eth2',intfName2='r6-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r6,r7,intfName1='r6-eth1',intfName2='r7-eth2',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.build()

h1.cmd("ifconfig h1-eth0 10.0.1.1/24")
h1.cmd("ifconfig h1-eth1 10.0.3.1/24")
h1.cmd("ip route flush all proto static scope global")
h1.cmd("ip route add 10.0.1.1/24 dev h1-eth0 table 5000")
h1.cmd("ip route add default via 10.0.1.2 dev h1-eth0 table 5000")

h1.cmd("ip route add 10.0.3.1/24 dev h1-eth1 table 5001")
h1.cmd("ip route add default via 10.0.3.2 dev h1-eth1 table 5001")
h1.cmd("ip rule add from 10.0.1.1 table 5000")
h1.cmd("ip rule add from 10.0.3.1 table 5001")
h1.cmd("ip route add default gw 10.0.1.2  dev h1-eth0")
#that be a must or else a tcp client would not know how to route packet out
h1.cmd("route add default gw 10.0.1.2  dev h1-eth0") #would not work for the second part when a tcp client bind a address


r1.cmd("ifconfig r1-eth0 10.0.1.2/24")
r1.cmd("ifconfig r1-eth1 10.0.2.1/24")
r1.cmd("ip route add to 10.0.1.0/24 via 10.0.1.1")
r1.cmd("ip route add to 10.0.2.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.5.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.6.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.7.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.8.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.9.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.10.0/24 via 10.0.2.2")
r1.cmd('sysctl net.ipv4.ip_forward=1')

r3.cmd("ifconfig r3-eth0 10.0.2.2/24")
r3.cmd("ifconfig r3-eth1 10.0.5.1/24")
r3.cmd("ifconfig r3-eth2 10.0.4.2/24")
r3.cmd("ip route add to 10.0.1.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.2.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.5.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.4.0/24 via 10.0.4.1")
r3.cmd("ip route add to 10.0.3.0/24 via 10.0.4.1")
r3.cmd("ip route add to 10.0.6.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.7.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.8.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.9.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.10.0/24 via 10.0.5.2")
r3.cmd('sysctl net.ipv4.ip_forward=1')

r2.cmd("ifconfig r2-eth0 10.0.3.2/24")
r2.cmd("ifconfig r2-eth1 10.0.4.1/24")
r2.cmd("ip route add to 10.0.3.0/24 via 10.0.3.1")
r2.cmd("ip route add to 10.0.4.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.5.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.6.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.7.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.8.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.9.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.10.0/24 via 10.0.4.2")
r2.cmd('sysctl net.ipv4.ip_forward=1')

r4.cmd("ifconfig r4-eth0 10.0.5.2/24")
r4.cmd("ifconfig r4-eth1 10.0.6.1/24")
r4.cmd("ifconfig r4-eth2 10.0.8.1/24")
r4.cmd("ip route add to 10.0.1.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.2.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.3.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.4.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.5.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.6.0/24 via 10.0.6.2")
r4.cmd("ip route add to 10.0.7.0/24 via 10.0.6.2")
r4.cmd("ip route add to 10.0.8.0/24 via 10.0.8.2")
r4.cmd("ip route add to 10.0.9.0/24 via 10.0.8.2")

r4.cmd("ip route add 10.0.6.1/24 dev r4-eth1 table 5000")
r4.cmd("ip route add default via 10.0.6.2 dev r4-eth1 table 5000")
r4.cmd("ip route add 10.0.8.1/24 dev r4-eth2 table 5001")
r4.cmd("ip route add default via 10.0.8.2 dev r4-eth2 table 5001")
r4.cmd("ip rule add from 10.0.1.1 table 5000")
r4.cmd("ip rule add from 10.0.3.1 table 5001")
r4.cmd('sysctl net.ipv4.ip_forward=1')

r5.cmd("ifconfig r5-eth0 10.0.6.2/24")
r5.cmd("ifconfig r5-eth1 10.0.7.1/24")
r5.cmd("ip route add to 10.0.1.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.2.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.3.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.4.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.5.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.6.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.7.0/24 via 10.0.7.2")
r5.cmd("ip route add to 10.0.10.0/24 via 10.0.7.2")
r5.cmd('sysctl net.ipv4.ip_forward=1')

r6.cmd("ifconfig r6-eth0 10.0.8.2/24")
r6.cmd("ifconfig r6-eth1 10.0.9.1/24")
r6.cmd("ip route add to 10.0.1.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.2.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.3.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.4.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.5.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.8.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.9.0/24 via 10.0.9.2")
r6.cmd("ip route add to 10.0.10.0/24 via 10.0.9.2")
r6.cmd('sysctl net.ipv4.ip_forward=1')

r7.cmd("ifconfig r7-eth0 10.0.7.2/24")
r7.cmd("ifconfig r7-eth1 10.0.10.1/24")
r7.cmd("ifconfig r7-eth2 10.0.9.2/24")
r7.cmd('sysctl net.ipv4.ip_forward=1')
r7.cmd("ip route add to 10.0.1.0/24 via 10.0.7.1")
r7.cmd("ip route add to 10.0.2.0/24 via 10.0.7.1")
r7.cmd("ip route add to 10.0.5.0/24 via 10.0.7.1")
r7.cmd("ip route add to 10.0.6.0/24 via 10.0.7.1")
r7.cmd("ip route add to 10.0.7.0/24 via 10.0.7.1")

r7.cmd("ip route add to 10.0.3.0/24 via 10.0.9.1")
r7.cmd("ip route add to 10.0.4.0/24 via 10.0.9.1")
r7.cmd("ip route add to 10.0.8.0/24 via 10.0.9.1")
r7.cmd("ip route add to 10.0.9.0/24 via 10.0.9.1")
r7.cmd("ip route add to 10.0.10.0/24 via 10.0.10.2")
r7.cmd('sysctl net.ipv4.ip_forward=1')

h2.cmd("ifconfig h2-eth0 10.0.10.2/24")
h2.cmd("route add default gw 10.0.10.1")

net.start()

time.sleep(1)
CLI(net)
net.stop()

dumbbell

# CMU 18731 HW2
# Code referenced from:git@bitbucket.org:huangty/cs144_bufferbloat.git
# Edited by: Deepti Sunder Prakash
# https://github.com/dhruvityagi/netsec/blob/master/dumbbell.py
#!/usr/bin/python

from mininet.topo import Topo
from mininet.node import CPULimitedHost
from mininet.link import TCLink
from mininet.net import Mininet
from mininet.log import lg, info
from mininet.util import dumpNodeConnections
from mininet.cli import CLI

from subprocess import Popen, PIPE
from time import sleep, time
from multiprocessing import Process
from argparse import ArgumentParser
import time
import sys
import os


class DumbbellTopo(Topo):
    "Dumbbell topology for Shrew experiment"
    def build(self, n=6, bw_net=100, delay='20ms', bw_host=10):
        #TODO:Add your code to create the topology.
        #Add 2 switches
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')
        self.addLink(s1, s2,bw=bw_net, delay=delay)

        #Left Side
        a1 = self.addHost('a1')
        hl1 = self.addHost('hl1')
        hl2 = self.addHost('hl2')
        # 10 Mbps, 20ms delay
        self.addLink(hl1, s1, bw=bw_host, delay=delay)
        self.addLink(hl2, s1, bw=bw_host, delay=delay)
        self.addLink(a1, s1, bw=bw_host, delay=delay)
        
        #Right Side
        a2 = self.addHost('a2')
        hr1 = self.addHost('hr1')
        hr2 = self.addHost('hr2')
        # 10 Mbps, 20ms delay
        self.addLink(hr1, s2, bw=bw_host, delay=delay)
        self.addLink(hr2, s2, bw=bw_host, delay=delay)
        self.addLink(a2, s2, bw=bw_host, delay=delay)
def myiperf(net, **kwargs):
    """
    Command to start a transfer between src and dst.
    :param kwargs: named arguments
        src: name of the source node.
        dst: name of the destination node.
        protocol: tcp or udp (default tcp).
        duration: duration of the transfert in seconds (default 10s).
        bw: for udp, bandwidth to send at in bits/sec (default 1 Mbit/sec)
    """
    kwargs.setdefault('protocol', 'TCP')
    kwargs.setdefault('duration', 5)
    kwargs.setdefault('bw', 100000)
    info('***iperf event at t={time}: {args}\n'.format(time=time.time(), args=kwargs))
    
    if not os.path.exists("output"):
        os.makedirs("output")
    server_output = "output/iperf-{protocol}-server-{src}-{dst}.txt".format(**kwargs)
    client_output = "output/iperf-{protocol}-client-{src}-{dst}.txt".format(**kwargs)
    
    client, server = net.get(kwargs['src'], kwargs['dst'])
    iperf_server_cmd = ''
    iperf_client_cmd = ''
    if kwargs['protocol'].upper() == 'UDP':
         iperf_server_cmd = 'iperf -u -s -i 1'
         iperf_client_cmd = 'iperf -u -t {duration} -c {server_ip} -b {bw}'.format(server_ip=server.IP(), **kwargs)


    elif kwargs['protocol'].upper() == 'TCP':
         iperf_server_cmd = 'iperf -s -i 1'
         iperf_client_cmd = 'iperf -t {duration} -c {server_ip}'.format(server_ip=server.IP(), **kwargs)
    else :
        raise Exception( 'Unexpected protocol:{protocol}'.format(**kwargs))

    server.sendCmd('{cmd} &>{output} &'.format(cmd=iperf_server_cmd, output=server_output))
    info('iperf server command: {cmd} -s -i 1 &>{output} &\n'.format(cmd=iperf_server_cmd,
                                                                            output=server_output))
    # This is a patch to allow sendingCmd while iperf is running in background.CONS: we can not know when
    # iperf finishes and get their output
    server.waiting = False

    if kwargs['protocol'].lower() == 'tcp':
        while 'Connected' not in client.cmd(
                        'sh -c "echo A | telnet -e A %s 5001"' % server.IP()):
            info('Waiting for iperf to start up...\n')
            sleep(.5)

    info('iperf client command: {cmd} &>{output} &\n'.format(
    cmd = iperf_client_cmd, output=client_output))
    client.sendCmd('{cmd} &>{output} &'.format(
    cmd = iperf_client_cmd, output=client_output))
    # This is a patch to allow sendingCmd while iperf is running in background.CONS: we can not know when
    # iperf finishes and get their output
    client.waiting = False
def bbnet():
    "Create network and run shrew  experiment"
    print "starting mininet ...."
    topo = DumbbellTopo()
    net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink,
              autoPinCpus=True)
    net.start()
    #dumpNodeConnections(net.hosts)

    #TODO:Add your code to test reachability of hosts.
    #print "Testing network connectivity"
    #net.pingAll()
    
    #TODO:Add your code to start long lived TCP flows between
    #hosts on the left and right.
    #print "Starting long lived tcp connection between hl1 and hr1, hl2 and hr2"
    #hl1, hl2, hr1, hr2, a1 = net.get('hl1','hl2','hr1','hr2', 'a1')
    #hl1_IP = hl1.IP()
    #hr1_IP = hl2.IP()
    #a1_IP = a1.IP()
    #print a1_IP,hl1_IP, hr1_IP
## the way to use kwargs
## https://blog.csdn.net/u010852680/article/details/77848570
    kwargs={'src':'hl1','dst':'hr1'}
    myiperf(net,**kwargs)
    time.sleep(10)
    #CLI(net)
    
    net.stop()

if __name__ == '__main__':
    bbnet()

topology zoo

https://github.com/cotyb/LISA
https://github.com/fnss/fnss/tree/master/fnss/topologies

### Mininet 使用教程 Mininet 是一种用于创建虚拟网络环境的工具,支持开发者在单机环境中模拟复杂的网络拓扑结构。以下是关于如何使用 Mininet 的一些基本指导。 #### 安装 Mininet 如果尚未克隆 Mininet 代码库,则可以通过以下命令获取最新版本的源码[^1]: ```bash git clone https://github.com/mininet/mininet ``` 对于已经拥有 Linux 系统并希望快速安装 Mininet 的用户来说,在不同的 Ubuntu 版本下可以选择对应的安装方式[^2][^3]: - **Ubuntu 14.10**: ```bash sudo apt-get update && sudo apt-get install mininet ``` - **Ubuntu 14.04**: ```bash sudo apt-get update && sudo apt-get install mininet ``` - **Ubuntu 12.04 (Precise)**: ```bash sudo apt-get update && sudo apt-get install mininet/precise-backports ``` 这些命令会自动下载并安装所需的依赖项以及 Mininet 软件包本身。 #### 启动 Mininet 并测试简单拓扑 一旦成功安装了 Mininet,就可以通过 `mn` 命令启动默认的最小化网络拓扑[^4]: ```bash sudo mn ``` 这将在终端中打开一个交互式的 Mininet CLI 工具,默认情况下它会创建两个主机节点 (`h1`, `h2`) 和一个交换机连接它们。此时可以在该界面执行各种命令来验证连通性和性能指标,比如 ping 测试: ```bash mininet> h1 ping h2 PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data. 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.798 ms ... ``` 另外值得注意的是自 version 2.2.0 开始引入了一个图形化编辑器叫做 MiniEdit ,允许更方便地构建复杂场景而无需手动编写脚本文件。要调用此功能只需键入如下指令即可进入 GUI 模式设计自己的定制化方案: ```bash sudo mn --topo single,3 --mac --switch ovsk --controller remote --custom /path/to/custom/topology/file.py ``` 或者直接运行独立的应用程序入口点: ```bash sudo wireshark & sudo miniedit ``` 以上就是有关 Mininet 初步使用的介绍内容概览。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值