2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState, \
23 from nepi.resources.linux.tunnel import LinuxTunnel
24 from nepi.util.sshfuncs import ProcStatus
25 from nepi.util.timefuncs import tnow, tdiffsec
32 class LinuxUdpTunnel(LinuxTunnel):
33 _rtype = "LinuxUdpTunnel"
34 _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
38 def _register_attributes(cls):
39 cipher = Attribute("cipher",
40 "Cipher to encript communication. "
41 "One of PLAIN, AES, Blowfish, DES, DES3. ",
43 allowed = ["PLAIN", "AES", "Blowfish", "DES", "DES3"],
44 type = Types.Enumerate,
47 cipher_key = Attribute("cipherKey",
48 "Specify a symmetric encryption key with which to protect "
49 "packets across the tunnel. python-crypto must be installed "
53 txqueuelen = Attribute("txQueueLen",
54 "Specifies the interface's transmission queue length. "
59 bwlimit = Attribute("bwLimit",
60 "Specifies the interface's emulated bandwidth in bytes "
65 cls._register_attribute(cipher)
66 cls._register_attribute(cipher_key)
67 cls._register_attribute(txqueuelen)
68 cls._register_attribute(bwlimit)
70 def __init__(self, ec, guid):
71 super(LinuxUdpTunnel, self).__init__(ec, guid)
72 self._home = "udp-tunnel-%s" % self.guid
75 def log_message(self, msg):
76 return " guid %d - udptunnel %s - %s - %s " % (self.guid,
77 self.endpoint1.node.get("hostname"),
78 self.endpoint2.node.get("hostname"),
81 def initiate_connection(self, endpoint, remote_endpoint):
82 cipher = self.get("cipher")
83 cipher_key = self.get("cipherKey")
84 bwlimit = self.get("bwLimit")
85 txqueuelen = self.get("txQueueLen")
87 # Return the command to execute to initiate the connection to the
89 connection_run_home = self.run_home(endpoint)
90 udp_connect_command = endpoint.udp_connect_command(
91 remote_endpoint, connection_run_home,
92 cipher, cipher_key, bwlimit, txqueuelen)
94 # upload command to connect.sh script
95 shfile = os.path.join(self.app_home(endpoint), "udp-connect.sh")
96 endpoint.node.upload(udp_connect_command,
101 # invoke connect script
102 cmd = "bash %s" % shfile
103 (out, err), proc = endpoint.node.run(cmd, self.run_home(endpoint))
105 # check if execution errors occurred
106 msg = " Failed to connect endpoints "
109 self.error(msg, out, err)
110 raise RuntimeError, msg
112 # Wait for pid file to be generated
113 pid, ppid = endpoint.node.wait_pid(self.run_home(endpoint))
115 # If the process is not running, check for error information
116 # on the remote machine
117 if not pid or not ppid:
118 (out, err), proc = endpoint.node.check_errors(self.run_home(endpoint))
119 # Out is what was written in the stderr file
121 msg = " Failed to start command '%s' " % command
122 self.error(msg, out, err)
123 raise RuntimeError, msg
125 # wait until port is written to file
126 port = self.wait_local_port(endpoint)
128 self._pids[endpoint] = (pid, ppid)
132 def establish_connection(self, endpoint, remote_endpoint, port):
133 self.upload_remote_port(endpoint, port)
135 def verify_connection(self, endpoint, remote_endpoint):
136 self.wait_result(endpoint)
138 def terminate_connection(self, endpoint, remote_endpoint):
139 pid, ppid = self._pids[endpoint]
142 (out, err), proc = endpoint.node.kill(pid, ppid,
145 # check if execution errors occurred
146 if proc.poll() and err:
147 msg = " Failed to STOP tunnel"
148 self.error(msg, out, err)
149 raise RuntimeError, msg
151 def check_state_connection(self):
152 # Make sure the process is still running in background
153 # No execution errors occurred. Make sure the background
154 # process with the recorded pid is still running.
155 pid1, ppid1 = self._pids[self.endpoint1]
156 pid2, ppid2 = self._pids[self.endpoint2]
158 status1 = self.endpoint1.node.status(pid1, ppid1)
159 status2 = self.endpoint2.node.status(pid2, ppid2)
161 if status1 == ProcStatus.FINISHED and \
162 status2 == ProcStatus.FINISHED:
164 # check if execution errors occurred
165 (out1, err1), proc1 = self.endpoint1.node.check_errors(
166 self.run_home(self.endpoint1))
168 (out2, err2), proc2 = self.endpoint2.node.check_errors(
169 self.run_home(self.endpoint2))
172 msg = "Error occurred in tunnel"
173 self.error(msg, err1, err2)
178 def wait_local_port(self, endpoint):
179 """ Waits until the local_port file for the endpoint is generated,
180 and returns the port number
183 return self.wait_file(endpoint, "local_port")
185 def wait_result(self, endpoint):
186 """ Waits until the return code file for the endpoint is generated
189 return self.wait_file(endpoint, "ret_file")
191 def wait_file(self, endpoint, filename):
192 """ Waits until file on endpoint is generated """
197 (out, err), proc = endpoint.node.check_output(
198 self.run_home(endpoint), filename)
207 msg = "Couldn't retrieve %s" % filename
208 self.error(msg, out, err)
209 raise RuntimeError, msg
213 def upload_remote_port(self, endpoint, port):
214 # upload remote port number to file
216 endpoint.node.upload(port,
217 os.path.join(self.run_home(endpoint), "remote_port"),