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>
21 from optparse import OptionParser
24 usage = ("usage: %prog -u <slicename> -N <vif-name> -t <vif-type> -a <ip4-address> "
25 "-n <net-prefix> -S <snat> -p <pointopoint> -q <txqueuelen> "
26 "-g <gre_key> -G <gre_remote> -f <vif-name-file> ")
28 parser = OptionParser(usage = usage)
30 parser.add_option("-u", "--slicename", dest="slicename",
31 help = "The name of the PlanetLab slice ",
34 parser.add_option("-N", "--vif-name", dest="vif_name",
35 help = "The name of the virtual interface, or a "
36 "unique numeric identifier to name the interface "
37 "if GRE mode is used.",
40 parser.add_option("-t", "--vif-type", dest="vif_type",
41 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
42 "Defaults to IFF_TAP. ", type="str")
44 parser.add_option("-a", "--ip4-address", dest="ip4_address",
45 help = "IPv4 address to assign to interface. It must belong to the "
46 "network segment owned by the slice, given by the vsys_vnet tag. ",
49 parser.add_option("-n", "--net-prefix", dest="net_prefix",
50 help = "IPv4 network prefix for the interface. It must be the one "
51 "given by the slice's vsys_vnet tag. ",
54 parser.add_option("-s", "--snat", dest="snat",
57 help="Enable SNAT for the interface")
59 parser.add_option("-p", "--pointopoint", dest="pointopoint",
60 help = "Peer end point for the interface. ",
64 parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
65 help = "Size of transmision queue. Defaults to 0.",
69 parser.add_option("-g", "--gre-key", dest="gre_key",
70 help = "When set, enables GRE mode with the corresponding GRE key.",
74 parser.add_option("-G", "--gre-remote", dest="gre_remote",
75 help = "Remote endpoint (public IP) for the GRE tunnel.",
79 parser.add_option("-f", "--vif-name-file", dest="vif_name_file",
80 help = "File to store the virtual interface name assigned by the OS",
81 default = "vif_name", type="str")
83 (options, args) = parser.parse_args()
85 vif_type = vsys.IFF_TAP
86 if options.vif_type and options.vif_type == "IFF_TUN":
87 vif_type = vsys.IFF_TUN
89 return (options.slicename, options.vif_name, vif_type, options.ip4_address,
90 options.net_prefix, options.snat, options.pointopoint,
91 options.txqueuelen, options.gre_key, options.gre_remote,
92 options.vif_name_file)
94 if __name__ == '__main__':
96 (slicename, vif_name, vif_type, ip4_address, net_prefix, snat, pointopoint,
97 txqueuelen, gre_key, gre_remote, vif_name_file) = get_options()
103 sliceid = pwd.getpwnam(slicename).pw_uid
105 if vif_type == vsys.IFF_TAP:
110 # if_name should be a unique numeric vif id
111 vif_name = "%s%s-%s" % (vif_prefix, sliceid, vif_name)
114 vsys.vif_up(vif_name, ip4_address, net_prefix, snat = snat,
115 pointopoint = pointopoint, txqueuelen = txqueuelen,
116 gre_key = gre_key, gre_remote = gre_remote)
118 except RuntimeError as e:
121 traceback.print_exc(file=sys.stderr)
124 if e.message.find("WARNING:") < 0:
127 # Saving interface name to vif_name_file
128 f = open(vif_name_file, 'w')