PlanetlabTap reads STOP from unix socket
[nepi.git] / src / nepi / resources / planetlab / scripts / pl-vif-create.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 import base64
21 import errno
22 import vsys
23 import socket
24 from optparse import OptionParser, SUPPRESS_HELP
25
26 # TODO: GRE OPTION!! CONFIGURE THE VIF-UP IN GRE MODE!!
27
28 STOP_MSG = "STOP"
29
30 def create_socket(socket_name):
31     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
32     sock.bind(socket_name)
33     return sock
34
35 def recv_msg(conn):
36     msg = []
37     chunk = ''
38
39     while '\n' not in chunk:
40         try:
41             chunk = conn.recv(1024)
42         except (OSError, socket.error), e:
43             if e[0] != errno.EINTR:
44                 raise
45             # Ignore eintr errors
46             continue
47
48         if chunk:
49             msg.append(chunk)
50         else:
51             # empty chunk = EOF
52             break
53
54     msg = ''.join(msg).split('\n')[0]
55     decoded = base64.b64decode(msg)
56     return decoded.rstrip()
57
58 def send_reply(conn, reply):
59     encoded = base64.b64encode(reply)
60     conn.send("%s\n" % encoded)
61
62 def stop_action():
63     return "STOP-ACK"
64
65 def reply_action(msg):
66     return "Reply to: %s" % msg
67
68 def get_options():
69     usage = ("usage: %prog -t <vif-type> -a <ip4-address> -n <net-prefix> "
70         "-s <snat> -p <pointopoint> -f <if-name-file> -S <socket-name>")
71     
72     parser = OptionParser(usage = usage)
73
74     parser.add_option("-t", "--vif-type", dest="vif_type",
75         help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
76             "Defaults to IFF_TAP. ", type="str")
77
78     parser.add_option("-a", "--ip4-address", dest="ip4_address",
79         help = "IPv4 address to assign to interface. It must belong to the "
80             "network segment owned by the slice, given by the vsys_vnet tag. ",
81         type="str")
82
83     parser.add_option("-n", "--net-prefix", dest="net_prefix",
84         help = "IPv4 network prefix for the interface. It must be the one "
85             "given by the slice's vsys_vnet tag. ",
86         type="int")
87
88     parser.add_option("-s", "--snat", dest="snat", default = False,
89         action="store_true", help="Enable SNAT for the interface")
90
91     parser.add_option("-p", "--pointopoint", dest="pointopoint",
92         help = "Peer end point for the interface  ", default = None,
93         type="str")
94
95     parser.add_option("-f", "--if-name-file", dest="if_name_file",
96         help = "File to store the interface name assigned by the OS", 
97         default = "if_name", type="str")
98
99     parser.add_option("-S", "--socket-name", dest="socket_name",
100         help = "Name for the unix socket used to interact with this process", 
101         default = "tap.sock", type="str")
102
103     (options, args) = parser.parse_args()
104     
105     vif_type = vsys.IFF_TAP
106     if options.vif_type and options.vif_type == "IFF_TUN":
107         vif_type = vsys.IFF_TUN
108
109     return (vif_type, options.ip4_address, options.net_prefix, options.snat,
110             options.pointopoint, options.if_name_file, options.socket_name)
111
112 if __name__ == '__main__':
113
114     (vif_type, ip4_address, net_prefix, snat, pointopoint,
115             if_name_file, socket_name) = get_options()
116
117     (fd, if_name) = vsys.fd_tuntap(vif_type)
118     vsys.vif_up(if_name, ip4_address, net_prefix, snat, pointopoint)
119     
120     # Saving interface name to 'if_name_file
121     f = open(if_name_file, 'w')
122     f.write(if_name)
123     f.close()
124
125     # create unix socket to receive instructions
126     sock = create_socket(socket_name)
127     sock.listen(0)
128
129     # wait for messages to arrive and process them
130     stop = False
131
132     while not stop:
133         conn, addr = sock.accept()
134         conn.settimeout(5)
135
136         while not stop:
137             try:
138                 msg = recv_msg(conn)
139             except socket.timeout, e:
140                 # Ingore time-out
141                 continue
142
143             if not msg:
144                 # Ignore - connection lost
145                 break
146
147             if msg == STOP_MSG:
148                 stop = True
149                 reply = stop_action()
150             else:
151                 reply = reply_action(msg)
152
153             try:
154                 send_reply(conn, reply)
155             except socket.error:
156                 break
157