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 from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
34 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
35 _rtype = "LinuxNS3Simulation"
38 def _register_attributes(cls):
39 impl_type = Attribute("simulatorImplementationType",
40 "The object class to use as the simulator implementation",
41 allowed = ["ns3::DefaultSimulatorImpl", "ns3::RealtimeSimulatorImpl"],
42 default = "ns3::DefaultSimulatorImpl",
43 type = Types.Enumerate,
46 sched_type = Attribute("schedulerType",
47 "The object class to use as the scheduler implementation",
48 allowed = ["ns3::MapScheduler",
52 "ns3::CalendarScheduler"
54 default = "ns3::MapScheduler",
55 type = Types.Enumerate,
58 check_sum = Attribute("checksumEnabled",
59 "A global switch to enable all checksums for all protocols",
64 ns_log = Attribute("nsLog",
65 "NS_LOG environment variable. " \
66 " Will only generate output if ns-3 is compiled in DEBUG mode. ",
69 verbose = Attribute("verbose",
70 "True to output debugging info from the ns3 client-server communication",
74 build_mode = Attribute("buildMode",
75 "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
77 allowed = ["debug", "release", "optimized"],
78 type = Types.Enumerate,
81 ns3_version = Attribute("ns3Version",
82 "Version of ns-3 to install from nsam repo",
86 pybindgen_version = Attribute("pybindgenVersion",
87 "Version of pybindgen to install from bazar repo",
91 cls._register_attribute(impl_type)
92 cls._register_attribute(sched_type)
93 cls._register_attribute(check_sum)
94 cls._register_attribute(ns_log)
95 cls._register_attribute(verbose)
96 cls._register_attribute(build_mode)
97 cls._register_attribute(ns3_version)
98 cls._register_attribute(pybindgen_version)
100 def __init__(self, ec, guid):
101 LinuxApplication.__init__(self, ec, guid)
102 NS3Simulation.__init__(self)
105 self._home = "ns3-simu-%s" % self.guid
106 self._socket_name = "ns3simu-%s" % os.urandom(8).encode('hex')
109 def socket_name(self):
110 return self._socket_name
113 def remote_socket(self):
114 return os.path.join(self.run_home, self.socket_name)
117 def local_socket(self):
118 if self.node.get('hostname') in ['localhost', '127.0.0.01']:
119 return self.remote_socket
121 return os.path.join("/", "tmp", self.socket_name)
123 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
125 return LinuxApplication.trace(self, name, attr, block, offset)
127 def upload_sources(self):
128 self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
130 # upload ns3 wrapper python script
131 ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
134 self.node.upload(ns3_wrapper,
135 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
138 # upload ns3_server python script
139 ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
142 self.node.upload(ns3_server,
143 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
146 if self.node.use_rpm:
147 # upload pygccxml sources
148 pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
149 "%s.tar.gz" % self.pygccxml_version)
151 self.node.upload(pygccxml_tar,
152 os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
155 # Upload user defined ns-3 sources
156 self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
157 src_dir = os.path.join(self.node.src_dir, "ns-3")
159 super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
161 def upload_start_command(self):
162 command = self.get("command")
163 env = self.get("env")
165 # We want to make sure the ccnd is running
166 # before the experiment starts.
167 # Run the command as a bash script in background,
168 # in the host ( but wait until the command has
169 # finished to continue )
170 env = self.replace_paths(env)
171 command = self.replace_paths(command)
173 shfile = os.path.join(self.app_home, "start.sh")
174 self.node.upload_command(command,
180 self._run_in_background()
183 if self.has_changed("simulatorImplementationType"):
184 simu_type = self.get("simulatorImplementationType")
185 stype = self.create("StringValue", simu_type)
186 self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
188 if self.has_changed("checksumEnabled"):
189 check_sum = self.get("checksumEnabled")
190 btrue = self.create("BooleanValue", check_sum)
191 self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
193 if self.has_changed("schedulerType"):
194 sched_type = self.get("schedulerType")
195 stype = self.create("StringValue", sched_type)
196 self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
199 if not self.node or self.node.state < ResourceState.READY:
200 self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
202 # ccnd needs to wait until node is deployed and running
203 self.ec.schedule(reschedule_delay, self.deploy)
205 if not self.get("command"):
206 self.set("command", self._start_command)
208 if not self.get("depends"):
209 self.set("depends", self._dependencies)
211 if self.get("sources"):
212 sources = self.get("sources")
213 source = sources.split(" ")[0]
214 basename = os.path.basename(source)
215 version = ( basename.strip().replace(".tar.gz", "")
218 .replace(".zip","") )
220 self.set("ns3Version", version)
221 self.set("sources", source)
223 if not self.get("build"):
224 self.set("build", self._build)
226 if not self.get("install"):
227 self.set("install", self._install)
229 if not self.get("env"):
230 self.set("env", self._environment)
236 self._client = LinuxNS3Client(self)
238 # Wait until local socket is created
239 for i in [1, 5, 15, 30, 60]:
240 if os.path.exists(self.local_socket):
244 if not os.path.exists(self.local_socket):
245 raise RuntimeError("Problem starting socat")
252 """ Starts simulation execution
255 self.info("Starting")
257 if self.state == ResourceState.READY:
262 msg = " Failed to execute command '%s'" % command
263 self.error(msg, out, err)
264 raise RuntimeError, msg
267 """ Stops simulation execution
270 if self.state == ResourceState.STARTED:
274 def do_release(self):
275 self.info("Releasing resource")
277 tear_down = self.get("tearDown")
279 self.node.execute(tear_down)
282 self._client.shutdown()
283 LinuxApplication.do_stop(self)
285 super(LinuxApplication, self).do_release()
288 def _start_command(self):
290 command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
292 command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % self.remote_socket )
294 ns_log = self.get("nsLog")
296 command.append("-L %s" % ns_log)
298 if self.get("verbose"):
301 command = " ".join(command)
305 def _dependencies(self):
306 if self.node.use_rpm:
307 return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
308 elif self.node.use_deb:
309 return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
314 return "http://code.nsnam.org"
317 def pygccxml_version(self):
318 return "pygccxml-1.0.0"
322 # If the user defined local sources for ns-3, we uncompress the sources
323 # on the remote sources directory. Else we clone ns-3 from the official repo.
324 source = self.get("sources")
326 copy_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \
328 'ns3_version': self.get("ns3Version"),
329 'ns3_repo': self.ns3_repo,
332 if source.find(".tar.gz") > -1:
334 "tar xzf ${SRC}/ns-3/%(basename)s "
335 " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
337 'basename': os.path.basename(source),
338 'ns3_version': self.get("ns3Version"),
340 elif source.find(".tar") > -1:
342 "tar xf ${SRC}/ns-3/%(basename)s "
343 " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
345 'basename': os.path.basename(source),
346 'ns3_version': self.get("ns3Version"),
348 elif source.find(".zip") > -1:
349 basename = os.path.basename(source)
350 bare_basename = basename.replace(".zip", "") \
351 .replace(".tar", "") \
352 .replace(".tar.gz", "")
355 "unzip ${SRC}/ns-3/%(basename)s && "
356 "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s "
358 'bare_basename': basename_name,
359 'basename': basename,
360 'ns3_version': self.get("ns3Version"),
364 # Test if ns-3 is alredy installed
367 " ( test -d ${SRC}/ns-3/%(ns3_version)s ) || "
368 " ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) "
369 " ) && echo 'binaries found, nothing to do' )"
372 # If not, install ns-3 and its dependencies
377 " python -c 'import pygccxml' && "
378 " echo 'pygccxml not found' "
382 " tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
383 " cd ${SRC}/%(pygccxml_version)s && "
384 " sudo -S python setup.py install "
391 " test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
392 " echo 'binaries found, nothing to do' "
395 # If not, clone and build
397 " mkdir -p ${SRC}/pybindgen && "
398 " bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
399 " cd ${SRC}/pybindgen/%(pybindgen_version)s && "
400 " ./waf configure && "
405 # Get ns-3 source code
407 " mkdir -p ${SRC}/ns-3/%(ns3_version)s && "
412 'ns3_version': self.get("ns3Version"),
413 'pybindgen_version': self.get("pybindgenVersion"),
414 'pygccxml_version': self.pygccxml_version,
415 'copy_ns3_cmd': copy_ns3_cmd,
421 # Test if ns-3 is alredy cloned
423 " ( ( (test -d ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build ) || "
424 " (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
425 " echo 'binaries found, nothing to do' )"
429 # If not, copy ns-3 build to bin
430 " cd ${SRC}/ns-3/%(ns3_version)s && "
431 " ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s && "
433 " mkdir -p ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s && "
434 " mv ${SRC}/ns-3/%(ns3_version)s/build ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build "
437 'ns3_version': self.get("ns3Version"),
438 'pybindgen_version': self.get("pybindgenVersion"),
439 'build_mode': self.get("buildMode"),
443 def _environment(self):
445 env.append("NS3BINDINGS=${NS3BINDINGS:=${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build/bindings/python/}" % ({
446 'ns3_version': self.get("ns3Version"),
447 'build_mode': self.get("buildMode")
449 env.append("NS3LIBRARIES=${NS3LIBRARIES:=${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build/}" % ({
450 'ns3_version': self.get("ns3Version"),
451 'build_mode': self.get("buildMode")
456 def valid_connection(self, guid):