assign_wrapper.in 30.7 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl -w

# This function as the main assign loop.  It converts the virtual
# topology into a top input including LAN and delay translation.  It
# then snapshots the current testbed physical state and runs assign,
# looping a couple times if assign fails.  When assign successfully
# completes it will interpret the results.  Attempt to match any
# existing portmap entries and then update the delays and vlans table.

# Syntax: assign_wrapper <pid> <eid>

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

# Settings
# delaythresh is the maximum delay in ms above which a delay node is needed.
# maxrun is maximum number of times we run assign.
18
19
# delaywithswitch if 1 will use the switch to delay when possible.  Currently
#  this only works for 10mbit links.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
20
21
$delaythresh = 3;
$maxrun = 5;
22
$delaywithswitch=0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23
24
25

use DBI;

26
$DELAYCAPACITY = @DELAYCAPACITY@;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
27
28
29
$TBROOT = "@prefix@";
$ENV{'PATH'} = "/usr/bin:$TBROOT/libexec:$TBROOT/sbin:$TBROOT/bin";
$TBDB = "@TBDBNAME@";
30
31
32
33
$TBLIB = "$TBROOT/lib";
push(@INC,$TBLIB);

require exitonwarn;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
85
86
87
88
89
90
91
92
93
94
95
96

$dbh = DBI->connect("DBI:mysql:database=$TBDB;host=localhost") || 
    die "Could not connect to DB.\n";

