-
Notifications
You must be signed in to change notification settings - Fork 5
/
start_mininet.py
executable file
·108 lines (89 loc) · 3.31 KB
/
start_mininet.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python
import sys
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.log import lg
from mininet.node import CPULimitedHost
from mininet.link import TCLink
from mininet.util import irange, custom, quietRun, dumpNetConnections
from mininet.cli import CLI
from time import sleep, time
from subprocess import Popen, PIPE
import subprocess
import argparse
import os
parser = argparse.ArgumentParser(description="Mininet portion of pyrouter")
# no arguments needed as yet :-)
args = parser.parse_args()
lg.setLogLevel('info')
class PyRouterTopo(Topo):
def __init__(self, args):
# Add default members to class.
super(PyRouterTopo, self).__init__()
# Host and link configuration
#
#
# server1
# \
# router----client
# /
# server2
#
nodeconfig = {'cpu':-1}
self.addHost('server1', **nodeconfig)
self.addHost('server2', **nodeconfig)
self.addHost('router', **nodeconfig)
self.addHost('client', **nodeconfig)
linkconfig = {
'bw': 10,
'delay': 0.010,
'loss': 0.0
}
for node in ['server1','server2','client']:
self.addLink(node, 'router', **linkconfig)
def set_ip_pair(net, node1, node2, ip1, ip2):
node1 = net.get(node1)
ilist = node1.connectionsTo(net.get(node2)) # returns list of tuples
intf = ilist[0]
intf[0].setIP(ip1)
intf[1].setIP(ip2)
def reset_macs(net, node, macbase):
ifnum = 1
node_object = net.get(node)
for intf in node_object.intfList():
node_object.setMAC(macbase.format(ifnum), intf)
ifnum += 1
for intf in node_object.intfList():
print node,intf,node_object.MAC(intf)
def set_route(net, fromnode, prefix, gw):
node_object = net.get(fromnode)
node_object.cmdPrint("route add -net {} gw {}".format(prefix, gw))
def setup_addressing(net):
reset_macs(net, 'server1', '10:00:00:00:00:{:02x}')
reset_macs(net, 'server2', '20:00:00:00:00:{:02x}')
reset_macs(net, 'client', '30:00:00:00:00:{:02x}')
reset_macs(net, 'router', '40:00:00:00:00:{:02x}')
set_ip_pair(net, 'server1','router','192.168.100.1/30','192.168.100.2/30')
set_ip_pair(net, 'server2','router','192.168.200.1/30','192.168.200.2/30')
set_ip_pair(net, 'client','router','10.1.1.1/30','10.1.1.2/30')
set_route(net, 'server1', '10.1.0.0/16', '192.168.100.2')
set_route(net, 'server1', '192.168.200.0/24', '192.168.100.2')
set_route(net, 'server2', '10.1.0.0/16', '192.168.200.2')
set_route(net, 'server2', '192.168.100.0/24', '192.168.200.2')
set_route(net, 'client', '192.168.100.0/24', '10.1.1.2')
set_route(net, 'client', '192.168.200.0/24', '10.1.1.2')
set_route(net, 'client', '172.16.0.0/16', '10.1.1.2')
forwarding_table = open('forwarding_table.txt', 'w')
table = '''192.168.100.0 255.255.255.0 192.168.100.1 router-eth0
192.168.200.0 255.255.255.0 192.168.200.1 router-eth1
10.1.0.0 255.255.0.0 10.1.1.1 router-eth2
'''
forwarding_table.write(table)
forwarding_table.close()
def main():
topo = PyRouterTopo(args)
net = Mininet(topo=topo, link=TCLink, cleanup=True, controller=None)
setup_addressing(net)
net.interact()
if __name__ == '__main__':
main()