gentopofile.in 24.6 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
Weibin Sun's avatar
Weibin Sun committed
4
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
5 6 7 8
# All rights reserved.
#
use English;
use Getopt::Std;
9
use Data::Dumper;
10 11 12 13

#
# usage: gentopofile <pid> <eid>
#
14 15 16 17
# This little program generates two topology files that are given to the
# physical nodes in an experiment.  The first one is used to generate the
# /etc/hosts files and their routes.  The second one is used by linktest to
# figure out what tests to run.
18 19 20
#
sub usage()
{
21
    print("Usage: gentopofile [-n] <pid> <eid>\n".
22
	  "  Use -n to print to stdout/stderr, but leave the file alone.\n");
23 24
    exit(-1);
}
25
my $optlist  = "n";
26 27
my $impotent = 0;
my $toponame = "topomap";
28
my $ltname   = "ltmap";
29
my $ltpname  = "ltpmap";
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

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

#
# 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 (defined($options{"n"})) {
    $impotent = 1;
}
if (@ARGV != 2) {
    usage();
}
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.");
}

85 86 87 88 89 90 91
# Do this in case we are not called from tbprerun.
my $workdir = TBExptWorkDir($pid, $eid);

chdir("$workdir") or
    die("*** $0:\n".
	"    Could not chdir to $workdir: $!");

92 93 94 95 96
# The output stream.
my $OUT;

if ($impotent) {
    $OUT = *STDOUT;
97
    $LTOUT = *STDERR;
98 99
}
else {
100 101 102
    unlink("${toponame}.new")
	if (-e "${toponame}.new");
    
103 104
    open(MAP, "> ${toponame}.new") or
	die("Could not create ${toponame}.new: $!\n");
105 106

    $OUT = *MAP;
107

108 109 110
    unlink("${ltname}.new")
	if (-e "${ltname}.new");
    
111 112 113 114
    open(LTMAP, "> ${ltname}.new") or
	die("Could not create ${ltname}.new: $!\n");

    $LTOUT = *LTMAP;
115 116
}

117 118 119 120
#
# Thanks Jon. I took this from ipassign_wrapper.
#
my %bridges = ();
121
my %bridged_links = ();
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
# Link parent in an equivalence class.
# ! exists implies that this is the root
our %linkparents = ();

sub getRoot($) {
    my ($link) = @_;
    while (exists($linkparents{$link})) {
	$link = $linkparents{$link};
    }
    return $link;
}

sub mergeLink($$)
{
    my ($leftlink, $rightlink) = @_;
    my $left = getRoot($leftlink);
    my $right = getRoot($rightlink);
    $linkparents{$left} = $right;
}

142
my $query_result =
143
    DBQueryFatal("select vname, vlink, vport from virt_bridges ".
144
		 " where pid='$pid' and eid='$eid'");
145
while (my ($bridgeid, $linkid, $vport) = $query_result->fetchrow_array) {
146 147 148 149 150 151 152 153 154 155 156 157
    if (! exists($bridges{$bridgeid})) {
	$bridges{$bridgeid} = [];
    }
    foreach $oldlink (@{ $bridges{$bridgeid} }) {
	mergeLink($linkid, $oldlink);
    }
    push(@{ $bridges{$bridgeid} }, $linkid);
}

#print STDERR Dumper(\%bridges);
#print STDERR Dumper(\%linkparents);

158 159 160 161
my %nodes = ();
my %ips   = ();
my %lans  = ();

162 163 164 165 166 167 168 169 170 171 172 173
#
# Find any firewall(s)
#
my %firewalls = ();
$query_result =
    DBQueryFatal("select fwname from virt_firewalls " .
		 "where pid='$pid' and eid='$eid' " .
		 "      order by fwname");
while (my ($fwname) = $query_result->fetchrow_array()) {
    $firewalls{$fwname} = 1;
}

