addinterface.in 6.75 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
#!/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] ".
36
	"[-r role] node_id iface_id\n";
37
    print STDERR "Usage: addinterface -r <node_id> <iface_id>\n";
38
    print STDERR "Usage: addinterface -i <node_id> <iface_id>\n";
39 40 41
    print STDERR "Options:\n";
    print STDERR " -v        - Turn on debugging\n";
    print STDERR " -n        - Impotent mode\n";
42
    print STDERR " -i        - Display management interface from DB.\n";
43 44 45 46 47 48
    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";
49
    print STDERR " iface_id  - eth0, eth1, 1/69, 1.6:1, Po4.5/9:1, etc.\n";
50 51
    exit(-1);
}
52
my $optlist   = "b:t:nvfm:re:i";
53 54 55
my $verbose   = 0;
my $impotent  = 0;
my $remove    = 0;
56
my $info      = 0;
57 58 59 60 61 62 63 64 65 66
my $force     = 0;
my $speed;
my $type;
my $mac       = "000000000000";
my $role;
my ($card,$port);

# Protos
sub fatal($);
sub RemoveInterface($$);
67
sub ShowInterface($$);
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

#
# 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);
}
117 118 119 120 121
if (defined($options{'i'})) {
    $info = 1;
    usage()
	if (@ARGV != 2);
}
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
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]));
}
167 168 169
elsif ($info) {
    exit(ShowInterface($ARGV[0], $ARGV[1]));
}
170 171

usage()
172
    if (@ARGV != 2 || !defined($type));
173

174 175
my $node_id   = $ARGV[0];
my $iface_id  = $ARGV[1];
176 177 178 179 180 181

# Sanity check node/portinfo.
my $node = Node->Lookup($node_id);
if (!defined($node)) {
    fatal("No such node $node_id");
}
182
my $interface = Interface->LookupByIface($node_id, $iface_id);
183
if (defined($interface)) {
184
    fatal("Interface already exists for $iface_id");
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
}
#
# Create the interface.
#
my $ifaceargs = {
    "node_id"     => $node_id,
    "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;
}

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 287 288 289 290
#
# Show interface.
#
sub ShowInterface($$)
{
    my ($nodeid, $iface) = @_;

    my $interface = Interface->LookupByIface($nodeid, $iface);
    fatal("No such interface")
	if (!defined($interface));
    print "Iface1:\n";
    $interface->Dump();
    print "\n";

    if ($interface->wiredup()) {
	my $wire = $interface->wire();
	my $switch_interface = Interface->LookupByIface($wire->node_id2(),
							$wire->iface2());
	if (defined($switch_interface)) {
	    print "Iface2:\n";
	    $switch_interface->Dump();
	    print "\n";
	}
	print "Wire:\n";
	$wire->Dump();
    }
    return 0;
}