ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from mininet.topo import Topo |
| 4 | from mininet.net import Mininet |
| 5 | from mininet.log import setLogLevel, output, info |
| 6 | from mininet.cli import CLI |
| 7 | from mininet.link import TCLink |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 8 | from mininet.util import ipStr, ipParse |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 9 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 10 | |
| 11 | from ndn import ExperimentManager |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 12 | from ndn.ndn_host import NdnHost, CpuLimitedNdnHost |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 13 | from ndn.conf_parser import parse_hosts, parse_links |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 14 | |
| 15 | import os.path, time |
| 16 | import optparse |
| 17 | import datetime |
ashu | 01b62f7 | 2015-03-12 15:16:11 -0500 | [diff] [blame] | 18 | from os.path import expanduser |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 19 | import sys |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 20 | |
| 21 | from ndn.nlsr import Nlsr, NlsrConfigGenerator |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 22 | from ndn.nfd import Nfd |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 23 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 24 | def printExperimentNames(): |
| 25 | experimentNames = ExperimentManager.getExperimentNames() |
| 26 | |
| 27 | print "Mini-NDN experiments:" |
| 28 | for experiment in experimentNames: |
| 29 | print " %s" % experiment |
| 30 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 31 | def parse_args(): |
| 32 | usage="""Usage: minindn [template_file] [ -t | --testbed ] |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 33 | If no template_file is given, ndn_utils/default-topology.conf (given sample file) |
| 34 | will be used. |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 35 | If --testbed is used, minindn will run the NDN Project Testbed. |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 36 | """ |
| 37 | |
| 38 | testbed = False |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 39 | hr = False |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 40 | |
| 41 | parser = optparse.OptionParser(usage) |
| 42 | |
| 43 | parser.add_option("-t", "--testbed", action="store_true", dest="testbed", |
| 44 | help="instantiates NDN Testbed") |
| 45 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 46 | parser.add_option("--experiment", action="store", dest="experiment", |
| 47 | help="Runs the specified experiment") |
| 48 | |
| 49 | parser.add_option("--list-experiments", action="store_true", dest="shouldListExperiments", |
| 50 | help="Lists the names of all available experiments") |
| 51 | |
| 52 | parser.add_option("--nPings", action="store", dest="nPings", type="int", |
| 53 | help="Number of pings to perform between each node in the experiment") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 54 | |
| 55 | parser.add_option("--ctime", action="store", dest="ctime", type="int", |
| 56 | help="Specify convergence time for the topology (Default 60 seconds)") |
| 57 | |
| 58 | parser.add_option("--hr", action="store_true", dest="hr", |
| 59 | help="--hr is used to turn on hyperbolic routing") |
| 60 | |
| 61 | parser.add_option("--faces", action="store", dest="faces", type="int", |
| 62 | help="Specify number of faces 0-60") |
| 63 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 64 | parser.add_option("--no-cli", action="store_false", dest="isCliEnabled", |
| 65 | help="Run experiments and exit without showing the command line interface") |
| 66 | |
| 67 | (options, arg) = parser.parse_args() |
| 68 | |
| 69 | testbed = options.testbed |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 70 | experimentName = options.experiment |
| 71 | shouldListExperiments = options.shouldListExperiments |
| 72 | nPings = options.nPings |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 73 | ctime = options.ctime |
| 74 | hr = options.hr |
| 75 | faces = options.faces |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 76 | isCliEnabled = options.isCliEnabled |
| 77 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 78 | if shouldListExperiments is not None: |
| 79 | printExperimentNames() |
| 80 | sys.exit() |
| 81 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 82 | if ctime is None: |
| 83 | ctime = 60 |
| 84 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 85 | if nPings is None: |
| 86 | nPings = 300 |
| 87 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 88 | if isCliEnabled is None: |
| 89 | isCliEnabled = True |
| 90 | |
| 91 | if len(arg) == 0 or len(arg) > 2: |
| 92 | file = '' |
| 93 | else: |
| 94 | file = arg[0] |
| 95 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 96 | return file, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 97 | |
| 98 | class NdnTopo(Topo): |
| 99 | def __init__(self, conf_arq, **opts): |
| 100 | Topo.__init__(self, **opts) |
| 101 | |
| 102 | global hosts_conf |
| 103 | global links_conf |
| 104 | hosts_conf = parse_hosts(conf_arq) |
| 105 | links_conf = parse_links(conf_arq) |
| 106 | |
| 107 | self.isTCLink = False |
| 108 | self.isLimited = False |
| 109 | |
| 110 | for host in hosts_conf: |
| 111 | if host.cpu != None and self.isLimited != True: |
| 112 | self.isLimited = True |
Ashlesh Gawande | 3a4afb1 | 2015-07-09 09:23:30 -0500 | [diff] [blame] | 113 | self.addHost(host.name, app=host.app, params=host.uri_tuples, cpu=host.cpu,cores=host.cores,cache=host.cache) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 114 | |
| 115 | for link in links_conf: |
| 116 | if len(link.linkDict) == 0: |
| 117 | self.addLink(link.h1, link.h2) |
| 118 | else: |
| 119 | self.addLink(link.h1, link.h2, **link.linkDict) |
| 120 | self.isTCLink = True |
| 121 | |
| 122 | info('Parse of ' + conf_arq + ' done.\n') |
| 123 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 124 | def execute(template_file='minindn.conf', testbed=False, experimentName=None, nPings=None, ctime=None, hr=False, faces=3, isCliEnabled=True): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 125 | "Create a network based on template_file" |
| 126 | |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 127 | install_dir='/usr/local/etc/mini-ndn/' |
| 128 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 129 | if template_file == '': |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 130 | template_file = install_dir + 'default-topology.conf' |
| 131 | |
| 132 | if testbed: |
| 133 | template_file = install_dir + 'minindn.testbed.conf' |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 134 | |
| 135 | if os.path.exists(template_file) == False: |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 136 | info('No template file given and default template file cannot be found. Exiting...\n') |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 137 | quit() |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 138 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 139 | topo = NdnTopo(template_file) |
| 140 | |
| 141 | t = datetime.datetime.now() |
| 142 | |
| 143 | if topo.isTCLink == True and topo.isLimited == True: |
| 144 | net = Mininet(topo,host=CpuLimitedNdnHost,link=TCLink) |
| 145 | elif topo.isTCLink == True and topo.isLimited == False: |
| 146 | net = Mininet(topo,host=NdnHost,link=TCLink) |
| 147 | elif topo.isTCLink == False and topo.isLimited == True: |
| 148 | net = Mininet(topo,host=CpuLimitedNdnHost) |
| 149 | else: |
| 150 | net = Mininet(topo,host=NdnHost) |
| 151 | |
| 152 | t2 = datetime.datetime.now() |
| 153 | |
| 154 | delta = t2 - t |
| 155 | |
| 156 | info('Setup time: ' + str(delta.seconds) + '\n') |
| 157 | |
| 158 | net.start() |
| 159 | |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 160 | # Giving proper IPs to intf so neighbor nodes can communicate |
| 161 | # This is one way of giving connectivity, another way could be |
| 162 | # to insert a switch between each pair of neighbors |
| 163 | ndnNetBase = "1.0.0.0" |
| 164 | interfaces = [] |
| 165 | for host in net.hosts: |
| 166 | for intf in host.intfList(): |
| 167 | link = intf.link |
| 168 | node1, node2 = link.intf1.node, link.intf2.node |
| 169 | if link.intf1 not in interfaces and link.intf2 not in interfaces: |
| 170 | interfaces.append(link.intf1) |
| 171 | interfaces.append(link.intf2) |
| 172 | node1.setIP(ipStr(ipParse(ndnNetBase) + 1) + '/30', intf=link.intf1) |
| 173 | node2.setIP(ipStr(ipParse(ndnNetBase) + 2) + '/30', intf=link.intf2) |
| 174 | ndnNetBase = ipStr(ipParse(ndnNetBase) + 4) |
| 175 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 176 | nodes = "" # Used later to check prefix name in checkFIB |
| 177 | |
| 178 | # NLSR initialization |
| 179 | for host in net.hosts: |
| 180 | nodes += str(host.name) + "," |
| 181 | |
| 182 | conf = next(x for x in hosts_conf if x.name == host.name) |
| 183 | host.nlsrParameters = conf.nlsrParameters |
| 184 | |
| 185 | if faces is not None: |
| 186 | host.nlsrParameters["max-faces-per-prefix"] = faces |
| 187 | |
| 188 | if hr is True: |
| 189 | host.nlsrParameters["hyperbolic-state"] = "on" |
| 190 | |
| 191 | # Generate NLSR configuration file |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 192 | configGenerator = NlsrConfigGenerator(host) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 193 | configGenerator.createConfigFile() |
| 194 | |
| 195 | # Start NLSR |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 196 | host.nlsr = Nlsr(host) |
| 197 | host.nlsr.start() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 198 | |
| 199 | nodes = nodes[0:-1] |
| 200 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 201 | for host in net.hosts: |
| 202 | if 'app' in host.params: |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 203 | if host.params['app'] != '': |
| 204 | app = host.params['app'] |
| 205 | print "Starting " + app + " on node " + host.name |
| 206 | print(host.cmd(app)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 207 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 208 | # Load experiment |
| 209 | if experimentName is not None: |
| 210 | print "Loading experiment: %s" % experimentName |
| 211 | |
| 212 | experimentArgs = { |
| 213 | "net": net, |
| 214 | "nodes": nodes, |
| 215 | "ctime": ctime, |
| 216 | "nPings": nPings, |
| 217 | "strategy": Nfd.STRATEGY_BEST_ROUTE_V3 |
| 218 | } |
| 219 | |
| 220 | experiment = ExperimentManager.create(experimentName, experimentArgs) |
| 221 | |
| 222 | if experiment is not None: |
| 223 | experiment.start() |
| 224 | else: |
| 225 | print "ERROR: Experiment '%s' does not exist" % experimentName |
| 226 | return |
| 227 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 228 | if isCliEnabled is True: |
| 229 | CLI(net) |
| 230 | |
| 231 | net.stop() |
| 232 | |
| 233 | if __name__ == '__main__': |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 234 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 235 | hosts_conf = [] |
| 236 | links_conf = [] |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 237 | template, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled = parse_args() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 238 | |
| 239 | setLogLevel('info') |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 240 | execute(template, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled) |