staticroutes.in 10.2 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4 5 6 7 8

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

9 10 11 12 13 14 15 16 17 18
use English;
use Getopt::Std;
use Socket;
use IO::Handle;     # thousands of lines just for autoflush :-(

#
# usage: staticroutes <pid> <eid>
#
sub usage()
{
19
    print("Usage: staticroutes [-n [-s|-l|-b]] [-d] [-f] [-t] <pid> <eid>\n".
20
	  "       Use -n to print routes, but leave the DB alone.\n".
21 22 23
	  "       Use -s (with -n) to print routes in NS format.\n".
	  "       Use -b (with -n) to print routes in Freebsd format.\n".
	  "       Use -l (with -n) to print routes in Linux format.\n".
24 25 26 27 28
	  "       Use -d to turn on debugging output.\n".
	  "       Use -t to optimize network routes.\n".
	  "       Use -f to force route calculation; ignore 'static' flag.\n");
    exit(-1);
}
29
my  $optlist = "dnftslb";
30 31 32 33 34 35 36 37 38 39 40

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

my $debug       = 0;
my $routecalc   = "$TB/libexec/routecalc";
my $impotent    = 0;
my $force	= 0;
41
my $optimize    = 1;
42
my $format      = "";
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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 2) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"f"})) {
    $force = 1;
}
if (defined($options{"t"})) {
81
    $optimize = 0;
82
}
83 84 85 86 87 88 89 90 91 92 93 94
if (defined($options{"s"})) {
    $format = "ns";
}
if (defined($options{"b"})) {
    $format = "bsd";
}
if (defined($options{"l"})) {
    $format = "suxs";
}
if ($format && !$impotent) {
    usage();
}
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

my $pid = $ARGV[0];
my $eid = $ARGV[1];

#
# Untaint args.
#
if ($pid =~ /^([-\@\w]+)$/) {
    $pid = $1;
}
else {
    die("Bad data in pid: $pid.");
}
if ($eid =~ /^([-\@\w]+)$/) {
    $eid = $1;
}
else {
    die("Bad data in eid: $eid.");
}

#
# Get the list of nodes.
# 
my $query_result =
    DBQueryFatal("select vname,ips,routertype from virt_nodes ".
		 "where pid='$pid' and eid='$eid'");

if (!$query_result->numrows) {
    warn("*** $0:\n".
	 "    No nodes in experiment $pid/$eid!\n");
    exit(0);
}

# A Flag.
my $dostatic = 0;

# A map of ips for each node, indexed by vname:port. 
my %ips      = ();

# A map (and reverse map) of node (vname) to unique index.
my %map      = ();
my %rmap     = ();
my $index    = 0;

# A map of lans, where each is a list of the members, indexed by lan.
my %lans     = ();

142
# A reverse map (sorta). Map a pair of links back to the lan they belong to.
143 144
my %rlans    = ();

145
# The costs, indexed by lan. This is kinda bogus, but since the cost
146 147 148
# is the same for each member of a lan, no big deal.
my %costs    = ();

149 150 151 152 153 154
# Number of links per node, indexed by node (vname). This is strictly
# for generating NS style output. When there is more than one link on
# a node, we *must* generate net routes instead of host routes, or the
# parser will be unhappy.
my %linkcount= ();

155 156 157 158 159 160 161 162 163 164
#
# Convert list of nodes into a map of $vname:$port to IP. We will need the
# IPs later when filling in the virt_routes table.
# 
while (my ($vname,$ips,$routertype) = $query_result->fetchrow_array) {
    if (!$force && $routertype ne "static") {
	next;
    }
    $dostatic++;
    
165 166
    $linkcount{$vname} = 0;
    
167 168 169 170 171 172 173 174
    foreach my $ipinfo (split(" ", $ips)) {
	my ($port,$ip) = split(":", $ipinfo);
	$ips{"$vname:$port"} = $ip;
	if (! defined($map{$vname})) {
	    $map{$vname}  = $index;
	    $rmap{$index} = $vname;
	    $index++;
	}
175
	$linkcount{$vname} += 1;
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    }
}

