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

#
# Copyright (c) 2012 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

use English;
use strict;

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

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

#
# Untaint the path
# 
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

# A ghastly bit of SQL to retrieve all testnode PCs, their reservation
# status, and number of experimental interfaces.
my $result = DBQueryWarn( "SELECT nodes.node_id, reserved.eid, " .
			  "COUNT(interfaces.node_id) FROM nodes " .
			  "LEFT OUTER JOIN reserved ON " .
			  "nodes.node_id = reserved.node_id INNER JOIN " .
			  "node_types ON nodes.type = node_types.type " .
			  "INNER JOIN interfaces ON nodes.node_id = " .
			  "interfaces.node_id " .
			  "WHERE node_types.class = 'pc' AND " .
			  "nodes.role = 'testnode' AND " .
			  "interfaces.role = 'expt' " .
			  "GROUP BY nodes.node_id;" );

die( "Could not retrieve node list!" ) unless defined( $result );

my $warnings = 0;
my $num_nodes = 0;
my $max_ifs = 0;
my $total_ifs = 0;
my @nodes = ();

while( my( $node_id, $eid, $ifs ) = $result->fetchrow_array() ) {
    if( defined( $eid ) ) {
	if( $warnings < 10 ) {
	    print STDERR "warning: $node_id is reserved\n";
	    $warnings++;
	}
    } else {
	my %dat = ( name => $node_id, ifs => $ifs );
	push( @nodes, \%dat );
	$max_ifs = $ifs if( $ifs > $max_ifs );
	$total_ifs += $ifs;
	$num_nodes++;
    }
}

print "set ns [new Simulator]\n";
print "source tb_compat.tcl\n\n";

my $i = 0;
foreach my $node (@nodes) {
    # It would be nice to use the physical node_ids for the Tcl node
    # names.  Unfortunately, we can't guarantee that they are legal
    # Tcl identifiers.  So we have to make up silly generic names instead.
    print "set node$i [\$ns node]\n";
    print "tb-fix-node \$node$i \"$node->{name}\"\n";
    print "tb-set-node-failure-action \$node$i \"nonfatal\"\n";
    $i++;
}

# Pick a sensible number of LANs to create.  The number isn't really
# critical... let's just pick sqrt( $total_ifs ) to avoid a huge number
# of tiny LANs or a tiny number of huge LANs.  Then make sure it's
# at least $max_ifs -- since we'll assign interfaces to LANs round-robin
# style, this will ensure each interface on any one node belongs to a
# different LAN.
my $num_lans = int( sqrt( $total_ifs ) );
$num_lans = $max_ifs if( $max_ifs > $num_lans );

my @lans = ("") x $num_lans;

$i = 0;
my $next_lan = 0;
foreach my $node (@nodes) {
    foreach (1..$node->{ifs}) {
	$lans[ $next_lan ] .= "\$node$i ";
	$next_lan++;
	$next_lan = 0 if( $next_lan == $num_lans );
    }
    $i++;
}

$i = 0;
print "\n";
foreach (@lans) {
    # Make all LANs 1 gigabit... this needs to be made more sophisticated.
    # We don't want to require any delay nodes, and we should really
    # set each interface to full speed and group them into VLANs
    # accordingly.  But we don't want to oversubscribe inter-switch
    # trunks, either!
    #
    # Fortunately, the 1 Gb default works fine for InstaGENI racks, which
    # is our immediate concern.
    chop;
    # nsverify is unhappy unless we assign the result somewhere
    print "set lan$i [\$ns make-lan \"$_\" 1000Mb 0ms]\n";
    $i++;
}

print "\n\$ns run\n";

exit( 0 );