174 175 176 177 178 179 180 181 182 183
#
# Grab the node table and save the ips for each node:port. We are going to use
# this info to convert the DB representation of:
#
# nodeA, 0:1.1.1.1 1:2.2.2.2 2:3.3.3.3
# to
# nodeA, lan0:1.1.1.1 lan1:2.2.2.2 lan2:3.3.3.3
#
# Since the port numbers are totally pointless outside of assign_wrapper.
#
184
$query_result =
185 186 187 188 189 190 191 192 193 194 195
    DBQueryFatal("select v.vname from virt_nodes as v " .
		 "where v.pid='$pid' and v.eid='$eid' " .
		 "      order by v.vname");

while (my ($vname) = $query_result->fetchrow_array()) {
    $nodes{$vname} = {};
    
    # If this is a bridge node, skip.
    next
	if (exists($bridges{$vname}));

196 197 198 199
    # If this is a firewall, skip.
    next
	if (exists($firewalls{$vname}));

200 201 202 203 204 205 206 207 208 209
    print $LTOUT "h $vname\n";
}
$query_result =
    DBQueryFatal("select vnode,vport,ip from virt_lans " .
		 "where pid='$pid' and eid='$eid'");

while (my ($vnode,$vport,$ip) = $query_result->fetchrow_array()) {
    $ips{"$vnode:$vport"} = $ip;
}

210 211 212
#
# Generate the linktest config for links, which looks like:
#
213 214 215 216 217 218 219 220
#   l node0 node1 bw delay loss lname dropstyle
#
# where node0 and node1 are the user-given names of the end points,
# bw is the bandwidth in bits/sec,
# delay is the latency in (fractional) seconds,
# loss is the packet loss rate as a fraction,
# lname is the user-given name of the link,
# dropstyle is "droptail" or "gred".
221
#
222
my %raw_lans = ();
223

224
$query_result =
225 226 227 228 229 230 231
    DBQueryFatal("select * from virt_lans ".
		 "where pid='$pid' and eid='$eid' ".
		 "order by vname,member");

while (my $rowref = $query_result->fetchrow_hashref()) {
    my $vname  = $rowref->{"vname"};
    my $member = $rowref->{"member"};
232
    my ($vnode, $port) = split(":", $member);
233 234 235 236

    #
    # Create a row for this lan, if we have not seen it already.
    #
237
    if (!exists($raw_lans{$vname})) {
238 239 240
	my $rec = {};
	$rec->{"MEMBERLIST"} = [];
	$rec->{"MEMBERS"}    = {};
241
	$raw_lans{$vname}    = $rec;
242
    }
243 244
    $raw_lans{$vname}->{"MEMBERS"}->{$member} = $rowref;
    push(@{$raw_lans{$vname}->{"MEMBERLIST"}}, $member);
245
}
246
my %virt_lans = %raw_lans;
247

248 249 250
#
# Post process for bridge nodes. 
#
251
if (keys(%bridges)) {
252 253 254 255 256 257 258
    my %tmp_lans = ();
    
    foreach my $lan (keys(%virt_lans)) {
	my $vname   = getRoot($lan);
	my @members = @{$virt_lans{$lan}->{"MEMBERLIST"}};
	my $rec;

259
	#print STDERR "$lan:$vname @members\n";
260 261 262 263 264

	if (!exists($tmp_lans{$vname})) {
	    $rec = {};
	    $rec->{"MEMBERLIST"} = [];
	    $rec->{"MEMBERS"}    = {};
265 266
	    $rec->{"BRIDGED"}    = 0;
	    $tmp_lans{$vname}    = $rec;
267 268
	}
	$rec = $tmp_lans{$vname};
269 270 271
	# Mark as being a bridged lan.
	$rec->{"BRIDGED"}++
	    if ($vname ne $lan);
272 273 274 275 276 277 278 279 280 281 282

	foreach my $memb (@members) {
	    my $member = $virt_lans{$lan}->{"MEMBERS"}->{$memb};
	    my $node   = $member->{"vnode"};
	    my $port   = $member->{"vport"};
	    my $mem    = "$node:$port";
	
	    $rec->{"MEMBERS"}->{$mem} = $member;
	    push(@{$rec->{"MEMBERLIST"}}, $mem);
	}
    }
283
    %virt_lans = %tmp_lans;
284 285
}

286 287
#print STDERR Dumper(\%raw_lans);

