-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigure.py
More file actions
executable file
·553 lines (518 loc) · 20.8 KB
/
configure.py
File metadata and controls
executable file
·553 lines (518 loc) · 20.8 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import json
import sys
import networkx as nx
# 3072 8 4
EPOCH_LENGTH = 2**16 # microseconds
LBW = 100 # Mbps
Q_RATE = 9000 # packets/second (default: 3072) 8400 6300
Q_DEPTH_FRAC = 8
CTH_DEPTH_FRAC = 8 # 4 bw, 8 burst
Q_DEPTH = int(Q_RATE/Q_DEPTH_FRAC) # packets (default: 128) Q_RATE/20
# packets (1/4 of queue capacity, default: 32) Q_DEPTH/4
CTH_DEPTH = int(Q_DEPTH/CTH_DEPTH_FRAC)
# microseconds 10000 (default: 10000) SLA DEPENDENT
CTH_TIMEDELTA = int((1000000/Q_DEPTH_FRAC)/CTH_DEPTH_FRAC)
STH_BITRATE = int((LBW*1e6*0.5/8)/(1e6/EPOCH_LENGTH))
def main(net_file):
with open(net_file, 'r') as f:
net = json.load(f)
n_nodes = net['nodes']
hosts_per_switch = net['hosts_per_node']
node_links = net['node_links']
e2e_delay_slas = net['e2e_delay_slas'] if 'e2e_delay_slas' in net else {}
bandwidth_slas = net['bandwidth_slas'] if 'bandwidth_slas' in net else {}
all_links = []
nodes = {}
hosts = {}
table_entries = {}
# create telemetry sink node
node_num = 0
nid = 's0'
nodes[nid] = {
'id': nid,
'num': node_num,
'ip': '10.0.0.0',
'mac': '01:02:04:08:16:32',
'adj': []
}
table_entries[nid] = []
# create nodes and hosts
for i in range(n_nodes):
node_num = i + 1
nid = 's{}'.format(node_num)
table_entries[nid] = []
nodes[nid] = {
'id': nid,
'num': node_num,
'ip': '10.0.{}.0'.format(node_num),
'mac': '00:00:00:00:{:02X}:00'.format(node_num),
'adj': []
}
for j in range(hosts_per_switch):
host_num = i*hosts_per_switch + j + 1
hid = 'h{}'.format(host_num)
hosts[hid] = {
'id': hid,
'num': host_num,
'ip': '10.0.{}.{}'.format(node_num, host_num),
'mac': '00:00:00:00:{:02X}:{:02X}'.format(node_num, host_num),
'adj': nid
}
# add adjacency to hosts
nodes[nid]['adj'].append(hid)
all_links.append([hid, nid, '0us', LBW])
# mac address
table_entries[nid].append({
"table": "egress.mac_addrs",
"match": {
"smd.egress_port": j + 1
},
"action_name": "egress.rewrite_mac_addrs",
"action_params": {
"src": nodes[nid]['mac'],
"dst": hosts[hid]['mac']
}
})
# add adjacency to telemetry sink node
nodes['s0']['adj'].append(nid)
nodes[nid]['adj'].append('s0')
# all_links.append(['s0', nid, '0us'])
all_links.append(['s0', nid, '0us', LBW])
table_entries[nid].append({
"table": "egress.mac_addrs",
"match": {
"smd.egress_port": hosts_per_switch + 1
},
"action_name": "egress.rewrite_mac_addrs",
"action_params": {
"src": nodes[nid]['mac'],
"dst": nodes['s0']['mac']
}
})
# node and analyzer IP addresses
table_entries[nid].append({
"table": "egress.node_and_analyzer_IP_addr",
"default_action": True,
"action_name": "egress.set_node_and_analyzer_IP_addr",
"action_params": {
"node": nodes[nid]['ip'],
"analyzer": "10.0.0.0"
}
})
# contention thresholds
table_entries[nid].append({
"table": "egress.contention_thresholds",
"default_action": True,
"action_name": "egress.set_contention_thresholds",
"action_params": {
"timedelta": CTH_TIMEDELTA,
"depth": CTH_DEPTH
}
})
# suspicion thresholds
table_entries[nid].append({
"table": "egress.suspicion_thresholds",
"default_action": True,
"action_name": "egress.set_suspicion_thresholds",
"action_params": {
"bitrate": STH_BITRATE,
}
})
for link in node_links:
if link[0] > link[1]:
link[0], link[1] = link[1], link[0]
link_sort_key = lambda x: (int(x[0][1:]), int(x[1][1:]))
node_links.sort(key=link_sort_key)
# add node to node adjacency
# and build a graph from link list
topo = nx.Graph()
for link in node_links:
uid, vid = link[0], link[1]
# all_links.append([uid, vid, '0us'])
all_links.append([uid, vid, '0us', LBW])
topo.add_edge(uid, vid)
nodes[uid]['adj'].append(vid)
nodes[vid]['adj'].append(uid)
# mac addresses
table_entries[uid].append({
"table": "egress.mac_addrs",
"match": {
"smd.egress_port": nodes[uid]['adj'].index(vid) + 1
},
"action_name": "egress.rewrite_mac_addrs",
"action_params": {
"src": nodes[uid]['mac'],
"dst": nodes[vid]['mac']
}
})
table_entries[vid].append({
"table": "egress.mac_addrs",
"match": {
"smd.egress_port": nodes[vid]['adj'].index(uid) + 1
},
"action_name": "egress.rewrite_mac_addrs",
"action_params": {
"src": nodes[vid]['mac'],
"dst": nodes[uid]['mac']
}
})
shortest_paths = dict(nx.all_pairs_shortest_path(topo))
# routing
routing_entries = {}
for u in nodes.keys():
routing_entries[u] = {}
table_entries['s0'].append({
"table": "ingress.ipv4_lpm",
"match": {
"hdrs.ipv4.dst_addr": [nodes['s0']['ip'], 24]
},
"action_name": "ingress.drop",
"action_params": {}
})
for src, paths in shortest_paths.items():
for h in range(hosts_per_switch):
table_entries[src].append({
"table": "ingress.ipv4_lpm",
"match": {
"hdrs.ipv4.dst_addr": [hosts[nodes[src]['adj'][h]]['ip'], 32]
},
"action_name": "ingress.ipv4_forward",
"action_params": {
"port": h + 1
}
})
for dst, path in paths.items():
if src == dst:
routing_entries[src][dst] = -1
# drop packets directed to itself
table_entries[src].append({
"table": "ingress.ipv4_lpm",
"match": {
"hdrs.ipv4.dst_addr": [nodes[src]['ip'], 24]
},
"action_name": "ingress.drop",
"action_params": {}
})
# forwarding packets directed to sink via port 'hosts_per_node + 1'
table_entries[src].append({
"table": "ingress.ipv4_lpm",
"match": {
"hdrs.ipv4.dst_addr": [nodes['s0']['ip'], 24]
},
"action_name": "ingress.ipv4_forward",
"action_params": {
"port": hosts_per_switch + 1
}
})
else:
for i in range(len(path) - 1):
u = path[i]
v = path[i + 1]
if dst not in routing_entries[u]:
routing_entries[u][dst] = nodes[u]['adj'].index(v) + 1
table_entries[u].append({
"table": "ingress.ipv4_lpm",
"match": {
"hdrs.ipv4.dst_addr": [nodes[dst]['ip'], 24]
},
"action_name": "ingress.ipv4_forward",
"action_params": {
"port": nodes[u]['adj'].index(v) + 1
}
})
else:
if routing_entries[u][dst] != nodes[u]['adj'].index(v) + 1:
print('error!')
# flow IDs
last_flow_ID = {}
for nid in nodes:
last_flow_ID[nid] = 0
for uid, hu in sorted(hosts.items(), key=lambda x: int(x[0][1:])):
for vid, hv in sorted(hosts.items(), key=lambda x: int(x[0][1:])):
if uid < vid:
uadj = hu['adj']
vadj = hv['adj']
######################
# FLOW: uid ===>>> vid
######################
# Ingress node
flow_ID_uadj = last_flow_ID[uadj] + 1
last_flow_ID[uadj] = last_flow_ID[uadj] + 1
table_entries[uadj].append({
"table": "ingress.flow_ID",
"match": {
"hdrs.ipv4.src_addr": hu['ip'],
"hdrs.ipv4.dst_addr": hv['ip']
},
"action_name": "ingress.set_flow_ID",
"action_params": {
"flow_ID": flow_ID_uadj
}
})
# # test code - beginning
# table_entries[uadj].append({
# "table": "ingress.flow_ID",
# "match": {
# "hdrs.ipv4.src_addr": hu['ip'],
# "hdrs.ipv4.dst_addr": hv['ip']
# },
# "action_name": "ingress.set_flow_ID",
# "action_params": {
# "flow_ID": flow_ID_uadj
# }
# })
# # test code - end
# Egress node
if uadj == vadj:
# the flow ID is the same because we are in the same node
flow_ID_vadj = flow_ID_uadj
else: # uadj != vadj
# we need a new flow ID
flow_ID_vadj = last_flow_ID[vadj] + 1
last_flow_ID[vadj] = last_flow_ID[vadj] + 1
# and a rule at the node adj to u
table_entries[vadj].append({
"table": "ingress.flow_ID",
"match": {
"hdrs.ipv4.src_addr": hu['ip'],
"hdrs.ipv4.dst_addr": hv['ip']
},
"action_name": "ingress.set_flow_ID",
"action_params": {
"flow_ID": flow_ID_vadj
}
})
# # test code - beginning
# table_entries[vadj].append({
# "table": "ingress.flow_ID",
# "match": {
# "hdrs.ipv4.src_addr": hu['ip'],
# "hdrs.ipv4.dst_addr": hv['ip']
# },
# "action_name": "ingress.set_flow_ID",
# "action_params": {
# "flow_ID": flow_ID_vadj
# }
# })
# # test code - end
# if there is an e2e delay SLA from u to v
if uid in e2e_delay_slas and vid in e2e_delay_slas[uid]:
# e2e delay threshold
table_entries[vadj].append({
"table": "egress.e2e_delay_threshold",
"match": {
"cmd.flow_ID": flow_ID_vadj
},
"action_name": "egress.set_e2e_delay_threshold",
"action_params": {
"threshold": e2e_delay_slas[uid][vid][0]
}
})
# number of high delays threshold
table_entries[vadj].append({
"table": "egress.high_delays_threshold",
"match": {
"cmd.flow_ID": flow_ID_vadj
},
"action_name": "egress.set_high_delays_threshold",
"action_params": {
"threshold": e2e_delay_slas[uid][vid][1]
}
})
if uid in bandwidth_slas and vid in bandwidth_slas[uid]:
# bandwidth thresholds
table_entries[vadj].append({
"table": "egress.bandwidth_thresholds",
"match": {
"cmd.flow_ID": flow_ID_vadj
},
"action_name": "egress.set_bandwidth_thresholds",
"action_params": {
"bandwidth": bandwidth_slas[uid][vid][0],
"drops": bandwidth_slas[uid][vid][1]
}
})
######################
# FLOW: uid <<<=== vid
######################
# Ingress node
flow_ID_vadj = last_flow_ID[vadj] + 1
last_flow_ID[vadj] = last_flow_ID[vadj] + 1
table_entries[vadj].append({
"table": "ingress.flow_ID",
"match": {
"hdrs.ipv4.src_addr": hv['ip'],
"hdrs.ipv4.dst_addr": hu['ip']
},
"action_name": "ingress.set_flow_ID",
"action_params": {
"flow_ID": flow_ID_vadj
}
})
# # test code - beginning
# table_entries[vadj].append({
# "table": "ingress.flow_ID",
# "match": {
# "hdrs.ipv4.src_addr": hv['ip'],
# "hdrs.ipv4.dst_addr": hu['ip']
# },
# "action_name": "ingress.set_flow_ID",
# "action_params": {
# "flow_ID": flow_ID_vadj
# }
# })
# # test code - end
# Egress node
if vadj == uadj:
# the flow ID is the same because we are in the same node
flow_ID_uadj = flow_ID_vadj
else:
# we need a new flow ID
flow_ID_uadj = last_flow_ID[uadj] + 1
last_flow_ID[uadj] = last_flow_ID[uadj] + 1
# and a rule at the node adj to u
table_entries[uadj].append({
"table": "ingress.flow_ID",
"match": {
"hdrs.ipv4.src_addr": hv['ip'],
"hdrs.ipv4.dst_addr": hu['ip']
},
"action_name": "ingress.set_flow_ID",
"action_params": {
"flow_ID": flow_ID_uadj
}
})
# # test code - beginning
# table_entries[uadj].append({
# "table": "ingress.flow_ID",
# "match": {
# "hdrs.ipv4.src_addr": hv['ip'],
# "hdrs.ipv4.dst_addr": hu['ip']
# },
# "action_name": "ingress.set_flow_ID",
# "action_params": {
# "flow_ID": flow_ID_uadj
# }
# })
# # test code - end
# if there is an e2e delay SLA from u to v
if vid in e2e_delay_slas and uid in e2e_delay_slas[vid]:
# e2e delay threshold
table_entries[uadj].append({
"table": "egress.e2e_delay_threshold",
"match": {
"cmd.flow_ID": flow_ID_uadj
},
"action_name": "egress.set_e2e_delay_threshold",
"action_params": {
"threshold": e2e_delay_slas[vid][uid][0]
}
})
# number of high delays threshold
table_entries[uadj].append({
"table": "egress.high_delays_threshold",
"match": {
"cmd.flow_ID": flow_ID_uadj
},
"action_name": "egress.set_high_delays_threshold",
"action_params": {
"threshold": e2e_delay_slas[vid][uid][1]
}
})
if vid in bandwidth_slas and uid in bandwidth_slas[vid]:
# bandwidth thresholds
table_entries[uadj].append({
"table": "egress.bandwidth_thresholds",
"match": {
"cmd.flow_ID": flow_ID_uadj
},
"action_name": "egress.set_bandwidth_thresholds",
"action_params": {
"bandwidth": bandwidth_slas[vid][uid][0],
"drops": bandwidth_slas[vid][uid][1]
}
})
# if uid != vid:
# # to
# flow_ID_to = last_flow_ID[hu['adj']] + 1
# last_flow_ID[hu['adj']] = last_flow_ID[hu['adj']] + 1
# table_entries[hu['adj']].append({
# "table": "egress.flow_ID",
# "match": {
# "hdrs.ipv4.src_addr": hu['ip'],
# "hdrs.ipv4.dst_addr": hv['ip']
# },
# "action_name": "egress.set_flow_ID",
# "action_params": {
# "flow_ID": flow_ID_to
# }
# })
# # fro
# if hu['adj'] != hv['adj']:
# table_entries[hu['adj']].append({
# "table": "egress.flow_ID",
# "match": {
# "hdrs.ipv4.src_addr": hv['ip'],
# "hdrs.ipv4.dst_addr": hu['ip']
# },
# "action_name": "egress.set_flow_ID",
# "action_params": {
# "flow_ID": last_flow_ID[hu['adj']] + 1
# }
# })
# last_flow_ID[hu['adj']] = last_flow_ID[hu['adj']] + 1
# GENERATE FILES
all_links.sort(key=lambda x: (x[0][0], int(x[0][1:]), int(x[1][1:])))
# generate topology configuration file
topology_cfg = {
"capture_traffic": net['capture_traffic'],
"run_workload": net['run_workload'],
"workload_file": net['workload_file'],
"hosts": list(hosts.keys()),
"switches": {},
"links": all_links
}
for nid, node in nodes.items():
if nid == 's0':
topology_cfg['switches'][nid] = {
"runtime_json": "s0-runtime.json"
}
else:
topology_cfg['switches'][nid] = {
"runtime_json": "{}-runtime.json".format(nid),
"cli_input": "{}-cli.txt".format(nid)
}
print('building topology.json', end='... ')
with open('topology.json', 'w') as f:
json.dump(topology_cfg, f, indent=4)
print('done')
# generate device configuration files
for nid, node in nodes.items():
# Runtime configuration
node_runtime_cfg = {
"target": "bmv2",
"p4info": "build/intsight.p4info",
"bmv2_json": "build/intsight.json",
"table_entries": table_entries[nid]
}
print('building {}-runtime.json'.format(nid), end='... ')
with open('{}-runtime.json'.format(nid), 'w') as f:
json.dump(node_runtime_cfg, f, indent=4)
print('done')
# Runtime configuration via CLI
node_cli_cfg = "register_write ingress.node_ID 0 {}\n".format(
node['num']
)
node_cli_cfg += "mirroring_add 42 {}\n".format(hosts_per_switch + 1)
node_cli_cfg += "set_queue_depth {}\n".format(Q_DEPTH)
node_cli_cfg += "set_queue_rate {}\n".format(Q_RATE)
print('building {}-cli.txt'.format(nid), end='... ')
with open('{}-cli.txt'.format(nid), 'w') as f:
f.write(node_cli_cfg)
print('done')
if __name__ == '__main__':
if len(sys.argv) > 1:
main(sys.argv[1])
else:
print('Usage: python3 configure.py <network_json>')
sys.exit(1)