Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
377 lines (330 loc) · 14.7 KB
/
Copy pathmain.py
File metadata and controls
377 lines (330 loc) · 14.7 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
import os
import logging
import logging.config
from libs import os_get_env as os_lib
from libs import traf_tester as remote_libs
from configobj import ConfigObj
from argparse import ArgumentParser
import pprint
if not os.path.exists('logs'):
os.makedirs('logs')
logging.config.fileConfig('conf/logging.ini')
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
action_list = ['start', 'stop']
parser = ArgumentParser(description="Datapath traffic testing")
parser.add_argument("-f", "--cfgfile", required=True, metavar="FILE")
parser.add_argument("-a", "--action", required=True)
parser.add_argument("-t",
"--tid",
required=True,
help="Timestamp ID to reference for labeling test output")
args = parser.parse_args()
cfgfile = args.cfgfile
config = ConfigObj(cfgfile)
action = args.action
tid = args.tid if args.tid else None
osutils = os_lib.OSUtils(config)
def get_routers(tenant):
neutron = osutils.get_neutron_client_by_tenant(tenant[1])
routers = neutron.list_routers()['routers']
routers = [router for router in routers if router['tenant_id'] == tenant[0]]
router_list = []
for router in routers:
if router['name'] != 'PHYSICAL_GLOBAL_ROUTER_ID':
router_list.append(router)
return router_list
def get_ports(tenant):
neutron = osutils.get_neutron_client_by_tenant(tenant[1])
ports = neutron.list_ports()['ports']
port_list = [port for port in ports if port['tenant_id'] == tenant[0]]
return port_list
def get_subnet_detail(tenant, subnet):
neutron = osutils.get_neutron_client_by_tenant(tenant[1])
return neutron.show_subnet(subnet)
def get_instance_detail(tenant, instance):
nova = osutils.get_nova_client_by_tenant(tenant[1])
return nova.servers.get(instance)
def get_floatingips(tenant):
neutron = osutils.get_neutron_client_by_tenant(tenant[1])
return neutron.list_floatingips()
def get_router_ports(router, ports):
"""
Returns a list of router interface ports for a given router
"""
router_port_list = []
for port in ports:
if (port['device_id'] == router['id'] and
port['device_owner'] == 'network:router_interface'):
router_port_list.append(port)
return router_port_list
def get_port_subnets(tenant, port):
"""
Using the subnets for a tenant, obtain IP for a given port
"""
subnet = get_subnet_detail(tenant, port['fixed_ips'][0]['subnet_id'])['subnet']
return {'id': port['fixed_ips'][0]['subnet_id'], 'name': subnet['name'],
'ip_address': port['fixed_ips'][0]['ip_address']}
def get_instance_ports(ports):
"""
Returns a list of instance interface ports
"""
instance_port_list = []
for port in ports:
if port['device_owner'] == 'compute:nova':
instance_port_list.append(port)
return instance_port_list
def get_subnet_endpoints(tenant, ports, subnet):
"""
Returns a list of vm ips for a a given subnet
"""
endpoints = []
for port in ports:
endpoint_detail = {}
if port['fixed_ips'][0]['subnet_id'] == subnet['id']:
device_id = port['device_id'].encode('unicode_escape')
ip = port['fixed_ips'][0]['ip_address'].encode('unicode_escape')
endpoint_detail['device_id'] = device_id
endpoint_detail['ip_address'] = ip
endpoints.append(endpoint_detail)
return endpoints
def get_endpoint_floatingips(tenant, ip):
"""
Returns floating ip for a given fixed-ip.
"""
endpoint = ''
for entry in get_floatingips(tenant)['floatingips']:
if entry['fixed_ip_address'] == ip:
endpoint = entry['floating_ip_address']
return endpoint.encode('unicode_escape')
def get_default_icmp_contract():
contract = [{'name': 'allow_icmp',
'protocol': 'icmp',
'port': 'None',
'direction': 'in',
'action': 'allow'}]
return contract
def get_default_tcp_contract():
contract = [{'name': 'allow_ssh',
'protocol': 'tcp',
'port': '22',
'direction': 'in',
'action': 'allow'}]
return contract
def get_default_udp_contract():
contract = [{'name': 'allow_udp',
'protocol': 'udp',
'port': 'None',
'direction': 'in',
'action': 'allow'}]
return contract
def get_traffic_testing_endpoint(src_tenant, dest_tenant,
src_eps, dest_eps, contract):
endpoint = {'src_tenant': src_tenant,
'dest_tenant': dest_tenant,
'src_eps': src_eps,
'dest_eps': dest_eps,
'contract': contract}
return endpoint
def main():
logger.info('Start the program')
logger.info('Initialize OSUtils')
endpoints_list = []
endpoint_list = []
logger.info('Get tenant details')
tenants = osutils.get_tenants_list()
tenant_data = {}
routers = {}
for tenant in tenants.items():
router_data = {}
# retrieve all rotuers for a tenant
routers = get_routers(tenant)
ports = get_ports(tenant)
instance_ports = get_instance_ports(ports)
for router in routers:
router_detail = {}
router_detail['id'] = router['id']
router_detail['ports'] = get_router_ports(router, ports)
subnets = []
instance_list = []
for port in router_detail['ports']:
subnets.append(get_port_subnets(tenant, port))
router_detail['subnets'] = subnets
for subnet in router_detail['subnets']:
if subnet['id'] != '':
endpoint_detail = get_subnet_endpoints(tenant, instance_ports, subnet)
ip_list = []
endpoint_list = []
subnet['instances'] = endpoint_detail
for entry in endpoint_detail:
server = get_instance_detail(tenant, entry['device_id'])
if entry['device_id'] not in instance_list and \
server.status == 'ACTIVE':
instance_list.append(entry['device_id'])
ip_list.append(entry['ip_address'])
endpoint_list.append({'instances': instance_list,
'endpoints': ip_list})
subnet['endpoints'] = ip_list
router_data[router['name'].encode('unicode_escape')] = router_detail
tenant_data[tenant[1]] = router_data
# print tenant_data
test_method = config['traffic']['test_method']
contract = []
if 'icmp' in test_method or len(test_method) == 0:
contract.append(get_default_icmp_contract()[0])
if 'tcp' in test_method:
contract.append(get_default_tcp_contract()[0])
if 'udp' in test_method:
contract.append(get_default_udp_contract()[0])
print contract
tenant = config['tenants']['tenants']
if config['traffic']['type'] == 'intra-tenant':
# intra-tenant
for tenant in tenants.items():
current_router = {}
current_net = {}
routers = get_routers(tenant)
for route in routers:
rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
if len(rsubnets) > 1:
current_net = {}
for entry in rsubnets:
tsrc = []
tdest = []
tsrc.append(entry['endpoints'])
for nextep in rsubnets:
if entry['name'] != nextep['name']:
tdest.append(nextep['endpoints'])
subnet_data = {'src_eps': [ep for eps in tsrc for ep in eps],
'dest_eps': [ep for eps in tdest for ep in eps]}
current_net[entry['name']] = subnet_data
current_router[route['name']] = current_net
for route in routers:
if route['name'] in current_router:
rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
for entry in rsubnets:
if entry['name'] in current_router[route['name']]:
net = current_router[route['name']][entry['name']]
if len(net['src_eps']) > 0 and \
len(net['dest_eps']) > 0:
endpoints_list.append(
get_traffic_testing_endpoint(tenant[1],
tenant[1],
net['src_eps'],
net['dest_eps'],
contract))
logger.debug("endpoints list = %s" % (pprint.pformat(endpoints_list)))
if config['traffic']['type'] == 'inter-tenant':
# inter-tenant
for tenant in tenants.items():
current_router = {}
current_net = {}
routers = get_routers(tenant)
for route in routers:
rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
if len(rsubnets) > 0:
current_net = {}
tsrc = []
tdest = []
for entry in rsubnets:
tsrc.append(entry['endpoints'])
src_eps = [ep for eps in tsrc for ep in eps]
for nexttenant in tenants.items():
if tenant[1] != nexttenant[1]:
for nroute in tenant_data[nexttenant[1]]:
for entry in tenant_data[nexttenant[1]][nroute]['subnets']:
if len(rsubnets) > 0:
for ip in entry['endpoints']:
floating_ip = get_endpoint_floatingips(nexttenant, ip)
if floating_ip != '':
tdest.append(floating_ip)
dest_eps = tdest
if len(src_eps) > 0 and len(dest_eps) > 0:
endpoints_list.append(
get_traffic_testing_endpoint(tenant[1],
nexttenant[1],
src_eps,
dest_eps,
contract))
logger.debug("endpoints list = %s" % (pprint.pformat(endpoints_list)))
if config['traffic']['type'] == 'south-north':
# south-north
for tenant in tenants.items():
current_router = {}
current_net = {}
routers = get_routers(tenant)
for route in routers:
rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
if len(rsubnets) > 0:
current_net = {}
tsrc = []
for entry in rsubnets:
tsrc.append(entry['endpoints'])
src_eps = [ep for eps in tsrc for ep in eps]
if config['external_host']['host'] != '':
dest_eps = [config['external_host']['host']]
else:
dest_eps = []
print "Please specify external host in the config"
if len(src_eps) > 0 and len(dest_eps) > 0:
endpoints_list.append(
get_traffic_testing_endpoint(tenant[1],
'External Host',
src_eps,
dest_eps,
contract))
logger.debug("endpoints list = %s" % (pprint.pformat(endpoints_list)))
if config['traffic']['type'] == 'north-south':
# north-south
for tenant in tenants.items():
current_router = {}
current_net = {}
routers = get_routers(tenant)
for route in routers:
rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
if len(rsubnets) > 0:
current_net = {}
tdest = []
if config['external_host']['host'] != '':
src_eps = [config['external_host']['host']]
else:
src_eps = []
print "Please specify external host in the config"
for entry in rsubnets:
for ip in entry['endpoints']:
floating_ip = get_endpoint_floatingips(tenant, ip)
if floating_ip != '':
tdest.append(floating_ip)
dest_eps = tdest
if len(src_eps) > 0 and len(dest_eps) > 0:
endpoints_list.append(
get_traffic_testing_endpoint('External Host',
tenant[1],
src_eps,
dest_eps,
contract))
logger.debug("endpoints list = %s" % (pprint.pformat(endpoints_list)))
# src_ip_list = ['192.168.61.131']
# dest_ip_list = ['192.168.61.129', '192.168.61.132']
# contract = [{'name': 'allow_ssh',
# 'protocol': 'tcp',
# 'port': 22,
# 'direction': 'in',
# 'action': 'allow'},
# {'name': 'allow_icmp',
# 'protocol': 'icmp',
# 'port': 'None',
# 'direction': 'in',
# 'action': 'allow'}]
# endpoints = {'src_tenant': 'dummy_tenant',
# 'dest_tenant': 'dummy_tenant',
# 'src_eps': src_ip_list,
# 'dest_eps': dest_ip_list,
# 'contract': contract}
# endpoints_list = [endpoints]
if tid:
remote_libs.start_task(config, endpoints_list, action, tid)
else:
remote_libs.start_task(config, endpoints_list, action)
if __name__ == '__main__':
main()