-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsdn.py
More file actions
151 lines (121 loc) · 4.72 KB
/
sdn.py
File metadata and controls
151 lines (121 loc) · 4.72 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from mininet.node import RemoteController, Host, OVSSwitch
from mininet.link import TCLink, Link, TCIntf
from random import random
from datetime import datetime as dt
import subprocess
class SdnIntf(TCIntf):
def __init__(self, name, node=None, port=None, link=None,
mac=None, **params):
super(SdnIntf, self).__init__(name, node=node, port=port, link=link, mac=mac, **params)
self._running = True
def up(self):
if not self._running:
self.ifconfig("up")
self._running = True
def down(self):
if self._running:
self.ifconfig("down")
self._running = False
class SdnLink(TCLink):
def __init__(self, node1, node2, port1=None, port2=None,
intfName1=None, intfName2=None, **params):
Link.__init__(self, node1, node2, port1=port1, port2=port2,
intfName1=intfName1, intfName2=intfName2,
cls1=SdnIntf,
cls2=SdnIntf,
params1=params,
params2=params)
self._connected = True
def reconnect(self):
if not self._connected:
self.intf1.up()
self.intf2.up()
print "Reconnect Link {} - {}".format(self.intf1.node.name, self.intf2.node.name)
self._connected = True
def disconnect(self):
if self._connected:
self.intf1.down()
self.intf2.down()
print "Disconnect Link {} - {}".format(self.intf1.node.name, self.intf2.node.name)
self._connected = False
class SdnHost(Host):
def __init__(self, name, inNamespace=True, schedule=None, reliability=1.0, **params):
super(SdnHost, self).__init__(name, inNamespace=inNamespace, **params)
self.reliability = reliability
self.schedule = schedule
self._online = True
self._processes = []
self._services = []
self.cmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1")
self.cmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1")
def _simulate_reliability(self):
r = random()
if self.reliability > r:
self.boot()
else:
self.shut_down()
def simulate_online(self):
if self.schedule is not None:
on_schedule = self.schedule(dt.now())
if on_schedule and not self._online:
self.boot()
elif not on_schedule and self._online:
self.shut_down()
elif on_schedule and self._online:
self._simulate_reliability()
else: # not on_schedule and offline
pass
else:
self._simulate_reliability()
def boot(self):
if not self._online:
for interface in self.intfList():
interface.up()
self.start_services()
print "Boot Host {}".format(self.name)
self._online = True
def shut_down(self):
if self._online:
for interface in self.intfList():
interface.down()
self.stop_services()
print "Shut down Host {}".format(self.name)
self._online = False
def start_services(self):
for cmd in self._services:
self._processes.append((cmd, self.popen(cmd.split())))
print "POPEN: {} running '{}'".format(self.name, cmd)
def stop_services(self):
for cmd, proc in self._processes:
proc.terminate()
print "POPEN: {} stopped '{}'".format(self.name, cmd)
self._processes = []
def add_service(self, cmd):
self._services.append(cmd)
class SdnNode(OVSSwitch):
def __init__(self, name, reliability=1.0, **params):
OVSSwitch.__init__(self, name, failMode='secure', datapath='kernel', inband=False, protocols=None, **params)
self.reliability = reliability
self._online = True
self.cmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1")
self.cmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1")
def simulate_reliability(self):
r = random()
if self.reliability > r:
self.boot()
else:
self.shut_down()
def sdn_intf_List(self):
return [interface for interface in self.intfList() if isinstance(interface, SdnIntf)]
def boot(self):
if not self._online:
for interface in self.sdn_intf_List():
interface.up()
print "Boot Node {}".format(self.name)
self._online = True
def shut_down(self):
if self._online:
for interface in self.sdn_intf_List():
interface.down()
print "Shut down Node {}".format(self.name)
self._online = False