tb_compat.tcl.in 14.9 KB
Newer Older
1
# -*- tcl -*-
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4 5 6 7
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

8 9 10
# This is the tb_compact.tcl that deals with all the TB specific commands.
# It should be loaded at the beginning of any ns script using the TB commands.

Leigh B. Stoller's avatar
Leigh B. Stoller committed
11 12 13 14 15 16 17 18 19 20 21 22 23
# We set up some helper stuff in a separate namespace to avoid any conflicts.
namespace eval TBCOMPAT {
    var_import ::GLOBALS::DB
    var_import ::GLOBALS::pid
    var_import ::GLOBALS::eid

    # This is regular expression that matches slightly more than valid
    # IP addresses.  The only thing it doesn't check is that IP 
    # addresses are in range (i.e. 0-255).
    variable IP {^([0-9]{1,3}\.){3,3}[0-9]{1,3}$}

    # This is an RE to match a floating point number.
    variable FLOAT {(^[0-9]+(\.[0-9]+)?$)|(^\.[0-9]+$)}
24 25 26 27 28 29 30

    # This is the default weight for a soft vtype.
    variable default_soft_vtype_weight 0.5

    # This is the default weight for a hard vtype.
    variable default_hard_vtype_weight 1.0

Leigh B. Stoller's avatar
Leigh B. Stoller committed
31 32 33 34 35 36 37 38 39
    # This is a general procedure that takes a node, an object (lan or link)
    # it is connected to, and an IP address, and sets the IP address
    # for the node on that object.  It checks both the validity of the
    # IP addresses and the fact that the node is actually a member of the
    # lan/link.
    proc set-ip {node obj ip} {
	variable IP
	set caller [lindex [info level -1] 0]
	if {[regexp $IP $ip] == 0} {
40
	    perror "$caller - $ip is not a valid IP address."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41 42 43 44
	    return
	}
	set port [$node find_port $obj]
	if {$port == -1} {
45
	    perror "$caller - $node is not connected to $obj."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
46 47 48 49 50 51 52
	    return
	}
	$node ip $port $ip
    }

    # Let's set up a hwtypes table that contains all valid hardware types.
    variable hwtypes
53 54
    variable isremote
    sql query $DB "select type,isremotenode from node_types"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
55
    while {[set row [sql fetchrow $DB]] != ""} {
56 57 58 59 60
	set type  [lindex $row 0]
	set isrem [lindex $row 1]

	set hwtypes($type) 1
	set isremote($type) $isrem
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61 62
    }
    sql endquery $DB
63
    sql query $DB "select class,isremotenode from node_types"
64
    while {[set row [sql fetchrow $DB]] != ""} {
65 66 67 68 69
	set type  [lindex $row 0]
	set isrem [lindex $row 1]

	set hwtypes($type) 1
	set isremote($type) $isrem
70 71
    }
    sql endquery $DB
Leigh B. Stoller's avatar
Leigh B. Stoller committed
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    # The permissions table. Entries in this table indicate who is allowed
    # to use nodes of a particular type. No entries means anyone can use it.
    #
    # We omit this check in anonymous mode.
    #
    if {!${GLOBALS::anonymous}} {
	variable nodetypeXpid_permissions
    
	sql query $DB "select type,pid from nodetypeXpid_permissions"
	while {[set row [sql fetchrow $DB]] != ""} {
	    set type  [lindex $row 0]
	    set allow [lindex $row 1]

	    if {([info exists nodetypeXpid_permissions($type)])} {
		lappend nodetypeXpid_permissions($type) $allow
	    } else {
		set nodetypeXpid_permissions($type) [list $allow]
	    }
	}
	sql endquery $DB
    }

95 96 97 98
    # And a os table with valid OS Descriptor names. While we still call
    # them "osids", we are using the user level name not the internal,
    # globally unique name. We leave it to a later phase to deal with it.
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
99 100 101 102
    # We omit this check in anonymous mode.
    if {!${GLOBALS::anonymous}} {
	variable osids
	sql query $DB \
103
	    "select osname from os_info where shared=1 or pid='$pid'"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
104 105 106 107 108
	while {[set row [sql fetchrow $DB]] != ""} {
	    set osids($row) 1
	}
	sql endquery $DB
    }
109 110
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
111 112
# IP addresses routines.  These all do some checks and convert into set-ip
# calls.
113
proc tb-set-ip {node ip} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
114 115
    $node instvar portlist
    if {[llength $portlist] != 1} {
116
	perror "\[tb-set-ip] $node does not have a single connection."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
117 118 119
	return
    }
    ::TBCOMPAT::set-ip $node [lindex $portlist 0] $ip
120 121
}
proc tb-set-ip-interface {src dst ip} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
122 123 124 125
    set sim [$src set sim]
    set reallink [$sim find_link $src $dst]
    if {$reallink == {}} {
	perror \
126
	    "\[tb-set-ip-interface] No connection between $src and $dst."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
127
	return
128
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
129
    ::TBCOMPAT::set-ip $src $reallink $ip
130 131
}
proc tb-set-ip-lan {src lan ip} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
132
    if {[$lan info class] != "Lan"} {
133
	perror "\[tb-set-ip-lan] $lan is not a LAN."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
134 135 136
	return
    }
    ::TBCOMPAT::set-ip $src $lan $ip
