Adding stopTime attribute to ns-3 simulation
[nepi.git] / src / nepi / resources / linux / ns3 / ns3simulation.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
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, ResourceFactory, 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
30
31 import os
32 import time
33 import threading
34
35 ## TODO: Clean up DCE part. All that is DCE specific should go
36 ##       in the linux ns3dceapplication.py
37
38 @clsinit_copy
39 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
40     _rtype = "LinuxNS3Simulation"
41
42     @classmethod
43     def _register_attributes(cls):
44         impl_type = Attribute("simulatorImplementationType",
45                 "The object class to use as the simulator implementation",
46             allowed = ["ns3::DefaultSimulatorImpl", "ns3::RealtimeSimulatorImpl"],
47             default = "ns3::DefaultSimulatorImpl",
48             type = Types.Enumerate,
49             flags = Flags.Design)
50
51         sched_type = Attribute("schedulerType",
52                 "The object class to use as the scheduler implementation",
53                 allowed = ["ns3::MapScheduler",
54                             "ns3::ListScheduler",
55                             "ns3::HeapScheduler",
56                             "ns3::MapScheduler",
57                             "ns3::CalendarScheduler"
58                     ],
59             default = "ns3::MapScheduler",
60             type = Types.Enumerate,
61             flags = Flags.Design)
62
63         check_sum = Attribute("checksumEnabled",
64                 "A global switch to enable all checksums for all protocols",
65             default = False,
66             type = Types.Bool,
67             flags = Flags.Design)
68
69         ns_log = Attribute("nsLog",
70             "NS_LOG environment variable. " \
71                     " Will only generate output if ns-3 is compiled in DEBUG mode. ",
72             flags = Flags.Design)
73
74         verbose = Attribute("verbose",
75             "True to output debugging info from the ns3 client-server communication",
76             type = Types.Bool,
77             flags = Flags.Design)
78
79         enable_dump = Attribute("enableDump",
80             "Enable dumping the remote executed ns-3 commands to a script "
81             "in order to later reproduce and debug the experiment",
82             type = Types.Bool,
83             default = False,
84             flags = Flags.Design)
85
86         build_mode = Attribute("buildMode",
87             "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
88             default = "optimized", 
89             allowed = ["debug", "release", "optimized"],
90             type = Types.Enumerate,
91             flags = Flags.Design)
92
93         ns3_version = Attribute("ns3Version",
94             "Version of ns-3 to install from nsam repo",
95             #default = "ns-3.19", 
96             default = "ns-3-dev", 
97             flags = Flags.Design)
98
99         pybindgen_version = Attribute("pybindgenVersion",
100             "Version of pybindgen to install from bazar repo",
101             #default = "864", 
102             default = "868", 
103             flags = Flags.Design)
104
105         populate_routing_tables = Attribute("populateRoutingTables",
106             "Invokes  Ipv4GlobalRoutingHelper.PopulateRoutingTables() ",
107             default = False,
108             type = Types.Bool,
109             flags = Flags.Design)
110
111         stoptime = Attribute("stopTime",
112             "Time at which the simulation will stop",
113             flags = Flags.Design)
114
115         cls._register_attribute(impl_type)
116         cls._register_attribute(sched_type)
117         cls._register_attribute(check_sum)
118         cls._register_attribute(ns_log)
119         cls._register_attribute(enable_dump)
120         cls._register_attribute(verbose)
121         cls._register_attribute(build_mode)
122         cls._register_attribute(ns3_version)
123         cls._register_attribute(pybindgen_version)
124         cls._register_attribute(populate_routing_tables)
125         cls._register_attribute(stoptime)
126
127     def __init__(self, ec, guid):
128         LinuxApplication.__init__(self, ec, guid)
129         NS3Simulation.__init__(self)
130
131         self._client = None
132         self._home = "ns3-simu-%s" % self.guid
133         self._socket_name = "ns3-%s.sock" % os.urandom(4).encode('hex')
134         self._dce_manager_helper_uuid = None
135         self._dce_application_helper_uuid = None
136         self._enable_dce = None
137
138     @property
139     def socket_name(self):
140         return self._socket_name
141
142     @property
143     def remote_socket(self):
144         return os.path.join(self.run_home, self.socket_name)
145
146     @property
147     def ns3_build_home(self):
148         return os.path.join(self.node.bin_dir, "ns-3", self.get("ns3Version"), 
149                 self.get("buildMode"), "build")
150
151     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
152         self._client.flush() 
153         return LinuxApplication.trace(self, name, attr, block, offset)
154
155     def upload_sources(self):
156         self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
157
158         # upload ns3 wrapper python script
159         ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3", 
160                 "ns3wrapper.py")
161
162         self.node.upload(ns3_wrapper,
163                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
164                 overwrite = False)
165
166         # upload ns3 wrapper debug python script
167         ns3_wrapper_debug = os.path.join(os.path.dirname(__file__), "..", "..", "ns3", 
168                 "ns3wrapper_debug.py")
169
170         self.node.upload(ns3_wrapper_debug,
171                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper_debug.py"),
172                 overwrite = False)
173
174         # upload ns3_server python script
175         ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
176                 "ns3server.py")
177
178         self.node.upload(ns3_server,
179                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
180                 overwrite = False)
181
182         if self.node.use_rpm:
183             # upload pygccxml sources
184             pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
185                     "%s.tar.gz" % self.pygccxml_version)
186
187             self.node.upload(pygccxml_tar,
188                     os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
189                     overwrite = False)
190
191         # Upload user defined ns-3 sources
192         self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
193         src_dir = os.path.join(self.node.src_dir, "ns-3")
194
195         super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
196     
197     def upload_extra_sources(self, sources = None, src_dir = None):
198         return super(LinuxNS3Simulation, self).upload_sources(
199                 sources = sources, 
200                 src_dir = src_dir)
201
202     def upload_start_command(self):
203         command = self.get("command")
204         env = self.get("env")
205
206         # We want to make sure the ccnd is running
207         # before the experiment starts.
208         # Run the command as a bash script in background,
209         # in the host ( but wait until the command has
210         # finished to continue )
211         env = self.replace_paths(env)
212         command = self.replace_paths(command)
213
214         shfile = os.path.join(self.app_home, "start.sh")
215         self.node.upload_command(command, 
216                     shfile = shfile,
217                     env = env,
218                     overwrite = True)
219
220         # Run the ns3wrapper 
221         self._run_in_background()
222
223     def configure(self):
224         if self.has_changed("simulatorImplementationType"):
225             simu_type = self.get("simulatorImplementationType")
226             stype = self.create("StringValue", simu_type)
227             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
228
229         if self.has_changed("checksumEnabled"):
230             check_sum = self.get("checksumEnabled")
231             btrue = self.create("BooleanValue", check_sum)    
232             self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
233         
234         if self.has_changed("schedulerType"):
235             sched_type = self.get("schedulerType")
236             stype = self.create("StringValue", sched_type)
237             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
238         
239     def do_deploy(self):
240         if not self.node or self.node.state < ResourceState.READY:
241             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
242             
243             # ccnd needs to wait until node is deployed and running
244             self.ec.schedule(reschedule_delay, self.deploy)
245         else:
246             if not self.get("command"):
247                 self.set("command", self._start_command)
248             
249             if not self.get("depends"):
250                 self.set("depends", self._dependencies)
251
252             if self.get("sources"):
253                 sources = self.get("sources")
254                 source = sources.split(" ")[0]
255                 basename = os.path.basename(source)
256                 version = ( basename.strip().replace(".tar.gz", "")
257                     .replace(".tar","")
258                     .replace(".gz","")
259                     .replace(".zip","") )
260
261                 self.set("ns3Version", version)
262                 self.set("sources", source)
263
264             if not self.get("build"):
265                 self.set("build", self._build)
266
267             if not self.get("install"):
268                 self.set("install", self._install)
269
270             if not self.get("env"):
271                 self.set("env", self._environment)
272
273             self.do_discover()
274             self.do_provision()
275
276             # Create client
277             self._client = LinuxNS3Client(self)
278
279             self.configure()
280             
281             self.set_ready()
282
283     def do_start(self):
284         """ Starts simulation execution
285
286         """
287         self.info("Starting")
288
289         if self.state == ResourceState.READY:
290             if self.get("populateRoutingTables") == True:
291                 self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables")
292
293             self._client.start()
294
295             # XXX: IS THIS REALLY NEEDED??!!!
296             # Wait until the Simulation is actually started... 
297             is_running = False
298             for i in xrange(1000):
299                 is_running = self.invoke(SIMULATOR_UUID, "isRunning")
300             
301                 if is_running:
302                     break
303                 else:
304                     time.sleep(1)
305             else:
306                 if not is_running:
307                     msg = " Simulation did not start"
308                     self.error(msg)
309                     raise RuntimeError
310
311             self.set_started()
312         else:
313             msg = " Failed to execute command '%s'" % command
314             self.error(msg, out, err)
315             raise RuntimeError, msg
316
317     def do_stop(self):
318         """ Stops simulation execution
319
320         """
321         if self.state == ResourceState.STARTED:
322             time = None
323             if self.get("stopTime"):
324                 time = self.get("stopTime")
325
326             self._client.stop(time=time) 
327             self.set_stopped()
328
329     def do_release(self):
330         self.info("Releasing resource")
331
332         tear_down = self.get("tearDown")
333         if tear_down:
334             self.node.execute(tear_down)
335
336         self.do_stop()
337         self._client.shutdown()
338         LinuxApplication.do_stop(self)
339         
340         super(LinuxApplication, self).do_release()
341
342     @property
343     def enable_dce(self):
344         if self._enable_dce is None:
345             from nepi.resources.ns3.ns3dceapplication import NS3BaseDceApplication
346             rclass = ResourceFactory.get_resource_type(
347                     NS3BaseDceApplication.get_rtype())
348             
349             self._enable_dce = False
350             for guid in self.ec.resources:
351                 rm = self.ec.get_resource(guid)
352                 if isinstance(rm, rclass):
353                     self._enable_dce = True
354                     break
355
356         return self._enable_dce
357
358     @property
359     def _start_command(self):
360         command = [] 
361
362         command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
363         
364         command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % \
365                 os.path.basename(self.remote_socket) )
366
367         ns_log = self.get("nsLog")
368         if ns_log:
369             command.append("-L '%s'" % ns_log)
370
371         if self.get("enableDump"):
372             command.append("-D")
373
374         if self.get("verbose"):
375             command.append("-v")
376
377         command = " ".join(command)
378         return command
379
380     @property
381     def _dependencies(self):
382         if self.node.use_rpm:
383             return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
384         elif self.node.use_deb:
385             return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
386         return ""
387
388     @property
389     def ns3_repo(self):
390         return "http://code.nsnam.org"
391
392     @property
393     def pygccxml_version(self):
394         return "pygccxml-1.0.0"
395
396     @property
397     def dce_repo(self):
398         return "http://code.nsnam.org/ns-3-dce"
399         #eturn "http://code.nsnam.org/epmancini/ns-3-dce"
400
401     @property
402     def _build(self):
403         # If the user defined local sources for ns-3, we uncompress the sources
404         # on the remote sources directory. Else we clone ns-3 from the official repo.
405         source = self.get("sources")
406         if not source:
407             clone_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \
408                     % {
409                         'ns3_version': self.get("ns3Version"),
410                         'ns3_repo':  self.ns3_repo,       
411                       }
412         else:
413             if source.find(".tar.gz") > -1:
414                 clone_ns3_cmd = ( 
415                             "tar xzf ${SRC}/ns-3/%(basename)s " 
416                             " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
417                             ) % {
418                                 'basename': os.path.basename(source),
419                                 'ns3_version': self.get("ns3Version"),
420                                 }
421             elif source.find(".tar") > -1:
422                 clone_ns3_cmd = ( 
423                             "tar xf ${SRC}/ns-3/%(basename)s " 
424                             " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
425                             ) % {
426                                 'basename': os.path.basename(source),
427                                 'ns3_version': self.get("ns3Version"),
428                                 }
429             elif source.find(".zip") > -1:
430                 basename = os.path.basename(source)
431                 bare_basename = basename.replace(".zip", "") \
432                         .replace(".tar", "") \
433                         .replace(".tar.gz", "")
434
435                 clone_ns3_cmd = ( 
436                             "unzip ${SRC}/ns-3/%(basename)s && "
437                             "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s "
438                             ) % {
439                                 'bare_basename': basename_name,
440                                 'basename': basename,
441                                 'ns3_version': self.get("ns3Version"),
442                                 }
443
444         clone_dce_cmd = " echo 'DCE will not be built' "
445         if self.enable_dce:
446             clone_dce_cmd = (
447                         # DCE installation
448                         # Test if dce is alredy installed
449                         " ( "
450                         "  ( "
451                         "    ( test -d ${SRC}/dce/ns-3-dce ) "
452                         "   && echo 'dce binaries found, nothing to do'"
453                         "  ) "
454                         " ) "
455                         "  || " 
456                         # Get dce source code
457                         " ( "
458                         "   mkdir -p ${SRC}/dce && "
459                         "   hg clone %(dce_repo)s ${SRC}/dce/ns-3-dce"
460                         " ) "
461                      ) % {
462                             'dce_repo': self.dce_repo
463                          }
464
465         return (
466                 # NS3 installation
467                 "( "
468                 " ( "
469                 # Test if ns-3 is alredy installed
470                 "  ((( test -d ${SRC}/ns-3/%(ns3_version)s ) || "
471                 "    ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'})) "
472                 "  && echo 'ns-3 binaries found, nothing to do' )"
473                 " ) "
474                 "  || " 
475                 # If not, install ns-3 and its dependencies
476                 " (   "
477                 # Install pygccxml
478                 "   (   "
479                 "     ( "
480                 "       python -c 'import pygccxml' && "
481                 "       echo 'pygccxml not found' "
482                 "     ) "
483                 "      || "
484                 "     ( "
485                 "       tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
486                 "       cd ${SRC}/%(pygccxml_version)s && "
487                 "       python setup.py build && "
488                 "       sudo -S python setup.py install "
489                 "     ) "
490                 "   ) " 
491                 # Install pybindgen
492                 "  && "
493                 "   (   "
494                 "     ( "
495                 "       test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
496                 "       echo 'binaries found, nothing to do' "
497                 "     ) "
498                 "      || "
499                 # If not, clone and build
500                 "      ( cd ${SRC} && "
501                 "        mkdir -p ${SRC}/pybindgen && "
502                 "        bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
503                 "        cd ${SRC}/pybindgen/%(pybindgen_version)s && "
504                 "        ./waf configure && "
505                 "        ./waf "
506                 "      ) "
507                 "   ) " 
508                 " && "
509                 # Get ns-3 source code
510                 "  ( "
511                 "     mkdir -p ${SRC}/ns-3/%(ns3_version)s && "
512                 "     %(clone_ns3_cmd)s "
513                 "  ) "
514                 " ) "
515                 ") "
516                 " && "
517                 "( "
518                 "   %(clone_dce_cmd)s "
519                 ") "
520              ) % { 
521                     'ns3_version': self.get("ns3Version"),
522                     'pybindgen_version': self.get("pybindgenVersion"),
523                     'pygccxml_version': self.pygccxml_version,
524                     'clone_ns3_cmd': clone_ns3_cmd,
525                     'clone_dce_cmd': clone_dce_cmd,
526                  }
527
528     @property
529     def _install(self):
530         install_dce_cmd = " echo 'DCE will not be installed' "
531         if self.enable_dce:
532             install_dce_cmd = (
533                         " ( "
534                         "   ((test -d %(ns3_build_home)s/bin_dce ) && "
535                         "    echo 'dce binaries found, nothing to do' )"
536                         " ) "
537                         " ||" 
538                         " (   "
539                          # If not, copy ns-3 build to bin
540                         "  cd ${SRC}/dce/ns-3-dce && "
541                         "  rm -rf ${SRC}/dce/ns-3-dce/build && "
542                         "  ./waf configure %(enable_opt)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
543                         "  --prefix=%(ns3_build_home)s --with-ns3=%(ns3_build_home)s && "
544                         "  ./waf build && "
545                         "  ./waf install && "
546                         "  mv %(ns3_build_home)s/lib*/python*/site-packages/ns/dce.so %(ns3_build_home)s/lib/python/site-packages/ns/ "
547                         " )"
548                 ) % { 
549                     'ns3_version': self.get("ns3Version"),
550                     'pybindgen_version': self.get("pybindgenVersion"),
551                     'ns3_build_home': self.ns3_build_home,
552                     'build_mode': self.get("buildMode"),
553                     'enable_opt': "--enable-opt" if  self.get("buildMode") == "optimized" else ""
554                     }
555
556         return (
557                  # Test if ns-3 is alredy installed
558                 "("
559                 " ( "
560                 "  ( ( (test -d %(ns3_build_home)s/lib ) || "
561                 "    (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
562                 "    echo 'binaries found, nothing to do' )"
563                 " ) "
564                 " ||" 
565                 " (   "
566                  # If not, copy ns-3 build to bin
567                 "  mkdir -p %(ns3_build_home)s && "
568                 "  cd ${SRC}/ns-3/%(ns3_version)s && "
569                 "  rm -rf ${SRC}/ns-3/%(ns3_version)s/build && "
570                 "  ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
571                 "  --prefix=%(ns3_build_home)s && "
572                 "  ./waf build && "
573                 "  ./waf install && "
574                 "  mv %(ns3_build_home)s/lib*/python* %(ns3_build_home)s/lib/python "
575                 " )"
576                 ") "
577                 " && "
578                 "( "
579                 "   %(install_dce_cmd)s "
580                 ") "
581               ) % { 
582                     'ns3_version': self.get("ns3Version"),
583                     'pybindgen_version': self.get("pybindgenVersion"),
584                     'build_mode': self.get("buildMode"),
585                     'ns3_build_home': self.ns3_build_home,
586                     'install_dce_cmd': install_dce_cmd
587                  }
588
589     @property
590     def _environment(self):
591         env = []
592         env.append("PYTHONPATH=$PYTHONPATH:${NS3BINDINGS:=%(ns3_build_home)s/lib/python/site-packages}" % { 
593                     'ns3_build_home': self.ns3_build_home
594                  })
595         # If NS3LIBRARIES is defined and not empty, assign its value, 
596         # if not assign ns3_build_home/lib/ to NS3LIBRARIES and LD_LIBARY_PATH
597         env.append("LD_LIBRARY_PATH=${NS3LIBRARIES:=%(ns3_build_home)s/lib}" % { 
598                     'ns3_build_home': self.ns3_build_home
599                  })
600         env.append("DCE_PATH=$NS3LIBRARIES/../bin_dce")
601         env.append("DCE_ROOT=$NS3LIBRARIES/..")
602
603         return " ".join(env) 
604
605     def replace_paths(self, command):
606         """
607         Replace all special path tags with shell-escaped actual paths.
608         """
609         return ( command
610             .replace("${USR}", self.node.usr_dir)
611             .replace("${LIB}", self.node.lib_dir)
612             .replace("${BIN}", self.node.bin_dir)
613             .replace("${SRC}", self.node.src_dir)
614             .replace("${SHARE}", self.node.share_dir)
615             .replace("${EXP}", self.node.exp_dir)
616             .replace("${EXP_HOME}", self.node.exp_home)
617             .replace("${APP_HOME}", self.app_home)
618             .replace("${RUN_HOME}", self.run_home)
619             .replace("${NODE_HOME}", self.node.node_home)
620             .replace("${HOME}", self.node.home_dir)
621             # If NS3LIBRARIES is defined and not empty, use that value, 
622             # if not use ns3_build_home/lib/
623             .replace("${BIN_DCE}", "${NS3LIBRARIES-%s/lib}/../bin_dce" % \
624                     self.ns3_build_home)
625             )
626
627     def valid_connection(self, guid):
628         # TODO: Validate!
629         return True
630