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 get_endpoints(self):
82 """ Returns the list of RM that are endpoints to the tunnel
85 for guid in self.connections:
86 rm = self.ec.get_resource(guid)
87 if hasattr(rm, "initiate_udp_connection"):
91 def initiate_connection(self, endpoint, remote_endpoint):
92 cipher = self.get("cipher")
93 cipher_key = self.get("cipherKey")
94 bwlimit = self.get("bwLimit")
95 txqueuelen = self.get("txQueueLen")
96 connection_app_home = self.app_home(endpoint)
97 connection_run_home = self.run_home(endpoint)
99 port = endpoint.initiate_udp_connection(
103 cipher, cipher_key, bwlimit, txqueuelen)
107 def establish_connection(self, endpoint, remote_endpoint, port):
108 endpoint.establish_udp_connection(remote_endpoint, port)
110 def verify_connection(self, endpoint, remote_endpoint):
111 endpoint.verify_connection()
113 def terminate_connection(self, endpoint, remote_endpoint):
114 endpoint.terminate_connection()
116 def check_state_connection(self):
117 # Make sure the process is still running in background
118 # No execution errors occurred. Make sure the background
119 # process with the recorded pid is still running.
121 status1 = self.endpoint1.check_status()
122 status2 = self.endpoint2.check_status()
124 if status1 == ProcStatus.FINISHED and \
125 status2 == ProcStatus.FINISHED:
127 # check if execution errors occurred
128 (out1, err1), proc1 = self.endpoint1.node.check_errors(
129 self.run_home(self.endpoint1))
131 (out2, err2), proc2 = self.endpoint2.node.check_errors(
132 self.run_home(self.endpoint2))
135 msg = "Error occurred in tunnel"
136 self.error(msg, err1, err2)
141 def wait_local_port(self, endpoint):
142 """ Waits until the local_port file for the endpoint is generated,
143 and returns the port number
146 return self.wait_file(endpoint, "local_port")
148 def wait_result(self, endpoint):
149 """ Waits until the return code file for the endpoint is generated
152 return self.wait_file(endpoint, "ret_file")
154 def wait_file(self, endpoint, filename):
155 """ Waits until file on endpoint is generated """
160 (out, err), proc = endpoint.node.check_output(
161 self.run_home(endpoint), filename)
170 msg = "Couldn't retrieve %s" % filename
171 self.error(msg, out, err)
172 raise RuntimeError, msg