if (! $dostatic) {
    warn("No nodes requested static routing in $pid/$eid. This is okay!\n");
    exit(0);
}

#
# Now get the lans. 
#
$query_result =
    DBQueryFatal("select vname,member,cost " .
		 "from virt_lans where pid='$pid' and eid='$eid'");
if (!$query_result->numrows) {
    warn("*** $0:\n".
	 "    No links or lans in experiment $pid/$eid!\n");
    exit(0);
}

while (my ($vname,$member,$cost) = $query_result->fetchrow_array) {
    if (! defined($lans{$vname})) {
	$lans{$vname} = [];
    }
    push(@{$lans{$vname}},$member);
    $costs{$vname} = $cost;
}

#
# We use perl IPC goo to create a child we can both write to and read from
# (normal perl I/O provides just unidirectional I/O to a process).
# 
if (! socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)) {
    die("*** $0:\n".
	"    socketpair failed: $!\n");
}

CHILD->autoflush(1);
PARENT->autoflush(1);

my $childpid = fork();
if (! $childpid) {
    close CHILD;

    #
    # Dup our descriptors to the parent, and exec the program.
    # The parent then talks to it read/write.
    #
    open(STDIN,  "<&PARENT") || die "Can't redirect stdin";
    open(STDOUT, ">&PARENT") || die "Can't redirect stdout";
    open(STDERR, ">&PARENT") || die "Can't redirect stderr";

    exec($routecalc);
    die("*** $0:\n".
	"    exec $routecalc failed: $!\n");
}
close PARENT;

#
# Okay, now send adjacency info for all the nodes on each lan to the child.
#
foreach my $lan (keys(%lans)) {
    my @members = @{$lans{$lan}};

    foreach my $member1 (@members) {
	foreach my $member2 (@members) {
	    if ($member1 ne $member2) {
		my ($node1,$port1) = split(":", $member1);
		my ($node2,$port2) = split(":", $member2);
		my $id1 = $map{$node1};
		my $id2 = $map{$node2};

		# Reverse mapping for later. 
		$rlans{"$node1:$node2"} = $lan;

		if ($debug) {
		    print STDERR "$member1($id1) $member2($id2)\n";
		}

		print CHILD "i $id1 $id2 $costs{$lan}\n";
	    }
	}
    }
}
# Tell the child to print the routes.
print CHILD "C\n";

#
# Read back the results from the child.
#
my @results = ();
while (<CHILD>) {
    if ($debug) {
	print "$_";
    }
    push(@results, $_);
}
# Tell the child to quit. Then wait for it.
print CHILD "q\n";
waitpid($childpid, 0);
if ($?) {
    die("*** $0:\n".
	"    $routecalc failed with status: $?\n");
}

#
# Parse the routes. Need to map the indices back to nodes, and then form a
# matrix. Why, well you will see in a bit ...
#
my %routes	= ();
my %netroutes   = ();

foreach my $result (@results) {
    my ($src, $dst, $hop);

    if ($result =~ /route (\d+) to (\d+) via (\d+)/) {
	$src = $rmap{$1};
	$dst = $rmap{$2};
	$hop = $rmap{$3};
    }
    else {
	die("*** $0:\n".
	    "    $routecalc returned a bad line: '$result'\n");
    }
    if ($debug) {
	print "src:$src dst:$dst hop:$hop\n";
    }
    $routes{"$src:$dst"} = $hop;
}

