addspecialiface.in 8.74 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35 36
#
use strict;
use English;
use Getopt::Std;
use Date::Parse;
use Time::Local;
use Data::Dumper;
use File::Temp qw(tempfile);

#
# Add a generic device to the DB. 
#
sub usage()
{
37
    print STDERR "Usage: addspecialiface -b <speed> -s <switchinfo> ".
38
	"<node_id> <iface_id>\n";
Leigh Stoller's avatar
Leigh Stoller committed
39
    print STDERR "Usage: addspecialiface [-t] -r <node_id> <iface_id>\n";
40 41 42 43 44
    print STDERR "Options:\n";
    print STDERR " -d       - Turn on debugging\n";
    print STDERR " -n       - Dry run mode\n";
    print STDERR " -t       - Do not call snmpit to set trunk mode\n";
    print STDERR " -r       - Delete interface\n";
45
    print STDERR " -f       - (with -r) force deletion, even if iface used\n";
46
    print STDERR "          - (with -t) force, no interface_state update\n";
Leigh Stoller's avatar
Leigh Stoller committed
47
    print STDERR " -b speed - Interface speed; 100Mb,1Gb,10Gb,40Gb,100Gb\n";
48 49 50 51
    print STDERR " -s info  - switchname,switchcard,switchport\n";
    print STDERR " iface_id - eth0, eth1, etc (must end in an integer)\n";
    exit(-1);
}
52
my $optlist   = "b:dfns:rt";
53 54 55
my $debug     = 0;
my $impotent  = 0;
my $removing  = 0;
56
my $force     = 0;
57 58 59
my $notrunk   = 0;
my $speed;
my $switchinfo;
60
my ($interface1,$interface2);
61 62 63 64 65 66 67 68

# Protos
sub fatal($);

#
# Configure variables
#
my $TB          = "@prefix@";
69
my $SNMPIT      = "$TB/bin/snmpit";
70 71 72 73 74 75 76 77 78 79 80 81

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use EmulabConstants;
use emutil;
use User;
use Node;
use NodeType;
use OSinfo;
82
use Lan;
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
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{'d'})) {
    $debug = 1;
}
if (defined($options{'n'})) {
    $impotent = 1;
}
if (defined($options{'r'})) {
    $removing = 1;
}
114 115 116
if (defined($options{'f'})) {
    $force = 1;
}
117 118 119 120 121 122
if (defined($options{'t'})) {
    $notrunk = 1;
}
if (defined($options{'b'})) {
    $speed = $options{'b'};
    usage()
Leigh Stoller's avatar
Leigh Stoller committed
123
	if ($speed ne "1Gb" && $speed ne "100Mb" && $speed ne "10Gb" &&
Leigh Stoller's avatar
Leigh Stoller committed
124
	    $speed ne "40Gb" && $speed ne "100Gb");
125 126 127 128 129
}
if (defined($options{'s'})) {
    $switchinfo = $options{"s"};
}
usage()
130
    if (@ARGV != 2 || (!$removing && !defined($switchinfo)));
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

my $node_id  = $ARGV[0];
my $iface_id = $ARGV[1];

#
# 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!");
}
my $node = Node->Lookup($node_id);
if (!defined($node)) {
    fatal("Node does not exist in the DB")
}
my $interface = Interface->LookupByIface($node, $iface_id);
if ($removing) {
    fatal("Node does not have an interface named $iface_id")
	if (!defined($interface));
}
else {
    fatal("Node already has an interface named $iface_id")
	if (defined($interface));
}
if ($removing) {
159 160 161 162 163 164 165 166
    my $otheriface;

    if (Lan->FindLansByMember($node, $interface->iface())) {
	if ($force) {
	    print "$interface appears to be in use; continuing anyway...\n";
	}
	else {
	    print "warning: $interface appears to be in use; " .
167 168 169
		"entering impotent mode (use -f to force removal)\n";
	    $impotent = 1;
	}
170
    }
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    
    #
    # Find the wire so we can delete the other interface too.
    #
    my $wire = Interface::Wire->Lookup($interface);
    if (defined($wire)) {
	$otheriface = Interface->LookupByIface($wire->node_id2(), $wire->iface2());

	if (Lan->FindLansByMember($otheriface->node_id(), $otheriface->iface())) {
	    if ($force) {
		print "$interface appears to be in use; continuing anyway...\n";
	    }
	    else {
		print "warning: $interface appears to be in use; " .
		    "entering impotent mode (use -f to force removal)\n";
		$impotent = 1;
	    }
	}
    }
    if (!$impotent) {
191 192 193 194 195 196 197 198
	if ($node->IsReserved() &&
	    $node->erole() eq "sharedhost" && !$notrunk) {
	    print "Turning off trunk mode for $node_id:$iface_id\n";
	    system("$SNMPIT -U $node_id:$iface_id") == 0
		or fatal("Could not turn off port trunking mode");
	}
	$interface->DeleteWire() == 0
	    or fatal("Could not delete wire for $interface");
199

200 201 202
	# Flag indicates it is okay to delete real interface.
	$interface->Delete(1) == 0
	    or fatal("Could not delete $interface");
203 204 205 206
	if ($otheriface) {
	    $otheriface->Delete(1) == 0
		or fatal("Could not delete $otheriface");
	}
207
    }
208 209 210 211 212 213 214 215 216

    exit(0);
}

