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.

203 lines
5.3 KiB

  1. /*++
  2. Copyright(c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. brdghash.h
  5. Abstract:
  6. Ethernet MAC level bridge.
  7. Hash table implementation header
  8. Author:
  9. Mark Aiken
  10. Environment:
  11. Kernel mode driver
  12. Revision History:
  13. October 2000 - Original version
  14. --*/
  15. // ===========================================================================
  16. //
  17. // DECLARATIONS
  18. //
  19. // ===========================================================================
  20. #define MAX_SUPPORTED_KEYSIZE 8 // Key can be up to 8 bytes
  21. //
  22. // Structure of a table entry
  23. //
  24. typedef struct _HASH_TABLE_ENTRY
  25. {
  26. struct _HASH_TABLE_ENTRY *Next;
  27. ULONG LastSeen; // Result of NdisGetSystemUpTime()
  28. UCHAR key[MAX_SUPPORTED_KEYSIZE];
  29. // User's data follows
  30. } HASH_TABLE_ENTRY, *PHASH_TABLE_ENTRY;
  31. // The prototype of a hash function
  32. typedef ULONG (*PHASH_FUNCTION)(PUCHAR pKey);
  33. // The prototype of a matching function
  34. typedef BOOLEAN (*PHASH_MATCH_FUNCTION)(PHASH_TABLE_ENTRY, PVOID);
  35. // The prototype of a data-copy function
  36. typedef VOID (*PHASH_COPY_FUNCTION)(PHASH_TABLE_ENTRY, PUCHAR);
  37. // The prototype of a function used in calls to BrdgHashPrefixMultiMatch
  38. typedef VOID (*PMULTIMATCH_FUNC)(PHASH_TABLE_ENTRY, PVOID);
  39. //
  40. // Structure of the table itself
  41. //
  42. typedef struct _HASH_TABLE
  43. {
  44. NPAGED_LOOKASIDE_LIST entryPool;
  45. //
  46. // The consistency of the buckets is protected by the tableLock.
  47. //
  48. // The LastSeen field in each entry is volatile and is updated
  49. // with interlocked instructions.
  50. //
  51. NDIS_RW_LOCK tableLock;
  52. // These fields never change after creation
  53. PHASH_FUNCTION pHashFunction;
  54. PHASH_TABLE_ENTRY *pBuckets;
  55. ULONG numBuckets, entrySize;
  56. UINT keySize;
  57. BRIDGE_TIMER timer;
  58. ULONG_PTR maxEntries;
  59. ULONG maxTimeoutAge; // Maximum possible timeoutAge
  60. // These fields change but are protected by the tableLock.
  61. ULONG_PTR numEntries;
  62. ULONG nextTimerBucket;
  63. // This field is manipulated with InterlockExchange() instructions
  64. // to avoid having to take the table lock to change it.
  65. ULONG timeoutAge;
  66. // In debug builds, this tracks how many entries are in each bucket
  67. // so we can tell whether the table is well balanced
  68. #if DBG
  69. PUINT bucketSizes;
  70. #endif
  71. } HASH_TABLE, *PHASH_TABLE;
  72. // ===========================================================================
  73. //
  74. // PROTOTYPES
  75. //
  76. // ===========================================================================
  77. PHASH_TABLE
  78. BrdgHashCreateTable(
  79. IN PHASH_FUNCTION pHashFunction,
  80. IN ULONG numBuckets,
  81. IN ULONG entrySize,
  82. IN ULONG maxEntries,
  83. IN ULONG startTimeoutAge,
  84. IN ULONG maxTimeoutAge,
  85. IN UINT keySize
  86. );
  87. VOID
  88. BrdgHashFreeHashTable(
  89. IN PHASH_TABLE pTable
  90. );
  91. PHASH_TABLE_ENTRY
  92. BrdgHashFindEntry(
  93. IN PHASH_TABLE pTable,
  94. IN PUCHAR pKey,
  95. IN LOCK_STATE *pLockState
  96. );
  97. PHASH_TABLE_ENTRY
  98. BrdgHashRefreshOrInsert(
  99. IN PHASH_TABLE pTable,
  100. IN PUCHAR pKey,
  101. OUT BOOLEAN *pIsNewEntry,
  102. OUT PLOCK_STATE pLockState
  103. );
  104. VOID
  105. BrdgHashRemoveMatching(
  106. IN PHASH_TABLE pTable,
  107. IN PHASH_MATCH_FUNCTION pMatchFunc,
  108. PVOID pData
  109. );
  110. ULONG
  111. BrdgHashCopyMatching(
  112. IN PHASH_TABLE pTable,
  113. IN PHASH_MATCH_FUNCTION pMatchFunc,
  114. IN PHASH_COPY_FUNCTION pCopyFunction,
  115. IN ULONG copyUnitSize,
  116. IN PVOID pData,
  117. IN PUCHAR pBuffer,
  118. IN ULONG BufferLength
  119. );
  120. VOID
  121. BrdgHashPrefixMultiMatch(
  122. IN PHASH_TABLE pTable,
  123. IN PUCHAR pPrefixKey,
  124. IN UINT prefixLen,
  125. IN PMULTIMATCH_FUNC pFunc,
  126. IN PVOID pData
  127. );
  128. // ===========================================================================
  129. //
  130. // INLINES
  131. //
  132. // ===========================================================================
  133. //
  134. // Changes the timeout value for a hash table
  135. //
  136. __forceinline
  137. VOID
  138. BrdgHashChangeTableTimeout(
  139. IN PHASH_TABLE pTable,
  140. IN ULONG timeout
  141. )
  142. {
  143. InterlockedExchange( (PLONG)&pTable->timeoutAge, (LONG)timeout );
  144. }
  145. //
  146. // Refreshes a table entry held by the caller.
  147. // ASSUMES the caller holds a read or write lock on the table
  148. // enclosing this entry!
  149. //
  150. __forceinline
  151. VOID
  152. BrdgHashRefreshEntry(
  153. IN PHASH_TABLE_ENTRY pEntry
  154. )
  155. {
  156. ULONG CurrentTime;
  157. NdisGetSystemUpTime( &CurrentTime );
  158. InterlockedExchange( (PLONG)&pEntry->LastSeen, (LONG)CurrentTime );
  159. }