Adding dummy ns3 fd-net-device test
[nepi.git] / src / nepi / resources / ns3 / ns3wrapper.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 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 import logging
21 import os
22 import sys
23 import threading
24 import time
25 import uuid
26
27 SINGLETON = "singleton::"
28 SIMULATOR_UUID = "singleton::Simulator"
29 CONFIG_UUID = "singleton::Config"
30 GLOBAL_VALUE_UUID = "singleton::GlobalValue"
31 IPV4_GLOBAL_ROUTING_HELPER_UUID = "singleton::Ipv4GlobalRoutingHelper"
32
33 def load_ns3_libraries():
34     import ctypes
35     import re
36
37     libdir = os.environ.get("NS3LIBRARIES")
38
39     # Load the ns-3 modules shared libraries
40     if libdir:
41         files = os.listdir(libdir)
42         regex = re.compile("(.*\.so)$")
43         libs = [m.group(1) for filename in files for m in [regex.search(filename)] if m]
44
45         initial_size = len(libs)
46         # Try to load the libraries in the right order by trial and error.
47         # Loop until all libraries are loaded.
48         while len(libs) > 0:
49             for lib in libs:
50                 libfile = os.path.join(libdir, lib)
51                 try:
52                     ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
53                     libs.remove(lib)
54                 except:
55                     #import traceback
56                     #err = traceback.format_exc()
57                     #print err
58                     pass
59
60             # if did not load any libraries in the last iteration break
61             # to prevent infinit loop
62             if initial_size == len(libs):
63                 raise RuntimeError("Imposible to load shared libraries %s" % str(libs))
64             initial_size = len(libs)
65
66 def load_ns3_module():
67     load_ns3_libraries()
68
69     # import the python bindings for the ns-3 modules
70     bindings = os.environ.get("NS3BINDINGS")
71     if bindings:
72         sys.path.append(bindings)
73
74     import pkgutil
75     import imp
76     import ns
77
78     # create a Python module to add all ns3 classes
79     ns3mod = imp.new_module("ns3")
80     sys.modules["ns3"] = ns3mod
81
82     for importer, modname, ispkg in pkgutil.iter_modules(ns.__path__):
83         if modname in [ "visualizer" ]:
84             continue
85
86         fullmodname = "ns.%s" % modname
87         module = __import__(fullmodname, globals(), locals(), ['*'])
88
89         for sattr in dir(module):
90             if sattr.startswith("_"):
91                 continue
92
93             attr = getattr(module, sattr)
94
95             # netanim.Config and lte.Config singleton overrides ns3::Config
96             if sattr == "Config" and modname in ['netanim', 'lte']:
97                 sattr = "%s.%s" % (modname, sattr)
98
99             setattr(ns3mod, sattr, attr)
100
101     return ns3mod
102
103 class NS3Wrapper(object):
104     def __init__(self, loglevel = logging.INFO, enable_dump = False):
105         super(NS3Wrapper, self).__init__()
106         # Thread used to run the simulation
107         self._simulation_thread = None
108         self._condition = None
109
110         # True if Simulator::Run was invoked
111         self._started = False
112
113         # holds reference to all C++ objects and variables in the simulation
114         self._objects = dict()
115
116         # Logging
117         self._logger = logging.getLogger("ns3wrapper")
118         self._logger.setLevel(loglevel)
119
120         ## NOTE that the reason to create a handler to the ns3 module,
121         # that is re-loaded each time a ns-3 wrapper is instantiated,
122         # is that else each unit test for the ns3wrapper class would need
123         # a separate file. Several ns3wrappers would be created in the 
124         # same unit test (single process), leading to inchorences in the 
125         # state of ns-3 global objects
126         #
127         # Handler to ns3 classes
128         self._ns3 = None
129
130         # Collection of allowed ns3 classes
131         self._allowed_types = None
132
133         # Object to dump instructions to reproduce and debug experiment
134         from ns3wrapper_debug import NS3WrapperDebuger
135         self._debuger = NS3WrapperDebuger(enabled = enable_dump)
136
137     @property
138     def debuger(self):
139         return self._debuger
140
141     @property
142     def ns3(self):
143         if not self._ns3:
144             # load ns-3 libraries and bindings
145             self._ns3 = load_ns3_module()
146
147         return self._ns3
148
149     @property
150     def allowed_types(self):
151         if not self._allowed_types:
152             self._allowed_types = set()
153             type_id = self.ns3.TypeId()
154             
155             tid_count = type_id.GetRegisteredN()
156             base = type_id.LookupByName("ns3::Object")
157
158             for i in xrange(tid_count):
159                 tid = type_id.GetRegistered(i)
160                 
161                 if tid.MustHideFromDocumentation() or \
162                         not tid.HasConstructor() or \
163                         not tid.IsChildOf(base): 
164                     continue
165
166                 type_name = tid.GetName()
167                 self._allowed_types.add(type_name)
168         
169         return self._allowed_types
170
171     @property
172     def logger(self):
173         return self._logger
174
175     @property
176     def is_running(self):
177         return self._started and not self.ns3.Simulator.IsFinished()
178
179     @property
180     def is_finished(self):
181         return self.ns3.Simulator.IsFinished()
182
183     def make_uuid(self):
184         return "uuid%s" % uuid.uuid4()
185
186     def get_object(self, uuid):
187         return self._objects.get(uuid)
188
189     def factory(self, type_name, **kwargs):
190         """ This method should be used to construct ns-3 objects
191         that have a TypeId and related introspection information """
192
193         if type_name not in self.allowed_types:
194             msg = "Type %s not supported" % (type_name) 
195             self.logger.error(msg)
196
197         uuid = self.make_uuid()
198         
199         ### DEBUG
200         self.logger.debug("FACTORY %s( %s )" % (type_name, str(kwargs)))
201         
202         ### DUMP
203         self.debuger.dump_factory(uuid, type_name, kwargs)
204
205         factory = self.ns3.ObjectFactory()
206         factory.SetTypeId(type_name)
207
208         for name, value in kwargs.iteritems():
209             ns3_value = self._attr_from_string_to_ns3_value(type_name, name, value)
210             factory.Set(name, ns3_value)
211
212         obj = factory.Create()
213
214         self._objects[uuid] = obj
215
216         ### DEBUG
217         self.logger.debug("RET FACTORY ( uuid %s ) %s = %s( %s )" % (
218             str(uuid), str(obj), type_name, str(kwargs)))
219  
220         return uuid
221
222     def create(self, clazzname, *args):
223         """ This method should be used to construct ns-3 objects that
224         do not have a TypeId (e.g. Values) """
225
226         if not hasattr(self.ns3, clazzname):
227             msg = "Type %s not supported" % (clazzname) 
228             self.logger.error(msg)
229
230         uuid = self.make_uuid()
231         
232         ### DEBUG
233         self.logger.debug("CREATE %s( %s )" % (clazzname, str(args)))
234     
235         ### DUMP
236         self.debuger.dump_create(uuid, clazzname, args)
237
238         clazz = getattr(self.ns3, clazzname)
239  
240         # arguments starting with 'uuid' identify ns-3 C++
241         # objects and must be replaced by the actual object
242         realargs = self.replace_args(args)
243        
244         obj = clazz(*realargs)
245         
246         self._objects[uuid] = obj
247
248         ### DEBUG
249         self.logger.debug("RET CREATE ( uuid %s ) %s = %s( %s )" % (str(uuid), 
250             str(obj), clazzname, str(args)))
251
252         return uuid
253
254     def invoke(self, uuid, operation, *args, **kwargs):
255         ### DEBUG
256         self.logger.debug("INVOKE %s -> %s( %s, %s ) " % (
257             uuid, operation, str(args), str(kwargs)))
258         ########
259
260         result = None
261         newuuid = None
262
263         if operation == "isRunning":
264             result = self.is_running
265
266         elif operation == "isFinished":
267             result = self.is_finished
268
269         elif operation == "isAppRunning":
270             result = self._is_app_running(uuid)
271
272         elif operation == "recvFD":
273             ### passFD operation binds to a different random socket 
274             ### en every execution, so the socket name that could be
275             ### dumped to the debug script using dump_invoke is
276             ### not be valid accross debug executions.
277             result = self._recv_fd(uuid, *args, **kwargs)
278
279         elif operation == "addStaticRoute":
280             result = self._add_static_route(uuid, *args)
281             
282             ### DUMP - result is static, so will be dumped as plain text
283             self.debuger.dump_invoke(result, uuid, operation, args, kwargs)
284
285         elif operation == "retrieveObject":
286             result = self._retrieve_object(uuid, *args, **kwargs)
287        
288             ### DUMP - result is static, so will be dumped as plain text
289             self.debuger.dump_invoke(result, uuid, operation, args, kwargs)
290        
291         else:
292             newuuid = self.make_uuid()
293
294             ### DUMP - result is a uuid that encoded an dynamically generated 
295             ### object
296             self.debuger.dump_invoke(newuuid, uuid, operation, args, kwargs)
297
298             if uuid.startswith(SINGLETON):
299                 obj = self._singleton(uuid)
300             else:
301                 obj = self.get_object(uuid)
302             
303             method = getattr(obj, operation)
304
305             # arguments starting with 'uuid' identify ns-3 C++
306             # objects and must be replaced by the actual object
307             realargs = self.replace_args(args)
308             realkwargs = self.replace_kwargs(kwargs)
309
310             result = method(*realargs, **realkwargs)
311
312             # If the result is an object (not a base value),
313             # then keep track of the object a return the object
314             # reference (newuuid)
315             if not (result is None or type(result) in [
316                     bool, float, long, str, int]):
317                 self._objects[newuuid] = result
318                 result = newuuid
319
320         ### DEBUG
321         self.logger.debug("RET INVOKE %s%s = %s -> %s(%s, %s) " % (
322             "(uuid %s) " % str(newuuid) if newuuid else "", str(result), uuid, 
323             operation, str(args), str(kwargs)))
324         ########
325
326         return result
327
328     def _set_attr(self, obj, name, ns3_value):
329         obj.SetAttribute(name, ns3_value)
330
331     def set(self, uuid, name, value):
332         ### DEBUG
333         self.logger.debug("SET %s %s %s" % (uuid, name, str(value)))
334     
335         ### DUMP
336         self.debuger.dump_set(uuid, name, value)
337
338         obj = self.get_object(uuid)
339         type_name = obj.GetInstanceTypeId().GetName()
340         ns3_value = self._attr_from_string_to_ns3_value(type_name, name, value)
341
342         # If the Simulation thread is not running,
343         # then there will be no thread-safety problems
344         # in changing the value of an attribute directly.
345         # However, if the simulation is running we need
346         # to set the value by scheduling an event, else
347         # we risk to corrupt the state of the
348         # simulation.
349         
350         event_executed = [False]
351
352         if self.is_running:
353             # schedule the event in the Simulator
354             self._schedule_event(self._condition, event_executed, 
355                     self._set_attr, obj, name, ns3_value)
356
357         if not event_executed[0]:
358             self._set_attr(obj, name, ns3_value)
359
360         ### DEBUG
361         self.logger.debug("RET SET %s = %s -> set(%s, %s)" % (str(value), uuid, name, 
362             str(value)))
363
364         return value
365
366     def _get_attr(self, obj, name, ns3_value):
367         obj.GetAttribute(name, ns3_value)
368
369     def get(self, uuid, name):
370         ### DEBUG
371         self.logger.debug("GET %s %s" % (uuid, name))
372         
373         ### DUMP
374         self.debuger.dump_get(uuid, name)
375
376         obj = self.get_object(uuid)
377         type_name = obj.GetInstanceTypeId().GetName()
378         ns3_value = self._create_attr_ns3_value(type_name, name)
379
380         event_executed = [False]
381
382         if self.is_running:
383             # schedule the event in the Simulator
384             self._schedule_event(self._condition, event_executed,
385                     self._get_attr, obj, name, ns3_value)
386
387         if not event_executed[0]:
388             self._get_attr(obj, name, ns3_value)
389
390         result = self._attr_from_ns3_value_to_string(type_name, name, ns3_value)
391
392         ### DEBUG
393         self.logger.debug("RET GET %s = %s -> get(%s)" % (str(result), uuid, name))
394
395         return result
396
397     def start(self):
398         ### DUMP
399         self.debuger.dump_start()
400
401         # Launch the simulator thread and Start the
402         # simulator in that thread
403         self._condition = threading.Condition()
404         self._simulator_thread = threading.Thread(
405                 target = self._simulator_run,
406                 args = [self._condition])
407         self._simulator_thread.setDaemon(True)
408         self._simulator_thread.start()
409         self._started = True
410
411         ### DEBUG
412         self.logger.debug("START")
413
414     def stop(self, time = None):
415         ### DUMP
416         self.debuger.dump_stop(time=time)
417         
418         if time is None:
419             self.ns3.Simulator.Stop()
420         else:
421             self.ns3.Simulator.Stop(self.ns3.Time(time))
422
423         ### DEBUG
424         self.logger.debug("STOP time=%s" % str(time))
425
426     def shutdown(self):
427         ### DUMP
428         self.debuger.dump_shutdown()
429
430         while not self.ns3.Simulator.IsFinished():
431             #self.logger.debug("Waiting for simulation to finish")
432             time.sleep(0.5)
433         
434         if self._simulator_thread:
435             self._simulator_thread.join()
436        
437         self.ns3.Simulator.Destroy()
438         
439         # Remove all references to ns-3 objects
440         self._objects.clear()
441         
442         sys.stdout.flush()
443         sys.stderr.flush()
444
445         ### DEBUG
446         self.logger.debug("SHUTDOWN")
447
448     def _simulator_run(self, condition):
449         # Run simulation
450         self.ns3.Simulator.Run()
451         # Signal condition to indicate simulation ended and
452         # notify waiting threads
453         condition.acquire()
454         condition.notifyAll()
455         condition.release()
456
457     def _schedule_event(self, condition, event_executed, func, *args):
458         """ Schedules event on running simulation, and wait until
459             event is executed"""
460
461         def execute_event(contextId, condition, event_executed, func, *args):
462             try:
463                 func(*args)
464                 event_executed[0] = True
465             finally:
466                 # notify condition indicating event was executed
467                 condition.acquire()
468                 condition.notifyAll()
469                 condition.release()
470
471         # contextId is defined as general context
472         contextId = long(0xffffffff)
473
474         # delay 0 means that the event is expected to execute inmediately
475         delay = self.ns3.Seconds(0)
476     
477         # Mark event as not executed
478         event_executed[0] = False
479
480         condition.acquire()
481         try:
482             self.ns3.Simulator.ScheduleWithContext(contextId, delay, execute_event, 
483                     condition, event_executed, func, *args)
484             if not self.ns3.Simulator.IsFinished():
485                 condition.wait()
486         finally:
487             condition.release()
488
489     def _create_attr_ns3_value(self, type_name, name):
490         TypeId = self.ns3.TypeId()
491         tid = TypeId.LookupByName(type_name)
492         info = TypeId.AttributeInformation()
493         if not tid.LookupAttributeByName(name, info):
494             msg = "TypeId %s has no attribute %s" % (type_name, name) 
495             self.logger.error(msg)
496
497         checker = info.checker
498         ns3_value = checker.Create() 
499         return ns3_value
500
501     def _attr_from_ns3_value_to_string(self, type_name, name, ns3_value):
502         TypeId = self.ns3.TypeId()
503         tid = TypeId.LookupByName(type_name)
504         info = TypeId.AttributeInformation()
505         if not tid.LookupAttributeByName(name, info):
506             msg = "TypeId %s has no attribute %s" % (type_name, name) 
507             self.logger.error(msg)
508
509         checker = info.checker
510         value = ns3_value.SerializeToString(checker)
511
512         type_name = checker.GetValueTypeName()
513         if type_name in ["ns3::UintegerValue", "ns3::IntegerValue"]:
514             return int(value)
515         if type_name == "ns3::DoubleValue":
516             return float(value)
517         if type_name == "ns3::BooleanValue":
518             return value == "true"
519
520         return value
521
522     def _attr_from_string_to_ns3_value(self, type_name, name, value):
523         TypeId = self.ns3.TypeId()
524         tid = TypeId.LookupByName(type_name)
525         info = TypeId.AttributeInformation()
526         if not tid.LookupAttributeByName(name, info):
527             msg = "TypeId %s has no attribute %s" % (type_name, name) 
528             self.logger.error(msg)
529
530         str_value = str(value)
531         if isinstance(value, bool):
532             str_value = str_value.lower()
533
534         checker = info.checker
535         ns3_value = checker.Create()
536         ns3_value.DeserializeFromString(str_value, checker)
537         return ns3_value
538
539     # singletons are identified as "ns3::ClassName"
540     def _singleton(self, ident):
541         if not ident.startswith(SINGLETON):
542             return None
543
544         clazzname = ident[ident.find("::")+2:]
545         if not hasattr(self.ns3, clazzname):
546             msg = "Type %s not supported" % (clazzname)
547             self.logger.error(msg)
548
549         return getattr(self.ns3, clazzname)
550
551     # replace uuids and singleton references for the real objects
552     def replace_args(self, args):
553         realargs = [self.get_object(arg) if \
554                 str(arg).startswith("uuid") else arg for arg in args]
555  
556         realargs = [self._singleton(arg) if \
557                 str(arg).startswith(SINGLETON) else arg for arg in realargs]
558
559         return realargs
560
561     # replace uuids and singleton references for the real objects
562     def replace_kwargs(self, kwargs):
563         realkwargs = dict([(k, self.get_object(v) \
564                 if str(v).startswith("uuid") else v) \
565                 for k,v in kwargs.iteritems()])
566  
567         realkwargs = dict([(k, self._singleton(v) \
568                 if str(v).startswith(SINGLETON) else v )\
569                 for k, v in realkwargs.iteritems()])
570
571         return realkwargs
572
573     def _is_app_running(self, uuid): 
574         now = self.ns3.Simulator.Now()
575         if now.IsZero():
576             return False
577
578         app = self.get_object(uuid)
579         stop_time_value = self.ns3.TimeValue()
580         app.GetAttribute("StopTime", stop_time_value)
581         stop_time = stop_time_value.Get()
582
583         start_time_value = self.ns3.TimeValue()
584         app.GetAttribute("StartTime", start_time_value)
585         start_time = start_time_value.Get()
586         
587         if now.Compare(start_time) >= 0 and now.Compare(stop_time) < 0:
588             return True
589
590         return False
591
592     def _add_static_route(self, ipv4_uuid, network, prefix, nexthop):
593         ipv4 = self.get_object(ipv4_uuid)
594
595         list_routing = ipv4.GetRoutingProtocol()
596         (static_routing, priority) = list_routing.GetRoutingProtocol(0)
597
598         ifindex = self._find_ifindex(ipv4, nexthop)
599         if ifindex == -1:
600             return False
601         
602         nexthop = self.ns3.Ipv4Address(nexthop)
603
604         if network in ["0.0.0.0", "0", None]:
605             # Default route: 0.0.0.0/0
606             static_routing.SetDefaultRoute(nexthop, ifindex)
607         else:
608             mask = self.ns3.Ipv4Mask("/%s" % prefix) 
609             network = self.ns3.Ipv4Address(network)
610
611             if prefix == 32:
612                 # Host route: x.y.z.w/32
613                 static_routing.AddHostRouteTo(network, nexthop, ifindex)
614             else:
615                 # Network route: x.y.z.w/n
616                 static_routing.AddNetworkRouteTo(network, mask, nexthop, 
617                         ifindex) 
618         return True
619
620     def _find_ifindex(self, ipv4, nexthop):
621         ifindex = -1
622
623         nexthop = self.ns3.Ipv4Address(nexthop)
624
625         # For all the interfaces registered with the ipv4 object, find
626         # the one that matches the network of the nexthop
627         nifaces = ipv4.GetNInterfaces()
628         for ifidx in xrange(nifaces):
629             iface = ipv4.GetInterface(ifidx)
630             naddress = iface.GetNAddresses()
631             for addridx in xrange(naddress):
632                 ifaddr = iface.GetAddress(addridx)
633                 ifmask = ifaddr.GetMask()
634                 
635                 ifindex = ipv4.GetInterfaceForPrefix(nexthop, ifmask)
636
637                 if ifindex == ifidx:
638                     return ifindex
639         return ifindex
640
641     def _retrieve_object(self, uuid, typeid, search = False):
642         obj = self.get_object(uuid)
643
644         type_id = self.ns3.TypeId()
645         tid = type_id.LookupByName(typeid)
646         nobj = obj.GetObject(tid)
647
648         newuuid = None
649         if search:
650             # search object
651             for ouuid, oobj in self._objects.iteritems():
652                 if nobj == oobj:
653                     newuuid = ouuid
654                     break
655         else: 
656             newuuid = self.make_uuid()
657             self._objects[newuuid] = nobj
658
659         return newuuid
660
661     def _recv_fd(self, uuid):
662         """ Waits on a local address to receive a file descriptor
663         from a local process. The file descriptor is associated
664         to a FdNetDevice to stablish communication between the
665         simulation and what ever process writes on that file descriptor
666         """
667
668         def recvfd(sock, fdnd):
669             (fd, msg) = passfd.recvfd(sock)
670             # Store a reference to the endpoint to keep the socket alive
671             fdnd.SetFileDescriptor(fd)
672         
673         import passfd
674         import socket
675         sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
676         sock.bind("")
677         address = sock.getsockname()
678         
679         fdnd = self.get_object(uuid)
680         t = threading.Thread(target=recvfd, args=(sock,fdnd))
681         t.start()
682
683         return address
684
685