blob: c8530e6a58788bb36e1d419a8901f553aa8c2f5b [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Vince Lehman5d5a5662015-12-02 12:33:12 -06003# Copyright (C) 2015-2016, The University of Memphis,
4# Arizona Board of Regents,
5# Regents of the University of California.
Vince Lehmanb8b18062015-07-14 13:07:22 -05006#
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.
ashuef3490b2015-02-17 11:01:04 -060060
61from mininet.topo import Topo
62from mininet.net import Mininet
63from mininet.log import setLogLevel, output, info
64from mininet.cli import CLI
65from mininet.link import TCLink
ashu2ad32e22015-05-29 13:37:40 -050066from mininet.util import ipStr, ipParse
ashuef3490b2015-02-17 11:01:04 -060067
Vince Lehman3b8bc652015-06-18 15:01:47 -050068
69from ndn import ExperimentManager
ashuef3490b2015-02-17 11:01:04 -060070from ndn.ndn_host import NdnHost, CpuLimitedNdnHost
Vince Lehmanfbd47c92015-10-14 16:00:06 -050071from ndn.conf_parser import parse_hosts, parse_switches, parse_links
ashuef3490b2015-02-17 11:01:04 -060072
73import os.path, time
Yucheng Zhang14aa5962016-04-05 17:10:15 -050074import shutil
ashuef3490b2015-02-17 11:01:04 -060075import optparse
76import datetime
ashu01b62f72015-03-12 15:16:11 -050077from os.path import expanduser
Vince Lehman3b8bc652015-06-18 15:01:47 -050078import sys
Yucheng Zhang14aa5962016-04-05 17:10:15 -050079from subprocess import call
ashuef3490b2015-02-17 11:01:04 -060080
81from ndn.nlsr import Nlsr, NlsrConfigGenerator
ashu34c3ee02015-03-25 14:41:14 -050082from ndn.nfd import Nfd
ashuef3490b2015-02-17 11:01:04 -060083
Vince Lehman68b08152015-10-23 10:15:36 -050084VERSION_NUMBER = "0.1.1"
Vince Lehmane9f116d2015-07-15 10:40:21 -050085
Vince Lehman194be242015-10-15 18:01:42 -050086def printExperimentNames(option, opt, value, parser):
Vince Lehman3b8bc652015-06-18 15:01:47 -050087 experimentNames = ExperimentManager.getExperimentNames()
88
89 print "Mini-NDN experiments:"
90 for experiment in experimentNames:
91 print " %s" % experiment
92
Vince Lehman194be242015-10-15 18:01:42 -050093 sys.exit()
94
Vince Lehmane9f116d2015-07-15 10:40:21 -050095def printVersion(option, opt, value, parser):
96 print "Mini-NDN v%s" % VERSION_NUMBER
97 sys.exit()
98
Vince Lehman194be242015-10-15 18:01:42 -050099class ProgramOptions:
100 def __init__(self):
101 self.ctime = 60
102 self.experimentName = None
103 self.nFaces = 3
104 self.templateFile = "minindn.conf"
105 self.hr = False
106 self.isCliEnabled = True
Vince Lehman5d5a5662015-12-02 12:33:12 -0600107 self.nlsrSecurity = False
Vince Lehman194be242015-10-15 18:01:42 -0500108 self.nPings = 300
109 self.testbed = False
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500110 self.workDir = "/tmp"
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500111 self.resultDir = None
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500112 self.pctTraffic = 1.0
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500113
114def createResultsDir(resultDir, faces, hr):
115 if faces == 0:
116 faces = "all"
117
118 if hr:
119 routingType = "/hr/"
120 else:
121 routingType = "/ls/"
122
123 resultDir = "%s/%s/faces-%s" % (str(resultDir), routingType, str(faces))
124 resultDir = os.path.abspath(resultDir)
125
126 if not os.path.isdir(resultDir):
127 os.makedirs(resultDir)
128 else:
129 print("Results directory (%s) already exists!" % resultDir)
130 sys.exit(1);
131
132 print "Results will be stored at: %s" % resultDir
133 return resultDir
Vince Lehman194be242015-10-15 18:01:42 -0500134
ashuef3490b2015-02-17 11:01:04 -0600135def parse_args():
Vince Lehman194be242015-10-15 18:01:42 -0500136 usage = """Usage: minindn [template_file] [ -t | --testbed ]
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500137 If no template_file is given, ndn_utils/default-topology.conf (given sample file)
138 will be used.
ashuef3490b2015-02-17 11:01:04 -0600139 If --testbed is used, minindn will run the NDN Project Testbed.
ashuef3490b2015-02-17 11:01:04 -0600140 """
141
ashuef3490b2015-02-17 11:01:04 -0600142 parser = optparse.OptionParser(usage)
143
Vince Lehman194be242015-10-15 18:01:42 -0500144 parser.add_option("--ctime", action="store", dest="ctime", type="int", default=60,
145 help="Specify convergence time for the topology (Default: 60 seconds)")
ashuef3490b2015-02-17 11:01:04 -0600146
Vince Lehman3b8bc652015-06-18 15:01:47 -0500147 parser.add_option("--experiment", action="store", dest="experiment",
148 help="Runs the specified experiment")
149
Vince Lehman194be242015-10-15 18:01:42 -0500150 parser.add_option("--faces", action="store", dest="faces", type="int", default=3,
ashuef3490b2015-02-17 11:01:04 -0600151 help="Specify number of faces 0-60")
152
Vince Lehman194be242015-10-15 18:01:42 -0500153 parser.add_option("--hr", action="store_true", dest="hr", default=False,
154 help="--hr is used to turn on hyperbolic routing")
155
156 parser.add_option("--list-experiments", action="callback", callback=printExperimentNames,
157 help="Lists the names of all available experiments")
158
159 parser.add_option("--no-cli", action="store_false", dest="isCliEnabled", default=True,
160 help="Run experiments and exit without showing the command line interface")
161
162 parser.add_option("--nPings", action="store", dest="nPings", type="int", default=300,
163 help="Number of pings to perform between each node in the experiment")
164
Vince Lehman5d5a5662015-12-02 12:33:12 -0600165 parser.add_option("--nlsr-security", action="store_true", dest="nlsrSecurity", default=False,
166 help="Enables NLSR security")
167
Vince Lehman194be242015-10-15 18:01:42 -0500168 parser.add_option("-t", "--testbed", action="store_true", dest="testbed", default=False,
169 help="instantiates NDN Testbed")
ashuef3490b2015-02-17 11:01:04 -0600170
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500171 parser.add_option("--work-dir", action="store", dest="workDir", default="/tmp",
172 help="Specify the working directory; default is /tmp")
173
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500174 parser.add_option("--result-dir", action="store", dest="resultDir", default=None,
175 help="Specify the full path destination folder where experiment results will be moved")
176
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500177 parser.add_option("--pct-traffic", action="store", dest="pctTraffic", type="float", default=1.0,
178 help="Specify the percentage of nodes each node should ping")
179
Vince Lehmane9f116d2015-07-15 10:40:21 -0500180 parser.add_option('--version', '-V', action='callback', callback=printVersion,
181 help='Displays version information')
182
Vince Lehman194be242015-10-15 18:01:42 -0500183 (args, arg) = parser.parse_args()
ashuef3490b2015-02-17 11:01:04 -0600184
Vince Lehman194be242015-10-15 18:01:42 -0500185 options = ProgramOptions()
186 options.ctime = args.ctime
187 options.experimentName = args.experiment
188 options.nFaces = args.faces
189 options.hr = args.hr
190 options.isCliEnabled = args.isCliEnabled
Vince Lehman5d5a5662015-12-02 12:33:12 -0600191 options.nlsrSecurity = args.nlsrSecurity
Vince Lehman194be242015-10-15 18:01:42 -0500192 options.nPings = args.nPings
Vince Lehman5d5a5662015-12-02 12:33:12 -0600193
Vince Lehman194be242015-10-15 18:01:42 -0500194 options.testbed = args.testbed
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500195 options.workDir = args.workDir
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500196 options.resultDir = args.resultDir
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500197 options.pctTraffic = args.pctTraffic
ashuef3490b2015-02-17 11:01:04 -0600198
Vince Lehman194be242015-10-15 18:01:42 -0500199 if options.experimentName is not None and options.experimentName not in ExperimentManager.getExperimentNames():
200 print("No experiment named %s" % options.experimentName)
Vince Lehman3b8bc652015-06-18 15:01:47 -0500201 sys.exit()
202
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500203 if options.experimentName is not None and options.resultDir is None:
204 print "No results folder specified; experiment results will remain in the working directory"
205
ashuef3490b2015-02-17 11:01:04 -0600206 if len(arg) == 0 or len(arg) > 2:
Vince Lehman194be242015-10-15 18:01:42 -0500207 options.templateFile = ''
ashuef3490b2015-02-17 11:01:04 -0600208 else:
Vince Lehman194be242015-10-15 18:01:42 -0500209 options.templateFile = arg[0]
ashuef3490b2015-02-17 11:01:04 -0600210
Vince Lehman194be242015-10-15 18:01:42 -0500211 return options
ashuef3490b2015-02-17 11:01:04 -0600212
213class NdnTopo(Topo):
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500214 def __init__(self, conf_arq, workDir, **opts):
ashuef3490b2015-02-17 11:01:04 -0600215 Topo.__init__(self, **opts)
216
217 global hosts_conf
218 global links_conf
219 hosts_conf = parse_hosts(conf_arq)
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500220 switches_conf = parse_switches(conf_arq)
ashuef3490b2015-02-17 11:01:04 -0600221 links_conf = parse_links(conf_arq)
222
223 self.isTCLink = False
224 self.isLimited = False
225
226 for host in hosts_conf:
227 if host.cpu != None and self.isLimited != True:
228 self.isLimited = True
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500229 self.addHost(host.name, app=host.app, params=host.uri_tuples, cpu=host.cpu,cores=host.cores,cache=host.cache, workdir=workDir)
ashuef3490b2015-02-17 11:01:04 -0600230
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500231 for switch in switches_conf:
232 self.addSwitch(switch.name)
233
ashuef3490b2015-02-17 11:01:04 -0600234 for link in links_conf:
235 if len(link.linkDict) == 0:
236 self.addLink(link.h1, link.h2)
237 else:
238 self.addLink(link.h1, link.h2, **link.linkDict)
239 self.isTCLink = True
240
241 info('Parse of ' + conf_arq + ' done.\n')
242
Vince Lehman194be242015-10-15 18:01:42 -0500243def execute(options):
ashuef3490b2015-02-17 11:01:04 -0600244 "Create a network based on template_file"
245
Vince Lehman194be242015-10-15 18:01:42 -0500246 template_file = options.templateFile
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500247 install_dir='/usr/local/etc/mini-ndn/'
248
ashuef3490b2015-02-17 11:01:04 -0600249 if template_file == '':
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500250 template_file = install_dir + 'default-topology.conf'
251
Vince Lehman194be242015-10-15 18:01:42 -0500252 if options.testbed:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500253 template_file = install_dir + 'minindn.testbed.conf'
ashuef3490b2015-02-17 11:01:04 -0600254
255 if os.path.exists(template_file) == False:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500256 info('No template file given and default template file cannot be found. Exiting...\n')
ashuef3490b2015-02-17 11:01:04 -0600257 quit()
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500258
Yucheng Zhang14aa5962016-04-05 17:10:15 -0500259 # Use nfd.conf as default configuration for NFD, else use the sample
260
Vince Lehman498625b2015-10-21 14:25:24 -0500261 nfdConfFile = "%s/nfd.conf" % install_dir
Yucheng Zhang14aa5962016-04-05 17:10:15 -0500262 if os.path.isfile("/usr/local/etc/ndn/nfd.conf") == True:
263 shutil.copy2("/usr/local/etc/ndn/nfd.conf", nfdConfFile)
264 elif os.path.isfile("/usr/local/etc/ndn/nfd.conf.sample") == True:
265 shutil.copy2("/usr/local/etc/ndn/nfd.conf.sample", nfdConfFile)
266 else:
267 sys.exit("nfd.conf or nfd.conf.sample cannot be found in the expected directory. Exit.")
268
269 call(["sudo", "sed", "-i", 's|default_level [A-Z]*$|default_level $LOG_LEVEL|g', nfdConfFile])
Vince Lehman498625b2015-10-21 14:25:24 -0500270
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500271 if options.resultDir is not None:
272 options.resultDir = createResultsDir(options.resultDir, options.nFaces, options.hr)
273
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500274 topo = NdnTopo(template_file, options.workDir)
ashuef3490b2015-02-17 11:01:04 -0600275
276 t = datetime.datetime.now()
277
278 if topo.isTCLink == True and topo.isLimited == True:
279 net = Mininet(topo,host=CpuLimitedNdnHost,link=TCLink)
280 elif topo.isTCLink == True and topo.isLimited == False:
281 net = Mininet(topo,host=NdnHost,link=TCLink)
282 elif topo.isTCLink == False and topo.isLimited == True:
283 net = Mininet(topo,host=CpuLimitedNdnHost)
284 else:
285 net = Mininet(topo,host=NdnHost)
286
287 t2 = datetime.datetime.now()
288
289 delta = t2 - t
290
291 info('Setup time: ' + str(delta.seconds) + '\n')
292
293 net.start()
294
ashu2ad32e22015-05-29 13:37:40 -0500295 # Giving proper IPs to intf so neighbor nodes can communicate
296 # This is one way of giving connectivity, another way could be
297 # to insert a switch between each pair of neighbors
298 ndnNetBase = "1.0.0.0"
299 interfaces = []
300 for host in net.hosts:
301 for intf in host.intfList():
302 link = intf.link
303 node1, node2 = link.intf1.node, link.intf2.node
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500304
305 if node1 in net.switches or node2 in net.switches:
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500306 continue
307
ashu2ad32e22015-05-29 13:37:40 -0500308 if link.intf1 not in interfaces and link.intf2 not in interfaces:
309 interfaces.append(link.intf1)
310 interfaces.append(link.intf2)
311 node1.setIP(ipStr(ipParse(ndnNetBase) + 1) + '/30', intf=link.intf1)
312 node2.setIP(ipStr(ipParse(ndnNetBase) + 2) + '/30', intf=link.intf2)
313 ndnNetBase = ipStr(ipParse(ndnNetBase) + 4)
314
ashuef3490b2015-02-17 11:01:04 -0600315 nodes = "" # Used later to check prefix name in checkFIB
316
Vince Lehman5d5a5662015-12-02 12:33:12 -0600317 # NLSR Security
318 if options.nlsrSecurity is True:
319 Nlsr.createKeysAndCertificates(net, options.workDir)
320
ashuef3490b2015-02-17 11:01:04 -0600321 # NLSR initialization
322 for host in net.hosts:
323 nodes += str(host.name) + ","
324
325 conf = next(x for x in hosts_conf if x.name == host.name)
326 host.nlsrParameters = conf.nlsrParameters
327
Vince Lehman194be242015-10-15 18:01:42 -0500328 if options.nFaces is not None:
329 host.nlsrParameters["max-faces-per-prefix"] = options.nFaces
ashuef3490b2015-02-17 11:01:04 -0600330
Vince Lehman194be242015-10-15 18:01:42 -0500331 if options.hr is True:
ashuef3490b2015-02-17 11:01:04 -0600332 host.nlsrParameters["hyperbolic-state"] = "on"
333
334 # Generate NLSR configuration file
Vince Lehman5d5a5662015-12-02 12:33:12 -0600335 configGenerator = NlsrConfigGenerator(host, options.nlsrSecurity)
ashuef3490b2015-02-17 11:01:04 -0600336 configGenerator.createConfigFile()
337
338 # Start NLSR
ashu34c3ee02015-03-25 14:41:14 -0500339 host.nlsr = Nlsr(host)
340 host.nlsr.start()
ashuef3490b2015-02-17 11:01:04 -0600341
342 nodes = nodes[0:-1]
343
ashuef3490b2015-02-17 11:01:04 -0600344 for host in net.hosts:
345 if 'app' in host.params:
Ashlesh Gawande557cb842015-07-01 15:39:44 -0500346 if host.params['app'] != '':
347 app = host.params['app']
348 print "Starting " + app + " on node " + host.name
349 print(host.cmd(app))
ashuef3490b2015-02-17 11:01:04 -0600350
Vince Lehman3b8bc652015-06-18 15:01:47 -0500351 # Load experiment
Vince Lehman194be242015-10-15 18:01:42 -0500352 experimentName = options.experimentName
353
Vince Lehman3b8bc652015-06-18 15:01:47 -0500354 if experimentName is not None:
355 print "Loading experiment: %s" % experimentName
356
357 experimentArgs = {
358 "net": net,
359 "nodes": nodes,
Vince Lehman194be242015-10-15 18:01:42 -0500360 "ctime": options.ctime,
361 "nPings": options.nPings,
Ashlesh Gawandeb07ada82016-08-05 14:29:17 -0500362 "strategy": Nfd.STRATEGY_BEST_ROUTE,
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500363 "pctTraffic": options.pctTraffic
Vince Lehman3b8bc652015-06-18 15:01:47 -0500364 }
365
366 experiment = ExperimentManager.create(experimentName, experimentArgs)
367
368 if experiment is not None:
369 experiment.start()
370 else:
371 print "ERROR: Experiment '%s' does not exist" % experimentName
372 return
373
Vince Lehman194be242015-10-15 18:01:42 -0500374 if options.isCliEnabled is True:
ashuef3490b2015-02-17 11:01:04 -0600375 CLI(net)
376
377 net.stop()
378
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500379 if options.resultDir is not None:
380 print("Moving results to %s" % options.resultDir)
381 os.system("sudo mv /%s/* %s" % (options.workDir, options.resultDir))
382
ashuef3490b2015-02-17 11:01:04 -0600383if __name__ == '__main__':
Vince Lehman3b8bc652015-06-18 15:01:47 -0500384
ashuef3490b2015-02-17 11:01:04 -0600385 hosts_conf = []
386 links_conf = []
Vince Lehman194be242015-10-15 18:01:42 -0500387
388 options = parse_args()
ashuef3490b2015-02-17 11:01:04 -0600389
390 setLogLevel('info')
Vince Lehman194be242015-10-15 18:01:42 -0500391 execute(options)