addinterface.in 6.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
#!/usr/bin/perl -w
#
# Copyright (c) 2003-2017 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Data::Dumper;

#
# Add an interface to the DB. Typically, this is used to add the switch
# side of a wire, but it can be used to add the node side too.a
#
sub usage()
{
    print STDERR "Usage: addinterface [-b speed | -t ifacetype] [-m mac] ".
	"[-r role] portinfo node_id [iface_id]\n";
    print STDERR "Usage: addinterface -r <node_id> <iface_id>\n";
    print STDERR "Options:\n";
    print STDERR " -v        - Turn on debugging\n";
    print STDERR " -n        - Impotent mode\n";
    print STDERR " -r        - Delete interface\n";
    print STDERR " -f        - (with -r) force deletion, even if iface used\n";
    print STDERR " -e role   - Optional interface role, default to 'other'\n";
    print STDERR " -m mac    - Optional MAC address. 0123456789AB format.\n";
    print STDERR " -b speed  - Interface speed; 100Mb,1Gb,10Gb,40Gb,100Gb\n";
    print STDERR " -t type   - Interface type instead of generic based on speed\n";
    print STDERR " portinfo  - card,port (for now, might change later)\n";
    print STDERR " iface_id  - eth0, eth1, 1/69, 1/6/9. If you do not specify,\n";
    print STDERR "             one will be constructed for you (ethXX).\n";
	
    exit(-1);
}
my $optlist   = "b:t:nvfm:re:";
my $verbose   = 0;
my $impotent  = 0;
my $remove    = 0;
my $force     = 0;
my $speed;
my $type;
my $mac       = "000000000000";
my $role;
my ($card,$port);

# Protos
sub fatal($);
sub RemoveInterface($$);

#
# Configure variables
#
my $TB          = "@prefix@";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use EmulabConstants;
use emutil;
use User;
use Node;
use NodeType;
use Interface;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# Parse command arguments. 
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'h'})) {
    usage();
}
if (defined($options{'v'})) {
    $verbose = 1;
}
if (defined($options{'n'})) {
    $impotent = 1;
}
if (defined($options{'r'})) {
    $remove = 1;
    usage()
	if (@ARGV != 2);
}
if (defined($options{'f'})) {
    $force = 1;
}
if (defined($options{'e'})) {
    $role = $options{'e'};
    if ($role !~ /^(expt|ctrl|other|mngmnt)$/) {
	fatal("Role must be one of: expt|ctrl|other|mngmnt");
    }
}
else {
    $role = TBDB_IFACEROLE_EXPERIMENT();
}
if (defined($options{'m'})) {
    $mac = $options{'m'};
    if ($mac !~ /^\w\w\w\w\w\w\w\w\w\w\w\w\w/)  {
	fatal("Invalid mac address format");
    }
}
if (defined($options{'t'})) {
    $type = Interface::Type->Lookup($options{'t'});
    if (!defined($type)) {
	fatal("No such interface type");
    }
}
if (defined($options{'b'})) {
    $speed = $options{'b'};
    $type = Interface::Type->GenericType($speed);
    if (!defined($type)) {
	fatal("No generic interface type for speed: $speed");
    }
}

#
# Verify user, must be admin.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}
if (!$this_user->IsAdmin()) {
    fatal("You are not a testbed administrator!");
}
if ($remove) {
    exit(RemoveInterface($ARGV[0], $ARGV[1]));
}

usage()
    if (@ARGV < 2 || !defined($type));

my $portinfo  = $ARGV[0];
my $node_id   = $ARGV[1];
my $iface_id  = $ARGV[2] if (@ARGV == 3);

# Sanity check node/portinfo.
my $node = Node->Lookup($node_id);
if (!defined($node)) {
    fatal("No such node $node_id");
}
if ($portinfo =~ /^(\d+),(\d+)$/) {
    $card = $1;
    $port = $2;
}
else {
    fatal("Invalid portinfo; must be in card,port format");
}
my $interface = Interface->Lookup($node_id, $card, $port);
if (defined($interface)) {
    fatal("Interface already exists for $card,$port");
}
if (defined($iface_id)) {
    $interface = Interface->LookupByIface($node_id, $iface_id);
    if (defined($interface)) {
	fatal("Interface already exists for $iface_id");
    }
}
else {
    if ($node->role() eq "testnode") {
	if ($node->isswitch()) {
	    $iface_id = sprintf("eth%02d%02d", $card, $port);
	}
	else {
	    $iface_id = "eth${card}";
	}
    }
    else {
	$iface_id = "${card}/${port}";
    }
    print "Will use iface: $iface_id\n";
    $interface = Interface->LookupByIface($node_id, $iface_id);
    if (defined($interface)) {
	fatal("Interface already exists for generated $iface_id");
    }
}
#
# Create the interface.
#
my $ifaceargs = {
    "node_id"     => $node_id,
    "card"        => $card,
    "port"	  => $port,
    "iface"       => $iface_id,
    "role"        => $role,
    "type"        => $type->type(),
    "max_speed"   => $type->max_speed() / 1000,
    "mac"         => $mac,
};
if ($verbose || $impotent) {
    if ($verbose) {
	print "Creating interface with arguments:\n";
    }
    else {
	print "Would create interface with arguments:\n";
    }
    print Dumper($ifaceargs);
}
if (!$impotent) {
    Interface->Create($node, $ifaceargs)
	or fatal("Could not create interface entry");
}
exit(0);

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}

#
# Remove an interface. 
#
sub RemoveInterface($$)
{
    my ($node_id, $iface_id) = @_;

    my $interface = Interface->LookupByIface($node_id, $iface_id);
    if (!defined($interface)) {
	fatal("No such interface to delete");
    }

    #
    # See if in use. 
    #
    my $query_result =
	DBQueryFatal("select lma1.lanid from lan_member_attributes as lma1, " .
		     "lan_member_attributes as lma2 where " .
		     "lma1.lanid = lma2.lanid and lma1.memberid=lma2.memberid " .
		     "and lma1.attrkey='node_id' and " .
		     "lma1.attrvalue='$node_id' and " .
		     "lma2.attrkey='iface' and " .
		     "lma2.attrvalue='$iface_id'");
    if ($query_result->numrows > 0) {
	if ($force) {
	    print "$node_id:$iface_id appears to be in use; " .
		"continuing anyway...\n";
	}
	else {
	    print "warning: $node_id:$iface_id appears to be in use; " .
		"entering impotent mode (use -f to force removal)\n";
	    $impotent = 1;
	}
    }
    if (!$impotent) {
	# Flag indicates it is okay to delete real interface. Safety measure.
	$interface->Delete(1) == 0
	    or fatal("Could not delete $interface");
    }
    return 0;
}