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 | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 82 | def printExperimentNames(): |
| 83 | experimentNames = ExperimentManager.getExperimentNames() |
| 84 | |
| 85 | print "Mini-NDN experiments:" |
| 86 | for experiment in experimentNames: |
| 87 | print " %s" % experiment |
| 88 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 89 | def parse_args(): |
| 90 | usage="""Usage: minindn [template_file] [ -t | --testbed ] |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 91 | If no template_file is given, ndn_utils/default-topology.conf (given sample file) |
| 92 | will be used. |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 93 | If --testbed is used, minindn will run the NDN Project Testbed. |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 94 | """ |
| 95 | |
| 96 | testbed = False |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 97 | hr = False |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 98 | |
| 99 | parser = optparse.OptionParser(usage) |
| 100 | |
| 101 | parser.add_option("-t", "--testbed", action="store_true", dest="testbed", |
| 102 | help="instantiates NDN Testbed") |
| 103 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 104 | parser.add_option("--experiment", action="store", dest="experiment", |
| 105 | help="Runs the specified experiment") |
| 106 | |
| 107 | parser.add_option("--list-experiments", action="store_true", dest="shouldListExperiments", |
| 108 | help="Lists the names of all available experiments") |
| 109 | |
| 110 | parser.add_option("--nPings", action="store", dest="nPings", type="int", |
| 111 | help="Number of pings to perform between each node in the experiment") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 112 | |
| 113 | parser.add_option("--ctime", action="store", dest="ctime", type="int", |
| 114 | help="Specify convergence time for the topology (Default 60 seconds)") |
| 115 | |
| 116 | parser.add_option("--hr", action="store_true", dest="hr", |
| 117 | help="--hr is used to turn on hyperbolic routing") |
| 118 | |
| 119 | parser.add_option("--faces", action="store", dest="faces", type="int", |
| 120 | help="Specify number of faces 0-60") |
| 121 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 122 | parser.add_option("--no-cli", action="store_false", dest="isCliEnabled", |
| 123 | help="Run experiments and exit without showing the command line interface") |
| 124 | |
| 125 | (options, arg) = parser.parse_args() |
| 126 | |
| 127 | testbed = options.testbed |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 128 | experimentName = options.experiment |
| 129 | shouldListExperiments = options.shouldListExperiments |
| 130 | nPings = options.nPings |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 131 | ctime = options.ctime |
| 132 | hr = options.hr |
| 133 | faces = options.faces |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 134 | isCliEnabled = options.isCliEnabled |
| 135 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 136 | if shouldListExperiments is not None: |
| 137 | printExperimentNames() |
| 138 | sys.exit() |
| 139 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 140 | if ctime is None: |
| 141 | ctime = 60 |
| 142 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 143 | if nPings is None: |
| 144 | nPings = 300 |
| 145 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 146 | if isCliEnabled is None: |
| 147 | isCliEnabled = True |
| 148 | |
| 149 | if len(arg) == 0 or len(arg) > 2: |
| 150 | file = '' |
| 151 | else: |
| 152 | file = arg[0] |
| 153 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 154 | return file, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 155 | |
| 156 | class NdnTopo(Topo): |
| 157 | def __init__(self, conf_arq, **opts): |
| 158 | Topo.__init__(self, **opts) |
| 159 | |
| 160 | global hosts_conf |
| 161 | global links_conf |
| 162 | hosts_conf = parse_hosts(conf_arq) |
| 163 | links_conf = parse_links(conf_arq) |
| 164 | |
| 165 | self.isTCLink = False |
| 166 | self.isLimited = False |
| 167 | |
| 168 | for host in hosts_conf: |
| 169 | if host.cpu != None and self.isLimited != True: |
| 170 | self.isLimited = True |
Ashlesh Gawande | 3a4afb1 | 2015-07-09 09:23:30 -0500 | [diff] [blame] | 171 | 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] | 172 | |
| 173 | for link in links_conf: |
| 174 | if len(link.linkDict) == 0: |
| 175 | self.addLink(link.h1, link.h2) |
| 176 | else: |
| 177 | self.addLink(link.h1, link.h2, **link.linkDict) |
| 178 | self.isTCLink = True |
| 179 | |
| 180 | info('Parse of ' + conf_arq + ' done.\n') |
| 181 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 182 | 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] | 183 | "Create a network based on template_file" |
| 184 | |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 185 | install_dir='/usr/local/etc/mini-ndn/' |
| 186 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 187 | if template_file == '': |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 188 | template_file = install_dir + 'default-topology.conf' |
| 189 | |
| 190 | if testbed: |
| 191 | template_file = install_dir + 'minindn.testbed.conf' |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 192 | |
| 193 | if os.path.exists(template_file) == False: |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 194 | 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] | 195 | quit() |
Ashlesh Gawande | 20f7076 | 2015-06-17 15:18:19 -0500 | [diff] [blame] | 196 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 197 | topo = NdnTopo(template_file) |
| 198 | |
| 199 | t = datetime.datetime.now() |
| 200 | |
| 201 | if topo.isTCLink == True and topo.isLimited == True: |
| 202 | net = Mininet(topo,host=CpuLimitedNdnHost,link=TCLink) |
| 203 | elif topo.isTCLink == True and topo.isLimited == False: |
| 204 | net = Mininet(topo,host=NdnHost,link=TCLink) |
| 205 | elif topo.isTCLink == False and topo.isLimited == True: |
| 206 | net = Mininet(topo,host=CpuLimitedNdnHost) |
| 207 | else: |
| 208 | net = Mininet(topo,host=NdnHost) |
| 209 | |
| 210 | t2 = datetime.datetime.now() |
| 211 | |
| 212 | delta = t2 - t |
| 213 | |
| 214 | info('Setup time: ' + str(delta.seconds) + '\n') |
| 215 | |
| 216 | net.start() |
| 217 | |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 218 | # Giving proper IPs to intf so neighbor nodes can communicate |
| 219 | # This is one way of giving connectivity, another way could be |
| 220 | # to insert a switch between each pair of neighbors |
| 221 | ndnNetBase = "1.0.0.0" |
| 222 | interfaces = [] |
| 223 | for host in net.hosts: |
| 224 | for intf in host.intfList(): |
| 225 | link = intf.link |
| 226 | node1, node2 = link.intf1.node, link.intf2.node |
| 227 | if link.intf1 not in interfaces and link.intf2 not in interfaces: |
| 228 | interfaces.append(link.intf1) |
| 229 | interfaces.append(link.intf2) |
| 230 | node1.setIP(ipStr(ipParse(ndnNetBase) + 1) + '/30', intf=link.intf1) |
| 231 | node2.setIP(ipStr(ipParse(ndnNetBase) + 2) + '/30', intf=link.intf2) |
| 232 | ndnNetBase = ipStr(ipParse(ndnNetBase) + 4) |
| 233 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 234 | nodes = "" # Used later to check prefix name in checkFIB |
| 235 | |
| 236 | # NLSR initialization |
| 237 | for host in net.hosts: |
| 238 | nodes += str(host.name) + "," |
| 239 | |
| 240 | conf = next(x for x in hosts_conf if x.name == host.name) |
| 241 | host.nlsrParameters = conf.nlsrParameters |
| 242 | |
| 243 | if faces is not None: |
| 244 | host.nlsrParameters["max-faces-per-prefix"] = faces |
| 245 | |
| 246 | if hr is True: |
| 247 | host.nlsrParameters["hyperbolic-state"] = "on" |
| 248 | |
| 249 | # Generate NLSR configuration file |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 250 | configGenerator = NlsrConfigGenerator(host) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 251 | configGenerator.createConfigFile() |
| 252 | |
| 253 | # Start NLSR |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 254 | host.nlsr = Nlsr(host) |
| 255 | host.nlsr.start() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 256 | |
| 257 | nodes = nodes[0:-1] |
| 258 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 259 | for host in net.hosts: |
| 260 | if 'app' in host.params: |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 261 | if host.params['app'] != '': |
| 262 | app = host.params['app'] |
| 263 | print "Starting " + app + " on node " + host.name |
| 264 | print(host.cmd(app)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 265 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 266 | # Load experiment |
| 267 | if experimentName is not None: |
| 268 | print "Loading experiment: %s" % experimentName |
| 269 | |
| 270 | experimentArgs = { |
| 271 | "net": net, |
| 272 | "nodes": nodes, |
| 273 | "ctime": ctime, |
| 274 | "nPings": nPings, |
| 275 | "strategy": Nfd.STRATEGY_BEST_ROUTE_V3 |
| 276 | } |
| 277 | |
| 278 | experiment = ExperimentManager.create(experimentName, experimentArgs) |
| 279 | |
| 280 | if experiment is not None: |
| 281 | experiment.start() |
| 282 | else: |
| 283 | print "ERROR: Experiment '%s' does not exist" % experimentName |
| 284 | return |
| 285 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 286 | if isCliEnabled is True: |
| 287 | CLI(net) |
| 288 | |
| 289 | net.stop() |
| 290 | |
| 291 | if __name__ == '__main__': |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 292 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 293 | hosts_conf = [] |
| 294 | links_conf = [] |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 295 | template, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled = parse_args() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 296 | |
| 297 | setLogLevel('info') |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 298 | execute(template, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled) |