137 138
}
proc tb-set-ip-link {src link ip} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
139
    if {[$link info class] != "Link"} {
140
	perror "\[tb-set-ip-link] $link is not a link."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
141 142 143
	return
    }
    ::TBCOMPAT::set-ip $src $link $ip
144
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
145 146

# Node state routines.
147
proc tb-set-hardware {node type args} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
148
    var_import ::TBCOMPAT::hwtypes
149
    var_import ::TBCOMPAT::isremote
150 151 152
    var_import ::GLOBALS::vtypes
    if {(! [info exists hwtypes($type)]) &&
	(! [info exists vtypes($type)])} {
153
	perror "\[tb-set-hardware] Invalid hardware type $type."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
154 155
	return
    }
156 157 158
    if {! ${GLOBALS::anonymous}} {
	var_import ::TBCOMPAT::nodetypeXpid_permissions
	var_import ::GLOBALS::pid
Leigh B. Stoller's avatar
Leigh B. Stoller committed
159
	set allowed 1
160 161
	
	if {[info exists nodetypeXpid_permissions($type)]} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
162
	    set allowed 0
163 164 165 166 167 168 169 170 171 172 173
	    foreach allowedpid $nodetypeXpid_permissions($type) {
		if {$allowedpid == $pid} {
		    set allowed 1
		}
	    }
	}
	if {! $allowed} {
	    perror "\[tb-set-hardware] No permission to use type $type."
	    return
	}
    }
174 175 176 177 178
    set remote 0
    if {[info exists isremote($type)]} {
	set remote $isremote($type)
    }
    $node set_hwtype $type $remote
179
}
180

