forked from intrig-unicamp/mininet-wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.py
More file actions
1186 lines (1022 loc) · 44.1 KB
/
Copy pathlink.py
File metadata and controls
1186 lines (1022 loc) · 44.1 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
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
"""
link.py: interface and link abstractions for mininet
It seems useful to bundle functionality for interfaces into a single
class.
Also it seems useful to enable the possibility of multiple flavors of
links, including:
- simple veth pairs
- tunneled links
- patchable links (which can be disconnected and reconnected via a patchbay)
- link simulators (e.g. wireless)
Basic division of labor:
Nodes: know how to execute commands
Intfs: know how to configure themselves
Links: know how to connect nodes together
Intf: basic interface object that can configure itself
TCIntf: interface with bandwidth limiting and delay via tc
Link: basic link class for creating veth pairs
"""
import re
from mininet.log import info, error, debug
from mininet.util import makeIntfPair
class Intf(object):
"Basic interface object that can configure itself."
def __init__(self, name, node=None, port=None, link=None,
mac=None, **params):
"""name: interface name (e.g. h1-eth0)
node: owning node (where this intf most likely lives)
link: parent link if we're part of a link
other arguments are passed to config()"""
self.node = node
self.name = name
self.link = link
self.port = port
self.mac = mac
self.ip, self.prefixLen = None, None
# if interface is lo, we know the ip is 127.0.0.1.
# This saves an ifconfig command per node
if self.name == 'lo':
self.ip = '127.0.0.1'
# Add to node (and move ourselves if necessary )
if node:
moveIntfFn = params.pop('moveIntfFn', None)
if moveIntfFn:
node.addIntf(self, port=port, moveIntfFn=moveIntfFn)
else:
node.addIntf(self, port=port)
# Save params for future reference
self.params = params
self.config(**params)
def cmd(self, *args, **kwargs):
"Run a command in our owning node"
return self.node.cmd(*args, **kwargs)
def ifconfig(self, *args):
"Configure ourselves using ifconfig"
return self.cmd('ifconfig', self.name, *args)
def setIP(self, ipstr, prefixLen=None):
"""Set our IP address"""
# This is a sign that we should perhaps rethink our prefix
# mechanism and/or the way we specify IP addresses
if '/' in ipstr:
self.ip, self.prefixLen = ipstr.split('/')
return self.ifconfig(ipstr, 'up')
else:
if prefixLen is None:
raise Exception('No prefix length set for IP address %s'
% (ipstr,))
self.ip, self.prefixLen = ipstr, prefixLen
return self.ifconfig('%s/%s' % (ipstr, prefixLen))
def setMAC(self, macstr):
"""Set the MAC address for an interface.
macstr: MAC address as string"""
self.mac = macstr
return (self.ifconfig('down') +
self.ifconfig('hw', 'ether', macstr) +
self.ifconfig('up'))
_ipMatchRegex = re.compile(r'\d+\.\d+\.\d+\.\d+')
_macMatchRegex = re.compile(r'..:..:..:..:..:..')
def updateIP(self):
"Return updated IP address based on ifconfig"
# use pexec instead of node.cmd so that we dont read
# backgrounded output from the cli.
ifconfig, _err, _exitCode = self.node.pexec(
'ifconfig %s' % self.name)
ips = self._ipMatchRegex.findall(ifconfig)
self.ip = ips[ 0 ] if ips else None
return self.ip
def updateMAC(self):
"Return updated MAC address based on ifconfig"
ifconfig = self.ifconfig()
macs = self._macMatchRegex.findall(ifconfig)
self.mac = macs[ 0 ] if macs else None
return self.mac
# Instead of updating ip and mac separately,
# use one ifconfig call to do it simultaneously.
# This saves an ifconfig command, which improves performance.
def updateAddr(self):
"Return IP address and MAC address based on ifconfig."
ifconfig = self.ifconfig()
ips = self._ipMatchRegex.findall(ifconfig)
macs = self._macMatchRegex.findall(ifconfig)
self.ip = ips[ 0 ] if ips else None
self.mac = macs[ 0 ] if macs else None
return self.ip, self.mac
def IP(self):
"Return IP address"
return self.ip
def MAC(self):
"Return MAC address"
return self.mac
def isUp(self, setUp=False):
"Return whether interface is up"
if setUp:
cmdOutput = self.ifconfig('up')
# no output indicates success
if cmdOutput:
# error( "Error setting %s up: %s " % ( self.name, cmdOutput ) )
return False
else:
return True
else:
return "UP" in self.ifconfig()
def rename(self, newname):
"Rename interface"
self.ifconfig('down')
result = self.cmd('ip link set', self.name, 'name', newname)
self.name = newname
self.ifconfig('up')
return result
# The reason why we configure things in this way is so
# That the parameters can be listed and documented in
# the config method.
# Dealing with subclasses and superclasses is slightly
# annoying, but at least the information is there!
def setParam(self, results, method, **param):
"""Internal method: configure a *single* parameter
results: dict of results to update
method: config method name
param: arg=value (ignore if value=None)
value may also be list or dict"""
name, value = param.items()[ 0 ]
f = getattr(self, method, None)
if not f or value is None:
return
if isinstance(value, list):
result = f(*value)
elif isinstance(value, dict):
result = f(**value)
else:
result = f(value)
results[ name ] = result
return result
def config(self, mac=None, ip=None, ifconfig=None, up=True, **_params):
"""Configure Node according to (optional) parameters:
mac: MAC address
ip: IP address
ifconfig: arbitrary interface configuration
Subclasses should override this method and call
the parent class's config(**params)"""
# If we were overriding this method, we would call
# the superclass config method here as follows:
# r = Parent.config( **params )
r = {}
self.setParam(r, 'setMAC', mac=mac)
self.setParam(r, 'setIP', ip=ip)
self.setParam(r, 'isUp', up=up)
self.setParam(r, 'ifconfig', ifconfig=ifconfig)
return r
def delete(self):
"Delete interface"
self.cmd('ip link del ' + self.name)
# We used to do this, but it slows us down:
# if self.node.inNamespace:
# Link may have been dumped into root NS
# quietRun( 'ip link del ' + self.name )
self.node.delIntf(self)
self.link = None
def status(self):
"Return intf status as a string"
links, _err, _result = self.node.pexec('ip link show')
if self.name in links:
return "OK"
else:
return "MISSING"
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self.name)
def __str__(self):
return self.name
class IntfWireless(object):
"Basic interface object that can configure itself."
def __init__(self, name, node=None, port=None, link=None,
mac=None, tc=False, **params):
"""name: interface name (e.g. h1-eth0)
node: owning node (where this intf most likely lives)
link: parent link if we're part of a link
other arguments are passed to config()"""
self.node = node
self.name = name
self.link = link
self.port = port
self.mac = mac
self.ip, self.prefixLen = None, None
# if interface is lo, we know the ip is 127.0.0.1.
# This saves an ifconfig command per node
if self.name == 'lo':
self.ip = '127.0.0.1'
# Add to node (and move ourselves if necessary )
moveIntfFn = params.pop('moveIntfFn', None)
if tc == False:
if moveIntfFn:
node.addIntf(self, port=port, moveIntfFn=moveIntfFn)
else:
node.addIntf(self, port=port)
# Save params for future reference
self.params = params
self.config(**params)
def cmd(self, *args, **kwargs):
"Run a command in our owning node"
return self.node.cmd(*args, **kwargs)
def ifconfig(self, *args):
"Configure ourselves using ifconfig"
return self.cmd('ifconfig', self.name, *args)
def setIP(self, ipstr, prefixLen=None):
"""Set our IP address"""
# This is a sign that we should perhaps rethink our prefix
# mechanism and/or the way we specify IP addresses
if '/' in ipstr:
self.ip, self.prefixLen = ipstr.split('/')
return self.ifconfig(ipstr, 'up')
else:
if prefixLen is None:
raise Exception('No prefix length set for IP address %s'
% (ipstr,))
self.ip, self.prefixLen = ipstr, prefixLen
return self.ifconfig('%s/%s' % (ipstr, prefixLen))
def setMAC(self, macstr):
"""Set the MAC address for an interface.
macstr: MAC address as string"""
self.mac = macstr
return (self.ifconfig('down') +
self.ifconfig('hw', 'ether', macstr) +
self.ifconfig('up'))
_ipMatchRegex = re.compile(r'\d+\.\d+\.\d+\.\d+')
_macMatchRegex = re.compile(r'..:..:..:..:..:..')
def updateIP(self):
"Return updated IP address based on ifconfig"
# use pexec instead of node.cmd so that we dont read
# backgrounded output from the cli.
ifconfig, _err, _exitCode = self.node.pexec(
'ifconfig %s' % self.name)
ips = self._ipMatchRegex.findall(ifconfig)
self.ip = ips[ 0 ] if ips else None
return self.ip
def updateMAC(self):
"Return updated MAC address based on ifconfig"
ifconfig = self.ifconfig()
macs = self._macMatchRegex.findall(ifconfig)
self.mac = macs[ 0 ] if macs else None
return self.mac
# Instead of updating ip and mac separately,
# use one ifconfig call to do it simultaneously.
# This saves an ifconfig command, which improves performance.
def updateAddr(self):
"Return IP address and MAC address based on ifconfig."
ifconfig = self.ifconfig()
ips = self._ipMatchRegex.findall(ifconfig)
macs = self._macMatchRegex.findall(ifconfig)
self.ip = ips[ 0 ] if ips else None
self.mac = macs[ 0 ] if macs else None
return self.ip, self.mac
def IP(self):
"Return IP address"
return self.ip
def MAC(self):
"Return MAC address"
return self.mac
def isUp(self, setUp=False):
"Return whether interface is up"
if setUp:
cmdOutput = self.ifconfig('up')
# no output indicates success
if cmdOutput:
# error( "Error setting %s up: %s " % ( self.name, cmdOutput ) )
return False
else:
return True
else:
return "UP" in self.ifconfig()
def rename(self, newname):
"Rename interface"
self.ifconfig('down')
result = self.cmd('ip link set', self.name, 'name', newname)
self.name = newname
self.ifconfig('up')
return result
# The reason why we configure things in this way is so
# That the parameters can be listed and documented in
# the config method.
# Dealing with subclasses and superclasses is slightly
# annoying, but at least the information is there!
def setParam(self, results, method, **param):
"""Internal method: configure a *single* parameter
results: dict of results to update
method: config method name
param: arg=value (ignore if value=None)
value may also be list or dict"""
name, value = param.items()[ 0 ]
f = getattr(self, method, None)
if not f or value is None:
return
if isinstance(value, list):
result = f(*value)
elif isinstance(value, dict):
result = f(**value)
else:
result = f(value)
results[ name ] = result
return result
def config(self, mac=None, ip=None, ifconfig=None, up=True, **_params):
"""Configure Node according to (optional) parameters:
mac: MAC address
ip: IP address
ifconfig: arbitrary interface configuration
Subclasses should override this method and call
the parent class's config(**params)"""
# If we were overriding this method, we would call
# the superclass config method here as follows:
# r = Parent.config( **params )
r = {}
self.setParam(r, 'setMAC', mac=mac)
self.setParam(r, 'setIP', ip=ip)
self.setParam(r, 'isUp', up=up)
self.setParam(r, 'ifconfig', ifconfig=ifconfig)
return r
def delete(self):
"Delete interface"
self.cmd('ip link del ' + self.name)
# We used to do this, but it slows us down:
# if self.node.inNamespace:
# Link may have been dumped into root NS
# quietRun( 'ip link del ' + self.name )
self.node.delIntf(self)
self.link = None
def status(self):
"Return intf status as a string"
links, _err, _result = self.node.pexec('ip link show')
if self.name in links:
return "OK"
else:
return "MISSING"
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self.name)
def __str__(self):
return self.name
class TCIntfWireless(IntfWireless):
"""Interface customized by tc (traffic control) utility
Allows specification of bandwidth limits (various methods)
as well as delay, loss and max queue length"""
# The parameters we use seem to work reasonably up to 1 Gb/sec
# For higher data rates, we will probably need to change them.
bwParamMax = 1000
def bwCmds(self, bw=None, speedup=0, use_hfsc=False, use_tbf=False,
latency_ms=None, enable_ecn=False, enable_red=False):
"Return tc commands to set bandwidth"
cmds, parent = [], ' root '
if bw and (bw < 0 or bw > self.bwParamMax):
error('Bandwidth limit', bw, 'is outside supported range 0..%d'
% self.bwParamMax, '- ignoring\n')
elif bw is not None:
# BL: this seems a bit brittle...
if (speedup > 0):
bw = speedup
# This may not be correct - we should look more closely
# at the semantics of burst (and cburst) to make sure we
# are specifying the correct sizes. For now I have used
# the same settings we had in the mininet-hifi code.
if use_hfsc:
cmds += [ '%s qdisc add dev %s root handle 5:0 hfsc default 1',
'%s class add dev %s parent 5:0 classid 5:1 hfsc sc '
+ 'rate %fMbit ul rate %fMbit' % (bw, bw) ]
elif use_tbf:
if latency_ms is None:
latency_ms = 15 * 8 / bw
cmds += [ '%s qdisc add dev %s root handle 5: tbf ' +
'rate %fMbit burst 15000 latency %fms' %
(bw, latency_ms) ]
else:
cmds += [ '%s qdisc add dev %s root handle 5:0 htb default 1',
'%s class add dev %s parent 5:0 classid 5:1 htb ' +
'rate %fMbit burst 15k' % bw ]
parent = ' parent 5:1 '
# ECN or RED
if enable_ecn:
cmds += [ '%s qdisc add dev %s' + parent +
'handle 6: red limit 1000000 ' +
'min 30000 max 35000 avpkt 1500 ' +
'burst 20 ' +
'bandwidth %fmbit probability 1 ecn' % bw ]
parent = ' parent 6: '
elif enable_red:
cmds += [ '%s qdisc add dev %s' + parent +
'handle 6: red limit 1000000 ' +
'min 30000 max 35000 avpkt 1500 ' +
'burst 20 ' +
'bandwidth %fmbit probability 1' % bw ]
parent = ' parent 6: '
return cmds, parent
@staticmethod
def delayCmds(parent, delay=None, jitter=None,
loss=None, max_queue_size=None):
"Internal method: return tc commands for delay and loss"
cmds = []
if delay and delay < 0:
error('Negative delay', delay, '\n')
elif jitter and jitter < 0:
error('Negative jitter', jitter, '\n')
elif loss and (loss < 0 or loss > 100):
error('Bad loss percentage', loss, '%%\n')
else:
# Delay/jitter/loss/max queue size
netemargs = '%s%s%s%s' % (
'delay %s ' % delay if delay is not None else '',
'%s ' % jitter if jitter is not None else '',
'loss %.5f ' % loss if loss is not None else '',
'limit %d' % max_queue_size if max_queue_size is not None
else '')
if netemargs:
cmds = [ '%s qdisc add dev %s ' + parent +
' handle 10: netem ' +
netemargs ]
parent = ' parent 10:1 '
return cmds, parent
def tc(self, cmd, tc='tc'):
"Execute tc command for our interface"
c = cmd % (tc, self) # Add in tc command and our name
debug(" *** executing command: %s\n" % c)
return self.cmd(c)
def config(self, bw=None, delay=None, jitter=None, loss=None,
gro=False, txo=True, rxo=True,
speedup=0, use_hfsc=False, use_tbf=False,
latency_ms=None, enable_ecn=False, enable_red=False,
max_queue_size=None, **params):
"""Configure the port and set its properties.
bw: bandwidth in b/s (e.g. '10m')
delay: transmit delay (e.g. '1ms' )
jitter: jitter (e.g. '1ms')
loss: loss (e.g. '1%' )
gro: enable GRO (False)
txo: enable transmit checksum offload (True)
rxo: enable receive checksum offload (True)
speedup: experimental switch-side bw option
use_hfsc: use HFSC scheduling
use_tbf: use TBF scheduling
latency_ms: TBF latency parameter
enable_ecn: enable ECN (False)
enable_red: enable RED (False)
max_queue_size: queue limit parameter for netem"""
# Support old names for parameters
gro = not params.pop('disable_gro', not gro)
result = IntfWireless.config(self, **params)
def on(isOn):
"Helper method: bool -> 'on'/'off'"
return 'on' if isOn else 'off'
# Set offload parameters with ethool
self.cmd('ethtool -K', self,
'gro', on(gro),
'tx', on(txo),
'rx', on(rxo))
# Optimization: return if nothing else to configure
# Question: what happens if we want to reset things?
if (bw is None and not delay and not loss
and max_queue_size is None):
return
# Clear existing configuration
tcoutput = self.tc('%s qdisc show dev %s')
if "priomap" not in tcoutput and "noqueue" not in tcoutput:
cmds = [ '%s qdisc del dev %s root' ]
else:
cmds = []
# Bandwidth limits via various methods
bwcmds, parent = self.bwCmds(bw=bw, speedup=speedup,
use_hfsc=use_hfsc, use_tbf=use_tbf,
latency_ms=latency_ms,
enable_ecn=enable_ecn,
enable_red=enable_red)
cmds += bwcmds
# Delay/jitter/loss/max_queue_size using netem
delaycmds, parent = self.delayCmds(delay=delay, jitter=jitter,
loss=loss,
max_queue_size=max_queue_size,
parent=parent)
cmds += delaycmds
# Execute all the commands in our node
debug("at map stage w/cmds: %s\n" % cmds)
tcoutputs = [ self.tc(cmd) for cmd in cmds ]
for output in tcoutputs:
if output != '':
error("*** Error: %s" % output)
debug("cmds:", cmds, '\n')
debug("outputs:", tcoutputs, '\n')
result[ 'tcoutputs'] = tcoutputs
result[ 'parent' ] = parent
return result
class TCIntf(Intf):
"""Interface customized by tc (traffic control) utility
Allows specification of bandwidth limits (various methods)
as well as delay, loss and max queue length"""
# The parameters we use seem to work reasonably up to 1 Gb/sec
# For higher data rates, we will probably need to change them.
bwParamMax = 1000
def bwCmds(self, bw=None, speedup=0, use_hfsc=False, use_tbf=False,
latency_ms=None, enable_ecn=False, enable_red=False):
"Return tc commands to set bandwidth"
cmds, parent = [], ' root '
if bw and (bw < 0 or bw > self.bwParamMax):
error('Bandwidth limit', bw, 'is outside supported range 0..%d'
% self.bwParamMax, '- ignoring\n')
elif bw is not None:
# BL: this seems a bit brittle...
if (speedup > 0):
bw = speedup
# This may not be correct - we should look more closely
# at the semantics of burst (and cburst) to make sure we
# are specifying the correct sizes. For now I have used
# the same settings we had in the mininet-hifi code.
if use_hfsc:
cmds += [ '%s qdisc add dev %s root handle 5:0 hfsc default 1',
'%s class add dev %s parent 5:0 classid 5:1 hfsc sc '
+ 'rate %fMbit ul rate %fMbit' % (bw, bw) ]
elif use_tbf:
if latency_ms is None:
latency_ms = 15.0 * 8 / bw
cmds += [ '%s qdisc add dev %s root handle 5: tbf ' +
'rate %fMbit burst 15000 latency %fms' %
(bw, latency_ms) ]
else:
cmds += [ '%s qdisc add dev %s root handle 5:0 htb default 1',
'%s class add dev %s parent 5:0 classid 5:1 htb ' +
'rate %fMbit burst 15k' % bw ]
parent = ' parent 5:1 '
# ECN or RED
if enable_ecn:
cmds += [ '%s qdisc add dev %s' + parent +
'handle 6: red limit 1000000 ' +
'min 30000 max 35000 avpkt 1500 ' +
'burst 20 ' +
'bandwidth %fmbit probability 1 ecn' % bw ]
parent = ' parent 6: '
elif enable_red:
cmds += [ '%s qdisc add dev %s' + parent +
'handle 6: red limit 1000000 ' +
'min 30000 max 35000 avpkt 1500 ' +
'burst 20 ' +
'bandwidth %fmbit probability 1' % bw ]
parent = ' parent 6: '
return cmds, parent
@staticmethod
def delayCmds(parent, delay=None, jitter=None,
loss=None, max_queue_size=None):
"Internal method: return tc commands for delay and loss"
cmds = []
if delay and delay < 0:
error('Negative delay', delay, '\n')
elif jitter and jitter < 0:
error('Negative jitter', jitter, '\n')
elif loss and (loss < 0 or loss > 100):
error('Bad loss percentage', loss, '%%\n')
else:
# Delay/jitter/loss/max queue size
netemargs = '%s%s%s%s' % (
'delay %s ' % delay if delay is not None else '',
'%s ' % jitter if jitter is not None else '',
'loss %.5f ' % loss if loss is not None else '',
'limit %d' % max_queue_size if max_queue_size is not None
else '')
if netemargs:
cmds = [ '%s qdisc add dev %s ' + parent +
' handle 10: netem ' +
netemargs ]
parent = ' parent 10:1 '
return cmds, parent
def tc(self, cmd, tc='tc'):
"Execute tc command for our interface"
c = cmd % (tc, self) # Add in tc command and our name
debug(" *** executing command: %s\n" % c)
return self.cmd(c)
def config( self, bw=None, delay=None, jitter=None, loss=None,
gro=False, txo=True, rxo=True,
speedup=0, use_hfsc=False, use_tbf=False,
latency_ms=None, enable_ecn=False, enable_red=False,
max_queue_size=None, **params ):
"""Configure the port and set its properties.
bw: bandwidth in b/s (e.g. '10m')
delay: transmit delay (e.g. '1ms' )
jitter: jitter (e.g. '1ms')
loss: loss (e.g. '1%' )
gro: enable GRO (False)
txo: enable transmit checksum offload (True)
rxo: enable receive checksum offload (True)
speedup: experimental switch-side bw option
use_hfsc: use HFSC scheduling
use_tbf: use TBF scheduling
latency_ms: TBF latency parameter
enable_ecn: enable ECN (False)
enable_red: enable RED (False)
max_queue_size: queue limit parameter for netem"""
# Support old names for parameters
gro = not params.pop( 'disable_gro', not gro )
result = Intf.config( self, **params)
def on( isOn ):
"Helper method: bool -> 'on'/'off'"
return 'on' if isOn else 'off'
# Set offload parameters with ethool
self.cmd( 'ethtool -K', self,
'gro', on( gro ),
'tx', on( txo ),
'rx', on( rxo ) )
# Optimization: return if nothing else to configure
# Question: what happens if we want to reset things?
if ( bw is None and not delay and not loss
and max_queue_size is None ):
return
# Clear existing configuration
tcoutput = self.tc( '%s qdisc show dev %s' )
if "priomap" not in tcoutput and "noqueue" not in tcoutput:
cmds = [ '%s qdisc del dev %s root' ]
else:
cmds = []
# Bandwidth limits via various methods
bwcmds, parent = self.bwCmds( bw=bw, speedup=speedup,
use_hfsc=use_hfsc, use_tbf=use_tbf,
latency_ms=latency_ms,
enable_ecn=enable_ecn,
enable_red=enable_red )
cmds += bwcmds
# Delay/jitter/loss/max_queue_size using netem
delaycmds, parent = self.delayCmds( delay=delay, jitter=jitter,
loss=loss,
max_queue_size=max_queue_size,
parent=parent )
cmds += delaycmds
# Ugly but functional: display configuration info
stuff = ( ( [ '%.2fMbit' % bw ] if bw is not None else [] ) +
( [ '%s delay' % delay ] if delay is not None else [] ) +
( [ '%s jitter' % jitter ] if jitter is not None else [] ) +
( ['%.5f%% loss' % loss ] if loss is not None else [] ) +
( [ 'ECN' ] if enable_ecn else [ 'RED' ]
if enable_red else [] ) )
info( '(' + ' '.join( stuff ) + ') ' )
# Execute all the commands in our node
debug("at map stage w/cmds: %s\n" % cmds)
tcoutputs = [ self.tc(cmd) for cmd in cmds ]
for output in tcoutputs:
if output != '':
error( "*** Error: %s" % output )
debug( "cmds:", cmds, '\n' )
debug( "outputs:", tcoutputs, '\n' )
result[ 'tcoutputs'] = tcoutputs
result[ 'parent' ] = parent
return result
class _4addrLink(object):
def __init__(self, node1, node2, intf=Intf):
"""Create WDS link to another node.
node1: first node
node2: second node
intf: default interface class/constructor
"""
intf1 = None
intf2 = None
cls = intf
ap = node1
client = node2
client_intfName = '%s.wds' % client.name
if node1.params['_4addr'] == 'client':
client = node1
ap = node2
client_intfName = '%s.wds' % node1.name
if client_intfName not in client.params['wlan']:
self.add4addrIface(client, client_intfName)
client.params['mac'].append(client.params['mac'][0][:3] + '09' + client.params['mac'][0][5:])
self.setMac(client)
self.bring4addrIfaceUP(client)
ap.params['mode'].append(ap.params['mode'][0])
ap.params['channel'].append(ap.params['channel'][0])
ap.params['frequency'].append(ap.params['frequency'][0])
ap.params['txpower'].append(14)
ap.params['antennaGain'].append(ap.params['antennaGain'][0])
client.params['mode'].append(node1.params['mode'][0])
client.params['channel'].append(client.params['channel'][0])
client.params['frequency'].append(client.params['frequency'][0])
client.params['txpower'].append(14)
client.params['antennaGain'].append(client.params['antennaGain'][0])
client.params['wlan'].append(client_intfName)
client.cmd('iwconfig %s essid %s ap %s' % (client.params['wlan'][1],
ap.params['ssid'][0], ap.params['mac'][0]))
params1 = {}
params2 = {}
params1[ 'port' ] = client.newPort()
params2[ 'port' ] = ap.newPort()
intf1 = cls(name=client_intfName, node=client, link=self, **params1)
if hasattr(ap, 'wds'):
ap.wds += 1
else:
ap.wds = 1
intfName2 = ap.params['wlan'][0] + '.sta%s' % ap.wds
intf2 = cls(name=intfName2, node=ap, link=self, **params2)
ap.params['wlan'].append(intfName2)
ap.params['mac'].append(ap.params['mac'][0])
# All we are is dust in the wind, and our two interfaces
self.intf1, self.intf2 = intf1, intf2
def bring4addrIfaceUP(self, node):
node.cmd('ip link set dev %s.wds up' % node.name)
def setMac(self, node):
node.cmd('ip link set dev %s.wds addr %s' % (node.name, node.params['mac'][1]))
def add4addrIface(self, node, intfName):
node.cmd('iw dev %s interface add %s type managed 4addr on' % (node.params['wlan'][0], intfName))
class WirelessLinkAP(object):
"""A basic link is just a veth pair.
Other types of links could be tunnels, link emulators, etc.."""
# pylint: disable=too-many-branches
def __init__(self, node1, port1=None,
intfName1=None, addr1=None,
intf=Intf, cls1=None, params1=None):
"""Create veth link to another node, making two new interfaces.
node1: first node
port1: node1 port number (optional)
intf: default interface class/constructor
cls1: optional interface-specific constructors
intfName1: node1 interface name (optional)
params1: parameters for interface 1"""
# This is a bit awkward; it seems that having everything in
# params is more orthogonal, but being able to specify
# in-line arguments is more convenient! So we support both.
if params1 is None:
params1 = {}
if port1 is not None:
params1[ 'port' ] = port1
ifacename = 'wlan'
if 'port' not in params1:
if intfName1 == None:
nodelen = int(len(node1.params['wlan']))
currentlen = node1.wlanports
if nodelen > currentlen + 1:
params1[ 'port' ] = node1.newPort()
else:
params1[ 'port' ] = currentlen
intfName1 = self.wlanName(node1, ifacename, params1[ 'port' ])
intf1 = cls1(name=intfName1, node=node1,
link=self, mac=addr1, **params1)
else:
params1[ 'port' ] = node1.newPort()
node1.newPort()
intf1 = cls1(name=intfName1, node=node1,
link=self, mac=addr1, **params1)
else:
intfName1 = self.wlanName(node1, ifacename, params1[ 'port' ])
intf1 = cls1(name=intfName1, node=node1,
link=self, mac=addr1, **params1)
if not intfName1:
intfName1 = self.wlanName(node1, ifacename, node1.newWlanPort())
if not cls1:
cls1 = intf
intf2 = 'wireless'
# All we are is dust in the wind, and our two interfaces
self.intf1, self.intf2 = intf1, intf2
# pylint: enable=too-many-branches
@staticmethod
def _ignore(*args, **kwargs):
"Ignore any arguments"
pass
def wlanName(self, node, ifacename, n):
"Construct a canonical interface name node-ethN for interface n."
# Leave this as an instance method for now
assert self
if 'phywlan' in node.params: # if physical Interface
return ifacename + repr(n)
else:
return node.name + '-' + ifacename + repr(n)
def delete(self):
"Delete this link"
self.intf1.delete()
self.intf1 = None
self.intf2.delete()
self.intf2 = None
def stop(self):
"Override to stop and clean up link as needed"
self.delete()
def status(self):
"Return link status as a string"
return "(%s %s)" % (self.intf1.status(), self.intf2.status())
def __str__(self):
return '%s<->%s' % (self.intf1, self.intf2)
class WirelessLinkStation(object):
"""A basic link is just a veth pair.
Other types of links could be tunnels, link emulators, etc.."""
# pylint: disable=too-many-branches
def __init__(self, node1, port1=None,
intfName1=None, addr1=None,
intf=Intf, cls1=None, params1=None):
"""Create veth link to another node, making two new interfaces.
node1: first node
port1: node1 port number (optional)
intf: default interface class/constructor
cls1: optional interface-specific constructors
intfName1: node1 interface name (optional)
params1: parameters for interface 1"""
# This is a bit awkward; it seems that having everything in
# params is more orthogonal, but being able to specify
# in-line arguments is more convenient! So we support both.
if params1 is None:
params1 = {}
if port1 is not None:
params1[ 'port' ] = port1
if 'port' not in params1:
params1[ 'port' ] = node1.newPort()
if not intfName1:
ifacename = 'wlan'
intfName1 = self.wlanName(node1, ifacename, node1.newWlanPort())
if not cls1:
cls1 = intf
intf1 = cls1(name=intfName1, node=node1,
link=self, mac=addr1, **params1)
intf2 = 'wireless'
# All we are is dust in the wind, and our two interfaces
self.intf1, self.intf2 = intf1, intf2
# pylint: enable=too-many-branches
@staticmethod
def _ignore(*args, **kwargs):
"Ignore any arguments"
pass
def wlanName(self, node, ifacename, n):
"Construct a canonical interface name node-ethN for interface n."
# Leave this as an instance method for now
assert self
if 'phywlan' in node.params: # if physical Interface
return ifacename + repr(n)
else:
return node.name + '-' + ifacename + repr(n)
def delete(self):
"Delete this link"
self.intf1.delete()
self.intf1 = None
self.intf2.delete()
self.intf2 = None
def stop(self):
"Override to stop and clean up link as needed"
self.delete()
def status(self):
"Return link status as a string"
return "(%s %s)" % (self.intf1.status(), self.intf2.status())
def __str__(self):
return '%s<->%s' % (self.intf1, self.intf2)
class Link(object):
"""A basic link is just a veth pair.
Other types of links could be tunnels, link emulators, etc.."""
# pylint: disable=too-many-branches
def __init__(self, node1, node2, port1=None, port2=None,
intfName1=None, intfName2=None, addr1=None, addr2=None,
intf=Intf, cls1=None, cls2=None, params1=None,
params2=None, fast=True):