-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowsch-leaf0.py
297 lines (266 loc) · 10.2 KB
/
flowsch-leaf0.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
#! /usr/bin/python
"""
The Flow Scheduler project utilizes floodlight OpenFlow controller rest APIs to dynamically update flow entries on all switches
based on network traffic.
This code gets the topology information connected to the Floodlight Controller
This information can be verified by visiting the website:
http://<controller IP address>:8080/ui/index.html
Example: http://128.110.152.148:8080/ui/index.html where the controller's IP address is 128.110.152.148
The code also gives example of how to add the forwarding rules in the switches
Syntax:
python flowsch.py {IP:REST_PORT} {num_groups}
@author Kunal Mahajan, [email protected]
PhD Candidate in CS
Columbia University
"""
import httplib
import os
import sys
import subprocess
import json
import argparse
import io
import time
parser = argparse.ArgumentParser(description='Flow Scheduler')
parser.add_argument("controllerRestIP", action='store', default='localhost:8080', help='controller IP:RESTport, e.g., localhost:8080 or A.B.C.D:8080')
parser.add_argument("num_groups", action='store', default='2', help='number of groups, e.g., 2 or 10')
args = parser.parse_args()
controllerRestIP = args.controllerRestIP # the ip address along with the port number
controllerIP, port = controllerRestIP.split(":")
num_groups = int(args.num_groups)
class Forwarding(object):
def __init__(self, server):
self.server = server
def get(self, data):
ret = self.rest_call({}, 'GET')
return json.loads(ret[2])
def set(self, data):
ret = self.rest_call(data, 'POST')
return ret[0] == 204
def rest_call(self, data, action):
path = '/wm/forwarding/json'
headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
}
body = json.dumps(data)
conn = httplib.HTTPConnection(self.server, 8080)
conn.request(action, path, body, headers)
response = conn.getresponse()
ret = (response.status, response.reason, response.read())
# print ret
conn.close()
return ret
# Get the list of switches with their dpid
command = "curl -s http://%s/wm/core/controller/switches/json" % (controllerRestIP)
result = os.popen(command).read()
switches = json.loads(result)
dpid = switches[0]['switchDPID']
# create stats and group variables
port_stats = {}
flow_stats = {}
flow_groups = {}
spine_ports = [1, 5, 22, 37, 65, 69] # UPDATE THESE TODO
path_assignment = {}
iptuple_port_dict = {}
all_ports = []
rack_attachments = {0:
[
('10.10.1.1', 9),
('10.10.1.2', 10),
('10.10.1.3', 11),
('10.10.1.4', 12),
('10.10.1.5', 13),
('10.10.1.6', 14),
('10.10.1.7', 15),
('10.10.1.8', 16),
('10.10.1.9', 17),
('10.10.1.10', 18),
('10.10.1.11', 19),
('10.10.1.12', 20),
('10.10.1.13', 21),
('10.10.1.14', 22),
('10.10.1.15', 23),
('10.10.1.16', 24),
],
1:
[
('10.10.2.1', 9),
('10.10.2.2', 10),
('10.10.2.3', 11),
('10.10.2.4', 12),
('10.10.2.5', 13),
('10.10.2.6', 14),
('10.10.2.7', 15),
('10.10.2.8', 16),
('10.10.2.9', 17),
('10.10.2.10', 18),
('10.10.2.11', 19),
('10.10.2.12', 20),
('10.10.2.13', 21),
('10.10.2.14', 22),
('10.10.2.15', 23),
('10.10.2.16', 24),
]
}
"""
Create the list of all ports associated with the switch
"""
def get_all_ports():
for item in spine_ports:
all_ports.append(item)
for (ip, port) in rack_attachments[0]:
all_ports.append(port)
"""
Create the IP tuple and possible output ports dictionary for Rack 0 switch
"""
def create_iptuple_outputport_comb():
# within rack 0 combinations
for (src_ip, src_port) in rack_attachments[0]:
for (dst_ip, dst_port) in rack_attachments[0]:
if src_ip == dst_ip:
continue
iptuple_port_dict[(src_ip, dst_ip)] = [dst_port]
# rack 0 to rack 1 combinations
for (src_ip, src_port) in rack_attachments[0]:
for (dst_ip, dst_port) in rack_attachments[1]:
if src_ip == dst_ip:
continue
iptuple_port_dict[(src_ip, dst_ip)] = spine_ports
"""
Get the group bandwidth usage
key is ipv4_src and ipv4_dst
value is dictionary of groups
for dictionary of groups: key is group number, value is bandwidth usage
"""
def get_group_bw_usage():
for flow in flow_stats:
# initialize the groups for this src-dst ip tuple if it does not exist
if (flow[0], flow[1]) not in flow_groups:
groups = {}
for i in range(0,num_groups):
groups[i] = 0
flow_groups[(flow[0], flow[1])] = groups
groups = flow_groups[(flow[0], flow[1])]
# compute group_id for this flow
group_id = (int(flow[2]) ^ int(flow[3])) % num_groups
# add byte_count to this group_id usage
counts = flow_stats[flow]
groups[group_id] += counts['byte_diff']
flow_groups[(flow[0], flow[1])] = groups
def get_port_usages():
port_congestion = {}
for port in all_ports:
port_congestion[port] = port_stats[dpid][port]['tx_bytes_diff']
return port_congestion
"""
For each src-dst ip tuple, there is a set of paths.
Scheduler has to assign paths to each of the groups based on path utilization
"""
def scheduler():
for ip_tuple in flow_groups:
groups = flow_groups[ip_tuple]
sorted_groups = sorted(groups.items(), key=lambda x: x[1], reverse=True)
groups_path = {}
port_congestion = get_port_usages()
for group_id, byte_count in sorted_groups:
min_value = 99999999999999999999
min_port = -1
for port in iptuple_port_dict[ip_tuple]:
if port not in port_congestion:
port_congestion[port] = 0
if port_congestion[port] < min_value:
min_value = port_congestion[port]
min_port = port
groups_path[group_id] = min_port
port_congestion[min_port] += byte_count
path_assignment[ip_tuple] = groups_path
return path_assignment
"""
flow_stats data structure:
key: tuple of (ipv4_src, ipv4_dst, tcp_src, tcp_dst)
value: dictionary, where key is (pkt_count, pkt_diff, byte_count, byte_diff)
"""
def parse_flows(flows, dpid):
parsedResult = json.loads(flows)
flow_results = parsedResult['flows']
for item in flow_results:
match = item['match']
if 'ipv4_src' not in match or 'tcp_src' not in match:
continue
if item['table_id'] == "0xc8": # table id = 200
if (match['ipv4_src'], match['ipv4_dst'], match['tcp_src'], match['tcp_dst']) in flow_stats:
stat = flow_stats[(match['ipv4_src'], match['ipv4_dst'], match['tcp_src'], match['tcp_dst'])]
flow_stats[(match['ipv4_src'], match['ipv4_dst'], match['tcp_src'], match['tcp_dst'])] = {'pkt_count' : long(item['packet_count']),
'pkt_diff' : long(item['packet_count']) - stat['pkt_count'],
'byte_count' : long(item['byte_count']),
'byte_diff': long(item['byte_count']) - stat['byte_count']}
else:
flow_stats[(match['ipv4_src'], match['ipv4_dst'], match['tcp_src'], match['tcp_dst'])] = {'pkt_count' : long(item['packet_count']),
'pkt_diff' : 0, 'byte_count' : long(item['byte_count']),
'byte_diff': 0}
# match = item['match']
# actions = item['instructions']['instruction_apply_actions']['actions']
# actions = actions.split("=")
# port_num = actions[1]
# print flow_stats
def add_port_stat(dpid, port_number, rx_packets, rx_bytes, tx_packets, tx_bytes, rx_packets_diff, rx_bytes_diff, tx_packets_diff, tx_bytes_diff):
port_stats[dpid][port_number] = {'rx_packets' : rx_packets, 'rx_bytes' : rx_bytes,
'tx_packets' : tx_packets, 'tx_bytes' : tx_bytes,
'rx_packets_diff': rx_packets_diff, 'rx_bytes_diff' : rx_bytes_diff,
'tx_packets_diff': tx_packets_diff, 'tx_bytes_diff' : tx_bytes_diff}
# Calculation does not account for overflows
def parse_ports(ports, dpid):
parsedResult = json.loads(ports)
stats = parsedResult['port_reply'][0]['port']
for item in stats:
if dpid in port_stats:
port_number = long(item['port_number'])
#print port_number
if port_number in port_stats[dpid]:
rx_packets_diff = long(item['receive_packets']) - port_stats[dpid][port_number]['rx_packets']
rx_bytes_diff = long(item['receive_bytes']) - port_stats[dpid][port_number]['rx_bytes']
tx_packets_diff = long(item['transmit_packets']) - port_stats[dpid][port_number]['tx_packets']
tx_bytes_diff = long(item['transmit_bytes']) - port_stats[dpid][port_number]['tx_bytes']
add_port_stat(dpid, port_number, long(item['receive_packets']), long(item['receive_bytes']), # update entry
long(item['transmit_packets']), long(item['transmit_bytes']),
rx_packets_diff, rx_bytes_diff, tx_packets_diff, tx_bytes_diff)
else:
add_port_stat(dpid, port_number, long(item['receive_packets']), long(item['receive_bytes']), # add entry for new port number
long(item['transmit_packets']), long(item['transmit_bytes']),
0, 0, 0, 0)
else: # add entry for new switch
port_stats[dpid] = {}
port_number = long(item['port_number'])
add_port_stat(dpid, port_number, long(item['receive_packets']), long(item['receive_bytes']),
long(item['transmit_packets']), long(item['transmit_bytes']),
0, 0, 0, 0)
# print port_stats
def rest_call(command):
return os.popen(command).read()
def convert_to_json(path_assignment):
# convert keys in path_assignment to strings
str_path_assignment = {}
for item in path_assignment:
str_path_assignment[str(item)] = path_assignment[item]
return json.dumps(str_path_assignment)
create_iptuple_outputport_comb()
pusher = Forwarding(controllerIP)
# Get all the flows for the switches:
while True:
start_time = time.time()
for i in range(len(switches)):
flow_command = "curl -s http://%s/wm/core/switch/%s/flow/json" % (controllerRestIP, switches[i]['switchDPID'])
flows = rest_call(flow_command)
port_command = "curl -s http://%s/wm/core/switch/%s/port/json" % (controllerRestIP, switches[i]['switchDPID'])
ports = rest_call(port_command)
parse_flows(flows, switches[i]['switchDPID'])
get_group_bw_usage()
parse_ports(ports, switches[i]['switchDPID'])
path_assignment = scheduler()
if len(path_assignment.keys()) != 0:
# print iptuple_port_dict
json_path_assignment = convert_to_json(path_assignment)
print json_path_assignment
pusher.set(json_path_assignment)
print("--- %s seconds ---" % (time.time() - start_time))