-
Notifications
You must be signed in to change notification settings - Fork 1
/
policy.py
318 lines (257 loc) · 10.9 KB
/
policy.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
from bwstats import BandwidthStats
from topo import Topology
import flood
import os
import sys
'''
Useful CoreSwitch class functions/properties:
CoreSwitch.name - the name of the switch (eg, 's101')
CoreSwitch.dpid - the dpid of the switch (eg, 101)
CoreSwitch.vlans - list of pre-assigned vlans for the switch to handle
Useful EdgeSwitch class functions/properties:
EdgeSwitch.name - the name of the switch (eg, 's101')
EdgeSwitch.dpid - the dpid of the switch (eg, 101)
EdgeSwitch.neighbors - list of hosts's names (eg, 'h6') attached to
the switch
Useful Host class functions/properties:
Host.name - the name of the host (eg, 'h6')
Host.eth - the MAC address of the host
Host.switch - the switch to which the host is connected
Host.vlans - a list of vlans with which the host is associated
Useful Topology class functions/properties:
Topology.hosts - a dictionary of Host objects keyed on host name
Topology.switches - a list of switch names
Topology.coreSwitches - dictionary of CoreSwitch objects keyed on
switch name
Topology.edgeSwitches - dictionary of EdgeSwitch objects keyed on
switch name
Topology.vlans - dictionary of vlan members, keyed on the vlan id (an integer)
Topology.ports[sw][inNode] - returns the port number on switch sw connected to
the adjacent node inNode, if inNode is a node name
(ie, switch or host). If inNode is a number, returns
the node or switch name on that port on switch sw.
Topology.getVlanCore(vlanId) - returns the core switch pre-assigned for the
vlan vlanId
Topology.dpidToName(dpid) - returns the switch name associated with the
specified dpid
'''
class AdaptivePolicy(object):
def __init__(self, topo, bwstats, logger):
self.topo = topo
self.bwstats = bwstats
self.logger = logger
self.assignments = {}
self.utilization = {}
default = topo.coreSwitches.keys()[0]
for host in topo.hosts.keys():
self.assignments[host] = default
self._routingTable = self.build(self.topo)
@property
def routingTable(self):
# rebuild with updated bandwidth stats
self.redistribute()
return self.build(self.topo)
def minUtilization(self):
# ASSIGNMENT 4:
# This function should return the core switch that
# is least utilized. We will use this to greedily
# assign hosts to core switches based on the amount
# of traffic they are receiving and balance load
# on the core switches.
# Use the dictionary self.utilization
# (key = switch name, value = utilization in bytes)
# to find the least utilized switch.
# [REPLACE WITH YOUR CODE]
#jlow = []
#for core in self.topo.coreSwitches.keys()
# core.ukk
print "IN MY ASS 4 CODE"
print self.utlization
print "END OF DEBUG"
return self.utilization.keys()[0]
def redistribute(self):
# we're installing flows by destination, so sort by received
stats = []
for host in self.bwstats.hostBw.keys():
stats.append((self.bwstats.hostBw[host]['in'], host))
# sort largest to smallest
stats.sort(reverse=True)
# reset utilization
for core in self.topo.coreSwitches.keys():
self.utilization[core] = 0
for stat in stats:
host = stat[1]
# greedily assign hosts to least utilized
core = self.minUtilization()
self.assignments[host] = core
self.utilization[core] += stat[0]
self.logger.info(self.utilization)
self.logger.info(self.assignments)
def build(self, topo):
routingTable = {}
# assign core-edge downward
for core in topo.coreSwitches.values():
routingTable[core.dpid] = []
for h in topo.hosts.values():
outport = topo.ports[core.name][h.switch]
routingTable[core.dpid].append({
'eth_dst' : h.eth,
'output' : [outport],
'priority' : 2,
'type' : 'dst'
})
for edge in topo.edgeSwitches.values():
routingTable[edge.dpid] = []
for h in topo.hosts.values():
# don't send neighbors up to core
if h.name in edge.neighbors:
outport = topo.ports[edge.name][h.name]
else:
# send to core
core = self.assignments[h.name]
outport = topo.ports[edge.name][core]
routingTable[edge.dpid].append({
'eth_dst' : h.eth,
'output' : [outport],
'priority' : 2,
'type' : 'dst'
})
return flood.add_arpflood(routingTable, topo)
class StaticPolicy(object):
def __init__(self, topo):
self.routingTable = self.build(topo)
def build(self, topo):
routingTable = {}
# core switches: for a given host destination, send packet
# to the host's edge switch ("downward")
for core in topo.coreSwitches.values():
routingTable[core.dpid] = []
for h in topo.hosts.values():
outport = topo.ports[core.name][h.switch]
routingTable[core.dpid].append({
'eth_dst' : h.eth,
'output' : [outport],
'priority' : 2,
'type' : 'dst'
})
# ASSIGNMENT 3:
# Rules to Install:
# On the Edge Switches: output the appropriate port if the destination
# is a neighboring host (that is, don't send it up to a core switch).
# Otherwise, send to the core switch for that destination's vlan
# ("upward"). If a host has multiple VLANs, you can use the first
# in the list of VLANs.
# (Hint: you can look up the port of a neighboring host using
# topo.ports[edge switch name][host name]
# (Hint: to find a the VLAN, use topo.getVlanCore(vlanId))
# [ADD YOUR CODE HERE]
for edge in topo.edgeSwitches.values():
routingTable[edge.dpid]=[]
for h in topo.hosts.values():
if h.name in edge.neighbors:
outport = topo.ports[edge.name][h.name]
#print "OutportNeigh Set to: ", outport, "for edge", edge, "host", h.name
#print "OUTPORT: ", outport
else:
outport = topo.ports[edge.name][topo.getVlanCore(h.vlans[0])]
#if h.name in topo.vlans[0]:
#outport = topo.ports[edge.name][topo.getVlanCore(h.vlans[0])]
#outport = topo.ports[edge.name][topo.getVlanCore(0)]
#print "Outport0 Set to: ", outport, "for edge:", edge, "host:", h.name
#print "VLANS0: ", topo.vlans[0]
#print "VLANS0: ", topo.getVlanCore(0)
#print "EDGE Name", edge.name, h.name
#print "HOST Name", h.name, h.vlans[0]
#print "CORE0", core
#print "PORTS0", topo.ports[edge.name][topo.getVlanCore(0)]
#elif h.name in topo.vlans[1]:
#print "VLANS1: ", topo.vlans[1]
#print "VLANS1: ", topo.getVlanCore(1)
#print "EDGE.NEIGHBORS", edge.name, h.name, core
#print "HOST Name", h.name, h.vlans
#print "PORTS1", topo.ports.keys()
#print "CORE1", core
#print "PORTS1", topo.ports[edge.name][topo.getVlanCore(1)]
#outport = topo.ports[edge.name][topo.getVlanCore(1)]
#print "Outport1 Set to: ", outport, "for edge:", edge, "host", h.name
#print "OUTPORT: ", outport
routingTable[edge.dpid].append({
'eth_dst' : h.eth,
'output' : [outport],
'priority' : 2,
'type' : 'dst'
})
#print "Routing Table: ", routingTable
return flood.add_arpflood(routingTable, topo)
class DefaultPolicy(object):
def __init__(self, topo):
self.routingTable = self.build(topo)
def build(self, topo):
routingTable = {}
# use only one core switch
core = topo.coreSwitches.keys()[0]
coreDpid = topo.coreSwitches[core].dpid
routingTable[coreDpid] = []
# create rules for packets from core -> edge (downward)
for h in topo.hosts.values():
outport = topo.ports[core][h.switch]
routingTable[coreDpid].append({
'eth_dst' : h.eth,
'output' : [outport],
'priority' : 2,
'type' : 'dst'
})
# DEFAULT
# create rules for packets from edge -> core (upward)
for edge in topo.edgeSwitches.values():
routingTable[edge.dpid] = []
for h in topo.hosts.values():
# don't send edge switch's neighbors up to core
if h.name in edge.neighbors:
outport = topo.ports[edge.name][h.name]
else:
outport = topo.ports[edge.name][core]
routingTable[edge.dpid].append({
'eth_dst' : h.eth,
'output' : [outport],
'priority' : 2,
'type' : 'dst'
})
return flood.add_arpflood(routingTable, topo)
if __name__ == "__main__":
class MockLogger(object):
def info(self, msg, *args, **kwargs):
pass
def debug(self, msg, *args, **kwargs):
pass
def warning(self, msg, *args, **kwargs):
pass
def error(self, msg, *args, **kwargs):
pass
def critical(self, msg, *args, **kwargs):
pass
def log(self, msg, *args, **kwargs):
pass
def exception(self, msg, *args, **kwargs):
pass
class MockBandwidthStats(object):
def __init__(self, topo):
self.topo = topo
self.hostBw = {}
def addHostBwStat(self, host, txbytes, rxbytes):
pass
cfgfile = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"..", "config.txt"))
if not os.path.isfile(cfgfile):
print "Cannot run test without file", cfgfile
print "To create file, run mdc (eg, sudo ./mdc --vid)"
sys.exit(1)
topo = Topology(cfgfile)
policy = AdaptivePolicy(topo, MockBandwidthStats(topo), MockLogger())
policy.utilization['s101'] = 100
policy.utilization['s102'] = 50
policy.utilization['s103'] = 200
print "Testing with values:", str(policy.utilization)
print "Expected min: s102"
print "Your code returns min:", str(policy.minUtilization())