X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fnepi%2Fresources%2Fplanetlab%2Fscripts%2Fpl-vif-down.py;h=f766a379c755eb79c725cf658e7529da3241a7dc;hb=039fbd9629d7570d4c175a5448d24badcd0f3aba;hp=0047be4eb7a40e8e7e4fc7dba5cb8e5f98af5a88;hpb=4ec5c9e5454b68a3dab82a5073aee50231706706;p=nepi.git diff --git a/src/nepi/resources/planetlab/scripts/pl-vif-down.py b/src/nepi/resources/planetlab/scripts/pl-vif-down.py index 0047be4e..f766a379 100644 --- a/src/nepi/resources/planetlab/scripts/pl-vif-down.py +++ b/src/nepi/resources/planetlab/scripts/pl-vif-down.py @@ -3,9 +3,8 @@ # Copyright (C) 2013 INRIA # # This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation; # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -17,6 +16,8 @@ # # Author: Alina Quereilhac +from __future__ import print_function + import base64 import socket import vsys @@ -26,16 +27,25 @@ from optparse import OptionParser STOP_MSG = "STOP" def get_options(): - usage = ("usage: %prog -N -D -S ") + usage = ("usage: %prog -u -N -t " + "-D -S ") parser = OptionParser(usage = usage) + parser.add_option("-u", "--slicename", dest="slicename", + help = "The name of the PlanetLab slice ", + type="str") + parser.add_option("-N", "--vif-name", dest="vif_name", help = "The name of the virtual interface, or a " "unique numeric identifier to name the interface " "if GRE mode is used.", type="str") + parser.add_option("-t", "--vif-type", dest="vif_type", + help = "Virtual interface type. Either IFF_TAP or IFF_TUN. " + "Defaults to IFF_TAP. ", type="str") + parser.add_option("-D", "--delete", dest="delete", action="store_true", default = False, @@ -43,28 +53,52 @@ def get_options(): parser.add_option("-S", "--socket-name", dest="socket_name", help = "Name for the unix socket used to interact with this process", - default = "tap.sock", type="str") + type="str") (options, args) = parser.parse_args() - - return (options.vif_name, options.delete, options.socket_name) + + vif_type = vsys.IFF_TAP + if options.vif_type and options.vif_type == "IFF_TUN": + vif_type = vsys.IFF_TUN + + return (options.socket_name, options.vif_name, options.slicename, + vif_type, options.delete) if __name__ == '__main__': - (vif_name, delete, socket_name) = get_options() + (socket_name, vif_name, slicename, vif_type, delete) = get_options() # If a socket name is sent, send the STOP message and wait for a reply if socket_name: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - sock.connect(socket_name) - encoded = base64.b64encode(STOP_MSG) - sock.send("%s\n" % encoded) - reply = sock.recv(1024) - reply = base64.b64decode(reply) - print reply + try: + sock.connect(socket_name) + encoded = base64.b64encode(STOP_MSG) + sock.send("%s\n" % encoded) + reply = sock.recv(1024) + reply = base64.b64decode(reply) + print(reply) + except: + print("Did not properly shutdown device") + # If a slicename is provided, use it to remove a GRE device + elif slicename: + import pwd + import getpass + + sliceid = pwd.getpwnam(slicename).pw_uid + + if vif_type == vsys.IFF_TAP: + vif_prefix = "tap" + else: + vif_prefix = "tun" + + # if_name should be a unique numeric vif id + vif_name = "%s%s-%s" % (vif_prefix, sliceid, vif_name) + + vsys.vif_down(vif_name, delete = True) # Else, use the vsys interface to set the virtual interface down - elif vif_name: - vsys.vif_down(vif_name, delete = delete) + else: + vsys.vif_down(vif_name)