File: ns-address.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 (360 lines) | stat: -rw-r--r-- 11,382 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
#
# Copyright (c) 1996 Regents of the University of California.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
# 	This product includes software developed by the MASH Research
# 	Group at the University of California Berkeley.
# 4. Neither the name of the University nor of the Research Group may be
#    used to endorse or promote products derived from this software without
#    specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

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

# API's for allocating bits to ns addressing structure
# These routines should be used for setting bits for portid (agent), for nodeid 
# (flat or hierarchical).
# DEFADDRSIZE_ and MAXADDRSIZE_ defined in ns-default.tcl


# Name :        set-address-format ()
# Options :     default, hierarchical (default) and hierarchical
#		(with specific allocations) 
# Synopsis :    set-address-format <option> <additional info, if required
# 		by option> 
#
# Option:1      def (default settings)
# Synopsis :    set-address-format def
# Currently 31 bit address space is the default feature in ns , the uppermost
# bit is avoided due to Tcl/C++ integer issues
#               1 bit for multicast
#               30 bits for node id

# Option :2     hierarchical (default)
# Synopsis :    set-address-format hierarchical
# Description:  This allows setting of hierarchical address as follows:
#                   * Sets mcast bit (if specified)
#                   * Sets default hierchical levels with:
#                      * 3 levels of hierarchy and
#                      * (9 11 11) by default 
#                      * (8 11 11) if mcast is enabled.
#
# Option 4:     hierarchical (specified)
# Synopsis:     set-address-format hierarchical <#n hierarchy levels> 
#		<#bits for level1> <#bits for level 2> ....
#		<#bits for nth level>
#               e.g  
#                   set-address-format hierarchical 4 2 2 2 10
# Description:  This allows setting of hierarchical address as specified for 
#		each level.
#                 * Sets mcast bit (if specified)
#                 * Sets hierchical levels with bits specified for each level.

# Errors: 	* if # of bits specified less than 0.
#               * if bit positions clash (contiguous # of requested free bits 
#		  not found)
#               * if total # of bits exceed MAXADDRSIZE_
#               * if # hierarchy levels donot match with #bits specified (for
#                 each level). 

Class AllocAddrBits

Simulator proc address-format {} {
	return [Simulator set AddressFormat_]
}

Simulator proc default-addr? {} {
	if { [Simulator set AddressFormat_] == "DEF" } {
		return 1
	} else { 
		return 0
	}
}

Simulator proc hier-addr? {} {
	if { [Simulator set AddressFormat_] == "HIER" } {
		return 1
	} else {
		return 0
	}
}

Simulator instproc set-address-format {opt args} {
	set len [llength $args]
	if {[string compare $opt "def"] == 0} {
		$self set-address 31
		set mcastshift [AddrParams McastShift]
		Simulator set McastAddr_ [expr 1 << $mcastshift]
		mrtObject expandaddr
		Simulator set AddressFormat_ DEF
	} elseif {[string compare $opt "expanded"] == 0} {
		puts "set-address-format expanded is obsoleted by 31-bit addressing."
	} elseif {[string compare $opt "hierarchical"] == 0 && $len == 0} {
		if [$self multicast?] {
			$self set-hieraddress 3 8 11 11
		} else {
			$self set-hieraddress 3 9 11 11
		}
	} elseif {[string compare $opt "hierarchical"] == 0 && $len > 0} {
		eval $self set-hieraddress [lindex $args 0] \
				[lrange $args 1 [expr $len - 1]]
	} else {
		error "ns-address.tcl:set-address-format: Unknown address format $opt"
	}
}

Simulator instproc set-hieraddress {hlevel args} {
	set a [$self get-AllocAddrBits "new"]
	$a set size_ [AllocAddrBits set MAXADDRSIZE_]
	# By default, setting hierarchical addressing turns on hier rtg
	Simulator set AddressFormat_ HIER
	Node enable-module "Hier"
	if [$self multicast?] {
		$a set-mcastbits 1
	}
	eval $a set-idbits $hlevel $args
}

# Sets address for nodeid and port fields
# The order of setting bits for different fields does matter and should 
# be as follows:
#   mcast
#   idbits
#   portbits
# this is true for both set-address and set-hieraddress
Simulator instproc set-address {node} {
	set a [$self get-AllocAddrBits "new"]
	$a set size_ [AllocAddrBits set DEFADDRSIZE_]
	if {[expr $node] > [$a set size_]} {
		$a set size_ [AllocAddrBits set MAXADDRSIZE_]
	}
		
	# One bit is set aside for mcast as default :: this waste of 1 bit 
	# may be avoided, if mcast option is enabled before the initialization
	# of Simulator.
	$a set-mcastbits 1
	set lastbit [expr $node - [$a set mcastsize_]]
	$a set-idbits 1 $lastbit
}

Simulator instproc get-AllocAddrBits {prog} {
	$self instvar allocAddr_
	if ![info exists allocAddr_] {
		set allocAddr_ [new AllocAddrBits]
	} elseif ![string compare $prog "new"] {
		set allocAddr_ [new AllocAddrBits]
	}
	return $allocAddr_
}

Simulator instproc expand-port-field-bits nbits {
	# The function is obsolete, given that ports are now 31 bits wide
	puts "Warning: Simulator::expand-port-field-bits is obsolete.  Ports are 31 bits wide"
	return
}

Simulator instproc expand-address {} {
	puts "Warning: Simulator::expand-address is obsolete.  The node address is 31 bits wide"
	return
}


AllocAddrBits instproc init {} {
	eval $self next
	$self instvar size_ portsize_ idsize_ mcastsize_
	set size_ 0
	set portsize_ 0
	set idsize_ 0
	set mcastsize_ 0
}

