f2c37f232850f3ec01616b53b4faa9f8d7d49534
[nepi.git] / src / nepi / resources / netns / netnsserver.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 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 import base64
20 import pickle
21 import errno
22 import logging
23 import os
24 import socket
25 import sys
26
27 from optparse import OptionParser, SUPPRESS_HELP
28
29 from netnswrapper import NetNSWrapper
30
31 class NetNSWrapperMessage:
32     CREATE = "CREATE"
33     INVOKE = "INVOKE"
34     SET = "SET"
35     GET = "GET"
36     FLUSH = "FLUSH"
37     SHUTDOWN = "SHUTDOWN"
38
39 def handle_message(wrapper, msg_type, args, kwargs):
40     if msg_type == NetNSWrapperMessage.SHUTDOWN:
41         wrapper.shutdown()
42
43         return "BYEBYE"
44     
45     if msg_type == NetNSWrapperMessage.CREATE:
46         clazzname = args.pop(0)
47         
48         return wrapper.create(clazzname, *args)
49         
50     if msg_type == NetNSWrapperMessage.INVOKE:
51         uuid = args.pop(0)
52         operation = args.pop(0)
53    
54         return wrapper.invoke(uuid, operation, *args, **kwargs)
55
56     if msg_type == NetNSWrapperMessage.GET:
57         uuid = args.pop(0)
58         name = args.pop(0)
59
60         return wrapper.get(uuid, name)
61         
62     if msg_type == NetNSWrapperMessage.SET:
63         uuid = args.pop(0)
64         name = args.pop(0)
65         value = args.pop(0)
66
67         return wrapper.set(uuid, name, value)
68
69     if msg_type == NetNSWrapperMessage.FLUSH:
70         # Forces flushing output and error streams.
71         # NS-3 output will stay unflushed until the program exits or 
72         # explicit invocation flush is done
73         sys.stdout.flush()
74         sys.stderr.flush()
75
76         wrapper.logger.debug("FLUSHED") 
77         
78         return "FLUSHED"
79
80 def create_socket(socket_name):
81     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
82     sock.bind(socket_name)
83     return sock
84
85 def recv_msg(conn):
86     msg = []
87     chunk = ''
88
89     while '\n' not in chunk:
90         try:
91             chunk = conn.recv(1024)
92         except (OSError, socket.error) as e:
93             if e[0] != errno.EINTR:
94                 raise
95             # Ignore eintr errors
96             continue
97
98         if chunk:
99             msg.append(chunk)
100         else:
101             # empty chunk = EOF
102             break
103  
104     msg = ''.join(msg).strip()
105
106     # The message is formatted as follows:
107     #   MESSAGE_TYPE|args|kwargs
108     #
109     #   where MESSAGE_TYPE, args and kwargs are pickld and enoded in base64
110
111     def decode(item):
112         item = base64.b64decode(item).rstrip()
113         return pickle.loads(item)
114
115     decoded = [ decode(x) for x in msg.split("|")]
116
117     # decoded message
118     dmsg_type = decoded.pop(0)
119     dargs = list(decoded.pop(0)) # transforming touple into list
120     dkwargs = decoded.pop(0)
121
122     return (dmsg_type, dargs, dkwargs)
123
124 def send_reply(conn, reply):
125     encoded = base64.b64encode(pickle.dumps(reply))
126     conn.send("%s\n" % encoded)
127
128 def get_options():
129     usage = ("usage: %prog -S <socket-name> -D <enable-dump> -v ")
130     
131     parser = OptionParser(usage = usage)
132
133     parser.add_option("-S", "--socket-name", dest="socket_name",
134         help = "Name for the unix socket used to interact with this process", 
135         default = "tap.sock", type="str")
136
137     parser.add_option("-D", "--enable-dump", dest="enable_dump",
138         help = "Enable dumping the remote executed commands to a script "
139             "in order to later reproduce and debug the experiment",
140         action = "store_true",
141         default = False)
142
143     parser.add_option("-v", "--verbose",
144         help="Print debug output",
145         action="store_true", 
146         dest="verbose", default=False)
147
148     (options, args) = parser.parse_args()
149     
150     return (options.socket_name, options.verbose, options.enable_dump)
151
152 def run_server(socket_name, level = logging.INFO, 
153         enable_dump = False):
154
155     ###### wrapper instantiation
156     if level == logging.DEBUG:
157         from syslog import LOG_DEBUG
158         import netns
159         netns.environ.set_log_level(LOG_DEBUG)
160
161     wrapper = NetNSWrapper(loglevel=level, enable_dump = enable_dump)
162     
163     wrapper.logger.info("STARTING...")
164
165     # create unix socket to receive instructions
166     sock = create_socket(socket_name)
167     sock.listen(0)
168
169     # wait for messages to arrive and process them
170     stop = False
171
172     while not stop:
173         conn, addr = sock.accept()
174         conn.settimeout(5)
175
176         try:
177             (msg_type, args, kwargs) = recv_msg(conn)
178         except socket.timeout as e:
179             # Ingore time-out
180             continue
181
182         if not msg_type:
183             # Ignore - connection lost
184             break
185
186         if msg_type == NetNSWrapperMessage.SHUTDOWN:
187            stop = True
188   
189         try:
190             reply = handle_message(wrapper, msg_type, args, kwargs)  
191         except:
192             import traceback
193             err = traceback.format_exc()
194             wrapper.logger.error(err) 
195             raise
196
197         try:
198             send_reply(conn, reply)
199         except socket.error:
200             break
201         
202     wrapper.logger.info("EXITING...")
203
204 if __name__ == '__main__':
205             
206     (socket_name, verbose, enable_dump) = get_options()
207
208     ## configure logging
209     FORMAT = "%(asctime)s %(name)s %(levelname)-4s %(message)s"
210     level = logging.DEBUG if verbose else logging.INFO
211     
212     logging.basicConfig(format = FORMAT, level = level)
213
214     ## Run the server
215     run_server(socket_name, level, enable_dump)
216