2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.trace import Trace, TraceAttr
22 from nepi.execution.resource import ResourceManager, clsinit_copy, \
23 ResourceState, reschedule_delay
24 from nepi.resources.linux.application import LinuxApplication
25 from nepi.util.timefuncs import tnow, tdiffsec
26 from nepi.resources.ns3.ns3simulation import NS3Simulation
27 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID, GLOBAL_VALUE_UUID, \
28 IPV4_GLOBAL_ROUTING_HELPER_UUID
29 from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
35 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
36 _rtype = "LinuxNS3Simulation"
39 def _register_attributes(cls):
40 impl_type = Attribute("simulatorImplementationType",
41 "The object class to use as the simulator implementation",
42 allowed = ["ns3::DefaultSimulatorImpl", "ns3::RealtimeSimulatorImpl"],
43 default = "ns3::DefaultSimulatorImpl",
44 type = Types.Enumerate,
47 sched_type = Attribute("schedulerType",
48 "The object class to use as the scheduler implementation",
49 allowed = ["ns3::MapScheduler",
53 "ns3::CalendarScheduler"
55 default = "ns3::MapScheduler",
56 type = Types.Enumerate,
59 check_sum = Attribute("checksumEnabled",
60 "A global switch to enable all checksums for all protocols",
65 ns_log = Attribute("nsLog",
66 "NS_LOG environment variable. " \
67 " Will only generate output if ns-3 is compiled in DEBUG mode. ",
70 verbose = Attribute("verbose",
71 "True to output debugging info from the ns3 client-server communication",
75 build_mode = Attribute("buildMode",
76 "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
77 default = "optimized",
78 allowed = ["debug", "release", "optimized"],
79 type = Types.Enumerate,
82 ns3_version = Attribute("ns3Version",
83 "Version of ns-3 to install from nsam repo",
87 enable_dce = Attribute("enableDCE",
88 "Install DCE source code",
93 pybindgen_version = Attribute("pybindgenVersion",
94 "Version of pybindgen to install from bazar repo",
98 populate_routing_tables = Attribute("populateRoutingTables",
99 "Invokes Ipv4GlobalRoutingHelper.PopulateRoutingTables() ",
102 flags = Flags.Design)
104 cls._register_attribute(impl_type)
105 cls._register_attribute(sched_type)
106 cls._register_attribute(check_sum)
107 cls._register_attribute(ns_log)
108 cls._register_attribute(verbose)
109 cls._register_attribute(build_mode)
110 cls._register_attribute(ns3_version)
111 cls._register_attribute(pybindgen_version)
112 cls._register_attribute(populate_routing_tables)
113 cls._register_attribute(enable_dce)
115 def __init__(self, ec, guid):
116 LinuxApplication.__init__(self, ec, guid)
117 NS3Simulation.__init__(self)
120 self._home = "ns3-simu-%s" % self.guid
121 self._socket_name = "ns3-%s.sock" % os.urandom(4).encode('hex')
124 def socket_name(self):
125 return self._socket_name
128 def remote_socket(self):
129 return os.path.join(self.run_home, self.socket_name)
132 def ns3_build_home(self):
133 return os.path.join(self.node.bin_dir, "ns-3", self.get("ns3Version"),
134 self.get("buildMode"), "build")
136 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
138 return LinuxApplication.trace(self, name, attr, block, offset)
140 def upload_sources(self):
141 self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
143 # upload ns3 wrapper python script
144 ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
147 self.node.upload(ns3_wrapper,
148 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
151 # upload ns3_server python script
152 ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
155 self.node.upload(ns3_server,
156 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
159 if self.node.use_rpm:
160 # upload pygccxml sources
161 pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
162 "%s.tar.gz" % self.pygccxml_version)
164 self.node.upload(pygccxml_tar,
165 os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
168 # Upload user defined ns-3 sources
169 self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
170 src_dir = os.path.join(self.node.src_dir, "ns-3")
172 super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
174 def upload_start_command(self):
175 command = self.get("command")
176 env = self.get("env")
178 # We want to make sure the ccnd is running
179 # before the experiment starts.
180 # Run the command as a bash script in background,
181 # in the host ( but wait until the command has
182 # finished to continue )
183 env = self.replace_paths(env)
184 command = self.replace_paths(command)
186 shfile = os.path.join(self.app_home, "start.sh")
187 self.node.upload_command(command,
193 self._run_in_background()
196 if self.has_changed("simulatorImplementationType"):
197 simu_type = self.get("simulatorImplementationType")
198 stype = self.create("StringValue", simu_type)
199 self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
201 if self.has_changed("checksumEnabled"):
202 check_sum = self.get("checksumEnabled")
203 btrue = self.create("BooleanValue", check_sum)
204 self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
206 if self.has_changed("schedulerType"):
207 sched_type = self.get("schedulerType")
208 stype = self.create("StringValue", sched_type)
209 self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
212 if not self.node or self.node.state < ResourceState.READY:
213 self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
215 # ccnd needs to wait until node is deployed and running
216 self.ec.schedule(reschedule_delay, self.deploy)
218 if not self.get("command"):
219 self.set("command", self._start_command)
221 if not self.get("depends"):
222 self.set("depends", self._dependencies)
224 if self.get("sources"):
225 sources = self.get("sources")
226 source = sources.split(" ")[0]
227 basename = os.path.basename(source)
228 version = ( basename.strip().replace(".tar.gz", "")
231 .replace(".zip","") )
233 self.set("ns3Version", version)
234 self.set("sources", source)
236 if not self.get("build"):
237 self.set("build", self._build)
239 if not self.get("install"):
240 self.set("install", self._install)
242 if not self.get("env"):
243 self.set("env", self._environment)
249 self._client = LinuxNS3Client(self)
256 """ Starts simulation execution
259 self.info("Starting")
261 if self.state == ResourceState.READY:
262 if self.get("populateRoutingTables") == True:
263 self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables")
269 msg = " Failed to execute command '%s'" % command
270 self.error(msg, out, err)
271 raise RuntimeError, msg
274 """ Stops simulation execution
277 if self.state == ResourceState.STARTED:
281 def do_release(self):
282 self.info("Releasing resource")
284 tear_down = self.get("tearDown")
286 self.node.execute(tear_down)
289 self._client.shutdown()
290 LinuxApplication.do_stop(self)
292 super(LinuxApplication, self).do_release()
295 def _start_command(self):
298 command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
300 command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % \
301 os.path.basename(self.remote_socket) )
303 ns_log = self.get("nsLog")
305 command.append("-L %s" % ns_log)
307 if self.get("verbose"):
310 command = " ".join(command)
314 def _dependencies(self):
315 if self.node.use_rpm:
316 return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
317 elif self.node.use_deb:
318 return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
323 return "http://code.nsnam.org"
326 def pygccxml_version(self):
327 return "pygccxml-1.0.0"
331 # If the user defined local sources for ns-3, we uncompress the sources
332 # on the remote sources directory. Else we clone ns-3 from the official repo.
333 source = self.get("sources")
335 clone_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \
337 'ns3_version': self.get("ns3Version"),
338 'ns3_repo': self.ns3_repo,
341 if source.find(".tar.gz") > -1:
343 "tar xzf ${SRC}/ns-3/%(basename)s "
344 " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
346 'basename': os.path.basename(source),
347 'ns3_version': self.get("ns3Version"),
349 elif source.find(".tar") > -1:
351 "tar xf ${SRC}/ns-3/%(basename)s "
352 " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
354 'basename': os.path.basename(source),
355 'ns3_version': self.get("ns3Version"),
357 elif source.find(".zip") > -1:
358 basename = os.path.basename(source)
359 bare_basename = basename.replace(".zip", "") \
360 .replace(".tar", "") \
361 .replace(".tar.gz", "")
364 "unzip ${SRC}/ns-3/%(basename)s && "
365 "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s "
367 'bare_basename': basename_name,
368 'basename': basename,
369 'ns3_version': self.get("ns3Version"),
372 clone_dce_cmd = " echo 'DCE will not be built' "
373 if self.get("enableDCE"):
376 # Test if dce is alredy installed
379 " ( test -d ${SRC}/dce/ns-3-dce ) "
380 " && echo 'dce binaries found, nothing to do'"
384 # Get dce source code
386 " mkdir -p ${SRC}/dce && "
387 " hg clone http://code.nsnam.org/ns-3-dce ${SRC}/dce/ns-3-dce"
395 # Test if ns-3 is alredy installed
396 " ((( test -d ${SRC}/ns-3/%(ns3_version)s ) || "
397 " ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'})) "
398 " && echo 'ns-3 binaries found, nothing to do' )"
401 # If not, install ns-3 and its dependencies
406 " python -c 'import pygccxml' && "
407 " echo 'pygccxml not found' "
411 " tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
412 " cd ${SRC}/%(pygccxml_version)s && "
413 " python setup.py build && "
414 " sudo -S python setup.py install "
421 " test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
422 " echo 'binaries found, nothing to do' "
425 # If not, clone and build
427 " mkdir -p ${SRC}/pybindgen && "
428 " bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
429 " cd ${SRC}/pybindgen/%(pybindgen_version)s && "
430 " ./waf configure && "
435 # Get ns-3 source code
437 " mkdir -p ${SRC}/ns-3/%(ns3_version)s && "
438 " %(clone_ns3_cmd)s "
444 " %(clone_dce_cmd)s "
447 'ns3_version': self.get("ns3Version"),
448 'pybindgen_version': self.get("pybindgenVersion"),
449 'pygccxml_version': self.pygccxml_version,
450 'clone_ns3_cmd': clone_ns3_cmd,
451 'clone_dce_cmd': clone_dce_cmd,
456 install_dce_cmd = " echo 'DCE will not be installed' "
457 if self.get("enableDCE"):
460 " ((test -d %(ns3_build_home)s/bin_dce ) && "
461 " echo 'dce binaries found, nothing to do' )"
465 # If not, copy ns-3 build to bin
466 " cd ${SRC}/dce/ns-3-dce && "
467 " ./waf configure --enable-opt --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
468 " --prefix=%(ns3_build_home)s --with-ns3=%(ns3_build_home)s && "
473 'ns3_version': self.get("ns3Version"),
474 'pybindgen_version': self.get("pybindgenVersion"),
475 'ns3_build_home': self.ns3_build_home,
476 'build_mode': self.get("buildMode"),
480 # Test if ns-3 is alredy installed
483 " ( ( (test -d %(ns3_build_home)s/lib ) || "
484 " (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
485 " echo 'binaries found, nothing to do' )"
489 # If not, copy ns-3 build to bin
490 " mkdir -p %(ns3_build_home)s && "
491 " cd ${SRC}/ns-3/%(ns3_version)s && "
492 " ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
493 " --prefix=%(ns3_build_home)s --includedir=%(ns3_build_home)s && "
496 " mv %(ns3_build_home)s/lib/python* %(ns3_build_home)s/lib/python "
501 " %(install_dce_cmd)s "
504 'ns3_version': self.get("ns3Version"),
505 'pybindgen_version': self.get("pybindgenVersion"),
506 'build_mode': self.get("buildMode"),
507 'ns3_build_home': self.ns3_build_home,
508 'install_dce_cmd': install_dce_cmd
512 def _environment(self):
514 env.append("PYTHONPATH=$PYTHONPATH:${NS3BINDINGS:=%(ns3_build_home)s/lib/python/site-packages}" % {
515 'ns3_build_home': self.ns3_build_home
517 env.append("LD_LIBRARY_PATH=${NS3LIBRARIES:=%(ns3_build_home)s/lib/}" % {
518 'ns3_build_home': self.ns3_build_home
523 def valid_connection(self, guid):