# Verify switch info
my $nodecard;
my $switchid;
my $switchcard;
my $switchport;
217
my $switchiface;
218

219 220 221 222 223 224 225 226 227 228
if (!$impotent && $node->IsReserved() &&
    $node->erole() eq "sharedhost" && $notrunk && !$force) {
    print STDERR
	"Using -t will create an inconsistency in the interface_state table!\n".
	"The next time snmpit runs on the port, it might try to put the port\n".
	"into trunk mode, which might remove all existing vlans on the port.\n".
	"And that might be VERY BAD! Use the force (-f) option.\n";
    exit(1);
}

229 230 231 232 233 234 235 236 237
#
# Derive a card number form the iface number. 
#
if ($iface_id =~ /^[^\d]*(\d*)$/) {
    $nodecard = $1;
}
else {
    fatal("iface_id is not in the proper format");
}
238 239 240 241 242 243 244
if (Interface->LookupByIface($node, $iface_id)) {
    fatal("Node already has an interface with iface=$iface_id");
}
# But also need to look for any thing using this card,port;
my $wire = Interface::Wire->LookupAny($node, $nodecard, 1);
if (defined($wire)) {
    fatal("Node $node_id already has a wire: $wire");
245 246 247 248 249
}
if ($switchinfo =~ /^([-\w]+),(\d+),(\d+)$/) {
    $switchid   = $1;
    $switchcard = $2;
    $switchport = $3;
250
    $switchiface= $switchcard . "/" . $switchport;
251 252 253 254 255 256 257 258
}
else {
    fatal("Invalid switch info");
}
my $switch = Node->Lookup($switchid);
if (!defined($switch)) {
    fatal("Switch $switchid does not exist");
}
259
if (Interface->LookupByIface($node, $switchiface)) {
260
    fatal("Switch $switchid already has an interface with ".
261
	  "iface=$switchiface");
262
}
263 264
# But also need to look for any thing using this card,port;
$wire = Interface::Wire->LookupAny($switchid, $switchcard, $switchport);
265 266 267 268
if (defined($wire)) {
    fatal("Switch $switchid already using this interface: $wire");
}

269
#
270
# Add the interfaces.
271
#
272 273 274 275 276 277 278
my $type = Interface::Type->GenericType($speed);
if (!defined($type)) {
    # default is 1G for backward compatibility
    $type = Interface::Type->Lookup("generic_1G");
    if (!defined($type)) {
	fatal("Could not find a generic type for $speed");
    }
279
}
280 281 282
my $ifaceargs1 = {
    "card"        => $switchcard,
    "port"        => $switchport,
283
    "iface"       => $switchiface,
284
    "role"        => TBDB_IFACEROLE_OTHER(),
285 286
    "type"        => $type->type(),
    "max_speed"   => $type->max_speed(),
287 288 289 290 291 292 293 294 295 296
    "mac"         => "000000000000",
};
print "Creating switch interface with arguments:\n";
print Dumper($ifaceargs1);
if (!$impotent) {
    $interface2 = Interface->Create($switch, $ifaceargs1)
	or fatal("Could not create interface entry for switch");
}

my $ifaceargs2 = {
297 298 299
    "card"        => $nodecard,
    "iface"       => $iface_id,
    "role"        => TBDB_IFACEROLE_EXPERIMENT(),
300 301
    "type"        => $type->type(),
    "max_speed"   => $type->max_speed(),
302 303
    "trunk"       => ($node->IsReserved() &&
		      $node->erole() eq "sharedhost" ? 1 : 0),
304 305 306
    "mac"         => "000000000000",
};
print "Creating interface with arguments:\n";
307
print Dumper($ifaceargs2);
308
if (!$impotent) {
309
    $interface1 = Interface->Create($node, $ifaceargs2)
310
	or fatal("Could not create interface entry");
311 312
}
if (!$impotent) {
313
    my $wireargs = {
314 315 316 317
	"card1"  => $nodecard,
	"port1"  => 1,
	"card2"  => $switchcard,
	"port2"  => $switchport,
318
    };
319
    print "Creating wire for $interface1,$interface2:\n";
320
    $wire = Interface::Wire->Create($interface1, $interface2, 'Node', $wireargs)
321 322 323 324 325 326 327 328
	or fatal("Could not create wire");
}
if (!$impotent &&
    $node->IsReserved() &&
    $node->erole() eq "sharedhost" && !$notrunk) {
    print "Setting $node_id:$iface_id to trunking mode\n";
    system("$SNMPIT -E $node_id:$iface_id") == 0
	or fatal("Could not put port into trunking mode");
329 330 331 332 333 334 335 336 337 338 339 340
}
exit(0);

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

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