tbswap.in 37.2 KB
Newer Older
Chad Barb's avatar
 
Chad Barb committed
1
2
3
4
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2005 University of Utah and the Flux Group.
Chad Barb's avatar
 
Chad Barb committed
6
7
8
9
# All rights reserved.
#
use English;

Chad Barb's avatar
   
Chad Barb committed
10
11
12
# Returns 0 on success.
# Returns 1 on non-assign_wrapper failure.
# Returns (1 | assign_wrapper's errorcode) on assign_wrapper failure.
13
14
# Returns |0x40 if update caused a swapout. Icky.
# Returns -1 on uncontrolled error (die called).
Chad Barb's avatar
   
Chad Barb committed
15

Chad Barb's avatar
   
Chad Barb committed
16
17
18
19
20
# XXX: handle error cases for update? (backup the db?)
# XXX: Shouldn't do idempotent stuff twice for update.
# XXX: repush/calc routing for update??? (tbprerun)
# XXX: previz for update???              (tbprerun)
# XXX: make snmpit faster for update.
Chad Barb's avatar
Chad Barb committed
21
22
23
#
# XXX: for update, expt is swapped out on os_setup fail.
#      (we only recover if assign fails)
Chad Barb's avatar
 
Chad Barb committed
24
25
26

sub usage()
{
27
    print STDERR "Usage: $0 -force { in | out | update [-reboot] [-eventsys_restart] } pid eid\n";
Chad Barb's avatar
 
Chad Barb committed
28
29
30
31
32
33
34
35
36
    exit(-1);
}

#
# Configure variables
#
my $TBROOT         = "@prefix@";
my $TESTMODE       = @TESTMODE@;
my $DISABLE_EVENTS = "@DISABLE_EVENT_SCHED@";
37
my $piper          = "$TBROOT/sbin/locpiper";
Chad Barb's avatar
 
Chad Barb committed
38
39
40
41
42
43
44
45
46
47
48

# Untaint the path
$ENV{'PATH'} = "/usr/bin:$TBROOT/libexec:$TBROOT/libexec/ns2ir" . 
    ":$TBROOT/sbin:$TBROOT/bin";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
49
use libadminctrl;
50
#require exitonwarn; # exitonwarn isn't really a module, so just require it
Chad Barb's avatar
 
Chad Barb committed
51
52
53
54

#
# Actual swap-in and swap-out functions, defined below.
#
Chad Barb's avatar
   
Chad Barb committed
55
56
57
sub doSwapout($);
sub doSwapin($);

58
59
60
61
#
# Firewall stuff
# XXX maybe should be elsewhere
#
62
63
64
65
66
sub FWSETUP()     { return 1; }
sub FWADDNODES()  { return 2; }
sub FWDELNODES()  { return 3; }
sub FWTEARDOWN()  { return 4; }
sub doFW($$$$);
67
68

# XXX fixme: should not be hardwired!
69
my $cnetstack = "-S Control";
70
71
72
my $cnetvlanname = "Control";


Chad Barb's avatar
   
Chad Barb committed
73
74
75
76
sub REAL()    { return 4; }
sub CLEANUP() { return 3; }
sub RETRY()   { return 2; }
sub UPDATE()  { return 1; }
Chad Barb's avatar
Chad Barb committed
77
sub UPDATE_RECOVER()  { return 0; }
Chad Barb's avatar
 
Chad Barb committed
78

79
80
81
#
# Grab global enable of linkdelays.
#
Kirk Webb's avatar
   
Kirk Webb committed
82
my $enablelinkdelays = TBGetSiteVar("general/linux_endnodeshaping");
83

Chad Barb's avatar
 
Chad Barb committed
84
85
86
87
88
89
#
# Turn off line buffering on output
#

$| = 1;

90
91
my $updateReboot   = 0;
my $updateReconfig = 1;
92
93
my $update_Eventsys_restart = 0;
my $elabinelab     = 0;
Chad Barb's avatar
 
Chad Barb committed
94
95
my $force  = 0;
my $errors = 0;
96
my $updatehosed = 0;
Chad Barb's avatar
 
Chad Barb committed
97
my $state;
98
my $canceled;
Chad Barb's avatar
 
Chad Barb committed
99
100
my $os_setup_pid;
my $cleanvlans;
101
my $nextState;
Chad Barb's avatar
 
Chad Barb committed
102
103

#
Chad Barb's avatar
   
Chad Barb committed
104
# First argument is either "in", "out", or "update";
Chad Barb's avatar
 
Chad Barb committed
105
106
107
108
109
# this value goes into $swapop.
#

my $swapop = shift;	

Chad Barb's avatar
   
Chad Barb committed
110
111
112
113
if (!$swapop || 
    (($swapop ne "in") && 
     ($swapop ne "out") &&
     ($swapop ne "update"))) {
Chad Barb's avatar
 
Chad Barb committed
114
115
116
117
118
119
120
121
122
123
124
    usage();
}

#
# Get other arguments.
#

while ($#ARGV > 1) {
    $arg = shift;
    if ($arg eq "-force") {
	$force = 1;
Chad Barb's avatar
Chad Barb committed
125
126
    } elsif ($arg eq "-reboot") {
	$updateReboot = 1;
127
	$updateReconfig = 0;
128
129
130
    } elsif ($arg eq "-noreconfig") {
	$updateReboot   = 0;
	$updateReconfig = 0;
131
132
    } elsif ($arg eq "-eventsys_restart" && $swapop eq "update") {
	$update_Eventsys_restart = 1;
Chad Barb's avatar
 
Chad Barb committed
133
134
135
136
137
138
139
140
141
    } else {
	usage();
    }
}
if ($#ARGV < 1) {
    usage();
}
my ($pid,$eid) = @ARGV;

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#
# Untaint the arguments.
#
if ($pid =~ /^([-\@\w.]+)$/) {
    $pid = $1;
}
else {
    die("Tainted argument $pid!\n");
}
if ($eid =~ /^([-\@\w.]+)$/) {
    $eid = $1;
}
else {
    die("Tainted argument $eid!\n");
}

Chad Barb's avatar
 
Chad Barb committed
158
159
160
161
162
TBDebugTimeStampsOn();

#
# Print starting message.
#
163
164
my $exptidx;
TBExptIDX($pid, $eid, \$exptidx);
Chad Barb's avatar
 
Chad Barb committed
165

166
167
print "Beginning swap-$swapop for $pid/$eid ($exptidx). " .
    TBTimeStampWithDate() . "\n";
Chad Barb's avatar
 
Chad Barb committed
168
169
170
171
172
173
TBDebugTimeStamp("tbswap $swapop started");

#
# Get experiment state; verify that experiment exists.
#
if (! ($state = ExpState($pid, $eid))) {
174
175
    die("*** $0:\n".
	"    No such experiment $pid/$eid\n");
Chad Barb's avatar
 
Chad Barb committed
176
}
177
178
179
180
181
182
# Sanity check the current state. 
if (!$force) {
    if ($swapop eq "in") {
	die("*** $0:\n".
	    "    Experiment should be ACTIVATING. Currently $state.\n")
	    if ($state ne EXPTSTATE_ACTIVATING);
Chad Barb's avatar
 
Chad Barb committed
183
    }
184
185
186
187
    elsif ($swapop eq "out") {
	die("*** $0:\n".
	    "    Experiment should be SWAPPING. Currently $state.\n")
	    if ($state ne EXPTSTATE_SWAPPING);
Chad Barb's avatar
 
Chad Barb committed
188
    }
189
190
191
192
    elsif ($swapop eq "update") {
	die("*** $0:\n".
	    "    Experiment should be MODIFY_RESWAP. Currently $state.\n")
	    if ($state ne EXPTSTATE_MODIFY_RESWAP);
Chad Barb's avatar
 
Chad Barb committed
193
194
    }
}
195
196
197
198
199
# Get elabinelab status. See below.
if (! TBExptIsElabInElab($pid, $eid, \$elabinelab)) {
    die("*** $0:\n".
	"    Could not get elabinelab status for experiment $pid/$eid\n");
}
Chad Barb's avatar
 
