caac65ad8e1798ff052cd305cdc78870972e8863
[nepi.git] / src / nepi / resources / planetlab / scripts / pl-vif-down.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 base64
21 import socket
22 import vsys
23
24 from optparse import OptionParser
25
26 STOP_MSG = "STOP"
27
28 def get_options():
29     usage = ("usage: %prog -u <slicename> -N <vif-name> -t <vif-type> "
30             "-D <delete> -S <socket-name>")
31     
32     parser = OptionParser(usage = usage)
33
34     parser.add_option("-u", "--slicename", dest="slicename",
35         help = "The name of the PlanetLab slice ",
36         type="str")
37
38     parser.add_option("-N", "--vif-name", dest="vif_name",
39         help = "The name of the virtual interface, or a "
40                 "unique numeric identifier to name the interface "
41                 "if GRE mode is used.",
42         type="str")
43
44     parser.add_option("-t", "--vif-type", dest="vif_type",
45             help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
46             "Defaults to IFF_TAP. ", type="str")
47
48     parser.add_option("-D", "--delete", dest="delete", 
49             action="store_true", 
50             default = False,
51             help="Removes virtual interface if GRE mode was used")
52
53     parser.add_option("-S", "--socket-name", dest="socket_name",
54         help = "Name for the unix socket used to interact with this process", 
55         type="str")
56
57     (options, args) = parser.parse_args()
58    
59     vif_type = vsys.IFF_TAP
60     if options.vif_type and options.vif_type == "IFF_TUN":
61         vif_type = vsys.IFF_TUN
62
63     return (options.socket_name, options.vif_name, options.slicename, 
64             vif_type, options.delete)
65
66 if __name__ == '__main__':
67
68     (socket_name, vif_name, slicename, vif_type, delete) = get_options()
69
70     # If a socket name is sent, send the STOP message and wait for a reply
71     if socket_name:
72         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
73         sock.connect(socket_name)
74         encoded = base64.b64encode(STOP_MSG)
75         sock.send("%s\n" % encoded)
76         reply = sock.recv(1024)
77         reply = base64.b64decode(reply)
78         print reply
79
80     # If a slicename is provided, use it to remove a GRE device
81     elif slicename:
82         import pwd
83         import getpass
84
85         sliceid = pwd.getpwnam(slicename).pw_uid
86
87         if vif_type == vsys.IFF_TAP:
88             vif_prefix = "tap"
89         else:
90             vif_prefix = "tun"
91
92         # if_name should be a unique numeric vif id
93         vif_name = "%s%s-%s" % (vif_prefix, sliceid, vif_name) 
94
95         vsys.vif_down(vif_name, delete = True)
96
97     # Else, use the vsys interface to set the virtual interface down
98     else:
99         vsys.vif_down(vif_name)
100
101