288 289 290
foreach my $lan (keys(%virt_lans)) {
    my @members = @{$virt_lans{$lan}->{"MEMBERLIST"}};

291
    #print "$lan: @members\n";
292

293 294 295 296
    if (@members == 2) {
	my $member0 = $virt_lans{$lan}->{"MEMBERS"}->{$members[0]};
	my $member1 = $virt_lans{$lan}->{"MEMBERS"}->{$members[1]};

297 298 299 300 301 302 303 304 305
	my $node0      = $member0->{"vnode"};
	my $delay0     = $member0->{"delay"};
	my $loss0      = $member0->{"lossrate"};
	my $bw0        = $member0->{"bandwidth"};
	my $backfill0  = $member0->{"backfill"};
	my $rdelay0    = $member0->{"rdelay"};
	my $rloss0     = $member0->{"rlossrate"};
	my $rbw0       = $member0->{"rbandwidth"};
	my $rbackfill0 = $member0->{"rbackfill"};
306 307 308 309
	my $qtype0  = "droptail";
	if ($member0->{"q_red"}) {
	    $qtype0 = ($member0->{"q_gentle"} ? "gred" : "red");
	}
310

311 312 313 314 315 316 317 318 319 320
	my $node1      = $member1->{"vnode"};
	my $delay1     = $member1->{"delay"};
	my $loss1      = $member1->{"lossrate"};
	my $bw1        = $member1->{"bandwidth"};
	my $backfill1  = $member1->{"backfill"};
	my $rdelay1    = $member1->{"rdelay"};
	my $rloss1     = $member1->{"rlossrate"};
	my $rbw1       = $member1->{"rbandwidth"};
	my $rbackfill1 = $member1->{"rbackfill"};
	my $qtype1     = "droptail";
321 322 323
	if ($member1->{"q_red"}) {
	    $qtype1 = ($member1->{"q_gentle"} ? "gred" : "red");
	}
324
	
325
	# ebw stands for effective bandwith, it is bw - backfill
326 327 328
	my $delay = ($delay0+$rdelay1) / 1000.0 ;
	my $loss = 1-(1-$loss0)*(1-$rloss1);
	my $bw = &min($bw0,$rbw1) * 1000;
329 330
	my $backfill = &max($backfill0,$rbackfill1) * 1000;
	my $ebw = $bw - $backfill;
331 332 333
	my $rdelay = ($rdelay0+$delay1) / 1000.0;
	my $rloss = 1-(1-$rloss0)*(1-$loss1);
	my $rbw = &min($rbw0,$bw1) * 1000;
334 335
	my $rbackfill = &max($rbackfill0,$backfill1) * 1000;
	my $rebw = $rbw - $rbackfill;
336 337

	printf $LTOUT
338
	    "l $node0 $node1 $ebw %.4f %.6f $lan $qtype0\n", $delay, $loss;
339
	printf $LTOUT
340
	    "l $node1 $node0 $rebw %.4f %.6f $lan $qtype1\n", $rdelay, $rloss;
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 381 382 383 384 385 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    elsif ($virt_lans{$lan}->{"BRIDGED"}) {
	if (@members == 4) {
	    #
	    # A bridged link. Handled specially since the shaping parameters
	    # are located in different places in the virt_lans entries.
	    #
	    # First find the bridge members
	    #
	    my @members = values(%{ $virt_lans{$lan}->{"MEMBERS"} });
	    my @bridges = grep {defined($_->{'bridge_vname'})} @members;
	    my $member0 = $bridges[0];
	    my $member1 = $bridges[1];

	    #
	    # We get the bridge side of the links above, but we need the other
	    # side of each link for the names to use in the output.
	    #
	    my ($node0, $node1);
	    foreach my $member (@members) {
		if ($member->{'vname'} eq $member0->{'vname'}) {
		    $node0 = $member->{'vnode'}
		      if ($member->{'vnode'} ne $member0->{'vnode'});
		}
		else {
		    $node1 = $member->{'vnode'}
		      if ($member->{'vnode'} ne $member1->{'vnode'});
		}
	    }

	    my $delay0     = $member0->{"delay"} / 1000.0;
	    my $loss0      = $member0->{"lossrate"};
	    my $bw0        = $member0->{"bandwidth"} || 100000;
	    my $qtype0     = "droptail";
	    if ($member0->{"q_red"}) {
		$qtype0 = ($member0->{"q_gentle"} ? "gred" : "red");
	    }

	    my $delay1     = $member1->{"delay"} / 1000.0;
	    my $loss1      = $member1->{"lossrate"};
	    my $bw1        = $member1->{"bandwidth"} || 100000;
	    my $qtype1     = "droptail";
	    if ($member1->{"q_red"}) {
		$qtype1 = ($member1->{"q_gentle"} ? "gred" : "red");
	    }
	    my $bw = min($bw0, $bw1) * 1000;

	    printf $LTOUT
		"l $node0 $node1 $bw %.4f %.6f $lan $qtype0\n", $delay0, $loss0;
	    printf $LTOUT
		"l $node1 $node0 $bw %.4f %.6f $lan $qtype1\n", $delay1, $loss1;
	}
	else {
	    #
	    # A lan with attached bridge nodes.
	    #
	    foreach my $memb0 (@members) {
		my $member0 = $virt_lans{$lan}->{"MEMBERS"}->{$memb0};
		my $node0   = $member0->{"vnode"};

		# Bridges are not part of the topology.
		next
		    if (exists($bridges{$node0}));

		#print STDERR "S: $memb0\n";

		#
		# If this node is attached to a bridge, find it. It has the
		# delay info for packets heading into the lan. This is
		# complicated by the fact that not all of the nodes in the
		# lan must be attached to a bridge (and thus delayed). 
		#
		my $origlan0  = $member0->{'vname'};
		my @omembers = values(%{$raw_lans{$origlan0}->{"MEMBERS"}});
		if (@omembers == 2) {
		    #
		    # Yep, a link. If its a lan, then this member is attached
		    # directly to the lan with no delay. Get the member on the
		    # other side of the link (the bridge side), it has the
		    # delay params.
		    #
		    $member0 = ($omembers[0]->{'vnode'} eq $node0 ?
				$omembers[1] : $omembers[0]);
		}

		my $delay0     = $member0->{"delay"};
		my $loss0      = $member0->{"lossrate"};
		my $bw0        = $member0->{"bandwidth"} || 100000;
		my $qtype0     = "droptail";
		if ($member0->{"q_red"}) {
		    $qtype0 = ($member0->{"q_gentle"} ? "gred" : "red");
		}

		foreach my $memb1 (@members) {
		    next
			if ($memb0 eq $memb1);

		    #print STDERR "D: $memb1\n";

		    my $member1    = $virt_lans{$lan}->{"MEMBERS"}->{$memb1};
		    my $node1      = $member1->{"vnode"};

		    # Bridges are not part of the topology.
		    next
			if (exists($bridges{$node1}));

		    my $origlan1 = $member1->{'vname'};
		    my @omembers = values(%{$raw_lans{$origlan1}->{"MEMBERS"}});
		    if (@omembers == 2) {
			my $wanted;
			
			#
			# Yep, a link. If its a lan, then this member is
			# attached directly to the lan with no delay. We
			# need to get the member on the other side of the
			# link (the bridge side).
			#
			my $memberL = ($omembers[0]->{'vnode'} eq $node1 ?
				       $omembers[1] : $omembers[0]);

			#
			# We get the bridgeid from that member.
			#
			my $bridgeid = $memberL->{'vnode'};

			#
			# Now we want the member on the other side of
			# bridge, since that has the delay parameters for
			# packets heading to the node. Search through the
			# member list again looking for another (different)
			# one attached to the bridge,
			#
			foreach my $membM (@members) {
			    my $memberM = $virt_lans{$lan}->{"MEMBERS"}->{$membM};

			    if ($memberM->{'vnode'} eq $bridgeid &&
				$memberM->{'vport'} != $memberL->{'vport'}) {
				$wanted = $memberM;
				last;
			    }
			}
			if (!defined($wanted)) {
			    print STDERR "No bridge member for $member1\n";
			}
			else {
			    $member1 = $wanted;
			}
		    }
		    #print STDERR Dumper($member1);
		    
		    my $delay1    = $member1->{"delay"};
		    my $loss1     = $member1->{"lossrate"};
		    my $bw1       = $member1->{"bandwidth"} || 100000;

		    my $delay = ($delay0+$delay1) / 1000.0;
		    my $loss  = 1-(1-$loss0)*(1-$loss1);
		    my $bw    = &min($bw0,$bw1) * 1000;

		    printf $LTOUT
			"l $node0 $node1 $bw %.4f %.6f $lan $qtype0\n",
			$delay, $loss;
		}
	    }
	}
    }
506 507 508 509
    else {
	foreach my $memb0 (@members) {
	    my $member0 = $virt_lans{$lan}->{"MEMBERS"}->{$memb0};
	    
510 511 512 513 514
	    my $node0     = $member0->{"vnode"};
	    my $delay0    = $member0->{"delay"};
	    my $loss0     = $member0->{"lossrate"};
	    my $bw0       = $member0->{"bandwidth"};
	    my $backfill0 = $member0->{"backfill"};
515 516 517 518 519
	    
	    foreach my $memb1 (@members) {
		next
		    if ($memb0 eq $memb1);

520 521 522 523 524 525
		my $member1    = $virt_lans{$lan}->{"MEMBERS"}->{$memb1};
		my $node1      = $member1->{"vnode"};
		my $rdelay1    = $member1->{"rdelay"};
		my $rloss1     = $member1->{"rlossrate"};
		my $rbw1       = $member1->{"rbandwidth"};
		my $rbackfill1 = $member1->{"rbackfill"};
526 527 528 529
		my $qtype1  = "droptail";
		if ($member1->{"q_red"}) {
		    $qtype1 = ($member1->{"q_gentle"} ? "gred" : "red");
		}
530

531
		# ebw stands for effective bandwith, it is bw - backfill
532 533 534
		my $delay = ($delay0+$rdelay1) / 1000.0;
		my $loss = 1-(1-$loss0)*(1-$rloss1);
		my $bw = &min($bw0,$rbw1) * 1000;
535 536
		my $backfill = &max($backfill0,$rbackfill1) * 1000;
		my $ebw = $bw - $backfill;
537 538

		printf $LTOUT
539
		    "l $node0 $node1 $ebw %.4f %.6f $lan $qtype1\n",
540
		    $delay, $loss;
541 542 543
	    }
	}
    }
544 545 546 547 548
}

#
# Generate the linktest config for the routing type.
#
549 550 551 552
# XXX Linktest only supports one type of routing per-experiment at the moment,
# not per-node.  We also have to prune out the firewall since it always has
# routertype == "none".
#
553
$query_result =
554 555 556 557 558 559
    DBQueryFatal("select vn.routertype from virt_nodes as vn ".
		 "left join virt_firewalls as vf on (vf.pid=vn.pid and ".
		 "  vf.eid=vn.eid and vf.fwname=vn.vname) ".
		 "where vn.pid='$pid' and vn.eid='$eid' ".
		 "  and vf.fwname is null ".
		 "group by routertype");
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

while (my ($rt) = $query_result->fetchrow_array) {
    print $LTOUT "r $rt\n";
}

#
# Generate the linktest config for the simulator agent's name.
#
$query_result =
    DBQueryFatal("select distinct vname from virt_agents ".
		 "where pid='$pid' and eid='$eid' and objecttype=6");

while (my ($sim) = $query_result->fetchrow_array) {
    print $LTOUT "s $sim\n";
}

576 577 578 579 580
#
# Grab the lans table. We are going to spit out one entry per lan, but
# we need to convert port:ip above to lan:ip.
# 
$query_result =
581 582 583
    DBQueryFatal("select v.vname,member,mask,cost,vlink from virt_lans as v ".
		 "left join virt_bridges as b on b.vname=v.bridge_vname and ".
		 "     b.pid=v.pid and b.eid=v.eid ".
584 585
		 "where v.pid='$pid' and v.eid='$eid' ");

586 587 588 589 590 591 592 593 594
while (my ($vname,$member,$mask,$cost,$bridgelink) =
       $query_result->fetchrow_array()) {
    my ($node,$port) = split(":", $member);
    
    # If this is a bridge node, skip address assignment for it.
    next
	if (exists($bridges{$node}));

    $vname = getRoot($vname);
595 596 597
    # One entry per lan.
    if (! defined($lans{$vname})) {
	$lans{$vname} = {};
598 599 600
	$lans{$vname}->{"mask"}    = $mask;
	$lans{$vname}->{"cost"}    = $cost;
	$lans{$vname}->{"members"} = {};
601
    }
602

603 604 605 606 607 608 609 610 611 612 613 614 615
    # Store lan:ip into the portlist for the node.
    my $ip = $ips{$member};
    $nodes{$node}->{$port} = "$vname:$ip";
}

#
# First spit out the nodes.
#
# ALWAYS print this header; rather then a version number, it serves
# to describe the format of the data that follows. 
#
print $OUT "# nodes: vname,links\n";

616
foreach my $node (sort keys(%nodes)) {
617 618 619 620
    # If this is a bridge node, skip.
    next
	if (exists($bridges{$node}));
    
621 622 623 624 625 626 627 628 629 630
    print $OUT "$node,";
    print $OUT join(" ", values(%{ $nodes{$node} }));
    print $OUT "\n";
}

#
# Then spit out the lans. As above, ALWAYS print the header.
#
print $OUT "# lans: vname,mask,cost\n";

631
foreach my $lan (sort keys(%lans)) {
632 633 634 635 636
    my $cost = $lans{$lan}->{"cost"};
    my $mask = $lans{$lan}->{"mask"};
    print $OUT "$lan,$mask,$cost\n";
}

637 638
#
# Finally generate a "physical resource" map for linktest if the
639 640 641 642 643 644 645 646 647 648
# experiment is swapped in.  It is a dual of the virtual ltmap,
# containing "instantiation" information about nodes and links.
# First we have a version number:
#
# V number
#
# What a forward-thinking concept!
#
# A node info line looks like:
#
649
#   H vname pname phost ptype OSID OS-name OS-version OS-features
650
#
651 652 653 654 655
# where vname is the experimenter given name like in the ltmap,
# pname is the physical node name (e.g., "pc10", "pcvm10-2"),
# phost is the "hosting" physical node for vnodes (or pname for regular nodes),
# OSID is the emulab OS identifier for the OS running (e.g., "FBSD410-STD"),
# OS-name is OS (e.g., "FreeBSD", "Linux"),
656 657 658
# OS-version is the version of that OS (e.g., "4.10", "9.0"),
# OS-features is a comma separated list of features supported by the OS
#  (including "linktest").
659 660 661 662 663 664 665 666 667 668 669 670 671
#
# A link info line looks like:
#
#   L node0 node1 lname node0-mac mpx-style shaping-method
#
# where node0 and node1 are the user-specified node names,
# lname is the user-specified link/lan name,
# node0-mac is the MAC address of node0's interface
# (can be mapped to interface name with findif),
# mpx-style is the multiplexing style: "none" for physical links,
# "veth" for encapsulated veth, or "veth-ne" for non-encapsulated veth,
# shaping-method is either "dnode" for delay node shaping or "linkdelay"
# for end node shaping.
672 673 674 675 676 677 678 679 680 681
#

#
# Note the regular joins here, not left joins, so that we do not
# get lines for delay nodes and virtnode hosts that are in reserved
# but not in virt_nodes.
#
my $havepmap = 0;
$query_result =
    DBQueryFatal("select v.vname,n.node_id,n.phys_nodeid,n.type,".
Weibin Sun's avatar
Weibin Sun committed
682
		 " o.osid,o.OS,o.version,o.osfeatures,r.erole".
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
		 " from virt_nodes as v,reserved as r,nodes as n,os_info as o".
		 " where v.pid=r.pid and v.eid=r.eid and v.vname=r.vname".
		 " and r.node_id=n.node_id and n.def_boot_osid=o.osid and".
		 " v.pid='$pid' and v.eid='$eid'");
if ($query_result->numrows) {
    $havepmap = 1;
    if ($impotent) {
	$LTPOUT = *STDERR;
    } else {
	unlink("${ltpname}.new")
	    if (-e "${ltpname}.new");
    
	open(LTPMAP, "> ${ltpname}.new") or
	    die("Could not create ${ltpname}.new: $!\n");

	$LTPOUT = *LTPMAP;
    }
700 701 702 703
    #
    # Version 2 added osfeatures field
    #
    print $LTPOUT "V 2\n";
Weibin Sun's avatar
Weibin Sun committed
704 705
    while (my ($vname,$node,$pnode,$ptype,$osid,$os,$osvers,$osfeatures,$role)
	   = $query_result->fetchrow_array) {
706 707
	$os = "UNKNOWN" if (!$os);
	$osvers = "0" if (!$osvers);
708
	$osfeatures = "UNKNOWN" if (!$osfeatures);
Weibin Sun's avatar
Weibin Sun committed
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	#
	# Ugh. Nodes with role "virthost" can now be part of the explicit
	# topology. Nothing wrong with that except that those nodes will
	# not run linktest! To ensure that such a node does not get nominated
	# as the coordinator for a linktest run, we remove the "linktest"
	# attribute from osfeatures. This will tell the linktest script not
	# to consider the node for that honor.
	#
	# One would think that we should just pass the node's role to linktest
	# and let it decide for itself, but that would create another level
	# of backward incompatibility.
	#
	if ($role eq "virthost" && $osfeatures =~ /linktest/) {
	    $osfeatures = join(',',
			       grep($_ ne "linktest",
				    split(',', $osfeatures)));
	}
726 727
	print $LTPOUT
	    "H $vname $node $pnode $ptype $osid $os $osvers $osfeatures\n";
728 729 730 731
    }

    #
    # The MAC address is either in interfaces (for physical interfaces)
732
    # or vinterfaces (for virtual interfaces).
733 734
    #
    # The multiplexing style is either "veth" if there is a row in the
735
    # vinterfaces table or "none" otherwise.  This is reflected in
736 737 738 739 740
    # the following query as: style is "veth" if vmac!=NULL.
    #
    # Linkdelays are in use if the link has a row in the linkdelays table.
    # This is reflected in the following query as: linkdelays if iface!=NULL.
    # We could look in the delays table to further distinguish unshaped
741 742
    # links, but at the moment, we don't need that info.  We do look at the
    # virt_lans nobwshaping field to see if the link is being shaped.
743
    #
744 745 746 747
    # Argh...further complicated by the reserved table node_id being either
    # a pnode or a vnode name while vinterfaces identifies a pnode with
    # node_id if vnode_id==NULL and a vnode with vnode_id if vnode_id!=NULL.
    #
748
    $query_result =
749
	DBQueryFatal("select v.member,v.vname,i.mac,vi.mac,vi.type,l.iface,v.nobwshaping".
750
		     " from reserved as r join virt_lans as v".
751 752 753 754 755
		     " left join interfaces as i".
		     "  on v.ip=i.IP and r.node_id=i.node_id".
		     " left join linkdelays as l".
		     "  on r.pid=l.pid and r.eid=l.eid and".
		     "   v.vname=l.vlan and v.vnode=l.vnode and v.ip=l.ip".
756 757 758 759 760
		     " left join vinterfaces as vi".
		     "  on v.ip=vi.IP and".
		     "    (vi.vnode_id is NULL and r.node_id=vi.node_id".
		     "      or".
		     "    vi.vnode_id is not NULL and r.node_id=vi.vnode_id)".
761 762 763 764
		     " where".
		     "  r.pid=v.pid and r.eid=v.eid and r.vname=v.vnode and".
		     "  r.pid='$pid' and r.eid='$eid'");
    if ($query_result->numrows) {
765
	while (my ($memb,$vlan,$imac,$vmac,$vtype,$iface,$noshape) =
766
	       $query_result->fetchrow_array) {
767
	    $vlan = getRoot($vlan);
768 769 770 771
	    if (exists($virt_lans{$vlan}->{"MEMBERS"}->{$memb})) {
		my $member = $virt_lans{$vlan}->{"MEMBERS"}->{$memb};
		if (defined($vmac)) {
		    $member->{"mac"} = $vmac;
772 773 774 775 776
		    if ($vtype eq "veth") {
			$member->{"encap"} = "veth";
		    } else {
			$member->{"encap"} = "none";
		    }
777 778 779 780
		} else {
		    $member->{"mac"} = $imac;
		    $member->{"encap"} = "none";
		}
781 782 783 784 785 786 787 788 789 790 791 792 793 794
		if ($noshape) {
		    #
		    # XXX "noshaping" means just no BW shaping,
		    # other shaping still happens.  So we identify it
		    # as such here.  We continue to distinguish linkdelays
		    # as the presence of on-node shaping has other
		    # implications for linktest.
		    #
		    if (defined($iface)) {
			$member->{"dstyle"} = "linkdelay-nobw";
		    } else {
			$member->{"dstyle"} = "dnode-nobw";
		    }
		} elsif (defined($iface)) {
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
		    $member->{"dstyle"} = "linkdelay";
		} else {
		    $member->{"dstyle"} = "dnode";
		}
	    } else {
		print $LTPOUT "E Bogus lan/member $vlan/$memb\n";
	    }
	}
    }
}

#
# Now dump the link info
#
if ($havepmap) {
    foreach my $vlan (keys(%virt_lans)) {
	my @members = @{$virt_lans{$vlan}->{"MEMBERLIST"}};
	foreach my $memb0 (@members) {
	    my $member0 = $virt_lans{$vlan}->{"MEMBERS"}->{$memb0};
	    my $node0 = $member0->{"vnode"};
815 816 817 818
	    
	    # If this is a bridge node, skip.
	    next
		if (exists($bridges{$node0}));
819 820 821 822 823 824 825 826 827 828 829 830

	    my $mac0 = $member0->{"mac"};
	    if (!defined($mac0)) {
		print $LTPOUT "E No link info found for $vlan/$memb0\n";
		next;
	    }
	    my $encap0 = $member0->{"encap"};
	    my $dstyle0 = $member0->{"dstyle"};
	    
	    foreach my $memb1 (@members) {
		next
		    if ($memb0 eq $memb1);
831
		
832 833 834
		my $member1 = $virt_lans{$vlan}->{"MEMBERS"}->{$memb1};
		my $node1 = $member1->{"vnode"};

835 836 837 838
		# If this is a bridge node, skip.
		next
		    if (exists($bridges{$node1}));

839 840 841 842
		print $LTPOUT
		    "L $node0 $node1 $vlan $mac0 $encap0 $dstyle0\n";
	    }
	}
843 844 845 846 847
    }
    close(LTPMAP)
	if (!$impotent);
}

848 849
if (! $impotent) {
    close(MAP);
850
    close(LTMAP);
851

852
    system("mv -f ${toponame}.new $toponame");
853
    system("mv -f ${ltname}.new $ltname");
854 855
    system("mv -f ${ltpname}.new $ltpname")
	if ($havepmap);
856

857 858 859 860
    #
    # Create a compressed copy of the file. The experiment nodes will look
    # for this first, so as to reduce the amount of data served up via NFS.
    #
861
    system("rm -f ${toponame}.gz ; cat $toponame | gzip > ${toponame}.gz");
862
    system("rm -f ${ltname}.gz ; cat $ltname | gzip > ${ltname}.gz");
863 864
    system("rm -f ${ltpname}.gz ; cat $ltpname | gzip > ${ltpname}.gz")
	if ($havepmap);
865 866 867 868 869 870 871 872 873 874

    #
    # Now copy over to the user exp directory since in a normal create/modify
    # files are not copied over from boss to ops until later, which is too
    # late cause the nodes have already booted, and the topomap will not be
    # where it supposed to be (or it is stale).
    #
    my $userdir = TBExptUserDir($pid, $eid);

    system("cp -fp $toponame ${toponame}.gz $userdir/tbdata");
875
    system("cp -fp $ltname ${ltname}.gz $userdir/tbdata");
876 877
    system("cp -fp $ltpname ${ltpname}.gz $userdir/tbdata")
	if ($havepmap);
878 879
}
exit(0);