d37a4eef36e6613ca008f9275b09ce81ec1131a7
[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 -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> ")
28  
29     parser = OptionParser(usage = usage)
30
31     parser.add_option("-u", "--slicename", dest="slicename",
32         help = "The name of the PlanetLab slice ",
33         type="str")
34
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.",
39         type="str")
40
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")
44
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. ",
48             type="str")
49
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. ",
53             type="int")
54
55     parser.add_option("-s", "--snat", dest="snat", 
56             action="store_true", 
57             default = False,
58             help="Enable SNAT for the interface")
59
60     parser.add_option("-p", "--pointopoint", dest="pointopoint",
61             help = "Peer end point for the interface. ", 
62             default = None,
63             type="str")
64
65     parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
66         help = "Size of transmision queue. Defaults to 0.",
67         default = 0,
68         type="int")
69
70     parser.add_option("-g", "--gre-key", dest="gre_key",
71         help = "When set, enables GRE mode with the corresponding GRE key.", 
72         default = None,
73         type="str")
74
75     parser.add_option("-G", "--gre-remote", dest="gre_remote",
76         help = "Remote endpoint (public IP) for the GRE tunnel.", 
77         default = None,
78         type="str")
79
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")
83
84     (options, args) = parser.parse_args()
85     
86     vif_type = vsys.IFF_TAP
87     if options.vif_type and options.vif_type == "IFF_TUN":
88         vif_type = vsys.IFF_TUN
89
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)
94
95 if __name__ == '__main__':
96
97     (slicename, vif_name, vif_type, ip4_address, net_prefix, snat, pointopoint,
98         txqueuelen, gre_key, gre_remote, vif_name_file) = get_options()
99
100     if (gre_key):
101         import pwd
102         import getpass
103
104         sliceid = pwd.getpwnam(slicename).pw_uid
105
106         if vif_type == vsys.IFF_TAP:
107             vif_prefix = "tap"
108         else:
109             vif_prefix = "tun"
110
111         # if_name should be a unique numeric vif id
112         vif_name = "%s%s-%s" % (vif_prefix, sliceid, vif_name) 
113
114     try:
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)
118
119     except RuntimeError as e:
120         import sys
121         import traceback
122         traceback.print_exc(file=sys.stderr)
123
124         # Ignore warnings
125         if e.message.find("WARNING:") < 0:
126             sys.exit(1)
127
128     # Saving interface name to vif_name_file
129     f = open(vif_name_file, 'w')
130     f.write(vif_name)
131     f.close()
132
133