a3ddecbc4f93018e0f96a873f6eaa7c29aea6a26
[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 passfd
23 import socket
24 import vsys
25 from optparse import OptionParser
26
27 STOP_MSG = "STOP"
28 PASSFD_MSG = "PASSFD"
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     # The message might have arguments that will be appended
56     # as a '|' separated list after the message type
57     args = msg.split("|")
58     msg = args.pop(0)
59
60     dmsg = base64.b64decode(msg)
61     dargs = []
62     for arg in args:
63         darg = base64.b64decode(arg)
64         dargs.append(darg.rstrip())
65
66     return (dmsg.rstrip(), dargs)
67
68 def send_reply(conn, reply):
69     encoded = base64.b64encode(reply)
70     conn.send("%s\n" % encoded)
71
72 def stop_action():
73     return "STOP-ACK"
74
75 def passfd_action(fd, args):
76     """ Sends the file descriptor associated to the TAP device 
77     to another process through a unix socket.
78     """
79     address = args.pop(0)
80     print address
81     sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
82     sock.connect(address)
83     passfd.sendfd(sock, fd, '0')
84     return "PASSFD-ACK"
85
86 def get_options():
87     usage = ("usage: %prog -t <vif-type> -a <ip4-address> -n <net-prefix> "
88         "-s <snat> -p <pointopoint> -q <txqueuelen> -f <vif-name-file> "
89         "-S <socket-name>")
90     
91     parser = OptionParser(usage = usage)
92
93     parser.add_option("-t", "--vif-type", dest="vif_type",
94         help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
95             "Defaults to IFF_TAP. ", type="str")
96
97     parser.add_option("-a", "--ip4-address", dest="ip4_address",
98         help = "IPv4 address to assign to interface. It must belong to the "
99             "network segment owned by the slice, given by the vsys_vnet tag. ",
100         type="str")
101
102     parser.add_option("-n", "--net-prefix", dest="net_prefix",
103         help = "IPv4 network prefix for the interface. It must be the one "
104             "given by the slice's vsys_vnet tag. ",
105         type="int")
106
107     parser.add_option("-s", "--snat", dest="snat", default = False,
108         action="store_true", help="Enable SNAT for the interface")
109
110     parser.add_option("-p", "--pointopoint", dest="pointopoint",
111         help = "Peer end point for the interface  ", default = None,
112         type="str")
113
114     parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
115         help = "Size of transmision queue. Defaults to 0.",
116         default = 0,
117         type="int")
118
119     parser.add_option("-f", "--vif-name-file", dest="vif_name_file",
120         help = "File to store the virtual interface name assigned by the OS", 
121         default = "vif_name", type="str")
122
123     parser.add_option("-S", "--socket-name", dest="socket_name",
124         help = "Name for the unix socket used to interact with this process", 
125         type="str")
126
127     (options, args) = parser.parse_args()
128     
129     vif_type = vsys.IFF_TAP
130     if options.vif_type and options.vif_type == "IFF_TUN":
131         vif_type = vsys.IFF_TUN
132
133     return (vif_type, options.ip4_address, options.net_prefix, 
134             options.snat, options.pointopoint, options.txqueuelen,
135             options.vif_name_file, options.socket_name)
136
137 if __name__ == '__main__':
138
139     (vif_type, ip4_address, net_prefix, snat, pointopoint, 
140             txqueuelen, vif_name_file, socket_name) = get_options()
141
142     (fd, vif_name) = vsys.fd_tuntap(vif_type)
143   
144     vsys.vif_up(vif_name, ip4_address, net_prefix, snat = snat, 
145             pointopoint = pointopoint, txqueuelen = txqueuelen) 
146      
147     # Saving interface name to vif_name_file
148     f = open(vif_name_file, 'w')
149     f.write(vif_name)
150     f.close()
151
152     # create unix socket to receive instructions
153     sock = create_socket(socket_name)
154     sock.listen(0)
155
156     # wait for messages to arrive and process them
157     stop = False
158
159     while not stop:
160         conn, addr = sock.accept()
161         conn.settimeout(5)
162
163         while not stop:
164             try:
165                 (msg, args) = recv_msg(conn)
166             except socket.timeout, e:
167                 # Ingore time-out
168                 continue
169
170             if not msg:
171                 # Ignore - connection lost
172                 break
173
174             if msg == STOP_MSG:
175                 stop = True
176                 reply = stop_action()
177             elif msg == PASSFD_MSG:
178                 reply = passfd_action(fd, args)
179
180             try:
181                 send_reply(conn, reply)
182             except socket.error:
183                 break
184