X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fnepi%2Fresources%2Flinux%2Fns3%2Fns3simulation.py;h=61eae0ab3286fe220d7ee8371638ae1a699e5233;hb=ac866efb762875550bdc0c05d693e5eb026f435e;hp=340660f0fd016e368be0a75e15619238a80fab7c;hpb=12ba748a370b2c86045e5b2037ed46778b91459e;p=nepi.git diff --git a/src/nepi/resources/linux/ns3/ns3simulation.py b/src/nepi/resources/linux/ns3/ns3simulation.py index 340660f0..61eae0ab 100644 --- a/src/nepi/resources/linux/ns3/ns3simulation.py +++ b/src/nepi/resources/linux/ns3/ns3simulation.py @@ -20,7 +20,7 @@ from nepi.execution.attribute import Attribute, Flags, Types from nepi.execution.trace import Trace, TraceAttr from nepi.execution.resource import ResourceManager, clsinit_copy, \ - ResourceState, ResourceFactory, reschedule_delay + ResourceState, ResourceFactory from nepi.resources.linux.application import LinuxApplication from nepi.util.timefuncs import tnow, tdiffsec from nepi.resources.ns3.ns3simulation import NS3Simulation @@ -92,14 +92,20 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): ns3_version = Attribute("ns3Version", "Version of ns-3 to install from nsam repo", - #default = "ns-3.19", - default = "ns-3-dev", + default = "ns-3.20", + #default = "ns-3-dev", flags = Flags.Design) pybindgen_version = Attribute("pybindgenVersion", "Version of pybindgen to install from bazar repo", - #default = "864", default = "868", + #default = "876", + flags = Flags.Design) + + dce_version = Attribute("dceVersion", + "Version of dce to install from nsam repo (tag branch for repo)", + #default = "dce-1.3", + default = "dce-dev", flags = Flags.Design) populate_routing_tables = Attribute("populateRoutingTables", @@ -108,7 +114,7 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): type = Types.Bool, flags = Flags.Design) - stoptime = Attribute("stopTime", + stoptime = Attribute("StopTime", "Time at which the simulation will stop", flags = Flags.Design) @@ -121,6 +127,7 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): cls._register_attribute(build_mode) cls._register_attribute(ns3_version) cls._register_attribute(pybindgen_version) + cls._register_attribute(dce_version) cls._register_attribute(populate_routing_tables) cls._register_attribute(stoptime) @@ -143,13 +150,19 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): def remote_socket(self): return os.path.join(self.run_home, self.socket_name) - @property - def ns3_build_home(self): - return os.path.join(self.node.bin_dir, "ns-3", self.get("ns3Version"), - self.get("buildMode"), "build") - def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0): - self._client.flush() + # stout needs to get flushed on the ns-3 server side, else we will + # get an empty stream. We try twice to retrieve the stream + # if we get empty stdout since the stream might not be + # flushed immediately. + if name.endswith("stdout"): + self._client.flush() + result = LinuxApplication.trace(self, name, attr, block, offset) + if result: + return result + # Let the stream be flushed + time.sleep(1) + return LinuxApplication.trace(self, name, attr, block, offset) def upload_sources(self): @@ -220,6 +233,9 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): # Run the ns3wrapper self._run_in_background() + # Wait until the remote socket is created + self.wait_remote_socket() + def configure(self): if self.has_changed("simulatorImplementationType"): simu_type = self.get("simulatorImplementationType") @@ -241,7 +257,7 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state ) # ccnd needs to wait until node is deployed and running - self.ec.schedule(reschedule_delay, self.deploy) + self.ec.schedule(self.reschedule_delay, self.deploy) else: if not self.get("command"): self.set("command", self._start_command) @@ -290,23 +306,11 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): if self.get("populateRoutingTables") == True: self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables") - self._client.start() + time = self.get("StopTime") + if time: + self._client.stop(time=time) - # XXX: IS THIS REALLY NEEDED??!!! - # Wait until the Simulation is actually started... - is_running = False - for i in xrange(1000): - is_running = self.invoke(SIMULATOR_UUID, "isRunning") - - if is_running: - break - else: - time.sleep(1) - else: - if not is_running: - msg = " Simulation did not start" - self.error(msg) - raise RuntimeError + self._client.start() self.set_started() else: @@ -319,11 +323,8 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): """ if self.state == ResourceState.STARTED: - time = None - if self.get("stopTime"): - time = self.get("stopTime") - - self._client.stop(time=time) + if not self.get("StopTime"): + self._client.stop() self.set_stopped() def do_release(self): @@ -399,75 +400,130 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): #eturn "http://code.nsnam.org/epmancini/ns-3-dce" @property - def _build(self): - # If the user defined local sources for ns-3, we uncompress the sources - # on the remote sources directory. Else we clone ns-3 from the official repo. + def dce_version(self): + dce_version = self.get("dceVersion") + return dce_version or "dce-dev" + + @property + def ns3_build_location(self): + location = "${BIN}/ns-3/%(ns3_version)s%(dce_version)s/%(build_mode)s/build" \ + % { + "ns3_version": self.get("ns3Version"), + "dce_version": "-%s" % self.get("dceVersion") \ + if self.enable_dce else "", + "build_mode": self.get("buildMode"), + } + + return location + + + @property + def ns3_src_location(self): + location = "${SRC}/ns-3/%(ns3_version)s" \ + % { + "ns3_version": self.get("ns3Version"), + } + + return location + + @property + def dce_src_location(self): + location = "${SRC}/ns-3-dce/%(dce_version)s" \ + % { + "dce_version": self.get("dceVersion"), + } + + return location + + @property + def _clone_ns3_command(self): source = self.get("sources") + if not source: - clone_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \ + clone_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s %(ns3_src)s" \ % { - 'ns3_version': self.get("ns3Version"), - 'ns3_repo': self.ns3_repo, + "ns3_version": self.get("ns3Version"), + "ns3_repo": self.ns3_repo, + "ns3_src": self.ns3_src_location, } else: if source.find(".tar.gz") > -1: clone_ns3_cmd = ( "tar xzf ${SRC}/ns-3/%(basename)s " - " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s " + " --strip-components=1 -C %(ns3_src)s" ) % { - 'basename': os.path.basename(source), - 'ns3_version': self.get("ns3Version"), + "basename": os.path.basename(source), + "ns3_src": self.ns3_src_location, } elif source.find(".tar") > -1: clone_ns3_cmd = ( "tar xf ${SRC}/ns-3/%(basename)s " - " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s " + " --strip-components=1 -C %(ns3_src)s" ) % { - 'basename': os.path.basename(source), - 'ns3_version': self.get("ns3Version"), + "basename": os.path.basename(source), + "ns3_src": self.ns3_src_location, } elif source.find(".zip") > -1: basename = os.path.basename(source) - bare_basename = basename.replace(".zip", "") \ - .replace(".tar", "") \ - .replace(".tar.gz", "") + bare_basename = basename.replace(".zip", "") clone_ns3_cmd = ( "unzip ${SRC}/ns-3/%(basename)s && " - "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s " + "mv ${SRC}/ns-3/%(bare_basename)s %(ns3_src)s" ) % { - 'bare_basename': basename_name, - 'basename': basename, - 'ns3_version': self.get("ns3Version"), + "bare_basename": basename_name, + "basename": basename, + "ns3_src": self.ns3_src_location, } + return clone_ns3_cmd + + @property + def _clone_dce_command(self): clone_dce_cmd = " echo 'DCE will not be built' " + if self.enable_dce: + dce_version = self.dce_version + dce_tag = "" + if dce_version != "dce-dev": + dce_tag = "-r %s" % dce_version + clone_dce_cmd = ( # DCE installation - # Test if dce is alredy installed + # Test if dce is alredy cloned " ( " " ( " - " ( test -d ${SRC}/dce/ns-3-dce ) " + " ( test -d %(dce_src)s ) " " && echo 'dce binaries found, nothing to do'" " ) " " ) " " || " # Get dce source code " ( " - " mkdir -p ${SRC}/dce && " - " hg clone %(dce_repo)s ${SRC}/dce/ns-3-dce" + " mkdir -p %(dce_src)s && " + " hg clone %(dce_repo)s %(dce_tag)s %(dce_src)s" " ) " ) % { - 'dce_repo': self.dce_repo + "dce_repo": self.dce_repo, + "dce_tag": dce_tag, + "dce_src": self.dce_src_location, } - return ( + return clone_dce_cmd + + @property + def _build(self): + # If the user defined local sources for ns-3, we uncompress the sources + # on the remote sources directory. Else we clone ns-3 from the official repo. + clone_ns3_cmd = self._clone_ns3_command + clone_dce_cmd = self._clone_dce_command + + ns3_build_cmd = ( # NS3 installation "( " " ( " - # Test if ns-3 is alredy installed - " ((( test -d ${SRC}/ns-3/%(ns3_version)s ) || " + # Test if ns-3 is alredy cloned + " ((( test -d %(ns3_src)s ) || " " ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'})) " " && echo 'ns-3 binaries found, nothing to do' )" " ) " @@ -508,7 +564,7 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): " && " # Get ns-3 source code " ( " - " mkdir -p ${SRC}/ns-3/%(ns3_version)s && " + " mkdir -p %(ns3_src)s && " " %(clone_ns3_cmd)s " " ) " " ) " @@ -518,60 +574,69 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): " %(clone_dce_cmd)s " ") " ) % { - 'ns3_version': self.get("ns3Version"), - 'pybindgen_version': self.get("pybindgenVersion"), - 'pygccxml_version': self.pygccxml_version, - 'clone_ns3_cmd': clone_ns3_cmd, - 'clone_dce_cmd': clone_dce_cmd, + "ns3_src": self.ns3_src_location, + "pybindgen_version": self.get("pybindgenVersion"), + "pygccxml_version": self.pygccxml_version, + "clone_ns3_cmd": clone_ns3_cmd, + "clone_dce_cmd": clone_dce_cmd, } + return ns3_build_cmd + @property - def _install(self): - install_dce_cmd = " echo 'DCE will not be installed' " + def _install_dce_command(self): + install_dce_cmd = " echo 'DCE will not be installed'" + if self.enable_dce: install_dce_cmd = ( " ( " - " ((test -d %(ns3_build_home)s/bin_dce ) && " + " ((test -d %(ns3_build)s/bin_dce ) && " " echo 'dce binaries found, nothing to do' )" " ) " " ||" " ( " - # If not, copy ns-3 build to bin - " cd ${SRC}/dce/ns-3-dce && " - " rm -rf ${SRC}/dce/ns-3-dce/build && " + # If not, copy build to dce + " cd %(dce_src)s && " + " rm -rf %(dce_src)s/build && " " ./waf configure %(enable_opt)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s " - " --prefix=%(ns3_build_home)s --with-ns3=%(ns3_build_home)s && " + " --prefix=%(ns3_build)s --with-ns3=%(ns3_build)s && " " ./waf build && " " ./waf install && " - " mv %(ns3_build_home)s/lib*/python*/site-packages/ns/dce.so %(ns3_build_home)s/lib/python/site-packages/ns/ " + " [ ! -e %(ns3_build)s/lib/python/site-packages/ns/dce.so ] && " + " mv %(ns3_build)s/lib*/python*/site-packages/ns/dce.so %(ns3_build)s/lib/python/site-packages/ns/ " " )" ) % { - 'ns3_version': self.get("ns3Version"), - 'pybindgen_version': self.get("pybindgenVersion"), - 'ns3_build_home': self.ns3_build_home, - 'build_mode': self.get("buildMode"), - 'enable_opt': "--enable-opt" if self.get("buildMode") == "optimized" else "" - } - - return ( + "pybindgen_version": self.get("pybindgenVersion"), + "enable_opt": "--enable-opt" if self.get("buildMode") == "optimized" else "", + "ns3_build": self.ns3_build_location, + "dce_src": self.dce_src_location, + } + + return install_dce_cmd + + @property + def _install(self): + install_dce_cmd = self._install_dce_command + + install_ns3_cmd = ( # Test if ns-3 is alredy installed "(" " ( " - " ( ( (test -d %(ns3_build_home)s/lib ) || " + " ( ( (test -d %(ns3_build)s/lib ) || " " (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && " " echo 'binaries found, nothing to do' )" " ) " " ||" " ( " # If not, copy ns-3 build to bin - " mkdir -p %(ns3_build_home)s && " - " cd ${SRC}/ns-3/%(ns3_version)s && " - " rm -rf ${SRC}/ns-3/%(ns3_version)s/build && " + " mkdir -p %(ns3_build)s && " + " cd %(ns3_src)s && " + " rm -rf %(ns3_src)s/build && " " ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s " - " --prefix=%(ns3_build_home)s && " + " --prefix=%(ns3_build)s && " " ./waf build && " " ./waf install && " - " mv %(ns3_build_home)s/lib*/python* %(ns3_build_home)s/lib/python " + " mv %(ns3_build)s/lib*/python* %(ns3_build)s/lib/python " " )" ") " " && " @@ -579,23 +644,25 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): " %(install_dce_cmd)s " ") " ) % { - 'ns3_version': self.get("ns3Version"), - 'pybindgen_version': self.get("pybindgenVersion"), - 'build_mode': self.get("buildMode"), - 'ns3_build_home': self.ns3_build_home, - 'install_dce_cmd': install_dce_cmd + "pybindgen_version": self.get("pybindgenVersion"), + "build_mode": self.get("buildMode"), + "install_dce_cmd": install_dce_cmd, + "ns3_build": self.ns3_build_location, + "ns3_src": self.ns3_src_location, } + return install_ns3_cmd + @property def _environment(self): env = [] - env.append("PYTHONPATH=$PYTHONPATH:${NS3BINDINGS:=%(ns3_build_home)s/lib/python/site-packages}" % { - 'ns3_build_home': self.ns3_build_home + env.append("PYTHONPATH=$PYTHONPATH:${NS3BINDINGS:=%(ns3_build)s/lib/python/site-packages}" % { + "ns3_build": self.ns3_build_location }) # If NS3LIBRARIES is defined and not empty, assign its value, # if not assign ns3_build_home/lib/ to NS3LIBRARIES and LD_LIBARY_PATH - env.append("LD_LIBRARY_PATH=${NS3LIBRARIES:=%(ns3_build_home)s/lib}" % { - 'ns3_build_home': self.ns3_build_home + env.append("LD_LIBRARY_PATH=${NS3LIBRARIES:=%(ns3_build)s/lib}" % { + "ns3_build": self.ns3_build_location }) env.append("DCE_PATH=$NS3LIBRARIES/../bin_dce") env.append("DCE_ROOT=$NS3LIBRARIES/..") @@ -621,10 +688,26 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): # If NS3LIBRARIES is defined and not empty, use that value, # if not use ns3_build_home/lib/ .replace("${BIN_DCE}", "${NS3LIBRARIES-%s/lib}/../bin_dce" % \ - self.ns3_build_home) + self.ns3_build_location) ) def valid_connection(self, guid): # TODO: Validate! return True + def wait_remote_socket(self): + """ Waits until the remote socket is created + """ + command = " [ -e %s ] && echo 'DONE' " % self.remote_socket + + for i in xrange(200): + (out, err), proc = self.node.execute(command, retry = 1, + with_lock = True) + + if out.find("DONE") > -1: + break + else: + raise RuntimeError("Remote socket not found at %s" % \ + self.remote_socket) + +