PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/ns-2.34/tcl/lib/ns-address.tcl

http://uu-cope.googlecode.com/
TCL | 390 lines | 215 code | 35 blank | 140 comment | 43 complexity | 5c36f7b848681a21090fb511ab1eb6e4 MD5 | raw file
Possible License(s): LGPL-2.0
  1. #
  2. # Copyright (c) 1996 Regents of the University of California.
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. # 1. Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # 3. All advertising materials mentioning features or use of this software
  14. # must display the following acknowledgement:
  15. # This product includes software developed by the MASH Research
  16. # Group at the University of California Berkeley.
  17. # 4. Neither the name of the University nor of the Research Group may be
  18. # used to endorse or promote products derived from this software without
  19. # specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. # SUCH DAMAGE.
  32. #
  33. #####################################################################################
  34. # API's for allocating bits to ns addressing structure
  35. # These routines should be used for setting bits for portid (agent), for nodeid
  36. # (flat or hierarchical).
  37. # DEFADDRSIZE_ and MAXADDRSIZE_ defined in ns-default.tcl
  38. # Name : set-address-format ()
  39. # Options : default, expanded, hierarchical (default) and hierarchical
  40. # (with specific allocations)
  41. # Synopsis : set-address-format <option> <additional info, if required
  42. # by option>
  43. #
  44. # Option:1 def (default settings)
  45. # Synopsis : set-address-format def
  46. # Currently 32 bit address space is the default feature in ns
  47. # (OLD) Description: This allows default settings in the following manner:
  48. # * 8bits portid
  49. # * 1 bit for mcast and 7 bits nodeid (mcast should be enabled
  50. # before Simulator is inited (new Simulator is called)--> leads
  51. # to unnecessary wasting of 1 bit even if mcast is not set.
  52. # [This is obsoleted by default 32 bit address space]
  53. # Option 2: expanded
  54. # Synopsis : set-address-format expanded
  55. # Description: This allows to expand the address space from default size 16
  56. # to 32 with bit allocated to the field in the foll manner:
  57. # * 8bits portid
  58. # * 1 bit for mcast and 21 bits nodeid (and same comments
  59. # regarding setting mcast as for default option above)
  60. #
  61. # Option :3 hierarchical (default)
  62. # Synopsis : set-address-format hierarchical
  63. # Description: This allows setting of hierarchical address as follows:
  64. # * Sets 8bits portid
  65. # * Sets mcast bit (if specified)
  66. # * Sets default hierchical levels with:
  67. # * 3 levels of hierarchy and
  68. # * (10 11 11) by default
  69. # * (9 11 11) if mcast is enabled.
  70. #
  71. # Option 4: hierarchical (specified)
  72. # Synopsis: set-address-format hierarchical <#n hierarchy levels>
  73. # <#bits for level1> <#bits for level 2> ....
  74. # <#bits for nth level>
  75. # e.g
  76. # set-address-format hierarchical 4 2 2 2 10
  77. # Description: This allows setting of hierarchical address as specified for
  78. # each level.
  79. # * Sets 8bits portid
  80. # * Sets mcast bit (if specified)
  81. # * Sets hierchical levels with bits specified for each level.
  82. # Name: expand-port-field-bits ()
  83. # Synopsis: expand-port-field-bits <#bits for portid>
  84. # Status: Obsolete. It is no longer needed in the 32-bit addressing
  85. # scheme
  86. # Description : This should be used incase of need to extend portid.
  87. # This commnad may be used in conjuction with
  88. # set-address-format command explained above.
  89. # expand-port-field-bits checks and raises error in the foll.
  90. # cases
  91. # * if the requested portsize cannot be accomodated (i.e
  92. # if sufficient num.of free bits are not available)
  93. # * if requested portsize is less than equal to existing
  94. # portsize, and incase of no errors sets port field with
  95. # bits as specified.
  96. #
  97. # Errors: * if # of bits specified less than 0.
  98. # * if bit positions clash (contiguous # of requested free bits
  99. # not found)
  100. # * if total # of bits exceed MAXADDRSIZE_
  101. # * if expand-port-field-bits is attempted with portbits less or
  102. # equal to the existing portsize.
  103. # * if # hierarchy levels donot match with #bits specified (for
  104. # each level).
  105. Class AllocAddrBits
  106. Simulator proc address-format {} {
  107. return [Simulator set AddressFormat_]
  108. }
  109. Simulator proc default-addr? {} {
  110. if { [Simulator set AddressFormat_] == "DEF" } {
  111. return 1
  112. } else {
  113. return 0
  114. }
  115. }
  116. Simulator proc hier-addr? {} {
  117. if { [Simulator set AddressFormat_] == "HIER" } {
  118. return 1
  119. } else {
  120. return 0
  121. }
  122. }
  123. Simulator instproc set-address-format {opt args} {
  124. set len [llength $args]
  125. if {[string compare $opt "def"] == 0} {
  126. $self set-address 32
  127. set mcastshift [AddrParams McastShift]
  128. Simulator set McastAddr_ [expr 1 << $mcastshift]
  129. mrtObject expandaddr
  130. Simulator set AddressFormat_ DEF
  131. } elseif {[string compare $opt "expanded"] == 0} {
  132. puts "set-address-format expanded is obsoleted by 32-bit addressing."
  133. } elseif {[string compare $opt "hierarchical"] == 0 && $len == 0} {
  134. if [$self multicast?] {
  135. $self set-hieraddress 3 9 11 11
  136. } else {
  137. $self set-hieraddress 3 10 11 11
  138. }
  139. } elseif {[string compare $opt "hierarchical"] == 0 && $len > 0} {
  140. eval $self set-hieraddress [lindex $args 0] \
  141. [lrange $args 1 [expr $len - 1]]
  142. } else {
  143. error "ns-address.tcl:set-address-format: Unknown address format $opt"
  144. }
  145. }
  146. Simulator instproc set-hieraddress {hlevel args} {
  147. set a [$self get-AllocAddrBits "new"]
  148. $a set size_ [AllocAddrBits set MAXADDRSIZE_]
  149. # By default, setting hierarchical addressing turns on hier rtg
  150. Simulator set AddressFormat_ HIER
  151. Node enable-module "Hier"
  152. if [$self multicast?] {
  153. $a set-mcastbits 1
  154. }
  155. eval $a set-idbits $hlevel $args
  156. }
  157. # Sets address for nodeid and port fields
  158. # The order of setting bits for different fields does matter and should
  159. # be as follows:
  160. # mcast
  161. # idbits
  162. # portbits
  163. # this is true for both set-address and set-hieraddress
  164. Simulator instproc set-address {node} {
  165. set a [$self get-AllocAddrBits "new"]
  166. $a set size_ [AllocAddrBits set DEFADDRSIZE_]
  167. if {[expr $node] > [$a set size_]} {
  168. $a set size_ [AllocAddrBits set MAXADDRSIZE_]
  169. }
  170. # One bit is set aside for mcast as default :: this waste of 1 bit
  171. # may be avoided, if mcast option is enabled before the initialization
  172. # of Simulator.
  173. $a set-mcastbits 1
  174. set lastbit [expr $node - [$a set mcastsize_]]
  175. $a set-idbits 1 $lastbit
  176. }
  177. Simulator instproc get-AllocAddrBits {prog} {
  178. $self instvar allocAddr_
  179. if ![info exists allocAddr_] {
  180. set allocAddr_ [new AllocAddrBits]
  181. } elseif ![string compare $prog "new"] {
  182. set allocAddr_ [new AllocAddrBits]
  183. }
  184. return $allocAddr_
  185. }
  186. Simulator instproc expand-port-field-bits nbits {
  187. # The function is obsolete, given that ports are now 32 bits wide
  188. puts "Warning: Simulator::expand-port-field-bits is obsolete. Ports are 32 bits wide"
  189. return
  190. }
  191. Simulator instproc expand-address {} {
  192. puts "Warning: Simulator::expand-address is obsolete. The node address is 32 bits wide"
  193. return
  194. }
  195. AllocAddrBits instproc init {} {
  196. eval $self next
  197. $self instvar size_ portsize_ idsize_ mcastsize_
  198. set size_ 0
  199. set portsize_ 0
  200. set idsize_ 0
  201. set mcastsize_ 0
  202. }
  203. AllocAddrBits instproc get-AllocAddr {} {
  204. $self instvar addrStruct_
  205. if ![info exists addrStruct_] {
  206. set addrStruct_ [new AllocAddr]
  207. }
  208. return $addrStruct_
  209. }
  210. AllocAddrBits instproc get-Address {} {
  211. $self instvar address_
  212. if ![info exists address_] {
  213. set address_ [new Address]
  214. }
  215. return $address_
  216. }
  217. AllocAddrBits instproc chksize {bit_num prog} {
  218. $self instvar size_ portsize_ idsize_ mcastsize_
  219. if {$bit_num <= 0 } {
  220. error "$prog : \# bits less than 1"
  221. }
  222. set totsize [expr $portsize_ + $idsize_ + $mcastsize_]
  223. if {$totsize > [AllocAddrBits set MAXADDRSIZE_]} {
  224. error "$prog : Total \# bits exceed MAXADDRSIZE"
  225. }
  226. if { $size_ < [AllocAddrBits set MAXADDRSIZE_]} {
  227. if {$totsize > [AllocAddrBits set DEFADDRSIZE_]} {
  228. set size_ [AllocAddrBits set MAXADDRSIZE_]
  229. return 1
  230. }
  231. }
  232. return 0
  233. }
  234. AllocAddrBits instproc set-portbits {bit_num} {
  235. # The function is obsolete, given that ports are now 32 bits wide
  236. puts "Warning: AllocAddrBits::set-portbits is obsolete. Ports are 32 bits wide."
  237. return
  238. }
  239. AllocAddrBits instproc expand-portbits nbits {
  240. # The function is obsolete, given that ports are now 32 bits wide
  241. puts "Warning: AllocAddrBits::expand-portbits is obsolete. Ports are 32 bits wide."
  242. return
  243. }
  244. AllocAddrBits instproc set-mcastbits {bit_num} {
  245. $self instvar size_ mcastsize_
  246. if {$bit_num != 1} {
  247. error "setmcast : mcastbit > 1"
  248. }
  249. set mcastsize_ $bit_num
  250. #chk to ensure there;s no change in size
  251. if [$self chksize mcastsize_ "setmcast"] {
  252. # assert {$chk == 0} --> assert doesn't seem to work
  253. error "set-mcastbits: size_ has been changed."
  254. }
  255. set a [$self get-AllocAddr]
  256. set v [$a setbit $bit_num $size_]
  257. AddrParams McastMask [lindex $v 0]
  258. AddrParams McastShift [lindex $v 1]
  259. ### TESTING
  260. # set mask [lindex $v 0]
  261. # set shift [lindex $v 1]
  262. # puts "Mcastshift = $shift \n McastMask = $mask\n"
  263. set ad [$self get-Address]
  264. $ad mcastbits-are [AddrParams McastShift] [AddrParams McastMask]
  265. }
  266. AllocAddrBits instproc set-idbits {nlevel args} {
  267. $self instvar size_ portsize_ idsize_ hlevel_ hbits_
  268. if {$nlevel != [llength $args]} {
  269. error "setid: hlevel < 1 OR nlevel and \# args donot match"
  270. }
  271. set a [$self get-AllocAddr]
  272. set old 0
  273. set idsize_ 0
  274. set nodebits 0
  275. # Set two global variables
  276. AddrParams hlevel $nlevel
  277. set hlevel_ $nlevel
  278. for {set i 0} {$i < $nlevel} {incr i} {
  279. set bpl($i) [lindex $args $i]
  280. set idsize_ [expr $idsize_ + $bpl($i)]
  281. # check to ensure there's no change in size
  282. set chk [$self chksize $bpl($i) "setid"]
  283. # assert {$chk ==0}
  284. if {$chk > 0} {
  285. error "set-idbits: size_ has been changed."
  286. }
  287. set v [$a setbit $bpl($i) $size_]
  288. AddrParams NodeMask [expr $i+1] [lindex $v 0]
  289. set m([expr $i+1]) [lindex $v 0]
  290. AddrParams NodeShift [expr $i+1] [lindex $v 1]
  291. set s([expr $i+1]) [lindex $v 1]
  292. lappend hbits_ $bpl($i)
  293. }
  294. AddrParams nodebits $idsize_
  295. set ad [$self get-Address]
  296. eval $ad idsbits-are [array get s]
  297. eval $ad idmbits-are [array get m]
  298. eval $ad bpl-are $hbits_
  299. ### TESTING
  300. # set mask [lindex $v 0]
  301. # set shift [lindex $v 1]
  302. # puts "Nodeshift = $shift \n NodeMask = $mask\n"
  303. }
  304. # Create an integer address from addr string
  305. AddrParams proc addr2id addrstr {
  306. if [Simulator hier-addr?] {
  307. set addressObj [[[Simulator instance] get-AllocAddrBits ""] \
  308. get-Address]
  309. return [$addressObj str2addr $addrstr]
  310. } else {
  311. return [expr $addrstr & [AddrParams NodeMask 1] << \
  312. [AddrParams NodeShift 1]]
  313. }
  314. }
  315. # Returns address string from an integer address: reverse of set-hieraddr.
  316. AddrParams proc id2addr addr {
  317. for {set i 1} {$i <= [AddrParams hlevel]} {incr i} {
  318. set a [expr ($addr >> [AddrParams NodeShift $i]) & \
  319. [AddrParams NodeMask $i]]
  320. lappend str $a
  321. }
  322. return $str
  323. }
  324. # Splitting up address string
  325. AddrParams proc split-addrstr addrstr {
  326. return [split $addrstr .]
  327. }
  328. # Returns number of elements at a given hierarchical level, that is visible to
  329. # a node.
  330. AddrParams proc elements-in-level? {nodeaddr level} {
  331. AddrParams instvar domain_num_ cluster_num_ nodes_num_
  332. set L [AddrParams split-addrstr $nodeaddr]
  333. set level [expr $level + 1]
  334. #
  335. # if no topology info found for last level, set default values
  336. #
  337. # For now, assuming only 3 levels of hierarchy
  338. if { $level == 1} {
  339. return $domain_num_
  340. }
  341. if { $level == 2} {
  342. return [lindex $cluster_num_ [lindex $L 0]]
  343. }
  344. if { $level == 3} {
  345. set C 0
  346. set index 0
  347. while {$C < [lindex $L 0]} {
  348. set index [expr $index + [lindex $cluster_num_ $C]]
  349. incr C
  350. }
  351. return [lindex $nodes_num_ [expr $index + [lindex $L 1]]]
  352. }
  353. }