ptopgen.in 4.44 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
2
#!/usr/bin/perl -w

3
4
5
6
7
8
9
# Settings - Change these to your particular network.

# Switch interface speed - In Mbps
$switch_speed = 100;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
10
11
12
use DBI;
my $TBDB = "@TBDBNAME@";
my $dbh = DBI->connect("DBI:mysql:database=$TBDB;host=localhost");
13
14
15
my $TBROOT = "@prefix@";
push(@INC,"$TBROOT/lib");
require exitonwarn;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
16

17
%switches=();
18
19
%used_switches=();

20
21
22
23
24
# Read class/type maps
$sth = $dbh->prepare("select class,type,delay_capacity from node_types");
$sth->execute;
while (($class,$type,$delaycapacity) = $sth->fetchrow_array) {
    $classes{$type} = $class;
25
    $nodetypes{$type} = $delaycapacity;
26
27
28
29
30
31
}
$sth->finish;

# switches can't delay
$nodetypes{"switch"} = 0;

32
33
34
35
36
37
38
39
# Print switches
$sth = $dbh->prepare("select node_id from nodes where role = \"testswitch\"");
$sth->execute;
while (($switch) = $sth->fetchrow_array) {
    print "node $switch switch:1\n";
    $switches{$switch} = 1;
}
$sth->finish;
40

Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
42
43
44
# Find available nodes
$sth = $dbh->prepare("select a.node_id,a.type from" .
		     " nodes as a left join reserved as b" .
		     " on a.node_id=b.node_id" .
45
		     " where b.node_id is null and a.role = \"testnode\"");
46
$sth->execute;		
Leigh B. Stoller's avatar
Leigh B. Stoller committed
47
48
49
50
51
52
53
54
55
56
57
58
59
while (($node,$type) = $sth->fetchrow_array) {
    # Shark hack
    if (($shelf,$number) = ($node =~ /^sh(\d+)-(\d+)/)) {
	if ($number == 1) {
	    $nodes{"sh$shelf"} = "shark-shelf";
	}
    } else {
	$nodes{$node} = $type;
    }
}
$sth->finish;


60
$sharklinks = "";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61
62
63
64
foreach $node (keys(%nodes)) {
    if ($node =~ /^sh/) {
	print "node $node shark-shelf:1\n";
    } else {
65
66
67
68
	$text = "node $node $nodes{$node}:1 $classes{$nodes{$node}}:1";
	$delay_capacity = $nodetypes{$nodes{$node}};
	if ($delay_capacity > 0) {
	    $text .= " delay:$delay_capacity";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69
70
71
72
73
	}
	print "$text\n";
    }
}

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
# Read interfaces
$sth = $dbh->prepare("SELECT node_id,card,port,iface,interface_type" .
		     " from interfaces");
$sth->execute;
while (($node,$card,$port,$iface,$type) = $sth->fetchrow_array) {
    $interfacemap{"$node:$card:$port"} = $iface;
    if ($type ne "") {
	$interfacetypes{"$node:$card:$port"} = $type;
    }
}
$sth->finish;

# Read interface types
$sth = $dbh->prepare("SELECT type,max_speed from interface_types");
$sth->execute;
while (($type,$speed) = $sth->fetchrow_array) {
    $interfacespeeds{$type} = $speed;
}
$sth->finish;

# Print out links
sub get_iface {
    ($node,$card,$port) = @_;
    if (defined($interfacemap{"$node:$card:$port"})) {
	return $interfacemap{"$node:$card:$port"};
    } else {
Christopher Alfeld's avatar
Christopher Alfeld committed
100
101
102
103
104
105
106
	# shark hack
	if ($node =~ /^sh/) {
	    return "eth0";
	} else {
	    return "(null)";
	}
	# end shark hack 
107
108
109
110
111
112
113
114
115
116
117
    }
};
sub get_ifacebw {
    ($node,$card,$port) = @_;
    if (defined($interfacetypes{"$node:$card:$port"})) {
	return $interfacespeeds{$interfacetypes{"$node:$card:$port"}};
    } else {
	return $switch_speed;
    }
};

118
$sth = $dbh->prepare("SELECT node_id1,card1,port1,node_id2,card2,port2" .
119
 		    " from wires where type=\"Node\" or type=\"Trunk\"");
120
$sth->execute;
121
while (($node1,$card1,$port1,$node2,$card2,$port2) = 
122
       $sth->fetchrow_array) {
123
    if ((defined($nodes{$node1}) || defined($switches{$node1})) && 
124
	(defined($nodes{$node2}) || defined($switches{$node2}))) {
125
126
127
128
129
130
131
132
133
	$iface1 = get_iface($node1,$card1,$port1);
	$iface2 = get_iface($node2,$card2,$port2);
	$iface1bw = get_ifacebw($node1,$card1,$port1);
	$iface2bw = get_ifacebw($node2,$card2,$port2);
	if ($iface1bw < $iface2bw) {
	    $bw = $iface1bw;
	} else {
	    $bw = $iface2bw;
	}
134
	if (defined($switches{$node2})) {
135
136
	    $used_switches{$node2} = 1;
	}
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	if (defined($switches{$node1})) {
	    $used_switches{$node1} = 1;
	}
	if (defined($switches{$node1}) && defined($switches{$node2})) {
	    # interswitch link
	    if (defined($interconnects{"$node1:$node2"})) {
		$interconnects{"$node1:$node2"} += $bw;
	    } else {
		$interconnects{"$node1:$node2"} = $bw;
	    }
	} else {
	    # !!! - Here we use our knowledge that in the wires table links
	    # to the switch always come as node2.
	    print "link link-$node1:$iface1-$node2:$iface2 $node1:$node1/$iface1" .
		" $node2:$iface2 $bw 1\n";
	}
    } 
154
155
}
$sth->finish;
156
157
158
159
foreach $interconnect (keys(%interconnects)) {
    ($src,$dst) = split(":",$interconnect);
    print "link link-$interconnect $src $dst $interconnects{$interconnect} 1\n";
}
160
161
162

print $sharklinks;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
163
# Add a bunch of LANs
164
165
166
167
168
169
foreach $switch (keys(%used_switches)) {
    for ($i = 0;$i <= 20;$i++) {
	print "node lan-$switch-$i lan:1\n";
	print "link link-lan-$switch-$i lan-$switch-${i}:lan-$switch-$i" .
	    " $switch 100000 1000\n";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
170
}