assign_wrapper.in 23.6 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
#!/usr/bin/perl -w

# This rather complex program acts as an interface between the IR
# and the assign engine.  It translate between LANs and the star
# topologies used to emulate lans in assign.  It genertes the 
# top file, calls assign, and then interprets the output, generating
# the /virtual and /vlan section.

# The ptop file should be generated with 'avail | ptopgen'

# Syntax: assign_wrapper <ir> <ptop>

# Limitations:
#   It is assumed that mac addresses are sufficient to generate vlans.  From
# this data it should be possible to look up all other pertinant information
# about nodes.  This is usually correct except in the case where there are
# multiple paths between nodes are different switches.  Any information
# about which path was chosen by assign (for load balancing) is lost.
#  Note that assign itself has a limitation that no two switches can have 
# more than one intermediate switch.

# Debugging:
#  Running with -v will produce a large amount of useful output.

# Caveats:
#   The support for direct and interswitch links has not been
# testbed much at all.

# Some settings
# delaythresh is the maximum delay in ms above which a delay node is needed
# maxrun is the number of times to rerun assign if no match is found.
# assign is the path to assign
33
$delaythresh = 3;
34 35
$maxrun = 5;

36 37 38 39 40 41 42
#
# Configure variables
#
my $IRLIB  = "@prefix@/lib/ir";
my $assign = "@prefix@/libexec/assign";

push(@INC,$IRLIB);
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 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 117 118 119 120 121 122
require libir;

$verbose = -1;
if ($#ARGV == 2) {
    if ($ARGV[0] eq "-v") {
	$verbose = 1;
	($irfile,$ptopfile) = @ARGV[1,2];
    } 
} elsif ($#ARGV == 1) {
    if ($ARGV[0] ne "-v") {
	$verbose = 0;
	($irfile,$ptopfile) = @ARGV;
    }
} 
if ($verbose == -1) {
    print STDERR "Syntax: $0 [-v] irfile ptopfile\n";
    exit(1);
}

# printdb - prints out a debug message passed to it if $verbose == 1
sub printdb {
    if ($verbose) {
	print "DB: $_[0]";
    }
};

# We now read in the topology section
eval {&ir_read($irfile)};
if ($@) {
    print STDERR "Could not read $irfile ($@)\n";
    exit(1);
}

# nodes is a hash indexed by node name and valued with a list of link names.
# nodetype is a hash indexed by node name and values with the type of the node.
$raw = eval {&ir_get("/topology/nodes")};
if ($@) {
    print STDERR "Could not load /topology/nodes.\n";
    exit(1);
}
foreach (split("\n",$raw)) {
    @info = split;
    ($nodename,$nodetype,@links) = @info;
    printdb "nodetype{$nodename} = $nodetype\n";
    $nodetypes{$nodename} = $nodetype;
    $linklist = [];
    foreach (@links) {
	push(@$linklist,$_);
    }
    printdb "nodes{$nodename} = " . join(" ",@$linklist) . "\n";
    $nodes{$nodename} = $linklist;
}

# links is a hash indexed by link name and containing a list:
#  [src,dst,bw,delay,loss]
$raw = eval {&ir_get("/topology/links")};
if ($@) {
    print STDERR "Could not load /topology/links.\n";
    exit(1);
}
foreach (split("\n",$raw)) {
    @info = split;
    ($link,$src,$dst,$bw,$delay,$loss) = @info[0,1,3,5,7,9];
    $delay =~ s/[mM][sS]$//;
    $bw =~ s/[mM][bB]$//;
    printdb "links{$link} = [$src,$dst,$bw,$delay,$loss]\n";
    $links{$link} = [$src,$dst,$bw,$delay,$loss];
}

