Adding compilation of DCE sources
[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, 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
34 @clsinit_copy
35 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
36     _rtype = "LinuxNS3Simulation"
37
38     @classmethod
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,
45             flags = Flags.Design)
46
47         sched_type = Attribute("schedulerType",
48                 "The object class to use as the scheduler implementation",
49                 allowed = ["ns3::MapScheduler",
50                             "ns3::ListScheduler",
51                             "ns3::HeapScheduler",
52                             "ns3::MapScheduler",
53                             "ns3::CalendarScheduler"
54                     ],
55             default = "ns3::MapScheduler",
56             type = Types.Enumerate,
57             flags = Flags.Design)
58
59         check_sum = Attribute("checksumEnabled",
60                 "A global switch to enable all checksums for all protocols",
61             default = False,
62             type = Types.Bool,
63             flags = Flags.Design)
64
65         ns_log = Attribute("nsLog",
66             "NS_LOG environment variable. " \
67                     " Will only generate output if ns-3 is compiled in DEBUG mode. ",
68             flags = Flags.Design)
69
70         verbose = Attribute("verbose",
71             "True to output debugging info from the ns3 client-server communication",
72             type = Types.Bool,
73             flags = Flags.Design)
74
75         build_mode = Attribute("buildMode",
76             "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
77             default = "optimized", 
78             allowed = ["debug", "release", "optimized"],
79             type = Types.Enumerate,
80             flags = Flags.Design)
81
82         ns3_version = Attribute("ns3Version",
83             "Version of ns-3 to install from nsam repo",
84             default = "ns-3.19", 
85             flags = Flags.Design)
86
87         enable_dce = Attribute("enableDCE",
88             "Install DCE source code",
89             default = False, 
90             type = Types.Bool,
91             flags = Flags.Design)
92
93         pybindgen_version = Attribute("pybindgenVersion",
94             "Version of pybindgen to install from bazar repo",
95             default = "834", 
96             flags = Flags.Design)
97
98         populate_routing_tables = Attribute("populateRoutingTables",
99             "Invokes  Ipv4GlobalRoutingHelper.PopulateRoutingTables() ",
100             default = False,
101             type = Types.Bool,
102             flags = Flags.Design)
103
104         cls._register_attribute(impl_type)
105         cls._register_attribute(sched_type)
106         cls._register_attribute(check_sum)
107         cls._register_attribute(ns_log)
108         cls._register_attribute(verbose)
109         cls._register_attribute(build_mode)
110         cls._register_attribute(ns3_version)
111         cls._register_attribute(pybindgen_version)
112         cls._register_attribute(populate_routing_tables)
113         cls._register_attribute(enable_dce)
114
115     def __init__(self, ec, guid):
116         LinuxApplication.__init__(self, ec, guid)
117         NS3Simulation.__init__(self)
118
119         self._client = None
120         self._home = "ns3-simu-%s" % self.guid
121         self._socket_name = "ns3-%s.sock" % os.urandom(4).encode('hex')
122
123     @property
124     def socket_name(self):
125         return self._socket_name
126
127     @property
128     def remote_socket(self):
129         return os.path.join(self.run_home, self.socket_name)
130
131     @property
132     def ns3_build_home(self):
133         return os.path.join(self.node.bin_dir, "ns-3", self.get("ns3Version"), 
134                 self.get("buildMode"), "build")
135
136     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
137         self._client.flush() 
138         return LinuxApplication.trace(self, name, attr, block, offset)
139
140     def upload_sources(self):
141         self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
142
143         # upload ns3 wrapper python script
144         ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3", 
145                 "ns3wrapper.py")
146
147         self.node.upload(ns3_wrapper,
148                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
149                 overwrite = False)
150
151         # upload ns3_server python script
152         ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
153                 "ns3server.py")
154
155         self.node.upload(ns3_server,
156                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
157                 overwrite = False)
158
159         if self.node.use_rpm:
160             # upload pygccxml sources
161             pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
162                     "%s.tar.gz" % self.pygccxml_version)
163
164             self.node.upload(pygccxml_tar,
165                     os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
166                     overwrite = False)
167
168         # Upload user defined ns-3 sources
169         self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
170         src_dir = os.path.join(self.node.src_dir, "ns-3")
171
172         super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
173
174     def upload_start_command(self):
175         command = self.get("command")
176         env = self.get("env")
177
178         # We want to make sure the ccnd is running
179         # before the experiment starts.
180         # Run the command as a bash script in background,
181         # in the host ( but wait until the command has
182         # finished to continue )
183         env = self.replace_paths(env)
184         command = self.replace_paths(command)
185
186         shfile = os.path.join(self.app_home, "start.sh")
187         self.node.upload_command(command, 
188                     shfile = shfile,
189                     env = env,
190                     overwrite = True)
191
192         # Run the ns3wrapper 
193         self._run_in_background()
194
195     def configure(self):
196         if self.has_changed("simulatorImplementationType"):
197             simu_type = self.get("simulatorImplementationType")
198             stype = self.create("StringValue", simu_type)
199             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
200
201         if self.has_changed("checksumEnabled"):
202             check_sum = self.get("checksumEnabled")
203             btrue = self.create("BooleanValue", check_sum)    
204             self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
205         
206         if self.has_changed("schedulerType"):
207             sched_type = self.get("schedulerType")
208             stype = self.create("StringValue", sched_type)
209             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
210
211     def do_deploy(self):
212         if not self.node or self.node.state < ResourceState.READY:
213             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
214             
215             # ccnd needs to wait until node is deployed and running
216             self.ec.schedule(reschedule_delay, self.deploy)
217         else:
218             if not self.get("command"):
219                 self.set("command", self._start_command)
220             
221             if not self.get("depends"):
222                 self.set("depends", self._dependencies)
223
224             if self.get("sources"):
225                 sources = self.get("sources")
226                 source = sources.split(" ")[0]
227                 basename = os.path.basename(source)
228                 version = ( basename.strip().replace(".tar.gz", "")
229                     .replace(".tar","")
230                     .replace(".gz","")
231                     .replace(".zip","") )
232
233                 self.set("ns3Version", version)
234                 self.set("sources", source)
235
236             if not self.get("build"):
237                 self.set("build", self._build)
238
239             if not self.get("install"):
240                 self.set("install", self._install)
241
242             if not self.get("env"):
243                 self.set("env", self._environment)
244
245             self.do_discover()
246             self.do_provision()
247
248             # Create client
249             self._client = LinuxNS3Client(self)
250
251             self.configure()
252             
253             self.set_ready()
254
255     def do_start(self):
256         """ Starts simulation execution
257
258         """
259         self.info("Starting")
260
261         if self.state == ResourceState.READY:
262             if self.get("populateRoutingTables") == True:
263                 self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables")
264
265             self._client.start() 
266
267             self.set_started()
268         else:
269             msg = " Failed to execute command '%s'" % command
270             self.error(msg, out, err)
271             raise RuntimeError, msg
272
273     def do_stop(self):
274         """ Stops simulation execution
275
276         """
277         if self.state == ResourceState.STARTED:
278             self._client.stop() 
279             self.set_stopped()
280
281     def do_release(self):
282         self.info("Releasing resource")
283
284         tear_down = self.get("tearDown")
285         if tear_down:
286             self.node.execute(tear_down)
287
288         self.do_stop()
289         self._client.shutdown()
290         LinuxApplication.do_stop(self)
291         
292         super(LinuxApplication, self).do_release()
293
294     @property
295     def _start_command(self):
296         command = [] 
297
298         command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
299         
300         command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % \
301                 os.path.basename(self.remote_socket) )
302
303         ns_log = self.get("nsLog")
304         if ns_log:
305             command.append("-L %s" % ns_log)
306
307         if self.get("verbose"):
308             command.append("-v")
309
310         command = " ".join(command)
311         return command
312
313     @property
314     def _dependencies(self):
315         if self.node.use_rpm:
316             return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
317         elif self.node.use_deb:
318             return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
319         return ""
320
321     @property
322     def ns3_repo(self):
323         return "http://code.nsnam.org"
324
325     @property
326     def pygccxml_version(self):
327         return "pygccxml-1.0.0"
328
329     @property
330     def _build(self):
331         # If the user defined local sources for ns-3, we uncompress the sources
332         # on the remote sources directory. Else we clone ns-3 from the official repo.
333         source = self.get("sources")
334         if not source:
335             clone_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \
336                     % {
337                         'ns3_version': self.get("ns3Version"),
338                         'ns3_repo':  self.ns3_repo,       
339                       }
340         else:
341             if source.find(".tar.gz") > -1:
342                 clone_ns3_cmd = ( 
343                             "tar xzf ${SRC}/ns-3/%(basename)s " 
344                             " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
345                             ) % {
346                                 'basename': os.path.basename(source),
347                                 'ns3_version': self.get("ns3Version"),
348                                 }
349             elif source.find(".tar") > -1:
350                 clone_ns3_cmd = ( 
351                             "tar xf ${SRC}/ns-3/%(basename)s " 
352                             " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
353                             ) % {
354                                 'basename': os.path.basename(source),
355                                 'ns3_version': self.get("ns3Version"),
356                                 }
357             elif source.find(".zip") > -1:
358                 basename = os.path.basename(source)
359                 bare_basename = basename.replace(".zip", "") \
360                         .replace(".tar", "") \
361                         .replace(".tar.gz", "")
362
363                 clone_ns3_cmd = ( 
364                             "unzip ${SRC}/ns-3/%(basename)s && "
365                             "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s "
366                             ) % {
367                                 'bare_basename': basename_name,
368                                 'basename': basename,
369                                 'ns3_version': self.get("ns3Version"),
370                                 }
371
372         clone_dce_cmd = " echo 'DCE will not be built' "
373         if self.get("enableDCE"):
374             clone_dce_cmd = (
375                         # DCE installation
376                         # Test if dce is alredy installed
377                         " ( "
378                         "  ( "
379                         "    ( test -d ${SRC}/dce/ns-3-dce ) "
380                         "   && echo 'dce binaries found, nothing to do'"
381                         "  ) "
382                         " ) "
383                         "  || " 
384                         # Get dce source code
385                         " ( "
386                         "   mkdir -p ${SRC}/dce && "
387                         "   hg clone http://code.nsnam.org/ns-3-dce ${SRC}/dce/ns-3-dce"
388                         " ) "
389                     )
390
391         return (
392                 # NS3 installation
393                 "( "
394                 " ( "
395                 # Test if ns-3 is alredy installed
396                 "  ((( test -d ${SRC}/ns-3/%(ns3_version)s ) || "
397                 "    ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'})) "
398                 "  && echo 'ns-3 binaries found, nothing to do' )"
399                 " ) "
400                 "  || " 
401                 # If not, install ns-3 and its dependencies
402                 " (   "
403                 # Install pygccxml
404                 "   (   "
405                 "     ( "
406                 "       python -c 'import pygccxml' && "
407                 "       echo 'pygccxml not found' "
408                 "     ) "
409                 "      || "
410                 "     ( "
411                 "       tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
412                 "       cd ${SRC}/%(pygccxml_version)s && "
413                 "       python setup.py build && "
414                 "       sudo -S python setup.py install "
415                 "     ) "
416                 "   ) " 
417                 # Install pybindgen
418                 "  && "
419                 "   (   "
420                 "     ( "
421                 "       test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
422                 "       echo 'binaries found, nothing to do' "
423                 "     ) "
424                 "      || "
425                 # If not, clone and build
426                 "      ( cd ${SRC} && "
427                 "        mkdir -p ${SRC}/pybindgen && "
428                 "        bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
429                 "        cd ${SRC}/pybindgen/%(pybindgen_version)s && "
430                 "        ./waf configure && "
431                 "        ./waf "
432                 "      ) "
433                 "   ) " 
434                 " && "
435                 # Get ns-3 source code
436                 "  ( "
437                 "     mkdir -p ${SRC}/ns-3/%(ns3_version)s && "
438                 "     %(clone_ns3_cmd)s "
439                 "  ) "
440                 " ) "
441                 ") "
442                 " && "
443                 "( "
444                 "   %(clone_dce_cmd)s "
445                 ") "
446              ) % { 
447                     'ns3_version': self.get("ns3Version"),
448                     'pybindgen_version': self.get("pybindgenVersion"),
449                     'pygccxml_version': self.pygccxml_version,
450                     'clone_ns3_cmd': clone_ns3_cmd,
451                     'clone_dce_cmd': clone_dce_cmd,
452                  }
453
454     @property
455     def _install(self):
456         install_dce_cmd = " echo 'DCE will not be installed' "
457         if self.get("enableDCE"):
458             install_dce_cmd = (
459                         " ( "
460                         "   ((test -d %(ns3_build_home)s/bin_dce ) && "
461                         "    echo 'dce binaries found, nothing to do' )"
462                         " ) "
463                         " ||" 
464                         " (   "
465                          # If not, copy ns-3 build to bin
466                         "  cd ${SRC}/dce/ns-3-dce && "
467                         "  ./waf configure --enable-opt --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
468                         "  --prefix=%(ns3_build_home)s --with-ns3=%(ns3_build_home)s && "
469                         "  ./waf build && "
470                         "  ./waf install "
471                         " )"
472                 ) % { 
473                     'ns3_version': self.get("ns3Version"),
474                     'pybindgen_version': self.get("pybindgenVersion"),
475                     'ns3_build_home': self.ns3_build_home,
476                     'build_mode': self.get("buildMode"),
477                     }
478
479         return (
480                  # Test if ns-3 is alredy installed
481                 "("
482                 " ( "
483                 "  ( ( (test -d %(ns3_build_home)s/lib ) || "
484                 "    (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
485                 "    echo 'binaries found, nothing to do' )"
486                 " ) "
487                 " ||" 
488                 " (   "
489                  # If not, copy ns-3 build to bin
490                 "  mkdir -p %(ns3_build_home)s && "
491                 "  cd ${SRC}/ns-3/%(ns3_version)s && "
492                 "  ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
493                 "  --prefix=%(ns3_build_home)s --includedir=%(ns3_build_home)s && "
494                 "  ./waf build && "
495                 "  ./waf install && "
496                 "  mv %(ns3_build_home)s/lib/python* %(ns3_build_home)s/lib/python "
497                 " )"
498                 ") "
499                 " && "
500                 "( "
501                 "   %(install_dce_cmd)s "
502                 ") "
503               ) % { 
504                     'ns3_version': self.get("ns3Version"),
505                     'pybindgen_version': self.get("pybindgenVersion"),
506                     'build_mode': self.get("buildMode"),
507                     'ns3_build_home': self.ns3_build_home,
508                     'install_dce_cmd': install_dce_cmd
509                  }
510
511     @property
512     def _environment(self):
513         env = []
514         env.append("PYTHONPATH=$PYTHONPATH:${NS3BINDINGS:=%(ns3_build_home)s/lib/python/site-packages}" % { 
515                     'ns3_build_home': self.ns3_build_home
516                  })
517         env.append("LD_LIBRARY_PATH=${NS3LIBRARIES:=%(ns3_build_home)s/lib/}" % { 
518                     'ns3_build_home': self.ns3_build_home
519                  })
520
521         return " ".join(env) 
522
523     def valid_connection(self, guid):
524         # TODO: Validate!
525         return True
526