181
proc tb-set-node-os {node os} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
182 183 184
    if {! ${GLOBALS::anonymous}} {
	var_import ::TBCOMPAT::osids
	if {! [info exists osids($os)]} {
185
	    perror "\[tb-set-node-os] Invalid osid $os."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
186 187 188 189 190 191 192 193 194 195
	    return
	}
    }
    $node set osid $os
}
proc tb-set-node-cmdline {node cmdline} {
    $node set cmdline $cmdline
}
proc tb-set-node-rpms {node args} {
    if {$args == {}} {
196
	perror "\[tb-set-node-rpms] No rpms given."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
197 198
	return
    }
199
    $node set rpms [join $args :]
200
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201 202 203 204 205
proc tb-set-node-startup {node cmd} {
    $node set startup $cmd
}
proc tb-set-node-tarfiles {node args} {
    if {$args == {}} {
206
	perror "\[tb-set-node-tarfiles] tb-set-node-tarfiles <node> (<dir> <tar>)+"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
207 208 209
	return
    }
    if {[expr [llength $args] % 2] != 0} {
210
	perror "\[tb-set-node-tarfiles] Arguments should be node and series of pairs."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211 212
	return
    }
213 214 215 216 217 218
    set tarfiles {}
    while {$args != {}} {
	lappend tarfiles [join [lrange $args 0 1]]
	set args [lrange $args 2 end]
    }
    $node set tarfiles [join $tarfiles :]
Leigh B. Stoller's avatar
Leigh B. Stoller committed
219 220 221
}
proc tb-set-node-deltas {node args} {
    if {$args == {}} {
222
	perror "\[tb-set-node-deltas] No deltas given."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
223 224
	return
    }
225
    $node set deltas [join $args :]
Leigh B. Stoller's avatar
Leigh B. Stoller committed
226
}
227 228 229
proc tb-set-ip-routing {type} {
    var_import ::GLOBALS::default_ip_routing_type

230
    if {$type == {}} {
231
	perror "\[tb-set-ip-routing] No type given."
232 233 234
	return
    }
    if {($type != "none") &&
235 236
	($type != "ospf")} {
	perror "\[tb-set-ip-routing] Type is not one of none|ospf"
237 238
	return
    }
239
    set default_ip_routing_type $type
240
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
241 242 243 244 245 246

# Lan/Link state routines.

# This takes two possible formats:
# tb-set-link-loss <link> <loss>
# tb-set-link-loss <src> <dst> <loss>
247
proc tb-set-link-loss {srclink args} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
248
    var_import ::TBCOMPAT::FLOAT
249 250
    if {[llength $args] == 2} {
	set dst [lindex $args 0]
Leigh B. Stoller's avatar
Leigh B. Stoller committed
251 252 253 254
	set lossrate [lindex $args 1]
	set sim [$srclink set sim]
	set reallink [$sim find_link $srclink $dst]
	if {$reallink == {}} {
255
	    perror "\[tb-set-link-loss] No link between $srclink and $dst."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
256
	    return
257 258
	}
    } else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
259
	set reallink $srclink
260
	set lossrate [lindex $args 0]
Leigh B. Stoller's avatar
Leigh B. Stoller committed
261 262 263
    }
    if {([regexp $FLOAT $lossrate] == 0) ||
	($lossrate > 1.0)} {
264
	perror "\[tb-set-link-loss] $lossrate is not a valid loss rate."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
265
    }
266 267
    $reallink instvar loss
    $reallink instvar rloss
Leigh B. Stoller's avatar
Leigh B. Stoller committed
268 269 270
    set adjloss [expr 1-sqrt(1-$lossrate)]
    foreach pair [array names loss] {
	set loss($pair) $adjloss
271
	set rloss($pair) $adjloss
272
    }
273
}
274

