File: BST.tcl

package info (click to toggle)
ns2 2.35%2Bdfsg-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,808 kB
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (595 lines) | stat: -rw-r--r-- 15,377 bytes parent folder | download | duplicates (8)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#
# tcl/mcast/BST.tcl
#
#
# Copyright (C) 1998 by USC/ISI
# All rights reserved.                                            
#                                                                
# Redistribution and use in source and binary forms are permitted
# provided that the above copyright notice and this paragraph are
# duplicated in all such forms and that any documentation, advertising
# materials, and other materials related to such distribution and use
# acknowledge that the software was developed by the University of
# Southern California, Information Sciences Institute.  The name of the
# University may not be used to endorse or promote products derived from
# this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
# 
#
#
# Written by Yuri Pryadkin
# Modified by Nader Saelhi
#
###############
# Implementation of a simple shared bi-directional tree protocol.  No
# timers.  Nodes send grafts/prunes toward the RP to join/leave the
# group.  The user needs to set two protocol variables: 
#
# "BST set RP_($group) $node" - indicates that $node 
#                               acts as an RP for the $group

Class BST -superclass McastProtocol

BST instproc init { sim node } {
	$self instvar mctrl_ oiflist_
	BST instvar RP_

	set mctrl_ [new Agent/Mcast/Control $self]
	$node attach $mctrl_
	$self next $sim $node
}

BST instproc start {} {
	$self instvar node_ oiflist_
	BST instvar RP_

	# need to do it in start when unicast routing is computed
	foreach grpx [array names RP_] {
                set grp [expr $grpx]
#	      	$self dbg "BST: grp $grp, node [$node_ id]"
               	if { [string compare $grp $grpx] } {
			set RP_($grp) $RP_(grpx)
			unset RP_($grpx)
               	}
		set rpfiif [$node_ from-node-iface $RP_($grp)]
#		$self dbg "BST: rpfiif $rpfiif"
		if { $rpfiif != "?" } {
			set rpfoif [$node_ iif2oif $rpfiif]
		} else {
			set rpfoif ""
		}

		# initialize with the value of rpfoif
		set oiflist_($grp) $rpfoif
		set neighbors [$node_ set neighbor_]
		if [info exists neighbors] {
			for {set i 0} {$i < [llength $neighbors]} {incr i} {
				set neighbor [lindex $neighbors $i]
				set class_info [$neighbor info class]
				if {$class_info == "LanNode"} {
					# This node is on a LAN, we
					# must designate a router for
					# the mcast group   
					$neighbor designate-ump-router $grp \
					    $RP_($grp)
				}
			}
		}
	}

}

BST instproc join-group  { group {src "x"} } {
	$self instvar node_ ns_ oiflist_
	BST instvar RP_
	
	set nbr [$node_ rpf-nbr $RP_($group)]
	set nbrs($nbr) 1
	$node_ add-mark m1 blue "[$node_ get-shape]"
	foreach nbr [array names nbrs] {
		if [$nbr is-lan?] {
			$nbr instvar receivers_
			if [info exists receivers_($group)] {
				incr receivers_($group)
			} else {
				$self send-ctrl "graft" $RP_($group) $group
				set receivers_($group) 1
			}
		}
		$self next $group ; #annotate
	}

	if { ![$node_ check-local $group] || [$node_ getReps "x" \
					      $group] == ""} { 
# 		$self dbg "Sending join-group"
		$self send-ctrl "graft" $RP_($group) $group
	}

}

BST instproc leave-group { group {src "x"} } {
	BST instvar RP_ 

	$self next $group ;#annotate
	$self instvar node_ oiflist_

	set nbr [$node_ rpf-nbr $RP_($group)]
	if  [$nbr is-lan?] {
		$nbr instvar receivers_
		if [info exists receivers_($group)] {
			if {$receivers_($group) > 0} {
				incr receivers_($group) -1
				if {$receivers_($group) == 0} {
					$node_ delete-mark m1
					$self send-ctrl "prune" $RP_($group) $group
				}
			}
		} else {
			# Nobody has joined yet
			return
		}
	} else {
		set rpfiif [$node_ from-node-iface $RP_($group)]
		if { $rpfiif != "?" } {
			set rpfoif [$node_ iif2oif $rpfiif]
		} else {
			set rpfoif ""
		}
		if { $oiflist_($group) == \
			 $rpfoif && ![$node_ check-local $group] } {
			# propagate
			$self send-ctrl "prune" $RP_($group) $group
			$node_ delete-mark m1
		} else {
			$node_ delete-mark m1
			$node_ add-mark m2 red circle
		}
	}

}

