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
|
#!/router/bin/python
import sys
if sys.version_info >= (3, 0):
import configparser
else:
import ConfigParser
import outer_packages
import yaml
from collections import namedtuple
import subprocess, shlex
import os
TRexConfig = namedtuple('TRexConfig', 'trex, router, tftp')
# debug/development purpose, lists object's attributes and their values
def print_r(obj):
for attr in dir(obj):
print('obj.%s %s' % (attr, getattr(obj, attr)))
def mix_string (str):
"""Convert all string to lowercase letters, and replaces spaces with '_' char"""
return str.replace(' ', '_').lower()
# executes given command, returns tuple (return_code, stdout, stderr)
def run_command(cmd, background = False):
if background:
print('Running command in background: %s' % cmd)
with open(os.devnull, 'w') as tempf:
subprocess.Popen(shlex.split(cmd), stdin=tempf, stdout=tempf, stderr=tempf)
return (None,)*3
else:
print('Running command: %s' % cmd)
proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
stdout = stdout.decode()
stderr = stderr.decode()
if stdout:
print('Stdout:\n%s' % stdout)
if proc.returncode:
if stderr:
print('Stderr:\n%s' % stderr)
print('Return code: %s' % proc.returncode)
return (proc.returncode, stdout, stderr)
def run_remote_command(host, command_string, background = False, timeout = 20):
cmd = 'ssh -tt %s \'sudo%s sh -ec "%s"\'' % (host, (' timeout %s' % timeout) if (timeout and not background) else '', command_string)
return run_command(cmd, background)
def generate_intf_lists (interfacesList):
retDict = {
'relevant_intf' : [],
'relevant_ip_addr' : [],
'relevant_mac_addr' : [],
'total_pairs' : None
}
for intf in interfacesList:
retDict['relevant_intf'].append(intf['client'])
retDict['relevant_ip_addr'].append(intf['client_config']['ip_addr'])
retDict['relevant_mac_addr'].append(intf['client_config']['mac_addr'])
retDict['relevant_intf'].append(intf['server'])
retDict['relevant_ip_addr'].append(intf['server_config']['ip_addr'])
retDict['relevant_mac_addr'].append(intf['server_config']['mac_addr'])
retDict['total_pairs'] = len(interfacesList)
return retDict
def get_single_net_client_addr (ip_addr, octetListDict = {'3' : 1}, ip_type = 'ipv4'):
""" get_single_net_client_addr(ip_addr, octetListDict, ip_type) -> str
Parameters
----------
ip_addr : str
a string an IP address (by default, of type A.B.C.D)
octetListDict : dict
a ditionary representing the octets on which to act such that ip[octet_key] = ip[octet_key] + octet_value
ip_type : str
a string that defines the ip type to parse. possible inputs are 'ipv4', 'ipv6'
By default- Returns a new ip address - A.B.C.(D+1)
"""
if ip_type == 'ipv4':
ip_lst = ip_addr.split('.')
for octet,increment in octetListDict.items():
int_octet = int(octet)
if ((int_octet < 0) or (int_octet > 3)):
raise ValueError('the provided octet is not legal in {0} format'.format(ip_type) )
else:
if (int(ip_lst[int_octet]) + increment) < 255:
ip_lst[int_octet] = str(int(ip_lst[int_octet]) + increment)
else:
raise ValueError('the requested increment exceeds 255 client address limit')
return '.'.join(ip_lst)
else: # this is a ipv6 address, handle accordingly
ip_lst = ip_addr.split(':')
for octet,increment in octetListDict.items():
int_octet = int(octet)
if ((int_octet < 0) or (int_octet > 7)):
raise ValueError('the provided octet is not legal in {0} format'.format(ip_type) )
else:
if (int(ip_lst[int_octet]) + increment) < 65535:
ip_lst[int_octet] = format( int(ip_lst[int_octet], 16) + increment, 'X')
else:
raise ValueError('the requested increment exceeds 65535 client address limit')
return ':'.join(ip_lst)
def load_complete_config_file (filepath):
"""load_complete_config_file(filepath) -> list
Loads a configuration file (.yaml) for both trex config and router config
Returns a list with a dictionary to each of the configurations
"""
# create response dictionaries
trex_config = {}
rtr_config = {}
tftp_config = {}
try:
with open(filepath, 'r') as f:
config = yaml.load(f)
# Handle TRex configuration
trex_config['trex_name'] = config["trex"]["hostname"]
trex_config['trex_password'] = config["trex"].get("password")
#trex_config['trex_is_dual'] = config["trex"]["is_dual"]
trex_config['trex_cores'] = int(config["trex"]["cores"])
#trex_config['trex_latency'] = int(config["trex"]["latency"])
# trex_config['trex_version_path'] = config["trex"]["version_path"]
trex_config['modes'] = config['trex'].get('modes', [])
if 'loopback' not in trex_config['modes']:
trex_config['router_interface'] = config["router"]["ip_address"]
# Handle Router configuration
rtr_config['model'] = config["router"]["model"]
rtr_config['hostname'] = config["router"]["hostname"]
rtr_config['ip_address'] = config["router"]["ip_address"]
rtr_config['image'] = config["router"]["image"]
rtr_config['line_pswd'] = config["router"]["line_password"]
rtr_config['en_pswd'] = config["router"]["en_password"]
rtr_config['interfaces'] = config["router"]["interfaces"]
rtr_config['clean_config'] = config["router"]["clean_config"]
rtr_config['intf_masking'] = config["router"]["intf_masking"]
rtr_config['ipv6_mask'] = config["router"]["ipv6_mask"]
rtr_config['mgmt_interface'] = config["router"]["mgmt_interface"]
# Handle TFTP configuration
tftp_config['hostname'] = config["tftp"]["hostname"]
tftp_config['ip_address'] = config["tftp"]["ip_address"]
tftp_config['images_path'] = config["tftp"]["images_path"]
if rtr_config['clean_config'] is None:
raise ValueError('A clean router configuration wasn`t provided.')
except ValueError:
print("")
raise
except Exception as inst:
print("\nBad configuration file provided: '{0}'\n".format(filepath))
raise inst
return TRexConfig(trex_config, rtr_config, tftp_config)
def load_object_config_file (filepath):
try:
with open(filepath, 'r') as f:
config = yaml.load(f)
return config
except Exception as inst:
print("\nBad configuration file provided: '{0}'\n".format(filepath))
print(inst)
exit(-1)
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = { "yes": True, "y": True, "ye": True,
"no": False, "n": False }
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def load_benchmark_config_file (filepath):
"""load_benchmark_config_file(filepath) -> list
Loads a configuration file (.yaml) for both trex config and router config
Returns a list with a dictionary to each of the configurations
"""
# create response dictionary
benchmark_config = {}
try:
with open(filepath, 'r') as f:
benchmark_config = yaml.load(f)
except Exception as inst:
print("\nBad configuration file provided: '{0}'\n".format(filepath))
print(inst)
exit(-1)
return benchmark_config
def get_benchmark_param (benchmark_path, test_name, param, sub_param = None):
config = load_benchmark_config_file(benchmark_path)
if sub_param is None:
return config[test_name][param]
else:
return config[test_name][param][sub_param]
def gen_increment_dict (dual_port_mask):
addr_lst = dual_port_mask.split('.')
result = {}
for idx, octet_increment in enumerate(addr_lst):
octet_int = int(octet_increment)
if octet_int>0:
result[str(idx)] = octet_int
return result
def get_network_addr (ip_type = 'ipv4'):
ipv4_addr = [1, 1, 1, 0] # base ipv4 address to start generating from- 1.1.1.0
ipv6_addr = ['2001', 'DB8', 0, '2222', 0, 0, 0, 0] # base ipv6 address to start generating from- 2001:DB8:1111:2222:0:0
while True:
if ip_type == 'ipv4':
if (ipv4_addr[2] < 255):
yield [".".join( map(str, ipv4_addr) ), '255.255.255.0']
ipv4_addr[2] += 1
else: # reached defined maximum limit of address allocation
return
else: # handling ipv6 addressing
if (ipv6_addr[2] < 4369):
tmp_ipv6_addr = list(ipv6_addr)
tmp_ipv6_addr[2] = hex(tmp_ipv6_addr[2])[2:]
yield ":".join( map(str, tmp_ipv6_addr) )
ipv6_addr[2] += 1
else: # reached defined maximum limit of address allocation
return
if __name__ == "__main__":
pass
|