1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | # Kevin Lahey, lahey@isi.edu |
---|
4 | # July 11, 2007 |
---|
5 | |
---|
6 | # Set up ssh tunnel infrastructure for federation: |
---|
7 | # |
---|
8 | # * Set up the synchronization system |
---|
9 | # |
---|
10 | # * Figure out our location. |
---|
11 | # From DETER: |
---|
12 | # + bring up em0 and assign the appropriate address (with only one |
---|
13 | # tunnel node, this'll be static) |
---|
14 | # + ssh out to WAIL node using the experiment name, creating a tunnel |
---|
15 | # for each of the experimental interfaces that is up (one, for now) |
---|
16 | # |
---|
17 | # From WAIL: |
---|
18 | # + exit and wait for the ssh from DETER, which'll trigger the |
---|
19 | # setup of everything else |
---|
20 | # |
---|
21 | # * pick out the experimental interface, remove the IP address |
---|
22 | # |
---|
23 | # * identify the tun interface created, and bridge it to the |
---|
24 | # experimental interface |
---|
25 | |
---|
26 | use strict; |
---|
27 | use Getopt::Std; |
---|
28 | use POSIX qw(strftime); |
---|
29 | use Sys::Hostname; |
---|
30 | |
---|
31 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
32 | my $SSH = "/usr/local/bin/ssh"; |
---|
33 | my $HOME = "/users/lahey"; |
---|
34 | |
---|
35 | # Where are we gonna get this info from!? Hardcoding is definitely not |
---|
36 | # gonna happen! |
---|
37 | |
---|
38 | my $tunnel_ip = "206.117.25.26"; |
---|
39 | my $tunnel_iface = "em0"; |
---|
40 | my $remote_node = "detertunnel."; |
---|
41 | my $remote_node_domain = ".deter.emulab.net"; |
---|
42 | my $ssh_port_fwds = "-R :139:users.isi.deterlab.net:139 -R :7777:boss.isi.deterlab.net:7777"; |
---|
43 | |
---|
44 | my $debug = 1; |
---|
45 | my $remote; |
---|
46 | |
---|
47 | my $count; |
---|
48 | my $addr; |
---|
49 | |
---|
50 | sub find_new_tun; |
---|
51 | sub setup_network($; $; $; $); |
---|
52 | |
---|
53 | # Option use is as follows: |
---|
54 | # -r remote execution on WAIL; set up as appropriate |
---|
55 | |
---|
56 | my $usage = "Usage: fed-tun.pl [-t remote-testbed-fqdn] [-r hostname count]\n"; |
---|
57 | |
---|
58 | my %opts; |
---|
59 | if ($#ARGV != 0 && !getopts('rt:', \%opts)) { |
---|
60 | die "$usage"; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | if (defined($opts{'t'})) { |
---|
65 | $remote_node_domain = $opts{'t'}; |
---|
66 | } |
---|
67 | |
---|
68 | if (defined($opts{'r'})) { |
---|
69 | $remote = 1; |
---|
70 | die "$usage" if ($#ARGV != 1); |
---|
71 | |
---|
72 | $count = pop @ARGV; |
---|
73 | $addr = pop @ARGV; |
---|
74 | } |
---|
75 | |
---|
76 | # Set up synchronization, so that the various user machines won't try to |
---|
77 | # contact boss before the tunnels are set up. |
---|
78 | |
---|
79 | # XXX: put off for now |
---|
80 | |
---|
81 | # Need these to make the Ethernet tap and bridge to work... |
---|
82 | |
---|
83 | system("kldload /boot/kernel/if_bridge.ko"); |
---|
84 | system("kldload /boot/kernel/if_tap.ko"); |
---|
85 | |
---|
86 | # Ask tmcd to figure out experiment and project; these should |
---|
87 | # be available as environment variables (PID, EID, and NODE) for |
---|
88 | # a startup script, but if run explicitly, we might not have 'em. |
---|
89 | |
---|
90 | my $project; |
---|
91 | my $experiment; |
---|
92 | my $node_id; |
---|
93 | |
---|
94 | open(TMCD, "$TMCC status |") || die "tmcc failed\n"; |
---|
95 | while (<TMCD>) { |
---|
96 | print if ($debug); |
---|
97 | if (/ALLOCATED=([\w\-]+)\/([\w\-]+) NICKNAME=([\w\-]+)/) { |
---|
98 | $project = $1; |
---|
99 | $experiment = $2; |
---|
100 | $node_id = $3; |
---|
101 | } |
---|
102 | } |
---|
103 | close(TMCD); |
---|
104 | |
---|
105 | die "Didn't find experiment or project name\n" if (!$project || !$experiment); |
---|
106 | |
---|
107 | print "project $project experiment $experiment node $node_id\n" if ($debug); |
---|
108 | |
---|
109 | |
---|
110 | # Figure out whether we're sourcing the tunnel or sinking it. For now |
---|
111 | # I'll do something ugly and use the hostname. |
---|
112 | |
---|
113 | my $hostname = hostname(); |
---|
114 | my @names = split(/\./, $hostname); |
---|
115 | |
---|
116 | if ($#names > 1) { |
---|
117 | my $domain = $names[$#names - 1]; |
---|
118 | if (!$remote && $domain ne "deterlab") { |
---|
119 | # Fix up ssh (ugh!) |
---|
120 | system("sed 's/tunnel.simplefed/wailtunnel.$experiment/' $HOME/root-id_rsa.pub >> /root/.ssh/authorized_keys"); |
---|
121 | die "Not on DETER; exiting for now\n"; |
---|
122 | } |
---|
123 | } else { |
---|
124 | die "Failed to find a name for this host; exiting\n"; |
---|
125 | } |
---|
126 | |
---|
127 | if (!$remote) { |
---|
128 | # If we're at DETER, open up a separate tunnel to the remote host for |
---|
129 | # each of the different experiment net interfaces on this machine. |
---|
130 | # Execute this startup script on the far end, but with the -r option |
---|
131 | # to indicate that it's getting invoked remotely and should start |
---|
132 | # setting up. |
---|
133 | |
---|
134 | # Lame DETER setup hacks: |
---|
135 | |
---|
136 | system("ifconfig $tunnel_iface $tunnel_ip"); |
---|
137 | system("route add -net 198.133.225.59 -netmask 0xffffff00 206.117.25.1"); |
---|
138 | system("route add -net 155.98.33.0 -netmask 0xfffff000 206.117.25.1"); |
---|
139 | system("route add 128.9.160.161 206.117.25.1"); |
---|
140 | |
---|
141 | # XXX: fix up ssh. Ugh. |
---|
142 | |
---|
143 | system("cp $HOME/root-id_rsa /root/.ssh/id_rsa"); |
---|
144 | system("cp $HOME/root-id_rsa.pub /root/.ssh/id_rsa.pub"); |
---|
145 | |
---|
146 | # XXX: Do we need to clear out previously created bridge interfaces? |
---|
147 | |
---|
148 | my $remote_name = $remote_node . $experiment . $remote_node_domain; |
---|
149 | my $count = 0; |
---|
150 | |
---|
151 | open(IFFILE, "/var/emulab/boot/ifmap") || die "couldn't open ifmap\n"; |
---|
152 | while (<IFFILE>) { |
---|
153 | my @a = split(' '); |
---|
154 | my $iface = $a[0]; |
---|
155 | my $addr = $a[1]; |
---|
156 | my $bridge = "bridge" . $count; |
---|
157 | my $tun = "tap" . $count; |
---|
158 | |
---|
159 | print "Found $iface, $addr, to bridge on $bridge\n" if ($debug); |
---|
160 | |
---|
161 | system("$SSH -w $count:$count $ssh_port_fwds $remote_name \"$HOME/fed-tun.pl -r $addr $count\" &"); |
---|
162 | |
---|
163 | # XXX: Ack, ssh will never return, since it's doing tunneling. |
---|
164 | # Unfortunately, though, it also could take quite a few seconds |
---|
165 | # to do the DNS lookup and actually run the command on the |
---|
166 | # other side. Hence, the delay. Shudder: |
---|
167 | |
---|
168 | sleep 10; |
---|
169 | |
---|
170 | setup_network($tun, $bridge, $iface, $addr); |
---|
171 | $count++; |
---|
172 | } |
---|
173 | close(IFFILE); |
---|
174 | } else { |
---|
175 | # We're on the remote system; for now, we just grab and use |
---|
176 | # the one experimental interface. Later, we'll actually look |
---|
177 | # at the address passed to match up the appropriate interfaces. |
---|
178 | |
---|
179 | my $iter = 0; |
---|
180 | |
---|
181 | open(IFFILE, "/var/emulab/boot/ifmap") || die "couldn't open ifmap\n"; |
---|
182 | while (<IFFILE>) { |
---|
183 | die "Argh, too many experimental interfaces!" if ($iter > 0); |
---|
184 | my @a = split(' '); |
---|
185 | my $iface = $a[0]; |
---|
186 | my $addr = $a[1]; |
---|
187 | my $bridge = "bridge" . $count; |
---|
188 | my $tun = "tap" . $count; |
---|
189 | |
---|
190 | setup_network($tun, $bridge, $iface, $addr); |
---|
191 | $iter++; |
---|
192 | } |
---|
193 | close(IFFILE); |
---|
194 | } |
---|
195 | |
---|
196 | print "all done!\n" if ($debug); |
---|
197 | exit; |
---|
198 | |
---|
199 | # XXX: Unused. Find the name of the new tunnel interface |
---|
200 | |
---|
201 | my %tuns_used; |
---|
202 | |
---|
203 | sub find_new_tun { |
---|
204 | my @tuns = split(/ /, `ifconfig -l`); |
---|
205 | my $tun; |
---|
206 | |
---|
207 | print @tuns . "\n"; |
---|
208 | |
---|
209 | foreach my $tun (@tuns) { |
---|
210 | print "Looking at $tun\n"; |
---|
211 | if (!$tuns_used{$tun}) { |
---|
212 | $tuns_used{$tun} = 1; |
---|
213 | print "Found $tun\n"; |
---|
214 | return $tun; |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | # Set up the bridging for the new stuff... |
---|
220 | |
---|
221 | sub setup_network($; $; $; $) { |
---|
222 | my ($tun, $bridge, $iface, $addr) = @_; |
---|
223 | |
---|
224 | print "Got new $tun\n" if ($debug); |
---|
225 | print "setting up $bridge with $iface and nuking $addr\n" if ($debug); |
---|
226 | |
---|
227 | system("ifconfig $bridge create"); |
---|
228 | system("ifconfig $iface delete $addr"); |
---|
229 | system("ifconfig $bridge addm $iface up"); |
---|
230 | system("ifconfig $bridge addm $tun"); |
---|
231 | } |
---|