# handle-wrong-iif
# This function does nothing
BST instproc handle-wrong-iif { srcID group iface } {
	$self instvar node_ oiflist_
	BST instvar RP_
	
#    	$self dbg "BST: wrong iif $iface, src $srcID, grp $group"
#    	$self dbg "\t oiflist: $oiflist_($group)"

	set rep [$node_ getReps "x" $group]

	$node_ add-mfc "x" $group $iface $oiflist_($group)
	set iif [$node_ lookup-iface "x" $group]
	if { $iface >= 0 } {
		set oif [$node_ iif2oif $iface]
		set rpfiif [$node_ from-node-iface $RP_($group)]
		if { $iface == $rpfiif } {
			# forward direction: disable oif to RP
			$rep disable [$node_ iif2oif $rpfiif]
		} else {
			# reverse direction: disable where it came from
			$rep disable $oif
			if { $node_ != $RP_($group) } {
				$rep insert [$node_ iif2oif $rpfiif]
			}
		}
	}
	$node_ change-iface "x" $group $iif $iface
	return 1 ;#classify packet again
}

# handle-cache-miss
# Creates a (*, G) entry for a group.  
BST instproc handle-cache-miss { srcID group iface } {
	$self instvar node_  ns_ oiflist_
	BST instvar RP_
	
	if { [$node_ getReps "x" $group] != "" } {
		error
	}

	
#      	$self dbg \
#  	    "handle-cache-miss, src: $srcID, group: $group, iface: $iface"
#     	$self dbg \
#  	    "********* miss: adding <x, $group, $iface, $oiflist_($group)>"

	# Check if the node is on a LAN.  If so we should NOT resume
	# if:
	#	1) The node is not the next upstream router, and
	#	2) The incoming interface is to a LanNode, and
	#	3) The node is not a source.

 	if {$iface != -1} {
		# if the node is not a source (3)
 		set neighbors [$node_ set neighbor_]
		if [info exists neighbors] {
			for {set i 0} {$i < [llength $neighbors]} {incr i} {
				set neighbor [lindex $neighbors $i]
				set nbr [$node_ rpf-nbr $RP_($group)]
				if {[$neighbor is-lan?] && \
					[$nbr info class] != "LanNode"} {
					# The node is directly
					# connected to RP --or at
					# least not via LanNode
					$neighbor instvar up_
					set up [$neighbor set up_($group)]
					if {$node_ != $up} {
						# If not the upstream
						# router (1) 
						if [$self link2lan? $neighbor \
							$iface] {
							# The interface is to 
							# the LAN
							return 0
						}
					}
				}
			}
		}
 	}
    
	$node_ add-mfc "x" $group $iface $oiflist_($group)

	if { $iface > 0 } {
		#disable reverse iface
		set rep [$node_ getReps "x" $group]
		$rep disable [$node_ iif2oif $iface]
	}
	return 1 ;# classify the packet again.
}

BST instproc drop { replicator src dst iface} {
	$self instvar node_ ns_
	BST instvar RP_
	
	# No downstream listeners? Just drop the packet. No purning is
	# necessary  
# 	$self dbg "drops src: $src, dst: $dst, replicator: [$replicator set srcID_]"
	
	if {$iface >= 0} {
		# so, this packet came from outside of the node.
		# Since PIM works based on explicit join mechanism,
		# the only action is to drop unwanted packets.
# 		$self dbg "drops the unwanted packet"
	}
}

BST instproc recv-prune { from src group iface} {
	$self instvar node_ ns_ oiflist_ 
	BST instvar RP_ 
# 	$self dbg "received a prune from: $from, src: $src, grp: $group, if: $iface"

	set rep [$node_ getReps "x" $group]
	if {$rep != ""} {
		set oif [$node_ iif2oif $iface]
		set idx [lsearch $oiflist_($group) $oif]
		if { $idx >= 0 } {
			set oiflist_($group) [lreplace $oiflist_($group) $idx $idx]
			$rep disable $oif
			set rpfiif [$node_ from-node-iface $RP_($group)]
			if { $rpfiif != "?" } {
				set rpfoif [$node_ iif2oif $rpfiif]
			} else {
				set rpfoif ""
			}
			if { $oiflist_($group) == $rpfoif && ![$node_ check-local $group] } {
				# propagate
				$node_ delete-mark m2
				$self send-ctrl "prune" $RP_($group) $group
			}
		}
	}
}