AllocAddrBits instproc get-AllocAddr {} {
	$self instvar addrStruct_
	if ![info exists addrStruct_] {
		set addrStruct_ [new AllocAddr]
	}
	return $addrStruct_
}

AllocAddrBits instproc get-Address {} {
	$self instvar address_
	if ![info exists address_] {
		set address_ [new Address]
	}
	return $address_
}

AllocAddrBits instproc chksize {bit_num prog} {
	$self instvar size_ portsize_ idsize_ mcastsize_  
	if {$bit_num <= 0 } {
		error "$prog : \# bits less than 1"
	}
	set totsize [expr $portsize_ + $idsize_ + $mcastsize_]
	if {$totsize > [AllocAddrBits set MAXADDRSIZE_]} {
		error "$prog : Total \# bits $totsize exceed MAXADDRSIZE"
	}
	if { $size_ < [AllocAddrBits set MAXADDRSIZE_]} {
		if {$totsize > [AllocAddrBits set DEFADDRSIZE_]} {
			set size_ [AllocAddrBits set MAXADDRSIZE_]
			return 1
		} 
	}
	return 0

}

AllocAddrBits instproc set-portbits {bit_num} {
	# The function is obsolete, given that ports are now 31 bits wide
	puts "Warning: AllocAddrBits::set-portbits is obsolete.  Ports are 31 bits wide."
	return
}

AllocAddrBits instproc expand-portbits nbits {
	# The function is obsolete, given that ports are now 31 bits wide
	puts "Warning: AllocAddrBits::expand-portbits is obsolete.  Ports are 31 bits wide."
	return
}

AllocAddrBits instproc set-mcastbits {bit_num} {
	$self instvar size_ mcastsize_
	if {$bit_num != 1} {
		error "setmcast : mcastbit > 1"
	}
	set mcastsize_ $bit_num

	#chk to ensure there;s no change in size
	if [$self chksize mcastsize_ "setmcast"] {
		# assert {$chk == 0} --> assert doesn't seem to work
		error "set-mcastbits: size_ has been changed."
	}
	set a [$self get-AllocAddr] 
	set v [$a setbit $bit_num $size_]
	AddrParams McastMask [lindex $v 0]
	AddrParams McastShift [lindex $v 1]
	### TESTING
	# set mask [lindex $v 0]
	# set shift [lindex $v 1]
	# puts "Mcastshift = $shift \n McastMask = $mask\n"

	set ad [$self get-Address]
	$ad mcastbits-are [AddrParams McastShift] [AddrParams McastMask]
}

AllocAddrBits instproc set-idbits {nlevel args} {
	$self instvar size_ portsize_ idsize_ hlevel_ hbits_
	if {$nlevel != [llength $args]} {
		error "setid: hlevel < 1 OR nlevel and \# args donot match"
	}
	set a [$self get-AllocAddr] 
	set old 0
	set idsize_ 0
	set nodebits 0
	# Set two global variables
	AddrParams hlevel $nlevel
	set hlevel_ $nlevel
	for {set i 0} {$i < $nlevel} {incr i} {
		set bpl($i) [lindex $args $i]
		set idsize_ [expr $idsize_ + $bpl($i)]
		
		# check to ensure there's no change in size
		set chk [$self chksize $bpl($i) "setid"]
		# assert {$chk ==0}
		if {$chk > 0} {
			error "set-idbits: size_ has been changed."
		}
		set v [$a setbit $bpl($i) $size_]
		AddrParams NodeMask [expr $i+1] [lindex $v 0]
		set m([expr $i+1]) [lindex $v 0]
		AddrParams NodeShift [expr $i+1] [lindex $v 1]
		set s([expr $i+1]) [lindex $v 1]
		lappend hbits_ $bpl($i)
		
	}
	AddrParams nodebits $idsize_
	set ad [$self get-Address]
	eval $ad idsbits-are [array get s]
	eval $ad idmbits-are [array get m]
	eval $ad bpl-are $hbits_
	### TESTING
	# set mask [lindex $v 0]
	# set shift [lindex $v 1]
	# puts "Nodeshift = $shift \n NodeMask = $mask\n"
}


# Create an integer address from addr string 
AddrParams proc addr2id addrstr {
	if [Simulator hier-addr?] {
		set addressObj [[[Simulator instance] get-AllocAddrBits ""] \
				get-Address]
		return [$addressObj str2addr $addrstr]
	} else {
		return [expr $addrstr & [AddrParams NodeMask 1] << \
				[AddrParams NodeShift 1]]
	}
}

# Returns address string from an integer address: reverse of set-hieraddr.
AddrParams proc id2addr addr {
	for {set i 1} {$i <= [AddrParams hlevel]} {incr i} {
		set a [expr ($addr >> [AddrParams NodeShift $i]) & \
				[AddrParams NodeMask $i]]
		lappend str $a
	}
	return $str
}

# Splitting up address string
AddrParams proc split-addrstr addrstr {
	return [split $addrstr .]
}

# Returns number of elements at a given hierarchical level, that is visible to 
# a node.
AddrParams proc elements-in-level? {nodeaddr level} {
	AddrParams instvar domain_num_ cluster_num_ nodes_num_ 
	set L [AddrParams split-addrstr $nodeaddr]
	set level [expr $level + 1]
	#
	# if no topology info found for last level, set default values
	#
	
	# For now, assuming only 3 levels of hierarchy 
	if { $level == 1} {
		return $domain_num_
	}
	if { $level == 2} {
		return [lindex $cluster_num_ [lindex $L 0]]
	}
	if { $level == 3} {
		set C 0
		set index 0
		while {$C < [lindex $L 0]} {
			set index [expr $index + [lindex $cluster_num_ $C]]
			incr C
		}
		return [lindex $nodes_num_ [expr $index + [lindex $L 1]]]
	}
}