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