Chad Barb committed
200

201
202
203
204
205
#
# See if the experiment is firewalled
#
my $firewalled = TBExptFirewall($pid, $eid);

Chad Barb's avatar
 
Chad Barb committed
206
207
208
#
# Do actual swapping
#
209
210
211
212
213
if ($swapop eq "out") {
    #
    # Swap out
    #
    $errors = doSwapout(REAL);
Chad Barb's avatar
   
Chad Barb committed
214
}
215
elsif ($swapop eq "update") {
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    #
    # Before swapout, do cursory admission control to see if the
    # modified experiment will be swappable. assign_wrapper does a
    # more stringent check using assign.
    #
    print STDERR "Checking with Admission Control ...\n";
    if (! TBAdmissionControlCheck($UID, $pid, $eid, undef)) {
	print STDERR "Admission control failure!\n";
	print "Failingly finished swap-$swapop for $pid/$eid. " .
	    TBTimeStamp() . "\n";
	TBDebugTimeStamp("tbswap $swapop finished (failed)");
	exit(1);
    }
    
230
231
232
233
234
235
236
    #
    # Update.
    #
    # Phase One -- swap experiment partially out.
    #
    print STDERR "Backing up physical state...\n";
    TBExptBackupPhysicalState($pid,$eid);
Chad Barb's avatar
 
Chad Barb committed
237

238
    $errors = doSwapout(UPDATE);
Chad Barb's avatar
Chad Barb committed
239

240
    if ($errors) {
Chad Barb's avatar
   
Chad Barb committed
241
	#
242
243
244
245
246
	# Clean up the mess, leaving the experiment in the SWAPPED state,
	# 
	print STDERR "Cleaning up after errors.\n";
	doSwapout(CLEANUP);
	$updatehosed = 1;
Chad Barb's avatar
   
Chad Barb committed
247
    }
248
    else {
Chad Barb's avatar
   
Chad Barb committed
249
	#
250
	# Phase Two -- swap experiment back in.
Chad Barb's avatar
   
Chad Barb committed
251
	#
Chad Barb's avatar
Chad Barb committed
252
253
	$errors = doSwapin(UPDATE);

254
	if ($errors) {
Chad Barb's avatar
Chad Barb committed
255
256
257
	    #
	    # There were errors; see if we can recover.
	    #
258
	    my $CanRecover = 1;
Chad Barb's avatar
Chad Barb committed
259
260
261

	    if ($errors != 7) {
		print STDERR "Update failure occurred _after_ assign phase; ";
262
		$CanRecover = 0;
Chad Barb's avatar
Chad Barb committed
263
264
	    }

265
266
267
268
269
270
271
272
	    if ($CanRecover) {
		print STDERR "Recovering virtual and physical state.\n";

		if (TBExptRemoveVirtualState($pid, $eid) ||
		    TBExptRestoreVirtualState($pid, $eid) ||
		    TBExptRestorePhysicalState($pid,$eid)) {
		    print STDERR "Could not restore backed-up state; ";
		    $CanRecover = 0;
Chad Barb's avatar
Chad Barb committed
273
		}
274
275
276
277
278
279
280
		else {
		    print STDERR "Doing a recovery swap-in of old state.\n";

		    if (doSwapin(UPDATE_RECOVER)) {
			print STDERR "Could not swap in old physical state; ";
			$CanRecover = 0;
		    }
Chad Barb's avatar
Chad Barb committed
281
282
		}
	    }
283
284
285
286
287
288
289

	    #
	    # Some part of the recovery failed; must swap it out. swapexp
	    # (caller) will then have to do more clean up, hence the special
	    # exit status indicated by $updatehosed.
	    # 
	    if (! $CanRecover) {
290
		print STDERR "Recovery aborted! Swapping experiment out.\n";
Chad Barb's avatar
Chad Barb committed
291
		doSwapout(CLEANUP);
292
293
294
295
		$updatehosed = 1;
	    }
	    else {
		print STDERR "Update recovery successful.\n";
Chad Barb's avatar
Chad Barb committed
296
	    }
Chad Barb's avatar
   
Chad Barb committed
297
	}
Chad Barb's avatar
Chad Barb committed
298
299
    }
}
300
301
302
303
304
elsif ($swapop eq "in") {
    #
    # Swap in
    #
    my $retries = 2;
305
306
307
308
309
310
311
312
313
314
315
316
317

    #
    # Before real swapin, do cursory admission control. assign_wrapper does
    # a more stringent check using assign.
    #
    print STDERR "Checking with Admission Control ...\n";
    if (! TBAdmissionControlCheck($UID, $pid, $eid, undef)) {
	print STDERR "Admission control failure!\n";
	print "Failingly finished swap-$swapop for $pid/$eid. " .
	    TBTimeStamp() . "\n";
	TBDebugTimeStamp("tbswap $swapop finished (failed)");
	exit(1);
    }
318
319
    
    $errors = doSwapin(REAL);
Chad Barb's avatar
Chad Barb committed
320

321
322
323
324
325
326
327
328
329
330
    #
    # Attempt a retry if: 
    #   a) there were errors, 
    #   b) doswapin() indicated (via return code 3) a retry is appropriate,
    #   c) we haven't tried too many times already.
    #   d) The cancelflag has not been set.
    #   e) $TESTMODE == 0.
    #
    while ($errors == 3 && $retries && !$canceled && !$TESTMODE) {
	$retries--;
Chad Barb's avatar
 
Chad Barb committed
331

332
333
	print STDERR "Cleaning up after errors; will try again.\n";
	doSwapout(RETRY);
Chad Barb's avatar
 
Chad Barb committed
334

335
336
	print STDERR "Trying again...\n";
	$errors = doSwapin(RETRY);
Chad Barb's avatar
 
Chad Barb committed
337
    }
338
339
    if ($errors || $canceled) {
	print STDERR "Cleaning up after " .
340
	    ($canceled ? "cancelation" : "errors") . ".\n";
341
	doSwapout(CLEANUP);
Chad Barb's avatar
 
Chad Barb committed
342
343
344
345
346
347
348
    }
}

#
# Write appropriate message and exit.
#
if ($errors) {
349
    print "Failingly finished swap-$swapop for $pid/$eid. ".TBTimeStamp()."\n";
Chad Barb's avatar
 
Chad Barb committed
350
    TBDebugTimeStamp("tbswap $swapop finished (failed)");
Chad Barb's avatar
Chad Barb committed
351

352
353
    # Pass out magic value to indicate that update failed!
    exit(1 | ($updatehosed ? 0x40 : 0));
Chad Barb's avatar
 
Chad Barb committed
354
}
355
print "Successfully finished swap-$swapop for $pid/$eid. " .TBTimeStamp()."\n";
356
357
TBDebugTimeStamp("tbswap $swapop finished (succeeded)");
exit(0);
Chad Barb's avatar
 
