blob: 324513b73e466de88b1e1492a765716b77673a5a [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
Ashlesh Gawande3807c1b2016-08-05 16:27:02 -050079import signal
Yucheng Zhang14aa5962016-04-05 17:10:15 -050080from subprocess import call
ashuef3490b2015-02-17 11:01:04 -060081
82from ndn.nlsr import Nlsr, NlsrConfigGenerator
ashu34c3ee02015-03-25 14:41:14 -050083from ndn.nfd import Nfd
ashuef3490b2015-02-17 11:01:04 -060084
Vince Lehman68b08152015-10-23 10:15:36 -050085VERSION_NUMBER = "0.1.1"
Vince Lehmane9f116d2015-07-15 10:40:21 -050086
Vince Lehman194be242015-10-15 18:01:42 -050087def printExperimentNames(option, opt, value, parser):
Vince Lehman3b8bc652015-06-18 15:01:47 -050088 experimentNames = ExperimentManager.getExperimentNames()
89
90 print "Mini-NDN experiments:"
91 for experiment in experimentNames:
92 print " %s" % experiment
93
Vince Lehman194be242015-10-15 18:01:42 -050094 sys.exit()
95
Vince Lehmane9f116d2015-07-15 10:40:21 -050096def printVersion(option, opt, value, parser):
97 print "Mini-NDN v%s" % VERSION_NUMBER
98 sys.exit()
99
Vince Lehman194be242015-10-15 18:01:42 -0500100class ProgramOptions:
101 def __init__(self):
102 self.ctime = 60
103 self.experimentName = None
104 self.nFaces = 3
105 self.templateFile = "minindn.conf"
106 self.hr = False
107 self.isCliEnabled = True
Vince Lehman5d5a5662015-12-02 12:33:12 -0600108 self.nlsrSecurity = False
Vince Lehman194be242015-10-15 18:01:42 -0500109 self.nPings = 300
110 self.testbed = False
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500111 self.workDir = "/tmp"
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500112 self.resultDir = None
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500113 self.pctTraffic = 1.0
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500114
115def createResultsDir(resultDir, faces, hr):
116 if faces == 0:
117 faces = "all"
118
119 if hr:
120 routingType = "/hr/"
121 else:
122 routingType = "/ls/"
123
124 resultDir = "%s/%s/faces-%s" % (str(resultDir), routingType, str(faces))
125 resultDir = os.path.abspath(resultDir)
126
127 if not os.path.isdir(resultDir):
128 os.makedirs(resultDir)
129 else:
130 print("Results directory (%s) already exists!" % resultDir)
131 sys.exit(1);
132
133 print "Results will be stored at: %s" % resultDir
134 return resultDir
Vince Lehman194be242015-10-15 18:01:42 -0500135
ashuef3490b2015-02-17 11:01:04 -0600136def parse_args():
Vince Lehman194be242015-10-15 18:01:42 -0500137 usage = """Usage: minindn [template_file] [ -t | --testbed ]
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500138 If no template_file is given, ndn_utils/default-topology.conf (given sample file)
139 will be used.
ashuef3490b2015-02-17 11:01:04 -0600140 If --testbed is used, minindn will run the NDN Project Testbed.
ashuef3490b2015-02-17 11:01:04 -0600141 """
142
ashuef3490b2015-02-17 11:01:04 -0600143 parser = optparse.OptionParser(usage)
144
Vince Lehman194be242015-10-15 18:01:42 -0500145 parser.add_option("--ctime", action="store", dest="ctime", type="int", default=60,
146 help="Specify convergence time for the topology (Default: 60 seconds)")
ashuef3490b2015-02-17 11:01:04 -0600147
Vince Lehman3b8bc652015-06-18 15:01:47 -0500148 parser.add_option("--experiment", action="store", dest="experiment",
149 help="Runs the specified experiment")
150
Vince Lehman194be242015-10-15 18:01:42 -0500151 parser.add_option("--faces", action="store", dest="faces", type="int", default=3,
ashuef3490b2015-02-17 11:01:04 -0600152 help="Specify number of faces 0-60")
153
Vince Lehman194be242015-10-15 18:01:42 -0500154 parser.add_option("--hr", action="store_true", dest="hr", default=False,
155 help="--hr is used to turn on hyperbolic routing")
156
157 parser.add_option("--list-experiments", action="callback", callback=printExperimentNames,
158 help="Lists the names of all available experiments")
159
160 parser.add_option("--no-cli", action="store_false", dest="isCliEnabled", default=True,
161 help="Run experiments and exit without showing the command line interface")
162
163 parser.add_option("--nPings", action="store", dest="nPings", type="int", default=300,
164 help="Number of pings to perform between each node in the experiment")
165
Vince Lehman5d5a5662015-12-02 12:33:12 -0600166 parser.add_option("--nlsr-security", action="store_true", dest="nlsrSecurity", default=False,
167 help="Enables NLSR security")
168
Vince Lehman194be242015-10-15 18:01:42 -0500169 parser.add_option("-t", "--testbed", action="store_true", dest="testbed", default=False,
170 help="instantiates NDN Testbed")
ashuef3490b2015-02-17 11:01:04 -0600171
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500172 parser.add_option("--work-dir", action="store", dest="workDir", default="/tmp",
173 help="Specify the working directory; default is /tmp")
174
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500175 parser.add_option("--result-dir", action="store", dest="resultDir", default=None,
176 help="Specify the full path destination folder where experiment results will be moved")
177
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500178 parser.add_option("--pct-traffic", action="store", dest="pctTraffic", type="float", default=1.0,
179 help="Specify the percentage of nodes each node should ping")
180
Vince Lehmane9f116d2015-07-15 10:40:21 -0500181 parser.add_option('--version', '-V', action='callback', callback=printVersion,
182 help='Displays version information')
183
Vince Lehman194be242015-10-15 18:01:42 -0500184 (args, arg) = parser.parse_args()
ashuef3490b2015-02-17 11:01:04 -0600185
Vince Lehman194be242015-10-15 18:01:42 -0500186 options = ProgramOptions()
187 options.ctime = args.ctime
188 options.experimentName = args.experiment
189 options.nFaces = args.faces
190 options.hr = args.hr
191 options.isCliEnabled = args.isCliEnabled
Vince Lehman5d5a5662015-12-02 12:33:12 -0600192 options.nlsrSecurity = args.nlsrSecurity
Vince Lehman194be242015-10-15 18:01:42 -0500193 options.nPings = args.nPings
Vince Lehman5d5a5662015-12-02 12:33:12 -0600194
Vince Lehman194be242015-10-15 18:01:42 -0500195 options.testbed = args.testbed
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500196 options.workDir = args.workDir
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500197 options.resultDir = args.resultDir
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500198 options.pctTraffic = args.pctTraffic
ashuef3490b2015-02-17 11:01:04 -0600199
Vince Lehman194be242015-10-15 18:01:42 -0500200 if options.experimentName is not None and options.experimentName not in ExperimentManager.getExperimentNames():
201 print("No experiment named %s" % options.experimentName)
Vince Lehman3b8bc652015-06-18 15:01:47 -0500202 sys.exit()
203
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500204 if options.experimentName is not None and options.resultDir is None:
205 print "No results folder specified; experiment results will remain in the working directory"
206
ashuef3490b2015-02-17 11:01:04 -0600207 if len(arg) == 0 or len(arg) > 2:
Vince Lehman194be242015-10-15 18:01:42 -0500208 options.templateFile = ''
ashuef3490b2015-02-17 11:01:04 -0600209 else:
Vince Lehman194be242015-10-15 18:01:42 -0500210 options.templateFile = arg[0]
ashuef3490b2015-02-17 11:01:04 -0600211
Vince Lehman194be242015-10-15 18:01:42 -0500212 return options
ashuef3490b2015-02-17 11:01:04 -0600213
214class NdnTopo(Topo):
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500215 def __init__(self, conf_arq, workDir, **opts):
ashuef3490b2015-02-17 11:01:04 -0600216 Topo.__init__(self, **opts)
217
218 global hosts_conf
219 global links_conf
220 hosts_conf = parse_hosts(conf_arq)
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500221 switches_conf = parse_switches(conf_arq)
ashuef3490b2015-02-17 11:01:04 -0600222 links_conf = parse_links(conf_arq)
223
224 self.isTCLink = False
225 self.isLimited = False
226
227 for host in hosts_conf:
228 if host.cpu != None and self.isLimited != True:
229 self.isLimited = True
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500230 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 -0600231
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500232 for switch in switches_conf:
233 self.addSwitch(switch.name)
234
ashuef3490b2015-02-17 11:01:04 -0600235 for link in links_conf:
236 if len(link.linkDict) == 0:
237 self.addLink(link.h1, link.h2)
238 else:
239 self.addLink(link.h1, link.h2, **link.linkDict)
240 self.isTCLink = True
241
242 info('Parse of ' + conf_arq + ' done.\n')
243
Vince Lehman194be242015-10-15 18:01:42 -0500244def execute(options):
ashuef3490b2015-02-17 11:01:04 -0600245 "Create a network based on template_file"
246
Vince Lehman194be242015-10-15 18:01:42 -0500247 template_file = options.templateFile
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500248 install_dir='/usr/local/etc/mini-ndn/'
249
ashuef3490b2015-02-17 11:01:04 -0600250 if template_file == '':
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500251 template_file = install_dir + 'default-topology.conf'
252
Vince Lehman194be242015-10-15 18:01:42 -0500253 if options.testbed:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500254 template_file = install_dir + 'minindn.testbed.conf'
ashuef3490b2015-02-17 11:01:04 -0600255
256 if os.path.exists(template_file) == False:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500257 info('No template file given and default template file cannot be found. Exiting...\n')
ashuef3490b2015-02-17 11:01:04 -0600258 quit()
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500259
Yucheng Zhang14aa5962016-04-05 17:10:15 -0500260 # Use nfd.conf as default configuration for NFD, else use the sample
261
Vince Lehman498625b2015-10-21 14:25:24 -0500262 nfdConfFile = "%s/nfd.conf" % install_dir
Yucheng Zhang14aa5962016-04-05 17:10:15 -0500263 if os.path.isfile("/usr/local/etc/ndn/nfd.conf") == True:
264 shutil.copy2("/usr/local/etc/ndn/nfd.conf", nfdConfFile)
265 elif os.path.isfile("/usr/local/etc/ndn/nfd.conf.sample") == True:
266 shutil.copy2("/usr/local/etc/ndn/nfd.conf.sample", nfdConfFile)
267 else:
268 sys.exit("nfd.conf or nfd.conf.sample cannot be found in the expected directory. Exit.")
269
270 call(["sudo", "sed", "-i", 's|default_level [A-Z]*$|default_level $LOG_LEVEL|g', nfdConfFile])
Vince Lehman498625b2015-10-21 14:25:24 -0500271
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500272 if options.resultDir is not None:
273 options.resultDir = createResultsDir(options.resultDir, options.nFaces, options.hr)
274
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500275 topo = NdnTopo(template_file, options.workDir)
ashuef3490b2015-02-17 11:01:04 -0600276
277 t = datetime.datetime.now()
278
279 if topo.isTCLink == True and topo.isLimited == True:
280 net = Mininet(topo,host=CpuLimitedNdnHost,link=TCLink)
281 elif topo.isTCLink == True and topo.isLimited == False:
282 net = Mininet(topo,host=NdnHost,link=TCLink)
283 elif topo.isTCLink == False and topo.isLimited == True:
284 net = Mininet(topo,host=CpuLimitedNdnHost)
285 else:
286 net = Mininet(topo,host=NdnHost)
287
288 t2 = datetime.datetime.now()
289
290 delta = t2 - t
291
292 info('Setup time: ' + str(delta.seconds) + '\n')
293
294 net.start()
295
ashu2ad32e22015-05-29 13:37:40 -0500296 # Giving proper IPs to intf so neighbor nodes can communicate
297 # This is one way of giving connectivity, another way could be
298 # to insert a switch between each pair of neighbors
299 ndnNetBase = "1.0.0.0"
300 interfaces = []
301 for host in net.hosts:
302 for intf in host.intfList():
303 link = intf.link
304 node1, node2 = link.intf1.node, link.intf2.node
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500305
306 if node1 in net.switches or node2 in net.switches:
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500307 continue
308
ashu2ad32e22015-05-29 13:37:40 -0500309 if link.intf1 not in interfaces and link.intf2 not in interfaces:
310 interfaces.append(link.intf1)
311 interfaces.append(link.intf2)
312 node1.setIP(ipStr(ipParse(ndnNetBase) + 1) + '/30', intf=link.intf1)
313 node2.setIP(ipStr(ipParse(ndnNetBase) + 2) + '/30', intf=link.intf2)
314 ndnNetBase = ipStr(ipParse(ndnNetBase) + 4)
315
ashuef3490b2015-02-17 11:01:04 -0600316 nodes = "" # Used later to check prefix name in checkFIB
317
Vince Lehman5d5a5662015-12-02 12:33:12 -0600318 # NLSR Security
319 if options.nlsrSecurity is True:
320 Nlsr.createKeysAndCertificates(net, options.workDir)
321
ashuef3490b2015-02-17 11:01:04 -0600322 # NLSR initialization
323 for host in net.hosts:
324 nodes += str(host.name) + ","
325
326 conf = next(x for x in hosts_conf if x.name == host.name)
327 host.nlsrParameters = conf.nlsrParameters
328
Vince Lehman194be242015-10-15 18:01:42 -0500329 if options.nFaces is not None:
330 host.nlsrParameters["max-faces-per-prefix"] = options.nFaces
ashuef3490b2015-02-17 11:01:04 -0600331
Vince Lehman194be242015-10-15 18:01:42 -0500332 if options.hr is True:
ashuef3490b2015-02-17 11:01:04 -0600333 host.nlsrParameters["hyperbolic-state"] = "on"
334
335 # Generate NLSR configuration file
Vince Lehman5d5a5662015-12-02 12:33:12 -0600336 configGenerator = NlsrConfigGenerator(host, options.nlsrSecurity)
ashuef3490b2015-02-17 11:01:04 -0600337 configGenerator.createConfigFile()
338
339 # Start NLSR
ashu34c3ee02015-03-25 14:41:14 -0500340 host.nlsr = Nlsr(host)
341 host.nlsr.start()
ashuef3490b2015-02-17 11:01:04 -0600342
343 nodes = nodes[0:-1]
344
ashuef3490b2015-02-17 11:01:04 -0600345 for host in net.hosts:
346 if 'app' in host.params:
Ashlesh Gawande557cb842015-07-01 15:39:44 -0500347 if host.params['app'] != '':
348 app = host.params['app']
349 print "Starting " + app + " on node " + host.name
350 print(host.cmd(app))
ashuef3490b2015-02-17 11:01:04 -0600351
Vince Lehman3b8bc652015-06-18 15:01:47 -0500352 # Load experiment
Vince Lehman194be242015-10-15 18:01:42 -0500353 experimentName = options.experimentName
354
Vince Lehman3b8bc652015-06-18 15:01:47 -0500355 if experimentName is not None:
356 print "Loading experiment: %s" % experimentName
357
358 experimentArgs = {
359 "net": net,
360 "nodes": nodes,
Vince Lehman194be242015-10-15 18:01:42 -0500361 "ctime": options.ctime,
362 "nPings": options.nPings,
Ashlesh Gawandeb07ada82016-08-05 14:29:17 -0500363 "strategy": Nfd.STRATEGY_BEST_ROUTE,
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500364 "pctTraffic": options.pctTraffic
Vince Lehman3b8bc652015-06-18 15:01:47 -0500365 }
366
367 experiment = ExperimentManager.create(experimentName, experimentArgs)
368
369 if experiment is not None:
370 experiment.start()
371 else:
372 print "ERROR: Experiment '%s' does not exist" % experimentName
373 return
374
Vince Lehman194be242015-10-15 18:01:42 -0500375 if options.isCliEnabled is True:
ashuef3490b2015-02-17 11:01:04 -0600376 CLI(net)
377
378 net.stop()
379
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500380 if options.resultDir is not None:
381 print("Moving results to %s" % options.resultDir)
382 os.system("sudo mv /%s/* %s" % (options.workDir, options.resultDir))
383
Ashlesh Gawande3807c1b2016-08-05 16:27:02 -0500384def signal_handler(signal, frame):
385 print('Cleaning up...')
386 call(["nfd-stop"])
387 call(["sudo", "mn", "--clean"])
388 sys.exit(1)
389
ashuef3490b2015-02-17 11:01:04 -0600390if __name__ == '__main__':
Vince Lehman3b8bc652015-06-18 15:01:47 -0500391
ashuef3490b2015-02-17 11:01:04 -0600392 hosts_conf = []
393 links_conf = []
Vince Lehman194be242015-10-15 18:01:42 -0500394
Ashlesh Gawande3807c1b2016-08-05 16:27:02 -0500395 signal.signal(signal.SIGINT, signal_handler)
396
Vince Lehman194be242015-10-15 18:01:42 -0500397 options = parse_args()
ashuef3490b2015-02-17 11:01:04 -0600398
399 setLogLevel('info')
Vince Lehman194be242015-10-15 18:01:42 -0500400 execute(options)