# lans is a hash indexed by lan name and containing a list:
#  [nodes,bw,delay,loss,links]
# where nodes and links are references to arrays of the nodes in
# the lan and the external links to the lan, respectively.
$raw = eval {&ir_get("/topology/lans")};
if ($@) {
    print STDERR "Could not load /topology/lans.\n";
    exit(1);
}
foreach (split("\n",$raw)) {
    ($lan,$nodes_raw,$bw,$delay,$loss,$links_raw) = 
123
	/([^ ]+) "([^\"]+)" ([^ ]+) ([^ ]+) ([^ ]+)(?:[ \t]+(.*))?$/;
124 125 126 127 128
    $nodes_tmp = [];
    foreach (split(" ",$nodes_raw)) {
	push(@$nodes_tmp,$_);
    }
    $links_tmp = [];
129 130
    if (defined $links_raw) {
      foreach (split(" ",$links_raw)) {
131
	push(@$links_tmp,$_);
132
      }
133 134 135
    }
    $delay =~ s/[mM][sS]$//;
    $bw =~ s/[mM][bB]$//;
Christopher Alfeld's avatar
Christopher Alfeld committed
136 137
    printdb "lans{$lan} = [$nodes_raw,$bw,$delay,$loss,".
	join(" ",@$links_tmp) ."]\n";
138 139 140 141 142 143 144 145 146 147 148 149 150
    $lans{$lan} = [$nodes_tmp,$bw,$delay,$loss,$links_tmp];
}

