Omitting explicit setting of pointopoint attribute for PlanetLab TAP/TUN devices
[nepi.git] / src / nepi / resources / planetlab / scripts / pl-vif-up.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 vsys
21
22 from optparse import OptionParser
23
24 def get_options():
25     usage = ("usage: %prog -N <vif-name> -t <vif-type> -a <ip4-address> "
26                     "-n <net-prefix> -s <snat> -p <pointopoint> "
27                     "-q <txqueuelen> -g <gre_key> -G <gre_remote> ")
28  
29     parser = OptionParser(usage = usage)
30
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.",
35         type="str")
36
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")
40
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. ",
44             type="str")
45
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. ",
49             type="int")
50
51     parser.add_option("-s", "--snat", dest="snat", 
52             action="store_true", 
53             default = False,
54             help="Enable SNAT for the interface")
55
56     parser.add_option("-p", "--pointopoint", dest="pointopoint",
57             help = "Peer end point for the interface. ", 
58             default = None,
59             type="str")
60
61     parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
62         help = "Size of transmision queue. Defaults to 0.",
63         default = 0,
64         type="int")
65
66     parser.add_option("-g", "--gre-key", dest="gre_key",
67         help = "When set, enables GRE mode with the corresponding GRE key.", 
68         default = None,
69         type="str")
70
71     parser.add_option("-G", "--gre-remote", dest="gre_remote",
72         help = "Remote endpoint (public IP) for the GRE tunnel.", 
73         default = None,
74         type="str")
75
76     (options, args) = parser.parse_args()
77     
78     vif_type = vsys.IFF_TAP
79     if options.vif_type and options.vif_type == "IFF_TUN":
80         vif_type = vsys.IFF_TUN
81
82     return (options.vif_name, vif_type, options.ip4_address, 
83             options.net_prefix, options.snat, options.pointopoint, 
84             options.txqueuelen, options.gre_key, options.gre_remote)
85
86 if __name__ == '__main__':
87
88     (vif_name, vif_type, ip4_address, net_prefix, snat, pointopoint,
89         txqueuelen, gre_key, gre_remote) = get_options()
90
91     if (gre_key):
92         import pwd
93         import getpass
94
95         slicename = getpass.getuser()
96         sliceid = pwd.getpwnam(slicename).pw_uid
97
98         if vif_type == vsys.IFF_TAP:
99             vif_prefix = "tap"
100         else:
101             vif_prefix = "tun"
102
103         # if_name should be a unique numeric vif id
104         vif_name = "%s%s-%d" % (vif_prefix, sliceid, vif_name) 
105
106     vsys.vif_up(vif_name, ip4_address, net_prefix, snat = snat, 
107             pointopoint = pointopoint, txqueuelen = txqueuelen, 
108             gre_key = gre_key, gre_remote = gre_remote)
109