Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
11 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. strie.h
  5. Abstract:
  6. This module contains support definitions for
  7. an S-trie data stucture, that forms the slow
  8. path in a fast IP route lookup implementation.
  9. Author:
  10. Chaitanya Kodeboyina (chaitk) 26-Nov-1997
  11. Revision History:
  12. --*/
  13. #ifndef STRIE_H_INCLUDED
  14. #define STRIE_H_INCLUDED
  15. #include "trie.h"
  16. //
  17. // Constants
  18. //
  19. // Direction in Iterator
  20. #define LCHILD 0
  21. #define RCHILD 1
  22. #define PARENT 2
  23. //
  24. // Structs
  25. //
  26. // A Node in an S-trie
  27. typedef struct _STrieNode STrieNode;
  28. struct _STrieNode
  29. {
  30. ULONG keyBits; // Value of addr bits to match in this node
  31. UINT numBits; // Actual num. of addr bits we are matching
  32. Dest *dest; // Destination starting the list of routes
  33. STrieNode *child[2]; // Pointer to the left and right child nodes
  34. };
  35. // An STrie Data Structure
  36. typedef struct _STrie STrie;
  37. struct _STrie
  38. {
  39. STrieNode *trieRoot; // Pointer to the root of the trie
  40. ULONG availMemory; // Memory available for allocation
  41. UINT numDests; // Total Num of dests in the trie
  42. UINT numRoutes; // Total Num of routes in the trie
  43. UINT numNodes; // Total Num of nodes in the trie
  44. };
  45. // An STrie Context Structure
  46. typedef struct _STrieCtxt STrieCtxt;
  47. struct _STrieCtxt
  48. {
  49. Route *pCRoute; // Pointer to current route in the trie
  50. ULONG currAddr; // Destination Addr of the current route
  51. ULONG currALen; // Length of the above destination addr
  52. };
  53. // Specific Route Macros
  54. #define NewRouteInSTrie(_pSTrie_, _pNewRoute_, _pOldRoute_) \
  55. { \
  56. AllocMemory1(_pNewRoute_, \
  57. sizeof(Route), \
  58. (_pSTrie_)->availMemory); \
  59. \
  60. NdisZeroMemory(_pNewRoute_, sizeof(Route)); \
  61. \
  62. DEST(_pNewRoute_) = DEST(_pOldRoute_); \
  63. MASK(_pNewRoute_) = MASK(_pOldRoute_); \
  64. LEN(_pNewRoute_) = LEN(_pOldRoute_); \
  65. METRIC(_pNewRoute_) = METRIC(_pOldRoute_); \
  66. \
  67. NEXT(_pNewRoute_) = NULL; \
  68. FLAGS(_pNewRoute_) = RTE_NEW; \
  69. \
  70. (_pSTrie_)->numRoutes++; \
  71. } \
  72. #define FreeRouteInSTrie(_pSTrie_, _pOldRoute_) \
  73. { \
  74. FreeMemory1(_pOldRoute_, \
  75. sizeof(Route), \
  76. (_pSTrie_)->availMemory); \
  77. \
  78. (_pSTrie_)->numRoutes--; \
  79. }
  80. // Specific Destination Macros
  81. #define NewDestInSTrie(_pSTrie_, _pRoute_, _pDest_) \
  82. { \
  83. AllocMemory1(_pDest_, \
  84. (sizeof(Dest) - sizeof(Route *)\
  85. + MaxEqualCostRoutes * \
  86. sizeof(Route *)), \
  87. (_pSTrie_)->availMemory); \
  88. \
  89. _pDest_->maxBestRoutes = MaxEqualCostRoutes;\
  90. _pDest_->numBestRoutes = 0; \
  91. \
  92. _pDest_->firstRoute = _pRoute_; \
  93. \
  94. (_pSTrie_)->numDests++; \
  95. }
  96. #define FreeDestInSTrie(_pSTrie_, _pOldDest_) \
  97. { \
  98. FreeMemory1(_pOldDest_, \
  99. (sizeof(Dest) - sizeof(Route *) \
  100. + MaxEqualCostRoutes * \
  101. sizeof(Route *)), \
  102. (_pSTrie_)->availMemory); \
  103. \
  104. (_pSTrie_)->numDests--; \
  105. }
  106. // Specific STrieNode Macros
  107. #define NewSTrieNode(_pSTrie_, _pSTrieNode_, _numBits_, _keyBits_, _pDest_) \
  108. { \
  109. AllocMemory1(_pSTrieNode_, \
  110. sizeof(STrieNode), \
  111. (_pSTrie_)->availMemory); \
  112. \
  113. _pSTrieNode_->numBits = _numBits_; \
  114. _pSTrieNode_->keyBits = _keyBits_; \
  115. \
  116. _pSTrieNode_->dest = _pDest_; \
  117. \
  118. _pSTrieNode_->child[0] = NULL; \
  119. _pSTrieNode_->child[1] = NULL; \
  120. \
  121. (_pSTrie_)->numNodes++; \
  122. }
  123. #define FreeSTrieNode(_pSTrie_, _pSTrieNode_) \
  124. { \
  125. FreeMemory1(_pSTrieNode_, \
  126. sizeof(STrieNode), \
  127. (_pSTrie_)->availMemory); \
  128. \
  129. (_pSTrie_)->numNodes--; \
  130. }
  131. // Other Route, Dest Macros
  132. #define CopyRoutePtr(_ppRoute_, _pRoute_) \
  133. if (_ppRoute_) \
  134. { \
  135. (*_ppRoute_) = _pRoute_; \
  136. } \
  137. #define CopyDestPtr(_ppDest_, _pDest_) \
  138. if (_ppDest_) \
  139. { \
  140. (*_ppDest_) = _pDest_; \
  141. } \
  142. // Prototypes
  143. UINT
  144. CALLCONV
  145. InitSTrie (IN STrie *pSTrie,
  146. IN ULONG maxMemory);
  147. UINT
  148. CALLCONV
  149. InsertIntoSTrie (IN STrie *pSTrie,
  150. IN Route *pIncRoute,
  151. IN ULONG matchFlags,
  152. OUT Route **ppInsRoute,
  153. OUT Dest **ppOldBestDest,
  154. OUT Dest **ppNewBestDest,
  155. OUT Route **ppOldBestRoute);
  156. UINT
  157. CALLCONV
  158. DeleteFromSTrie (IN STrie *pSTrie,
  159. IN Route *pIncRoute,
  160. IN ULONG matchFlags,
  161. OUT Route **ppDelRoute,
  162. OUT Dest **ppOldBestDest,
  163. OUT Dest **ppNewBestDest,
  164. OUT Route **ppOldBestRoute);
  165. UINT
  166. CALLCONV
  167. SearchRouteInSTrie (IN STrie *pSTrie,
  168. IN ULONG routeDest,
  169. IN ULONG routeMask,
  170. IN ULONG routeNHop,
  171. IN PVOID routeOutIF,
  172. IN ULONG matchFlags,
  173. OUT Route **ppBestRoute);
  174. Dest *
  175. CALLCONV
  176. SearchAddrInSTrie (IN STrie *pSTrie,
  177. IN ULONG Addr);
  178. UINT
  179. CALLCONV
  180. IterateOverSTrie (IN STrie *pSTrie,
  181. IN STrieCtxt *pCtxt,
  182. OUT Route **ppNextRoute,
  183. OUT Dest **ppNextDest OPTIONAL);
  184. UINT
  185. CALLCONV
  186. IsSTrieIteratorValid (IN STrie *pSTrie,
  187. IN STrieCtxt *pCtxt);
  188. UINT
  189. CALLCONV
  190. CleanupSTrie (IN STrie *pSTrie);
  191. VOID
  192. CALLCONV
  193. CacheBestRoutesInDest (IN Dest *pDest);
  194. #if DBG
  195. VOID
  196. CALLCONV
  197. PrintSTrie (IN STrie *pSTrie,
  198. IN UINT fPrintAll);
  199. VOID
  200. CALLCONV
  201. PrintSTrieNode (IN STrieNode *pSTrieNode);
  202. #endif
  203. #endif // STRIE_H_INCLUDED