# min just returns the minimum value of it's arguments
sub min {
    $min = $_[0];
    foreach (@_[1,$#_]) {
	if ($_ < $min) {
	    $min = $_;
	}
    }
    return $min;
};
Christopher Alfeld's avatar
Christopher Alfeld committed
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 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

# Shark shelf

# We need to find LANs with sharks in them, check to see all's ok and
# convert them in to shark shelves, splitting the LAN if necessary.  The 
# following conditions must be checked:
# Foreach lan with sharks
#   1. 10 MB, 0 latency, no loss
#   2. <= 8 sharks in LAN
#   3. <= 1 external link.
# Foreach shark
#   1. In LAN
#   2. No other links

# %sharkshelves is indexed by sharkshelf name (ss-<lan>) and contains
# a list of the sharks in it.
foreach $lan (keys(%lans)) {
    ($lnodes,$bw,$delay,$loss,$llinks) = @{$lans{$lan}};
    @sharks = ();
    @others = ();
    foreach $node (@$lnodes) {
	if ($nodetypes{$node} eq "shark") {
	    push(@sharks,$node);
	} else {
	    push(@others,$node);
	}
    }
    # Skip if nothing more to do
    if ($#sharks == -1) {next;}

    # Check LAN
    if (($bw != 10) || ($delay != 0) || ($loss != 0)) {
	print STDERR "$lan has sharks but is not 10Mb/0 delay/0 loss.\n";
	exit(1);
    }
    if ($#sharks >= 8) {
	print STDERR "$lan has too many sharks.  Max 8.\n";
	exit(1);
    }

    printdb "shark-lan $lan\n";
    # Check sharks and remove them from consideration if they are OK
    foreach $shark (@sharks) {
	if (@{$nodes{$shark}}) {
	    print STDERR "Shark $shark has links.\n";
	    exit(1);
	}
	delete $nodes{$shark};
	delete $nodetypes{$shark};
    }
    
    # Final checks
    @lanlinks = @$llinks;
    if (@others) {
	print STDERR "Shark lan $lan has non-sharks.\n";
	exit(1);
    }
    if ($#lanlinks > 0) {
	print STDERR "Shark lan $lan has more than one outside link.\n";
	exit(1);
    }

    # Generate the shark shelf node
    $ssnode = "ss-$lan";
    $nodetypes{$ssnode} = "shark-shelf";
    printdb "  nodetypes{$ssnode} = shark-shelf\n";
    $sharkshelves{$ssnode} = [];
    push(@{$sharkshelves{$ssnode}},@sharks);
    printdb "  sharkshelves{$ssnode} = " . join(" ",@{$sharkshelves{$ssnode}}) . "\n";
    if ($#lanlinks == 0) {
	printdb "  Setting up link\n";
	$extlink = $lanlinks[0];
	($src,$dst,$bw,$delay,$loss) = @{$links{$extlink}};
Christopher Alfeld's avatar
Christopher Alfeld committed
224
	delete $links{$extlink};
Christopher Alfeld's avatar
Christopher Alfeld committed
225 226 227 228 229
	if ($src eq "$lan") {
	    $rdst = $dst;
	} else {
	    $rdst = $src;
	}
Christopher Alfeld's avatar
Christopher Alfeld committed
230 231 232 233
	$nodes{$ssnode} = ["$extlink"];
	printdb "  nodes{$ssnode} = $extlink\n";
	$links{"$extlink"} = [$ssnode,$rdst,$bw,$delay,$loss];
	printdb "  links{$extlink} = [$ssnode,$rdst,$bw,$delay,$loss]\n";
Christopher Alfeld's avatar
Christopher Alfeld committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    } else {
	$nodes{$ssnode} = [];
	printdb "  nodes{$ssnode} = []\n";
    }

    # Remove the original LAN
    delete $lans{$lan};
}

# Check all remaining nodes to make sure none are sharks
foreach $node (keys(%nodes)) {
    printdb "Checking $node for sharkness ($nodetypes{$node})\n";
    if ($nodetypes{$node} eq "shark") {
	print STDERR "$node is s shark but not in a LAN.\n";
	exit(1);
    }
}


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
# LAN conversion

# We now need to turn LANs into links and nodes.  Each lan gets a central
# node of type "lan".  In an undealyed lan all the links in the lan are
# linked directly to that node.  In a delayed lan a bunch of delay nodes 
# are created with "1/2" delays and linked to the lan node and the nodes in
# the lan are linked to those delay nodes.  We actually don't create the delay
# nodes here we just set up the links as delayed and let the next section
# deal with it.
foreach $lan (keys(%lans)) {
    @info = @{$lans{$lan}};
    @nodes = @{$info[0]};
    @extlinks = @{$info[4]};
    ($bw,$delay,$loss) = @info[1,2,3];

    printdb "Converting $lan ($bw,$delay,$loss)\n";

    # Add lan node
    $nodetypes{$lan} = "lan";
    $nodes{$lan} = [];
    push(@{$nodes{$lan}},@extlinks);

    # Add links
    foreach $node (@nodes) {
	$link = "$lan-$node";
	# "1/2" of the loss in this sequential case if 1-sqrt(1-loss)
	$links{$link} = [$lan,$node,$bw,$delay/2,1-sqrt(1-$loss)];
	printdb "Created link $link = [" . join(",",@{$links{$link}}) . "]\n";
	push(@{$nodes{$lan}},$link);
    }
    printdb "nodes{$lan} = " . join(" ",@{$nodes{$lan}}) . "\n";
}


# Add delays 
$delayindex = 0;

# delayinfo is indexed by delay node name and contains a list of:
# [$bw,$delay,$loss]

# We now handle delays.  We just need to split links with delay
# charateristics.  There is one special case in that we need to add a
# special delay node for lan<->lan links even if there are no delay
# characteristics on either lan or the link.
foreach $link (keys(%links)) {
    ($src,$dst,$bw,$delay,$loss) = @{$links{$link}};
    
    printdb "Looking at $link ($src,$dst,$bw,$delay,$loss)\n";
    if (defined($lans{$src}) && defined($lans{$dst})) {
	# lan<->lan link
	# Create delay node
	$delayname = "delay$delayindex";
	$delayindex++;
	$nodetypes{$delayname} = "delay";
	$nodes{$delayname} = ["dsrc_$link","ddst_$link"];
	$links{"dsrc_$link"} = [$src,$delayname,0,0,0];
	$links{"ddst_$link"} = [$delayname,$dst,0,0,0];
	printdb "lan<->lan Created $delayname with links dsrc_$link and ddst_$link\n";
	# !!! - This could be improved (splice)
	$newsrclinks = [];
	foreach (@{$nodes{$src}}) {
	    if ($_ eq $link) {
		push(@$newsrclinks,"dsrc_$link");
	    } else {
		push(@$newsrclinks,$_);
	    }
	}
	$nodes{$src} = $newsrclinks;
	$newdstlinks = [];
	foreach (@{$nodes{$dst}}) {
	    if ($_ eq $link) {
		push(@$newdstlinks,"ddst_$link");
	    } else {
		push(@$newdstlinks,$_);
	    }
	}
	$nodes{$dst} = $newdstlinks;
	
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
#  	($Abw,$Adelay,$Aloss) = @{$lans{$src}}[1,2,3];
#  	($Bbw,$Bdelay,$Bloss) = @{$lans{$dst}}[1,2,3];
#  	# For loss here we have a probability problem.  We know that
#  	# our packet will go through a delay node on each lan
#  	# with a loss rate of (1-sqrt(1-$[AB]loss)).  We want to set
#   	# the loss rate of this delay node to be the total loss rate
#  	# as if the packet passed through a hop of loss rate $Aloss
#  	# and a hop of $Bloss and a hop of $loss.
#  	# Let A=(1-sqrt(1-Aloss)
#  	# Let B=(1-sqrt(1-Bloss)
#  	# A, B are fixed along with Aloss, Bloss, and loss.  
#  	# Let r be the loss of this delay node.
#  	# The loss rate then is: (1-(1-A)*(1-B)*(1-r))
#  	# We want this to equal (1-(1-Aloss)*(1-Bloss)*(1-loss))
#  	# Solving for r:
#  	# r = 1-(1-Aloss)*(1-Bloss)*(1-loss)/((1-A)*(1-B))
#  	# The denomenator is 0 if either A or B is 1 which happens if
#  	# Aloss or Bloss is 1.  In this case we want r to be 1.
#  	if (($Aloss == 1) || ($Bloss == 1)) {
#  	    $r = 1;
#  	} else {
#  	    $r = (1-(1-$Aloss)*(1-$Bloss)*(1-$loss))/
#  		(sqrt(1-$Aloss)*sqrt(1-$Bloss));
#  	}
#  	$delayinfo{$link} = [&min($Abw,$Bbw,$bw),
#  			     $Adelay/2+$Bdelay/2+$delay,
#  			     $r];

	# The above commented region assumed that a packet traveresed
	# a node<->hub<->node<->link<->node<->hub<->node
	# when actually want a traversal of
	# node<->hub<->link<->hub<->node
Christopher Alfeld's avatar
Woops!  
Christopher Alfeld committed
363
	$delayinfo{$link} = [$bw,$delay,$loss];
364
	printdb "delayinfo{$link} = [" . join(",",@{$delayinfo{$link}}) . "]\n";
365 366 367 368 369 370 371 372 373 374 375
	
	# Remove the original link
	delete $links{$link};
    } elsif (($bw != 100) || ($loss != 0.0) || ($delay > $delaythresh)) {
	# We need a delay link
	$delayname = "delay$delayindex";
	$delayindex++;
	$nodetypes{$delayname} = "delay";
	$nodes{$delayname} = ["dsrc_$link","ddst_$link"];
	$links{"dsrc_$link"} = [$src,$delayname,0,0,0];
	$links{"ddst_$link"} = [$delayname,$dst,0,0,0];
376
	$delayinfo{$link} = [$bw,$delay,$loss];
377 378

	printdb "Created delay $delayname and links dsrc_$link and ddst_$link\n";
379
	printdb "delayinfo{$link} = [$bw,$delay,$loss]\n";
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
	
        # !!! - This could be improved (splice)
	$newsrclinks = [];
	foreach (@{$nodes{$src}}) {
	    if ($_ eq $link) {
		push(@$newsrclinks,"dsrc_$link");
	    } else {
		push(@$newsrclinks,$_);
	    }
	}
	$nodes{$src} = $newsrclinks;
	$newdstlinks = [];
	foreach (@{$nodes{$dst}}) {
	    if ($_ eq $link) {
		push(@$newdstlinks,"ddst_$link");
	    } else {
		push(@$newdstlinks,$_);
	    }
	}
	$nodes{$dst} = $newdstlinks;
	
	# Remove the original link
	delete $links{$link};
    }
}

# TOP file
# It's time to write out the top file.
Christopher Alfeld's avatar
Christopher Alfeld committed
408
$topfile = "$$.top";
409 410 411 412 413 414 415 416 417 418 419 420 421 422
open(TOPFILE,"> $topfile") || do {
    print STDERR "Could not open $topfile.";
    exit(1);
};

foreach $node (keys(%nodetypes)) {
    print TOPFILE "node $node $nodetypes{$node}\n";
}
foreach $link (keys(%links)) {
    ($src,$dst) = @{$links{$link}}[0,1];
    print TOPFILE "link $link $src $dst 100\n";
}
close(TOPFILE);

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
# Check for available resources
# This is very heuristic and won't work at all if our topology changes much.
open(PTOP,"$ptopfile");
$avail_sshelves = 0;
$avail_pcs = 0;
while (<PTOP>) {
    if (! /^node/) {next;}
    if (/shark-shelf/) {$avail_sshelves++;}
    if (/pc/) {$avail_pcs++;}
}
@t = keys(%sharkshelves);
$num_sshelves = $#t+1;
@t = keys(%nodes);
# Counting delay nodes as 1 when rwe really only need them as 1/2.  So..
$num_pcs = $#t+1 - int($delayindex/2);
if ($num_pcs > $avail_pcs) {
    print STDERR "Insufficient PCs to do mapping.  Have $avail_pcs; need $num_pcs.\n";
440
    exit(77);
441 442 443
}
if ($num_sshelves > $avail_sshelves) {
    print STDERR "Insufficient Shark Shelves to do mapping.  Have $avail_sshelves; need $num_sshelves.\n";
444
    exit(77);
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
# Run assign

# After assign finishes correctly several data structures will be
# filled out.
# v2pmap is indexed by virtual node name and contains the physical node name
# p2vmap is indexed by physical node name and contains the virtual node name
# plinks is indexed by input link name and contains:
#   ["intraswitch",srclink,srcmac,dstlink,dstmac]
#   ["interswitch",srclink,srcmac,dstlink,dstmac]
#   ["direct",plink,srcmac,dstmac]

# getmac takes a string in the format (mac0,mac1) where mac0,mac1 is either
# [a-f0-9]+ or (null) and returns the first non-null one.
sub getmac {
    $macstring=$_[0];
    ($A,$B) = ($macstring =~ /^\(([^,]+),([^,]+)\)$/);
    if ($A ne "(null)") {
	return $A;
    } else {
	return $B;
    }
};

471 472
#print "Running assign ($assign -b -t $ptopfile $topfile)\n";
#print "  Log in assign.log\n";
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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
$run = 1;
while ($run <= $maxrun) {
    printdb "run $run\n"; 
    open(ASSIGNFP,"$assign -b -t $ptopfile $topfile | tee assign.log |");
    
    # init some variables
    $violations=0;
    $score=-1;
    
    # read output
    # Header
    while (<ASSIGNFP>) {
	chop;
	/^With ([0-9]+) violations$/ && do {
	    $violations = $1;
	    last;
	};
	/^[ \t]+BEST SCORE: [ \t]+([0-9]+\.[0-9]+)/ && ($score=$1);
    }
    # We don't bother reading anything else if violations occured.
    if (($violations == 0) && ($score != -1)) {
	# read nodes section
	while (<ASSIGNFP> !~ /^Nodes:/) {}
	while (<ASSIGNFP>) {
	    chop;
	    /^End Nodes$/ && last;
	    @info = split;
	    ($virtual,$physical) = @info[0,2];
	    printdb "v2pmap{$virtual} = $physical\n";
	    printdb "p2vmap{$physical} = $virtual\n";
	    $v2pmap{$virtual} = $physical;
	    $p2vmap{$physical} = $virtual;
	}
	# read Edges
	while (<ASSIGNFP> !~ /^Edges:/) {}
	while (<ASSIGNFP>) {
	    /^End Edges$/ && last;
	    @info = split;
	    $line = $_;
	    $_ = $info[1]; # type
	  SWITCH1: {
	      /^intraswitch$/ && do {
		  ($vlink,$plinkA,$macA,$plinkB,$macB) = @info[0,2,3,4,5];
		  $plinks{$vlink} = ["intraswitch",$plinkA,
				     &getmac($macA),$plinkB,&getmac($macB)];
		  printdb "plinks{$vlink}=[" . join(",",@{$plinks{$vlink}}) . "]\n";
		  last SWITCH1;
	      };
	      /^interswitch$/ && do {
		  # Can either be:
		  # vlink "interswitch" plinkA macA plinkB macB plinkC macC
		  # or 
		  # vlink "interswitch" plinkA macA plinkB macB
		  # In the first case plinkB/macB will be a switch<->switch
		  # link that we ignore.
		  if ($#info == 7) {
		      ($vlink,$plinkA,$macA,$plinkB,$macB) = @info[0,2,3,6,7];
		  } else {
		      ($vlink,$plinkA,$macA,$plinkB,$macB) = @info[0,2,3,4,5];
		  }
		  $plinks{$vlink} = ["interswitch",$plinkA,
				     &getmac($macA),$plinkB,&getmac($macB)];
		  printdb "plinks{$vlink}=[" . join(",",@{$plinks{$vlink}}) . "]\n";
		  last SWITCH1;
	      };
	      /^direct$/ && do {
		  ($vlink,$plink,$mac) = @info[0,2,3];
		  ($macA,$macB) = ($mac =~ /^\(([^,]+),([^,]+)\)$/);
		  $plinks{$vlink} = ["direct",$plink,$macA,$macB];
		  printdb "plinks{$vlink}=[" . join(",",@{$plinks{$vlink}}) . "]\n";
		  last SWITCH1;
	      };
	      print "Found garbage: $line\n";
	  }
	}
Christopher Alfeld's avatar
Christopher Alfeld committed
548 549 550 551 552 553 554
    } else {
	# spit out up to nodes
	print "ASSIGN FAILED:\n";
	while (<ASSIGNFP>) {
	    if (/^Nodes:/) {last;}
	    print "$_";
	}
555
    }
Christopher Alfeld's avatar
Christopher Alfeld committed
556

557 558
    while (<ASSIGNFP>) { } # Read anything left in the pipe before closing

559 560 561
    close(ASSIGNFP);
    
    if ($violations > 0) {
562
	print "Run $run resulted in $violations violations.\n";
563 564 565
	$run++;
	next;
    } elsif ($score == -1) {
566
	print "No score reported???\n";
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	$run++;
	next;
    }

    last;
}

if (($violations != 0) || ($score == -1)) {
    print "Assign failed to find a valid solution - Exiting.\n";
    exit -1;
}

# Filling out IR file

# We now have all the information we need to fill out the /virtual and
# /vlan sections of the IR file as well as fill out the delay table of
# the database.

# node<->node
# /virtual/links
#   <vlink> <psrc> <pdst>
# /vlan
#   <vlink> <srcmac> <dstmac>

# node<->delay<->node
# /virtual/links
#   <vlink> <src> <delayA> <delayB> <dst>
# /vlan
#   <dsrc_vlink> <srcmac> <delayAmac>
#   <ddst_vlink> <delayBmac> <dstmac>

# lan<->delay<->lan
# /virtual/links
#   <vlink> <src> <delayA> <delayB> <dst>
# /vlan
#   put <delayAmac> into vlan for <src> and <delayBmac> into vlan for <dst>

# lan<->delay<->node
# /virtual/links
#   nothing
# /vlan
#   put <delayAmac> into vlan for the lan and add entry
#   <ddst/dsrc_vlink> <delayBmac> <nodemac>

# lan<->node
# /virtual/links 
#   nothing
# /vlan
#   put <nodemac> into vlan for lan

# We also will have a /virtual/nodes that has: <vnode> <pnode>
# In all cases we use the virtual lan name instead of the physical lan
# name, which has no persistant meaning.

# vlans will be indexed by vlan name and contain a list of mac addresses.
# vlinks is indexed by virtual link and contains the physical links
#   that make it up.

# nodename - assign returns <node>-<port> strings.  This returns the
# <node> part.
sub nodename {
    $s = $_[0];
    @r = split("-",$s);
    return $r[0];
};

# getlan - This just takes a <pnode>-<port> string and returns the 
# virtual lan or "".
sub getlan {
    $pnode = $_[0];
    if (defined($lans{$p2vmap{&nodename($pnode)}})) {
	return $p2vmap{&nodename($pnode)};
    } else {
	return "";
    }
};

# We initialize the vlans table for all lans
foreach $lan (keys(%lans)) {
    $vlans{$lan} = [];
}

649 650
@delaysection = ();

651 652 653 654
foreach (keys(%plinks)) {
    # ignore ddst_* as those will be handled when we handle dsrc_*
    /^ddst_.+$/ && next;

655 656
    $vlink = $_;

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    @info = @{$plinks{$_}};
    ($type,$plinkA,$macA) = @info[0,1,2];
    if ($type ne "direct") {
	($plinkB,$macB) = @info[3,4];
    } else {
	$plinkB = "";
	$macB = $info[3];
    }
    printdb "Looking at $_ ($type,$plinkA,$macA,$plinkB,$macB)\n";
    if (/^dsrc_(.+)$/) {
	# delayed link
	# read in second half
	$vlink=$1;
	@info2 = @{$plinks{"ddst_$vlink"}};
	($type2,$plinkA2,$macA2) = @info2[0,1,2];
	if ($type2 ne "direct") {
	    ($plinkB2,$macB2) = @info2[3,4];
	} else {
	    $plinkB2 = "";
	    $macB2 = $info2[3];
	}
	printdb "Delayed link, second half = ($type2,$plinkA2,$macA2,$plinkB2,$macB2)\n";

	# We have a total of 16 possibilities for delay links.
	# Luckily it comes in two dimensions that can be handled
	# seperately.

	# now we sort into src, delayA, delayB, and dst.
	# possibilities:
	#   A & A2
	#   A & B2
	#   B & A2
	#   B & B2
	if (&nodename($plinkA) eq &nodename($plinkA2)) {
	    $src = $plinkB;$srcmac = $macB;
	    $delayA = $plinkA;$delayAmac = $macA;
	    $delayB = $plinkA2;$delayBmac = $macA2;
	    $dst = $plinkB2;$dstmac = $macB2;
	} elsif (&nodename($plinkA) eq &nodename($plinkB2)) {
	    $src = $plinkB;$srcmac = $macB;
	    $delayA = $plinkA;$delayAmac = $macA;
	    $delayB = $plinkB2;$delayBmac = $macB2;
	    $dst = $plinkA2;$dstmac = $macA2;
	} elsif (&nodename($plinkB) eq &nodename($plinkA2)) {
	    $src = $plinkA;$srcmac = $macA;
	    $delayA = $plinkB;$delayAmac = $macB;
	    $delayB = $plinkA2;$delayBmac = $macA2;
	    $dst = $plinkB2;$dstmac = $macB2;
	} elsif (&nodename($plinkB) eq &nodename($plinkB2)) {
	    $src = $plinkA;$srcmac = $macA;
	    $delayA = $plinkB;$delayAmac = $macB;
	    $delayB = $plinkB2;$delayBmac = $macB2;
	    $dst = $plinkA2;$dstmac = $macA2;
	}
711 712 713 714

	push(@delaysection,&nodename($delayA) . " " . join(" ",@{$delayinfo{$vlink}}) . " $delayAmac $delayBmac");

#	push(@{$delayinfo{$p2vmap{&nodename($delayA)}}},($delayAmac,$delayBmac));
715
	printdb "src=$src ($srcmac) delayA=$delayA ($delayAmac) delayB=$delayB ($delayBmac) dst=$dst ($dstmac)\n";
716
#	printdb "delayinfo{" . $p2vmap{&nodename($delayA)} . "}=[" . join(",",@{$delayinfo{$p2vmap{&nodename($delayA)}}}) . "]\n";
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

	# now we have another four possibilities:
	if (&getlan($src) && &getlan($dst)) {
	    # lan<->delay<->lan
	    $srclan = &getlan($src);
	    push(@{$vlans{$srclan}},$delayAmac);
	    $dstlan = &getlan($dst);
	    push(@{$vlans{$dstlan}},$delayBmac);
	    $vlinks{$vlink} = [$srclan,$delayA,$delayB,$dstlan];
	    printdb "lan<->delay<->lan - $delayAmac->$srclan, $delayBmac->$dstlan\n";
	    printdb "vlinks{$vlink}=[" . join(" ",@{$vlinks{$vlink}}) . "]\n";
	} elsif (&getlan($dst)) {
	    # node<->delay<->lan
	    $lan = &getlan($dst);
	    push(@{$vlans{$lan}},$delayBmac);
	    $vlans{"dsrc_$vlink"} = [$srcmac,$delayAmac];
	    printdb "node<->delay<->lan - dsrc_$vlink = [$srcmac,$delayAmac], $delayBmac->$lan\n";
	} elsif (&getlan($src)) {
	    # lan<->delay<->node
	    $lan = &getlan($src);
	    push(@{$vlans{$lan}},$delayAmac);
	    $vlans{"dsrc_$vlink"} = [$delayBmac,$dstmac];
	    printdb "lan<->delay<->node - ddst_$vlink = [$delayBmac,$dstmac], $delayAmac -> $lan\n";
	} else {
	    # node<->delay<->node
	    $vlans{"dsrc_$vlink"} = [$srcmac,$delayAmac];
	    $vlans{"ddst_$vlink"} = [$delayBmac,$dstmac];
	    $vlinks{$vlink} = [$src,$delayA,$delayB,$dst];
	    printdb "node<->delay<->node - dsrc_$vlink = [$srcmac,$delayAmac], ddst_$vlink = [$delayBmac,$dstmac]\n";
	    printdb "vlinks{$vlink}=[" . join(",",@{$vlinks{$vlink}}) . "]\n";
	}
    } else {
	# undelayed link
	printdb "Undelayed link\n";
	if (&getlan($plinkA) || &getlan($plinkB)) {
	    # lan<->node - i.e. the node belongs in the lan
	    $lan = &getlan($plinkA);
	    $mac = $macB;
	    if (! $lan) {
		$lan = &getlan($plinkB);
		$mac = $macA;
	    }
	    push(@{$vlans{$lan}},$mac);
	    printdb "lan<->node - $mac->$lan\n";
	} else {
	    # node<->node
	    $vlans{$_} = [$macA,$macB];
	    $vlinks{$_} = [$plinkA,$plinkB];
	    printdb "node<->node $_=[$macA,$macB]\n";
	    printdb "vlinks{$_}=[" . join(",",@{$vlinks{$_}}) . "]\n";
	}
    }
}

# Open IR file for appending
# This is just a matter of dumping data structures

open(IRFILE,">>$irfile") || do {
    print STDERR "Could not open $irfile for appending.\n";
    exit(1);
};

print IRFILE "START virtual\n";
print IRFILE "START nodes\n";
foreach $vnode (keys(%v2pmap)) {
Christopher Alfeld's avatar
Christopher Alfeld committed
782
    if (defined($sharkshelves{$vnode})) {
783
	$i = 8;
Christopher Alfeld's avatar
Christopher Alfeld committed
784 785
	foreach $vshark (@{$sharkshelves{$vnode}}) {
	    print IRFILE "$vshark $v2pmap{$vnode}-$i\n";
786
	    $i--;
Christopher Alfeld's avatar
Christopher Alfeld committed
787 788
	}
    } elsif (! defined($lans{$vnode})) {
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	print IRFILE "$vnode $v2pmap{$vnode}\n";
    }
}
print IRFILE "END nodes\n";

print IRFILE "START links\n";
foreach $vlink (keys(%vlinks)) {
    print IRFILE "$vlink " . join(" ",@{$vlinks{$vlink}}) . "\n";
}
print IRFILE "END links\n";
print IRFILE "END virtual\n";

print IRFILE "START vlan\n";
foreach $vlan (keys(%vlans)) {
    print IRFILE "$vlan " . join(" ",@{$vlans{$vlan}}) . "\n";
}
print IRFILE "END vlan\n";

print IRFILE "START delay\n";
808 809 810 811 812
#foreach $delay (keys(%delayinfo)) {
#    print IRFILE $v2pmap{$delay} . " " . join(" ",@{$delayinfo{$delay}}) . "\n";
#}
foreach (@delaysection) {
    print IRFILE $_ . "\n";
813 814 815 816 817 818 819 820 821 822 823 824 825
}
print IRFILE "END delay\n";

close IRFILE;


# We're done! - Now the calling script will make sure the resources are
# available, reserve them, and call postassign to copy the data into the
# DB.

print "Assignment complete\n";

0;