ns-rtmodule.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:10k
源码类别:

通讯编程

开发平台:

Visual C++

  1. # -*- Mode:tcl; tcl-indent-level:8; tab-width:8; indent-tabs-mode:t -*-
  2. #
  3. #  Time-stamp: <2000-09-13 18:22:04 haoboy>
  4. #  Copyright (c) 2000 by the University of Southern California
  5. #  All rights reserved.
  6. #  This program is free software; you can redistribute it and/or
  7. #  modify it under the terms of the GNU General Public License,
  8. #  version 2, as published by the Free Software Foundation.
  9. #
  10. #  This program is distributed in the hope that it will be useful,
  11. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #  GNU General Public License for more details.
  14. #
  15. #  You should have received a copy of the GNU General Public License along
  16. #  with this program; if not, write to the Free Software Foundation, Inc.,
  17. #  59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18. #
  19. #  The copyright of this module includes the following
  20. #  linking-with-specific-other-licenses addition:
  21. #
  22. #  In addition, as a special exception, the copyright holders of
  23. #  this module give you permission to combine (via static or
  24. #  dynamic linking) this module with free software programs or
  25. #  libraries that are released under the GNU LGPL and with code
  26. #  included in the standard release of ns-2 under the Apache 2.0
  27. #  license or under otherwise-compatible licenses with advertising
  28. #  requirements (or modified versions of such code, with unchanged
  29. #  license).  You may copy and distribute such a system following the
  30. #  terms of the GNU GPL for this module and the licenses of the
  31. #  other code concerned, provided that you include the source code of
  32. #  that other code when and as the GNU GPL requires distribution of
  33. #  source code.
  34. #
  35. #  Note that people who make modified versions of this module
  36. #  are not obligated to grant this special exception for their
  37. #  modified versions; it is their choice whether to do so.  The GNU
  38. #  General Public License gives permission to release a modified
  39. #  version without this exception; this exception also makes it
  40. #  possible to release a modified version which carries forward this
  41. #  exception.
  42. #  $Header: /cvsroot/nsnam/ns-2/tcl/lib/ns-rtmodule.tcl,v 1.13 2005/09/16 03:05:43 tomh Exp $
  43. #
  44. # OTcl interface definition for the base routing module. They provide 
  45. # linkage to Node, hence all derived classes should inherit these interfaces
  46. # and fill in their specific handling code.
  47. RtModule instproc register { node } {
  48. # Attach to node and register routing notifications
  49. $self attach-node $node
  50. $node route-notify $self
  51. $node port-notify $self
  52. }
  53. RtModule instproc init {} {
  54. $self next
  55. $self instvar classifier_ next_rtm_
  56. set next_rtm_ ""
  57. set classifier_ ""
  58. }
  59. # Only called when the default classifier of this module is REPLACED.
  60. RtModule instproc unregister {} {
  61. $self instvar classifier_
  62. delete $classifier_
  63. [$self node] unreg-route-notify $self
  64. [$self node] unreg-port-notify $self
  65. }
  66. RtModule instproc route-notify { module } {
  67. $self instvar next_rtm_
  68. if {$next_rtm_ == ""} {
  69. set next_rtm_ $module
  70. } else {
  71. $next_rtm_ route-notify $module
  72. }
  73. }
  74. RtModule instproc unreg-route-notify { module } {
  75. $self instvar next_rtm_
  76. if {$next_rtm_ != ""} {
  77. if {$next_rtm_ == $module} {
  78. set next_rtm_ [$next_rtm_ set next_rtm_]
  79. } else {
  80. $next_rtm_ unreg-route-notify $module
  81. }
  82. }
  83. }
  84. RtModule instproc add-route { dst target } {
  85. $self instvar next_rtm_
  86. [$self set classifier_] install $dst $target
  87. if {$next_rtm_ != ""} {
  88. $next_rtm_ add-route $dst $target
  89. }
  90. }
  91. RtModule instproc delete-route { dst nullagent} {
  92. $self instvar next_rtm_
  93. [$self set classifier_] install $dst $nullagent
  94. if {$next_rtm_ != ""} {
  95. $next_rtm_ delete-route $dst $nullagent
  96. }
  97. }
  98. RtModule instproc attach { agent port } {
  99. # Send target
  100. $agent target [[$self node] entry]
  101. # Recv target
  102. [[$self node] demux] install $port $agent
  103. }
  104. RtModule instproc detach { agent nullagent } {
  105. # Empty by default
  106. }
  107. RtModule instproc reset {} {
  108. # Empty by default
  109. }
  110. #
  111. # Base routing module
  112. #
  113. # Use standard naming although this is a pure OTcl class 
  114. #
  115. # In fact, all functionalities of Base are already implemented in RtModule.
  116. # We add this class only to provide a uniform interface to the RtModule 
  117. # creation process, where a single line [new RtModule/$name] is used.
  118. #
  119. # Defined in ~ns/rtmodule.{h,cc}
  120. RtModule/Base instproc register { node } {
  121. $self next $node
  122. $self instvar classifier_
  123. set classifier_ [new Classifier/Hash/Dest 32]
  124. $classifier_ set mask_ [AddrParams NodeMask 1]
  125. $classifier_ set shift_ [AddrParams NodeShift 1]
  126. # XXX Base should ALWAYS be the first module to be installed.
  127. $node install-entry $self $classifier_
  128. }
  129. #
  130. # Illustrates usage of insert-entry{}. However:
  131. # XXX Ugly hack: We should keep all variables local to this particular
  132. # module. However, given all existing multicast code assumes that 
  133. # switch_ and multiclassifier_ are inside Node, we have to make this 
  134. # compromise if we do not want to modify all existing code. :(
  135. #
  136. RtModule/Mcast instproc register { node } {
  137. $self next $node
  138. $self instvar classifier_
  139. # Keep old classifier so we can use RtModule::add-route{}.
  140. $self set classifier_ [$node entry]
  141. #if {[$classifier_ info class] != "Classifier/Virtual"} {
  142. # donot want to add-route if virtual classifier
  143. #$self attach-classifier $classifier_
  144. #}
  145. $node set switch_ [new Classifier/Addr]
  146. # Set up switch to route unicast packet to slot 0 and
  147. # multicast packets to slot 1
  148. [$node set switch_] set mask_ [AddrParams McastMask]
  149. [$node set switch_] set shift_ [AddrParams McastShift]
  150. # Create a classifier for multicast routing
  151. $node set multiclassifier_ [new Classifier/Multicast/Replicator]
  152. [$node set multiclassifier_] set node_ $node
  153. $node set mrtObject_ [new mrtObject $node]
  154. # Install existing classifier at slot 0, new classifier at slot 1
  155. $node insert-entry $self [$node set switch_] 0
  156. [$node set switch_] install 1 [$node set multiclassifier_]
  157. }
  158. #
  159. # Hierarchical routing module. 
  160. #
  161. RtModule/Hier instproc register { node } {
  162. $self next $node
  163. $self instvar classifier_
  164. set classifier_ [new Classifier/Hier]
  165. $node install-entry $self $classifier_
  166. }
  167. RtModule/Hier instproc delete-route args {
  168. eval [$self set classifier_] clear $args
  169. }
  170. Classifier/Hier instproc init {} {
  171. $self next
  172. for {set n 1} {$n <= [AddrParams hlevel]} {incr n} {
  173. set classifier [new Classifier/Addr]
  174. $classifier set mask_ [AddrParams NodeMask $n]
  175. $classifier set shift_ [AddrParams NodeShift $n]
  176. $self cmd add-classifier $n $classifier
  177. }
  178. }
  179. Classifier/Hier instproc destroy {} {
  180. for {set n 1} {$n <= [AddrParams hlevel]} {incr n} {
  181. delete [$self cmd classifier $n]
  182. }
  183. $self next
  184. }
  185. Classifier/Hier instproc clear args {
  186. set l [llength $args]
  187. [$self cmd classifier $l] clear [lindex $args [expr $l-1]] 
  188. }
  189. Classifier/Hier instproc install { dst target } {
  190. set al [AddrParams split-addrstr $dst]
  191. set l [llength $al]
  192. for {set i 1} {$i < $l} {incr i} {
  193. set d [lindex $al [expr $i-1]]
  194. [$self cmd classifier $i] install $d 
  195. [$self cmd classifier [expr $i+1]]
  196. }
  197. [$self cmd classifier $l] install [lindex $al [expr $l-1]] $target
  198. }
  199. #
  200. # Manual Routing Nodes:
  201. # like normal nodes, but with a hash classifier.
  202. #
  203. RtModule/Manual instproc register { node } {
  204. $self next $node
  205. $self instvar classifier_
  206. # Note the very small hash size---
  207. # you're supposed to resize it if you want more.
  208. set classifier_ [new Classifier/Hash/Dest 2]
  209. $classifier_ set mask_ [AddrParams NodeMask 1]
  210. $classifier_ set shift_ [AddrParams NodeShift 1]
  211. $node install-entry $self $classifier_
  212. }
  213. RtModule/Manual instproc add-route {dst_address target} {
  214. $self instvar classifier_ 
  215. set slot [$classifier_ installNext $target]
  216. if {$dst_address == "default"} {
  217. $classifier_ set default_ $slot
  218. } else {
  219. # don't encode the address here, set-hash bypasses that for us
  220. set encoded_dst_address [expr $dst_address << [AddrParams NodeShift 1]]
  221. $classifier_ set-hash auto 0 $encoded_dst_address 0 $slot
  222. }
  223. }
  224. RtModule/Manual instproc add-route-to-adj-node { args } {
  225. $self instvar classifier_ 
  226. set dst ""
  227. if {[lindex $args 0] == "-default"} {
  228. set dst default
  229. set args [lrange $args 1 end]
  230. }
  231. if {[llength $args] != 1} {
  232. error "ManualRtNode::add-route-to-adj-node [-default] node"
  233. }
  234. set target_node $args
  235. if {$dst == ""} {
  236. set dst [$target_node set address_]
  237. }
  238. set ns [Simulator instance]
  239. set link [$ns link [$self node] $target_node]
  240. set target [$link head]
  241. return [$self add-route $dst $target]
  242. }
  243. #
  244. # Source Routing Nodes.
  245. #
  246. RtModule/Source instproc register { node } {
  247.         $self next $node
  248.         $self instvar classifier_
  249.         # Keep old classifier so we can use RtModule::add-route{}.
  250.         $self set classifier_ [$node entry]
  251.         # Set up switch to route unicast packet to slot 0 and
  252.         # multicast packets to slot 1
  253.         #[$node set switch_] set mask_ [AddrParams McastMask]
  254.         #[$node set switch_] set shift_ [AddrParams McastShift]
  255.         # Create a classifier for multicast routing
  256.         $node set src_classifier_ [new Classifier/SR]
  257.         $node set src_agent_ [new Agent/SRAgent]
  258.         $node set switch_ [$node set src_classifier_]
  259.         # $node set multiclassifier_ [new Classifier/Multicast/Replicator]
  260.         # [$node set multiclassifier_] set node_ $node
  261. #       $node set mrtObject_ [new mrtObject $node]
  262.         # Install existing classifier at slot 0, new classifier at slot 1
  263.         $node insert-entry $self [$node set switch_] 1
  264.         [$node set switch_]  install 0 [$node set src_agent_]
  265.         $node attach [$node set src_agent_]
  266. #       $self set src_rt 1
  267. }
  268. #
  269. # Virtual Classifier Nodes:
  270. # like normal nodes, but with a virtual unicast classifier.
  271. #
  272. RtModule/VC instproc register { node } {
  273. # We do not do route-notify. Only port-notify will suffice.
  274. $self instvar classifier_
  275. $self attach-node $node
  276. $node port-notify $self
  277. set classifier_ [new Classifier/Virtual]
  278. $classifier_ set node_ $node
  279. $classifier_ set mask_ [AddrParams NodeMask 1]
  280. $classifier_ set shift_ [AddrParams NodeShift 1]
  281. $classifier_ nodeaddr [$node node-addr]
  282. $node install-entry $self $classifier_ 
  283. }
  284. RtModule/VC instproc add-route { dst target } {
  285. }
  286. Classifier/Virtual instproc find dst {
  287. $self instvar node_
  288. if {[$node_ id] == $dst} {
  289. return [$node_ set dmux_]
  290. } else {
  291. return [[[Simulator instance] link $node_ 
  292. [[Simulator instance] set Node_($dst)]] head]
  293. }
  294. }
  295. Classifier/Virtual instproc install {dst target} {
  296. }