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