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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
22 from optparse import OptionParser
25 usage = ("usage: %prog -u <slicename> -N <vif-name> -t <vif-type> -a <ip4-address> "
26 "-n <net-prefix> -S <snat> -p <pointopoint> -q <txqueuelen> "
27 "-g <gre_key> -G <gre_remote> -f <vif-name-file> ")
29 parser = OptionParser(usage = usage)
31 parser.add_option("-u", "--slicename", dest="slicename",
32 help = "The name of the PlanetLab slice ",
35 parser.add_option("-N", "--vif-name", dest="vif_name",
36 help = "The name of the virtual interface, or a "
37 "unique numeric identifier to name the interface "
38 "if GRE mode is used.",
41 parser.add_option("-t", "--vif-type", dest="vif_type",
42 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
43 "Defaults to IFF_TAP. ", type="str")
45 parser.add_option("-a", "--ip4-address", dest="ip4_address",
46 help = "IPv4 address to assign to interface. It must belong to the "
47 "network segment owned by the slice, given by the vsys_vnet tag. ",
50 parser.add_option("-n", "--net-prefix", dest="net_prefix",
51 help = "IPv4 network prefix for the interface. It must be the one "
52 "given by the slice's vsys_vnet tag. ",
55 parser.add_option("-s", "--snat", dest="snat",
58 help="Enable SNAT for the interface")
60 parser.add_option("-p", "--pointopoint", dest="pointopoint",
61 help = "Peer end point for the interface. ",
65 parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
66 help = "Size of transmision queue. Defaults to 0.",
70 parser.add_option("-g", "--gre-key", dest="gre_key",
71 help = "When set, enables GRE mode with the corresponding GRE key.",
75 parser.add_option("-G", "--gre-remote", dest="gre_remote",
76 help = "Remote endpoint (public IP) for the GRE tunnel.",
80 parser.add_option("-f", "--vif-name-file", dest="vif_name_file",
81 help = "File to store the virtual interface name assigned by the OS",
82 default = "vif_name", type="str")
84 (options, args) = parser.parse_args()
86 vif_type = vsys.IFF_TAP
87 if options.vif_type and options.vif_type == "IFF_TUN":
88 vif_type = vsys.IFF_TUN
90 return (options.slicename, options.vif_name, vif_type, options.ip4_address,
91 options.net_prefix, options.snat, options.pointopoint,
92 options.txqueuelen, options.gre_key, options.gre_remote,
93 options.vif_name_file)
95 if __name__ == '__main__':
97 (slicename, vif_name, vif_type, ip4_address, net_prefix, snat, pointopoint,
98 txqueuelen, gre_key, gre_remote, vif_name_file) = get_options()
104 sliceid = pwd.getpwnam(slicename).pw_uid
106 if vif_type == vsys.IFF_TAP:
111 # if_name should be a unique numeric vif id
112 vif_name = "%s%s-%s" % (vif_prefix, sliceid, vif_name)
115 vsys.vif_up(vif_name, ip4_address, net_prefix, snat = snat,
116 pointopoint = pointopoint, txqueuelen = txqueuelen,
117 gre_key = gre_key, gre_remote = gre_remote)
119 except RuntimeError as e:
122 traceback.print_exc(file=sys.stderr)
125 if e.message.find("WARNING:") < 0:
128 # Saving interface name to vif_name_file
129 f = open(vif_name_file, 'w')