Serious refactoring of TUN/TAP and tunnel code. Linux udp/gre tunnels not yet functional
[nepi.git] / src / nepi / resources / planetlab / tap.py
index 7d8cd3e..61ff3d6 100644 (file)
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
 from nepi.execution.attribute import Attribute, Flags, Types
-from nepi.execution.resource import ResourceManager, clsinit_copy, ResourceState, \
-        reschedule_delay
-from nepi.resources.linux.application import LinuxApplication
+from nepi.execution.resource import clsinit_copy, ResourceState 
+from nepi.resources.linux.tap import LinuxTap
 from nepi.resources.planetlab.node import PlanetlabNode
 from nepi.util.timefuncs import tnow, tdiffsec
 
 import os
 import time
 
-# TODO: - routes!!!
-#       - Make base clase 'virtual device' and redefine vif_type
-#       - write the name of the device (if_name) to a file and allow the 
-#           RM to read it and set the 'deviceName' attribute
-#       - Instead of doing an infinite loop, open a port for communication allowing
-#           to pass the fd to another process
-
 PYTHON_VSYS_VERSION = "1.0"
 
 @clsinit_copy
-class PlanetlabTap(LinuxApplication):
-    _rtype = "PlanetlabTap"
+class PlanetlabTap(LinuxTap):
+    _rtype = "planetlab::Tap"
+    _help = "Creates a TAP device on a PlanetLab host"
+    _platform = "planetlab"
 
     @classmethod
     def _register_attributes(cls):
-        ip4 = Attribute("ip4", "IPv4 Address",
-              flags = Flags.ExecReadOnly)
-
-        mac = Attribute("mac", "MAC Address",
-                flags = Flags.ExecReadOnly)
-
-        prefix4 = Attribute("prefix4", "IPv4 network prefix",
-                flags = Flags.ExecReadOnly)
-
-        mtu = Attribute("mtu", "Maximum transmition unit for device",
-                type = Types.Integer)
-
-        devname = Attribute("deviceName", 
-                "Name of the network interface (e.g. eth0, wlan0, etc)",
-                flags = Flags.ReadOnly)
-
-        up = Attribute("up", "Link up", type = Types.Bool)
+        snat = Attribute("snat", "Set SNAT=1", 
+                type = Types.Bool,
+                flags = Flags.Design)
         
-        snat = Attribute("snat", "Set SNAT=1", type = Types.Bool,
-                flags = Flags.ReadOnly)
-        
-        pointopoint = Attribute("pointopoint", "Peer IP address", 
-                flags = Flags.ReadOnly)
-
-        tear_down = Attribute("tearDown", "Bash script to be executed before " + \
-                "releasing the resource",
-                flags = Flags.ExecReadOnly)
-
-        cls._register_attribute(ip4)
-        cls._register_attribute(mac)
-        cls._register_attribute(prefix4)
-        cls._register_attribute(mtu)
-        cls._register_attribute(devname)
-        cls._register_attribute(up)
         cls._register_attribute(snat)
-        cls._register_attribute(pointopoint)
-        cls._register_attribute(tear_down)
 
     def __init__(self, ec, guid):
         super(PlanetlabTap, self).__init__(ec, guid)
         self._home = "tap-%s" % self.guid
+        self._gre_enabled = False
 
     @property
     def node(self):
-        node = self.get_connected(PlanetlabNode.rtype())
+        node = self.get_connected(PlanetlabNode.get_rtype())
         if node: return node[0]
-        return None
+        raise RuntimeError, "TAP/TUN devices must be connected to Node"
 
     def upload_sources(self):
-        depends = "mercurial make gcc"
-        self.set("depends", depends)
+        scripts = []
 
-        install = ( " ( "
-                    "   python -c 'import vsys, os;  vsys.__version__ == \"%(version)s\" or os._exit(1)' "
-                    " ) "
-                    " ||"
-                    " ( "
-                    "   cd ${SRC} ; "
-                    "   hg clone http://nepi.inria.fr/code/python-vsys ; "
-                    "   cd python-vsys ; "
-                    "   make all ; "
-                    "   sudo -S make install "
-                    " )" ) % ({
-                        "version": PYTHON_VSYS_VERSION
-                        })
+        # vif-creation python script
+        pl_vif_create = os.path.join(os.path.dirname(__file__), "scripts",
+                "pl-vif-create.py")
 