Chad Barb committed
358
359
360
361

#################################

##
Chad Barb's avatar
Chad Barb committed
362
#
Chad Barb's avatar
 
Chad Barb committed
363
364
# doSwapout - Swaps experiment out.
#
Chad Barb's avatar
Chad Barb committed
365
#             If in REAL or CLEANUP,
Chad Barb's avatar
 
Chad Barb committed
366
367
368
#             this function will free all nodes for the 
#             experiment.
#
Chad Barb's avatar
Chad Barb committed
369
#             If in RETRY or UDPATE,
Chad Barb's avatar
 
Chad Barb committed
370
371
372
373
374
375
#             only nodes not in RES_READY will be freed.
#
#             Returns 0 on success, >0 on failure.
#
##

Chad Barb's avatar
   
Chad Barb committed
376
377
sub doSwapout($) {
    my $type = shift; # REAL==4, CLEANUP==3, RETRY==2, UPDATE==1.
Chad Barb's avatar
 
Chad Barb committed
378
379
380
381
382
383
384
385
386
387
388
    my $swapout_errors = 0;

    #
    # wait for os_setup;
    # this only applies if called after a failed doswapin.
    #
    if ($os_setup_pid) {
	print "Waiting for os_setup to finish\n";
	waitpid($os_setup_pid, 0);
	undef $os_setup_pid;
    }
Chad Barb's avatar
Chad Barb committed
389

Chad Barb's avatar
 
Chad Barb committed
390
    if (! $TESTMODE) { 
391
392
393
	if (! ($DISABLE_EVENTS || $elabinelab)) {
	    if ($type >= RETRY ||
		($update_Eventsys_restart && $type == UPDATE) ) {
Chad Barb's avatar
   
Chad Barb committed
394
395
396
397
398
		print "Stopping the event system\n";
		if (system("eventsys_control stop $pid $eid")) {
		    print STDERR "*** Failed to stop the event system.\n";
		    $swapout_errors = 1;
		}
399
400
401
402
403
404
405
406
407
408
409

		#
		# Stop the location piper.
		#
		if (-x $piper) {
		    print "Stopping the location piper\n";
		    if (system("$piper -k $pid $eid")) {
			print STDERR "*** Failed to stop location piper.\n";
			$swapout_errors = 1;
		    }
		}
Chad Barb's avatar
 
Chad Barb committed
410
411
	    }
	}
412
413
414
415
416
417
	
	#
	# Do teardown of inner elab. We must do this before we teardown the
	# vlans since the inner control network is a vlan, and we want that
	# active so inner boss can reboot the inner nodes (avoid power cycle).
	#
418
	if ($elabinelab && $type >= CLEANUP) {
419
420
421
422
423
424
425
	    print "Tearing down elabinelab. This could take a while.\n";
	    if (system("elabinelab -k $pid $eid")) {
		print STDERR "*** Failed to teardown elabinelab!\n";
		$swapout_errors = 1;
	    }
	}

Chad Barb's avatar
 
Chad Barb committed
426
	#
Chad Barb's avatar
Chad Barb committed
427
	# Clean up any VLANs in experiment.
Chad Barb's avatar
 
Chad Barb committed
428
	#
429
430
431
432
433
434
435
436
437
438
439
440
441
	# When modifying an elabinelab experiment, leave the vlans intact
	# so that the inner networks are not suddenly disconnected!
	#
	if (! ($elabinelab && $type == UPDATE)) {
	    TBDebugTimeStamp("snmpit started");
	    print STDERR "Removing VLANs.\n";
	    if (system("snmpit -r $pid $eid")) {
		print STDERR "*** Failed to reset VLANs\n";
		$swapout_errors = 1;
	    } else {
		$cleanvlans = 0;
	    }
	    TBDebugTimeStamp("snmpit finished");
Chad Barb's avatar
 
Chad Barb committed
442
	}
443
    }
Chad Barb's avatar
Chad Barb committed
444

445
446
447
    if ($type >= CLEANUP) {
	#
	# We're not attempting a retry;
Chad Barb's avatar
 
Chad Barb committed
448
	#
449
	# Stop all of the vnodes.
Chad Barb's avatar
 
Chad Barb committed
450
	#
451
	if (! $TESTMODE) { 	
Chad Barb's avatar
   
Chad Barb committed
452
453
454
455
456
457
458
	    print "Tearing down virtual nodes.\n";
	    TBDebugTimeStamp("vnode_setup -k started");
	    if (system("vnode_setup -d -k $pid $eid")) {
		print STDERR "*** Failed to tear down vnodes.\n";
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("vnode_setup finished");
Chad Barb's avatar
 
Chad Barb committed
459
460
	}

461
	if ($firewalled) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
	    #
	    # If the panic button was pressed, put all nodes into admin
	    # mode and power them off. After the firewall is torn down,
	    # we can power them back up.
	    #
	    my @nodes     = ExpNodes($pid, $eid, 1);
	    my $ADMINOSID = TB_OSID_FREEBSD_MFS;
	    my $paniced;

	    TBExptGetPanicBit($pid, $eid, \$paniced);

	    if ($paniced) {
		print STDERR "Powering down nodes.\n";
		TBDebugTimeStamp("Powering down nodes");

		system("power off @nodes");
		if ($?) {
		    #
		    # If an error, cannot continue. Must leave firewall in
		    # place. 
		    #
		    print STDERR "*** Failed to power nodes off! Stopping.\n";
		    return 1;
		}

		#
		# XXX: node_admin should take a list of nodes, or pid,eid.
		#      Instead, call os_select directly for now. 
		#
		# Clear any one-shot boots and partition boots.
		#
		print STDERR "Changing OSID to admin MFS.\n";
		TBDebugTimeStamp("Changing OSID to admin MFS");

		# Order matters cause os_select sillyness.
		if (system("os_select -c -1 @nodes") ||
		    system("os_select -t $ADMINOSID @nodes") ||
		    system("os_select -c @nodes")) {

		    #
		    # If an error, cannot continue. Must leave firewall in
		    # place. 
		    #
		    print STDERR "*** Failed to reset OSIDs! Stopping.\n";
		    return 1;
		}

		#
		# Now we can power up the nodes again.
		#
		print STDERR "Powering up nodes.\n";
		TBDebugTimeStamp("Powering up nodes");

		system("power on @nodes");
		if ($?) {
		    #
		    # If an error, cannot continue. Must leave firewall in
		    # place. Eventually, we can continue past this, if we
		    # know what nodes failed. But for now we have to do the
		    # ISUP test to make sure nodes really got into the MFS.
		    #
		    print STDERR "*** Failed to power on nodes! Stopping.\n";
		    return 1;
		}
	    }
527
528
529
530
531

	    #
	    # Once all nodes are safely in the admin MFS, we can take
	    # down the firewall
	    #
532
	    doFW($pid, $eid, FWTEARDOWN, undef);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553

	    if ($paniced) {
		#
		# Now wait for ISUP. I do this here cause the reload daemon
		# will power cycle the nodes again if its reboot fails, and
		# that will happen if the nodes are not back into the MFS in
		# time. THe right thing to do is to store the power state in
		# the DB, and have the reload daemon turn the nodes back on.
		#
		print STDERR "Waiting for nodes to boot the MFS.\n";
		TBDebugTimeStamp("Waiting for nodes to boot the MFS");
		
		system("node_statewait -s " . TBDB_NODESTATE_ISUP . " @nodes");
		if ($?) {
		    #
		    # Okay to continue; reload daemon will probably send
		    # email later when the reload fails.
		    #
		    print STDERR "*** Some nodes failed to reboot. Continuing\n";
		}
	    }	    
554
555
	}

Chad Barb's avatar
 
Chad Barb committed
556
557
	#
	# remove all nodes from the experiment.
Chad Barb's avatar
   
Chad Barb committed
558
	# (nfree will send them to RES_FREE_DIRTY)
Chad Barb's avatar
 
Chad Barb committed
559
560
561
562
563
564
565
566
	#
	print STDERR "Freeing nodes.\n";
	TBDebugTimeStamp("nfree started");
	if (system("nfree $pid $eid")) {
	    print STDERR "*** Could not free nodes.\n";
	    $swapout_errors = 1;
	}
	TBDebugTimeStamp("nfree finished");
Chad Barb's avatar
   
Chad Barb committed
567
568
569
570
571

	#
	# Since this is an actual swapout, 
	# reset our count of swap out nag emails sent.
	#
572
	DBQueryWarn("update experiments set swap_requests='',sim_reswap_count='0' ".
573
		    "where eid='$eid' and pid='$pid'");
Chad Barb's avatar
 
Chad Barb committed
574
575
    } else {
	#
Chad Barb's avatar
   
Chad Barb committed
576
	# $type == RETRY or $type == UPDATE.
Chad Barb's avatar
 
Chad Barb committed
577
578
	# Therefore, don't deallocate nodes which have been successfully
	# incorporated into the experiment (i.e., are RES_READY).
Chad Barb's avatar
   
Chad Barb committed
579
	# (nfree will send deallocated nodes to RES_FREE_DIRTY)
Chad Barb's avatar
 
Chad Barb committed
580
	#
581
582
583
584
585
586
587
588
	my @failedpnodes = ();
	my @failedvnodes = ();
	
	my $db_result =
	    DBQueryFatal("select rv.node_id,n.allocstate,nt.isvirtnode ".
                         "  from reserved as rv ".
			 "left join nodes as n on n.node_id = rv.node_id ".
			 "left join node_types as nt on nt.type=n.type ".
Chad Barb's avatar
 
Chad Barb committed
589
590
			 "where rv.pid='$pid' and rv.eid='$eid'");

591
	while (my ($node,$allocstate,$isvirt) = $db_result->fetchrow_array) {
Chad Barb's avatar
 
Chad Barb committed
592
	    if ($allocstate ne TBDB_ALLOCSTATE_RES_READY()) {
593
594
595
596
597
598
		if ($isvirt) {
		    push(@failedvnodes, $node);
		}
		else {
		    push(@failedpnodes, $node);
		}
Chad Barb's avatar
 
Chad Barb committed
599
600
601
	    }
	}

602
603
604
605
606
607
608
609
610
611
612
613
	#
	# Tear down failed vnodes. Perhaps not needed?
	# 
	if (!$TESTMODE && @failedvnodes > 0) {
	    print "Tearing down failed virtual nodes.\n";
	    TBDebugTimeStamp("vnode_setup -k started");
	    if (system("vnode_setup -d -k $pid $eid @failedvnodes")) {
		print STDERR "*** Failed to tear down vnodes.\n";
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("vnode_setup -k finished");
	}
Chad Barb's avatar
   
Chad Barb committed
614

615
616
617
618
	#
	# Release all failed nodes.
	# 
	if (@failedpnodes > 0 || @failedvnodes > 0) {
619
620
	    print STDERR "Freeing failed nodes.\n";
	    
Chad Barb's avatar
 
Chad Barb committed
621
	    TBDebugTimeStamp("nfree started");
Chad Barb's avatar
   
Chad Barb committed
622
623
624
625
626
	    #
	    # Specify -x switch so when a physical node gets freed,
	    # any virtual nodes (owned by this experiment)
	    # sitting on top of it are freed as well.
	    #
627
628
	    if (system("nfree -x $pid $eid " .
		       join(" ", (@failedpnodes, @failedvnodes)))) {
Chad Barb's avatar
 
Chad Barb committed
629
630
631
632
633
634
635
		print STDERR "*** Could not free nodes.\n";
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("nfree finished");
	}
    }

636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
    if (! $TESTMODE) {
	#
	# If the experiment has no Plab dslice nodes left, but still has
	# a Plab slice, destroy the slice
	#

	# Does the slice exist?
	$db_result =
	    DBQueryFatal("select slicename from plab_slices ".
			 "where pid='$pid' and eid='$eid'");

	if ($db_result->numrows) {
	    # Are there any dslice nodes left?
	    $db_result =
		DBQueryFatal("select n.node_id from nodes as n ".
			     "left join node_types as nt on n.type = nt.type ".
			     "left join reserved as r ".
			     " on r.node_id = n.node_id ".
			     "where r.pid='$pid' and r.eid='$eid' ".
			     " and nt.isplabdslice = 1");

	    if (!$db_result->numrows) {
		print "Destroying Planetlab slice.\n";
		TBDebugTimeStamp("plabslice destroy started");
660
		if (system("plabslice destroy $pid $eid")) {
661
662
663
664
665
666
667
668
		    print STDERR "*** Failed to destroy Plab dslice\n";
		    $swapout_errors = 1;
		}
		TBDebugTimeStamp("plabslice destroy finished");
	    }
	}
    }

Chad Barb's avatar
 
Chad Barb committed
669
670
671
672
673
674
675
676
    if (! $TESTMODE) {
	#
	# All of these errors are non-fatal on swapout. We find out about them
	# via email sent from the individual scripts.
	#

	#
	# Only reset mountpoints if this is an actual swapout, and
Chad Barb's avatar
   
Chad Barb committed
677
	# not a failed swapin(cleanup), update, or retry.
Chad Barb's avatar
 
Chad Barb committed
678
	#
Chad Barb's avatar
   
Chad Barb committed
679
	if ($type == REAL) {
Chad Barb's avatar
 
Chad Barb committed
680
681
682
683
684
685
686
	    print "Resetting mountpoints.\n";
	    TBDebugTimeStamp("exports started");
	    if (system("exports_setup")) {
		print STDERR "*** Failed to reset mountpoints.\n";
	    }
	    TBDebugTimeStamp("exports finished");
	}
Chad Barb's avatar
Chad Barb committed
687

Chad Barb's avatar
   
Chad Barb committed
688
689
690
691
	#
	# Resetting named maps and email lists is fast and idempotent,
	# so whatever.
	#
Chad Barb's avatar
 
Chad Barb committed
692
693
694
695
696
697
	print "Resetting named maps.\n";
	TBDebugTimeStamp("named started");
	if (system("named_setup")) {
	    print "*** WARNING: Failed to reset named map.\n";
	}
	TBDebugTimeStamp("named finished");
Chad Barb's avatar
Chad Barb committed
698

Chad Barb's avatar
 
Chad Barb committed
699
700
	print "Resetting email lists.\n";
	TBDebugTimeStamp("genelists started");
701
	if (system("genelists -t")) {
Chad Barb's avatar
 
Chad Barb committed
702
703
704
705
706
	    print "*** WARNING: Failed to reset email lists.\n";
	}
	TBDebugTimeStamp("genelists finished");
    }

Chad Barb's avatar
   
Chad Barb committed
707
    #
708
709
710
    # Wipe the DB clean except during UPDATE or RETRY. In those
    #    cases, assign_wrapper will reset the DB after reading
    #    the info.
Chad Barb's avatar
   
Chad Barb committed
711
    #
712
713
714
715
    if ( $type >= CLEANUP ) {
	print STDERR "Resetting DB.\n";
	TBExptRemovePhysicalState( $pid, $eid );
    }
Chad Barb's avatar
 
Chad Barb committed
716
717
718
719
720

    return $swapout_errors;
}

##
Chad Barb's avatar
Chad Barb committed
721
#
Chad Barb's avatar
 
Chad Barb committed
722
723
# doSwapin - Swaps experiment in.
#
Chad Barb's avatar
Chad Barb committed
724
725
726
727
728
#            Returns:
#              0 - successful swapin
#              1 - failed swapin; cleanup required.
#              3 - failed swapin; cleanup required; can retry.
#              7 - failed swapin; assign failed; no cleanup.
Chad Barb's avatar
 
Chad Barb committed
729
730
##

Chad Barb's avatar
   
Chad Barb committed
731
sub doSwapin($) {
732
733
734
    my $type = shift; # REAL==4, RETRY==2, UPDATE==1, UPDATE_RECOVER=0.
    # Just the physnodes ...
    my @deleted_pnodes = ();
Chad Barb's avatar
   
Chad Barb committed
735

Chad Barb's avatar
 
Chad Barb committed
736
737
738
739
740
    #
    # assign_wrapper does all the virtual to physical mapping 
    # and updating the DB state.
    #

Chad Barb's avatar
Chad Barb committed
741
    if ($type > UPDATE_RECOVER) {
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
        #
        # Hacky test to allow disabling of linkdelays if the node is going
        # to run Linux. See sitevar above.
        #
	if (! $enablelinkdelays) {
	    $db_result =
		DBQueryFatal("select distinct e.pid,e.eid,vl.vnode,vn.osname ".
			 "  from experiments as e ".
			 "left join virt_lans as vl on vl.pid=e.pid and ".
			 "     vl.eid=e.eid ".
			 "left join virt_nodes as vn on vn.pid=e.pid and ".
			 "     vn.eid=e.eid and vn.vname=vl.vnode ".
			 "left join os_info as o on o.osname=vn.osname and ".
			 "  (o.pid=vl.pid or o.pid='" . TBOPSPID() . "') ".
			 "where (vl.uselinkdelay!=0 or e.uselinkdelays!=0 or ".
			 "       e.forcelinkdelays!=0) and ".
			 "     (o.os is NULL or o.os='Linux') and ".
			 "     e.pid='$pid' and e.eid='$eid'");

	    if ($db_result->numrows) {
		print "*** Endnodeshaping is disabled on Linux Images!\n";
		print "*** You must modify your experiment to swap it in.\n";
		return 1;
	    }
	}
	
Chad Barb's avatar
Chad Barb committed
768
769
770
771
	print "Mapping to physical reality ...\n";
	TBDebugTimeStamp("assign_wrapper started");

	#
772
773
774
775
	# Pass the -u (update) switch into assign_wrapper, which turns on
	# update mode. When doing a retry, must also fix the current nodes
	# to avoid stuff jumping around when simply trying to replace a node
	# that did not boot.
Chad Barb's avatar
Chad Barb committed
776
777
	#
	my $exitcode;
778
779
780
	my $wrapper = "assign_wrapper -u";
	$wrapper .= " -f"
	    if ($type == RETRY);
781
	
782
	if (system("$wrapper $pid $eid")) {
783
784
785
786
	    $exitcode = $? >> 8;

	    print STDERR "*** Failed ($exitcode) to map to reality.\n";

787
788
	    # Wrapper sets this bit when recovery is possible.
	    if ($exitcode & 64) {
789
		# We can recover. 
Chad Barb's avatar
Chad Barb committed
790
		return 7;
791
792
	    }
	    else {
793
		# No recovery, no retry.
Chad Barb's avatar
Chad Barb committed
794
795
796
797
		return 1;
	    }
	}
	TBDebugTimeStamp("assign_wrapper finished");
Chad Barb's avatar
 
Chad Barb committed
798

Chad Barb's avatar
Chad Barb committed
799
800
	print "Mapped to physical reality!\n";
    }
Chad Barb's avatar
 
Chad Barb committed
801

802
    # Check cancel flag before continuing. No retry, 
803
    TBGetCancelFlag($pid, $eid, \$canceled);
804
805
806
    return 1
	if ($canceled);

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
    #
    # Look for any nodes in RES_TEARDOWN. These need to be released,
    # and if a virtnode, they need to be torn down. We cannot wait for
    # the virtnodes to go down with the physnode they are hosted on,
    # so teardown and release the virtnodes first, and then do the
    # physnodes.
    #
    # Errors are fatal; no recovery or retry.
    #
    if ($type == UPDATE) {
	my $allocstate = TBDB_ALLOCSTATE_RES_TEARDOWN();
	
	$db_result =
	    DBQueryFatal("select r.node_id,nt.isvirtnode,nt.isremotenode ".
			 "  from reserved as r ".
			 "left join nodes as n on n.node_id=r.node_id ".
			 "left join node_types as nt on nt.type=n.type ".
			 "where r.pid='$pid' and r.eid='$eid' and ".
			 "      n.allocstate='$allocstate'");

	if ($db_result->numrows) {
	    my @virtnodes = ();
	    my @physnodes = ();
	    
	    print "Tearing down and releasing unused nodes\n";

	    # First teardown/release virtnodes. 
	    while (my ($node,$isvirt,$isrem) = $db_result->fetchrow_array()) {
		if ($isvirt) {
		    push(@virtnodes, $node);
		}
		elsif (!$isrem) {
		    push(@physnodes, $node);
		}
	    }
842
843
844
	    # See below.
	    @deleted_pnodes = @physnodes;
	    
845
846
847
	    if (@virtnodes) {
		TBDebugTimeStamp("vnode_setup started");
		
848
		if (system("vnode_setup -k $pid $eid @virtnodes")) {
849
850
851
852
853
854
855
856
857
858
859
		    print "Failed to tear down unused virtnodes!\n";
		    return 1;
		}
		TBDebugTimeStamp("vnode_setup finished");
		
		if (system("nfree $pid $eid @virtnodes")) {
		    print "Failed to nfree unused virtnodes!\n";
		    return 1;
		}
	    }
	    if (@physnodes) {
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
		if ($elabinelab) {
		    print "Removing nodes from inner elab.\n";
		    if (system("elabinelab -r $pid $eid @physnodes")) {
			print STDERR "*** Failed to remove inner nodes!\n";
			return 1;
		    }
		}

		#
		# XXX Nodes should actually be cleaned up, but we are not
		# doing that in the swapout case either, unless the panic
		# button was pressed, and in that case swapmod is not
		# allowed anyway (just swapout).
		# 
		if ($firewalled && doFW($pid, $eid,
					FWDELNODES, \@deleted_pnodes)) {
		    return 1;
		}
		
879
880
881
882
883
884
885
886
		if (system("nfree $pid $eid @physnodes")) {
		    print "Failed to nfree unused physnodes!\n";
		    return 1;
		}
	    }
	}
    }

Chad Barb's avatar
 
Chad Barb committed
887
888
889
890
891
892
    # Exit here if we are testing.
    if ($TESTMODE) {
	print "Testing run - Stopping here.\n";
	return 0;
    }

893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
    #
    # Handle tarballs - we might need to fetch some from URLs if the user
    # asked for that.
    #
    print "Fetching tarballs and RPMs (if any) ...\n";
    TBDebugTimeStamp("tarfiles_setup started");

    if (system("tarfiles_setup $pid $eid")) {
	#
	# No recovery for now - what would we do?
	#
	print STDERR "*** Failed to set up tarballs.\n";
	return 1;
    }
    TBDebugTimeStamp("tarfiles_setup finished");

909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
    #
    # If there are any Plab dslice nodes in the experiment, create the
    # dslice now
    #
    if ($type > UPDATE_RECOVER) {
	# Are there any Plab nodes?
	$db_result =
	    DBQueryFatal("select n.node_id from nodes as n ".
			 "left join node_types as nt on n.type = nt.type ".
			 "left join reserved as r on r.node_id = n.node_id ".
			 "where r.pid='$pid' and r.eid='$eid' ".
			 " and nt.isplabdslice = 1");

	if ($db_result->numrows) {
	    # Does slice already exist?
	    $db_result =
		DBQueryFatal("select slicename from plab_slices ".
			     "where pid='$pid' and eid='$eid'");

	    if (! $db_result->numrows) {
		my @plabnodes = ();
		
		while (my ($node) = $db_result->fetchrow_array()) {
		    push(@plabnodes, $node);
		}
		
		print "Creating Planetlab slice.\n";
		TBDebugTimeStamp("plabslice create started");
937
		if (system("plabslice create $pid $eid")) {
938
939
940
941
942
943
944
945
		    print STDERR "*** Failed to create Plab dslice\n";
		    return 3;
		}
		TBDebugTimeStamp("plabslice alloc finished");
	    }
	}
    }

946
    # Check cancel flag before continuing. No retry, 
947
    TBGetCancelFlag($pid, $eid, \$canceled);
948
949
950
    return 1
	if ($canceled);

Chad Barb's avatar
 
Chad Barb committed
951
952
953
954
955
956
957
958
959
960
961
962
963
    #
    # These things need to get started before the nodes come up, so we'll
    # do them before the os_setup. Everything else can done in parallel with
    # os_setup. (Actually, these probably can too, since they should finish
    # long before the nodes reboot, but better safe than sorry)
    #
    print "Setting up mountpoints.\n";
    TBDebugTimeStamp("mountpoints started");
    if (system("exports_setup")) {
	print STDERR "*** Failed to setup mountpoints.\n";
	return 1;
    }
    TBDebugTimeStamp("mountpoints finished");
Chad Barb's avatar
Chad Barb committed
964

Chad Barb's avatar
 
Chad Barb committed
965
966
967
968
969
970
    TBDebugTimeStamp("named started");
    print "Setting up named maps.\n";
    if (system("named_setup")) {
	print STDERR "*** WARNING: Failed to add node names to named map.\n";
	#
	# This is a non-fatal error.
Chad Barb's avatar
Chad Barb committed
971
	#
Chad Barb's avatar
 
Chad Barb committed
972
973
    }
    TBDebugTimeStamp("named finished");
Chad Barb's avatar
Chad Barb committed
974

975
    # Check cancel flag before continuing. No retry, 
976
    TBGetCancelFlag($pid, $eid, \$canceled);
977
978
    return 1
	if ($canceled);
Chad Barb's avatar
Chad Barb committed
979

980
981
982
983
    #
    # Setup any control-net firewall.
    # This must be done before reloading and rebooting nodes.
    #
984
985
    if ($firewalled && ($type == REAL || $type == UPDATE) &&
	doFW($pid, $eid, (($type == UPDATE) ? FWADDNODES : FWSETUP), undef)) {
986
987
988
	return 1;
    }

Chad Barb's avatar
Chad Barb committed
989
990
    #
    # If user specified -reboot to update,
Chad Barb's avatar
   
Chad Barb committed
991
    # and we are successfully performing the update,
992
    # then mark all nodes in experiment so os_setup will reboot them.
Chad Barb's avatar
Chad Barb committed
993
    #
994
995
996
    if (($type == UPDATE) &&
	($updateReboot || $updateReconfig)) {
	print STDERR "Marking nodes for reboot/reconfig.\n";
Chad Barb's avatar
Chad Barb committed
997
	$db_result =
998
999
1000
	    DBQueryFatal("select r.node_id,n.allocstate from reserved as r ".
			 "left join nodes as n on n.node_id=r.node_id ".
			 "where r.pid='$pid' and r.eid='$eid'");
Chad Barb's avatar
Chad Barb committed
1001

1002
1003
1004
1005
1006
1007
1008
1009
	while (my ($node,$allocstate) = $db_result->fetchrow_array) {
	    #
	    # If the node is INIT_CLEAN, leave it alone. It will still get
	    # rebooted, but will not falsely be tagged as dirty. This is
	    # important for vnodes too, where INIT_CLEAN indicated the vnode
	    # does not even exist yet (plab nodes).
	    #
	    if ($allocstate ne TBDB_ALLOCSTATE_RES_INIT_CLEAN()) {
1010
1011
1012
1013
		TBSetNodeAllocState($node,
				    ($updateReboot ?
				     TBDB_ALLOCSTATE_RES_INIT_DIRTY() :
				     TBDB_ALLOCSTATE_RES_RECONFIG()));
1014
	    }
Chad Barb's avatar
Chad Barb committed
1015
1016
1017
	}
    }

Chad Barb's avatar
 
Chad Barb committed
1018
1019
1020
1021
1022
    #
    # Since it'll take a while for the nodes to reboot, we'll start now, and
    # wait for the os_setup to finish, down below
    #
    print "Resetting OS and rebooting.\n";
1023
    TBDebugTimeStamp("launching os_setup");
Chad Barb's avatar
 
Chad Barb committed
1024
1025
1026
1027
1028
1029
    if (!($os_setup_pid = fork())) { 
	exec("os_setup $pid $eid") or return 1;
    } elsif ($os_setup_pid == -1) {
	print STDERR "*** Fork failed.\n";
	return 1;
    }
Chad Barb's avatar
Chad Barb committed
1030

Chad Barb's avatar
 
Chad Barb committed
1031
    #
1032
1033
1034
1035
1036
    # XXX
    # Don't add any steps between here and the waitpid() call below
    # without verifying that 1) It's OK for nodes to come up before
    # the step has completed and 2) It's OK for the command to run in
    # parallel with os_setup (no DB dependencies, etc.)
Chad Barb's avatar
 
Chad Barb committed
1037
1038
1039
1040
1041
1042
1043
1044
1045
    #

    print "Setting up VLANs.\n";
    TBDebugTimeStamp("snmpit started");
    if (system("snmpit -t $pid $eid")) {
	print STDERR "*** Failed to set up VLANs.\n";
	return 1;
    }
    TBDebugTimeStamp("snmpit finished");
Chad Barb's avatar
Chad Barb committed
1046

Chad Barb's avatar
 
Chad Barb committed
1047
1048
1049
1050
    #
    # An error now means that the VLANS need to be cleaned up.
    #
    $cleanvlans = 1;
Chad Barb's avatar
Chad Barb committed
1051

Chad Barb's avatar
 
Chad Barb committed
1052
1053
    print "Setting up email lists.\n";
    TBDebugTimeStamp("genelists started");
1054
    if (system("genelists -t")) {
Chad Barb's avatar
 
Chad Barb committed
1055
1056
1057
1058
1059
1060
	print STDERR "*** WARNING: Failed to update email lists.\n";
	#
	# This is a non-fatal error.
	# 
    }
    TBDebugTimeStamp("genelists finished");
Chad Barb's avatar
Chad Barb committed
1061

Chad Barb's avatar
   
Chad Barb committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
    #
    # Don't clear port counters on UPDATE.
    # (XXX should clear new nodes' port counters.)

    if ($type >= RETRY) {
	print "Clearing port counters.\n";
	TBDebugTimeStamp("portstats started");
	if (system("portstats -z -a -q $pid $eid")) {
	    print STDERR "*** WARNING: Failed to clear port counters.\n";
	    #
	    # This is a non-fatal error.
	    # 
	}
	TBDebugTimeStamp("portstats finished");
Chad Barb's avatar
 
Chad Barb committed
1076
    }
Chad Barb's avatar
Chad Barb committed
1077

Chad Barb's avatar
 
Chad Barb committed
1078
1079
1080
1081
1082
1083
1084
1085
    #
    # OK, let's see how that os_setup did
    #
    $kid = waitpid($os_setup_pid,0);
    if ($kid == $os_setup_pid) {
	undef $os_setup_pid; # Make sure doswapout() doesn't wait for it.
	if ($CHILD_ERROR) {
	    print STDERR "*** Failed to reset OS and reboot nodes.\n";
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
	    #
	    # If there is a firewall involved, it could be that the
	    # firewall rules are preventing essential communication,
	    # so don't retry.
	    #
	    # XXX should only do this if the user has specified additional
	    # rules.  But right now, I may screw up too!
	    #
	    if ($firewalled) {
		print STDERR "Not retrying, ".
		    "firewall may be preventing setup.\n";
		return 1;
	    }

Chad Barb's avatar
 
Chad Barb committed
1100
	    #
Chad Barb's avatar
   
Chad Barb committed
1101
1102
	    # Use returncode from os_setup process to
	    # set global $retry flag, indicating to caller
Chad Barb's avatar
 
Chad Barb committed
1103
1104
1105
	    # that it may be beneficial to attempt
	    # a doSwapin() again.
	    #
Chad Barb's avatar
   
Chad Barb committed
1106
            if (($CHILD_ERROR >> 8) == 1) {
Chad Barb's avatar
Chad Barb committed
1107
		return 3;
Chad Barb's avatar
   
Chad Barb committed
1108
1109
	    } else {
		print STDERR "Not retrying due to error type.\n";
Chad Barb's avatar
Chad Barb committed
1110
		return 1;
Chad Barb's avatar
   
Chad Barb committed
1111
	    }
Chad Barb's avatar
 
Chad Barb committed
1112
1113
1114
1115
1116
1117
1118
	}
    } else {
	undef $os_setup_pid;
	print STDERR "*** Error waiting for os_setup to finish.\n";
	return 1;
    }
    TBDebugTimeStamp("os_setup finished");
Chad Barb's avatar
Chad Barb committed
1119

Chad Barb's avatar
 
Chad Barb committed
1120
1121
1122
1123
    #
    # Okay, start the event system now that we know all the nodes have
    # rebooted (os_setup is done). This only takes a moment (puts itself
    # in the background), so its not enough of a delay to worry about.
Chad Barb's avatar
   
Chad Barb committed
1124
1125
    # Don't do this during an update, since we didn't kill the 
    # event system previously, so starting it again will fail!
Chad Barb's avatar
 
Chad Barb committed
1126
    # 
1127
    if (! ($DISABLE_EVENTS || $elabinelab)) {
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
	#
	# For the robot testbed, start the location piper *before* the event
	# system.
	#
	if (-x $piper && ($type != UPDATE && $type != UPDATE_RECOVER)) {
	    print "Starting the location piper.\n";
	    if (system("$piper $pid $eid")) {
		print STDERR "*** Failed to start the location piper.\n";
		return 1;
	    }
	}
	
1140
1141
	if ( $update_Eventsys_restart || 
	    ($type != UPDATE && $type != UPDATE_RECOVER) ) {
Chad Barb's avatar
   
Chad Barb committed
1142
1143
1144
1145
1146
1147
1148
	    print "Starting the event system.\n";
	    TBDebugTimeStamp("eventsys_control started");
	    if (system("eventsys_control start $pid $eid")) {
		print STDERR "*** Failed to start the event system.\n";
		return 1;
	    }
	    TBDebugTimeStamp("eventsys_control finished");
Chad Barb's avatar
 
Chad Barb committed
1149
1150
	}
    }
Chad Barb's avatar
Chad Barb committed
1151

1152
1153
1154
    #
    # ElabinElab setup. This might not be the right place for this!
    #
1155
1156
1157
    if ($elabinelab && !$TESTMODE && ($type == REAL || $type == UPDATE)) {
	my $optarg = ($type == UPDATE ? "-u" : "");
	
1158
1159
	print "Setting up elabinelab. This could take a while!\n";
	TBDebugTimeStamp("elabinelab setup started");
1160
	if (system("elabinelab $optarg $pid $eid")) {
1161
1162
1163
	    print STDERR "*** Failed to setup elabinelab!\n";
	    return 1;
	}
1164
	TBDebugTimeStamp("ElabInElab setup finished");
1165
1166
    }

Chad Barb's avatar
 
Chad Barb committed
1167
1168
    return 0;
}
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180

#
# Setup and teardown experiment firewall.
#
# XXX note that right now, we just setup the switch infrastructure
# first, and then just let everything else go.  Firewalled nodes will
# not boot until the firewall is up since the VLAN is isolated til then.
# The firewall will boot ok since it still talks to the real control net.
#
# XXX for tearing down firewalls, we assume that nodes have been "cleansed"
# and it is safe to put ports back into the default control net VLAN.
#
1181
1182
sub doFW($$$$) {
    my ($pid, $eid, $action, $nodelist) = @_;
1183
    my ($fwnode, $fwvlanname, $fwvlan, $fwport, $fwvid);
1184
    my %nodenames;
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197

    #
    # See if there is a firewall, fetching node/VLAN info if so.
    # If not, we are all done.
    #
    if (!TBExptFirewall($pid, $eid, \$fwnode, \$fwvid, \$fwvlan)) {
	return 0;
    }

    if ($action == FWSETUP) {
	$fwvid = TBGetUniqueIndex("cnet_vlanid");

	print "Setting up control net firewall.\n";
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
    }
    else {
	if ($action == FWADDNODES) {
	    print "Adding nodes to control net firewall.\n";
	}
	elsif ($action == FWDELNODES) {
	    print "Removing nodes from control net firewall.\n";
	}
	else {
	    print "Tearing down control net firewall.\n";
	}
1209
1210
1211
1212
1213
1214
1215

	# Prior setup didn't succeed, nothing to do
	if (!defined($fwvid)) {
	    return 0;
	}
    }

1216
1217
1218
1219
1220
1221
1222
    # See below.
    if (defined($nodelist)) {
	foreach my $node (@$nodelist) {
	    $nodenames{$node} = $node;
	}
    }

1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
    # XXX vlanid in the DB is currently an int, we need a more unique name
    $fwvlanname = "fw$fwvid";

    #
    # Find all the experiment nodes and their control interface switch ports
    #
    # XXX this may be replaced by a call to SNMPIT that just specifies
    # the pid/eid.  In that case someone else will first have to poplulate
    # the vlans table with this same info. 
    #
    my $db_result =
1234
1235
1236
1237
1238
1239
	DBQueryWarn("SELECT r.node_id,w.card1 ".
		    "  FROM wires AS w, reserved AS r ".
		    "WHERE r.node_id=w.node_id1 AND r.pid='$pid' ".
		    "  AND r.eid='$eid' AND w.type='Control'");
    return 1
	if (!$db_result);
1240
1241
1242

    my $portlist = "";
    while (my ($node,$cif) = $db_result->fetchrow_array()) {
1243
	print "$node $cif\n";
1244
1245
	if ($node eq $fwnode) {
	    $fwport = "$node:$cif";
1246
1247
1248
1249
1250
1251
1252
1253
	}
	elsif (defined($nodelist)) {
 	    print "foo @$nodelist\n";
	    # Only nodes we are moving in/out of the experiment.
	    $portlist .= " $node:$cif"
		if (exists($nodenames{$node}));
	}
	else {
1254
1255
1256
1257
	    $portlist .= " $node:$cif";
	}
    }
    if (!defined($fwport)) {
1258
	print STDERR "*** Firewall node '$fwnode' not found in $pid/${eid}!\n";
1259
1260
1261
	return 0;
    }
    if ($portlist eq "") {
1262
1263
1264
1265
1266
1267
	#
	# We catch this up in swapexp; admin users can specify just a firewall,
	# but mere users must have at least one firewalled node. Just print
	# the warning though. 
	# 
	print STDERR "*** WARNING: No firewalled nodes in $pid/${eid}!\n";
1268
1269
1270
1271
1272
1273
1274
    }

    #
    # XXX hack commands til we nail down the API
    #
    my $fwsetupstr1 = "snmpit $cnetstack -m $fwvlanname $portlist";
    my $fwsetupstr2 = "snmpit $cnetstack -T $fwport $cnetvlanname $fwvlanname";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1275
    my $fwtakedownstr0 = "snmpit $cnetstack -e $fwport";
1276
1277
    my $fwtakedownstr1 = ($portlist eq "" ? "true" :
			  "snmpit $cnetstack -m $cnetvlanname $portlist");
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
    my $fwtakedownstr2 = "snmpit $cnetstack -o $fwvlanname";
    my $fwtakedownstr3 = "snmpit $cnetstack -U $fwport";
    my $fwtakedownstr4 = "snmpit $cnetstack -m $cnetvlanname $fwport";

    if (0) {
    # XXX debug fer now
    TBDebugTimeStamp("    setup1: $fwsetupstr1");
    TBDebugTimeStamp("    setup2: $fwsetupstr2");
    TBDebugTimeStamp(" teardown1: $fwtakedownstr1");
    TBDebugTimeStamp(" teardown2: $fwtakedownstr2");
    TBDebugTimeStamp(" teardown3: $fwtakedownstr3");
    TBDebugTimeStamp(" teardown4: $fwtakedownstr4");
    if ($action == FWSETUP) {
	my $lan = 100 + $fwvid;
	$fwsetupstr1    = "/bin/echo VLAN \#$lan on cisco2";
	$fwsetupstr2    = "true";
    } else {
	$fwtakedownstr1 = "true";
	$fwtakedownstr2 = "true";
	$fwtakedownstr3 = "true";
	$fwtakedownstr4 = "true";
    }
    }

    if ($action == FWSETUP) {
	TBDebugTimeStamp("snmpit firewall setup: VLAN");
1304
	print "doFW: '$fwsetupstr1'\n";
1305
1306
1307
1308
1309
1310
1311
1312
	my $snmpit_out = `$fwsetupstr1`;
	if ($? != 0 || $snmpit_out !~ /VLAN #(\d+) on /) {
	    print STDERR "*** Failed to setup Firewall control net VLAN.\n";
	    return 1;
	}
	my $fwvlan = $1;

	TBDebugTimeStamp("snmpit firewall setup: trunk");
1313
	print "doFW: '$fwsetupstr2'\n";
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
	if (system($fwsetupstr2)) {
	    print STDERR "*** Failed to setup Firewall trunk on port $fwport.\n";
	    if (system($fwtakedownstr1)) {
		print STDERR
		    "*** Could not return $portlist to Control VLAN!\n";
	    }
	    if (system($fwtakedownstr2)) {
		print STDERR
		    "*** Could not destroy VLAN $fwvlanname ($fwvlan)!\n";
	    }
	    return 1;
	}
	TBDebugTimeStamp("snmpit firewall setup done");

	# Record VLAN info now that everything is done
	TBSetExptFirewallVlan($pid, $eid, $fwvid, $fwvlan);
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
    }
    elsif ($action == FWADDNODES) {
	TBDebugTimeStamp("snmpit firewall port addition");
	print "doFW: '$fwsetupstr1'\n";
	my $snmpit_out = `$fwsetupstr1`;
	if ($?) {
	    print STDERR
	        "*** Failed to add nodes to Firewall control net VLAN.\n";
	    return 1;
	}
	TBDebugTimeStamp("snmpit firewall setup done");
    }
    elsif ($action == FWDELNODES) {
	TBDebugTimeStamp("snmpit firewall port deletion");
	print "doFW: '$fwtakedownstr1'\n";
	if (system($fwtakedownstr1)) {
	    print STDERR
		"*** Failed to remove nodes from Firewall control net VLAN.\n";
	    return 1;
	}
	TBDebugTimeStamp("snmpit firewall setup done");
    }
    else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1353
	TBDebugTimeStamp("snmpit re-enable fw control port: $fwport");
1354
	print "doFW: '$fwtakedownstr0'\n";
1355
	my $failed = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1356
1357
1358
1359
1360
1361
	if (system($fwtakedownstr0)) {
	    print STDERR
		"*** Could not re-enable firewall control port $fwport!\n";
	    $failed = 1;
	}
	TBDebugTimeStamp("snmpit firewall teardown: VLAN");
1362
	print "doFW: '$fwtakedownstr1'\n";
1363
1364
1365
1366
1367
	if (system($fwtakedownstr1)) {
	    print STDERR
		"*** Could not return $portlist to Control VLAN!\n";
	    $failed = 1;
	}
1368
	print "doFW: '$fwtakedownstr2'\n";
1369
1370
1371
1372
1373
1374
	if (system($fwtakedownstr2)) {
	    print STDERR
		"*** Could not destroy VLAN $fwvlanname ($fwvlan)!\n";
	    $failed = 1;
	}
	TBDebugTimeStamp("snmpit firewall teardown: trunk");
1375
	print "doFW: '$fwtakedownstr3'\n";
1376
1377
1378
1379
1380
	if (system($fwtakedownstr3)) {
	    print STDERR
		"*** Could not tear down trunk on $fwport!\n";
	    $failed = 1;
	}
1381
	print "doFW: '$fwtakedownstr4'\n";
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
	if (system($fwtakedownstr4)) {
	    print STDERR
		"*** Could not return $fwport to Control VLAN!\n";
	    $failed = 1;
	}
	if ($failed) {
	    return 1;
	}
	TBDebugTimeStamp("snmpit firewall teardown done");

	# Clean VLAN info from DB
1393
	TBClearExptFirewallVlan($pid, $eid);
1394
1395
1396
    }
    return 0;
}