carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from mininet.topo import Topo |
| 4 | from mininet.net import Mininet |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 5 | from mininet.log import setLogLevel, output, info |
| 6 | from mininet.cli import CLI |
| 7 | from mininet.node import CPULimitedCCNHost, CCNHost |
| 8 | from mininet.link import TCLink |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 9 | from mininet.conf_parser import parse_hosts,parse_routers, parse_links |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 10 | import os.path, time |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 11 | import optparse |
| 12 | import datetime |
Caio | 188d2f3 | 2015-01-22 23:35:08 -0200 | [diff] [blame] | 13 | import pdb |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 14 | |
| 15 | def parse_args(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 16 | usage="""Usage: miniccnx [template_file] [ -t | --testbed ] |
| 17 | If no template_file is given, will try to load template |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 18 | from file miniccnx.conf in the current directory. |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 19 | If --testbed is used, miniccnx will run the NDN Project Testbed. |
| 20 | This assumes you are in the testbed directory in the miniccnx installation |
| 21 | directory. |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 22 | """ |
| 23 | |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 24 | testbed = False |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 25 | parser = optparse.OptionParser(usage) |
| 26 | |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 27 | parser.add_option("-t", "--testbed", action="store_true", dest="testbed", |
| 28 | help="instantiates NDN Testbed") |
| 29 | |
| 30 | (options, arg) = parser.parse_args() |
| 31 | |
| 32 | testbed = options.testbed |
| 33 | |
| 34 | if len(arg) == 0 or len(arg) > 2: |
| 35 | file = '' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 36 | else: |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 37 | file = arg[0] |
| 38 | |
| 39 | return file, testbed |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 40 | |
| 41 | |
| 42 | class CCNTopo(Topo): |
| 43 | def __init__(self, conf_arq, **opts): |
| 44 | Topo.__init__(self, **opts) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 45 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 46 | hosts_conf = parse_hosts(conf_arq) |
Caio | 188d2f3 | 2015-01-22 23:35:08 -0200 | [diff] [blame] | 47 | routers_conf = parse_routers(conf_arq) |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 48 | links_conf = parse_links(conf_arq) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 49 | |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 50 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 51 | self.isTCLink = False |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 52 | self.isLimited = False |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 53 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 54 | for host in hosts_conf: |
carlosmscabral | 3d060fc | 2013-07-18 13:18:09 -0300 | [diff] [blame] | 55 | # print host |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 56 | if host.cpu != None and self.isLimited != True: |
| 57 | self.isLimited = True |
carlosmscabral | b9d85b7 | 2013-07-15 15:24:33 -0300 | [diff] [blame] | 58 | self.addHost(host.name, app=host.app, fib=host.uri_tuples,cpu=host.cpu,cores=host.cores,cache=host.cache) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 59 | |
Caio | 188d2f3 | 2015-01-22 23:35:08 -0200 | [diff] [blame] | 60 | for router in routers_conf: |
| 61 | if router.cpu != None and self.isLimited != True: |
| 62 | self.isLimited = True |
| 63 | self.addHost(router.name,fib=router.uri_tuples,cpu=router.cpu,cores=router.cores, cache=router.cache) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 64 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 65 | for link in links_conf: |
| 66 | if len(link.linkDict) == 0: |
Caio | 188d2f3 | 2015-01-22 23:35:08 -0200 | [diff] [blame] | 67 | #pdb.set_trace() |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 68 | self.addLink(link.h1, link.h2) |
| 69 | else: |
| 70 | self.addLink(link.h1, link.h2, **link.linkDict) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 71 | self.isTCLink = True |
| 72 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 73 | info('Parse of ' + conf_arq + ' done.\n') |
| 74 | |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 75 | def execute(template_file='miniccnx.conf', testbed=False): |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 76 | "Create a network based on template_file" |
| 77 | |
| 78 | if template_file == '': |
| 79 | template_file='miniccnx.conf' |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 80 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 81 | if os.path.exists(template_file) == False: |
| 82 | info('No template file given and default template file miniccnx.conf not found. Exiting...\n') |
| 83 | quit() |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 84 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 85 | topo = CCNTopo(template_file) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 86 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 87 | t = datetime.datetime.now() |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 88 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 89 | if topo.isTCLink == True and topo.isLimited == True: |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 90 | net = Mininet(topo,host=CPULimitedCCNHost,link=TCLink) |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 91 | elif topo.isTCLink == True and topo.isLimited == False: |
| 92 | net = Mininet(topo,host=CCNHost,link=TCLink) |
| 93 | elif topo.isTCLink == False and topo.isLimited == True: |
| 94 | net = Mininet(topo,host=CPULimitedCCNHost) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 95 | else: |
| 96 | net = Mininet(topo,host=CCNHost) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 97 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 98 | t2 = datetime.datetime.now() |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 99 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 100 | delta = t2 - t |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 101 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 102 | info('Setup time: ' + str(delta.seconds) + '\n') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 103 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 104 | net.start() |
| 105 | |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 106 | if testbed == True: |
| 107 | info('Starting OSPFN ...\n') |
| 108 | for host in net.hosts: |
| 109 | host.cmd("cd {0}".format(host.name)) |
| 110 | host.cmd("./routing.sh {0}".format(host.name)) |
| 111 | |
| 112 | time.sleep(60) |
| 113 | |
| 114 | for host in net.hosts: |
| 115 | host.cmd("./ospfn-start.sh {0}".format(host.name)) |
| 116 | |
| 117 | info('OSPFN configuration completed!\n') |
| 118 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 119 | for host in net.hosts: |
| 120 | if 'app' in host.params: |
| 121 | if host.params['app'] != '_': |
| 122 | host.cmd(host.params['app']) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 123 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 124 | CLI(net) |
| 125 | net.stop() |
| 126 | |
| 127 | if __name__ == '__main__': |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 128 | |
| 129 | template, testbed = parse_args() |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 130 | setLogLevel('info') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 131 | execute(template, testbed) |