Added GRE functionallity to PlanetLabTAP
[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> -q <txqueuelen> "
27                     "-g <gre_key> -G <gre_remote> -f <vif-name-file> ")
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     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")
79
80     (options, args) = parser.parse_args()
81     
82     vif_type = vsys.IFF_TAP
83     if options.vif_type and options.vif_type == "IFF_TUN":
84         vif_type = vsys.IFF_TUN
85
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)
90
91 if __name__ == '__main__':
92
93     (vif_name, vif_type, ip4_address, net_prefix, snat, pointopoint,
94         txqueuelen, gre_key, gre_remote, vif_name_file) = get_options()
95
96     if (gre_key):
97         import pwd
98         import getpass
99
100         slicename = getpass.getuser()
101         sliceid = pwd.getpwnam(slicename).pw_uid
102
103         if vif_type == vsys.IFF_TAP:
104             vif_prefix = "tap"
105         else:
106             vif_prefix = "tun"
107
108         # if_name should be a unique numeric vif id
109         vif_name = "%s%s-%d" % (vif_prefix, sliceid, vif_name) 
110
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)
114
115     # Saving interface name to vif_name_file
116     f = open(vif_name_file, 'w')
117     f.write(vif_name)
118     f.close()
119
120