blob: c9bb36fe6b9f92705ab3899154ac49dc7816fc6b [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- 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.
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
74import optparse
75import datetime
ashu01b62f72015-03-12 15:16:11 -050076from os.path import expanduser
Vince Lehman3b8bc652015-06-18 15:01:47 -050077import sys
ashuef3490b2015-02-17 11:01:04 -060078
79from ndn.nlsr import Nlsr, NlsrConfigGenerator
ashu34c3ee02015-03-25 14:41:14 -050080from ndn.nfd import Nfd
ashuef3490b2015-02-17 11:01:04 -060081
Vince Lehmane9f116d2015-07-15 10:40:21 -050082VERSION_NUMBER = "0.1.0"
83
Vince Lehman194be242015-10-15 18:01:42 -050084def printExperimentNames(option, opt, value, parser):
Vince Lehman3b8bc652015-06-18 15:01:47 -050085 experimentNames = ExperimentManager.getExperimentNames()
86
87 print "Mini-NDN experiments:"
88 for experiment in experimentNames:
89 print " %s" % experiment
90
Vince Lehman194be242015-10-15 18:01:42 -050091 sys.exit()
92
Vince Lehmane9f116d2015-07-15 10:40:21 -050093def printVersion(option, opt, value, parser):
94 print "Mini-NDN v%s" % VERSION_NUMBER
95 sys.exit()
96
Vince Lehman194be242015-10-15 18:01:42 -050097class 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 Gawande1b663692015-10-14 16:38:10 -0500107 self.workDir = "/tmp"
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500108 self.resultDir = None
109
110def createResultsDir(resultDir, faces, hr):
111 if faces == 0:
112 faces = "all"
113
114 if hr:
115 routingType = "/hr/"
116 else:
117 routingType = "/ls/"
118
119 resultDir = "%s/%s/faces-%s" % (str(resultDir), routingType, str(faces))
120 resultDir = os.path.abspath(resultDir)
121
122 if not os.path.isdir(resultDir):
123 os.makedirs(resultDir)
124 else:
125 print("Results directory (%s) already exists!" % resultDir)
126 sys.exit(1);
127
128 print "Results will be stored at: %s" % resultDir
129 return resultDir
Vince Lehman194be242015-10-15 18:01:42 -0500130
ashuef3490b2015-02-17 11:01:04 -0600131def parse_args():
Vince Lehman194be242015-10-15 18:01:42 -0500132 usage = """Usage: minindn [template_file] [ -t | --testbed ]
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500133 If no template_file is given, ndn_utils/default-topology.conf (given sample file)
134 will be used.
ashuef3490b2015-02-17 11:01:04 -0600135 If --testbed is used, minindn will run the NDN Project Testbed.
ashuef3490b2015-02-17 11:01:04 -0600136 """
137
ashuef3490b2015-02-17 11:01:04 -0600138 parser = optparse.OptionParser(usage)
139
Vince Lehman194be242015-10-15 18:01:42 -0500140 parser.add_option("--ctime", action="store", dest="ctime", type="int", default=60,
141 help="Specify convergence time for the topology (Default: 60 seconds)")
ashuef3490b2015-02-17 11:01:04 -0600142
Vince Lehman3b8bc652015-06-18 15:01:47 -0500143 parser.add_option("--experiment", action="store", dest="experiment",
144 help="Runs the specified experiment")
145
Vince Lehman194be242015-10-15 18:01:42 -0500146 parser.add_option("--faces", action="store", dest="faces", type="int", default=3,
ashuef3490b2015-02-17 11:01:04 -0600147 help="Specify number of faces 0-60")
148
Vince Lehman194be242015-10-15 18:01:42 -0500149 parser.add_option("--hr", action="store_true", dest="hr", default=False,
150 help="--hr is used to turn on hyperbolic routing")
151
152 parser.add_option("--list-experiments", action="callback", callback=printExperimentNames,
153 help="Lists the names of all available experiments")
154
155 parser.add_option("--no-cli", action="store_false", dest="isCliEnabled", default=True,
156 help="Run experiments and exit without showing the command line interface")
157
158 parser.add_option("--nPings", action="store", dest="nPings", type="int", default=300,
159 help="Number of pings to perform between each node in the experiment")
160
161 parser.add_option("-t", "--testbed", action="store_true", dest="testbed", default=False,
162 help="instantiates NDN Testbed")
ashuef3490b2015-02-17 11:01:04 -0600163
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500164 parser.add_option("--work-dir", action="store", dest="workDir", default="/tmp",
165 help="Specify the working directory; default is /tmp")
166
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500167 parser.add_option("--result-dir", action="store", dest="resultDir", default=None,
168 help="Specify the full path destination folder where experiment results will be moved")
169
Vince Lehmane9f116d2015-07-15 10:40:21 -0500170 parser.add_option('--version', '-V', action='callback', callback=printVersion,
171 help='Displays version information')
172
Vince Lehman194be242015-10-15 18:01:42 -0500173 (args, arg) = parser.parse_args()
ashuef3490b2015-02-17 11:01:04 -0600174
Vince Lehman194be242015-10-15 18:01:42 -0500175 options = ProgramOptions()
176 options.ctime = args.ctime
177 options.experimentName = args.experiment
178 options.nFaces = args.faces
179 options.hr = args.hr
180 options.isCliEnabled = args.isCliEnabled
181 options.nPings = args.nPings
182 options.testbed = args.testbed
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500183 options.workDir = args.workDir
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500184 options.resultDir = args.resultDir
ashuef3490b2015-02-17 11:01:04 -0600185
Vince Lehman194be242015-10-15 18:01:42 -0500186 if options.experimentName is not None and options.experimentName not in ExperimentManager.getExperimentNames():
187 print("No experiment named %s" % options.experimentName)
Vince Lehman3b8bc652015-06-18 15:01:47 -0500188 sys.exit()
189
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500190 if options.experimentName is not None and options.resultDir is None:
191 print "No results folder specified; experiment results will remain in the working directory"
192
ashuef3490b2015-02-17 11:01:04 -0600193 if len(arg) == 0 or len(arg) > 2:
Vince Lehman194be242015-10-15 18:01:42 -0500194 options.templateFile = ''
ashuef3490b2015-02-17 11:01:04 -0600195 else:
Vince Lehman194be242015-10-15 18:01:42 -0500196 options.templateFile = arg[0]
ashuef3490b2015-02-17 11:01:04 -0600197
Vince Lehman194be242015-10-15 18:01:42 -0500198 return options
ashuef3490b2015-02-17 11:01:04 -0600199
200class NdnTopo(Topo):
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500201 def __init__(self, conf_arq, workDir, **opts):
ashuef3490b2015-02-17 11:01:04 -0600202 Topo.__init__(self, **opts)
203
204 global hosts_conf
205 global links_conf
206 hosts_conf = parse_hosts(conf_arq)
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500207 switches_conf = parse_switches(conf_arq)
ashuef3490b2015-02-17 11:01:04 -0600208 links_conf = parse_links(conf_arq)
209
210 self.isTCLink = False
211 self.isLimited = False
212
213 for host in hosts_conf:
214 if host.cpu != None and self.isLimited != True:
215 self.isLimited = True
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500216 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 -0600217
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500218 for switch in switches_conf:
219 self.addSwitch(switch.name)
220
ashuef3490b2015-02-17 11:01:04 -0600221 for link in links_conf:
222 if len(link.linkDict) == 0:
223 self.addLink(link.h1, link.h2)
224 else:
225 self.addLink(link.h1, link.h2, **link.linkDict)
226 self.isTCLink = True
227
228 info('Parse of ' + conf_arq + ' done.\n')
229
Vince Lehman194be242015-10-15 18:01:42 -0500230def execute(options):
ashuef3490b2015-02-17 11:01:04 -0600231 "Create a network based on template_file"
232
Vince Lehman194be242015-10-15 18:01:42 -0500233 template_file = options.templateFile
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500234 install_dir='/usr/local/etc/mini-ndn/'
235
ashuef3490b2015-02-17 11:01:04 -0600236 if template_file == '':
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500237 template_file = install_dir + 'default-topology.conf'
238
Vince Lehman194be242015-10-15 18:01:42 -0500239 if options.testbed:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500240 template_file = install_dir + 'minindn.testbed.conf'
ashuef3490b2015-02-17 11:01:04 -0600241
242 if os.path.exists(template_file) == False:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500243 info('No template file given and default template file cannot be found. Exiting...\n')
ashuef3490b2015-02-17 11:01:04 -0600244 quit()
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500245
Vince Lehman498625b2015-10-21 14:25:24 -0500246 # Update nfd.conf file used by Mini-NDN to match the currently installed version of NFD
247 nfdConfFile = "%s/nfd.conf" % install_dir
248 os.system("sudo cp /usr/local/etc/ndn/nfd.conf.sample %s" % nfdConfFile)
249 os.system("sudo sed -i \'s|default_level [A-Z]*$|default_level $LOG_LEVEL|g\' %s" % nfdConfFile)
250
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500251 if options.resultDir is not None:
252 options.resultDir = createResultsDir(options.resultDir, options.nFaces, options.hr)
253
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500254 topo = NdnTopo(template_file, options.workDir)
ashuef3490b2015-02-17 11:01:04 -0600255
256 t = datetime.datetime.now()
257
258 if topo.isTCLink == True and topo.isLimited == True:
259 net = Mininet(topo,host=CpuLimitedNdnHost,link=TCLink)
260 elif topo.isTCLink == True and topo.isLimited == False:
261 net = Mininet(topo,host=NdnHost,link=TCLink)
262 elif topo.isTCLink == False and topo.isLimited == True:
263 net = Mininet(topo,host=CpuLimitedNdnHost)
264 else:
265 net = Mininet(topo,host=NdnHost)
266
267 t2 = datetime.datetime.now()
268
269 delta = t2 - t
270
271 info('Setup time: ' + str(delta.seconds) + '\n')
272
273 net.start()
274
ashu2ad32e22015-05-29 13:37:40 -0500275 # Giving proper IPs to intf so neighbor nodes can communicate
276 # This is one way of giving connectivity, another way could be
277 # to insert a switch between each pair of neighbors
278 ndnNetBase = "1.0.0.0"
279 interfaces = []
280 for host in net.hosts:
281 for intf in host.intfList():
282 link = intf.link
283 node1, node2 = link.intf1.node, link.intf2.node
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500284
285 if node1 in net.switches or node2 in net.switches:
286 print "%s or %s is a switch" % (node1.name, node2.name)
287 continue
288
ashu2ad32e22015-05-29 13:37:40 -0500289 if link.intf1 not in interfaces and link.intf2 not in interfaces:
290 interfaces.append(link.intf1)
291 interfaces.append(link.intf2)
292 node1.setIP(ipStr(ipParse(ndnNetBase) + 1) + '/30', intf=link.intf1)
293 node2.setIP(ipStr(ipParse(ndnNetBase) + 2) + '/30', intf=link.intf2)
294 ndnNetBase = ipStr(ipParse(ndnNetBase) + 4)
295
ashuef3490b2015-02-17 11:01:04 -0600296 nodes = "" # Used later to check prefix name in checkFIB
297
298 # NLSR initialization
299 for host in net.hosts:
300 nodes += str(host.name) + ","
301
302 conf = next(x for x in hosts_conf if x.name == host.name)
303 host.nlsrParameters = conf.nlsrParameters
304
Vince Lehman194be242015-10-15 18:01:42 -0500305 if options.nFaces is not None:
306 host.nlsrParameters["max-faces-per-prefix"] = options.nFaces
ashuef3490b2015-02-17 11:01:04 -0600307
Vince Lehman194be242015-10-15 18:01:42 -0500308 if options.hr is True:
ashuef3490b2015-02-17 11:01:04 -0600309 host.nlsrParameters["hyperbolic-state"] = "on"
310
311 # Generate NLSR configuration file
ashu2ad32e22015-05-29 13:37:40 -0500312 configGenerator = NlsrConfigGenerator(host)
ashuef3490b2015-02-17 11:01:04 -0600313 configGenerator.createConfigFile()
314
315 # Start NLSR
ashu34c3ee02015-03-25 14:41:14 -0500316 host.nlsr = Nlsr(host)
317 host.nlsr.start()
ashuef3490b2015-02-17 11:01:04 -0600318
319 nodes = nodes[0:-1]
320
ashuef3490b2015-02-17 11:01:04 -0600321 for host in net.hosts:
322 if 'app' in host.params:
Ashlesh Gawande557cb842015-07-01 15:39:44 -0500323 if host.params['app'] != '':
324 app = host.params['app']
325 print "Starting " + app + " on node " + host.name
326 print(host.cmd(app))
ashuef3490b2015-02-17 11:01:04 -0600327
Vince Lehman3b8bc652015-06-18 15:01:47 -0500328 # Load experiment
Vince Lehman194be242015-10-15 18:01:42 -0500329 experimentName = options.experimentName
330
Vince Lehman3b8bc652015-06-18 15:01:47 -0500331 if experimentName is not None:
332 print "Loading experiment: %s" % experimentName
333
334 experimentArgs = {
335 "net": net,
336 "nodes": nodes,
Vince Lehman194be242015-10-15 18:01:42 -0500337 "ctime": options.ctime,
338 "nPings": options.nPings,
Vince Lehman3b8bc652015-06-18 15:01:47 -0500339 "strategy": Nfd.STRATEGY_BEST_ROUTE_V3
340 }
341
342 experiment = ExperimentManager.create(experimentName, experimentArgs)
343
344 if experiment is not None:
345 experiment.start()
346 else:
347 print "ERROR: Experiment '%s' does not exist" % experimentName
348 return
349
Vince Lehman194be242015-10-15 18:01:42 -0500350 if options.isCliEnabled is True:
ashuef3490b2015-02-17 11:01:04 -0600351 CLI(net)
352
353 net.stop()
354
Ashlesh Gawanded829bfc2015-10-14 16:38:10 -0500355 if options.resultDir is not None:
356 print("Moving results to %s" % options.resultDir)
357 os.system("sudo mv /%s/* %s" % (options.workDir, options.resultDir))
358
ashuef3490b2015-02-17 11:01:04 -0600359if __name__ == '__main__':
Vince Lehman3b8bc652015-06-18 15:01:47 -0500360
ashuef3490b2015-02-17 11:01:04 -0600361 hosts_conf = []
362 links_conf = []
Vince Lehman194be242015-10-15 18:01:42 -0500363
364 options = parse_args()
ashuef3490b2015-02-17 11:01:04 -0600365
366 setLogLevel('info')
Vince Lehman194be242015-10-15 18:01:42 -0500367 execute(options)