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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.trace import Trace, TraceAttr
21 from nepi.execution.resource import ResourceManager, clsinit_copy, \
22 ResourceState, ResourceFactory
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.util.timefuncs import tnow, tdiffsec
25 from nepi.resources.ns3.ns3simulation import NS3Simulation
26 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID, GLOBAL_VALUE_UUID, \
27 IPV4_GLOBAL_ROUTING_HELPER_UUID
28 from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
35 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
36 _rtype = "linux::ns3::Simulation"
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 enable_dump = Attribute("enableDump",
76 "Enable dumping the remote executed ns-3 commands to a script "
77 "in order to later reproduce and debug the experiment",
82 build_mode = Attribute("buildMode",
83 "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
84 default = "optimized",
85 allowed = ["debug", "release", "optimized"],
86 type = Types.Enumerate,
89 ns3_version = Attribute("ns3Version",
90 "Version of ns-3 to install from nsam repo",
92 #default = "ns-3-dev",
95 pybindgen_version = Attribute("pybindgenVersion",
96 "Version of pybindgen to install from bazar repo",
101 dce_version = Attribute("dceVersion",
102 "Version of dce to install from nsam repo (tag branch for repo)",
103 #default = "dce-1.3",
105 flags = Flags.Design)
107 populate_routing_tables = Attribute("populateRoutingTables",
108 "Invokes Ipv4GlobalRoutingHelper.PopulateRoutingTables() ",
111 flags = Flags.Design)
113 stoptime = Attribute("StopTime",
114 "Time at which the simulation will stop",
115 flags = Flags.Design)
117 cls._register_attribute(impl_type)
118 cls._register_attribute(sched_type)
119 cls._register_attribute(check_sum)
120 cls._register_attribute(ns_log)
121 cls._register_attribute(enable_dump)
122 cls._register_attribute(verbose)
123 cls._register_attribute(build_mode)
124 cls._register_attribute(ns3_version)
125 cls._register_attribute(pybindgen_version)
126 cls._register_attribute(dce_version)
127 cls._register_attribute(populate_routing_tables)
128 cls._register_attribute(stoptime)
130 def __init__(self, ec, guid):
131 LinuxApplication.__init__(self, ec, guid)
132 NS3Simulation.__init__(self)
135 self._home = "ns3-simu-%s" % self.guid
136 self._socket_name = "ns3-%s.sock" % os.urandom(4).encode('hex')
137 self._enable_dce = None
138 self._dce_helper = None
141 def socket_name(self):
142 return self._socket_name
145 def remote_socket(self):
146 return os.path.join(self.run_home, self.socket_name)
148 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
149 # stout needs to get flushed on the ns-3 server side, else we will
150 # get an empty stream. We try twice to retrieve the stream
151 # if we get empty stdout since the stream might not be
152 # flushed immediately.
153 if name.endswith("stdout"):
155 result = LinuxApplication.trace(self, name, attr, block, offset)
158 # Let the stream be flushed
161 return LinuxApplication.trace(self, name, attr, block, offset)
163 def upload_sources(self):
164 self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
166 # upload ns3 wrapper python script
167 ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
170 self.node.upload(ns3_wrapper,
171 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
174 # upload ns3 wrapper debug python script
175 ns3_wrapper_debug = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
176 "ns3wrapper_debug.py")
178 self.node.upload(ns3_wrapper_debug,
179 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper_debug.py"),
182 # upload ns3_server python script
183 ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
186 self.node.upload(ns3_server,
187 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
190 if self.node.use_rpm:
191 # upload pygccxml sources
192 pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
193 "%s.tar.gz" % self.pygccxml_version)
195 self.node.upload(pygccxml_tar,
196 os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
199 # Upload user defined ns-3 sources
200 self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
201 src_dir = os.path.join(self.node.src_dir, "ns-3")
203 super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
205 def upload_extra_sources(self, sources = None, src_dir = None):
206 return super(LinuxNS3Simulation, self).upload_sources(
210 def upload_start_command(self):
211 command = self.get("command")
212 env = self.get("env")
214 # We want to make sure the ccnd is running
215 # before the experiment starts.
216 # Run the command as a bash script in background,
217 # in the host ( but wait until the command has
218 # finished to continue )
219 env = self.replace_paths(env)
220 command = self.replace_paths(command)
222 shfile = os.path.join(self.app_home, "start.sh")
223 self.node.upload_command(command,
229 self._run_in_background()
231 # Wait until the remote socket is created
232 self.wait_remote_socket()
235 if self.has_changed("simulatorImplementationType"):
236 simu_type = self.get("simulatorImplementationType")
237 stype = self.create("StringValue", simu_type)
238 self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
240 if self.has_changed("checksumEnabled"):
241 check_sum = self.get("checksumEnabled")
242 btrue = self.create("BooleanValue", check_sum)
243 self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
245 if self.has_changed("schedulerType"):
246 sched_type = self.get("schedulerType")
247 stype = self.create("StringValue", sched_type)
248 self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
251 if not self.node or self.node.state < ResourceState.READY:
252 self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
254 # ccnd needs to wait until node is deployed and running
255 self.ec.schedule(self.reschedule_delay, self.deploy)
257 if not self.get("command"):
258 self.set("command", self._start_command)
260 if not self.get("depends"):
261 self.set("depends", self._dependencies)
263 if self.get("sources"):
264 sources = self.get("sources")
265 source = sources.split(" ")[0]
266 basename = os.path.basename(source)
267 version = ( basename.strip().replace(".tar.gz", "")
270 .replace(".zip","") )
272 self.set("ns3Version", version)
273 self.set("sources", source)
275 if not self.get("build"):
276 self.set("build", self._build)
278 if not self.get("install"):
279 self.set("install", self._install)
281 if not self.get("env"):
282 self.set("env", self._environment)
288 self._client = LinuxNS3Client(self)
295 """ Starts simulation execution
298 self.info("Starting")
300 if self.state == ResourceState.READY:
301 if self.get("populateRoutingTables") == True:
302 self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables")
304 time = self.get("StopTime")
306 self._client.stop(time=time)
312 msg = " Failed to execute command '%s'" % command
313 self.error(msg, out, err)
314 raise RuntimeError(msg)
317 """ Stops simulation execution
320 if self.state == ResourceState.STARTED:
321 if not self.get("StopTime"):
325 def do_release(self):
326 self.info("Releasing resource")
328 tear_down = self.get("tearDown")
330 self.node.execute(tear_down)
333 self._client.shutdown()
334 LinuxApplication.do_stop(self)
336 super(LinuxApplication, self).do_release()
340 super(LinuxApplication, self).state
342 if self._state == ResourceState.STARTED:
344 is_finished = self.invoke(SIMULATOR_UUID, "isFinished")
349 msg = "Simulator failed. Can not retrieve state"
353 err = traceback.format_exc()
354 self.error(msg, out, err)
360 def enable_dce(self):
361 if self._enable_dce is None:
362 from nepi.resources.ns3.ns3dceapplication import NS3BaseDceApplication
363 rclass = ResourceFactory.get_resource_type(
364 NS3BaseDceApplication.get_rtype())
366 self._enable_dce = False
367 for guid in self.ec.resources:
368 rm = self.ec.get_resource(guid)
369 if isinstance(rm, rclass):
370 self._enable_dce = True
372 from nepi.resources.ns3.ns3dcehelper import NS3DceHelper
373 self._dce_helper = NS3DceHelper(self)
376 return self._enable_dce
379 def dce_helper(self):
380 return self._dce_helper
383 def _start_command(self):
386 command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
388 command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % \
389 os.path.basename(self.remote_socket) )
391 ns_log = self.get("nsLog")
393 command.append("-L '%s'" % ns_log)
395 if self.get("enableDump"):
398 if self.get("verbose"):
401 command = " ".join(command)
405 def _dependencies(self):
406 if self.node.use_rpm:
407 return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
408 elif self.node.use_deb:
409 return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
414 return "http://code.nsnam.org"
417 def pygccxml_version(self):
418 return "pygccxml-1.0.0"
422 return "http://code.nsnam.org/ns-3-dce"
423 #eturn "http://code.nsnam.org/epmancini/ns-3-dce"
426 def dce_version(self):
427 dce_version = self.get("dceVersion")
428 return dce_version or "dce-dev"
431 def ns3_build_location(self):
432 location = "${BIN}/ns-3/%(ns3_version)s%(dce_version)s/%(build_mode)s/build" \
434 "ns3_version": self.get("ns3Version"),
435 "dce_version": "-%s" % self.get("dceVersion") \
436 if self.enable_dce else "",
437 "build_mode": self.get("buildMode"),
444 def ns3_src_location(self):
445 location = "${SRC}/ns-3/%(ns3_version)s" \
447 "ns3_version": self.get("ns3Version"),
453 def dce_src_location(self):
454 location = "${SRC}/ns-3-dce/%(dce_version)s" \
456 "dce_version": self.get("dceVersion"),
462 def _clone_ns3_command(self):
463 source = self.get("sources")
466 clone_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s %(ns3_src)s" \
468 "ns3_version": self.get("ns3Version"),
469 "ns3_repo": self.ns3_repo,
470 "ns3_src": self.ns3_src_location,
473 if source.find(".tar.gz") > -1:
475 "tar xzf ${SRC}/ns-3/%(basename)s "
476 " --strip-components=1 -C %(ns3_src)s"
478 "basename": os.path.basename(source),
479 "ns3_src": self.ns3_src_location,
481 elif source.find(".tar") > -1:
483 "tar xf ${SRC}/ns-3/%(basename)s "
484 " --strip-components=1 -C %(ns3_src)s"
486 "basename": os.path.basename(source),
487 "ns3_src": self.ns3_src_location,
489 elif source.find(".zip") > -1:
490 basename = os.path.basename(source)
491 bare_basename = basename.replace(".zip", "")
494 "unzip ${SRC}/ns-3/%(basename)s && "
495 "mv ${SRC}/ns-3/%(bare_basename)s %(ns3_src)s"
497 "bare_basename": basename_name,
498 "basename": basename,
499 "ns3_src": self.ns3_src_location,
505 def _clone_dce_command(self):
506 clone_dce_cmd = " echo 'DCE will not be built' "
509 dce_version = self.dce_version
511 if dce_version != "dce-dev":
512 dce_tag = "-r %s" % dce_version
516 # Test if dce is alredy cloned
519 " ( test -d %(dce_src)s ) "
520 " && echo 'dce binaries found, nothing to do'"
524 # Get dce source code
526 " mkdir -p %(dce_src)s && "
527 " hg clone %(dce_repo)s %(dce_tag)s %(dce_src)s"
530 "dce_repo": self.dce_repo,
532 "dce_src": self.dce_src_location,
539 # If the user defined local sources for ns-3, we uncompress the sources
540 # on the remote sources directory. Else we clone ns-3 from the official repo.
541 clone_ns3_cmd = self._clone_ns3_command
542 clone_dce_cmd = self._clone_dce_command
548 # Test if ns-3 is alredy cloned
549 " ((( test -d %(ns3_src)s ) || "
550 " ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'})) "
551 " && echo 'ns-3 binaries found, nothing to do' )"
554 # If not, install ns-3 and its dependencies
559 " python -c 'import pygccxml' && "
560 " echo 'pygccxml not found' "
564 " tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
565 " cd ${SRC}/%(pygccxml_version)s && "
566 " python setup.py build && "
567 " sudo -S python setup.py install "
574 " test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
575 " echo 'binaries found, nothing to do' "
578 # If not, clone and build
580 " mkdir -p ${SRC}/pybindgen && "
581 " bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
582 " cd ${SRC}/pybindgen/%(pybindgen_version)s && "
583 " ./waf configure && "
588 # Get ns-3 source code
590 " mkdir -p %(ns3_src)s && "
591 " %(clone_ns3_cmd)s "
597 " %(clone_dce_cmd)s "
600 "ns3_src": self.ns3_src_location,
601 "pybindgen_version": self.get("pybindgenVersion"),
602 "pygccxml_version": self.pygccxml_version,
603 "clone_ns3_cmd": clone_ns3_cmd,
604 "clone_dce_cmd": clone_dce_cmd,
610 def _install_dce_command(self):
611 install_dce_cmd = " echo 'DCE will not be installed'"
616 " ((test -d %(ns3_build)s/bin_dce ) && "
617 " echo 'dce binaries found, nothing to do' )"
621 # If not, copy build to dce
622 " cd %(dce_src)s && "
623 " rm -rf %(dce_src)s/build && "
624 " ./waf configure %(enable_opt)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
625 " --prefix=%(ns3_build)s --with-ns3=%(ns3_build)s && "
628 " [ ! -e %(ns3_build)s/lib/python/site-packages/ns/dce.so ] && "
629 " mv %(ns3_build)s/lib*/python*/site-packages/ns/dce.so %(ns3_build)s/lib/python/site-packages/ns/ "
632 "pybindgen_version": self.get("pybindgenVersion"),
633 "enable_opt": "--enable-opt" if self.get("buildMode") == "optimized" else "",
634 "ns3_build": self.ns3_build_location,
635 "dce_src": self.dce_src_location,
638 return install_dce_cmd
642 install_dce_cmd = self._install_dce_command
645 # Test if ns-3 is alredy installed
648 " ( ( (test -d %(ns3_build)s/lib ) || "
649 " (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
650 " echo 'binaries found, nothing to do' )"
654 # If not, copy ns-3 build to bin
655 " mkdir -p %(ns3_build)s && "
656 " cd %(ns3_src)s && "
657 " rm -rf %(ns3_src)s/build && "
658 " ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
659 " --prefix=%(ns3_build)s && "
662 " mv %(ns3_build)s/lib*/python* %(ns3_build)s/lib/python "
667 " %(install_dce_cmd)s "
670 "pybindgen_version": self.get("pybindgenVersion"),
671 "build_mode": self.get("buildMode"),
672 "install_dce_cmd": install_dce_cmd,
673 "ns3_build": self.ns3_build_location,
674 "ns3_src": self.ns3_src_location,
677 return install_ns3_cmd
680 def _environment(self):
682 env.append("PYTHONPATH=$PYTHONPATH:${NS3BINDINGS:=%(ns3_build)s/lib/python/site-packages}" % {
683 "ns3_build": self.ns3_build_location
685 # If NS3LIBRARIES is defined and not empty, assign its value,
686 # if not assign ns3_build_home/lib/ to NS3LIBRARIES and LD_LIBARY_PATH
687 env.append("LD_LIBRARY_PATH=${NS3LIBRARIES:=%(ns3_build)s/lib}" % {
688 "ns3_build": self.ns3_build_location
690 env.append("DCE_PATH=$NS3LIBRARIES/../bin_dce")
691 env.append("DCE_ROOT=$NS3LIBRARIES/..")
695 def replace_paths(self, command):
697 Replace all special path tags with shell-escaped actual paths.
700 .replace("${USR}", self.node.usr_dir)
701 .replace("${LIB}", self.node.lib_dir)
702 .replace("${BIN}", self.node.bin_dir)
703 .replace("${SRC}", self.node.src_dir)
704 .replace("${SHARE}", self.node.share_dir)
705 .replace("${EXP}", self.node.exp_dir)
706 .replace("${EXP_HOME}", self.node.exp_home)
707 .replace("${APP_HOME}", self.app_home)
708 .replace("${RUN_HOME}", self.run_home)
709 .replace("${NODE_HOME}", self.node.node_home)
710 .replace("${HOME}", self.node.home_dir)
711 # If NS3LIBRARIES is defined and not empty, use that value,
712 # if not use ns3_build_home/lib/
713 .replace("${BIN_DCE}", "${NS3LIBRARIES-%s/lib}/../bin_dce" % \
714 self.ns3_build_location)
717 def valid_connection(self, guid):
721 def wait_remote_socket(self):
722 """ Waits until the remote socket is created
724 command = " [ -e %s ] && echo 'DONE' " % self.remote_socket
727 (out, err), proc = self.node.execute(command, retry = 1,
730 if out.find("DONE") > -1:
733 raise RuntimeError("Remote socket not found at %s" % \