rc.trace 8.53 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;
use POSIX qw(setsid);

sub usage()
{
    print "Usage: " .
	scriptname() . " [-j vnodeid] [-r rtabid] ".
	               "boot|shutdown|reconfig|reset\n";
    exit(1);
}
my $optlist = "j:r:";
my $action  = "boot";
my $vnodeid;
my $rtabid;		# Currently ignored.

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

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

# Only root.
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

# Script specific goo.
my $PCAPPER    = "/usr/local/etc/emulab/pcapper";
my @childpids  = ();

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;
use libtmcc;
use librc;

#
# Not all clients support this.
#
exit(0)
    if (MFS() || PLAB());

# Now safe to do this since MFS does not have full perl install.
require Socket;
import Socket;

# Protos.
sub doboot();
sub doshutdown();
sub doreconfig();
sub docleanup();

# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'j'})) {
    $vnodeid = $options{'j'};
    libsetup_setvnodeid($vnodeid);
}
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

# Local goo
# After libsetup include; tcpdumps run outside vnodes.
my $PIDFILE  = (defined($vnodeid) ? CONFDIR() : "/var/run") . "/trace.pid";
my $LOGFILE  = (defined($vnodeid) ? CONFDIR() : $LOGDIR)    . "/trace.debug";

# Execute the action.
SWITCH: for ($action) {
    /^boot$/i && do {
	doboot();
	last SWITCH;
    };
    /^shutdown$/i && do {
	doshutdown();
	last SWITCH;
    };
    /^reconfig$/i && do {
	doreconfig();
	last SWITCH;
    };
    /^reset$/i && do {
	docleanup();
	last SWITCH;
    };
    fatal("Invalid action: $action\n");
}
exit(0);

# Signal handler to initiate cleanup in parent and the children.
sub Pcleanup($)
{
    my ($signame) = @_;

    $SIG{TERM} = 'IGNORE';
    $SIG{INT}  = 'IGNORE';
    $SIG{HUP}  = 'IGNORE';

    foreach my $pid (@childpids) {
	print "Send a kill to $pid\n";
	system("kill $pid");
	waitpid($pid, 0);
    }

    unlink $PIDFILE;
    exit(0);
}

