Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2015 The University of Memphis, |
| 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 6 | # |
| 7 | # This file is part of Mini-NDN. |
| 8 | # See AUTHORS.md for a complete list of Mini-NDN authors and contributors. |
| 9 | # |
| 10 | # Mini-NDN is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 3 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # Mini-NDN is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with Mini-NDN, e.g., in COPYING.md file. |
| 22 | # If not, see <http://www.gnu.org/licenses/>. |
| 23 | # |
| 24 | # This file incorporates work covered by the following copyright and |
| 25 | # permission notice: |
| 26 | # |
| 27 | # Mininet 2.2.1 License |
| 28 | # |
| 29 | # Copyright (c) 2013-2015 Open Networking Laboratory |
| 30 | # Copyright (c) 2009-2012 Bob Lantz and The Board of Trustees of |
| 31 | # The Leland Stanford Junior University |
| 32 | # |
| 33 | # Original authors: Bob Lantz and Brandon Heller |
| 34 | # |
| 35 | # We are making Mininet available for public use and benefit with the |
| 36 | # expectation that others will use, modify and enhance the Software and |
| 37 | # contribute those enhancements back to the community. However, since we |
| 38 | # would like to make the Software available for broadest use, with as few |
| 39 | # restrictions as possible permission is hereby granted, free of charge, to |
| 40 | # any person obtaining a copy of this Software to deal in the Software |
| 41 | # under the copyrights without restriction, including without limitation |
| 42 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 43 | # and/or sell copies of the Software, and to permit persons to whom the |
| 44 | # Software is furnished to do so, subject to the following conditions: |
| 45 | # |
| 46 | # The above copyright notice and this permission notice shall be included |
| 47 | # in all copies or substantial portions of the Software. |
| 48 | # |
| 49 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 50 | # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 51 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 52 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 53 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 54 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 55 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 56 | # |
| 57 | # The name and trademarks of copyright holder(s) may NOT be used in |
| 58 | # advertising or publicity pertaining to the Software or any derivatives |
| 59 | # without specific, written prior permission. |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 60 | |
| 61 | from mininet.topo import Topo |
| 62 | from mininet.net import Mininet |
| 63 | from mininet.log import setLogLevel, output, info |
| 64 | from mininet.cli import CLI |
| 65 | from mininet.link import TCLink |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 66 | from mininet.util import ipStr, ipParse |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 67 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 68 | |
| 69 | from ndn import ExperimentManager |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 70 | from ndn.ndn_host import NdnHost, CpuLimitedNdnHost |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 71 | from ndn.conf_parser import parse_hosts, parse_links |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 72 | |
| 73 | import os.path, time |
| 74 | import optparse |
| 75 | import datetime |
ashu | 01b62f7 | 2015-03-12 15:16:11 -0500 | [diff] [blame] | 76 | from os.path import expanduser |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 77 | import sys |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 78 | |
| 79 | from ndn.nlsr import Nlsr, NlsrConfigGenerator |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 80 | from ndn.nfd import Nfd |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 81 | |
Vince Lehman | e9f116d | 2015-07-15 10:40:21 -0500 | [diff] [blame] | 82 | VERSION_NUMBER = "0.1.0" |
| 83 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 84 | def printExperimentNames(option, opt, value, parser): |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 85 | experimentNames = ExperimentManager.getExperimentNames() |
| 86 | |
| 87 | print "Mini-NDN experiments:" |
| 88 | for experiment in experimentNames: |
| 89 | print " %s" % experiment |
| 90 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 91 | sys.exit() |
| 92 | |
Vince Lehman | e9f116d | 2015-07-15 10:40:21 -0500 | [diff] [blame] | 93 | def printVersion(option, opt, value, parser): |
| 94 | print "Mini-NDN v%s" % VERSION_NUMBER |
| 95 | sys.exit() |
| 96 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 97 | class ProgramOptions: |
| 98 | def __init__(self): |
| 99 | self.ctime = 60 |
| 100 | self.experimentName = None |
| 101 | self.nFaces = 3 |
| 102 | self.templateFile = "minindn.conf" |
| 103 | self.hr = False |
| 104 | self.isCliEnabled = True |
| 105 | self.nPings = 300 |
| 106 | self.testbed = False |
Ashlesh Gawande | 1b66369 | 2015-10-14 16:38:10 -0500 | [diff] [blame^] | 107 | self.workDir = "/tmp" |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 108 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 109 | def parse_args(): |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 110 | usage = """Usage: minindn [template_file] [ -t | --testbed ] |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 111 | If no template_file is given, ndn_utils/default-topology.conf (given sample file) |
| 112 | will be used. |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 113 | If --testbed is used, minindn will run the NDN Project Testbed. |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 114 | """ |
| 115 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 116 | parser = optparse.OptionParser(usage) |
| 117 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 118 | parser.add_option("--ctime", action="store", dest="ctime", type="int", default=60, |
| 119 | help="Specify convergence time for the topology (Default: 60 seconds)") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 120 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 121 | parser.add_option("--experiment", action="store", dest="experiment", |
| 122 | help="Runs the specified experiment") |
| 123 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 124 | parser.add_option("--faces", action="store", dest="faces", type="int", default=3, |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 125 | help="Specify number of faces 0-60") |
| 126 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 127 | parser.add_option("--hr", action="store_true", dest="hr", default=False, |
| 128 | help="--hr is used to turn on hyperbolic routing") |
| 129 | |
| 130 | parser.add_option("--list-experiments", action="callback", callback=printExperimentNames, |
| 131 | help="Lists the names of all available experiments") |
| 132 | |
| 133 | parser.add_option("--no-cli", action="store_false", dest="isCliEnabled", default=True, |
| 134 | help="Run experiments and exit without showing the command line interface") |
| 135 | |
| 136 | parser.add_option("--nPings", action="store", dest="nPings", type="int", default=300, |
| 137 | help="Number of pings to perform between each node in the experiment") |
| 138 | |
| 139 | parser.add_option("-t", "--testbed", action="store_true", dest="testbed", default=False, |
| 140 | help="instantiates NDN Testbed") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 141 | |
Ashlesh Gawande | 1b66369 | 2015-10-14 16:38:10 -0500 | [diff] [blame^] | 142 | parser.add_option("--work-dir", action="store", dest="workDir", default="/tmp", |
| 143 | help="Specify the working directory; default is /tmp") |
| 144 | |
Vince Lehman | e9f116d | 2015-07-15 10:40:21 -0500 | [diff] [blame] | 145 | parser.add_option('--version', '-V', action='callback', callback=printVersion, |
| 146 | help='Displays version information') |
| 147 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 148 | (args, arg) = parser.parse_args() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 149 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 150 | options = ProgramOptions() |
| 151 | options.ctime = args.ctime |
| 152 | options.experimentName = args.experiment |
| 153 | options.nFaces = args.faces |
| 154 | options.hr = args.hr |
| 155 | options.isCliEnabled = args.isCliEnabled |
| 156 | options.nPings = args.nPings |
| 157 | options.testbed = args.testbed |
Ashlesh Gawande | 1b66369 | 2015-10-14 16:38:10 -0500 | [diff] [blame^] | 158 | options.workDir = args.workDir |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 159 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 160 | if options.experimentName is not None and options.experimentName not in ExperimentManager.getExperimentNames(): |
| 161 | print("No experiment named %s" % options.experimentName) |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 162 | sys.exit() |
| 163 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 164 | if len(arg) == 0 or len(arg) > 2: |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 165 | options.templateFile = '' |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 166 | else: |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 167 | options.templateFile = arg[0] |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 168 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 169 | return options |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 170 | |
| 171 | class NdnTopo(Topo): |
Ashlesh Gawande | 1b66369 | 2015-10-14 16:38:10 -0500 | [diff] [blame^] | 172 | def __init__(self, conf_arq, workDir, **opts): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 173 | Topo.__init__(self, **opts) |
| 174 | |
| 175 | global hosts_conf |
| 176 | global links_conf |
| 177 | hosts_conf = parse_hosts(conf_arq) |
| 178 | links_conf = parse_links(conf_arq) |
| 179 | |
| 180 | self.isTCLink = False |
| 181 | self.isLimited = False |
| 182 | |
| 183 | for host in hosts_conf: |
| 184 | if host.cpu != None and self.isLimited != True: |
| 185 | self.isLimited = True |
Ashlesh Gawande | 1b66369 | 2015-10-14 16:38:10 -0500 | [diff] [blame^] | 186 | self.addHost(host.name, app=host.app, params=host.uri_tuples, cpu=host.cpu,cores=host.cores,cache=host.cache, workdir=workDir) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 187 | |
| 188 | for link in links_conf: |
| 189 | if len(link.linkDict) == 0: |
| 190 | self.addLink(link.h1, link.h2) |
| 191 | else: |
| 192 | self.addLink(link.h1, link.h2, **link.linkDict) |
| 193 | self.isTCLink = True |
| 194 | |
| 195 | info('Parse of ' + conf_arq + ' done.\n') |
| 196 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 197 | def execute(options): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 198 | "Create a network based on template_file" |
| 199 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 200 | template_file = options.templateFile |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 201 | install_dir='/usr/local/etc/mini-ndn/' |
| 202 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 203 | if template_file == '': |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 204 | template_file = install_dir + 'default-topology.conf' |
| 205 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 206 | if options.testbed: |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 207 | template_file = install_dir + 'minindn.testbed.conf' |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 208 | |
| 209 | if os.path.exists(template_file) == False: |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 210 | 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] | 211 | quit() |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 212 | |
Vince Lehman | 498625b | 2015-10-21 14:25:24 -0500 | [diff] [blame] | 213 | # Update nfd.conf file used by Mini-NDN to match the currently installed version of NFD |
| 214 | nfdConfFile = "%s/nfd.conf" % install_dir |
| 215 | os.system("sudo cp /usr/local/etc/ndn/nfd.conf.sample %s" % nfdConfFile) |
| 216 | os.system("sudo sed -i \'s|default_level [A-Z]*$|default_level $LOG_LEVEL|g\' %s" % nfdConfFile) |
| 217 | |
Ashlesh Gawande | 1b66369 | 2015-10-14 16:38:10 -0500 | [diff] [blame^] | 218 | topo = NdnTopo(template_file, options.workDir) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 219 | |
| 220 | t = datetime.datetime.now() |
| 221 | |
| 222 | if topo.isTCLink == True and topo.isLimited == True: |
| 223 | net = Mininet(topo,host=CpuLimitedNdnHost,link=TCLink) |
| 224 | elif topo.isTCLink == True and topo.isLimited == False: |
| 225 | net = Mininet(topo,host=NdnHost,link=TCLink) |
| 226 | elif topo.isTCLink == False and topo.isLimited == True: |
| 227 | net = Mininet(topo,host=CpuLimitedNdnHost) |
| 228 | else: |
| 229 | net = Mininet(topo,host=NdnHost) |
| 230 | |
| 231 | t2 = datetime.datetime.now() |
| 232 | |
| 233 | delta = t2 - t |
| 234 | |
| 235 | info('Setup time: ' + str(delta.seconds) + '\n') |
| 236 | |
| 237 | net.start() |
| 238 | |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 239 | # Giving proper IPs to intf so neighbor nodes can communicate |
| 240 | # This is one way of giving connectivity, another way could be |
| 241 | # to insert a switch between each pair of neighbors |
| 242 | ndnNetBase = "1.0.0.0" |
| 243 | interfaces = [] |
| 244 | for host in net.hosts: |
| 245 | for intf in host.intfList(): |
| 246 | link = intf.link |
| 247 | node1, node2 = link.intf1.node, link.intf2.node |
| 248 | if link.intf1 not in interfaces and link.intf2 not in interfaces: |
| 249 | interfaces.append(link.intf1) |
| 250 | interfaces.append(link.intf2) |
| 251 | node1.setIP(ipStr(ipParse(ndnNetBase) + 1) + '/30', intf=link.intf1) |
| 252 | node2.setIP(ipStr(ipParse(ndnNetBase) + 2) + '/30', intf=link.intf2) |
| 253 | ndnNetBase = ipStr(ipParse(ndnNetBase) + 4) |
| 254 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 255 | nodes = "" # Used later to check prefix name in checkFIB |
| 256 | |
| 257 | # NLSR initialization |
| 258 | for host in net.hosts: |
| 259 | nodes += str(host.name) + "," |
| 260 | |
| 261 | conf = next(x for x in hosts_conf if x.name == host.name) |
| 262 | host.nlsrParameters = conf.nlsrParameters |
| 263 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 264 | if options.nFaces is not None: |
| 265 | host.nlsrParameters["max-faces-per-prefix"] = options.nFaces |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 266 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 267 | if options.hr is True: |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 268 | host.nlsrParameters["hyperbolic-state"] = "on" |
| 269 | |
| 270 | # Generate NLSR configuration file |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 271 | configGenerator = NlsrConfigGenerator(host) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 272 | configGenerator.createConfigFile() |
| 273 | |
| 274 | # Start NLSR |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 275 | host.nlsr = Nlsr(host) |
| 276 | host.nlsr.start() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 277 | |
| 278 | nodes = nodes[0:-1] |
| 279 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 280 | for host in net.hosts: |
| 281 | if 'app' in host.params: |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 282 | if host.params['app'] != '': |
| 283 | app = host.params['app'] |
| 284 | print "Starting " + app + " on node " + host.name |
| 285 | print(host.cmd(app)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 286 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 287 | # Load experiment |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 288 | experimentName = options.experimentName |
| 289 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 290 | if experimentName is not None: |
| 291 | print "Loading experiment: %s" % experimentName |
| 292 | |
| 293 | experimentArgs = { |
| 294 | "net": net, |
| 295 | "nodes": nodes, |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 296 | "ctime": options.ctime, |
| 297 | "nPings": options.nPings, |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 298 | "strategy": Nfd.STRATEGY_BEST_ROUTE_V3 |
| 299 | } |
| 300 | |
| 301 | experiment = ExperimentManager.create(experimentName, experimentArgs) |
| 302 | |
| 303 | if experiment is not None: |
| 304 | experiment.start() |
| 305 | else: |
| 306 | print "ERROR: Experiment '%s' does not exist" % experimentName |
| 307 | return |
| 308 | |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 309 | if options.isCliEnabled is True: |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 310 | CLI(net) |
| 311 | |
| 312 | net.stop() |
| 313 | |
| 314 | if __name__ == '__main__': |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 315 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 316 | hosts_conf = [] |
| 317 | links_conf = [] |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 318 | |
| 319 | options = parse_args() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 320 | |
| 321 | setLogLevel('info') |
Vince Lehman | 194be24 | 2015-10-15 18:01:42 -0500 | [diff] [blame] | 322 | execute(options) |