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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState
21 from nepi.resources.linux.tunnel import LinuxTunnel
22 from nepi.util.sshfuncs import ProcStatus
23 from nepi.util.timefuncs import tnow, tdiffsec
30 class LinuxUdpTunnel(LinuxTunnel):
31 _rtype = "linux::UdpTunnel"
32 _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
36 def _register_attributes(cls):
37 cipher = Attribute("cipher",
38 "Cipher to encript communication. "
39 "One of PLAIN, AES, Blowfish, DES, DES3. ",
41 allowed = ["PLAIN", "AES", "Blowfish", "DES", "DES3"],
42 type = Types.Enumerate,
45 cipher_key = Attribute("cipherKey",
46 "Specify a symmetric encryption key with which to protect "
47 "packets across the tunnel. python-crypto must be installed "
51 txqueuelen = Attribute("txQueueLen",
52 "Specifies the interface's transmission queue length. "
57 bwlimit = Attribute("bwLimit",
58 "Specifies the interface's emulated bandwidth in bytes "
63 cls._register_attribute(cipher)
64 cls._register_attribute(cipher_key)
65 cls._register_attribute(txqueuelen)
66 cls._register_attribute(bwlimit)
68 def __init__(self, ec, guid):
69 super(LinuxUdpTunnel, self).__init__(ec, guid)
70 self._home = "udp-tunnel-%s" % self.guid
73 def log_message(self, msg):
74 return " guid %d - udptunnel %s - %s - %s " % (self.guid,
75 self.endpoint1.node.get("hostname"),
76 self.endpoint2.node.get("hostname"),
79 def get_endpoints(self):
80 """ Returns the list of RM that are endpoints to the tunnel
83 for guid in self.connections:
84 rm = self.ec.get_resource(guid)
85 if hasattr(rm, "initiate_udp_connection"):
89 def initiate_connection(self, endpoint, remote_endpoint):
90 cipher = self.get("cipher")
91 cipher_key = self.get("cipherKey")
92 bwlimit = self.get("bwLimit")
93 txqueuelen = self.get("txQueueLen")
94 connection_app_home = self.app_home(endpoint)
95 connection_run_home = self.run_home(endpoint)
97 port = endpoint.initiate_udp_connection(
101 cipher, cipher_key, bwlimit, txqueuelen)
105 def establish_connection(self, endpoint, remote_endpoint, port):
106 connection_app_home = self.app_home(endpoint)
107 connection_run_home = self.run_home(endpoint)
109 endpoint.establish_udp_connection(remote_endpoint,
114 def verify_connection(self, endpoint, remote_endpoint):
115 connection_app_home = self.app_home(endpoint)
116 connection_run_home = self.run_home(endpoint)
118 endpoint.verify_connection(remote_endpoint,
122 def terminate_connection(self, endpoint, remote_endpoint):
123 connection_app_home = self.app_home(endpoint)
124 connection_run_home = self.run_home(endpoint)
126 endpoint.terminate_connection(remote_endpoint,
130 def check_state_connection(self):
131 # Make sure the process is still running in background
132 # No execution errors occurred. Make sure the background
133 # process with the recorded pid is still running.
135 status1 = self.endpoint1.check_status()
136 status2 = self.endpoint2.check_status()
138 if status1 == ProcStatus.FINISHED and \
139 status2 == ProcStatus.FINISHED:
141 # check if execution errors occurred
142 (out1, err1), proc1 = self.endpoint1.node.check_errors(
143 self.run_home(self.endpoint1))
145 (out2, err2), proc2 = self.endpoint2.node.check_errors(
146 self.run_home(self.endpoint2))
149 msg = "Error occurred in tunnel"
150 self.error(msg, err1, err2)