$verbose = 0;
if (($#ARGV == 2) && ($ARGV[0] eq "-v")) {
    $verbose = 1;
    shift;
} elsif ($#ARGV != 1) {
    print STDERR "Syntax: $0 pid eid\n";
    exit(1);
}
($pid,$eid) = @ARGV;

$ptopfile = "$pid-$eid-$$.ptop";

sub printdb {
    if ($verbose) {
	print $_[0];
    }
};

######################################################################
# Step 1 - Setup virtual topology
#
# Here we need to read the virtual topology in from the virt_nodes
# and virt_lans table.  We then need to add delay and lan nodes as
# necessary.
#
# Conversion details:
#   Let L be a LAN with N members.
#   If N == 2 
#      Let N1 be node 1
#      Let N2 be node 2
#      If L is delayed
#         Generate delay node D
#         Link N1 to D
#         Link N2 to D
#      Else
#         Link N1 to N2
#   Else
#      Generate lan node A
#      If L is delayed
#        Foreach node N in L
#           Generate delay node DN
#           Link A to DN
#           Link N to DN
#      Else
#        Foreach node N in L
#           Link N to A
#
# Datastructures:
#  nodes is indexed by virtual node and contains the node type.
#  nodelans is indexed by virtual node and contains a list of
#    <port>:<lan> that it is connected to.
#  ips is indexed by nodeport and contains the IP addresses.
#  lans is indexed by virtual lan and is a list of nodeport members.
#  delayinfo is indexed by virtual lan:node:port and is a list of delay,
#    bandwidth, lossrate.
#  okbandwidths is indexed by bandwidth and is just a set.
#  lannodes is indexed by physical name is the set of lan nodes.
#  interfacespeed is indexed by type and contains the bandwidth.
#  delaynodes is indexed by link name and contains [delay,bw,loss]
97
98
99
100
#  portbw is indexed by virtual nodeport and contains the bandwidth
#    of that port.  Note that the bandwidth of a port is one of 100
#    or 10.  For LANs with other bandwidths the port speed will be
#    100 and a delay node will be inserted.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
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
167
168
169
170
#
# Delay node names:
#  delay nodes are named tbdelayXX N > 2
#   and tbsdelayXX for N == 2.
# 
# Lan node nameS:
#  lan nodes are named lan/<virtual lan>
######################################################################

# Shark Hack
# For each LAN we replace all the sharks in the LAN with a single
# shark shelf node.  After this goes through assign we pull them
# all back out.
#
# sharkshelves is indexed by virtual shelf name and is a list of
# the virtual nodes in it.
# sharkshelfid is used to generate ids for shark shelves.
$sharkshelfid = 0;

# delayid is used to generate ids for delay nodes.
$delayid = 0;

printdb "Generating TOP file.\n";

# Let's figure out what kind of links we have.
printdb "Finding interface speeds:";
$sth=$dbh->prepare("SELECT type,max_speed from interface_types");
$sth->execute;
while (($type,$bandwidth) = $sth->fetchrow_array) {
    $okbandwidths{$bandwidth} = 1;
    $interfacespeed{$type} = $bandwidth;
    printdb " $bandwidth";
}
$sth->finish;
printdb "\n";

printdb "Loading virt_nodes.\n";
$sth = $dbh->prepare("SELECT vname,ips,type from virt_nodes" .
		     " where pid=\"$pid\" and eid=\"$eid\"");
$sth->execute;
while (($vname,$ips,$type) = $sth->fetchrow_array) {
    printdb "  $vname $type $ips\n";
    # We need to check the names to make sure they won't clash with
    # our internal delay node names.
    if (($vname =~ /^tbdelay\d+/) ||
	($vname =~ /^tbsdelay\d+/)) {
	print STDERR "Warning: $vname is a reserved name.  Working around.\n";
	($num) = ($vname =~ /(\d+)/);
	$delayid = $num + 1;
    }
    $nodes{$vname} = $type;
    $nodelans{$vname} = [];
    foreach $ipinfo (split(" ",$ips)) {
	($port,$ip) = split(":",$ipinfo);
	$ips{"$vname:$port"} = $ip;
    }
}
$sth->finish;

printdb "Loading virt_lans.\n";
$sth = $dbh->prepare("SELECT vname,member,delay,bandwidth,lossrate" .
		     " from virt_lans where pid=\"$pid\" and eid=\"$eid\"");
$sth->execute;
while (($vname,$member,$delay,$bandwidth,$lossrate) = $sth->fetchrow_array) {
    if (! defined($lans{$vname})) {
	$lans{$vname} = [];
    }
    push(@{$lans{$vname}},$member);
    $delayinfo{"$vname:$member"} = [$delay,$bandwidth,$lossrate];
    ($node,$port) = split(":",$member);
171
172
173
174
175
    if ($bandwidth == 10) {
	$portbw{$member} = 10;
    } else {
	$portbw{$member} = 100;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
176
    push(@{$nodelans{$node}},"$port:$vname");
177
    printdb "  portbw of $member = $portbw{$member}\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
178
179
180
181
182
183
184
185
186
187
188
189
    printdb "  $vname $member - $delay $bandwidth $lossrate\n";
    printdb "    Added $port:$vname to nodelans of $node\n";
}
$sth->finish;

# Shark hack
foreach $lan (keys(%lans)) {
    $realmembers = [];
    $sharks = [];
    $hassharks = 0;
    foreach $member (@{$lans{$lan}}) {
	($node) = (split(":",$member))[0];
190
	if (($nodes{$node} eq "shark") || ($nodes{$node} eq "dnard")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
191
192
193
194
195
196
197
198
199
200
201
202
	    push(@$sharks,$member);
	    $hassharks = 1;
	} else {
	    push(@$realmembers,$member);
	}
    }
    if ($hassharks) {
	$shelfid = "sharkshelf$sharkshelfid";
	printdb "  Creating shark shelf: $shelfid (" . 
	    join(" ",@$sharks) . ")\n";
	$sharkshelfid++;
	$sharkshelves{$shelfid} = $sharks;
203
	$delayinfo{"$lan:$shelfid:uplink"} = [0,100,0.0];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
	push(@$realmembers,"$shelfid:uplink");
	$nodes{$shelfid} = "shark-shelf";
    }
    $lans{$lan} = $realmembers;
}
# End shark hack

# min(a,b)
# Returns the minimum of a and b.
sub min {
    my ($a,$b) = @_;
    return ($a < $b ? $a : $b);
};

# getbandwidth(bw)
# Returns the lowest ok bandwidth that is greater than or equal to
# the one passed.
sub getbandwidth {
    my $targetbandwidth= $_[0];
    my $bandwidth;
    my $best = 10000000000;
    foreach $bandwidth (keys(%okbandwidths)) {
	if (($bandwidth >= $targetbandwidth) && ($bandwidth < $best)) {
	    $best = $bandwidth;
	}
    }
    return $best;
};

# Open the TOP file
$topfile = "$pid-$eid-$$.top";
open(TOPFILE,"> $topfile") || do {
236
    print STDERR "$0: *** Could not open $topfile.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
237
238
239
    exit(1);
};

240
241
242
243

$nodes=0;
$delaynodes=0;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
244
245
foreach $node (keys(%nodes)) {
    # Shark hack
246
    if (($nodes{$node} ne "shark") && ($nodes{$node} ne "dnard")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
247
	print TOPFILE "node $node $nodes{$node}\n";
248
249
250
	if ($nodes{$node} ne "shark-shelf") {
	    $nodes++;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
251
252
253
254
255
256
    }
    # End Shark hack
}

foreach $lan (keys(%lans)) {
    @members = @{$lans{$lan}};
257
    printdb "$lan - " . join(" ",@members) . "\n";
258
259
260
261
262
263
264
265
266
267
268
    # Shark hack for rvr
    $sharks = 0;
    $nonsharks = 0;
    foreach $member (@members) {
	($node) = (split(":",$member))[0];
	if ($nodes{$node} eq "shark-shelf") {
	    $sharks++;
	} else {
	    $nonsharks++;
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
269
270
271
272
273
274
    if ($#members == 1) {
	($nodeport0,$nodeport1) = @members;
	$node0 = (split(":",$nodeport0))[0];
	$node1 = (split(":",$nodeport1))[0];
	($delay0,$bw0,$loss0) = @{$delayinfo{"$lan:$nodeport0"}};
	($delay1,$bw1,$loss1) = @{$delayinfo{"$lan:$nodeport1"}};
275
276
	$delay = $delay0+$delay1;
	$loss = 1-(1-$loss0)*(1-$loss1);
277
	$bw = &min($bw0,$bw1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
278
	$bandwidth = &getbandwidth(&min($bw0,$bw1));
279
	if (($delay > $delaythresh) ||
280
	    (($bw != 100) && ($bw != 10)) ||
281
282
	    (($delaywithswitch == 0) && 
	     (($bw != 100) && (($sharks == 0) || ($nonsharks > 1)))) || 
283
	    ($loss != 0)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284
	    $delayname = "tbsdelay$delayid";
285
	    $delaynodes{"linksdelaysrc/$lan"} = [$delay,$bw,$loss];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
286
287
288
289
	    printdb "Delay node linksdelaysrc/$lan ($delayname) = " . 
		join(" ",@{$delaynodes{"linksdelaysrc/$lan"}}) . "\n";
	    $delayid++;
	    print TOPFILE "node $delayname delay\n";
290
	    $delaynodes++;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
291
292
293
294
295
296
297
	    print TOPFILE "link linksdelaysrc/$lan $node0 $delayname"
		. " $bandwidth\n";
	    print TOPFILE "link linksdelaydst/$lan $node1 $delayname"
		. " $bandwidth\n";
	} else {
	    print TOPFILE "link linksimple/$lan $node0 $node1 $bandwidth\n";
	}
298
    } elsif ($#members != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
299
300
301
302
303
304
	print TOPFILE "node lan/$lan lan\n";
	$lannodes{"lan/$lan"} = 1;
	foreach $member (@members) {
	    ($delay,$bw,$loss) = @{$delayinfo{"$lan:$member"}};
	    $bandwidth = &getbandwidth($bw);
	    ($node) = (split(":",$member))[0];
305
306
# XXX The expression below should be modified for better bandwidth support.
# Probably needs to happen post assign somehow.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
307
	    if (($delay > $delaythresh) ||
308
		(($bw != 100) && ($bw != 10))  ||
309
310
		(($delaywithswitch == 0) && 
		 (($bw != 100) && (($sharks == 0) || ($nonsharks > 1)))) || 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
311
312
313
314
315
316
317
		($loss != 0)) {
		$delayname = "tbdelay$delayid";
		$delaynodes{"linkdelaysrc/$lan/$member"} = [$delay,$bw,$loss];
		printdb "Delay node linkdelaysrc/$lan/$member ($delayname) " .
		    " = " . join(" ",@{$delaynodes{"linkdelaysrc/$lan/$member"}}) . "\n";
		$delayid++;
		print TOPFILE "node $delayname delay\n";
318
		$delaynodes++;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
319
320
321
322
323
324
325
326
327
328
		print TOPFILE "link linkdelaysrc/$lan/$member" .
		    " $node $delayname $bandwidth\n";
		print TOPFILE "link linkdelaydst/$lan/$member" .
		    " lan/$lan $delayname $bandwidth\n";
	    } else {
		print TOPFILE "link linklan/$lan/$member $node lan/$lan" .
		    " $bandwidth\n";
	    }
	}
    }
329
    # If a LAN has only one member we don't do anything.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
330
331
332
333
}

close TOPFILE;

334
335
336
337
338
339
340
341
# Set estimations
$minimum_nodes = $nodes + $delaynodes/$DELAYCAPACITY;
$maximum_nodes = $nodes + $delaynodes;
$dbh->do("UPDATE experiments set maximum_nodes=$maximum_nodes, " .
	 "minimum_nodes=$minimum_nodes where pid=\"$pid\" and eid=\"$eid\"");
print "Minumum nodes = $minimum_nodes\n";
print "Maximum nodes = $maximum_nodes\n";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
######################################################################
# Step 2 - Assign Loop
# 
# Here we loop up to maxrun times.  In each loop we snapshot the
# current testbed state into a ptop file.  We then run assign.  If
# assign succeeds we attempt to reserve the resources.  If that works
# we're done with step 2 otherwise we loop again.
#
# v2pmap is indexed by virtual and contains the physical node.
# p2vmap is indexed by physical and contains the virtual node
# plinks is indexed by virtual name and contains 
#  (pnodeportA,pnodeportB) .  If one is a delay node it is always
#  the second.
#######################################################################

# getnodeport(s)
# Takes a ports result from assign (mac0,mac1) and returns the
# first non-null one.
sub getnodeport {
    $macstring=$_[0];
    ($A,$B) = ($macstring =~ /^\(([^,]+),([^,]+)\)$/);
    if ($A ne "(null)") {
	return $A;
    } else {
	return $B;
    }
};

# isdelay(pnodeport)
# Takes a physical nodeport and retruns 1 if the node is a delay node.
sub isdelay {
    my $pnodeport = $_[0];

    my $node = (split(":",$pnodeport))[0];
    if (($p2vmap{$node} =~ m|^delay/|) ||
	($p2vmap{$node} =~ m|^sdelay.|)) {
	return 1;
    } 
    return 0;
};

$currentrun = 1;
while (1) {
    print "Assign Run $currentrun\n";

    # Violation counts
    $linkusers = -1;
    $bandwidth = -1;
    $desires = -1;

    # Clear v2pmap, p2vmap, and plinks
    undef %v2pmap;
    undef %p2vmap;
    undef %plinks;

    # Snapshot
    system("ptopgen > $ptopfile");

    # Get number of nodes
    $numnodes = $dbh->do("select a.node_id,a.type from" .
		     " nodes as a left join reserved as b" .
		     " on a.node_id=b.node_id" .
		     " where b.node_id is null" .
405
		     " and a.role='testnode' and a.type!='dnard'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
406
407
    
    if ($numnodes < $minimum_nodes) {
408
	print STDERR "$0: *** Insufficient nodes available.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
	exit(2);
    }

    # Run assign
    $fail = 0;
    print "assign -b -t $ptopfile $topfile\n";
    open(ASSIGNFP,"assign -b -t $ptopfile $topfile | tee assign.log |");
    $violations = 0;
    $score = -1;

    # read output
    # Header
    printdb "Reading assign results.\n";
    while (<ASSIGNFP>) {
	chop;
424
425
426
427
	/No physical nodes of type (.+)$/ && do {
	    $score=-2;
	    print $_ . "\n";
	};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
428
429
430
431
432
433
	/^With ([0-9]+) violations$/ && do {
	    $violations = $1;
	    last;
	};
	/^[ \t]+BEST SCORE: [ \t]+([0-9]+\.[0-9]+)/ && ($score=$1);
    }
434
435
436
437
438
    if ($score == -2) {
	# Type error
	print "Giving up.\n";
	exit(2);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
    printdb "Found score $score, violations $violations.\n";

    # We don't bother reading anything else if violations occured.
    if (($violations == 0) && ($score != -1)) {
	# read nodes section
	while (<ASSIGNFP> !~ /^Nodes:/) {}
	printdb "Nodes:\n";
	while (<ASSIGNFP>) {
	    chop;
	    /^End Nodes$/ && last;
	    @info = split;
	    ($virtual,$physical) = @info[0,2];
	    $v2pmap{$virtual} = $physical;
	    $p2vmap{$physical} = $virtual;
	    printdb "  $virtual $physical\n";
	}

	# read Edges
	# By convention, in plinks, the delay node is always the second
	# entry.
	while (<ASSIGNFP> !~ /^Edges:/) {}
	printdb "Edges:\n";
	while (<ASSIGNFP>) {
	    /^End Edges$/ && last;
	    @info = split;
	    $line = $_;
	    $_ = $info[1]; # type
	  SWITCH1: {
	      /^intraswitch$/ && do {
		  ($vlink,$rawA,$rawB) = @info[0,3,5];
		  last SWITCH1;
	      };
	      /^interswitch$/ && do {
472
473
		  ($vlink,$rawA,$rawB) = @info[0,3,$#info];
		  last SWITCH1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
474
475
	      };
	      /^direct$/ && do {
476
		  print STDERR "$0: *** Unsupported link type: direct.\n";
477
		  exit(1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
478
479
480
	      };
	      print "Found garbage: $line\n";
	  }
481
482
483
484
485
486
487
488
489
	    $nodeportA = &getnodeport($rawA);
	    $nodeportB = &getnodeport($rawB);
	    $nodeportA =~ s/\//:/;
	    $nodeportB =~ s/\//:/;
	    if (&isdelay($nodeportB)) {
		$plinks{$vlink} = [$nodeportA,$nodeportB];
	    } else {
		$plinks{$vlink} = [$nodeportB,$nodeportA];
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
	    printdb "  $vlink " . join(" ",@{$plinks{$vlink}}) . "\n";
	}
    } else {
	# spit out up to nodes
	print "ASSIGN FAILED:\n";       
	while (<ASSIGNFP>) {
	    if (/link_users: (\d+)$/) {
		$linkusers = $1;
	    } elsif (/bandwidth: (\d+)$/) {
		$bandwidth = $1;
	    } elsif (/desires: (\d+)$/) {
		$desires = $1;
	    }
	    if (/^Nodes:/) {last;}
	    print "$_";
	}
	$fail = 1;
    }
    while (<ASSIGNFP>) { } # Read anything left in the pipe before closing
    close(ASSIGNFP);

    # Reserve resources
    if ($fail == 0) {
	print "Reserving resources.\n";
	@toreserve = ();
	# We don't reserve LAN nodes.
	foreach $node (keys(%p2vmap)) { 
	    if (! defined($lannodes{$p2vmap{$node}})) {
		push(@toreserve,$node);
	    }
	}
	if (system("nalloc $pid $eid " . join(" ",@toreserve))) {
	    print "Failed to reserve resources.  Trying again.\n";
	} else {
	    print "Successfully reserved resources.  Finishing setup.\n";
	    last;
	}
    }

    # Check for exit
    if ($currentrun >= $maxrun) {
	$exitcode = 1;
	if ($bandwidth > 0) {
	    $exitcode += 4;
	} 
	if ($linkusers > 0) {
	    $exitcode += 8;
	}
	if ($desires > 0) {
	    $exitcode += 16;
	}
541
	print "$0: *** Reached run limit.  Giving up.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
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
649
650
651
652
653
654
655
656
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
711
712
713
714
715
716
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
	exit($exitcode);
    }
    $currentrun++;
}


######################################################################
# Step 3 - Convert to vlans, delays, and portmap
# 
# Here we convert the plinks into vlans, delays, and portmap.  We
# convert them first into internal datastructure.  After Step 4
# when we do some port swapping we'll upload the modified versions
# of these structures into the database.
#
# vlans is indexed by an internal ID contains:
#  [vname, members]
# delays is indexed by an internal ID and contains:
#  [pnode, int0, int1, vname, delay, bandwidth, lossrate]
# portmap is indexed by <virtual node>:<virtual port> and contains
#  the physical port.
# nodevlans is indexed by physical node and contains the VLANs
#  it's interfaces are in.  It does not contain entries for delay
#  nodes.
#
# vlan ids
#  vlan ids are increasing integers in the case of node<->delay connections.
#  In the case of actual LANs either of real node or of delay nodes
#  they are indexed by virtual lan name.
# delay ids
#  delay ids are increasing integers.  We could have used a list of
# delays just as well.  Having it as an array may prove useful for
# future changes however.
######################################################################

# find_vport vnode virtlan
# This finds and returns the virtual port of vnode that it is
# virtlan and not in portmap.
sub find_vport {
    my ($vnode,$virtlan) = @_;
    my $portlan;
    my $vport;
    my $lan;
    # Shark Hack
    if ($nodes{$vnode} eq "shark-shelf") {
	return "0";
    }
    # End Shark Hack
    foreach $portlan (@{$nodelans{$vnode}}) {
	($vport,$lan) = split(":",$portlan);
	if (($virtlan eq $lan) &&
	    (! defined($portmap{"$vnode:$vport"}))) {
	    return $vport;
	}
    }
    return "";
};

$vlanid = 0;
$delayid = 0;
foreach $pnode (keys(%p2vmap)) {
    $nodevlans{$pnode} = [];
}

printdb "Interpreting results.\n";
foreach $plink (keys(%plinks)) {
    ($nodeportA,$nodeportB) = @{$plinks{$plink}};
    ($nodeA,$portA) = split(":",$nodeportA);
    ($nodeB,$portB) = split(":",$nodeportB);
    printdb "plink $plink - $nodeportA $nodeportB\n";
    if (($lan) = ($plink =~ m|^linksdelaysrc/(.+)$|)) {
	# Node has a single entry in lan.
	# Node is nodeportA
	# Delay node is nodeportB
	# Other end of delay node will be given by plink
	#   linksdelaydst/lan where nodeportA will be the other node in
	#   the virtual LAN and nodeportB will be the other end of the
	#   delay node.

	($nodeportC,$nodeportD) = @{$plinks{"linksdelaydst/$lan"}};
	($nodeC,$portC) = split(":",$nodeportC);
	($nodeD,$portD) = split(":",$nodeportD);
	printdb "LINK delay: other end = $nodeportC $nodeportD\n";

	# assert nodeB == nodeD

	printdb "  VLANS:\n";
	$vlans{$vlanid} = [$lan,[$nodeportA, $nodeportB]];
	push(@{$nodevlans{$nodeA}},$vlanid);
	printdb "    $vlanid = $lan, \[$nodeportA, $nodeportB\]\n";
	$vlanid++;
	$vlans{$vlanid} = [$lan,[$nodeportC, $nodeportD]];
	push(@{$nodevlans{$nodeC}},$vlanid);
	printdb "    $vlanid = $lan, \[$nodeportC, $nodeportD\]\n";
	$vlanid++;

	($delay,$bandwidth,$lossrate) = @{$delaynodes{$plink}};
	$delays{$delayid} = [$nodeB,$portB,$portD,$lan,
			     $delay,$bandwidth,$lossrate];
	printdb "  Delays: $delayid = \[$nodeB,$portB,$portD,$lan" .
	    ",$delay,$bandwidth,$lossrate\]\n";
	$delayid++;

	$virtA = &find_vport($p2vmap{$nodeA},$lan);
	$virtC = &find_vport($p2vmap{$nodeC},$lan);
	$portmap{"$p2vmap{$nodeA}:$virtA"} = $portA;
	$portmap{"$p2vmap{$nodeC}:$virtC"} = $portC;
	printdb "  Portmap:\n";
	printdb "    $p2vmap{$nodeA}:$virtA = $portA\n";
	printdb "    $p2vmap{$nodeC}:$virtC = $portC\n";
    } elsif (($lan,$member) = ($plink =~ m|^linkdelaysrc/([^/]+)/(.+)$|)) {
	# Node may have multiple entries in lan.
	# Delay node is nodeB and portB.
	# Other end of delay node will be given by plink
	#  linkdelaydst/lan/node where nodeportA will the LAN node and
	#  nodeportB will be the other end of the delay node.
	($nodeportC,$nodeportD) = @{$plinks{"linkdelaydst/$lan/$member"}};
	($nodeC,$portC) = split(":",$nodeportC);
	($nodeD,$portD) = split(":",$nodeportD);
	printdb "LAN delay src: other end = $nodeportC $nodeportD\n";
	
	$vlans{$vlanid} = [$lan,[$nodeportA, $nodeportB]];
	push(@{$nodevlans{$nodeA}},$vlanid);
	printdb "  VLANS:\n";
	printdb "    $vlanid = $lan, \[$nodeportA $nodeportB\]\n";
	$vlanid++;
	
	if (! defined($vlans{$lan})) {
	    $vlans{$lan} = [$lan,[]];
	}
	$members = (@{$vlans{$lan}})[1];
	push(@$members,$nodeportD);
	printdb "    $lan = $lan, \[" . join(" ",@$members) . "\]\n"; 

	($delay,$bandwidth,$loss) = @{$delaynodes{$plink}};
	$delays{$delayid} = [$nodeB,$portB,$portD,$lan,
			     $delay,$bandwidth,$loss];
	printdb "  Delays: $delayid = \[$nodeB,$portB,$portD,$lan," .
	    "$delay,$loss\]\n";
	$delayid++;
	
	$virt = &find_vport($p2vmap{$nodeA},$lan);
	$portmap{"$p2vmap{$nodeA}:$virt"} = $portA;
	printdb "  Portmap: $p2vmap{$nodeA}:$virt = $portA\n";
    } elsif (($lan) = ($plink =~ m|^linksimple/(.+)$|)) {
	# nodeportA and nodeportB are the only two nodes in the
	# virtual LAN and there are no delays.
	printdb "Simple Link\n";
	$vlans{$lan} = [$lan,[$nodeportA,$nodeportB]];
	printdb "  VLANS: $lan = $lan, \[$nodeportA $nodeportB\]\n";
	
	push(@{$nodevlans{$nodeA}},$lan);
	push(@{$nodevlans{$nodeB}},$lan);
	
	$virtA = &find_vport($p2vmap{$nodeA},$lan);
	$virtB = &find_vport($p2vmap{$nodeB},$lan);
	$portmap{"$p2vmap{$nodeA}:$virtA"} = $portA;
	$portmap{"$p2vmap{$nodeB}:$virtB"} = $portB;
	printdb "  Portmap:\n";
	printdb "    $p2vmap{$nodeA}:$virtA = $portA\n";
	printdb "    $p2vmap{$nodeB}:$virtB = $portB\n";
    } elsif (($lan) = ($plink =~ m|^linklan/([^/]+)/.+$|)) {
	# node may be the LAN multiple times.
	# nodeportA is the node.
	# nodeportB is the LAN
	# No delays
	printdb "LAN\n";
	if (! defined($vlans{$lan})) {
	    $vlans{$lan} = [$lan,[]];
	}
	$members = (@{$vlans{$lan}})[1];
	push(@$members,$nodeportA);
	printdb "  VLANS: $lan = $lan,\[" . join(" ",@$members) . "\]\n";

	$virt = &find_vport($p2vmap{$nodeA},$lan);
	$portmap{"$p2vmap{$nodeA}:$virt"} = $portA;
	printdb "  Portmap: $p2vmap{$nodeA}:$virt = $portA\n";
    }
    # Else delaysrc case, will be handled by one of the other cases.
}

# At this point we know our vlans, delays, and the portmap.  We could
# just dump everything to the DB and be done.  However, we have an 
# opportunity to preserve port mappings across swap ins.  We do this
# by looking at the portmap table in the DB which will contain the
# portmapping we found the last time we swapped in.  We're going
# to attempt to swap things around so that our portmap matches that
# portmap.  We can swap two ports if they have the same bandwidth and
# physically connect to the same location.  Swapping involves swapping
# node:portA and node:portB in every VLAN.  We use the vlans array
# to avoid having to search through every VLAN on every swap.
# Let's read in the current portmap, if any.
#

# We can just skip this step if this is the first portmap.  If a
# portmap exists but does not have the same node:vport's in it that
# our portmap does then we have a problem.  We could try to do
# something intelligent, but the main cause of this is an uncleared
# previous version of the experiment.  So what we actually do is
# clear the previous portmap out of the DB, display a warning, and
# skip as if there was no portmap.

# keys_equal A B
# Returns 1 if hashs A and B have the same keys.
sub keys_equal {
    my ($ARef,$BRef) = @_;
    my @AKeys = sort keys(%$ARef);
    my @BKeys = sort keys(%$BRef);
    if ($#AKeys != $#BKeys) {
	return 0;
    }
    my $i;
    for ($i=0;$i<=$#AKeys;++$i) {
	if ($AKeys[$i] ne $BKeys[$i]) {
	    return 0;
	}
    }
    return 1;
};

printdb "Considering Port Swapping\n";

%prevportmap = ();
$sth = $dbh->prepare("SELECT vnode,vport,pport from portmap" .
		     " where pid = \"$pid\" and eid = \"$eid\"");
$sth->execute;
while (($vnode,$vport,$pport) = $sth->fetchrow_array) {
    $prevportmap{"$vnode:$vport"} = $pport;
}
$sth->finish;

# Compare portmaps
if (keys(%prevportmap) == 0) {
    $portswap = 0;
} elsif (! &keys_equal(\%portmap,\%prevportmap)){
    print STDERR "WARNING: Portmap in database is incompatible with current portmap.\n";
    print STDERR "   Perhaps a previous version of the experiment did not get cleared?\n";
    $portswap = 0;
} else {
    $portswap = 1;
}

# swapok(pnode,portA,nodeportB)
# This returns 1 if it is ok to swap portA and portB on node pnode
# and 0 otherwise.
sub swapok {
    my ($pnode,$portA,$portB) = @_;
    # For the Utah testbed we just check to make sure they have
    # the same bandwidth.  In other testbeds we'd also want to make
    # sure they went to the same destination.
    my $sth = $dbh->prepare("SELECT interface_type FROM interfaces" .
			     " where node_id = \"$pnode\"" .
			     " and iface = \"$portA\"");
    $sth->execute;
    my ($Atype) = $sth->fetchrow_array;
    $sth->finish;
    $sth = $dbh->prepare("SELECT interface_type FROM interfaces" .
			     " where node_id = \"$pnode\"" .
			     " and iface = \"$portB\"");
    $sth->execute;
    my ($Btype) = $sth->fetchrow_array;
    $sth->finish;

    return ($interfacespeed{$Atype} == $interfacespeed{$Btype});
};

if ($portswap) {
    foreach $vnodeport (keys(%portmap)) {
	($vnode,$vport) = split(":",$vnodeport);
	$pnode = $v2pmap{$vnode};
	$pport = $portmap{$vnodeport};
	$prevpport = $prevportmap{$vnodeport};
	printdb "Looking at $pnode - $pport vs. $prevpport\n";
	if ($pport ne $prevpport) {
	    $okswap = 1;
	    printdb "  Want to swap.\n";
	    # We want to swap.
	    if (&swapok($pnode,$pport,$prevpport)) {
		printdb "  Swap ok.\n";
		foreach $vlan (@{$nodevlans{$pnode}}) {
		    $members = @{$vlans{$vlan}}[1];
		    grep {
			if (/^$pnode:$pport$/) {
			    $_ = "$pnode:$prevpport";
			} elsif (/^$pnode:$prevpport$/) {
			    $_ = "$pnode:$pport";
			}
		    } @$members;
		    printdb "  $vlan: " . join(" ",@$members) . "\n";
		}
	    } else {
		print "Warning: Could not swap $pport and $prevpport.\n";
		print "  Port mapping is not consistent with last swap.\n";
		$okswap = 0;
	    }
	    if ($okswap) {
		$portmap{$vnodeport} = $prevpport;
	    }
	}
    }
}

######################################################################
# Step 4 - Upload to DB
#
# Nothing fancy.
######################################################################
printdb "Uploading to DB\n";
foreach $vlan (keys(%vlans)) {
    ($lan,$members) = @{$vlans{$vlan}};
    $dbh->do("insert into vlans (id,pid,eid,virtual,members) values" .
	     " (0,\"$pid\",\"$eid\",\"$lan\",\"" .
	     join(" ",@$members) . "\")") || do {
854
855
		 print STDERR "$0: *** Could not update vlans table." .
		     "  Giving up.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
856
857
858
859
860
861
862
		 exit(1);
	     };
}
foreach $delay (keys(%delays)) {
    ($pnode,$int0,$int1,$vname,$delay,$bandwidth,$lossrate) = 
	@{$delays{$delay}};
    $dbh->do("insert into delays" .
863
	     " (pid,eid,node_id,iface0,iface1" .
Leigh B. Stoller's avatar
Leigh B. Stoller committed
864
865
	     ",delay,bandwidth,lossrate,vname)" .
	     " values (\"$pid\",\"$eid\",\"$pnode\",\"$int0\",\"$int1\"".
866
	     ",$delay,$bandwidth,$lossrate,\"$vname\")") || do {
867
868
		 print STDERR "$0: *** Could not update delays table." .
		     "  Giving up.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
869
870
871
872
873
874
875
876
877
878
879
880
881
		 exit(1);
	     };
}
$dbh->do("delete from portmap where pid=\"$pid\" and eid=\"$eid\"") || do {
    print STDERR "Could not clear portmap table.  Giving up.\n";
    exit(1);
};
foreach $vnodeport (keys(%portmap)) {
    ($vnode,$vport) = split(":",$vnodeport);
    $pport = $portmap{$vnodeport};
    $dbh->do("insert into portmap (pid,eid,vnode,vport,pport)" .
	     " values (\"$pid\",\"$eid\",\"$vnode\"" .
	     ",\"$vport\",\"$pport\")") || do {
882
883
		 print STDERR "$0: *** Could not update portmap table." .
		     "  Giving up.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
884
885
886
887
888
889
890
891
892
		 exit(1);
	     };
    # Shark Hack
    if ($nodes{$vnode} eq "shark-shelf") {
	$shelf = $v2pmap{$vnode};
	$i = 1;
	foreach $shark (@{$sharkshelves{$vnode}}) {
	    $dbh->do("update interfaces set IPalias=\"$ips{$shark}\" where" .
		     " node_id = \"$shelf-$i\"") || do {
893
894
			 print STDERR "$0: *** Could not update IPalias" .
			     " for $shelf-$i.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
895
896
897
898
899
900
901
			 exit(1);
		     };
	    $i++;
	}
    } else {
	$dbh->do("update interfaces set IP=\"$ips{$vnodeport}\" where" .
		 " node_id = \"$v2pmap{$vnode}\" and iface = \"$pport\"") || do {
902
903
		     print STDERR "$0: *** Could not update interfaces" .
			 " table.  Giving up.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
904
905
906
907
908
		     exit(1);
		 };
    }
    # End Shark Hack
}
909
910
# Load delay_osids and default osids for types
$sth = $dbh->prepare("SELECT type,delay_osid,osid from node_types");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
911
$sth->execute;
912
while (($type,$delayosid,$defosid) = $sth->fetchrow_array) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
913
    $delayosids{$type} = $delayosid;
914
    $defosids{$type} = $defosid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
}
$sth->finish;
@nodepairs = ();
foreach $pnode (keys(%p2vmap)) {
    if (defined($sharkshelves{$p2vmap{$pnode}})) {
	$i = 1;
	foreach $sharknodeport (@{$sharkshelves{$p2vmap{$pnode}}}) {
	    $shark = (split(":",$sharknodeport))[0];
	    push(@nodepairs,["$pnode-$i",$shark]);
	    $i++;
	}
    } else {
	push(@nodepairs,[$pnode,$p2vmap{$pnode}]);
    }
}
foreach $pair (@nodepairs) {
    ($pnode,$vnode) = @$pair;
    $sth = $dbh->prepare("SELECT osid,cmd_line,rpms,deltas,startupcmd," .
933
934
935
			 "tarfiles,failureaction from virt_nodes" .
			 " where pid=\"$pid\"" .
			 " and eid=\"$eid\" and vname=\"$vnode\"");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
936
937
938
    $sth->execute;
    # The if statement will cause us to skip nodes that belong to
    # the experiment but aren't virtual.  I.e. delay nodes.
939
940
941
942
943
944
    # Figure out type of pnode
    $sth2 = $dbh->prepare("SELECT type from nodes" .
			  " where node_id=\"$pnode\"");
    $sth2->execute;
    my ($type) = $sth2->fetchrow_array;
    $sth2->finish;
945
    if (($osid,$cmdline,$rpms,$deltas,$startupcmd,$tarfiles,$failureaction) = 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
946
	$sth->fetchrow_array) {
947
948
949
	if (!defined($osid) || $osid eq "") {
	    $osid = $defosids{$type};
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
950
951
952
	if (! $dbh->do("UPDATE nodes set def_boot_osid=\"$osid\"," .
		       " def_boot_cmd_line=\"$cmdline\"," .
		       " startstatus=\"none\"," .
953
		       " bootstatus=\"unknown\"," .
Leigh B. Stoller's avatar
Leigh B. Stoller committed
954
955
956
957
		       " ready=0," .
		       " rpms=\"$rpms\"," .
		       " deltas=\"$deltas\"," .
		       " tarballs=\"$tarfiles\"," .
958
959
		       " startupcmd=\"$startupcmd\"," .
		       " failureaction=\"$failureaction\"" .
Leigh B. Stoller's avatar
Leigh B. Stoller committed
960
		       " where node_id=\"$pnode\"")) {
961
	    print STDERR "$0: *** Could not update nodes table." .
Leigh B. Stoller's avatar
Leigh B. Stoller committed
962
963
964
965
966
967
968
969
		"  Giving up.\n";
	    exit(1);
	} 
    } elsif (! defined($lannodes{$p2vmap{$pnode}})) {
	# Delay node
	if (! $dbh->do("UPDATE nodes set def_boot_osid=\"" .
		       $delayosids{$type} . "\"," .
		       " startstatus=\"none\"," .
970
		       " bootstatus=\"unknown\"," .
Leigh B. Stoller's avatar
Leigh B. Stoller committed
971
972
		       " ready=0" .
		       " where node_id=\"$pnode\"")) {
973
	    print STDERR "$0: *** Could not update nodes table." .
Leigh B. Stoller's avatar
Leigh B. Stoller committed
974
975
976
977
978
979
980
981
		"  Giving up.\n";
	    exit(1);
	}
    }
    $sth->finish;
}

foreach $pnode (keys(%p2vmap)) {
982
983
984
985
986
987
988
989
    $vnode = $p2vmap{$pnode};
    $i = 1;
    if (defined($nodes{$vnode}) && 
	($nodes{$vnode} eq "shark-shelf")) {
	foreach $shark (@{$sharkshelves{$vnode}}) {
	    $vname = (split(":",$shark))[0];
	    $dbh->do("update reserved set vname=\"$vname\" where" .
		     " node_id = \"$pnode-$i\"") || do {
990
991
			 print STDERR "$0: *** Could not update reserved" .
			     " table.  Giving up.\n";
992
993
994
995
996
997
998
			 exit(1);
		     };
	    $i++;
	}
    } else {
	$dbh->do("update reserved set vname=\"$vnode\"" .
		 " where node_id = \"$pnode\"") || do {
999
1000
		     print STDERR "$0: *** Could not update reserved" .
			 " table. Giving up.\n";
1001
1002
1003
		     exit(1);
		 };
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1004
1005
}

1006
1007
foreach $vnodeport (keys(%portbw)) {
    ($vnode,$vport) = split(":",$vnodeport);
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
    # SHARK HACK
    if (defined($v2pmap{$vnode})) {
	$pnode = $v2pmap{$vnode};
	$pport = $portmap{$vnodeport};
	$dbh->do("update interfaces set current_speed=\"$portbw{$vnodeport}\"" .
		 " where node_id=\"$pnode\" and iface=\"$pport\"") || do {
		     print STDERR "$0: *** Could not update current_speed on" .
			 " interfaces.  Giving up.\n";
		     exit(1);
		 }
    }
1019
}