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 ",
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 pybindgen_version = Attribute("pybindgenVersion",
88 "Version of pybindgen to install from bazar repo",
92 populate_routing_tables = Attribute("populateRoutingTables",
93 "Invokes Ipv4GlobalRoutingHelper.PopulateRoutingTables() ",
98 cls._register_attribute(impl_type)
99 cls._register_attribute(sched_type)
100 cls._register_attribute(check_sum)
101 cls._register_attribute(ns_log)
102 cls._register_attribute(verbose)
103 cls._register_attribute(build_mode)
104 cls._register_attribute(ns3_version)
105 cls._register_attribute(pybindgen_version)
106 cls._register_attribute(populate_routing_tables)
108 def __init__(self, ec, guid):
109 LinuxApplication.__init__(self, ec, guid)
110 NS3Simulation.__init__(self)
113 self._home = "ns3-simu-%s" % self.guid
114 self._socket_name = "ns3simu-%s" % os.urandom(8).encode('hex')
117 def socket_name(self):
118 return self._socket_name
121 def remote_socket(self):
122 return os.path.join(self.run_home, self.socket_name)
124 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
126 return LinuxApplication.trace(self, name, attr, block, offset)
128 def upload_sources(self):
129 self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
131 # upload ns3 wrapper python script
132 ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
135 self.node.upload(ns3_wrapper,
136 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
139 # upload ns3_server python script
140 ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
143 self.node.upload(ns3_server,
144 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
147 if self.node.use_rpm:
148 # upload pygccxml sources
149 pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
150 "%s.tar.gz" % self.pygccxml_version)
152 self.node.upload(pygccxml_tar,
153 os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
156 # Upload user defined ns-3 sources
157 self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
158 src_dir = os.path.join(self.node.src_dir, "ns-3")
160 super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
162 def upload_start_command(self):
163 command = self.get("command")
164 env = self.get("env")
166 # We want to make sure the ccnd is running
167 # before the experiment starts.
168 # Run the command as a bash script in background,
169 # in the host ( but wait until the command has
170 # finished to continue )
171 env = self.replace_paths(env)
172 command = self.replace_paths(command)
174 shfile = os.path.join(self.app_home, "start.sh")
175 self.node.upload_command(command,
181 self._run_in_background()
184 if self.has_changed("simulatorImplementationType"):
185 simu_type = self.get("simulatorImplementationType")
186 stype = self.create("StringValue", simu_type)
187 self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
189 if self.has_changed("checksumEnabled"):
190 check_sum = self.get("checksumEnabled")
191 btrue = self.create("BooleanValue", check_sum)
192 self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
194 if self.has_changed("schedulerType"):
195 sched_type = self.get("schedulerType")
196 stype = self.create("StringValue", sched_type)
197 self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
200 if not self.node or self.node.state < ResourceState.READY:
201 self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
203 # ccnd needs to wait until node is deployed and running
204 self.ec.schedule(reschedule_delay, self.deploy)
206 if not self.get("command"):
207 self.set("command", self._start_command)
209 if not self.get("depends"):
210 self.set("depends", self._dependencies)
212 if self.get("sources"):
213 sources = self.get("sources")
214 source = sources.split(" ")[0]
215 basename = os.path.basename(source)
216 version = ( basename.strip().replace(".tar.gz", "")
219 .replace(".zip","") )
221 self.set("ns3Version", version)
222 self.set("sources", source)
224 if not self.get("build"):
225 self.set("build", self._build)
227 if not self.get("install"):
228 self.set("install", self._install)
230 if not self.get("env"):
231 self.set("env", self._environment)
237 self._client = LinuxNS3Client(self)
244 """ Starts simulation execution
247 self.info("Starting")
249 if self.state == ResourceState.READY:
250 if self.get("populateRoutingTables") == True:
251 self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables")
257 msg = " Failed to execute command '%s'" % command
258 self.error(msg, out, err)
259 raise RuntimeError, msg
262 """ Stops simulation execution
265 if self.state == ResourceState.STARTED:
269 def do_release(self):
270 self.info("Releasing resource")
272 tear_down = self.get("tearDown")
274 self.node.execute(tear_down)
277 self._client.shutdown()
278 LinuxApplication.do_stop(self)
280 super(LinuxApplication, self).do_release()
283 def _start_command(self):
285 command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
287 command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % self.remote_socket )
289 ns_log = self.get("nsLog")
291 command.append("-L %s" % ns_log)
293 if self.get("verbose"):
296 command = " ".join(command)
300 def _dependencies(self):
301 if self.node.use_rpm:
302 return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
303 elif self.node.use_deb:
304 return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
309 return "http://code.nsnam.org"
312 def pygccxml_version(self):
313 return "pygccxml-1.0.0"
317 # If the user defined local sources for ns-3, we uncompress the sources
318 # on the remote sources directory. Else we clone ns-3 from the official repo.
319 source = self.get("sources")
321 copy_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \
323 'ns3_version': self.get("ns3Version"),
324 'ns3_repo': self.ns3_repo,
327 if source.find(".tar.gz") > -1:
329 "tar xzf ${SRC}/ns-3/%(basename)s "
330 " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
332 'basename': os.path.basename(source),
333 'ns3_version': self.get("ns3Version"),
335 elif source.find(".tar") > -1:
337 "tar xf ${SRC}/ns-3/%(basename)s "
338 " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
340 'basename': os.path.basename(source),
341 'ns3_version': self.get("ns3Version"),
343 elif source.find(".zip") > -1:
344 basename = os.path.basename(source)
345 bare_basename = basename.replace(".zip", "") \
346 .replace(".tar", "") \
347 .replace(".tar.gz", "")
350 "unzip ${SRC}/ns-3/%(basename)s && "
351 "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s "
353 'bare_basename': basename_name,
354 'basename': basename,
355 'ns3_version': self.get("ns3Version"),
359 # Test if ns-3 is alredy installed
362 " ( test -d ${SRC}/ns-3/%(ns3_version)s ) || "
363 " ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) "
364 " ) && echo 'binaries found, nothing to do' )"
367 # If not, install ns-3 and its dependencies
372 " python -c 'import pygccxml' && "
373 " echo 'pygccxml not found' "
377 " tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
378 " cd ${SRC}/%(pygccxml_version)s && "
379 " sudo -S python setup.py install "
386 " test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
387 " echo 'binaries found, nothing to do' "
390 # If not, clone and build
392 " mkdir -p ${SRC}/pybindgen && "
393 " bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
394 " cd ${SRC}/pybindgen/%(pybindgen_version)s && "
395 " ./waf configure && "
400 # Get ns-3 source code
402 " mkdir -p ${SRC}/ns-3/%(ns3_version)s && "
407 'ns3_version': self.get("ns3Version"),
408 'pybindgen_version': self.get("pybindgenVersion"),
409 'pygccxml_version': self.pygccxml_version,
410 'copy_ns3_cmd': copy_ns3_cmd,
416 # Test if ns-3 is alredy cloned
418 " ( ( (test -d ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build ) || "
419 " (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
420 " echo 'binaries found, nothing to do' )"
424 # If not, copy ns-3 build to bin
425 " cd ${SRC}/ns-3/%(ns3_version)s && "
426 " ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s && "
428 " mkdir -p ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s && "
429 " mv ${SRC}/ns-3/%(ns3_version)s/build ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build "
432 'ns3_version': self.get("ns3Version"),
433 'pybindgen_version': self.get("pybindgenVersion"),
434 'build_mode': self.get("buildMode"),
438 def _environment(self):
440 env.append("NS3BINDINGS=${NS3BINDINGS:=${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build/bindings/python/}" % ({
441 'ns3_version': self.get("ns3Version"),
442 'build_mode': self.get("buildMode")
444 env.append("NS3LIBRARIES=${NS3LIBRARIES:=${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build/}" % ({
445 'ns3_version': self.get("ns3Version"),
446 'build_mode': self.get("buildMode")
451 def valid_connection(self, guid):