addinterface.in 7.1 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Leigh Stoller's avatar
Leigh Stoller committed
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 23 24 25 26 27 28 29 30 31 32 33 34 35
# 
# {{{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] ".
Leigh Stoller's avatar
Leigh Stoller committed
36
	"[-e 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";
Leigh Stoller's avatar
Leigh Stoller committed
42
    print STDERR " -i        - Display interface info from DB.\n";
43 44
    print STDERR " -r        - Delete interface\n";
    print STDERR " -f        - (with -r) force deletion, even if iface used\n";
45
    print STDERR " -I IP     - Optional IP address (as for control iface)\n";
46
    print STDERR " -M Mask   - IP Mask, defaults to 255.255.255.0\n";
47 48 49 50
    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";
51
    print STDERR " iface_id  - eth0, eth1, 1/69, 1.6:1, Po4.5/9:1, etc.\n";
52 53
    exit(-1);
}
54
my $optlist   = "b:t:nvfm:re:iI:M:";
55 56 57
my $verbose   = 0;
my $impotent  = 0;
my $remove    = 0;
58
my $info      = 0;
59
my $force     = 0;
60
my $IP;
61
my $MASK;
62 63 64 65 66 67 68 69
my $speed;
my $type;
my $mac       = "000000000000";
my $role;
my ($card,$port);

# Protos
sub fatal($);
Leigh Stoller's avatar
Leigh Stoller committed
70 71
sub RemoveInterface(@);
sub ShowInterface(@);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

#
# 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;
Leigh Stoller's avatar
Leigh Stoller committed
89
use Lan;
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

#
# 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;
}
120 121 122
if (defined($options{'i'})) {
    $info = 1;
}
123 124 125 126 127 128 129 130 131 132 133 134
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();
}
135 136
if (defined($options{'I'})) {
    $IP = $options{'I'};
Leigh Stoller's avatar
Leigh Stoller committed
137
    if ($IP !~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)  {
138 139 140
	fatal("Invalid IP format");
    }
}
141 142 143 144 145 146
if (defined($options{'M'})) {
    $MASK = $options{'M'};
    if ($MASK !~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)  {
	fatal("Invalid IP mask format");
    }
}
147 148
if (defined($options{'m'})) {
    $mac = $options{'m'};
149
    if ($mac !~ /^\w\w\w\w\w\w\w\w\w\w\w\w$/)  {
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
	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) {
Leigh Stoller's avatar
Leigh Stoller committed
178
    exit(RemoveInterface(@ARGV));
179
}
180
elsif ($info) {
Leigh Stoller's avatar
Leigh Stoller committed
181
    exit(ShowInterface(@ARGV));
182
}
183 184

usage()
185
    if (@ARGV != 2 || !defined($type));
186

187 188
my $node_id   = $ARGV[0];
my $iface_id  = $ARGV[1];
189 190 191 192 193 194

# Sanity check node/portinfo.
my $node = Node->Lookup($node_id);
if (!defined($node)) {
    fatal("No such node $node_id");
}
195
my $interface = Interface->LookupByIface($node_id, $iface_id);
196
if (defined($interface)) {
197
    fatal("Interface already exists for $iface_id");
198 199 200 201 202 203 204 205 206 207 208
}
#
# 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,
209
    "IP"          => $IP,
210
    "mask"        => $MASK,
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
};
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. 
#
Leigh Stoller's avatar
Leigh Stoller committed
238
sub RemoveInterface(@)
239
{
Leigh Stoller's avatar
Leigh Stoller committed
240
    my ($nodeid, $iface) = @_;
241

Leigh Stoller's avatar
Leigh Stoller committed
242 243 244 245 246 247 248 249 250 251
    if (!defined($iface)) {
	if ($nodeid =~ /^([^:]+):([-\w\/\/:]+)$/) {
	    $nodeid = $1;
	    $iface  = $2;
	}
	else {
	    usage();
	}
    }
    my $interface = Interface->LookupByIface($nodeid, $iface);
252 253 254 255 256 257 258
    if (!defined($interface)) {
	fatal("No such interface to delete");
    }

    #
    # See if in use. 
    #
Leigh Stoller's avatar
Leigh Stoller committed
259
    if (Lan->FindLansByMember($nodeid, $iface)) {
260
	if ($force) {
Leigh Stoller's avatar
Leigh Stoller committed
261
	    print "$nodeid:$iface appears to be in use; " .
262 263 264
		"continuing anyway...\n";
	}
	else {
Leigh Stoller's avatar
Leigh Stoller committed
265
	    print "warning: $nodeid:$iface appears to be in use; " .
266 267 268 269 270 271 272 273 274 275 276 277
		"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;
}

278 279 280
#
# Show interface.
#
Leigh Stoller's avatar
Leigh Stoller committed
281
sub ShowInterface(@)
282 283 284
{
    my ($nodeid, $iface) = @_;

Leigh Stoller's avatar
Leigh Stoller committed
285 286 287 288 289 290 291 292 293
    if (!defined($iface)) {
	if ($nodeid =~ /^([^:]+):([-\w\/\/:]+)$/) {
	    $nodeid = $1;
	    $iface  = $2;
	}
	else {
	    usage();
	}
    }
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    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;
}