This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
mininfv.py
executable file
·584 lines (513 loc) · 21.2 KB
/
mininfv.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
#!/usr/bin/python
# Author: Jose Castillo Lema <josecastillolema@gmail.com>
"Main module of the mini-nfv framework"
import sys
import netaddr
import yaml
import subprocess
from collections import defaultdict
from jinja2 import Template
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
# from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
from mininet.node import OVSController
from mininet.node import RemoteController
from mininet.cli import CLI, output
MAX_VDUS = 100
VNFD = {}
VNFS = []
VNFFGS = []
VNFFGD = {}
HOSTS = []
SWITCH = {}
PORTS = defaultdict(list)
INC = 10
def parse_tosca(path):
"Parses the yaml file corresponding to the TOSCA vnfd ou vnnffgg template."
try:
yaml_file = open(path, 'r')
except IOError:
print('File does not exist')
return None
content = yaml_file.read()
parsed_file = yaml.load(content)
return parsed_file
class MyTopo(Topo):
"Creates the mininet topology"
def __init__(self, **opts):
Topo.__init__(self, **opts)
def configure_network(net, vnfd, host):
"Configures the networks."
if MULTSWITCHES:
i = 1
switchs = []
topo = vnfd['topology_template']['node_templates']
while topo.has_key('VL%s' % i):
if topo['CP%s' % i]['properties'].has_key('ip_address'):
ip_address = topo['CP%s' % i]['properties']['ip_address']
switch_name = 's' + ip_address
switchs.append(switch_name)
if not SWITCH.has_key(switch_name):
SWITCH[switch_name] = net.addSwitch(switch_name[:10])
else:
if topo['VL%s' % i]['properties']['network_name'] == 'net_mgmt':
switch_name = 's' + '192.168.120.0'
switchs.append(switch_name[:10])
if not SWITCH.has_key(switch_name):
SWITCH[switch_name] = net.addSwitch(switch_name[:10])
elif topo['VL%s' % i]['properties']['network_name'] == 'net0':
switch_name = 's' + '10.10.0.0'
switchs.append(switch_name[:10])
if not SWITCH.has_key(switch_name):
SWITCH[switch_name] = net.addSwitch(switch_name[:10])
elif topo['VL%s' % i]['properties']['network_name'] == 'net1':
switch_name = 's' + '10.10.1.0'
switchs.append(switch_name[:10])
if not SWITCH.has_key(switch_name):
SWITCH[switch_name] = net.addSwitch(switch_name[:10])
elif topo['VL%s' % i]['properties'].has_key('cidr'):
cidr = netaddr.IPNetwork(topo['VL%s' % i]['properties']['cidr'])
if topo['node_templates']['VL%s' % i]['properties'].has_key('start_ip'):
start_ip = netaddr.IPNetwork(topo['VL%s' % i]['properties']['start_ip'])
switch_name = 's%s' % start_ip.network
switchs.append(switch_name[:10])
if not SWITCH.has_key(switch_name):
SWITCH[switch_name] = net.addSwitch(switch_name[:10])
else:
switch_name = 's%s' % cidr.ip
switchs.append(switch_name[:10])
if not SWITCH.has_key(switch_name):
SWITCH[switch_name] = net.addSwitch(switch_name[:10])
i += 1
host1 = net.getNodeByName(host)
for i in switchs:
net.addLink(i, host1)
PORTS[i].append(host1)
else:
host1 = net.getNodeByName(host)
net.addLink('s1', host1)
PORTS['s1'].append(host1)
def configure_host(net, vnfd, host):
"Configures the host."
host1 = net.getNodeByName(host)
i = 1
global INC
topo = vnfd['topology_template']['node_templates']
while topo.has_key('VL%s' % i):
if topo['CP%s' % i]['properties'].has_key('ip_address'):
ip_address = topo['CP%s' % i]['properties']['ip_address']
else:
if topo['VL%s' % i]['properties']['network_name'] == 'net_mgmt':
ip_address = '192.168.120.%s/24' % INC
INC += 1
elif topo['VL%s' % i]['properties']['network_name'] == 'net0':
ip_address = '10.10.0.%s/24' % INC
INC += 1
elif topo['VL%s' % i]['properties']['network_name'] == 'net1':
ip_address = '10.10.1.%s/24' % INC
INC += 1
elif topo['VL%s' % i]['properties'].has_key('cidr'):
cidr = netaddr.IPNetwork(topo['VL%s' % i]['properties']['cidr'])
if topo['VL%s' % i]['properties'].has_key('start_ip'):
start_ip = topo['VL%s' % i]['properties']['start_ip']
ip_address = '%s/%s' % (start_ip, cidr.prefixlen)
else:
ip_address = str(cidr.ip + INC) + '/%s' % cidr.prefixlen
INC += 1
else:
ip_address = '10.0.%s.%s/24' % (i, INC)
INC += 1
host1.setIP(ip_address, intf=host + '-eth%s' % (i - 1))
c = netaddr.IPNetwork(ip_address)
host1.cmd('ip route add default via %s' % netaddr.IPAddress(c.first + 1))
PORTS[host].append(ip_address)
if topo['CP%s' % i]['properties'].has_key('mac_address'):
mac_address = topo['CP%s' % i]['properties']['mac_address']
host1.setMAC(mac_address, intf=host + '-eth%s' % (i - 1))
i += 1
def configure_host2(net, ips, host):
"Configures the host."
host1 = net.getNodeByName(host)
for i in range(len(ips)):
ip_address = netaddr.IPNetwork(ips[i])
ip_address_final = '%s/%s' % (ip_address.ip, ip_address.prefixlen)
host1.setIP(ip_address_final, intf=host + '-eth%s' % i)
host1.cmd('ip route add default via %s' % netaddr.IPAddress(ip_address.first + 1))
PORTS[host].append(ip_address)
PORTS[host].append(ip_address_final)
def list_ports(self, line):
"List all ports."
if len(line.split()) != 0:
output('Use: list_ports\n')
return None
for i in PORTS:
if i[0] == 's':
output('%s ' % i)
for j in PORTS[i]:
output('%s ' % j.IP())
else:
output(i, PORTS[i])
output('\n')
return None
def find_port(ip_address):
"Returns the host of the port if the port exists."
for i in PORTS:
if ip_address in PORTS[i]:
return i
return None
def find_port2(ip_address):
"Returns the number of the port in the swtich if the port exists."
for i in PORTS:
if i[0] == 's':
port_number = 1
for j in PORTS[i]:
if j.IP() == str(ip_address):
return port_number
port_number += 1
return None
def find_port3(host, ip_src, ip_dst):
"Returns the IP of the host if the host exists."
if host in PORTS:
for i in PORTS[host]:
i2 = netaddr.IPNetwork(i)
if i2.cidr == ip_src.cidr or i2.cidr == ip_dst.cidr:
return i2.ip
return None
def add_host(self, line):
"Adds a host to the mininet topology."
net = self.mn
if len(line.split()) < 1:
output('Use: add_host <HOST-NAME> [<IP1/masc> <IP2/masc> ...]\n')
return None
host_name = line.split()[0]
if host_name in HOSTS:
output('<HOST-NAME> already in use\n')
return None
ips = line.split()[1:]
if MULTSWITCHES:
i = 1
switchs = []
for i in ips:
try:
ip_address = netaddr.IPNetwork(i)
except netaddr.core.AddrFormatError:
output('IP format not valid: ' + i + '\n')
output('Use: add_host <HOST-NAME> [<IP1/masc> <IP2/masc> ...]\n')
return None
switch_name = 's%s' % ip_address.network
if not SWITCH.has_key(switch_name):
SWITCH[switch_name] = net.addSwitch(switch_name[:10])
switchs.append(switch_name)
host = net.addHost(host_name)
HOSTS.append(host_name)
for i in switchs:
net.addLink(i[:10], host)
PORTS[i[:10]].append(host)
else:
host = net.addHost(host_name)
HOSTS.append(host_name)
net.addLink('s1', host)
PORTS['s1'].append(host)
configure_host2(net, ips, host_name)
return None
def cloud_init(net, vnfd, host_name):
"Configures the networks."
host = net.getNodeByName(host_name)
cloudinit = vnfd['topology_template']['node_templates']['VDU1']['properties']['user_data']
output('*** Initializing VDU ' + host_name + ' ...\n')
host.cmdPrint(cloudinit)
# VNFD
def vnfd_create(self, line, jinja=False):
"Creates vnfd from template."
if len(line.split()) != 3 or line.split()[0] != '--vnfd-file':
output('Use: vnfd_create --vnfd-file <yaml file path> <VNFD-NAME>\n')
return None
file_path = line.split()[1]
vnfd = parse_tosca(file_path)
if vnfd:
vnfd_name = line.split()[2]
if not VNFD.has_key(vnfd_name):
VNFD[vnfd_name] = vnfd
else:
output('<VNFD-NAME> already in use\n')
return None
def vnfd_create_jinja(self, line):
"Creates vnfd using jinja template."
vnfd_create(self, line, jinja=True)
return None
def vnfd_list(self, line):
"Lists all VNFD uploaded."
if line:
output('Use: vnfd_list\n')
return None
for i in VNFD:
output('%s: %s\n' % (i, VNFD[i]['description']))
# output('%s' % VNFD.keys() + '\n')
return None
def vnfd_delete(self, line):
"Deletes a given vnfd."
if len(line.split()) != 1:
output('Use: vnfd_delete <VNFD-NAME>\n')
return None
vnfd_name = line.split()[0]
if VNFD.has_key(vnfd_name):
del VNFD[vnfd_name]
else:
output('<VNFD-NAME> does not exist\n')
return None
def vnfd_template_show(self, line):
"Shows the template of a given vnfd."
if len(line.split()) != 1:
output('Wrong number or arguments\n')
output('Use: vnfd_template_show <VNFD-NAME>\n')
return None
vnfd_name = line.split()[0]
output(('%s' % VNFD[vnfd_name]) + '\n')
return None
# VNF
def vnf_create(self, line, jinja=False):
"Creates vnf from vnfd previously created or directly from template."
net = self.mn
if len(line.split()) != 3 or line.split()[0] not in ['--vnfd-name', '--vnfd-file', '--vnfd-template']:
output('Use: vnf_create --vnfd-name <VNFD-NAME> <VNF-NAME>\n')
output(' vnf_create --vnfd-file <yaml file path> <VNFD-NAME>\n')
output(' vnf_create --vnfd-template <yaml file path> <VNFD-NAME>\n')
return None
if line.split()[0] in ['--vnfd-file', '--vnfd-template']:
file_path = line.split()[1]
vnfd = parse_tosca(file_path)
if jinja:
template = Template(str(vnfd))
print('template jinja',)
print("{}".format(template.render(net.values)))
else: # --vnfd-name
vnfd_name = line.split()[1]
vnfd = VNFD[vnfd_name]
if vnfd:
vnf_name = line.split()[2]
if vnf_name in VNFS:
output('<VNF-NAME> already in use\n')
return None
VNFS.append(vnf_name)
net.addHost(vnf_name)
configure_network(net, vnfd, vnf_name)
configure_host(net, vnfd, vnf_name)
if vnfd['topology_template']['node_templates']['VDU1']['properties'].has_key('user_data'):
cloud_init(net, vnfd, vnf_name)
return None
return None
def vnf_create_jinja(self, line):
"Creates vnf using jinja templates."
vnf_create(self, line, jinja=True)
return None
def vnf_list(self, line):
"Lists all vnfs created."
output('%s' % VNFS + '\n')
def vnf_delete(self, line):
"Deletes a given vnf."
net = self.mn
if len(line.split()) != 1:
output('Use: vnf_delete <VNF-NAME>\n')
return None
vnf_name = line.split()[0]
if vnf_name in VNFS:
del VNFS[VNFS.index(vnf_name)]
# net.delNode(vnf_name)
# AttributeError: 'Mininet' object has no attribute 'delNode'
else:
output('<VNF-NAME> does not exist\n')
return None
# VNFFG
def configure_vnffg(net, vnffg, vnffg_name, binds):
"NFV Orchestration function."
criteria = vnffg['topology_template']['node_templates']['Forwarding_path1']['properties']['policy']['criteria']
path = vnffg['topology_template']['node_templates']['Forwarding_path1']['properties']['path'][0]
vnfs = vnffg['topology_template']['groups']['VNFFG1']['properties']['constituent_vnfs']
if vnfs[0] != binds[0]:
output('vnf-mapping <' + binds[0] + '> not defined in template\n')
return
if len(criteria) != 1:
for i in range(len(criteria)):
if criteria[i].has_key('network_src_port_id'):
port_id = criteria[i]['network_src_port_id']
elif criteria[i].has_key('ip_src_prefix'):
ip_src = criteria[i]['ip_src_prefix']
if not find_port(ip_src):
output('ip_src_prefix ,' + ip_src + '> not exists in current environment\n')
return
ip_src = netaddr.IPNetwork(ip_src)
# ip_address_final = '%s/%s' % (ip_address.ip, ip_address.prefixlen)
elif criteria[i].has_key('ip_dst_prefix'):
ip_dst = criteria[i]['ip_dst_prefix']
if not find_port(ip_dst):
output('ip_dst_prefix ,' + ip_dst + '> not exists in current environment\n')
return
ip_dst = netaddr.IPNetwork(ip_dst)
elif criteria[i].has_key('ip_proto'):
ip_proto = criteria[i]['ip_proto']
elif criteria[i].has_key('destination_port_range'):
port_range = criteria[i]['destination_port_range']
else:
if criteria[0].has_key('network_src_port_id'):
port_id = criteria[0]['network_src_port_id']
if criteria[0].has_key('ip_src_prefix'):
ip_src = criteria[0]['ip_src_prefix']
if not find_port(ip_src):
output('ip_src_prefix ,' + ip_src + '> not exists in current environment\n')
return
if criteria[0].has_key('ip_dst_prefix'):
ip_dst = criteria[0]['ip_dst_prefix']
if not find_port(ip_dst):
output('ip_dst_prefix ,' + ip_dst + '> not exists in current environment\n')
return
if criteria[0].has_key('ip_proto'):
ip_proto = criteria[0]['ip_proto']
if criteria[i].has_key('destination_port_range'):
port_range = criteria[0]['destination_port_range']
print('vnfs0', binds[1], 'ip_src', ip_src, 'ip_dst', ip_dst)
vnf = binds[1]
vnf = net.getNodeByName(vnf)
forwarder = path['forwarder']
port_dst = find_port2(ip_dst.ip)
port_vnf = find_port2(find_port3(forwarder, ip_src, ip_dst))
VNFFGS.append(vnffg_name)
print(ip_src.ip, find_port2(ip_src.ip), ip_dst.ip, port_dst, forwarder, port_vnf)
if MULTSWITCHES:
# command = 'sudo ovs-ofctl mod-flows s192.168.1 ip,nw_src=192.168.120.1,actions=output:2,3'
command2 = "ovs-ofctl add-flow s192.168.1 priority=1,arp,actions=flood"
command = 'sudo ovs-ofctl mod-flows s192.168.1 ip,nw_src=%s,actions=output:%s,%s' % (ip_src.ip, port_dst, port_vnf)
# command = 'sudo ovs-ofctl mod-flows s192.168.1 ip,nw_src=%s,actions=output:%s' % (ip_src.ip, port_vnf)
# command2 = 'sudo ovs-ofctl mod-flows s192.168.1 in_port=%s,actions=output:%s' % (port_vnf, port_dst)
# command3 = 'sudo ovs-ofctl mod-flows s192.168.1 arp,in_port="s192.168.1-eth4",vlan_tci=0x0000/0x1fff,dl_src=12:b9:d1:5d:26:5e,dl_dst=1e:29:c2:41:d5:02,arp_spa=192.168.120.2,arp_tpa=192.168.120.1,arp_op=2,actions=output:"s192.168.1-eth3"'
s2 = subprocess.check_output(command2, shell=True)
s = subprocess.check_output(command, shell=True)
# s3 = subprocess.check_output(command2, shell=True)
print(s, s2)
else:
vnf.cmdPrint('ip addr add %s/24 brd + dev %s-eth0' % (netaddr.IPAddress(netaddr.IPNetwork(ip_src).first + 1), binds[1]))
vnf.cmdPrint('ip addr add %s/24 brd + dev %s-eth0' % (netaddr.IPAddress(netaddr.IPNetwork(ip_dst).first + 1), binds[1]))
vnf.cmdPrint("echo 1 > /proc/sys/net/ipv4/ip_forward")
# s1 = net.getNodeByName('s1')
# s1.cmdPrint('ovs-ofctl add-flow s1 priority=1,arp,actions=flood')
# s1.cmdPrint("ovs-ofctl add-flow s1 priority=65535,ip,dl_dst=00:00:00:00:01:00,actions=output:1")
# s1.cmdPrint("ovs-ofctl add-flow s1 priority=10,ip,nw_dst=10.0.10.0/24,actions=output:2")
# s1.cmdPrint("ovs-ofctl add-flow s1 priority=10,ip,nw_dst=10.0.20.0/24,actions=output:3")
def read_binding(binding):
"Translates something in the form VNF:'vnf' into ('VNF', 'vnf')"
return (binding.split(':')[0], binding.split(':')[1].replace("'", ''))
def vnffg_create(self, line, jinja=False):
"Creates vnffg from previously defined vnffgd or directly from template."
net = self.mn
# if len(line.split()) != 7:
# print 'problema e tamanho'
# print line.split()[0]
if len(line.split()) != 7 or line.split()[0] not in ['--vnffgd-name', '--vnffgd-template'] or line.split()[2] != '--vnf-mapping' or line.split()[4] != '--symmetrical':
output('''Use: vnffg-create --vnffgd-name <vnffgd-name> --vnf-mapping <vnf-mapping>
--symmetrical <boolean> <vnffg-name>\n''')
output(''' vnffg-create --vnffgd-template <vnffgd-template> --vnf-mapping <vnf-mapping>
--symmetrical <boolean> <vnffg-name>\n''')
return None
if line.split()[0] == '--vnffgd-template':
file_path = line.split()[1]
vnffg = parse_tosca(file_path)
# print 'antes: ', vnffg
if jinja:
template = Template(str(vnffg))
# print 'template jinja',
# print "{}".format(template.render(net.values))
vnffg = yaml.load("{}".format(template.render(net.values)))
# print 'despues: ', vnffg
else: # --vnffg-name
vnffg_name = line.split()[1]
vnffg = VNFFGD[vnffg_name]
binds = read_binding(line.split()[3])
if vnffg:
vnffg_name = line.split()[6]
if vnffg_name in VNFFGS:
output('<VNFFG-NAME> already in use\n')
return None
# VNFFGS.append(vnffg_name)
configure_vnffg(net, vnffg, vnffg_name, binds)
return None
return None
def vnffg_create_jinja(self, line):
"Creates vnffg using jinja templates."
vnffg_create(self, line, jinja=True)
return None
def vnffg_list(self, line):
"Lists all vnffgs created."
output('%s' % VNFFGS + '\n')
def vnffg_delete(self, line):
"Deletes a given vnffg."
net = self.mn
if len(line.split()) != 1:
output('Use: vnffg_delete <VNFFG-NAME>\n')
return None
vnffg_name = line.split()[0]
if vnffg_name in VNFFGS:
del VNFFGS[VNFFGS.index(vnffg_name)]
# net.delNode(vnf_name)
# AttributeError: 'Mininet' object has no attribute 'delNode'
else:
output('<VNFFG-NAME> does not exist\n')
return None
def do_print(self, line):
"Prints a given line."
output(line + '\n')
return None
if __name__ == '__main__':
setLogLevel('info')
usage = """Usage: mininfv [options]
The mininfv utility loads vNFs into Mininet networks from the command line.
It can create parametrized topologies, invoke the mininfv CLI, and run tests.
Options:
-h, --help show this help message and exit
--controller=CONTROLLER
remote=RemoteController
--multipleswitches creates a individual switch for every L2 topo"""
if len(sys.argv) > 2:
sys.exit(usage)
elif len(sys.argv) == 2:
if (sys.argv[1] == '-h' or sys.argv[1] == '--help'):
sys.exit(usage)
elif sys.argv[1] == '--controller=remote':
STANDALONE = False
elif sys.argv[1] == '--multipleswitches':
STANDALONE = True
MULTSWITCHES = True
print('true')
else:
sys.exit(usage)
else:
STANDALONE = True
MULTSWITCHES = False
TOPO = MyTopo()
# NET = Mininet(topo=topo, link=TCLink)
if STANDALONE:
NET = Mininet(topo=TOPO, link=TCLink, controller=OVSController)
else:
NET = Mininet(topo=TOPO, link=TCLink, controller=RemoteController)
if not MULTSWITCHES:
NET.addSwitch('s1')
NET.start()
CLI.do_add_host = add_host
CLI.do_list_ports = list_ports
CLI.do_vnfd_create = vnfd_create
CLI.do_vnfd_create_jinja = vnfd_create_jinja
CLI.do_vnfd_list = vnfd_list
CLI.do_vnfd_delete = vnfd_delete
CLI.do_vnfd_template_show = vnfd_template_show
CLI.do_vnf_create = vnf_create
CLI.do_vnf_create_jinja = vnf_create_jinja
CLI.do_vnf_list = vnf_list
CLI.do_vnf_delete = vnf_delete
CLI.do_vnffg_create = vnffg_create
CLI.do_vnffg_create_jinja = vnffg_create_jinja
CLI.do_vnffg_list = vnffg_list
CLI.do_vnffg_delete = vnffg_delete
CLI.do_print = do_print
CLI.prompt = 'mininfv> '
CLI(NET)
NET.stop()