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
|
#!/router/bin/python
from trex_stl_exceptions import *
from trex_stl_types import verify_exclusive_arg, validate_type
from trex_stl_packet_builder_interface import CTrexPktBuilderInterface
from trex_stl_packet_builder_scapy import CScapyTRexPktBuilder, Ether, IP, UDP, TCP, RawPcapReader
from collections import OrderedDict, namedtuple
from dpkt import pcap
import random
import yaml
import base64
import string
import traceback
from types import NoneType
import copy
# base class for TX mode
class STLTXMode(object):
def __init__ (self, pps = None, bps_L1 = None, bps_L2 = None, percentage = None):
args = [pps, bps_L1, bps_L2, percentage]
# default
if all([x is None for x in args]):
pps = 1.0
else:
verify_exclusive_arg(args)
self.fields = {'rate': {}}
if pps is not None:
validate_type('pps', pps, [float, int])
self.fields['rate']['type'] = 'pps'
self.fields['rate']['value'] = pps
elif bps_L1 is not None:
validate_type('bps_L1', bps_L1, [float, int])
self.fields['rate']['type'] = 'bps_L1'
self.fields['rate']['value'] = bps_L1
elif bps_L2 is not None:
validate_type('bps_L2', bps_L2, [float, int])
self.fields['rate']['type'] = 'bps_L2'
self.fields['rate']['value'] = bps_L2
elif percentage is not None:
validate_type('percentage', percentage, [float, int])
if not (percentage > 0 and percentage <= 100):
raise STLArgumentError('percentage', percentage)
self.fields['rate']['type'] = 'percentage'
self.fields['rate']['value'] = percentage
def to_json (self):
return self.fields
# continuous mode
class STLTXCont(STLTXMode):
def __init__ (self, **kwargs):
super(STLTXCont, self).__init__(**kwargs)
self.fields['type'] = 'continuous'
def __str__ (self):
return "Continuous"
# single burst mode
class STLTXSingleBurst(STLTXMode):
def __init__ (self, total_pkts = 1, **kwargs):
if not isinstance(total_pkts, int):
raise STLArgumentError('total_pkts', total_pkts)
super(STLTXSingleBurst, self).__init__(**kwargs)
self.fields['type'] = 'single_burst'
self.fields['total_pkts'] = total_pkts
def __str__ (self):
return "Single Burst"
# multi burst mode
class STLTXMultiBurst(STLTXMode):
def __init__ (self,
pkts_per_burst = 1,
ibg = 0.0, # usec not SEC
count = 1,
**kwargs):
if not isinstance(pkts_per_burst, int):
raise STLArgumentError('pkts_per_burst', pkts_per_burst)
if not isinstance(ibg, (int, float)):
raise STLArgumentError('ibg', ibg)
if not isinstance(count, int):
raise STLArgumentError('count', count)
super(STLTXMultiBurst, self).__init__(**kwargs)
self.fields['type'] = 'multi_burst'
self.fields['pkts_per_burst'] = pkts_per_burst
self.fields['ibg'] = ibg
self.fields['count'] = count
def __str__ (self):
return "Multi Burst"
STLStreamDstMAC_CFG_FILE=0
STLStreamDstMAC_PKT =1
STLStreamDstMAC_ARP =2
# RX stats class
class STLRxStats(object):
def __init__ (self, user_id):
self.fields = {}
self.fields['enabled'] = True
self.fields['stream_id'] = user_id
self.fields['seq_enabled'] = False
self.fields['latency_enabled'] = False
def to_json (self):
return dict(self.fields)
@staticmethod
def defaults ():
return {'enabled' : False}
class STLStream(object):
def __init__ (self,
name = None,
packet = None,
mode = STLTXCont(pps = 1),
enabled = True,
self_start = True,
isg = 0.0,
rx_stats = None,
next = None,
stream_id = None,
action_count = 0,
mac_src_override_by_pkt=None,
mac_dst_override_mode=None #see STLStreamDstMAC_xx
):
# type checking
validate_type('mode', mode, STLTXMode)
validate_type('packet', packet, (NoneType, CTrexPktBuilderInterface))
validate_type('enabled', enabled, bool)
validate_type('self_start', self_start, bool)
validate_type('isg', isg, (int, float))
validate_type('stream_id', stream_id, (NoneType, int))
if (type(mode) == STLTXCont) and (next != None):
raise STLError("continuous stream cannot have a next stream ID")
# tag for the stream and next - can be anything
self.name = name
self.next = next
self.id = stream_id
self.fields = {}
int_mac_src_override_by_pkt = 0;
int_mac_dst_override_mode = 0;
if mac_src_override_by_pkt == None:
int_mac_src_override_by_pkt=0
if packet :
if packet.is_def_src_mac ()==False:
int_mac_src_override_by_pkt=1
else:
int_mac_src_override_by_pkt = int(mac_src_override_by_pkt);
if mac_dst_override_mode == None:
int_mac_dst_override_mode = 0;
if packet :
if packet.is_def_dst_mac ()==False:
int_mac_dst_override_mode=STLStreamDstMAC_PKT
else:
int_mac_dst_override_mode = int(mac_dst_override_mode);
self.fields['flags'] = (int_mac_src_override_by_pkt&1) + ((int_mac_dst_override_mode&3)<<1)
self.fields['action_count'] = action_count
# basic fields
self.fields['enabled'] = enabled
self.fields['self_start'] = self_start
self.fields['isg'] = isg
# mode
self.fields['mode'] = mode.to_json()
self.mode_desc = str(mode)
# packet
self.fields['packet'] = {}
self.fields['vm'] = {}
if not packet:
packet = CScapyTRexPktBuilder(pkt = Ether()/IP())
# packet builder
packet.compile()
# packet and VM
self.fields['packet'] = packet.dump_pkt()
self.fields['vm'] = packet.get_vm_data()
self.pkt = base64.b64decode(self.fields['packet']['binary'])
# this is heavy, calculate lazy
self.packet_desc = None
if not rx_stats:
self.fields['rx_stats'] = STLRxStats.defaults()
else:
self.fields['rx_stats'] = rx_stats.to_json()
def __str__ (self):
s = "Stream Name: {0}\n".format(self.name)
s += "Stream Next: {0}\n".format(self.next)
s += "Stream JSON:\n{0}\n".format(json.dumps(self.fields, indent = 4, separators=(',', ': '), sort_keys = True))
return s
def to_json (self):
return dict(self.fields)
def get_id (self):
return self.id
def get_name (self):
return self.name
def get_next (self):
return self.next
def get_pkt (self):
return self.pkt
def get_pkt_len (self, count_crc = True):
pkt_len = len(self.get_pkt())
if count_crc:
pkt_len += 4
return pkt_len
def get_pkt_type (self):
if self.packet_desc == None:
self.packet_desc = CScapyTRexPktBuilder.pkt_layers_desc_from_buffer(self.get_pkt())
return self.packet_desc
def get_mode (self):
return self.mode_desc
@staticmethod
def get_rate_from_field (rate_json):
t = rate_json['type']
v = rate_json['value']
if t == "pps":
return format_num(v, suffix = "pps")
elif t == "bps_L1":
return format_num(v, suffix = "bps (L1)")
elif t == "bps_L2":
return format_num(v, suffix = "bps (L2)")
elif t == "percentage":
return format_num(v, suffix = "%")
def get_rate (self):
return self.get_rate_from_field(self.fields['mode']['rate'])
def to_yaml (self):
y = {}
if self.name:
y['name'] = self.name
if self.next:
y['next'] = self.next
y['stream'] = copy.deepcopy(self.fields)
# some shortcuts for YAML
rate_type = self.fields['mode']['rate']['type']
rate_value = self.fields['mode']['rate']['value']
y['stream']['mode'][rate_type] = rate_value
del y['stream']['mode']['rate']
return y
def dump_to_yaml (self, yaml_file = None):
yaml_dump = yaml.dump([self.to_yaml()], default_flow_style = False)
# write to file if provided
if yaml_file:
with open(yaml_file, 'w') as f:
f.write(yaml_dump)
return yaml_dump
class YAMLLoader(object):
def __init__ (self, yaml_file):
self.yaml_path = os.path.dirname(yaml_file)
self.yaml_file = yaml_file
def __parse_packet (self, packet_dict):
packet_type = set(packet_dict).intersection(['binary', 'pcap'])
if len(packet_type) != 1:
raise STLError("packet section must contain either 'binary' or 'pcap'")
if 'binary' in packet_type:
try:
pkt_str = base64.b64decode(packet_dict['binary'])
except TypeError:
raise STLError("'binary' field is not a valid packet format")
builder = CScapyTRexPktBuilder(pkt_buffer = pkt_str)
elif 'pcap' in packet_type:
pcap = os.path.join(self.yaml_path, packet_dict['pcap'])
if not os.path.exists(pcap):
raise STLError("'pcap' - cannot find '{0}'".format(pcap))
builder = CScapyTRexPktBuilder(pkt = pcap)
return builder
def __parse_mode (self, mode_obj):
if not mode_obj:
return None
rate_parser = set(mode_obj).intersection(['pps', 'bps_L1', 'bps_L2', 'percentage'])
if len(rate_parser) != 1:
raise STLError("'rate' must contain exactly one from 'pps', 'bps_L1', 'bps_L2', 'percentage'")
rate_type = rate_parser.pop()
rate = {rate_type : mode_obj[rate_type]}
mode_type = mode_obj.get('type')
if mode_type == 'continuous':
mode = STLTXCont(**rate)
elif mode_type == 'single_burst':
defaults = STLTXSingleBurst()
mode = STLTXSingleBurst(total_pkts = mode_obj.get('total_pkts', defaults.fields['total_pkts']),
**rate)
elif mode_type == 'multi_burst':
defaults = STLTXMultiBurst()
mode = STLTXMultiBurst(pkts_per_burst = mode_obj.get('pkts_per_burst', defaults.fields['pkts_per_burst']),
ibg = mode_obj.get('ibg', defaults.fields['ibg']),
count = mode_obj.get('count', defaults.fields['count']),
**rate)
else:
raise STLError("mode type can be 'continuous', 'single_burst' or 'multi_burst")
return mode
def __parse_rx_stats (self, rx_stats_obj):
# no such object
if not rx_stats_obj or rx_stats_obj.get('enabled') == False:
return None
user_id = rx_stats_obj.get('stream_id')
if user_id == None:
raise STLError("enabled RX stats section must contain 'stream_id' field")
return STLRxStats(user_id = user_id)
def __parse_stream (self, yaml_object):
s_obj = yaml_object['stream']
# parse packet
packet = s_obj.get('packet')
if not packet:
raise STLError("YAML file must contain 'packet' field")
builder = self.__parse_packet(packet)
# mode
mode = self.__parse_mode(s_obj.get('mode'))
# rx stats
rx_stats = self.__parse_rx_stats(s_obj.get('rx_stats'))
defaults = STLStream()
# create the stream
stream = STLStream(name = yaml_object.get('name'),
packet = builder,
mode = mode,
rx_stats = rx_stats,
enabled = s_obj.get('enabled', defaults.fields['enabled']),
self_start = s_obj.get('self_start', defaults.fields['self_start']),
isg = s_obj.get('isg', defaults.fields['isg']),
next = yaml_object.get('next'),
action_count = s_obj.get('action_count', defaults.fields['action_count']),
mac_src_override_by_pkt = s_obj.get('mac_src_override_by_pkt', 0),
mac_dst_override_mode = s_obj.get('mac_src_override_by_pkt', 0)
)
# hack the VM fields for now
if 'vm' in s_obj:
stream.fields['vm'].update(s_obj['vm'])
return stream
def parse (self):
with open(self.yaml_file, 'r') as f:
# read YAML and pass it down to stream object
yaml_str = f.read()
try:
objects = yaml.load(yaml_str)
except yaml.parser.ParserError as e:
raise STLError(str(e))
streams = [self.__parse_stream(object) for object in objects]
return streams
# profile class
class STLProfile(object):
def __init__ (self, streams = None):
if streams == None:
streams = []
if not type(streams) == list:
streams = [streams]
if not all([isinstance(stream, STLStream) for stream in streams]):
raise STLArgumentError('streams', streams, valid_values = STLStream)
self.streams = streams
def get_streams (self):
return self.streams
def __str__ (self):
return '\n'.join([str(stream) for stream in self.streams])
@staticmethod
def load_yaml (yaml_file):
# check filename
if not os.path.isfile(yaml_file):
raise STLError("file '{0}' does not exists".format(yaml_file))
yaml_loader = YAMLLoader(yaml_file)
streams = yaml_loader.parse()
return STLProfile(streams)
@staticmethod
def load_py (python_file):
# check filename
if not os.path.isfile(python_file):
raise STLError("file '{0}' does not exists".format(python_file))
basedir = os.path.dirname(python_file)
sys.path.append(basedir)
try:
file = os.path.basename(python_file).split('.')[0]
module = __import__(file, globals(), locals(), [], -1)
reload(module) # reload the update
streams = module.register().get_streams()
return STLProfile(streams)
except Exception as e:
a, b, tb = sys.exc_info()
x =''.join(traceback.format_list(traceback.extract_tb(tb)[1:])) + a.__name__ + ": " + str(b) + "\n"
summary = "\nPython Traceback follows:\n\n" + x
raise STLError(summary)
finally:
sys.path.remove(basedir)
# loop_count = 0 means loop forever
@staticmethod
def load_pcap (pcap_file, ipg_usec = None, speedup = 1.0, loop_count = 1, vm = None):
# check filename
if not os.path.isfile(pcap_file):
raise STLError("file '{0}' does not exists".format(pcap_file))
streams = []
last_ts_usec = 0
pkts = RawPcapReader(pcap_file).read_all()
for i, (cap, meta) in enumerate(pkts, start = 1):
# IPG - if not provided, take from cap
if ipg_usec == None:
ts_usec = (meta[0] * 1e6 + meta[1]) / float(speedup)
else:
ts_usec = (ipg_usec * i) / float(speedup)
# handle last packet
if i == len(pkts):
next = 1
action_count = loop_count
else:
next = i + 1
action_count = 0
streams.append(STLStream(name = i,
packet = CScapyTRexPktBuilder(pkt_buffer = cap, vm = vm),
mode = STLTXSingleBurst(total_pkts = 1, percentage = 100),
self_start = True if (i == 1) else False,
isg = (ts_usec - last_ts_usec), # seconds to usec
action_count = action_count,
next = next))
last_ts_usec = ts_usec
return STLProfile(streams)
@staticmethod
def load (filename):
x = os.path.basename(filename).split('.')
suffix = x[1] if (len(x) == 2) else None
if suffix == 'py':
profile = STLProfile.load_py(filename)
elif suffix == 'yaml':
profile = STLProfile.load_yaml(filename)
elif suffix in ['cap', 'pcap']:
profile = STLProfile.load_pcap(filename, speedup = 1, ipg_usec = 1e6)
else:
raise STLError("unknown profile file type: '{0}'".format(suffix))
return profile
def dump_to_yaml (self, yaml_file = None):
yaml_list = [stream.to_yaml() for stream in self.streams]
yaml_str = yaml.dump(yaml_list, default_flow_style = False)
# write to file if provided
if yaml_file:
with open(yaml_file, 'w') as f:
f.write(yaml_str)
return yaml_str
def __len__ (self):
return len(self.streams)
|