-
Notifications
You must be signed in to change notification settings - Fork 0
/
latencycontroller.py
1422 lines (1267 loc) · 50 KB
/
latencycontroller.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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import os
import sys
import json
import pulp
import numpy
import random
import datetime
import threading
from sortundirectednodepair import _sort_pair
from graphtools import Dijkstra, find_all_paths, graph_from_topo
from time import sleep
from io import StringIO
from id2ip import id2ip, ip2id
from typing import Any
from typing import Union
from typing import Tuple
from typing import List
from typing import Dict
from typing import Optional
from configparser import ConfigParser
from ryu.lib import hub
from ryu.base import app_manager
from ryu.lib import ofctl_v1_3
from ryu.lib.packet import ether_types
from ryu.lib.packet import ethernet
from ryu.ofproto import ofproto_v1_3
from ryu.controller.handler import set_ev_cls
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER, DEAD_DISPATCHER
from ryu.controller import ofp_event
from ryu.lib.packet import packet
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
NEG_INF = float('-inf')
MIN_POSITIVE_FLOAT = numpy.nextafter(0, 1)
EMPTY_ITER = iter(list())
SPEED_KILOBIT = 0.001
SPEED_MEGABIT = 1
SPEED_GIGABIT = 1000
SPEED_TERABIT = 1000000
UNLIMITED_BANDWIDTH = 70*SPEED_TERABIT
NoneType = type(None)
print(f"loading network {sys.argv[2]}", file=sys.stderr)
network_topo = ''
with open(sys.argv[2]) as f:
network_topo = json.loads(f.read())
# ryu uses eventlet, which, on some versions, breaks pathlib's read_text
base_net_name = '.'.join(sys.argv[2].split('.')[:-1])
with open('~current.state', 'w') as f:
f.write(base_net_name)
with open('~current.sws.state', 'w') as f:
f.write("")
if os.path.exists(f'{base_net_name}.state'):
os.unlink(f'{base_net_name}.state')
print(f"reading network configuration variables.ini", file=sys.stderr)
network_config = ConfigParser()
with open('variables.ini') as f:
network_config.read_string(f.read())
m1 = float(network_config['linearalgconst']['m1'])
m2 = float(network_config['linearalgconst']['m2'])
hop_delay = float(network_config['linearalgconst']['hop_delay'])
apa_path_stretch = float(network_config['APA']['path_stretch'])
routing_algo = network_config['GENERAL']['algo']
network_graph = graph_from_topo(network_topo)
def jsonload_list2tuple(x):
if isinstance(x, type(None)) or isinstance(x, int) or isinstance(x, float) or isinstance(x, str):
return x
elif isinstance(x, list) or isinstance(x, tuple):
return tuple([jsonload_list2tuple(k) for k in x])
elif isinstance(x, dict):
return {jsonload_list2tuple(k): jsonload_list2tuple(v) for k, v in x.items()}
else:
raise ValueError("Input didn't come from a standard JSON")
def prepare_pop_pair_alternative_paths_for_availability(graph, hosts):
pop_apa = [[None for x in hosts] for y in hosts]
for i, h1 in enumerate(hosts):
for j, h2 in enumerate(hosts):
if pop_apa[i][j] is None:
apa = tuple(list(find_all_paths(graph, h1, h2)))
pop_apa[i][j] = apa
pop_apa[j][i] = apa
return pop_apa
apacachefile = f"{base_net_name}.apa.json"
print(
f"checking if APA for {sys.argv[2]} is cached at {apacachefile}", file=sys.stderr)
pop_apa_candidates = None
if os.path.isfile(apacachefile):
print(f"loading APA from {apacachefile}", file=sys.stderr)
with open(apacachefile) as f:
pop_apa_candidates = jsonload_list2tuple(json.loads(f.read()))
else:
print(f"calculating APA for {sys.argv[2]}", file=sys.stderr)
pop_apa_candidates = prepare_pop_pair_alternative_paths_for_availability(
network_graph,
network_topo[0]
)
print(f"caching APA at {apacachefile}", file=sys.stderr)
with open(apacachefile, 'w') as f:
f.write(json.dumps(pop_apa_candidates))
def filter_out_invalid_paths_from_multiple_paths(h1, h2, candidates, sw=None):
valid_paths = list()
valid_paths_bits_rev = set()
for path in sorted(
candidates,
key=lambda a: (len(a), a)
):
if sw is not None and sw not in path:
continue
if path[0] == h2 and path[-1] == h1:
path = list(reversed(path))
path_bits = set([
tuple(path[i:i+2])
for i in range(len(path)-1)
])
if len(path_bits.intersection(valid_paths_bits_rev)) <= 0:
valid_paths.append(path)
valid_paths_bits_rev = valid_paths_bits_rev.union(set([
tuple(reversed(i))
for i in path_bits
]))
return valid_paths
class LatencyController:
def __init__(self, datapath):
super().__init__()
self._datapath = datapath
self._sw = f"s{self._datapath.id}"
l = [
(i, x) for i, x in enumerate(network_topo[2])
if x[0] == self._sw or x[1] == self._sw
]
self._l = l
self._links = portno_from_list(l)
self._flow_xfer = dict()
self._flow_speed = dict()
self._link_speed = UsageStoreProxyFromFlowDict(self._flow_speed)
self._link_rules = usage_store_from_list(l)
self._bandwidth = bandwidth_from_list(l)
self._ospf = None
def connected(self, la: 'LatencyApp'):
print(f"Switch connected: {self._sw}")
print(f"Switch OSPF-fallback loading: {self._sw}")
parser = self._datapath.ofproto_parser
self._ospf = la.ospf_dijkstra(self._sw)
for h in network_topo[0]:
ipv4_dst = id2ip(int(h[1:])-1)
match_ipv4 = parser.OFPMatch(
eth_type=0x0800,
ipv4_dst=ipv4_dst
)
match_arp = parser.OFPMatch(
eth_type=0x0806,
arp_tpa=ipv4_dst
)
next_hop = self._ospf(h)[0][1]
out_port = self._links[(self._sw, next_hop)]
actions = [parser.OFPActionOutput(out_port)]
print(f"{self._sw} --[{out_port}]--> {next_hop} ({ipv4_dst})")
self.add_flow(1, match_ipv4, actions)
self.add_flow(1, match_arp, actions)
print(f"Switch OSPF-fallback loaded: {self._sw}")
if routing_algo == 'ecmp':
print(f"Switch ECMP setting up: {self._sw}")
self._configureECMP(la)
print(f"Switch ECMP set up: {self._sw}")
hub.spawn(self._write_in_initialized_file)
# self._write_in_initialized_file()
def _write_in_initialized_file(self, retries=5):
with initialized_switch_writing_lock:
with open('~current.sws.state', 'a') as f:
f.write(f"{self._sw}{os.linesep}")
if retries>0:
sleep(random.uniform(0.5, 2.5))
self._write_in_initialized_file(retries-1)
def disconnected(self):
print(f"Switch disconnected: {self._sw}")
def add_flow(self, priority, match, actions, buffer_id=None, idle_timeout=0, hard_timeout=0):
ofproto = self._datapath.ofproto
parser = self._datapath.ofproto_parser
inst = [parser.OFPInstructionActions(
ofproto.OFPIT_APPLY_ACTIONS,
actions
)]
additionals = dict()
if buffer_id:
additionals['buffer_id'] = buffer_id
if idle_timeout:
additionals['idle_timeout'] = idle_timeout
if hard_timeout:
additionals['hard_timeout'] = hard_timeout
mod = parser.OFPFlowMod(datapath=self._datapath,
priority=priority, match=match,
instructions=inst,
**additionals)
self._datapath.send_msg(mod)
def _the_other_link_endpoint(self, linktuple):
return linktuple[abs(linktuple.index(self._sw)-1)]
def _on_stats(self, ev, la: 'LatencyApp'):
self._link_rules = usage_store_from_list(self._l)
parser = self._datapath.ofproto_parser
stats = ev.msg.body
allkeys = []
for k in self._link_rules._pairs.keys():
self._link_rules._pairs[k] = 0
for stat in stats:
# ltp -> link tuple
# ftp -> flow tuple
# bc -> byte count
# obc -> old byte count
action = stat.instructions[0].actions[0]
outs = list()
if isinstance(action, parser.OFPActionGroup):
weight_sum = sum([
bucket.weight
for bucket in la.ecmp_group_id_buckets[action.group_id]
])
for bucket in la.ecmp_group_id_buckets[action.group_id]:
outs.append((
bucket.actions[0].port,
bucket.weight/max(1, weight_sum)
))
else:
outs.append((action.port, 1))
for out_port, weight in outs:
ltps = self._links.reverse_lookup(out_port)
if len(ltps) <= 0:
continue
ltp = ltps[0]
nxt = ltp[abs(ltp.index(self._sw)-1)]
# measuring link flows
self._link_rules[ltp] = 1+self._link_rules[ltp]
# measuring load
if stat.match['eth_type'] == 0x0800:
src = stat.match.get('ipv4_src', '10.0.0.0')
dst = stat.match['ipv4_dst']
src = f"h{ip2id(src)+1}"
dst = f"h{ip2id(dst)+1}"
if src == 'h0':
src = None
key = (src, self._sw, nxt, dst)
bc = stat.byte_count
obc = self._flow_xfer.get(key, 0)
tfd = max(0, bc-obc)*weight # bytes per cycle
tfr = ((8*tfd)/la.interval)/10**6 # mbps
self._flow_xfer[key] = bc
self._flow_speed[key] = tfr
allkeys.append(key)
for k in (set(self._flow_speed.keys())-set(allkeys)):
self._flow_speed[k] = 0
# with parts from <https://github.com/wildan2711/multipath/blob/master/ryu_multipath.py>
# commented by <https://wildanmsyah.wordpress.com/2018/01/13/multipath-routing-with-load-balancing-using-ryu-openflow-controller/>
def _configureECMP(self, la: 'LatencyApp'):
ofproto = self._datapath.ofproto
parser = self._datapath.ofproto_parser
prints = list()
for h1p, h1 in enumerate(network_topo[0]):
for h2p, h2 in enumerate(network_topo[0]):
if h1p != h2p:
ips = id2ip(int(h1[1:])-1)
ipd = id2ip(int(h2[1:])-1)
match = parser.OFPMatch(
eth_type=0x0800,
ipv4_src=ips,
ipv4_dst=ipd
)
valid_paths = filter_out_invalid_paths_from_multiple_paths(
h1,
h2,
pop_apa_candidates[h1p][h2p],
self._sw
)
portouts = list()
nexthops = list()
for path in valid_paths:
nexthop = (path[path.index(self._sw)+1], 1.0)
link = (self._sw, nexthop[0])
portout = self._links[link]
portouts.append(portout)
nexthops.append(nexthop[0])
portouts = list(set(portouts))
nexthops = list(set(nexthops))
prints.append(
f"({ips}) {self._sw} --{portouts}-> {set(nexthops)} ({ipd})"
)
if len(portouts) <= 0:
continue
# no output
elif len(portouts) == 1:
actions = [parser.OFPActionOutput(portouts[0])]
self.add_flow(7, match, actions)
# add simple rule
else:
all_bws = [x if x is not None else UNLIMITED_BANDWIDTH for x in [
network_topo[2][portout-1][2] for portout in portouts]]
sum_all_bws = sum(all_bws)
weighted_bws = [bw/sum_all_bws for bw in all_bws]
out_ports = list(zip(portouts, weighted_bws))
del weighted_bws
del sum_all_bws
del portouts
tpl = tuple([h1, self._sw, h2])
gnw = False
if tpl not in la.ecmp_group_ids:
la.ecmp_group_ids[tpl] = len(la.ecmp_group_ids)+1
gnw = True
gid = la.ecmp_group_ids[tpl]
buckets = []
for port, weight in out_ports:
bucket_weight = int(
round(
65535 * min(
1.0,
max(
0.0,
(1 - weight)
)
)
)
)
bucket_action = [parser.OFPActionOutput(port)]
buckets.append(
parser.OFPBucket(
weight=bucket_weight,
watch_port=port,
watch_group=ofproto.OFPG_ANY,
actions=bucket_action
)
)
la.ecmp_group_id_buckets[gid] = buckets
req = parser.OFPGroupMod(
self._datapath,
ofproto.OFPGC_ADD if gnw else ofproto.OFPGC_MODIFY,
ofproto.OFPGT_SELECT,
gid,
buckets
)
self._datapath.send_msg(req)
# set group
actions = [parser.OFPActionGroup(gid)]
self.add_flow(7, match, actions)
# add rule
print(os.linesep.join(prints))
pass
def add_latency_segment(self, ips: str, ipd: str, nxt: str, ignore_routing_algo: bool = False):
if (routing_algo not in ['ldr', 'minmax']) and not ignore_routing_algo:
return
link = (self._sw, nxt)
parser = self._datapath.ofproto_parser
actions = [parser.OFPActionOutput(self._links[link])]
match = parser.OFPMatch(
eth_type=0x0800,
ipv4_src=ips,
ipv4_dst=ipd
)
self.add_flow(5, match, actions)
# hs = f"h{ip2id(ips)+1}"
# hd = f"h{ip2id(ipd)+1}"
# self._flow_xfer[(hs, hd, nxt)] = 0
def add_weighted_latency(self, la: 'LatencyApp', ips: str, ipd: str, nxts: Dict[str, float]):
if len(nxts) <= 0:
return
elif len(nxts) == 1:
self.add_latency_segment(ips, ipd, list(nxts.keys())[0])
else:
ofproto = self._datapath.ofproto
parser = self._datapath.ofproto_parser
match = parser.OFPMatch(
eth_type=0x0800,
ipv4_src=ips,
ipv4_dst=ipd
)
h1 = f"h{ip2id(ips)+1}"
h2 = f"h{ip2id(ipd)+1}"
out_ports = [
(self._links[(self._sw, nexthop)], weight)
for nexthop, weight in nxts.items()
]
tpl = tuple([h1, self._sw, h2])
gnw = False
if tpl not in la.ecmp_group_ids:
la.ecmp_group_ids[tpl] = len(la.ecmp_group_ids)+1
gnw = True
gid = la.ecmp_group_ids[tpl]
buckets = []
for port, weight in out_ports:
bucket_weight = int(round(weight * 65535))
bucket_action = [parser.OFPActionOutput(port)]
buckets.append(
parser.OFPBucket(
weight=bucket_weight,
watch_port=port,
watch_group=ofproto.OFPG_ANY,
actions=bucket_action
)
)
la.ecmp_group_id_buckets[gid] = buckets
req = parser.OFPGroupMod(
self._datapath,
ofproto.OFPGC_ADD if gnw else ofproto.OFPGC_MODIFY,
ofproto.OFPGT_SELECT,
gid,
buckets
)
self._datapath.send_msg(req)
# set group
actions = [parser.OFPActionGroup(gid)]
self.add_flow(5, match, actions)
# add rule
@property
def simulatable(self):
return SimulatableSwitch(
self._sw,
self._flow_speed.copy(),
self._bandwidth.copy()
)
class SimulatableSwitch:
def __init__(self, sw: str, fs: dict, bw: 'UsageStore'):
self._sw = sw
self._flow_speed = fs
self._bandwidth = bw
self._link_speed = UsageStoreProxyFromFlowDict(self._flow_speed)
self._reinit()
def _reinit(self):
self._link_rules = self.get_link_rules()
def get_link_rules(self):
cnt = UsageStore()
for key, bw in self._flow_speed.items():
if bw > 0:
k = (key[1], key[2])
cnt[k] = 1+cnt[k]
return cnt
@property
def name(self):
return self._sw
def copy(self):
return type(self)(
self._sw,
self._flow_speed.copy(),
self._bandwidth.copy()
)
class SimulatableNetwork:
def __init__(self, sws: List[SimulatableSwitch], paths: 'UsageStore'):
self.switches: Dict[str, SimulatableSwitch] = {
sw._sw: sw
for sw in sws
}
self.max_speed = bandwidth_from_list(enumerate(network_topo[2]))
self.paths = paths
self._reinit()
def _reinit(self):
self._flow_speed = DictAggregator(
[sw._flow_speed for sw in self.switches.values()]
)
self.last_speed = UsageStoreProxyFromCallableIterable(
AttrCallableIterable(
AttrCallableIterable(
self.switches.values,
'_link_speed'
),
'as_regular_storage'
),
sum
).as_regular_storage
self.link_usage = self.max_speed.calculate(
self.last_speed.as_regular_storage,
lambda maxspeed, lastspeed: (
lastspeed / max(maxspeed, 0.0000000000001)
)
).as_regular_storage
self.link_flows = self.get_link_flows()
def get_link_flows(self):
cnt = UsageStore()
for switch in self.switches.values():
for (_, sw, nxt, __), speed in switch._flow_speed.items():
key = (sw, nxt)
if switch._bandwidth[key] > 0 and speed > 0:
cnt[key] = 1+cnt[key]
return cnt
def get_routes(self):
return self.paths.copy()
def get_path(self, h1, h2):
return self.paths[(h1, h2)]
def copy(self) -> 'SimulatableNetwork':
return type(self)(
[sw.copy() for sw in self.switches.values()],
self.paths.copy()
)
def sort_by_max_flow_load(self, seqs):
wl = list()
for seq in seqs:
if not isinstance(seq, WeightedPathAggregate):
seq = WeightedPathAggregate({tuple(seq): 1.0})
ml = self.get_max_flow_load(seq)
wl.append((
ml,
sorted(seq.keys(), key=lambda a: len(a))[0],
seq
))
return list(map(
lambda a: a[2],
reversed(sorted(wl))
))
def get_max_flow_load(self, wpa):
h1, h2 = _sort_pair(wpa[0], wpa[-1])
ml = 0
if not isinstance(wpa, WeightedPathAggregate):
wpa = WeightedPathAggregate({tuple(wpa): 1.0})
for seq, weight in wpa.items():
for i in range(len(seq)-1):
link = tuple(seq[i:i+2])
revlink = tuple(list(reversed(link)))
ml = max(
ml,
((
(
self._flow_speed.get(tuple([h1, *link, h2]), 0)
/
self.max_speed[link]
)+(
self._flow_speed.get(tuple([h2, *revlink, h1]), 0)
/
self.max_speed[revlink]
)
))*weight
)
return ml
def get_max_flow_speed(self, wpa):
h1, h2 = _sort_pair(wpa[0], wpa[-1])
if not isinstance(wpa, WeightedPathAggregate):
wpa = WeightedPathAggregate({tuple(wpa): 1.0})
ml = 0
for seq, weight in wpa.items():
for i in range(len(seq)-1):
link = tuple(seq[i:i+2])
revlink = tuple(list(reversed(link)))
ml = max(
ml,
((
self._flow_speed.get(tuple([h1, *link, h2]), 0)
+
self._flow_speed.get(tuple([h2, *revlink, h1]), 0)
))*weight
)
return ml
def get_max_path_load(self, wpa):
ml = 0
if not isinstance(wpa, WeightedPathAggregate):
wpa = WeightedPathAggregate({tuple(wpa): 1.0})
for seq in wpa.keys():
for i in range(len(seq)-1):
link = tuple(seq[i:i+2])
ml = max(
ml,
self.link_usage[link]
)
return ml
def with_modified_path(self, newpath):
mod = self.copy()
oldpath = self.get_path(newpath[0], newpath[-1])
flowspeed = self.get_max_flow_speed(oldpath)
# removing old flows
for sw in mod.switches.values():
for k in sw._flow_speed.copy().keys():
if (
k[0] in [newpath[0], newpath[-1]]
and
k[3] in [newpath[0], newpath[-1]]
):
del sw._flow_speed[k]
# adding new flows
if not isinstance(newpath, WeightedPathAggregate):
newpath = WeightedPathAggregate({tuple(newpath): 1.0})
for newpath2, weight in newpath.items():
for i in range(1, len(newpath2)-1):
ky = newpath2[0], newpath2[i], newpath2[i+1], newpath2[-1]
rk = newpath2[-1], newpath2[i-1], newpath2[i], newpath2[0]
if ky[1].startswith('s') and ky[1] in mod.switches:
mod.switches[ky[1]]._flow_speed[ky] = (weight*flowspeed)/2
if rk[1].startswith('s') and rk[1] in mod.switches:
mod.switches[rk[1]]._flow_speed[rk] = (weight*flowspeed)/2
for sw in mod.switches.values():
sw._reinit()
self.paths[tuple([newpath[0], newpath[-1]])] = newpath
mod._reinit()
return mod
def copy_normalized(self):
net = self.copy()
for path in net.get_routes()._pairs.values():
net = net.with_modified_path(path)
return net
# Gvozdiev et al @ SIGCOMM2018, p. 94 => Algorithm 1
def copy_scaling(self, prev_prediction: Dict[Tuple[str], float], fixed_hedge: float, decay_multiplier: float) -> Tuple['SimulatableNetwork', Dict[Tuple[str], float]]:
if prev_prediction is None:
prev_prediction = dict()
net = self.copy()
next_prediction = dict()
for sw in net.switches.values():
for idtf, prev_value in sw._flow_speed.items():
scaled_est = prev_value*fixed_hedge
if scaled_est > prev_prediction.get(idtf, 0):
next_prediction[idtf] = scaled_est
else:
decay_prediction = prev_prediction.get(
idtf, 0) * decay_multiplier
next_prediction[idtf] = max(decay_prediction, scaled_est)
for sw in net.switches.values():
for idtf in sw._flow_speed.keys():
sw._flow_speed[idtf] = next_prediction[idtf]
sw._reinit()
net._reinit()
return net, next_prediction
class AbstractPathEvaluator:
def __init__(self, simulatable_network: SimulatableNetwork, keepdata: Optional[Any]):
self._simnet: SimulatableNetwork = simulatable_network
self._keepdata = keepdata
def __call__(self) -> Tuple['UsageStore', NoneType]: pass
class NullPathEvaluator(AbstractPathEvaluator):
def __call__(self) -> Tuple['UsageStore', NoneType]:
return UsageStore(), None
class LDRSinglePathEvaluator(AbstractPathEvaluator):
def __call__(self) -> Tuple['UsageStore', NoneType]:
return self._figure13(self._simnet.copy()), None
# return self._figure12(
# self._simnet.link_usage,
# self._simnet.link_flows
# )
def _figure13(self, net: SimulatableNetwork) -> 'UsageStore':
net = net.copy_normalized()
_tmp = self._figure12(
net.link_usage,
net.link_flows
)
target = len(_tmp._pairs)
changed = UsageStore()
newnet = None
while len(changed._pairs) < target:
aggregate_paths = self._figure12(
net.link_usage,
net.link_flows
)
loaded_paths = net.sort_by_max_flow_load([
*aggregate_paths._pairs.values()
])
most_loaded = None
for loaded in loaded_paths:
if not changed.contains((loaded[0], loaded[-1])):
most_loaded = loaded
break
if most_loaded is None:
break
newnet = net.with_modified_path(most_loaded)
old_path_for_most_loaded = net.get_path(
most_loaded[0], most_loaded[-1])
if (
newnet.get_max_path_load(most_loaded)
<=
net.get_max_path_load(old_path_for_most_loaded)
):
net = newnet
changed[(most_loaded[0], most_loaded[-1])] = most_loaded
return net.get_routes()
def _figure12(self, link_usage, link_flows) -> 'UsageStore':
processed = UsageStore()
currently_reserved = UsageStore()
ospf = Dijkstra(network_graph)
l1 = network_topo[0][:]
random.shuffle(l1)
for h1 in l1:
l2 = network_topo[0][:]
random.shuffle(l2)
for h2 in l2:
if h1 != h2 and not processed.contains((h1, h2)):
h1p = network_topo[0].index(h1)
h2p = network_topo[0].index(h2)
ospf_aggregate = ospf(h1)(h2)[0]
ospf_delay = sum([
link_usage[
tuple([*ospf_aggregate[i:i+2]])
] + hop_delay
for i in range(len(ospf_aggregate)-1)
])
aggregates = list(pop_apa_candidates[h1p][h2p])
random.shuffle(aggregates)
del h1p
del h2p
min_aggregate = None
min_aggregate_val = None
min_aggregate_links = list()
nalist = list()
na = 0 # flows in aggregate
omax = 0 # maximum overload
for aggregate in aggregates:
links = [
_sort_pair(*aggregate[i:i+2])
for i in range(len(aggregate)-1)
]
omax2 = 0
for link in links:
omax2 = max(omax2, link_usage[link])
if link not in nalist:
na += link_flows[link]
na += currently_reserved[link]
nalist.append(link)
pass # fraction of aggregate on path
if omax2 > omax:
omax = omax2
# del nalist
# for aggregate in aggregates:
links = [
_sort_pair(*aggregate[i:i+2])
for i in range(len(aggregate)-1)
]
eps2 = 0
xap = 1
for link in links:
dp = link_usage[link]+hop_delay
pass # path delay
eps2 += xap*(dp+((dp*m1)/ospf_delay))
exp0 = m2*omax
eps3 = 0
eps3 += sum([link_usage[link] for link in links])
aggregate_val = na*eps2+exp0+eps3
if (
min_aggregate_val is None
or
min_aggregate_val > aggregate_val
):
min_aggregate_val = aggregate_val
min_aggregate = aggregate
min_aggregate_links = links
processed[(h1, h2)] = min_aggregate
for link in min_aggregate_links:
currently_reserved[link] = 1+currently_reserved[link]
return processed
class LDRPathEvaluator(AbstractPathEvaluator):
def __call__(self) -> Tuple['UsageStore', NoneType]:
net, kd = self._simnet.copy_scaling(self._keepdata, 1.1, 0.98)
return self._figure12(net.copy()), kd
def _figure13(self, net: SimulatableNetwork) -> 'UsageStore':
net = net.copy_normalized()
_tmp = self._figure12(net.copy())
target = len(_tmp._pairs)
changed = UsageStore()
newnet = None
while len(changed._pairs) < target:
aggregate_paths = self._figure12(net.copy())
loaded_paths = net.sort_by_max_flow_load([
*aggregate_paths._pairs.values()
])
most_loaded = None
for loaded in loaded_paths:
if not changed.contains((loaded[0], loaded[-1])):
most_loaded = loaded
break
if most_loaded is None:
break
newnet = net.with_modified_path(most_loaded)
old_path_for_most_loaded = net.get_path(
most_loaded[0], most_loaded[-1])
if (
newnet.get_max_path_load(most_loaded)
<=
net.get_max_path_load(old_path_for_most_loaded)
):
net = newnet
changed[(most_loaded[0], most_loaded[-1])] = most_loaded
return net.get_routes()
def _figure12(self, net: SimulatableNetwork) -> 'UsageStore':
link_usage = net.link_usage
link_flows = net.link_flows
ospf = Dijkstra(network_graph)
opt_model = pulp.LpProblem("LDR", pulp.LpMinimize)
xap = pulp.LpVariable("xa", 0, 1)
processed = UsageStore(default=dict())
l1 = network_topo[0][:]
random.shuffle(l1)
sumaggregates = 0
for h1 in l1:
l2 = network_topo[0][:]
random.shuffle(l2)
for h2 in l2:
if h1 != h2 and not processed.contains((h1, h2)):
h1, h2 = _sort_pair(h1, h2)
opt_vars = dict()
processed[(h1, h2)] = opt_vars
h1p = network_topo[0].index(h1)
h2p = network_topo[0].index(h2)
ospf_aggregate = ospf(h1)(h2)[0]
ospf_delay = sum([
link_usage[
tuple([*ospf_aggregate[i:i+2]])
] + hop_delay
for i in range(len(ospf_aggregate)-1)
])
aggregates = list(pop_apa_candidates[h1p][h2p])[:]
for aggregate in aggregates[:]:
links = [
_sort_pair(*aggregate[i:i+2])
for i in range(len(aggregate)-1)
]
dp = sum(
[link_usage[link]+hop_delay for link in links])
if (ospf_delay == 0) or ((dp/ospf_delay) > apa_path_stretch):
del aggregates[aggregates.index(aggregate)]
random.shuffle(aggregates)
sumpinpa = 0
sumxappinpa = 0
na = sum([
link_flows[link]
for aggregate in aggregates
for link in [
_sort_pair(*aggregate[i:i+2])
for i in range(len(aggregate)-1)
]
])
na = max(na, MIN_POSITIVE_FLOAT)
for aggregate in aggregates:
links = [
_sort_pair(*aggregate[i:i+2])
for i in range(len(aggregate)-1)
]
xap = pulp.LpVariable(
f"xap_{h1}_{h2}__{'_'.join(aggregate)}", 0, 1)
opt_vars[tuple(aggregate)] = xap
omax = max(0, *[link_usage[link] for link in links])
olinks = sum([link_usage[link] for link in links])
dp = sum(
[link_usage[link]+hop_delay for link in links])
tiebreaker = (dp+((dp*m1)/ospf_delay))
sumxappinpa += xap
sumpinpa += xap*(tiebreaker+(m2*omax)+olinks)
opt_model += 0 <= xap <= 1
opt_model += sumxappinpa == 1
sumaggregates += na*sumpinpa
opt_model += sumaggregates
opt_model.solve()
solution = {
(tuple(k[4:].split('__')[0].split('_')), tuple(k[4:].split('__')[1].split('_'))):
v.varValue
for k, v in opt_model.variablesDict().items()
if k.startswith('xap_')
}
# print(opt_model)
d = dict()
for (tpl, path), weight in solution.items():
if tpl not in d:
d[tpl] = dict()
d[tpl][path] = weight
weighted_result = UsageStore(initial={
k: WeightedPathAggregate(v)
for k, v in d.items()
})
# print(weighted_result)
# opt_model.solve() # solving with CBC
# opt_model.solve(solver=pulp.GLPK_CMD()) # solving with Glpk
return weighted_result
class MinMaxSinglePathEvaluator(AbstractPathEvaluator):
def __call__(self) -> Tuple['UsageStore', NoneType]:
return self.minmax(self._simnet.copy()), None
def minmax(self, net: SimulatableNetwork) -> 'UsageStore':
net = net.copy_normalized()
processed = UsageStore()
l1 = network_topo[0][:]
random.shuffle(l1)
for h1 in l1:
l2 = network_topo[0][:]
random.shuffle(l2)
for h2 in l2:
if h1 != h2 and not processed.contains((h1, h2)):
h1p = network_topo[0].index(h1)
h2p = network_topo[0].index(h2)
aggregates = list(pop_apa_candidates[h1p][h2p])
configurations = list()
for aggregate in aggregates:
links = [
_sort_pair(*aggregate[i:i+2])
for i in range(len(aggregate)-1)
]
newnet = net.with_modified_path(aggregate)
configurations.append((
newnet.get_max_path_load(aggregate),
len(aggregate),
aggregate,
newnet
))
configurations.sort()
configuration = configurations[0]
processed[(h1, h2)] = configuration[2]
net = configuration[3]
return processed
path_evaluators = {
'ospf': NullPathEvaluator,
'ldr-single': LDRSinglePathEvaluator,
'minmax-single': MinMaxSinglePathEvaluator,
'ecmp': NullPathEvaluator,
'ldr': LDRPathEvaluator,
}
PathEvaluator = path_evaluators[routing_algo]
ppe = ProcessPoolExecutor(1)
last_process = None
last_process_lock = threading.Lock()
initialized_switch_writing_lock = threading.Lock()
class LatencyApp(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mac_to_port = {}
self.controllers = {}
self._paths = host_path_from_list(network_topo[0])
self._loads = usage_store_from_list(enumerate([]))
self.max_speed = bandwidth_from_list(enumerate(network_topo[2]))
self.last_speed = UsageStoreProxyFromCallableIterable(