-
Notifications
You must be signed in to change notification settings - Fork 1
/
start_mininet.py
89 lines (71 loc) · 2.52 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
#!/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
#
# external----firewall----internal
#
# external refers to the internet outside a given
# enterprise's network. internal refers to the
# enterprise's network.
self.addHost('external')
self.addHost('internal')
self.addHost('firewall')
for node in ['internal','external']:
self.addLink(node, 'firewall', bw=1000, delay="10ms")
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 set_ip(net, node, ifname, addr):
node_object = net.get(node)
intf = node_object.intf(ifname)
intf.setIP(addr)
def reset_macs(net, node, macbase):
ifnum = 1
node_object = net.get(node)
for intf in node_object.intfList():
if node not in str(intf):
continue # don't set lo or other interfaces
node_object.setMAC(macbase.format(ifnum), intf)
ifnum += 1
for intf in node_object.intfList():
print node,intf,node_object.MAC(intf)
def set_def_route(net, fromnode, gw):
node_object = net.get(fromnode)
node_object.cmdPrint("route add default gw {}".format(gw))
def setup_addressing(net):
reset_macs(net, 'internal', '00:00:00:00:01:{:02x}')
reset_macs(net, 'external', '00:00:00:00:10:{:02x}')
reset_macs(net, 'firewall', '00:00:00:00:0b:{:02x}')
set_ip(net,'internal','internal-eth0','192.168.0.1/24')
set_ip(net,'external','external-eth0','192.168.0.2/24')
def main():
topo = PyRouterTopo(args)
net = Mininet(topo=topo, link=TCLink, cleanup=True, autoSetMacs=True, controller=None)
setup_addressing(net)
net.staticArp()
net.interact()
if __name__ == '__main__':
main()