Simple cross ping experiment between linux and ns3
[nepi.git] / src / nepi / resources / linux / tap.py
index 6b3dce6..636e471 100644 (file)
@@ -18,8 +18,7 @@
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
 from nepi.execution.attribute import Attribute, Flags, Types
-from nepi.execution.resource import clsinit_copy, ResourceState, \
-        reschedule_delay
+from nepi.execution.resource import clsinit_copy, ResourceState
 from nepi.resources.linux.application import LinuxApplication
 from nepi.resources.linux.node import LinuxNode
 from nepi.util.timefuncs import tnow, tdiffsec
@@ -31,19 +30,22 @@ PYTHON_VSYS_VERSION = "1.0"
 
 @clsinit_copy
 class LinuxTap(LinuxApplication):
-    _rtype = "LinuxTap"
+    _rtype = "linux::Tap"
     _help = "Creates a TAP device on a Linux host"
     _backend = "linux"
 
+    IFF_TUN = 0x0001
+    IFF_TAP = 0x0002
+
     @classmethod
     def _register_attributes(cls):
-        ip4 = Attribute("ip4", "IPv4 Address",
+        endpoint_ip = Attribute("endpoint_ip", "IPv4 Address",
               flags = Flags.Design)
 
         mac = Attribute("mac", "MAC Address",
                 flags = Flags.Design)
 
-        prefix4 = Attribute("prefix4", "IPv4 network prefix",
+        endpoint_prefix = Attribute("endpoint_prefix", "IPv4 network prefix",
                 type = Types.Integer,
                 flags = Flags.Design)
 
@@ -83,9 +85,9 @@ class LinuxTap(LinuxApplication):
                 "Bash script to be executed before releasing the resource",
                 flags = Flags.Design)
 
-        cls._register_attribute(ip4)
+        cls._register_attribute(endpoint_ip)
         cls._register_attribute(mac)
-        cls._register_attribute(prefix4)
+        cls._register_attribute(endpoint_prefix)
         cls._register_attribute(mtu)
         cls._register_attribute(devname)
         cls._register_attribute(up)
@@ -106,7 +108,7 @@ class LinuxTap(LinuxApplication):
     def node(self):
         node = self.get_connected(LinuxNode.get_rtype())
         if node: return node[0]
-        raise RuntimeError, "TAP/TUN devices must be connected to Node"
+        raise RuntimeError, "linux::TAP/TUN devices must be connected to a linux::Node"
 
     @property
     def gre_enabled(self):
@@ -176,7 +178,7 @@ class LinuxTap(LinuxApplication):
 
     def do_deploy(self):
         if not self.node or self.node.state < ResourceState.PROVISIONED:
-            self.ec.schedule(reschedule_delay, self.deploy)
+            self.ec.schedule(self.reschedule_delay, self.deploy)
         else:
             if not self.get("deviceName"):
                 self.set("deviceName", "%s%d" % (self.vif_prefix, self.guid)) 
@@ -223,7 +225,7 @@ class LinuxTap(LinuxApplication):
                 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
 
             if self.get("deviceName"):
-                (out, err), proc = self.node.execute("ifconfig")
+                (out, err), proc = self.node.execute("ip a")
 
                 if out.strip().find(self.get("deviceName")) == -1: 
                     # tap is not running is not running (socket not found)
@@ -241,7 +243,7 @@ class LinuxTap(LinuxApplication):
 
         for rm in rms:
             if rm.state < ResourceState.STOPPED:
-                self.ec.schedule(reschedule_delay, self.release)
+                self.ec.schedule(self.reschedule_delay, self.release)
                 return 
 
         super(LinuxTap, self).do_release()
@@ -283,7 +285,12 @@ class LinuxTap(LinuxApplication):
         
         return True
 
-    ## XXX: NOT REALLY WORKING YET!
+    def initiate_udp_connection(self, remote_endpoint, connection_app_home, 
+            connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
+        port = self.udp_connect(remote_endpoint, connection_app_home, 
+            connection_run_home, cipher, cipher_key, bwlimit, txqueuelen)
+        return port
+
     def udp_connect(self, remote_endpoint, connection_app_home, 
             connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
         udp_connect_command = self._udp_connect_command(
@@ -291,14 +298,14 @@ class LinuxTap(LinuxApplication):
                 cipher, cipher_key, bwlimit, txqueuelen)
 
         # upload command to connect.sh script
-        shfile = os.path.join(connection_app_home, "udp-connect.sh")
+        shfile = os.path.join(self.app_home, "udp-connect.sh")
         self.node.upload_command(udp_connect_command,
                 shfile = shfile,
                 overwrite = False)
 
         # invoke connect script
         cmd = "bash %s" % shfile
-        (out, err), proc = self.node.run(cmd, connection_run_home) 
+        (out, err), proc = self.node.run(cmd, self.run_home) 
              
         # check if execution errors occurred
         msg = "Failed to connect endpoints "
@@ -308,25 +315,27 @@ class LinuxTap(LinuxApplication):
             raise RuntimeError, msg
     
         # Wait for pid file to be generated
-        pid, ppid = self.node.wait_pid(connection_run_home)
+        self._pid, self._ppid = self.node.wait_pid(self.run_home)
         
         # If the process is not running, check for error information
         # on the remote machine
-        if not pid or not ppid:
-            (out, err), proc = self.node.check_errors(connection_run_home)
+        if not self._pid or not self._ppid:
+            (out, err), proc = self.node.check_errors(self.run_home)
             # Out is what was written in the stderr file
             if err:
                 msg = " Failed to start command '%s' " % command
                 self.error(msg, out, err)
                 raise RuntimeError, msg
 
-        return pid, ppid
+        port = self.wait_local_port()
+
+        return port
 
     def _udp_connect_command(self, remote_endpoint, connection_run_home, 
             cipher, cipher_key, bwlimit, txqueuelen):
 
         # Set the remote endpoint
-        self.set("pointopoint", remote_endpoint.get("ip4"))
+        self.set("pointopoint", remote_endpoint.get("endpoint_ip"))
         
         # Planetlab TAPs always use PI headers
         from nepi.resources.planetlab.tap import PlanetlabTap
@@ -335,13 +344,13 @@ class LinuxTap(LinuxApplication):
 
         remote_ip = remote_endpoint.node.get("ip")
 
-        local_port_file = os.path.join(connection_run_home, 
+        local_port_file = os.path.join(self.run_home, 
                 "local_port")
 
-        remote_port_file = os.path.join(connection_run_home, 
+        remote_port_file = os.path.join(self.run_home, 
                 "remote_port")
 
-        ret_file = os.path.join(connection_run_home, 
+        ret_file = os.path.join(self.run_home, 
                 "ret_file")
 
         # Generate UDP connect command
@@ -382,7 +391,7 @@ class LinuxTap(LinuxApplication):
 
     def _gre_connect_command(self, remote_endpoint, connection_run_home): 
         # Set the remote endpoint
-        self.set("pointopoint", remote_endpoint.get("ip4"))
+        self.set("pointopoint", remote_endpoint.get("endpoint_ip"))
         self.set("greRemote", remote_endpoint.node.get("ip"))
 
         # Generate GRE connect command
@@ -397,6 +406,65 @@ class LinuxTap(LinuxApplication):
 
         return command
 
+    def establish_udp_connection(self, remote_endpoint, port):
+        # upload remote port number to file
+        rem_port = "%s\n" % port
+        self.node.upload(rem_port,
+                os.path.join(self.run_home, "remote_port"),
+                text = True, 
+                overwrite = False)
+
+    def verify_connection(self):
+        self.wait_result()
+
+    def terminate_connection(self):
+        if  self._pid and self._ppid:
+            (out, err), proc = self.node.kill(self._pid, self._ppid, 
+                    sudo = True) 
+
+            # check if execution errors occurred
+            if proc.poll() and err:
+                msg = " Failed to Kill the Tap"
+                self.error(msg, out, err)
+                raise RuntimeError, msg
+
+    def check_status(self):
+        return self.node.status(self._pid, self._ppid)
+
+    def wait_local_port(self):
+        """ Waits until the local_port file for the endpoint is generated, 
+        and returns the port number 
+        
+        """
+        return self.wait_file("local_port")
+
+    def wait_result(self):
+        """ Waits until the return code file for the endpoint is generated 
+        
+        """ 
+        return self.wait_file("ret_file")
+    def wait_file(self, filename):
+        """ Waits until file on endpoint is generated """
+        result = None
+        delay = 1.0
+
+        for i in xrange(20):
+            (out, err), proc = self.node.check_output(
+                    self.run_home, filename)
+            if out:
+                result = out.strip()
+                break
+            else:
+                time.sleep(delay)
+                delay = delay * 1.5
+        else:
+            msg = "Couldn't retrieve %s" % filename
+            self.error(msg, out, err)
+            raise RuntimeError, msg
+
+        return result
+
     @property
     def _start_command(self):
         command = []
@@ -411,8 +479,8 @@ class LinuxTap(LinuxApplication):
                 "pi" if self.get("pi") else ""))
             start_command.append("sudo -S ip link set %s up" % self.get("deviceName"))
             start_command.append("sudo -S ip addr add %s/%d dev %s" % (
-                self.get("ip4"),
-                self.get("prefix4"),
+                self.get("endpoint_ip"),
+                self.get("endpoint_prefix"),
                 self.get("deviceName"),
                 ))
 
@@ -445,10 +513,10 @@ class LinuxTap(LinuxApplication):
                 self.get("greKey")
             ))
         command.append("sudo -S ip addr add %s/%d peer %s/%d dev %s" % (
-                self.get("ip4"),
-                self.get("prefix4"),
+                self.get("endpoint_ip"),
+                self.get("endpoint_prefix"),
                 self.get("pointopoint"),
-                self.get("prefix4"),
+                self.get("endpoint_prefix"),
                 self.get("deviceName"),
                 ))
         command.append("sudo -S ip link set %s up " % self.get("deviceName"))
@@ -459,6 +527,10 @@ class LinuxTap(LinuxApplication):
     def vif_type(self):
         return "IFF_TAP"
 
+    @property
+    def vif_type_flag(self):
+        return LinuxTap.IFF_TAP
     @property
     def vif_prefix(self):
         return "tap"