#
# Boot Action.
#
sub doboot()
{
    my @traces;

    print STDOUT "Checking Testbed trace configuration ... \n";

    if (gettraceconfig(\@traces)) {
	fatal("Could not get trace configuration from libsetup!");
    }

    return
	if (!@traces);

    #
    # Create the /local partition to hold the logs, and mkdir the log
    # directory. This works on Linux too!
    #
    if (! JAILED()) {
	if (! -d "/local") {
	    system("mkdir /local") == 0
		or fatal("Could not mkdir /local");
	    system("$BINDIR/mkextrafs.pl -f /local");
	}
	if (! -d "/local/local") {
	    fatal("Could not create the /local filesystem");
	}
    }
    if (! -d "/local/logs") {
	system("mkdir /local/logs") == 0
	    or fatal("Could not mkdir /local/logs");
    }

    #
    # Need this for setting up the event sys arguments.
    # 
    my ($pid, $eid, $vname) = check_nickname();

    #
    # Now daemonize and let the boot continue. We have to disconnect and
    # change our session id so that we do not get killed off (nor the tcpdump
    # children) when boot script terminates. 
    #
    if (TBBackGround($LOGFILE)) {
	sleep(1);
	exit(0);
    }
    # Fully disconnect from bootup. 
    setsid();

    #
    # Write our pid to a file so we can be killed later.
    # 
    system("echo '$PID' > $PIDFILE") == 0
	or fatal("Could not create $PIDFILE!");
    
    $SIG{TERM} = \&Pcleanup;
    $SIG{INT}  = \&Pcleanup;
    $SIG{HUP}  = \&Pcleanup;

    foreach my $trace (@traces) {
	my $linkname      = $trace->{"LINKNAME"};
	my $idx           = $trace->{"IDX"};
	my $mac0          = $trace->{"MAC0"};
	my $mac1          = $trace->{"MAC1"};
	my $vnode         = $trace->{"VNODE"};
	my $vnode_mac     = $trace->{"VNODE_MAC"};
	my $trace_type    = $trace->{"TRACE_TYPE"};
	my $trace_expr    = $trace->{"TRACE_EXPR"};
	my $trace_snaplen = $trace->{"TRACE_SNAPLEN"};
	my $log           = "$LOGDIR/trace_${linkname}-${vnode}.debug";
 	my $optargs	  = "";
	my $eventargs     = "-e $pid/$eid -s localhost";
	my $expr          = "";

	#
	# For the event system, listen for events to the entire link/lan,
	# and for this node on the link/lan.
	#
	$eventargs .= " -L ${linkname}-${vnode}-tracemon,${linkname}-tracemon";

	#
	# User can specify a snaplen. Otherwise, If the trace type is
	# "packet" then want a big enough snaplen to get an entire
	# ethernet packet. 
	#
	if ($trace_snaplen > 0) {
	    $optargs .= "-l $trace_snaplen";
	}
	elsif ($trace_type eq "packet") {
	    $optargs .= "-l 1518";
	}
	if ($trace_type eq "packet" || $trace_type eq "header") {
	    $optargs .= " -c";
	}
	if (defined($idx)) {
	    #
	    # Construct a local port argument that is known by boss.
	    #
	    $optargs .= " -P " . (4442 + $idx);
	}

	if (! JAILED()) {
	    #
	    # Need the ethernet address for the vnode so we can create an ether
	    # rule for tcpdump. We want to make sure that we get the correct
	    # packets. We want to capture just the packets from the source node
	    # for the link, both before the pipe and after the pipe.  In this
	    # way, we prevent too much duplication of packets, but we also get
	    # to see how the dummynet pipes have affected te packets, since an
	    # packet will end up in the output twice; once before the pipe and
	    # again after the pipe. At the same time, we can also support
	    # asymmetric rules (remember, a duplex link has two queues).
	    #
	    if ($vnode_mac =~ /^(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})$/) {
		$vnode_mac = "$1:$2:$3:$4:$5:$6";
	    }
	    else {
		fatal("Bad mac in trace config: $vnode_mac\n");
	    }
	    $expr = "ether src $vnode_mac";
	}

	#
	# If the user supplied their own rules, then combine it with the
	# above expression. 
	#
	if (defined($trace_expr) and $trace_expr ne "") {
	    if ($expr eq "") {
		$expr = $trace_expr;
	    }
	    else {
		$expr = "($expr) and ($trace_expr)";
	    }
	}

	# Do this sillyness to avoid a wrapper sh, provided by perl.
	# See perl manpage topic on exec ...
	my @cmd  = split('\s+', "$PCAPPER -I $optargs $eventargs -t");
	my @args = ();

	foreach my $dir ("recv", "xmit") {
	    my $file = "/local/logs/trace_${vnode}-${linkname}.${dir}";
	    my $iface;
	    my $mac;

	    #
	    # Need the proper iface.
	    #
	    if ($dir eq "recv") {
		$mac = $mac0;
	    }
	    else {
		$mac = $mac1;
	    }

	    #
	    # If the iface is not there, then the tcpdump is getting
	    # run on the end node (as in a linkdelay or jail). In this case
	    # there is no input interface to bolt the tcpdump to; we just
	    # capture packets as they leave (after the ipfw dummynet pipe).
	    #
	    next
		if (!defined($mac) || $mac eq "");

	    # convert to iface.
	    if (! ($iface = findiface($mac))) {
		print "*** WARNING: Could not map MAC $mac to an interface!\n";
		next;
	    }
	    push(@cmd, "-f");
	    push(@cmd, $file);
	    push(@args, $iface);
	    push(@args, "$expr");
	}
	my @cmdargs = (@cmd, @args);
	print "Starting tcpdump: '@cmdargs'\n";

	#
	# Fork a child to run tcpdump. The parent saves up all the
	# pids so that we can react to a shutdown later. We do this cause
	# tcpdump is 3rd party and we do not have very good control
	# over its output, and we want to avoid a shell wrapper around it
	# by using a redirect. 
	#
	my $childpid = fork();
	if ($childpid) {
	    #
	    # Parent. Save pid and keep going.
	    #
	    push(@childpids, $childpid);
	    sleep(1);
	}
	else {
	    $SIG{TERM} = 'DEFAULT';
	    $SIG{INT}  = 'IGNORE';
	    $SIG{HUP}  = 'DEFAULT';

	    open(STDIN, "</dev/null") or
		fatal("opening /dev/null for STDIN: $!");

	    # Redirect stdout/stderr to log file.
	    open(STDOUT, ">>$log")
		or fatal("Could not redirect STDOUT");
	    open(STDERR, ">&STDOUT")
		    or fatal("Could not redirect STDERR");

	    exec(@cmdargs);
	    die("*** $0:\n".
		"    Could not exec '@cmdargs'\n");
	}
    }

    #
    # Just wait. We will die via the signal handler above.
    #
    while (1) {
	sleep(10000);
    }
    return;
}

#
# Shutdown Action.
#
sub doshutdown()
{
    if (-e $PIDFILE) {
	system("kill `cat $PIDFILE`");
	sleep(1);
	# Wait for it to die.
	while (-e $PIDFILE) {
	    print "Waiting for $PIDFILE to die ...\n";
	    sleep(10);
	}
    }
}

#
# Node Reconfig Action (without rebooting).
#
sub doreconfig()
{
    # Shutdown tunnels before doing reconfig.
    doshutdown();
    return doboot();
}

#
# Node cleanup action (node is reset to completely clean state).
#
sub docleanup()
{
}