Leigh B. Stoller's avatar
Leigh B. Stoller committed
275 276 277
proc tb-set-lan-loss {lan lossrate} {
    var_import ::TBCOMPAT::FLOAT
    if {[$lan info class] != "Lan"} {
278
	perror "\[tb-set-lan-loss] $lan is not a lan."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
279 280 281 282
	return
    }
    if {([regexp $FLOAT $lossrate] == 0) ||
	($lossrate > 1.0)} {
283
	perror "\[tb-set-lan-loss] $lossrate is not a valid loss rate."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284 285
    }
    $lan instvar loss
286
    $lan instvar rloss
Leigh B. Stoller's avatar
Leigh B. Stoller committed
287 288 289
    set adjloss [expr 1-sqrt(1-$lossrate)]
    foreach pair [array names loss] {
	set loss($pair) $adjloss
290
	set rloss($pair) $adjloss
Leigh B. Stoller's avatar
Leigh B. Stoller committed
291
    }
Christopher Alfeld's avatar
Added:  
Christopher Alfeld committed
292
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
293 294 295

proc tb-set-node-lan-delay {node lan delay} {
    if {[$node info class] != "Node"} {
296
	perror "\[tb-set-node-lan-delay] $node is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
297 298 299
	return
    }
    if {[$lan info class] != "Lan"} {
300
	perror "\[tb-set-node-lan-delay] $lan is not a lan."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301 302 303 304
	return
    }
    set port [$lan get_port $node]
    if {$port == {}} {
305
	perror "\[tb-set-node-lan-delay] $node is not in $lan."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
306 307 308
	return
    }
    $lan set delay([list $node $port]) [parse_delay $delay]
309
    $lan set rdelay([list $node $port]) [parse_delay $delay]
Christopher Alfeld's avatar
Added:  
Christopher Alfeld committed
310
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
311 312
proc tb-set-node-lan-bandwidth {node lan bw} {
    if {[$node info class] != "Node"} {
313
	perror "\[tb-set-node-lan-delay] $node is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
314 315 316
	return
    }
    if {[$lan info class] != "Lan"} {
317
	perror "\[tb-set-node-lan-delay] $lan is not a lan."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
318 319 320 321
	return
    }
    set port [$lan get_port $node]
    if {$port == {}} {
322
	perror "\[tb-set-node-lan-delay] $node is not in $lan."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
323 324 325
	return
    }
    $lan set bandwidth([list $node $port]) [parse_bw $bw]
326
    $lan set rbandwidth([list $node $port]) [parse_bw $bw]
327
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
328 329 330
proc tb-set-node-lan-loss {node lan loss} {
    var_import ::TBCOMPAT::FLOAT
    if {[$node info class] != "Node"} {
331
	perror "\[tb-set-node-lan-delay] $node is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
332 333 334
	return
    }
    if {[$lan info class] != "Lan"} {
335
	perror "\[tb-set-node-lan-delay] $lan is not a lan."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
336 337 338 339
	return
    }
    set port [$lan get_port $node]
    if {$port == {}} {
340
	perror "\[tb-set-node-lan-delay] $node is not in $lan."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
341 342 343 344
	return
    }
    if {([regexp $FLOAT $loss] == 0) ||
	($loss > 1.0)} {
345
	perror "\[tb-set-link-loss] $loss is not a valid loss rate."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
346 347
    }
    $lan set loss([list $node $port]) $loss
348
    $lan set rloss([list $node $port]) $loss
Christopher Alfeld's avatar
Added:  
Christopher Alfeld committed
349
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
350 351 352 353
proc tb-set-node-lan-params {node lan delay bw loss} {
    tb-set-node-lan-delay $node $lan $delay
    tb-set-node-lan-bandwidth $node $lan $bw
    tb-set-node-lan-loss $node $lan $loss
354
}
355 356 357 358 359 360 361 362 363 364 365

proc tb-set-node-failure-action {node type} {
    if {[$node info class] != "Node"} {
	perror "\[tb-set-node-failure-action] $node is not a node."
	return
    }
    if {[lsearch -exact {fatal nonfatal ignore} $type] == -1} {
	perror "\[tb-set-node-failure-action] type must be one of fatal|nonfatal|ignore."
	return
    }
    $node set failureaction $type
366 367 368 369 370 371 372 373 374 375
}

proc tb-fix-node {vnode pnode} {
    if {[$vnode info class] != "Node"} {
	perror "\[tb-fix-node] $node is not a node."
	return
    }
    $vnode set fixed $pnode
}

376 377
proc tb-make-soft-vtype {name types} {
    var_import ::TBCOMPAT::hwtypes
378
    var_import ::TBCOMPAT::isremote
379 380 381 382 383 384 385
    var_import ::GLOBALS::vtypes
    var_import ::TBCOMPAT::default_soft_vtype_weight

    foreach type $types {
	if {! [info exists hwtypes($type)]} {
	    perror "\[tb-make-soft-vtype] Invalid hardware type $type."
	}
386 387 388
	if {$isremote($type)} {
	    perror "\[tb-make-soft-vtype] Remote type $type not allowed."
	}
389 390 391 392 393 394
    }
    set vtypes($name) [Vtype $name $default_soft_vtype_weight $types]
}

proc tb-make-hard-vtype {name types} {
    var_import ::TBCOMPAT::hwtypes
395
    var_import ::TBCOMPAT::isremote
396 397 398 399 400 401 402
    var_import ::GLOBALS::vtypes
    var_import ::TBCOMPAT::default_hard_vtype_weight

    foreach type $types {
	if {! [info exists hwtypes($type)]} {
	    perror "\[tb-make-hard-vtype] Invalid hardware type $type."
	}
403 404 405
	if {$isremote($type)} {
	    perror "\[tb-make-hard-vtype] Remote type $type not allowed."
	}
406 407 408 409 410 411
    }
    set vtypes($name) [Vtype $name $default_hard_vtype_weight $types]
}

proc tb-make-weighted-vtype {name weight types} {
    var_import ::TBCOMPAT::hwtypes
412
    var_import ::TBCOMPAT::isremote
413 414 415 416 417
    var_import ::GLOBALS::vtypes
    var_import ::TBCOMPAT::FLOAT

    foreach type $types {
	if {! [info exists hwtypes($type)]} {
418 419 420 421
	    perror "\[tb-make-weighted-vtype] Invalid hardware type $type."
	}
	if {$isremote($type)} {
	    perror "\[tb-make-weighted-vtype] Remote type $type not allowed."
422 423 424 425 426 427 428
	}
    }
    if {([regexp $FLOAT $weight] == 0) ||
	($weight <= 0) || ($weight >= 1.0)} {
	perror "\[tb-make-weighted-vtype] $weight is not a valid weight. (0 < weight < 1)."
    }
    set vtypes($name) [Vtype $name $weight $types]
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 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
}

proc tb-set-link-simplex-params {link src delay bw loss} {
    var_import ::TBCOMPAT::FLOAT
    if {[$link info class] != "Link"} {
	perror "\[tb-set-link-simplex-params] $link is not a link."
	return
    }
    if {[$src info class] != "Node"} {
	perror "\[tb-set-link-simplex-params] $src is not a link."
	return
    }
    set port [$link get_port $src]
    if {$port == {}} {
	perror "\[tb-set-link-simplex-params] $src is not in $link."
	return
    }
    if {([regexp $FLOAT $loss] == 0) || ($loss > 1.0)} {
	perror "\[tb-set-link-simplex-params] $loss is not a valid loss rate."
	return
    }
    set adjloss [expr 1-sqrt(1-$loss)]
    set np [list $src $port]
    foreach nodeport [$link set nodelist] {
	if {$nodeport != $np} {
	    set onp $nodeport
	}
    }

    set realdelay [parse_delay $delay]
    set realbw [parse_bw $bw]
    $link set delay($np) [expr $realdelay / 2.0]
    $link set rdelay($onp) [expr $realdelay / 2.0]
    $link set bandwidth($np) $realbw
    $link set rbandwidth($onp) $realbw
    $link set loss($np) [expr $adjloss]
    $link set rloss($onp) [expr $adjloss]
}

proc tb-set-lan-simplex-params {lan node todelay tobw toloss fromdelay frombw fromloss} {
    var_import ::TBCOMPAT::FLOAT
    if {[$node info class] != "Node"} {
	perror "\[tb-set-node-lan-delay] $node is not a node."
	return
    }
    if {[$lan info class] != "Lan"} {
	perror "\[tb-set-node-lan-delay] $lan is not a lan."
	return
    }
    set port [$lan get_port $node]
    if {$port == {}} {
	perror "\[tb-set-node-lan-delay] $node is not in $lan."
	return
    }
    if {([regexp $FLOAT $toloss] == 0) || ($toloss > 1.0)} {
	perror "\[tb-set-link-loss] $toloss is not a valid loss rate."
    }
    if {([regexp $FLOAT $fromloss] == 0) || ($fromloss > 1.0)} {
	perror "\[tb-set-link-loss] $fromloss is not a valid loss rate."
    }

    set realtodelay [parse_delay $todelay]
    set realfromdelay [parse_delay $fromdelay]
    set realtobw [parse_bw $tobw]
    set realfrombw [parse_bw $frombw]

    $lan set delay([list $node $port]) $realtodelay
    $lan set rdelay([list $node $port]) $realfromdelay
    $lan set loss([list $node $port]) $toloss
    $lan set rloss([list $node $port]) $fromloss
    $lan set bandwidth([list $node $port]) $realtobw
    $lan set rbandwidth([list $node $port]) $realfrombw
501 502 503
}

proc tb-set-uselatestwadata {onoff} {
504
    var_import ::GLOBALS::uselatestwadata
505 506 507 508 509 510 511 512

    if {$onoff != 0 && $onoff != 1} {
	perror "\[tb-set-uselatestwadata] $onoff must be 0/1"
	return
    }

    set uselatestwadata $onoff
}
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

proc tb-set-wasolver-weights {delay bw plr} {
    var_import ::GLOBALS::wa_delay_solverweight
    var_import ::GLOBALS::wa_bw_solverweight
    var_import ::GLOBALS::wa_plr_solverweight

    if {($delay < 0) || ($bw < 0) || ($plr < 0)} {
	perror "\[tb-set-wasolver-weights] Weights must be postive integers."
	return
    }
    if {($delay == {}) || ($bw == {}) || ($plr == {})} {
	perror "\[tb-set-wasolver-weights] Must provide delay, bw, and plr."
	return
    }

    set wa_delay_solverweight $delay
    set wa_bw_solverweight $bw
    set wa_plr_solverweight $plr
}