Bug fixing and ordering openvswitch code
[nepi.git] / src / nepi / resources / linux / udptunnel.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState
22 from nepi.resources.linux.tunnel import LinuxTunnel
23 from nepi.util.sshfuncs import ProcStatus
24 from nepi.util.timefuncs import tnow, tdiffsec
25
26 import os
27 import socket
28 import time
29
30 @clsinit_copy
31 class LinuxUdpTunnel(LinuxTunnel):
32     _rtype = "linux::UdpTunnel"
33     _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
34     _platform = "linux"
35
36     @classmethod
37     def _register_attributes(cls):
38         cipher = Attribute("cipher",
39                "Cipher to encript communication. "
40                 "One of PLAIN, AES, Blowfish, DES, DES3. ",
41                 default = None,
42                 allowed = ["PLAIN", "AES", "Blowfish", "DES", "DES3"],
43                 type = Types.Enumerate, 
44                 flags = Flags.Design)
45
46         cipher_key = Attribute("cipherKey",
47                 "Specify a symmetric encryption key with which to protect "
48                 "packets across the tunnel. python-crypto must be installed "
49                 "on the system." ,
50                 flags = Flags.Design)
51
52         txqueuelen = Attribute("txQueueLen",
53                 "Specifies the interface's transmission queue length. "
54                 "Defaults to 1000. ", 
55                 type = Types.Integer, 
56                 flags = Flags.Design)
57
58         bwlimit = Attribute("bwLimit",
59                 "Specifies the interface's emulated bandwidth in bytes "
60                 "per second.",
61                 type = Types.Integer, 
62                 flags = Flags.Design)
63
64         cls._register_attribute(cipher)
65         cls._register_attribute(cipher_key)
66         cls._register_attribute(txqueuelen)
67         cls._register_attribute(bwlimit)
68
69     def __init__(self, ec, guid):
70         super(LinuxUdpTunnel, self).__init__(ec, guid)
71         self._home = "udp-tunnel-%s" % self.guid
72         self._pids = dict()
73
74     def log_message(self, msg):
75         return " guid %d - udptunnel %s - %s - %s " % (self.guid, 
76                 self.endpoint1.node.get("hostname"), 
77                 self.endpoint2.node.get("hostname"), 
78                 msg)
79
80     def get_endpoints(self):
81         """ Returns the list of RM that are endpoints to the tunnel 
82         """
83         connected = []
84         for guid in self.connections:
85             rm = self.ec.get_resource(guid)
86             if hasattr(rm, "initiate_udp_connection"):
87                 connected.append(rm)
88         return connected
89
90     def initiate_connection(self, endpoint, remote_endpoint):
91         cipher = self.get("cipher")
92         cipher_key = self.get("cipherKey")
93         bwlimit = self.get("bwLimit")
94         txqueuelen = self.get("txQueueLen")
95         connection_app_home = self.app_home(endpoint)
96         connection_run_home = self.run_home(endpoint)
97
98         port = endpoint.initiate_udp_connection(
99                 remote_endpoint, 
100                 connection_app_home,
101                 connection_run_home, 
102                 cipher, cipher_key, bwlimit, txqueuelen)
103
104         return port
105
106     def establish_connection(self, endpoint, remote_endpoint, port):
107         connection_app_home = self.app_home(endpoint)
108         connection_run_home = self.run_home(endpoint)
109
110         endpoint.establish_udp_connection(remote_endpoint,
111                 connection_app_home,
112                 connection_run_home, 
113                 port)
114
115     def verify_connection(self, endpoint, remote_endpoint):
116         endpoint.verify_connection()
117
118     def terminate_connection(self, endpoint, remote_endpoint):
119         endpoint.terminate_connection()
120
121     def check_state_connection(self):
122         # Make sure the process is still running in background
123         # No execution errors occurred. Make sure the background
124         # process with the recorded pid is still running.
125
126         status1 = self.endpoint1.check_status()
127         status2 = self.endpoint2.check_status()
128
129         if status1 == ProcStatus.FINISHED and \
130                 status2 == ProcStatus.FINISHED:
131
132             # check if execution errors occurred
133             (out1, err1), proc1 = self.endpoint1.node.check_errors(
134                     self.run_home(self.endpoint1))
135
136             (out2, err2), proc2 = self.endpoint2.node.check_errors(
137                     self.run_home(self.endpoint2))
138
139             if err1 or err2: 
140                 msg = "Error occurred in tunnel"
141                 self.error(msg, err1, err2)
142                 self.fail()
143             else:
144                 self.set_stopped()
145
146     def wait_local_port(self, endpoint):
147         """ Waits until the local_port file for the endpoint is generated, 
148         and returns the port number 
149         
150         """
151         return self.wait_file(endpoint, "local_port")
152
153     def wait_result(self, endpoint):
154         """ Waits until the return code file for the endpoint is generated 
155         
156         """ 
157         return self.wait_file(endpoint, "ret_file")
158  
159     def wait_file(self, endpoint, filename):
160         """ Waits until file on endpoint is generated """
161         result = None
162         delay = 1.0
163
164         for i in xrange(20):
165             (out, err), proc = endpoint.node.check_output(
166                     self.run_home(endpoint), filename)
167
168             if out:
169                 result = out.strip()
170                 break
171             else:
172                 time.sleep(delay)
173                 delay = delay * 1.5
174         else:
175             msg = "Couldn't retrieve %s" % filename
176             self.error(msg, out, err)
177             raise RuntimeError, msg
178
179         return result
180