/
main.py
111 lines (90 loc) · 3.38 KB
/
main.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
109
110
111
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.topo import Topo
from mininet.log import info
from p4env.controller import P4Controller
from itertools import groupby
import os
import json
import time
from p4env.p4switch import P4Switch
from cmd import Cmd
class NetworkTopo(Topo):
def __init__(self, topo_file, **opts):
Topo.__init__(self, **opts)
with open(topo_file, 'r') as f:
topo = json.load(f)
self.topo = topo
for sw, params in topo['switches'].items():
p4info_path = os.path.dirname(os.path.abspath(topo_file)) + '/' + params['p4info']
pipeline_path = os.path.dirname(os.path.abspath(topo_file)) + '/' + params['pipeline']
self.addSwitch(sw, cls=P4Switch, sw_path=params['exe'], p4info=p4info_path,
pipeline=pipeline_path, table_entries=params['table_entries'])
for h, params in topo['hosts'].items():
self.addHost(h, ip=params['ip'], mac=params['mac'])
for l in topo['links']:
self.addLink(l[0], l[1])
class MininetRunner:
def __init__(self, topo_file):
topo = NetworkTopo(topo_file)
self.net = Mininet(topo=topo, controller=None)
self.net.addController(controller=P4Controller, switches=self.net.switches)
self.start()
time.sleep(1)
""" Execute any commands provided in the topology.json file on each Mininet host
"""
for host_name, host_info in topo.topo['hosts'].items():
h = self.net.get(host_name)
if "commands" in host_info:
for cmd in host_info["commands"]:
h.cmd(cmd)
self.net.staticArp()
CLI(self.net)
# stop right after the CLI is exited
self.net.stop()
def start(self):
"Start controller and switches."
if not self.net.built:
self.net.build()
info('*** Starting %s switches\n' % len(self.net.switches))
for switch in self.net.switches:
info(switch.name + ' ')
switch.start(self.net.controllers)
started = {}
for swclass, switches in groupby(
sorted(self.net.switches,
key=lambda s: str(type(s))), type):
switches = tuple(switches)
if hasattr(swclass, 'batchStartup'):
success = swclass.batchStartup(switches)
started.update({s: s for s in success})
info('\n')
info('*** Starting controller\n')
for controller in self.net.controllers:
info(controller.name + ' ')
controller.start()
info('\n')
if self.net.waitConn:
self.net.waitConnected()
class CmdLine(Cmd):
intro = 'Welcome to the PDP-env shell. Type help or ? to list commands.\n'
prompt = 'PDP-env> '
def __init__(self):
Cmd.__init__(self)
def do_start_network(self, line):
if line == '':
print 'please input topo file path'
return
if not os.path.exists(line):
print 'cannot find topo file'
return
MininetRunner(line)
def do_clean_mininet(self, line):
os.system('sudo mn -c')
def do_exit(self, line):
return True
if __name__ == '__main__':
setLogLevel("info")
c = CmdLine()
c.cmdloop()