2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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;
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.
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/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
24 from optparse import OptionParser
29 def create_socket(socket_name):
30 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
31 sock.bind(socket_name)
38 while '\n' not in chunk:
40 chunk = conn.recv(1024)
41 except (OSError, socket.error), e:
42 if e[0] != errno.EINTR:
53 msg = ''.join(msg).split('\n')[0]
54 # The message might have arguments that will be appended
55 # as a '|' separated list after the message type
59 dmsg = base64.b64decode(msg)
62 darg = base64.b64decode(arg)
63 dargs.append(darg.rstrip())
65 return (dmsg.rstrip(), dargs)
67 def send_reply(conn, reply):
68 encoded = base64.b64encode(reply)
69 conn.send("%s\n" % encoded)
74 def passfd_action(fd, args):
75 """ Sends the file descriptor associated to the TAP device
76 to another process through a unix socket.
80 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
82 passfd.sendfd(sock, fd, '0')
86 usage = ("usage: %prog -t <vif-type> -a <ip4-address> -n <net-prefix> "
87 "-s <snat> -p <pointopoint> -q <txqueuelen> -f <vif-name-file> "
90 parser = OptionParser(usage = usage)
92 parser.add_option("-t", "--vif-type", dest="vif_type",
93 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
94 "Defaults to IFF_TAP. ", type="str")
96 parser.add_option("-a", "--ip4-address", dest="ip4_address",
97 help = "IPv4 address to assign to interface. It must belong to the "
98 "network segment owned by the slice, given by the vsys_vnet tag. ",
101 parser.add_option("-n", "--net-prefix", dest="net_prefix",
102 help = "IPv4 network prefix for the interface. It must be the one "
103 "given by the slice's vsys_vnet tag. ",
106 parser.add_option("-s", "--snat", dest="snat", default = False,
107 action="store_true", help="Enable SNAT for the interface")
109 parser.add_option("-p", "--pointopoint", dest="pointopoint",
110 help = "Peer end point for the interface ", default = None,
113 parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
114 help = "Size of transmision queue. Defaults to 0.",
118 parser.add_option("-f", "--vif-name-file", dest="vif_name_file",
119 help = "File to store the virtual interface name assigned by the OS",
120 default = "vif_name", type="str")
122 parser.add_option("-S", "--socket-name", dest="socket_name",
123 help = "Name for the unix socket used to interact with this process",
126 (options, args) = parser.parse_args()
128 vif_type = vsys.IFF_TAP
129 if options.vif_type and options.vif_type == "IFF_TUN":
130 vif_type = vsys.IFF_TUN
132 return (vif_type, options.ip4_address, options.net_prefix,
133 options.snat, options.pointopoint, options.txqueuelen,
134 options.vif_name_file, options.socket_name)
136 if __name__ == '__main__':
138 (vif_type, ip4_address, net_prefix, snat, pointopoint,
139 txqueuelen, vif_name_file, socket_name) = get_options()
141 (fd, vif_name) = vsys.fd_tuntap(vif_type)
143 vsys.vif_up(vif_name, ip4_address, net_prefix, snat = snat,
144 pointopoint = pointopoint, txqueuelen = txqueuelen)
146 # Saving interface name to vif_name_file
147 f = open(vif_name_file, 'w')
151 # create unix socket to receive instructions
152 sock = create_socket(socket_name)
155 # wait for messages to arrive and process them
159 conn, addr = sock.accept()
164 (msg, args) = recv_msg(conn)
165 except socket.timeout, e:
170 # Ignore - connection lost
175 reply = stop_action()
176 elif msg == PASSFD_MSG:
177 reply = passfd_action(fd, args)
180 send_reply(conn, reply)