-        self.set("install", install)
+        scripts.append(pl_vif_create)
+        
+        # vif-up python script
+        pl_vif_up = os.path.join(os.path.dirname(__file__), "scripts",
+                "pl-vif-up.py")
+        
+        scripts.append(pl_vif_up)
 
-    def upload_start_command(self):
-        # upload tap-creation python script
-        start_script = self.replace_paths(self._start_script)
-        self.node.upload(start_script,
-                os.path.join(self.app_home, "tap_create.py"),
-                text = True, 
-                overwrite = False)
+        # vif-down python script
+        pl_vif_down = os.path.join(os.path.dirname(__file__), "scripts",
+                "pl-vif-down.py")
+        
+        scripts.append(pl_vif_down)
+
+        # udp-connect python script
+        udp_connect = os.path.join(os.path.dirname(__file__), 
+                "..",
+                "linux",
+                "scripts",
+                "linux-udp-connect.py")
+        
+        scripts.append(udp_connect)
 
-        # upload start.sh
-        start_command = self.replace_paths(self._start_command)
+        # tunnel creation python script
+        tunchannel = os.path.join(os.path.dirname(__file__), 
+                "..", 
+                "linux",
+                "scripts", 
+                "tunchannel.py")
 
-        self.info("Uploading command '%s'" % start_command)
-        
-        self.set("command", start_command)
-        self.node.upload(start_command,
-                os.path.join(self.app_home, "start.sh"),
-                text = True, 
+        scripts.append(tunchannel)
+
+        # Upload scripts
+        scripts = ";".join(scripts)
+
+        self.node.upload(scripts,
+                os.path.join(self.node.src_dir),
                 overwrite = False)
 
-        # We want to make sure the device is up and 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 )
-        self._run_in_background()
-        
-        # Retrive if_name
-        if_name = self.wait_if_name()
-        self.set("deviceName", if_name) 
+        # upload stop.sh script
+        stop_command = self.replace_paths(self._stop_command)
 
-    def deploy(self):
-        if not self.node or self.node.state < ResourceState.PROVISIONED:
-            self.ec.schedule(reschedule_delay, self.deploy)
-        else:
+        self.node.upload_command(stop_command,
+                shfile = os.path.join(self.app_home, "stop.sh"),
+                # Overwrite file every time. 
+                # The stop.sh has the path to the socket, which should change
+                # on every experiment run.
+                overwrite = True)
+
+    def upload_start_command(self):
+        super(PlanetlabTap, self).upload_start_command()
+
+        # Planetlab TAPs always add a PI header
+        self.set("pi", True)
+
+        if not self.gre_enabled:
+            # After creating the TAP, the pl-vif-create.py script
+            # will write the name of the TAP to a file. We wait until
+            # we can read the interface name from the file.
+            vif_name = self.wait_vif_name()
+            self.set("deviceName", vif_name) 
+
+    def wait_vif_name(self, exec_run_home = None):
+        """ Waits until the vif_name file for the command is generated, 
+            and returns the vif_name for the device """
+        vif_name = None
+        delay = 0.5
+
+        # The vif_name file will be created in the tap-home, while the
+        # current execution home might be elsewhere to check for errors
+        # (e.g. could be a tunnel-home)
+        if not exec_run_home:
+            exec_run_home = self.run_home
+
+        for i in xrange(20):
+            (out, err), proc = self.node.check_output(self.run_home, "vif_name")
+
+            if proc.poll() > 0:
+                (out, err), proc = self.node.check_errors(exec_run_home)
+                
+                if err.strip():
+                    raise RuntimeError, err
 
-            try:
-                self.discover()
-                self.provision()
-            except:
-                self.fail()
-                raise
-            self.debug("----- READY ---- ")
-            self._ready_time = tnow()
-            self._state = ResourceState.READY
-
-    def start(self):
-        if self._state == ResourceState.READY:
-            command = self.get("command")
-            self.info("Starting command '%s'" % command)
-
-            self._start_time = tnow()
-            self._state = ResourceState.STARTED
+            if out:
+                vif_name = out.strip()
+                break
+            else:
+                time.sleep(delay)
+                delay = delay * 1.5
         else:
-            msg = " Failed to execute command '%s'" % command
+            msg = "Couldn't retrieve vif_name"
             self.error(msg, out, err)
-            self._state = ResourceState.FAILED
             raise RuntimeError, msg
 
-    def stop(self):
-        command = self.get('command') or ''
-        state = self.state
+        return vif_name
+
+    def gre_connect(self, remote_endpoint, connection_app_home,
+            connection_run_home):
+        super(PlanetlabTap, self).gre_connect(remote_endpoint, 
+                connection_app_home, connection_run_home)
+         # After creating the TAP, the pl-vif-create.py script
+        # will write the name of the TAP to a file. We wait until
+        # we can read the interface name from the file.
+        vif_name = self.wait_vif_name(exec_run_home = connection_run_home)
+        self.set("deviceName", vif_name) 
+
+        return True
+
+    def _gre_connect_command(self, remote_endpoint, connection_app_home, 
+            connection_run_home): 
+        # Set the remote endpoint, (private) IP of the device
+        self.set("pointopoint", remote_endpoint.get("ip"))
+        # Public IP of the node
+        self.set("greRemote", remote_endpoint.node.get("ip"))
+
+        # Generate GRE connect command
+
+        # Use vif_down command to first kill existing TAP in GRE mode
+        vif_down_command = self._vif_down_command
+
+        # Use pl-vif-up.py script to configure TAP with peer info
+        vif_up_command = self._vif_up_command
         
-        if state == ResourceState.STARTED:
-            self.info("Stopping command '%s'" % command)
+        command = ["("]
+        command.append(vif_down_command)
+        command.append(") ; (")
+        command.append(vif_up_command)
+        command.append(")")
 
-            command = "rm %s" % os.path.join(self.run_home, "if_stop")
-            (out, err), proc = self.execute_command(command)
+        command = " ".join(command)
+        command = self.replace_paths(command)
 
-            self._stop_time = tnow()
-            self._state = ResourceState.STOPPED
+        return command
 
     @property
-    def state(self):
-        # First check if the ccnd has failed
-        state_check_delay = 0.5
-        if self._state == ResourceState.STARTED and \
-                tdiffsec(tnow(), self._last_state_check) > state_check_delay:
-
-            if self.get("deviceName"):
-                (out, err), proc = self.node.execute("ifconfig")
+    def _start_command(self):
+        if self.gre_enabled:
+            command = []
+        else:
+            command = ["sudo -S python ${SRC}/pl-vif-create.py"]
+            
+            command.append("-t %s" % self.vif_type)
+            command.append("-a %s" % self.get("ip"))
+            command.append("-n %s" % self.get("prefix"))
+            command.append("-f %s " % self.vif_name_file)
+            command.append("-S %s " % self.sock_name)
 
-                if out.strip().find(self.get("deviceName")) == -1: 
-                    # tap is not running is not running (socket not found)
-                    self._state = ResourceState.FINISHED
+            if self.get("snat") == True:
+                command.append("-s")
 
-            self._last_state_check = tnow()
+            if self.get("pointopoint"):
+                command.append("-p %s" % self.get("pointopoint"))
+            
+            if self.get("txqueuelen"):
+                command.append("-q %s" % self.get("txqueuelen"))
 
-        return self._state
+        return " ".join(command)
 
-    def wait_if_name(self):
-        """ Waits until the if_name file for the command is generated, 
-            and returns the if_name for the devide """
-        if_name = None
-        delay = 1.0
+    @property
+    def _stop_command(self):
+        if self.gre_enabled:
+            command = self._vif_down_command
+        else:
+            command = ["sudo -S "]
+            command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
+            command.append("python ${SRC}/pl-vif-down.py")
+            command.append("-S %s " % self.sock_name)
+            command = " ".join(command)
 
-        for i in xrange(4):
-            (out, err), proc = self.node.check_output(self.run_home, "if_name")
+        return command
 
-            if out:
-                if_name = out.strip()
-                break
-            else:
-                time.sleep(delay)
-                delay = delay * 1.5
+    @property
+    def _vif_up_command(self):
+        if self.gre_enabled:
+            device_name = "%s" % self.guid
         else:
-            msg = "Couldn't retrieve if_name"
-            self.error(msg, out, err)
-            self.fail()
-            raise RuntimeError, msg
+            device_name = self.get("deviceName")
+
+        # Use pl-vif-up.py script to configure TAP
+        command = ["sudo -S "]
+        command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
+        command.append("python ${SRC}/pl-vif-up.py")
+        command.append("-u %s" % self.node.get("username"))
+        command.append("-N %s" % device_name)
+        command.append("-t %s" % self.vif_type)
+        command.append("-a %s" % self.get("ip"))
+        command.append("-n %s" % self.get("prefix"))
+
+        if self.get("snat") == True:
+            command.append("-s")
+
+        if self.get("pointopoint"):
+            command.append("-p %s" % self.get("pointopoint"))
+        
+        if self.get("txqueuelen"):
+            command.append("-q %s" % self.get("txqueuelen"))
+
+        if self.gre_enabled:
+            command.append("-g %s" % self.get("greKey"))
+            command.append("-G %s" % self.get("greRemote"))
+        
+        command.append("-f %s " % self.vif_name_file)
 
-        return if_name
+        return " ".join(command)
 
     @property
-    def _start_command(self):
-        return "sudo -S python ${APP_HOME}/tap_create.py" 
+    def _vif_down_command(self):
+        if self.gre_enabled:
+            device_name = "%s" % self.guid
+        else:
+            device_name = self.get("deviceName")
+
+        command = ["sudo -S "]
+        command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
+        command.append("python ${SRC}/pl-vif-down.py")
+        command.append("-N %s " % device_name)
+        
+        if self.gre_enabled:
+            command.append("-u %s" % self.node.get("username"))
+            command.append("-t %s" % self.vif_type)
+            command.append("-D")
+
+        return " ".join(command)
 
     @property
-    def _start_script(self):
-        return ( "import vsys, time, os \n"
-                 "(fd, if_name) = vsys.fd_tuntap(vsys.%(devtype)s)\n"
-                 "vsys.vif_up(if_name, '%(ip)s', %(prefix)s%(snat)s%(pointopoint)s)\n"
-                 "f = open('%(if_name_file)s', 'w')\n"
-                 "f.write(if_name)\n"
-                 "f.close()\n\n"
-                 "f = open('%(if_stop_file)s', 'w')\n"
-                 "f.close()\n\n"
-                 "while os.path.exists('%(if_stop_file)s'):\n"
-                 "    time.sleep(2)\n"
-               ) % ({
-                   "devtype": self._vif_type,
-                   "ip": self.get("ip4"),
-                   "prefix": self.get("prefix4"),
-                   "snat": ", snat=True" if self.get("snat") else "",
-                   "pointopoint": ", pointopoint=%s" % self.get("pointopoint") \
-                           if self.get("pointopoint") else "",
-                    "if_name_file": os.path.join(self.run_home, "if_name"),
-                    "if_stop_file": os.path.join(self.run_home, "if_stop"),
-                   })
+    def vif_name_file(self):
+        return os.path.join(self.run_home, "vif_name")
 
     @property
-    def _vif_type(self):
-        return "IFF_TAP"
+    def _install(self):
+        # Install python-vsys and python-passfd
+        install_vsys = ( " ( "
+                    "   python -c 'import vsys, os;  vsys.__version__ == \"%(version)s\" or os._exit(1)' "
+                    " ) "
+                    " || "
+                    " ( "
+                    "   cd ${SRC} ; "
+                    "   hg clone http://nepi.inria.fr/code/python-vsys ; "
+                    "   cd python-vsys ; "
+                    "   make all ; "
+                    "   sudo -S make install "
+                    " )" ) % ({
+                        "version": PYTHON_VSYS_VERSION
+                        })
+
+        install_passfd = ( " ( python -c 'import passfd' ) "
+                    " || "
+                    " ( "
+                    "   cd ${SRC} ; "
+                    "   hg clone http://nepi.inria.fr/code/python-passfd ; "
+                    "   cd python-passfd ; "
+                    "   make all ; "
+                    "   sudo -S make install "
+                    " )" )
+
+        return "%s ; %s" % ( install_vsys, install_passfd )
 
     def valid_connection(self, guid):
         # TODO: Validate!