blob: bf5a525dc35a98fb97a8fd724fde82c76aa5ca5e [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
ashu2ad32e22015-05-29 13:37:40 -050071from ndn.conf_parser import parse_hosts, 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"
Vince Lehman194be242015-10-15 18:01:42 -0500108
ashuef3490b2015-02-17 11:01:04 -0600109def parse_args():
Vince Lehman194be242015-10-15 18:01:42 -0500110 usage = """Usage: minindn [template_file] [ -t | --testbed ]
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500111 If no template_file is given, ndn_utils/default-topology.conf (given sample file)
112 will be used.
ashuef3490b2015-02-17 11:01:04 -0600113 If --testbed is used, minindn will run the NDN Project Testbed.
ashuef3490b2015-02-17 11:01:04 -0600114 """
115
ashuef3490b2015-02-17 11:01:04 -0600116 parser = optparse.OptionParser(usage)
117
Vince Lehman194be242015-10-15 18:01:42 -0500118 parser.add_option("--ctime", action="store", dest="ctime", type="int", default=60,
119 help="Specify convergence time for the topology (Default: 60 seconds)")
ashuef3490b2015-02-17 11:01:04 -0600120
Vince Lehman3b8bc652015-06-18 15:01:47 -0500121 parser.add_option("--experiment", action="store", dest="experiment",
122 help="Runs the specified experiment")
123
Vince Lehman194be242015-10-15 18:01:42 -0500124 parser.add_option("--faces", action="store", dest="faces", type="int", default=3,
ashuef3490b2015-02-17 11:01:04 -0600125 help="Specify number of faces 0-60")
126
Vince Lehman194be242015-10-15 18:01:42 -0500127 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")
ashuef3490b2015-02-17 11:01:04 -0600141
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500142 parser.add_option("--work-dir", action="store", dest="workDir", default="/tmp",
143 help="Specify the working directory; default is /tmp")
144
Vince Lehmane9f116d2015-07-15 10:40:21 -0500145 parser.add_option('--version', '-V', action='callback', callback=printVersion,
146 help='Displays version information')
147
Vince Lehman194be242015-10-15 18:01:42 -0500148 (args, arg) = parser.parse_args()
ashuef3490b2015-02-17 11:01:04 -0600149
Vince Lehman194be242015-10-15 18:01:42 -0500150 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 Gawande1b663692015-10-14 16:38:10 -0500158 options.workDir = args.workDir
ashuef3490b2015-02-17 11:01:04 -0600159
Vince Lehman194be242015-10-15 18:01:42 -0500160 if options.experimentName is not None and options.experimentName not in ExperimentManager.getExperimentNames():
161 print("No experiment named %s" % options.experimentName)
Vince Lehman3b8bc652015-06-18 15:01:47 -0500162 sys.exit()
163
ashuef3490b2015-02-17 11:01:04 -0600164 if len(arg) == 0 or len(arg) > 2:
Vince Lehman194be242015-10-15 18:01:42 -0500165 options.templateFile = ''
ashuef3490b2015-02-17 11:01:04 -0600166 else:
Vince Lehman194be242015-10-15 18:01:42 -0500167 options.templateFile = arg[0]
ashuef3490b2015-02-17 11:01:04 -0600168
Vince Lehman194be242015-10-15 18:01:42 -0500169 return options
ashuef3490b2015-02-17 11:01:04 -0600170
171class NdnTopo(Topo):
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500172 def __init__(self, conf_arq, workDir, **opts):
ashuef3490b2015-02-17 11:01:04 -0600173 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 Gawande1b663692015-10-14 16:38:10 -0500186 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 -0600187
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 Lehman194be242015-10-15 18:01:42 -0500197def execute(options):
ashuef3490b2015-02-17 11:01:04 -0600198 "Create a network based on template_file"
199
Vince Lehman194be242015-10-15 18:01:42 -0500200 template_file = options.templateFile
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500201 install_dir='/usr/local/etc/mini-ndn/'
202
ashuef3490b2015-02-17 11:01:04 -0600203 if template_file == '':
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500204 template_file = install_dir + 'default-topology.conf'
205
Vince Lehman194be242015-10-15 18:01:42 -0500206 if options.testbed:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500207 template_file = install_dir + 'minindn.testbed.conf'
ashuef3490b2015-02-17 11:01:04 -0600208
209 if os.path.exists(template_file) == False:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500210 info('No template file given and default template file cannot be found. Exiting...\n')
ashuef3490b2015-02-17 11:01:04 -0600211 quit()
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500212
Vince Lehman498625b2015-10-21 14:25:24 -0500213 # 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 Gawande1b663692015-10-14 16:38:10 -0500218 topo = NdnTopo(template_file, options.workDir)
ashuef3490b2015-02-17 11:01:04 -0600219
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
ashu2ad32e22015-05-29 13:37:40 -0500239 # 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
ashuef3490b2015-02-17 11:01:04 -0600255 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 Lehman194be242015-10-15 18:01:42 -0500264 if options.nFaces is not None:
265 host.nlsrParameters["max-faces-per-prefix"] = options.nFaces
ashuef3490b2015-02-17 11:01:04 -0600266
Vince Lehman194be242015-10-15 18:01:42 -0500267 if options.hr is True:
ashuef3490b2015-02-17 11:01:04 -0600268 host.nlsrParameters["hyperbolic-state"] = "on"
269
270 # Generate NLSR configuration file
ashu2ad32e22015-05-29 13:37:40 -0500271 configGenerator = NlsrConfigGenerator(host)
ashuef3490b2015-02-17 11:01:04 -0600272 configGenerator.createConfigFile()
273
274 # Start NLSR
ashu34c3ee02015-03-25 14:41:14 -0500275 host.nlsr = Nlsr(host)
276 host.nlsr.start()
ashuef3490b2015-02-17 11:01:04 -0600277
278 nodes = nodes[0:-1]
279
ashuef3490b2015-02-17 11:01:04 -0600280 for host in net.hosts:
281 if 'app' in host.params:
Ashlesh Gawande557cb842015-07-01 15:39:44 -0500282 if host.params['app'] != '':
283 app = host.params['app']
284 print "Starting " + app + " on node " + host.name
285 print(host.cmd(app))
ashuef3490b2015-02-17 11:01:04 -0600286
Vince Lehman3b8bc652015-06-18 15:01:47 -0500287 # Load experiment
Vince Lehman194be242015-10-15 18:01:42 -0500288 experimentName = options.experimentName
289
Vince Lehman3b8bc652015-06-18 15:01:47 -0500290 if experimentName is not None:
291 print "Loading experiment: %s" % experimentName
292
293 experimentArgs = {
294 "net": net,
295 "nodes": nodes,
Vince Lehman194be242015-10-15 18:01:42 -0500296 "ctime": options.ctime,
297 "nPings": options.nPings,
Vince Lehman3b8bc652015-06-18 15:01:47 -0500298 "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 Lehman194be242015-10-15 18:01:42 -0500309 if options.isCliEnabled is True:
ashuef3490b2015-02-17 11:01:04 -0600310 CLI(net)
311
312 net.stop()
313
314if __name__ == '__main__':
Vince Lehman3b8bc652015-06-18 15:01:47 -0500315
ashuef3490b2015-02-17 11:01:04 -0600316 hosts_conf = []
317 links_conf = []
Vince Lehman194be242015-10-15 18:01:42 -0500318
319 options = parse_args()
ashuef3490b2015-02-17 11:01:04 -0600320
321 setLogLevel('info')
Vince Lehman194be242015-10-15 18:01:42 -0500322 execute(options)