README moves to markdown
[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 version 2 as
7 #    published by the Free Software Foundation;
8 #
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.
13 #
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/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
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
24
25 import os
26 import socket
27 import time
28
29 @clsinit_copy
30 class LinuxUdpTunnel(LinuxTunnel):
31     _rtype = "linux::UdpTunnel"
32     _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
33     _platform = "linux"
34
35     @classmethod
36     def _register_attributes(cls):
37         cipher = Attribute("cipher",
38                "Cipher to encript communication. "
39                 "One of PLAIN, AES, Blowfish, DES, DES3. ",
40                 default = None,
41                 allowed = ["PLAIN", "AES", "Blowfish", "DES", "DES3"],
42                 type = Types.Enumerate, 
43                 flags = Flags.Design)
44
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 "
48                 "on the system." ,
49                 flags = Flags.Design)
50
51         txqueuelen = Attribute("txQueueLen",
52                 "Specifies the interface's transmission queue length. "
53                 "Defaults to 1000. ", 
54                 type = Types.Integer, 
55                 flags = Flags.Design)
56
57         bwlimit = Attribute("bwLimit",
58                 "Specifies the interface's emulated bandwidth in bytes "
59                 "per second.",
60                 type = Types.Integer, 
61                 flags = Flags.Design)
62
63         cls._register_attribute(cipher)
64         cls._register_attribute(cipher_key)
65         cls._register_attribute(txqueuelen)
66         cls._register_attribute(bwlimit)
67
68     def __init__(self, ec, guid):
69         super(LinuxUdpTunnel, self).__init__(ec, guid)
70         self._home = "udp-tunnel-%s" % self.guid
71         self._pids = dict()
72
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"), 
77                 msg)
78
79     def get_endpoints(self):
80         """ Returns the list of RM that are endpoints to the tunnel 
81         """
82         connected = []
83         for guid in self.connections:
84             rm = self.ec.get_resource(guid)
85             if hasattr(rm, "initiate_udp_connection"):
86                 connected.append(rm)
87         return connected
88
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)
96
97         port = endpoint.initiate_udp_connection(
98                 remote_endpoint, 
99                 connection_app_home,
100                 connection_run_home, 
101                 cipher, cipher_key, bwlimit, txqueuelen)
102
103         return port
104
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)
108
109         endpoint.establish_udp_connection(remote_endpoint,
110                 connection_app_home,
111                 connection_run_home, 
112                 port)
113
114     def verify_connection(self, endpoint, remote_endpoint):
115         connection_app_home = self.app_home(endpoint)
116         connection_run_home = self.run_home(endpoint)
117
118         endpoint.verify_connection(remote_endpoint,
119                 connection_app_home, 
120                 connection_run_home)
121
122     def terminate_connection(self, endpoint, remote_endpoint):
123         connection_app_home = self.app_home(endpoint)
124         connection_run_home = self.run_home(endpoint)
125
126         endpoint.terminate_connection(remote_endpoint,
127                 connection_app_home, 
128                 connection_run_home)
129
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.
134
135         status1 = self.endpoint1.check_status()
136         status2 = self.endpoint2.check_status()
137
138         if status1 == ProcStatus.FINISHED and \
139                 status2 == ProcStatus.FINISHED:
140
141             # check if execution errors occurred
142             (out1, err1), proc1 = self.endpoint1.node.check_errors(
143                     self.run_home(self.endpoint1))
144
145             (out2, err2), proc2 = self.endpoint2.node.check_errors(
146                     self.run_home(self.endpoint2))
147
148             if err1 or err2: 
149                 msg = "Error occurred in tunnel"
150                 self.error(msg, err1, err2)
151                 self.fail()
152             else:
153                 self.set_stopped()
154
155