#
# Now it gets fun. We have to go through each src:dst pair in the route
# list. The problem is that the dst could be multihomed and multiple hops
# away, and so we have no idea what interface (IP) we are actually routing
# to. We have to know the IP, or else we won't be able to insert the route!
# So, we use the matrix to recurse through and find our way to the
# destination. When we get to the last hop, we can use the src:dst of that
# last hop to index into rlans, which gives the full lanlink list. Then we
# search that to find the IP.
#
foreach my $route (keys(%routes)) {
    my ($src,$dst) = split(":", $route);
    my $hop  = $routes{$route};
    my $type = 'host';

    #
    # If directly connected, skip.
    #
    if ($dst eq $hop) {
	next;
    }

    #
    # This is the lan that contains the src and hop. (the link). 
    #
    my $srclan = $rlans{"$src:$hop"};

    #
    # Now we need to find the destination lan (link) by stepping through
    # each hop till we get to it.
    #
    my $nhop = $hop;
    while (1) {
	# Next hop to destination.
	if (!defined($routes{"$nhop:$dst"})) {
	    die("*** $0:\n".
		"    Failed to find route: $nhop:$dst!\n");
	}
	my $nnhop = $routes{"$nhop:$dst"};

	# Last hop to destination.
	if ($nnhop eq $dst) {
	    last;
	}
	$nhop = $nnhop;
    }
    my $dstlan = $rlans{"$nhop:$dst"};

    if ($debug) {
	print "$src --> $dst : $nhop:$dst\n";
    }
    
    #
    # The members of the lan are used to map back to the IPs of the
    # interfaces (since we need IPs to insert route entries).
    #
    my @srcmembers = @{$lans{$srclan}};
    my @dstmembers = @{$lans{$dstlan}};

    # Want to find these.
    my ($srcip,$dstip,$hopip);

    #
    # Scan dstmembers till we find the dst. This is the IP we want.
    #
    foreach my $member (@dstmembers) {
	my ($node,$port) = split(":", $member);

	if ($node eq $dst) {
	    $dstip = $ips{$member};
	    last;
	}
    }

    #
381 382 383
    # If optimzing create a net route and avoid a per-host entry for each
    # pair. Convert the route, but remember we did it so we can skip similar
    # routes in the future.
384
    #
385
    if ($optimize) {
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	my $newip = inet_ntoa(inet_aton($dstip) & inet_aton($NETMASK));

	if (defined($netroutes{"$src:$newip"})) {
	    if ($netroutes{"$src:$newip"} ne $hop) {
		die("*** $0:\n".
		    "    network route mismatch: $src:$dst:$hop!\n");
	    }
	    next;
	}

	$netroutes{"$src:$newip"} = $hop;
	$type  = "net";
	$dstip = $newip;
    }

    #
    # Scan srcmembers to find the IPs for the src and hop.
    #
    foreach my $member (@srcmembers) {
	my ($node,$port) = split(":", $member);

	if ($node eq $src) {
	    $srcip   = $ips{$member};
	    next;
	}
	if ($node eq $hop) {
	    $hopip = $ips{$member};
	    next;
	}
    }

417 418 419 420 421 422 423 424
    if ($impotent) {
	if ($debug) {
	    printf(STDERR "%s: %-23s --> %-23s - %s\n",
		   ($type eq "net" ? "n" : "h"),
		   "$src:$srcip", "$dst:$dstip", "$hop:$hopip");
	}
	if ($format eq "ns") {
	    print "\$${src} add-route ";
425
	    if ($type eq "host" && $linkcount{$dst} == 1) {
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
		print "\$${dst} \$${hop}\n";
	    }
	    else {
		print "\$${dstlan} \$${hop}\n";
	    }
	}
	elsif ($format eq "bsd") {
	    if ($type eq "host") {
		print "route add -host $dstip $hopip\n";
	    }
	    else {
		print "route add -net $dstip $hopip $NETMASK\n";
	    }
	}
	elsif ($format eq "suxs") {
	    if ($type eq "host") {
		print "route add -host $dstip gw $hopip\n";
	    }
	    else {
		print "route add -net $dstip netmask $NETMASK gw $hopip\n";
	    }
	}
448
    }
449
    else {
450 451 452 453 454 455 456 457
	DBQueryFatal("insert into virt_routes ".
		     " (pid,eid,vname,dst,nexthop,dst_type) ".
		     " values ('$pid', '$eid', '$src', '$dstip', ".
		     "         '$hopip', '$type')");
    }
}

exit 0;