BST instproc recv-graft { from to group iface } {
	$self instvar node_ ns_ oiflist_
	BST instvar RP_
	
#  	$self dbg "received a graft from: $from, to: $to, if: $iface"
	set oif [$node_ iif2oif $iface]
	set rpfiif [$node_ from-node-iface $RP_($group)]
	if { $rpfiif != "?" } {
		set rpfoif [$node_ iif2oif $rpfiif]
	} else {
		set rpfoif ""
	}

	if { $oiflist_($group) == $rpfoif && ![$node_ check-local $group] } {
		# propagate
		$node_ add-mark m2 red circle
		$self send-ctrl "graft" $RP_($group) $group
	}
	if { [lsearch $oiflist_($group) $oif] < 0 } {
		lappend oiflist_($group) $oif
		if { [$node_ lookup-iface "x" $group] != $iface } {
			set rep [$node_ getReps "x" $group]
			if { $rep != "" } {
				$rep insert $oif
			}
		}
	}
}

#
# send a graft/prune for src/group up the RPF tree towards dst
#
BST instproc send-ctrl { which dst group } {
        $self instvar mctrl_ ns_ node_
	
	if {$node_ != $dst} {
		set nbr [$node_ rpf-nbr $dst]
		if [$nbr is-lan?] {
			# we're requested to send via a lan
			$nbr instvar receivers_
			# send graft/prune only if there's no other receiver.
			if { [info exists receivers_($group)] && \
				 $receivers_($group) > 0 } return

			set nbr [$nbr rpf-nbr $dst]
#  			$self dbg "BST::send-ctrl The first hop router to LAN is node [$nbr id]"
		}

#  		$self dbg "BST::send-ctrl $ns_ simplex-connect \[\[\[$nbr getArbiter\] getType \[$self info class\]\] set mctrl_\]"

		$ns_ simplex-connect $mctrl_ \
				[[[$nbr getArbiter] getType [$self info class]] set mctrl_]
		if { $which == "prune" } {
			$mctrl_ set fid_ 2
		} else {
			$mctrl_ set fid_ 3
		}
# 		$self dbg "BST::send-ctrl $mctrl_ ([$mctrl_ info class]) send $which [$node_ id] $dst $group"
		$mctrl_ send $which [$node_ id] $dst $group
	}
}

################ Helpers

BST instproc dbg arg {
	$self instvar ns_ node_
	puts [format "At %.4f : node [$node_ id] $arg" [$ns_ now]]
}


					
# designate-ump-router
# Designates a router on a LAN in order to avoid transmission of
# duplicate and redundant messages.  The node with the highest ID is
# chosen as the designated router.
LanNode instproc designate-ump-router {group dst} {
	$self instvar nodelist_
	$self instvar up_

	set nbr [$self rpf-nbr $dst]
	set up_($group) $nbr
	return
}


# Returns the next hop router to a node
BST instproc next-hop-router {node group} {
	BST instvar RP_

	set nbr [$node rpf-nbr $RP_($group)]
	if [$nbr is-lan?] {
		set nbr [$nbr rpf-nbr $RP_($group)]
	}
	return $nbr
}

# Checks if an mcast group is BST 
BST instproc is-group-bidir? {group} {
	BST instvar RP_

	foreach grp [array names RP_] {
		if {$grp == $group} {
			return 1
		}
	}
	return 0
}

# Finds out which Connector or Vlink corresponds to (link)
BST instproc match-oif {group link} {
	$self instvar oiflist_

 	set oiflist $oiflist_($group)
  	if {$oiflist != ""} {
		foreach oif $oiflist {
			set oiflink [$oif set link_]
			if {$oiflink == $link} {
				return $oiflink
			}
		}
  	}
	return
}

# Finds the outgoing link to the next node (dst)
BST instproc find-oif {dst group} {
	$self instvar node_ ns_

	if {$node_ != $dst} {
		set ns [$self set ns_]
		$ns instvar link_
		set link [$ns set link_([$node_ id]:[$dst id])]
		# Now find which Connector or VLink corresponds to this link
		return [$self match-oif $group $link]
	} else {
		return ""
	}
}

