This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
trex-txrx-profile.py
1647 lines (1431 loc) · 106 KB
/
trex-txrx-profile.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
from __future__ import print_function
import sys, getopt
sys.path.append('/opt/trex/current/automation/trex_control_plane/interactive')
import argparse
import string
import datetime
import math
import threading
import uuid
from decimal import *
from trex.stl.api import *
from trex_tg_lib import *
class t_global(object):
args=None
constants=None
variables=None
def myprint(*args, **kwargs):
stderr_only = False
if 'stderr_only' in kwargs:
stderr_only = kwargs['stderr_only']
del kwargs['stderr_only']
if not stderr_only:
print(*args, **kwargs)
if stderr_only or t_global.args.mirrored_log:
print(*args, file = sys.stderr, **kwargs)
return
def setup_global_constants ():
forward_direction = '->'
reverse_direction = '<-'
t_global.constants = { 'forward_direction': forward_direction,
'reverse_direction': reverse_direction,
'both_directions': "%s%s" % (reverse_direction, forward_direction),
'directions': [ forward_direction,
reverse_direction ],
}
def setup_global_variables ():
t_global.variables = { 'packet_resources': { 'ips': { 'dynamic_streams': { 'octet_1': 10,
'octet_2': { 'start': 2,
'stop': 255,
'current': 2 },
'octet_3': 0,
'octet_4': 0 },
'static_streams': { 'octet_1': 10,
'octet_2': { 'A': 0,
'B': 1 },
'octet_3': { 'A': { 'start': 0,
'stop': 255,
'current': 0 },
'B': { 'start': 0,
'stop': 255,
'current': 0 } },
'octet_4': { 'A': { 'start': 0,
'stop': 255,
'current': 0 },
'B': { 'start': 0,
'stop': 255,
'current': 0 } } } },
'ports': { 'src': 32768,
'dst': 49152 },
'mac_prefixes': [],
'stream_ids': {},
'vlan': None },
'uuids': [] }
def process_options ():
parser = argparse.ArgumentParser(usage="generate network traffic and report packet loss")
parser.add_argument('--trex-host',
dest='trex_host',
help='Hostname/IP address of the server where TRex is running',
default='localhost',
type = str
)
parser.add_argument('--debug',
dest='debug',
help='Should debugging be enabled',
action = 'store_true'
)
parser.add_argument('--mirrored-log',
dest='mirrored_log',
help='Should the logging sent to STDOUT be mirrored on STDERR',
action = 'store_true',
)
parser.add_argument('--enable-profiler',
dest='enable_profiler',
help='Should the TRex profiler be enabled',
action = 'store_true',
)
parser.add_argument('--device-pairs',
dest='device_pairs',
help='List of device pairs in the for A:B[,C:D][,E:F][,...]',
default="0:1",
)
parser.add_argument('--active-device-pairs',
dest='active_device_pairs',
help='List of active device pairs in the for A:B[,C:D][,E:F][,...]',
default="--",
)
parser.add_argument('--runtime',
dest='runtime',
help='trial period in seconds',
default=30,
type = int,
)
parser.add_argument('--runtime-tolerance',
dest='runtime_tolerance',
help='The percentage of time that the test is allowed in excess of the requested runtime before it is stopped',
default=5,
type = float,
)
parser.add_argument('--rate-modifier',
dest='rate_modifier',
help='Percentage to modifiy the traffic profile\'s specified rates by',
default = 100.0,
type = float
)
parser.add_argument('--measure-latency',
dest='measure_latency',
help='Collect latency statistics or not',
action = 'store_true'
)
parser.add_argument('--latency-rate',
dest='latency_rate',
help='Rate to send latency packets per second',
default = 1000,
type = int
)
parser.add_argument('--max-loss-pct',
dest='max_loss_pct',
help='Maximum percentage of packet loss',
default=0.002,
type = float
)
parser.add_argument('--disable-flow-cache',
dest='enable_flow_cache',
help='Force disablement of the flow cache',
action = 'store_false',
)
parser.add_argument('--teaching-measurement-interval',
dest='teaching_measurement_interval',
help='Interval to send teaching packets on from the receiving port during the measurement phase in seconds',
default = 10.0,
type = float
)
parser.add_argument('--teaching-warmup-packet-rate',
dest='teaching_warmup_packet_rate',
help='Rate to send teaching packets at from the receiving port in packets per second (pps) during the warmup',
default = 1000,
type = int
)
parser.add_argument('--teaching-measurement-packet-rate',
dest='teaching_measurement_packet_rate',
help='Rate to send teaching packets at from the receiving port in packets per second (pps) during the measurement phase',
default = 1000,
type = int
)
parser.add_argument('--traffic-profile',
dest='traffic_profile',
help='Name of the file containing traffic profiles to load',
default = '',
type = str
)
parser.add_argument('--random-seed',
dest='random_seed',
help='Specify a fixed random seed for repeatable results (defaults to not repeatable)',
default = None,
type = float
)
parser.add_argument('--profiler-interval',
dest='profiler_interval',
help='What interval (in seconds) should the TRex profiler collect data',
default = 3.0,
type = float
)
parser.add_argument('--profiler-logfile',
dest='profiler_logfile',
help='Name of the file to log the profiler to',
default = 'trex-profiler.log',
type = str
)
parser.add_argument('--no-promisc',
dest='no_promisc',
help='Do not use promiscuous mode for network interfaces (usually needed for virtual-functions)',
action = 'store_true'
)
parser.add_argument('--binary-search-synchronize',
dest='binary_search_synchronize',
help='Enable synchronization through binary-search.py. Used to coordinate with other traffic generators.',
action='store_true'
)
t_global.args = parser.parse_args()
if t_global.args.active_device_pairs == '--':
t_global.args.active_device_pairs = t_global.args.device_pairs
random.seed(t_global.args.random_seed)
myprint(t_global.args)
def get_stream_ip (stream, port_id):
ip = ""
if stream['flow_mods']['ip']['dst'] or stream['flow_mods']['ip']['src']:
if t_global.variables['packet_resources']['ips']['dynamic_streams']['octet_2']['current'] > t_global.variables['packet_resources']['ips']['dynamic_streams']['octet_2']['stop']:
raise ValueError("Exhausted dynamic stream IP address pool")
ip = "%d.%d.%d.%d" % (t_global.variables['packet_resources']['ips']['dynamic_streams']['octet_1'],
t_global.variables['packet_resources']['ips']['dynamic_streams']['octet_2']['current'],
t_global.variables['packet_resources']['ips']['dynamic_streams']['octet_3'],
t_global.variables['packet_resources']['ips']['dynamic_streams']['octet_4'])
t_global.variables['packet_resources']['ips']['dynamic_streams']['octet_2']['current'] += 1
else:
if t_global.variables['packet_resources']['ips']['static_streams']['octet_4'][port_id]['current'] > t_global.variables['packet_resources']['ips']['static_streams']['octet_4'][port_id]['stop'] and t_global.variables['packet_resources']['ips']['static_streams']['octet_3'][port_id]['current'] > t_global.variables['packet_resources']['ips']['static_streams']['octet_3'][port_id]['stop']:
raise ValueError("Exhausted static stream IP address pool")
ip = "%d.%d.%d.%d" % (t_global.variables['packet_resources']['ips']['static_streams']['octet_1'],
t_global.variables['packet_resources']['ips']['static_streams']['octet_2'][port_id],
t_global.variables['packet_resources']['ips']['static_streams']['octet_3'][port_id]['current'],
t_global.variables['packet_resources']['ips']['static_streams']['octet_4'][port_id]['current'])
t_global.variables['packet_resources']['ips']['static_streams']['octet_4'][port_id]['current'] += 1
if t_global.variables['packet_resources']['ips']['static_streams']['octet_4'][port_id]['current'] > t_global.variables['packet_resources']['ips']['static_streams']['octet_4'][port_id]['stop']:
t_global.variables['packet_resources']['ips']['static_streams']['octet_4'][port_id]['current'] = t_global.variables['packet_resources']['ips']['static_streams']['octet_4'][port_id]['start']
t_global.variables['packet_resources']['ips']['static_streams']['octet_3'][port_id]['current'] += 1
return ip
def generate_random_mac ():
# ensure that we generate a unique prefix (first 4 octets) so
# that no two streams ever generate the same mac address
while True:
mac_prefix = "%02x:%02x:%02x:%02x" % (0x0,
0x16,
0x3e,
random.randint(0, 255))
if not mac_prefix in t_global.variables['packet_resources']['mac_prefixes']:
t_global.variables['packet_resources']['mac_prefixes'].append(mac_prefix)
break
return "%s:%02x:%02x" % (mac_prefix,
random.randint(0, 255),
random.randint(0, 255))
def setup_stream_packet_values (stream):
if not stream['the_packet'] is None:
if not 'packet_values' in stream:
stream['packet_values'] = { 'vlan': { 'A': t_global.variables['packet_resources']['vlan'],
'B': t_global.variables['packet_resources']['vlan'] },
'ports': { 'A': { 'src': t_global.variables['packet_resources']['ports']['src'],
'dst': t_global.variables['packet_resources']['ports']['dst'] },
'B': { 'src': t_global.variables['packet_resources']['ports']['src'],
'dst': t_global.variables['packet_resources']['ports']['dst'] } } }
layer_counter=0
while True:
layer = stream['the_packet'].getlayer(layer_counter)
if not layer is None:
#myprint("Layer %d is '%s'" % (layer_counter, layer.name))
if layer.name == 'Ethernet':
stream['packet_values']['macs'] = { 'A': layer.src,
'B': layer.dst }
elif layer.name == 'Dot1Q':
stream['packet_values']['vlan'] = { 'A': layer.vlan,
'B': layer.vlan }
elif layer.name == 'IP':
stream['packet_values']['ips'] = { 'A': layer.src,
'B': layer.dst }
elif layer.name == 'TCP' or layer.name == 'UDP':
stream['packet_values']['ports'] = { 'A': { 'src': layer.sport,
'dst': layer.dport },
'B': { 'src': layer.sport,
'dst': layer.dport } }
else:
break
layer_counter += 1
if stream['stream_id']:
t_global.variables['packet_resources']['stream_ids'][stream['stream_id']] = stream['packet_values']
else:
if stream['stream_id'] and stream['stream_id'] in t_global.variables['packet_resources']['stream_ids']:
stream['packet_values'] = t_global.variables['packet_resources']['stream_ids'][stream['stream_id']]
else:
if not 'packet_values' in stream:
stream['packet_values'] = { 'ports': { 'A': { 'src': t_global.variables['packet_resources']['ports']['src'],
'dst': t_global.variables['packet_resources']['ports']['dst'] },
'B': { 'src': t_global.variables['packet_resources']['ports']['src'],
'dst': t_global.variables['packet_resources']['ports']['dst'] } },
'ips': { 'A': get_stream_ip(stream, 'A'),
'B': get_stream_ip(stream, 'B') },
'macs': { 'A': generate_random_mac(),
'B': generate_random_mac() },
'vlan': { 'A': t_global.variables['packet_resources']['vlan'],
'B': t_global.variables['packet_resources']['vlan'] } }
if stream['stream_id']:
t_global.variables['packet_resources']['stream_ids'][stream['stream_id']] = stream['packet_values']
return
def get_uuid ():
while True:
my_uuid = str(uuid.uuid4())
if not my_uuid in t_global.variables['uuids']:
t_global.variables['uuids'].append(my_uuid)
return(my_uuid)
class stl_stream:
def __init__(self,
direction = '',
uuid = None,
mode_uuid = None,
segment_id = -1,
segment_part = -1,
packet = None,
flow_stats_type = None,
flow_stats_pg_id = 0,
mode = None,
name = '',
next_name = '',
dummy = True,
isg = 0.0,
self_start = True,
packet_protocol = 'UDP',
pps = 0.0,
duration = 0,
flow_count = 0,
packet_count = 0,
frame_size = 64,
offset = 0,
substream = False,
packets_per_burst = 0,
ibg = 0.0,
intervals = 0,
standard = False,
teaching = False,
stream_type = ''):
self.direction = direction
self.uuid = uuid
self.mode_uuid = mode_uuid
self.segment_id = segment_id
self.segment_part = segment_part
self.packet = packet
self.flow_stats_type = flow_stats_type
self.flow_stats_pg_id = flow_stats_pg_id
self.mode = mode
self.name = name
self.next_name = next_name
self.dummy = dummy
self.isg = isg
self.self_start = self_start
self.packet_protocol = packet_protocol
self.pps = pps
self.duration = duration
self.flow_count = flow_count
self.packet_count = packet_count
self.frame_size = frame_size
self.offset = offset
self.substream = substream
self.packets_per_burst = packets_per_burst
self.ibg = ibg
self.intervals = intervals
self.standard = standard
self.teaching = teaching
self.stream_type = stream_type
def to_dictionary(self):
return({ 'direction': self.direction,
'uuid': self.uuid,
'mode_uuid': self.mode_uuid,
'segment_id': self.segment_id,
'segment_part': self.segment_part,
'packet': self.packet,
'flow_stats_type': self.flow_stats_type,
'flow_stats_pg_id': self.flow_stats_pg_id,
'mode': self.mode,
'name': self.name,
'next_name': self.next_name,
'dummy': self.dummy,
'isg': self.isg,
'self_start': self.self_start,
'packet_protocol': self.packet_protocol,
'pps': self.pps,
'duration': self.duration,
'flow_count': self.flow_count,
'packet_count': self.packet_count,
'frame_size': self.frame_size,
'offset': self.offset,
'substream': self.substream,
'packets_per_burst': self.packets_per_burst,
'ibg': self.ibg,
'intervals': self.intervals,
'standard': self.standard,
'teaching': self.teaching,
'stream_type': self.stream_type })
def create_stream(self):
stream_control = None
if self.mode == 'burst':
if self.dummy:
# there is no reason to fake traffic at a high
# rate because that just adds overhead, so fake it
# slowly while adjusting accordingly
dummy_pps = 1.0
dummy_pps_ratio = dummy_pps/self.pps
self.packet_count = int(math.ceil(self.packet_count * dummy_pps_ratio))
self.pps = dummy_pps
stream_control = STLTXSingleBurst(pps = self.pps, total_pkts = self.packet_count)
elif self.mode == 'multiburst':
stream_control = STLTXMultiBurst(pkts_per_burst = self.packets_per_burst, ibg = sec_to_usec(self.ibg), count = int(self.intervals), pps = self.pps)
flow_stats = None
my_pg_id = self.flow_stats_pg_id
if self.dummy:
my_pg_id = 0
if self.flow_stats_type == 'default':
flow_stats = STLFlowStats(pg_id = int(my_pg_id))
elif self.flow_stats_type == 'latency':
flow_stats = STLFlowLatencyStats(pg_id = int(my_pg_id))
return(STLStream(packet = self.packet,
flow_stats = flow_stats,
mode = stream_control,
name = self.name,
next = self.next_name,
dummy_stream = self.dummy,
isg = sec_to_usec(self.isg),
self_start = self.self_start))
def append_config(self, device_pair):
if not self.dummy and not self.teaching:
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['protocol'].append(self.packet_protocol)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['pps'].append(self.pps)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['pg_ids'].append(self.flow_stats_pg_id)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['names'].append(self.name)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['next_stream_names'].append(self.next_name)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['frame_sizes'].append(self.frame_size)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['traffic_shares'].append(None)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['self_starts'].append(self.self_start)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['runtime'].append(self.duration)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['stream_modes'].append(self.mode)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['flows'].append(self.flow_count)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['offset'].append(self.offset)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['isg'].append(self.isg)
device_pair[self.direction]['traffic_profile'][self.flow_stats_type]['traffic_type'].append(self.stream_type)
return
class segment_object:
def __init__(self,
stage,
segment_type,
duration,
offset,
isg = 0.0,
skip = False,
standard = False):
self.stage = stage
self.type = segment_type
self.duration = duration
self.offset = offset
self.isg = isg
self.skip = skip
self.standard = standard
def to_dictionary(self):
return({ 'stage': self.stage,
'type': self.type,
'duration': self.duration,
'offset': self.offset,
'isg': self.isg,
'skip': self.skip,
'standard': self.standard })
def build_stream_segments(stream):
segments = []
segments_total_time = 0
stream_runtime = stream['duration']
if stream_runtime is None:
stream_runtime = t_global.args.runtime
else:
if stream_runtime > t_global.args.runtime:
stream_runtime = t_global.args.runtime
# build the initial list of segments, this is a very verbose list
if stream['offset']:
segments.append(segment_object(0, 'null', stream['offset'], segments_total_time))
segments_total_time += stream['offset']
if (stream_runtime + stream['offset']) > t_global.args.runtime:
stream_runtime = t_global.args.runtime - stream['offset']
if stream['repeat']:
remaining_time = t_global.args.runtime - segments_total_time
while segments_total_time < t_global.args.runtime:
if remaining_time >= stream_runtime:
segments.append(segment_object(1, 'tx', stream_runtime, segments_total_time))
segments_total_time += stream_runtime
remaining_time -= stream_runtime
if remaining_time >= stream['repeat_delay']:
segments.append(segment_object(2, 'null', stream['repeat_delay'], segments_total_time))
segments_total_time += stream['repeat_delay']
remaining_time -= stream['repeat_delay']
else:
if remaining_time:
segments.append(segment_object(3, 'null', remaining_time, segments_total_time))
segments_total_time += remaining_time
else:
if remaining_time:
segments.append(segment_object(4, 'tx', remaining_time, segments_total_time))
segments_total_time += remaining_time
else:
segments.append(segment_object(5, 'tx', stream_runtime, segments_total_time))
segments_total_time += stream_runtime
if segments_total_time < t_global.args.runtime:
remaining_time = t_global.args.runtime - segments_total_time
segments.append(segment_object(6, 'null', remaining_time, segments_total_time))
#myprint("stream segments")
#myprint(dump_json_readable(segments))
return(segments)
def build_measurement_segments(segments):
measurement_segments = copy.deepcopy(segments)
if len(measurement_segments) > 1:
# since there are multiple segments try and reduce them
trim_segments = False
for segment_idx in range(0, len(measurement_segments)-1):
# we can eliminate a dummy stream by replacing it with ISG on the following real stream
# this should produce fewer streams and use fewer pg_id resources
if measurement_segments[segment_idx].type == 'null' and measurement_segments[segment_idx+1].type == 'tx':
measurement_segments[segment_idx+1].isg += (measurement_segments[segment_idx].isg + measurement_segments[segment_idx].duration)
measurement_segments[segment_idx+1].offset -= (measurement_segments[segment_idx].isg + measurement_segments[segment_idx].duration)
measurement_segments[segment_idx].stage = 7
measurement_segments[segment_idx+1].stage = 7
measurement_segments[segment_idx].skip = True
trim_segments = True
if trim_segments:
# get rid of all segments marked to be skipped
tmp_segments = []
for segment in measurement_segments:
if not segment.skip:
# merge the isg and duration if the segment is null
if segment.type == 'null' and segment.isg:
segment.duration += segment.isg
segment.isg = 0
tmp_segments.append(segment)
measurement_segments = tmp_segments
# determine if this is a 'standard' stream -- meaning it is the
# first segment and has no ISG and no offset
if len(measurement_segments) == 1:
for segment in measurement_segments:
if segment.isg == segment.offset == 0:
segment.standard = True
#myprint("measurement segments")
#myprint(dump_json_readable(measurement_segments))
return(measurement_segments)
def build_warmup_segments(segments, rate, flows):
warmup_segments = copy.deepcopy(segments)
warmup_duration = float(flows) / float(rate)
warmup_gap = 1.0
if len(warmup_segments) > 1:
# since there are multiple segments try and reduce them
trim_segments = False
for segment_idx in range(0, len(warmup_segments)-1):
if segment_idx == 0 and warmup_segments[segment_idx].type == 'tx' and warmup_segments[segment_idx].isg == 0 and warmup_segments[segment_idx].offset == 0:
warmup_segments[segment_idx].standard = True
elif warmup_segments[segment_idx].type == 'null' and warmup_segments[segment_idx+1].type == 'tx':
warmup_segments[segment_idx].type = 'tx'
warmup_segments[segment_idx+1].type = 'null'
warmup_segments[segment_idx].isg = warmup_segments[segment_idx].duration - warmup_duration - warmup_gap
warmup_segments[segment_idx].duration = warmup_duration
warmup_segments[segment_idx+1].duration += warmup_gap
warmup_segments[segment_idx+1].isg = 0
warmup_segments[segment_idx+1].offset -= (warmup_duration + warmup_gap)
# determine if this is a 'standard' stream -- meaning it is the
# first segment and has no ISG and no offset
if len(warmup_segments) == 1:
for segment in warmup_segments:
if segment.isg == segment.offset == 0:
segment.standard = True
#myprint("warmup segments")
#myprint(dump_json_readable(warmup_segments))
return(warmup_segments)
def create_stream (stream, device_pair, direction, other_direction, flow_scaler):
if not stream['enabled']:
myprint("")
myprint("\tSkipping stream %d for '%s' due to explicit disablement in the profile" % (stream['profile_id'], device_pair[direction]['id_string']))
return
if stream['offset'] >= t_global.args.runtime:
myprint("")
myprint("\tSkipping stream %d for '%s' due to offset >= runtime" % (stream['profile_id'], device_pair[direction]['id_string']))
return
segments = build_stream_segments(stream)
if not stream['repeat_flows'] and stream['repeat']:
for segment in segments:
if segment.type == 'tx':
new_stream = copy.deepcopy(stream)
new_stream['repeat'] = False
new_stream['duration'] = segment.duration
new_stream['offset'] = segment.offset + segment.isg
new_stream['isg'] = 0
new_stream['repeat_delay'] = None
new_stream['repeat_flows'] = True
create_stream(new_stream, device_pair, direction, other_direction, flow_scaler)
return
# assume direction == t_global.constants['forward_direction']
src_port = 'A'
dst_port = 'B'
if direction == t_global.constants['reverse_direction']:
src_port = 'B'
dst_port = 'A'
protocols = [ stream['protocol'] ]
latency = stream['latency']
if not t_global.args.measure_latency:
latency = False
if stream['flow_mods']['protocol']:
if protocols[0] == 'UDP':
protocols.append('TCP')
elif protocols[0] == 'TCP':
protocols.append('UDP')
stream_modes = [ 'default' ]
if stream['latency_only']:
if latency:
stream_modes = [ 'latency' ]
else:
stream_modes = []
elif latency:
stream_modes.append('latency')
stream_flows = int(stream['flows'] * flow_scaler)
stream_packets = { 'measurement': [],
'teaching': [] }
flow_stats = None
if not stream['the_packet'] is None:
stream_packets['teaching'].append({ 'protocol': "user-pkt",
'packet': load_user_pkt(stream['the_packet'],
stream['frame_size'],
stream['packet_values']['macs'][dst_port],
stream['packet_values']['macs'][src_port],
stream['packet_values']['ips'][dst_port],
stream['packet_values']['ips'][src_port],
stream['packet_values']['ports'][dst_port]['src'],
stream['packet_values']['ports'][src_port]['dst'],
stream['flow_mods'],
stream_flows,
t_global.args.enable_flow_cache,
flow_offset = stream['flow_offset'],
old_mac_flow = False) })
stream_packets['measurement'].append({ 'protocol': "user-pkt",
'packet': load_user_pkt(stream['the_packet'],
stream['frame_size'],
stream['packet_values']['macs'][src_port],
stream['packet_values']['macs'][dst_port],
stream['packet_values']['ips'][src_port],
stream['packet_values']['ips'][dst_port],
stream['packet_values']['ports'][src_port]['src'],
stream['packet_values']['ports'][dst_port]['dst'],
stream['flow_mods'],
stream_flows,
t_global.args.enable_flow_cache,
flow_offset = stream['flow_offset'],
old_mac_flow = False) })
else:
if stream['frame_type'] == 'generic':
# teaching packets don't need to cover all the protocols; they just need to cover all the MAC addresses
stream_packets['teaching'].append({ 'protocol': "%s-%s" % (stream['frame_type'], protocols[0]),
'packet': create_generic_pkt(stream['frame_size'],
stream['packet_values']['macs'][dst_port],
stream['packet_values']['macs'][src_port],
stream['packet_values']['ips'][dst_port],
stream['packet_values']['ips'][src_port],
stream['packet_values']['ports'][dst_port]['src'],
stream['packet_values']['ports'][src_port]['dst'],
protocols[0],
stream['packet_values']['vlan'][dst_port],
stream['flow_mods'],
stream_flows,
t_global.args.enable_flow_cache,
flow_offset = stream['flow_offset'],
old_mac_flow = False) })
for protocol in protocols:
stream_packets['measurement'].append({ 'protocol': "%s-%s" % (stream['frame_type'], protocol),
'packet': create_generic_pkt(stream['frame_size'],
stream['packet_values']['macs'][src_port],
stream['packet_values']['macs'][dst_port],
stream['packet_values']['ips'][src_port],
stream['packet_values']['ips'][dst_port],
stream['packet_values']['ports'][src_port]['src'],
stream['packet_values']['ports'][dst_port]['dst'],
protocol,
stream['packet_values']['vlan'][src_port],
stream['flow_mods'],
stream_flows,
t_global.args.enable_flow_cache,
flow_offset = stream['flow_offset'],
old_mac_flow = False) })
elif stream['frame_type'] == 'garp':
# GARP packet types: 0x1=request, 0x2=reply
garp_packets = [ { 'name': 'request',
'opcode': 0x1 },
{ 'name': 'response',
'opcode': 0x2 } ]
for garp_packet in garp_packets:
stream_packets['measurement'].append({ 'protocol': "%s-%s" % (stream['frame_type'], garp_packet['name']),
'packet': create_garp_pkt(stream['packet_values']['macs'][src_port],
stream['packet_values']['ips'][src_port],
stream['packet_values']['vlan'][src_port],
garp_packet['opcode'],
stream['flow_mods'],
stream_flows,
t_global.args.enable_flow_cache,
flow_offset = stream['flow_offset'],
old_mac_flow = False) })
stream_packets['teaching'].append({ 'protocol': "%s-%s" % (stream['frame_type'], garp_packet['name']),
'packet': create_garp_pkt(stream['packet_values']['macs'][dst_port],
stream['packet_values']['ips'][dst_port],
stream['packet_values']['vlan'][dst_port],
garp_packet['opcode'],
stream['flow_mods'],
stream_flows,
t_global.args.enable_flow_cache,
flow_offset = stream['flow_offset'],
old_mac_flow = False) })
elif stream['frame_type'] == 'icmp':
stream_packets['measurement'].append({ 'protocol': stream['frame_type'],
'packet': create_icmp_pkt(stream['frame_size'],
stream['packet_values']['macs'][src_port],
stream['packet_values']['macs'][dst_port],
stream['packet_values']['ips'][src_port],
stream['packet_values']['ips'][dst_port],
stream['packet_values']['vlan'][src_port],
stream['flow_mods'],
stream_flows,
t_global.args.enable_flow_cache,
flow_offset = stream['flow_offset'],
old_mac_flow = False) })
stream_packets['teaching'].append({ 'protocol': stream['frame_type'],
'packet': create_icmp_pkt(stream['frame_size'],
stream['packet_values']['macs'][dst_port],
stream['packet_values']['macs'][src_port],
stream['packet_values']['ips'][dst_port],
stream['packet_values']['ips'][src_port],
stream['packet_values']['vlan'][dst_port],
stream['flow_mods'],
stream_flows,
t_global.args.enable_flow_cache,
flow_offset = stream['flow_offset'],
old_mac_flow = False) })
else:
raise ValueError("Invalid frame_type: %s" % (stream['frame_type']))
stream_rate = stream['rate']
measurement_segments = build_measurement_segments(segments)
if not len(measurement_segments):
raise ValueError("Hmm, for some reason there are no measurement segments.")
stl_stream_types = [ 'traffic_streams', 'teaching_measurement_traffic_streams', 'teaching_warmup_traffic_streams', 'teaching_warmup_standard_traffic_streams' ]
stl_streams = { }
for stl_stream_type in stl_stream_types:
stl_streams[stl_stream_type] = []
# generate a uuid to represent this measurement stream
stream_uuid = get_uuid()
for stream_type in stream['stream_types']:
if stream_type != 'measurement' and stream_type != 'teaching_warmup' and stream_type != 'teaching_measurement' and stream_type != 'ddos':
raise ValueError("Invalid stream_type: %s" % (stream_type))
# 'measurement' and 'ddos' (Distributed Denial-of-Service)
# packets are exactly the same -- except we don't expect to
# get any 'ddos' packets back because they should be filtered
# by the DUT
if stream_type == 'measurement' or stream_type == 'ddos':
for stream_mode in stream_modes:
# generate a uuid to represent this measurement stream's mode
stream_mode_uuid = get_uuid()
stream_rate = stream['rate']
if len(stream_packets['measurement']) > 1:
stream_rate /= len(protocols)
if 'latency' in stream_modes and stream_mode == 'default':
stream_rate -= t_global.args.latency_rate
if stream_rate <= 0:
continue
elif stream_mode == 'latency' and stream_rate > t_global.args.latency_rate:
stream_rate = t_global.args.latency_rate
for stream_packet in stream_packets['measurement']:
stream_pg_id = None
myprint("")
myprint("\t'%s' stream for '%s' with uuid=%s, mode_uuid=%s, flows=%s, frame size=%s, rate=%s, and mode=%s" % (stream_type,
device_pair[direction]['id_string'],
stream_uuid,
stream_mode_uuid,
commify(stream_flows),
commify(stream['frame_size']),
commify(stream_rate),
stream_mode))
myprint("\t\tSegments:")
segment_auto_start = True
base_stream_name = ""
segment_part_counter = 1
for segment_idx in range(0, len(measurement_segments)):
if measurement_segments[segment_idx].type == 'tx' and (stream_pg_id is None or stream_mode == 'latency'):
if device_pair[direction]['pg_ids'][stream_mode]['available']:
stream_pg_id = device_pair[direction]['pg_ids'][stream_mode]['start_index'] + (device_pair[direction]['pg_ids'][stream_mode]['total'] - device_pair[direction]['pg_ids'][stream_mode]['available'])
device_pair[direction]['pg_ids'][stream_mode]['available'] -= 1
else:
raise RuntimeError("Not enough available pg_ids for the requested stream configuration")
base_stream_name = "stream-%s_mode-%s_%s_%s" % (stream_uuid, stream_mode_uuid, stream_mode, stream_packet['protocol'])
if stream['stream_id']:
base_stream_name = "%s_%s" % (base_stream_name, stream['stream_id'])
stream_name = "%s_part-%d" % (base_stream_name, segment_part_counter)
next_stream_name = "%s_part-%d" % (base_stream_name, segment_part_counter+1)
dummy = True
flow_stats = None
tmp_stream_pg_id = None
if measurement_segments[segment_idx].type == 'tx':
dummy = False
tmp_stream_pg_id = stream_pg_id
else:
tmp_stream_pg_id = 'null'
myprint("\t\t\t%d: name=%s, pg_id=%s, type=%s, duration=%s, offset=%s, and isg=%s" % (segment_idx+1,
stream_name,
tmp_stream_pg_id,
measurement_segments[segment_idx].type,
commify(measurement_segments[segment_idx].duration),
commify(measurement_segments[segment_idx].offset),
commify(measurement_segments[segment_idx].isg)))
if (len(measurement_segments) == 1) or (segment_idx == (len(measurement_segments) - 1)):
next_stream_name = None
if segment_idx > 0:
segment_auto_start = False
stream_total_pkts = int(stream_rate * measurement_segments[segment_idx].duration)
# check if the total number of packets to TX is greater than can be held in an uint32 (API limit)
max_uint32 = int(4294967295)
if stream_total_pkts > max_uint32:
stream_loop_remainder = stream_total_pkts % max_uint32
stream_loops = int(((stream_total_pkts - stream_loop_remainder) / max_uint32))
if stream_loop_remainder == 0:
stream_loops -= 1
stream_loops_remainder = max_uint32
substream_self_start = segment_auto_start
substream_isg = measurement_segments[segment_idx].isg
for loop_idx in range(1, stream_loops+1):
substream_name = "%s_part-%d" % (base_stream_name, segment_part_counter)
substream_next_name = "%s_part-%d" % (base_stream_name, segment_part_counter+1)
myprint("\t\t\t\tSubstream %d with name=%s" % (loop_idx, substream_name))
stl_streams['traffic_streams'].append(stl_stream(direction = direction,
uuid = stream_uuid,
mode_uuid = stream_mode_uuid,
segment_id = segment_idx,
segment_part = segment_part_counter,
packet = stream_packet['packet'],
flow_stats_type = stream_mode,
flow_stats_pg_id = stream_pg_id,
mode = 'burst',
name = substream_name,
next_name = substream_next_name,
dummy = dummy,
isg = substream_isg,
self_start = substream_self_start,
packet_protocol = stream_packet['protocol'],
pps = stream_rate,
duration = (max_uint32 / stream_rate),
offset = 0, # fix me
flow_count = stream_flows,
frame_size = stream['frame_size'],
packet_count = max_uint32,
substream = True,
standard = False,
stream_type = stream_type))
substream_self_start = False
substream_isg = 0.0
segment_part_counter += 1
substream_name = "%s_part-%d" % (base_stream_name, segment_part_counter)
if not next_stream_name is None:
next_stream_name = "%s_part-%d" % (base_stream_name, segment_part_counter+1)
myprint("\t\t\t\tSubstream %d with name=%s" % (stream_loops+1, substream_name))
stl_streams['traffic_streams'].append(stl_stream(direction = direction,
uuid = stream_uuid,
mode_uuid = stream_mode_uuid,
segment_id = segment_idx,
segment_part = segment_part_counter,
packet = stream_packet['packet'],
flow_stats_type = stream_mode,
flow_stats_pg_id = stream_pg_id,
mode = 'burst',
name = substream_name,
next_name = next_stream_name,
dummy = dummy,
isg = substream_isg,
self_start = substream_self_start,
packet_protocol = stream_packet['protocol'],
pps = stream_rate,
duration = (stream_loop_remainder / stream_rate),
offset = 0, # fix me
flow_count = stream_flows,
frame_size = stream['frame_size'],
packet_count = stream_loop_remainder,
substream = True,
standard = False,
stream_type = stream_type))
else:
stl_streams['traffic_streams'].append(stl_stream(direction = direction,
uuid = stream_uuid,
mode_uuid = stream_mode_uuid,
segment_id = segment_idx,
segment_part = segment_part_counter,
packet = stream_packet['packet'],
flow_stats_type = stream_mode,
flow_stats_pg_id = stream_pg_id,
mode = 'burst',
name = stream_name,
next_name = next_stream_name,
dummy = dummy,
isg = measurement_segments[segment_idx].isg,
self_start = segment_auto_start,
packet_protocol = stream_packet['protocol'],
pps = stream_rate,
duration = measurement_segments[segment_idx].duration,
offset = measurement_segments[segment_idx].offset,
flow_count = stream_flows,
frame_size = stream['frame_size'],
packet_count = stream_total_pkts,
standard = measurement_segments[segment_idx].standard,
stream_type = stream_type))
segment_part_counter += 1
elif stream_type == 'teaching_warmup':
# if teaching_warmup is the only type for this stream, use the stream's configured rate
# otherwise use the global default for teaching warmup rate
if len(stream['stream_types']) != 1:
stream_rate = t_global.args.teaching_warmup_packet_rate
warmup_segments = build_warmup_segments(segments, stream_rate, stream_flows)
if not len(warmup_segments):
raise ValueError("Hmm, for some reason there are no warmup segments.")
device_pair[direction]['teaching_warmup_max_runtime'] = max(device_pair[direction]['teaching_warmup_max_runtime'],
(stream_flows / stream_rate))
for stream_packet in stream_packets['teaching']:
myprint("")
myprint("\tTeaching warmup stream for '%s' with uuid=%s, flows=%s, frame size=%s, rate=%s, and protocol=%s" % (device_pair[direction]['id_string'],
stream_uuid,
commify(stream_flows),