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 -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("-N", "--vif-name", dest="vif_name",
32 help = "The name of the virtual interface, or a "
33 "unique numeric identifier to name the interface "
34 "if GRE mode is used.",
37 parser.add_option("-t", "--vif-type", dest="vif_type",
38 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
39 "Defaults to IFF_TAP. ", type="str")
41 parser.add_option("-a", "--ip4-address", dest="ip4_address",
42 help = "IPv4 address to assign to interface. It must belong to the "
43 "network segment owned by the slice, given by the vsys_vnet tag. ",
46 parser.add_option("-n", "--net-prefix", dest="net_prefix",
47 help = "IPv4 network prefix for the interface. It must be the one "
48 "given by the slice's vsys_vnet tag. ",
51 parser.add_option("-s", "--snat", dest="snat",
54 help="Enable SNAT for the interface")
56 parser.add_option("-p", "--pointopoint", dest="pointopoint",
57 help = "Peer end point for the interface. ",
61 parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
62 help = "Size of transmision queue. Defaults to 0.",
66 parser.add_option("-g", "--gre-key", dest="gre_key",
67 help = "When set, enables GRE mode with the corresponding GRE key.",
71 parser.add_option("-G", "--gre-remote", dest="gre_remote",
72 help = "Remote endpoint (public IP) for the GRE tunnel.",
76 parser.add_option("-f", "--vif-name-file", dest="vif_name_file",
77 help = "File to store the virtual interface name assigned by the OS",
78 default = "vif_name", type="str")
80 (options, args) = parser.parse_args()
82 vif_type = vsys.IFF_TAP
83 if options.vif_type and options.vif_type == "IFF_TUN":
84 vif_type = vsys.IFF_TUN
86 return (options.vif_name, vif_type, options.ip4_address,
87 options.net_prefix, options.snat, options.pointopoint,
88 options.txqueuelen, options.gre_key, options.gre_remote,
89 options.vif_name_file)
91 if __name__ == '__main__':
93 (vif_name, vif_type, ip4_address, net_prefix, snat, pointopoint,
94 txqueuelen, gre_key, gre_remote, vif_name_file) = get_options()
100 slicename = getpass.getuser()
101 sliceid = pwd.getpwnam(slicename).pw_uid
103 if vif_type == vsys.IFF_TAP:
108 # if_name should be a unique numeric vif id
109 vif_name = "%s%s-%d" % (vif_prefix, sliceid, vif_name)
111 vsys.vif_up(vif_name, ip4_address, net_prefix, snat = snat,
112 pointopoint = pointopoint, txqueuelen = txqueuelen,
113 gre_key = gre_key, gre_remote = gre_remote)
115 # Saving interface name to vif_name_file
116 f = open(vif_name_file, 'w')