# Finds if the interface (iface) is toward the LAN represented by (neighbor) 
# or not.
BST instproc link2lan? {neighbor iface} {
	$self instvar node_ ns_

	set link1 [[[$node_ iif2oif $iface] set link_] set iif_]
	set link2 [[$ns_ link $node_ $neighbor] set iif_]
    	if {$link1 == $link2} {
		return 1
	} else {
		return 0
	}
}

####################
Class Classifier/Multicast/Replicator/BST -superclass Classifier/Multicast/BST

#
# This method called when a new multicast group/source pair
# is seen by the underlying classifier/mcast object.
# We install a hash for the pair mapping it to a slot
# number in the classifier table and point the slot
# at a replicator object that sends each packet along
# the RPF tree.
#
Classifier/Multicast/BST instproc new-group { src group iface code} {
	$self instvar node_
	$node_ new-group $src $group $iface $code
}

Classifier/Multicast/BST instproc no-slot slot {
	# NOTHING
}

Classifier/Multicast/Replicator/BST instproc init args {
	$self next
	$self instvar nrep_
	set nrep_ 0
}

Classifier/Multicast/Replicator/BST instproc add-rep { rep src group iif } {
	$self instvar nrep_
	$self set-hash $src $group $nrep_ $iif
	$self install $nrep_ $rep
	incr nrep_
}

####################
# match-BST-iif
# Performs the RPF 
Classifier/Multicast/Replicator/BST instproc match-BST-iif {iface group} {
	$self instvar node_

	list retval_
	set agents [$node_ set agents_]
	for {set i 0} {$i < [llength $agents]} {incr i} {
		# Check if you can find a BST agent
		set agent [lindex $agents $i]
		$agent instvar proto_
		if [info exists proto_] {
			set protocol [$agent set proto_]
			if {[$protocol info class] == "BST"} {
				# See if the group is a BST group
				BST instvar RP_
				$protocol instvar oiflist_
				set bidir [$protocol is-group-bidir? $group]
				if {$bidir == 1} {
					if {$node_ == $RP_($group)} {
						return 1
					}

					# Find the incoming interface
					# from RP.
					set iif [$node_ from-node-iface \
						     $RP_($group)]
					if {$iif == $iface} {
						return 1
					} else {
						return 0
					}
				}
			}
		}
	}
	return -1
}

# Classifier/Multicast/Replicator upstream-link
# Finds the link from the next hop upstream router to the current
# node.  This function is called from C++ to provide the classifier
# with appropriate information to set the UMP option.
Classifier/Multicast/Replicator/BST instproc upstream-link {group} {
	$self instvar node_

	list retval_
	set agents [$node_ set agents_]
	for {set i 0} {$i < [llength $agents]} {incr i} {
		# Check if you can find a BST agent
		set agent [lindex $agents $i]
		$agent instvar proto_
		if [info exists proto_] {
			set protocol [$agent set proto_]
			if {[$protocol info class] == "BST"} {
				# See if the group is a BST group
				BST instvar RP_
				$protocol instvar oiflist_
				set bidir [$protocol is-group-bidir? $group]
				if {$bidir == 1} {
					# Get the next node, REAL or VIRTUAL!
					set nbr [$node_ rpf-nbr $RP_($group)]

					# Find the outgoing link to the next
					# node 
					set oif [$protocol find-oif $nbr \
						     $group]

					if {$oif == ""} {
						set oif "self"
					} 
					lappend retval_ $oif

					# Now attach the next node's ID
					# If the neighbor is a virtual
					# name, we need to find the
					# designated router on the
					# LAN. Currently, the
					# designated router is the one
					# which is the closest to the RP.
					if [$nbr is-lan?] {
					    set nbr [$nbr rpf-nbr $RP_($group)]
					}
					lappend retval_ [$nbr id]
					return $retval_

				}
			}
		}
	}
# 	$self dbg "There is no agent for this node!"
	return {}
}

# check-rpf-link 
# Finds out the RPF link for a node
Classifier/Multicast/Replicator/BST instproc check-rpf-link {node group} {
	$self instvar node_

	set agents [$node_ set agents_]
	for {set i 0} {$i < [llength $agents]} {incr i} {
		# Check if you can find a BST agent
		set agent [lindex $agents $i]
		$agent instvar proto_
		if [info exists proto_] {
			set protocol [$agent set proto_]
			set classInfo [$protocol info class]
			if {$classInfo == "BST"} {
				# See if the group is a BST group
				BST instvar RP_
				set rpfiif [$node_ from-node-iface \
						$RP_($group)]
				return $rpfiif
			}
		}
	}
	return -1
}