from nepi.resources.linux.application import LinuxApplication
from nepi.util.timefuncs import tnow, tdiffsec
from nepi.resources.ns3.ns3simulation import NS3Simulation
+from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID, GLOBAL_VALUE_UUID, \
+ IPV4_GLOBAL_ROUTING_HELPER_UUID
from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
import os
+import time
+import threading
@clsinit_copy
class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
@classmethod
def _register_attributes(cls):
+ impl_type = Attribute("simulatorImplementationType",
+ "The object class to use as the simulator implementation",
+ allowed = ["ns3::DefaultSimulatorImpl", "ns3::RealtimeSimulatorImpl"],
+ default = "ns3::DefaultSimulatorImpl",
+ type = Types.Enumerate,
+ flags = Flags.Design)
+
+ sched_type = Attribute("schedulerType",
+ "The object class to use as the scheduler implementation",
+ allowed = ["ns3::MapScheduler",
+ "ns3::ListScheduler",
+ "ns3::HeapScheduler",
+ "ns3::MapScheduler",
+ "ns3::CalendarScheduler"
+ ],
+ default = "ns3::MapScheduler",
+ type = Types.Enumerate,
+ flags = Flags.Design)
+
+ check_sum = Attribute("checksumEnabled",
+ "A global switch to enable all checksums for all protocols",
+ default = False,
+ type = Types.Bool,
+ flags = Flags.Design)
+
ns_log = Attribute("nsLog",
- "NS_LOG environment variable ",
+ "NS_LOG environment variable. " \
+ " Will only generate output if ns-3 is compiled in DEBUG mode. ",
flags = Flags.Design)
verbose = Attribute("verbose",
type = Types.Bool,
flags = Flags.Design)
+ build_mode = Attribute("buildMode",
+ "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
+ default = "optimized",
+ allowed = ["debug", "release", "optimized"],
+ type = Types.Enumerate,
+ flags = Flags.Design)
+
+ ns3_version = Attribute("ns3Version",
+ "Version of ns-3 to install from nsam repo",
+ #default = "ns-3.19",
+ default = "ns-3-dev",
+ flags = Flags.Design)
+
+ enable_dce = Attribute("enableDCE",
+ "Install DCE source code",
+ default = False,
+ type = Types.Bool,
+ flags = Flags.Design)
+
+ pybindgen_version = Attribute("pybindgenVersion",
+ "Version of pybindgen to install from bazar repo",
+ default = "868",
+ flags = Flags.Design)
+
+ populate_routing_tables = Attribute("populateRoutingTables",
+ "Invokes Ipv4GlobalRoutingHelper.PopulateRoutingTables() ",
+ default = False,
+ type = Types.Bool,
+ flags = Flags.Design)
+
+ cls._register_attribute(impl_type)
+ cls._register_attribute(sched_type)
+ cls._register_attribute(check_sum)
cls._register_attribute(ns_log)
-
cls._register_attribute(verbose)
+ cls._register_attribute(build_mode)
+ cls._register_attribute(ns3_version)
+ cls._register_attribute(pybindgen_version)
+ cls._register_attribute(populate_routing_tables)
+ cls._register_attribute(enable_dce)
def __init__(self, ec, guid):
LinuxApplication.__init__(self, ec, guid)
self._client = None
self._home = "ns3-simu-%s" % self.guid
- self._socket_name = "ns3simu-%s" % os.urandom(8).encode('hex')
+ self._socket_name = "ns3-%s.sock" % os.urandom(4).encode('hex')
+ self._dce_manager_helper_uuid = None
+ self._dce_application_helper_uuid = None
+
+ # Lock used to synchronize usage of DceManagerHelper
+ self.dce_manager_lock = threading.Lock()
+ # Lock used to synchronize usage of DceApplicationHelper
+ self.dce_application_lock = threading.Lock()
@property
def socket_name(self):
return os.path.join(self.run_home, self.socket_name)
@property
- def local_socket(self):
- if self.node.get('hostname') in ['localhost', '127.0.0.01']:
- return self.remote_socket
+ def dce_manager_helper_uuid(self):
+ return self._dce_manager_helper_uuid
- return os.path.join("/", "tmp", self.socket_name)
+ @property
+ def dce_application_helper_uuid(self):
+ return self._dce_application_helper_uuid
+
+ @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()
+ return LinuxApplication.trace(self, name, attr, block, offset)
def upload_sources(self):
self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
overwrite = False)
+ # Upload user defined ns-3 sources
+ self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
+ src_dir = os.path.join(self.node.src_dir, "ns-3")
+
+ super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
+
def upload_start_command(self):
command = self.get("command")
env = self.get("env")
# Run the ns3wrapper
self._run_in_background()
+ def configure(self):
+ if self.has_changed("simulatorImplementationType"):
+ simu_type = self.get("simulatorImplementationType")
+ stype = self.create("StringValue", simu_type)
+ self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
+
+ if self.has_changed("checksumEnabled"):
+ check_sum = self.get("checksumEnabled")
+ btrue = self.create("BooleanValue", check_sum)
+ self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
+
+ if self.has_changed("schedulerType"):
+ sched_type = self.get("schedulerType")
+ stype = self.create("StringValue", sched_type)
+ self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
+
+ if self.get("enableDCE"):
+ self._dce_manager_helper_uuid = self.create("DceManagerHelper")
+ self._dce_application_helper_uuid = self.create("DceApplicationHelper")
+
def do_deploy(self):
if not self.node or self.node.state < ResourceState.READY:
self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
if not self.get("depends"):
self.set("depends", self._dependencies)
+ if self.get("sources"):
+ sources = self.get("sources")
+ source = sources.split(" ")[0]
+ basename = os.path.basename(source)
+ version = ( basename.strip().replace(".tar.gz", "")
+ .replace(".tar","")
+ .replace(".gz","")
+ .replace(".zip","") )
+
+ self.set("ns3Version", version)
+ self.set("sources", source)
+
if not self.get("build"):
self.set("build", self._build)
self.do_provision()
# Create client
- self._client = LinuxNS3Client(self)
+ self._client = LinuxNS3Client(self)
+ self.configure()
+
self.set_ready()
def do_start(self):
""" Starts simulation execution
"""
- self.info("Starting ns-3 simulation")
+ self.info("Starting")
if self.state == ResourceState.READY:
+ if self.get("populateRoutingTables") == True:
+ self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables")
+
+ self._client.start()
+
self.set_started()
else:
msg = " Failed to execute command '%s'" % command
"""
if self.state == ResourceState.STARTED:
- # TODO: Stop simulation
- LinuxApplication.do_stop(self)
+ self._client.stop()
+ self.set_stopped()
def do_release(self):
self.info("Releasing resource")
self.node.execute(tear_down)
self.do_stop()
-
self._client.shutdown()
+ LinuxApplication.do_stop(self)
super(LinuxApplication, self).do_release()
@property
def _start_command(self):
command = []
- command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
- command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % self.remote_socket )
+ command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
+
+ command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % \
+ os.path.basename(self.remote_socket) )
ns_log = self.get("nsLog")
if ns_log:
- command.append("-L %s" % ns_log)
+ command.append("-L '%s'" % ns_log)
+
if self.get("verbose"):
command.append("-v")
- command.append("-H")
- command.append(self.run_home)
-
command = " ".join(command)
return command
@property
def _dependencies(self):
if self.node.use_rpm:
- return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml")
+ return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
elif self.node.use_deb:
- return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml")
+ return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
return ""
@property
def ns3_repo(self):
- return "http://code.nsnam.org"
+ return "http://code.nsnam.org"
@property
- def ns3_version(self):
- return "ns-3.19"
-
- @property
- def pybindgen_version(self):
- return "834"
+ def pygccxml_version(self):
+ return "pygccxml-1.0.0"
@property
- def pygccxml_version(self):
- return "pygccxml-1.0.0"
+ def dce_repo(self):
+ #return "http://code.nsnam.org/ns-3-dce"
+ return "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.
+ source = self.get("sources")
+ if not source:
+ clone_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \
+ % {
+ 'ns3_version': self.get("ns3Version"),
+ 'ns3_repo': self.ns3_repo,
+ }
+ 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 "
+ ) % {
+ 'basename': os.path.basename(source),
+ 'ns3_version': self.get("ns3Version"),
+ }
+ 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 "
+ ) % {
+ 'basename': os.path.basename(source),
+ 'ns3_version': self.get("ns3Version"),
+ }
+ elif source.find(".zip") > -1:
+ basename = os.path.basename(source)
+ bare_basename = basename.replace(".zip", "") \
+ .replace(".tar", "") \
+ .replace(".tar.gz", "")
+
+ clone_ns3_cmd = (
+ "unzip ${SRC}/ns-3/%(basename)s && "
+ "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s "
+ ) % {
+ 'bare_basename': basename_name,
+ 'basename': basename,
+ 'ns3_version': self.get("ns3Version"),
+ }
+
+ clone_dce_cmd = " echo 'DCE will not be built' "
+ if self.get("enableDCE"):
+ clone_dce_cmd = (
+ # DCE installation
+ # Test if dce is alredy installed
+ " ( "
+ " ( "
+ " ( test -d ${SRC}/dce/ns-3-dce ) "
+ " && 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"
+ " ) "
+ ) % {
+ 'dce_repo': self.dce_repo
+ }
+
+
return (
- # Test if ns-3 is alredy installed
+ # NS3 installation
+ "( "
" ( "
- " (( "
- " ( test -d ${SRC}/%(ns3_version)s ) || (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
- " echo 'binaries found, nothing to do' )"
+ # Test if ns-3 is alredy installed
+ " ((( test -d ${SRC}/ns-3/%(ns3_version)s ) || "
+ " ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'})) "
+ " && echo 'ns-3 binaries found, nothing to do' )"
" ) "
" || "
# If not, install ns-3 and its dependencies
" ( "
" tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
" cd ${SRC}/%(pygccxml_version)s && "
+ " python setup.py build && "
" sudo -S python setup.py install "
" ) "
" ) "
" && "
" ( "
" ( "
- " test -d ${BIN}/pybindgen && "
+ " test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
" echo 'binaries found, nothing to do' "
" ) "
" || "
# If not, clone and build
" ( cd ${SRC} && "
- " bzr checkout lp:pybindgen -r %(pybindgen_version)s && "
- " cd ${SRC}/pybindgen && "
+ " mkdir -p ${SRC}/pybindgen && "
+ " bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
+ " cd ${SRC}/pybindgen/%(pybindgen_version)s && "
" ./waf configure && "
" ./waf "
" ) "
" ) "
- " && "
- # Clone and build ns-3
+ " && "
+ # Get ns-3 source code
" ( "
- " hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/%(ns3_version)s && "
- " cd ${SRC}/%(ns3_version)s && "
- " ./waf configure -d optimized && "
- " ./waf "
- " ) "
+ " mkdir -p ${SRC}/ns-3/%(ns3_version)s && "
+ " %(clone_ns3_cmd)s "
+ " ) "
" ) "
- ) % ({
- 'ns3_repo': self.ns3_repo,
- 'ns3_version': self.ns3_version,
- 'pybindgen_version': self.pybindgen_version,
- 'pygccxml_version': self.pygccxml_version
- })
+ ") "
+ " && "
+ "( "
+ " %(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,
+ }
@property
def _install(self):
+ install_dce_cmd = " echo 'DCE will not be installed' "
+ if self.get("enableDCE"):
+ install_dce_cmd = (
+ " ( "
+ " ((test -d %(ns3_build_home)s/bin_dce ) && "
+ " echo 'dce binaries found, nothing to do' )"
+ " ) "
+ " ||"
+ " ( "
+ # If not, copy ns-3 build to bin
+ " cd ${SRC}/dce/ns-3-dce && "
+ " ./waf configure %(enable_opt)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
+ " --prefix=%(ns3_build_home)s --with-ns3=%(ns3_build_home)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/ "
+ " )"
+ ) % {
+ '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 (
- # Test if ns-3 is alredy cloned
+ # Test if ns-3 is alredy installed
+ "("
" ( "
- " ( ( (test -d ${BIN}/%(ns3_version)s/build ) || "
+ " ( ( (test -d %(ns3_build_home)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 ${BIN}/%(ns3_version)s && "
- " mv ${SRC}/%(ns3_version)s/build ${BIN}/%(ns3_version)s/build "
+ " mkdir -p %(ns3_build_home)s && "
+ " cd ${SRC}/ns-3/%(ns3_version)s && "
+ " ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
+ " --prefix=%(ns3_build_home)s && "
+ " ./waf build && "
+ " ./waf install && "
+ " mv %(ns3_build_home)s/lib*/python* %(ns3_build_home)s/lib/python "
" )"
- ) % ({
- 'ns3_version': self.ns3_version
- })
+ ") "
+ " && "
+ "( "
+ " %(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
+ }
@property
def _environment(self):
env = []
- env.append("NS3BINDINGS=${NS3BINDINGS:=${BIN}/%(ns3_version)s/build/bindings/python/}" % ({
- 'ns3_version': self.ns3_version,
- }))
- env.append("NS3LIBRARIES=${NS3LIBRARIES:=${BIN}/%(ns3_version)s/build/}" % ({
- 'ns3_version': self.ns3_version,
- }))
+ env.append("PYTHONPATH=$PYTHONPATH:${NS3BINDINGS:=%(ns3_build_home)s/lib/python/site-packages}" % {
+ 'ns3_build_home': self.ns3_build_home
+ })
+ env.append("LD_LIBRARY_PATH=${NS3LIBRARIES:=%(ns3_build_home)s/lib/}" % {
+ 'ns3_build_home': self.ns3_build_home
+ })
+ env.append("DCE_PATH=$PATH:$NS3LIBRARIES/../bin_dce")
+ env.append("DCE_ROOT=$PATH:$NS3LIBRARIES/..")
return " ".join(env)