FdNetDevice working with PlanetLab TAPs
[nepi.git] / src / nepi / resources / planetlab / ns3 / tun_tap_fd_link.py
index c658fb8..041e6b8 100644 (file)
 #
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
-from nepi.execution.attribute import Attribute, Flags, Types
-from nepi.execution.resource import ResourceManager, ResourceState, \
-        clsinit_copy
+from nepi.execution.resource import ResourceState, clsinit_copy
+from nepi.resources.linux.ns3.tun_tap_fd_link import LinuxTunTapFdLink
 
+import base64
+import fcntl
 import os
 import socket
 import struct
-import fcntl
 
 @clsinit_copy
-class PlanetlabTunTapFdLink(ResourceManager):
-    """ Interconnects a TAP or TUN Linux device to a FdNetDevice
+class PlanetlabTunTapFdLink(LinuxTunTapFdLink):
+    """ Interconnects a TAP or TUN PlanetLab device to a FdNetDevice
     """
     _rtype = "planetlab::ns3::TunTapFdLink"
 
     def __init__(self, ec, guid):
         super(PlanetlabTunTapFdLink, self).__init__(ec, guid)
-        self._tap = None
-        self._fdnetdevice = None
-        self._fd_sock_address = None
 
     @property
     def fdnetdevice(self):
@@ -44,11 +41,19 @@ class PlanetlabTunTapFdLink(ResourceManager):
             from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
             devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
             if not devices or len(devices) != 1: 
-                msg = "TapFdLink must be connected to exactly one FdNetDevices"
+                msg = "planetlab::ns3::TunTapFdLink must be connected to exactly one FdNetDevice"
                 self.error(msg)
                 raise RuntimeError, msg
 
             self._fdnetdevice = devices[0]
+
+            # Set PI headers on
+            self._fdnetdevice.set("EncapsulationMode", "DixPi")
+        
+            simu = self._fdnetdevice.simulation
+            from nepi.resources.planetlab.node import PlanetlabNode
+            nodes = simu.get_connected(PlanetlabNode.get_rtype())
+            self._fd_node = nodes[0]
         
         return self._fdnetdevice
 
@@ -59,7 +64,7 @@ class PlanetlabTunTapFdLink(ResourceManager):
             devices = self.get_connected(PlanetlabTap.get_rtype())
 
             if not devices or len(devices) != 1: 
-                msg = "TapFdLink must be connected to exactly one PlanetlabTap"
+                msg = "planetlab::ns3::TunTapFdLink must be connected to exactly one PlanetlabTap"
                 self.error(msg)
                 raise RuntimeError, msg
 
@@ -67,19 +72,13 @@ class PlanetlabTunTapFdLink(ResourceManager):
         
         return self._tap
 
-    @property
-    def fd_sock_address(self):
-        return self._fd_sock_address
-
-    @property
-    def node(self):
-        return self.tap.node
-
     def upload_sources(self):
         scripts = []
 
         # vif-passfd python script
-        pl_vif_passfd = os.path.join(os.path.dirname(__file__), "scripts",
+        pl_vif_passfd = os.path.join(os.path.dirname(__file__), 
+                "..",
+                "scripts",
                 "pl-vif-passfd.py")
 
         scripts.append(pl_vif_passfd)
@@ -91,61 +90,16 @@ class PlanetlabTunTapFdLink(ResourceManager):
                 os.path.join(self.node.src_dir),
                 overwrite = False)
 
-    def upload_start_command(self):
-        if self.tap.node.get("hostname") != \
-                self.fdnetdevice.node.get("hostname"):
-            msg = "Tap and FdNetDevice are not in the same host"
-            self.error(msg)
-            raise RuntimeError, msg
-
-        self._fd_sock_address = self.fdnetdevice.recv_fd()
-        self.set("command", self._start_command)
-
-        command = self.get("command")
-        env = self.get("env")
-
-        # We want to make sure the ccnd is running
-        # before the experiment starts.
-        # Run the command as a bash script in background,
-        # in the host ( but wait until the command has
-        # finished to continue )
-        env = self.replace_paths(env)
-        command = self.replace_paths(command)
-
-        shfile = os.path.join(self.app_home, "start.sh")
-        self.node.run_and_wait(command, self.run_home,
-                shfile = shfile,
-                overwrite = True)
-
-    def do_deploy(self):
-        if self.tap.state < ResourceState.READY or \
-                self.fdnetdevice.state < ResourceState.READY:
-            self.ec.schedule(self.reschedule_delay, self.deploy)
-        else:
-            self.do_discover()
-            self.do_provision()
-
-            super(PlanetlabTunTapFdLink, self).do_deploy()
-
-    def do_start(self):
-        if self.state == ResourceState.READY:
-            command = self.get("command")
-            self.info("Starting command '%s'" % command)
-
-            self.set_started()
-        else:
-            msg = " Failed to execute command '%s'" % command
-            self.error(msg, out, err)
-            raise RuntimeError, msg
-
     @property
     def _start_command(self):
+        address = base64.b64encode(self.send_address)
+
         command = []
         # Use pl-vif-passfd.py to send fd from TAP to FdNetDevice
         command.append("sudo -S")
         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
-        command.append("python ${SRC}/pl-vif-passfd")
-        command.append("-a %s" % self.fd_sock_address)
+        command.append("python ${SRC}/pl-vif-passfd.py")
+        command.append("-a %s" % address)
         command.append("-S %s " % self.tap.sock_name)
 
         command = " ".join(command)