blob: af4be4d011ab775548cffbfd95fb8ac0f0e5171e [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 Lehman3b8bc652015-06-18 15:01:47 -050082def printExperimentNames():
83 experimentNames = ExperimentManager.getExperimentNames()
84
85 print "Mini-NDN experiments:"
86 for experiment in experimentNames:
87 print " %s" % experiment
88
ashuef3490b2015-02-17 11:01:04 -060089def parse_args():
90 usage="""Usage: minindn [template_file] [ -t | --testbed ]
Ashlesh Gawande20f70762015-06-17 15:18:19 -050091 If no template_file is given, ndn_utils/default-topology.conf (given sample file)
92 will be used.
ashuef3490b2015-02-17 11:01:04 -060093 If --testbed is used, minindn will run the NDN Project Testbed.
ashuef3490b2015-02-17 11:01:04 -060094 """
95
96 testbed = False
ashuef3490b2015-02-17 11:01:04 -060097 hr = False
ashuef3490b2015-02-17 11:01:04 -060098
99 parser = optparse.OptionParser(usage)
100
101 parser.add_option("-t", "--testbed", action="store_true", dest="testbed",
102 help="instantiates NDN Testbed")
103
Vince Lehman3b8bc652015-06-18 15:01:47 -0500104 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")
ashuef3490b2015-02-17 11:01:04 -0600112
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
ashuef3490b2015-02-17 11:01:04 -0600122 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 Lehman3b8bc652015-06-18 15:01:47 -0500128 experimentName = options.experiment
129 shouldListExperiments = options.shouldListExperiments
130 nPings = options.nPings
ashuef3490b2015-02-17 11:01:04 -0600131 ctime = options.ctime
132 hr = options.hr
133 faces = options.faces
ashuef3490b2015-02-17 11:01:04 -0600134 isCliEnabled = options.isCliEnabled
135
Vince Lehman3b8bc652015-06-18 15:01:47 -0500136 if shouldListExperiments is not None:
137 printExperimentNames()
138 sys.exit()
139
ashuef3490b2015-02-17 11:01:04 -0600140 if ctime is None:
141 ctime = 60
142
Vince Lehman3b8bc652015-06-18 15:01:47 -0500143 if nPings is None:
144 nPings = 300
145
ashuef3490b2015-02-17 11:01:04 -0600146 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 Lehman3b8bc652015-06-18 15:01:47 -0500154 return file, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled
ashuef3490b2015-02-17 11:01:04 -0600155
156class 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 Gawande3a4afb12015-07-09 09:23:30 -0500171 self.addHost(host.name, app=host.app, params=host.uri_tuples, cpu=host.cpu,cores=host.cores,cache=host.cache)
ashuef3490b2015-02-17 11:01:04 -0600172
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 Lehman3b8bc652015-06-18 15:01:47 -0500182def execute(template_file='minindn.conf', testbed=False, experimentName=None, nPings=None, ctime=None, hr=False, faces=3, isCliEnabled=True):
ashuef3490b2015-02-17 11:01:04 -0600183 "Create a network based on template_file"
184
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500185 install_dir='/usr/local/etc/mini-ndn/'
186
ashuef3490b2015-02-17 11:01:04 -0600187 if template_file == '':
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500188 template_file = install_dir + 'default-topology.conf'
189
190 if testbed:
191 template_file = install_dir + 'minindn.testbed.conf'
ashuef3490b2015-02-17 11:01:04 -0600192
193 if os.path.exists(template_file) == False:
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500194 info('No template file given and default template file cannot be found. Exiting...\n')
ashuef3490b2015-02-17 11:01:04 -0600195 quit()
Ashlesh Gawande20f70762015-06-17 15:18:19 -0500196
ashuef3490b2015-02-17 11:01:04 -0600197 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
ashu2ad32e22015-05-29 13:37:40 -0500218 # 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
ashuef3490b2015-02-17 11:01:04 -0600234 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
ashu2ad32e22015-05-29 13:37:40 -0500250 configGenerator = NlsrConfigGenerator(host)
ashuef3490b2015-02-17 11:01:04 -0600251 configGenerator.createConfigFile()
252
253 # Start NLSR
ashu34c3ee02015-03-25 14:41:14 -0500254 host.nlsr = Nlsr(host)
255 host.nlsr.start()
ashuef3490b2015-02-17 11:01:04 -0600256
257 nodes = nodes[0:-1]
258
ashuef3490b2015-02-17 11:01:04 -0600259 for host in net.hosts:
260 if 'app' in host.params:
Ashlesh Gawande557cb842015-07-01 15:39:44 -0500261 if host.params['app'] != '':
262 app = host.params['app']
263 print "Starting " + app + " on node " + host.name
264 print(host.cmd(app))
ashuef3490b2015-02-17 11:01:04 -0600265
Vince Lehman3b8bc652015-06-18 15:01:47 -0500266 # 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
ashuef3490b2015-02-17 11:01:04 -0600286 if isCliEnabled is True:
287 CLI(net)
288
289 net.stop()
290
291if __name__ == '__main__':
Vince Lehman3b8bc652015-06-18 15:01:47 -0500292
ashuef3490b2015-02-17 11:01:04 -0600293 hosts_conf = []
294 links_conf = []
Vince Lehman3b8bc652015-06-18 15:01:47 -0500295 template, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled = parse_args()
ashuef3490b2015-02-17 11:01:04 -0600296
297 setLogLevel('info')
Vince Lehman3b8bc652015-06-18 15:01:47 -0500298 execute(template, testbed, experimentName, nPings, ctime, hr, faces, isCliEnabled)