IxEthDBSearch.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * @file IxEthDBSearch.c
  3. *
  4. * @par
  5. * IXP400 SW Release version 2.0
  6. *
  7. * -- Copyright Notice --
  8. *
  9. * @par
  10. * Copyright 2001-2005, Intel Corporation.
  11. * All rights reserved.
  12. *
  13. * @par
  14. * SPDX-License-Identifier: BSD-3-Clause
  15. * @par
  16. * -- End of Copyright Notice --
  17. */
  18. #include "IxEthDB_p.h"
  19. extern HashTable dbHashtable;
  20. /**
  21. * @brief matches two database records based on their MAC addresses
  22. *
  23. * @param untypedReference record to match against
  24. * @param untypedEntry record to match
  25. *
  26. * @return true if the match is successful or false otherwise
  27. *
  28. * @internal
  29. */
  30. IX_ETH_DB_PUBLIC
  31. BOOL ixEthDBAddressRecordMatch(void *untypedReference, void *untypedEntry)
  32. {
  33. MacDescriptor *entry = (MacDescriptor *) untypedEntry;
  34. MacDescriptor *reference = (MacDescriptor *) untypedReference;
  35. /* check accepted record types */
  36. if ((entry->type & reference->type) == 0) return false;
  37. return (ixEthDBAddressCompare((UINT8 *) entry->macAddress, (UINT8 *) reference->macAddress) == 0);
  38. }
  39. /**
  40. * @brief matches two database records based on their MAC addresses
  41. * and VLAN IDs
  42. *
  43. * @param untypedReference record to match against
  44. * @param untypedEntry record to match
  45. *
  46. * @return true if the match is successful or false otherwise
  47. *
  48. * @internal
  49. */
  50. IX_ETH_DB_PUBLIC
  51. BOOL ixEthDBVlanRecordMatch(void *untypedReference, void *untypedEntry)
  52. {
  53. MacDescriptor *entry = (MacDescriptor *) untypedEntry;
  54. MacDescriptor *reference = (MacDescriptor *) untypedReference;
  55. /* check accepted record types */
  56. if ((entry->type & reference->type) == 0) return false;
  57. return (IX_ETH_DB_GET_VLAN_ID(entry->recordData.filteringVlanData.ieee802_1qTag) ==
  58. IX_ETH_DB_GET_VLAN_ID(reference->recordData.filteringVlanData.ieee802_1qTag)) &&
  59. (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
  60. }
  61. /**
  62. * @brief matches two database records based on their MAC addresses
  63. * and port IDs
  64. *
  65. * @param untypedReference record to match against
  66. * @param untypedEntry record to match
  67. *
  68. * @return true if the match is successful or false otherwise
  69. *
  70. * @internal
  71. */
  72. IX_ETH_DB_PUBLIC
  73. BOOL ixEthDBPortRecordMatch(void *untypedReference, void *untypedEntry)
  74. {
  75. MacDescriptor *entry = (MacDescriptor *) untypedEntry;
  76. MacDescriptor *reference = (MacDescriptor *) untypedReference;
  77. /* check accepted record types */
  78. if ((entry->type & reference->type) == 0) return false;
  79. return (entry->portID == reference->portID) &&
  80. (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
  81. }
  82. /**
  83. * @brief dummy matching function, registered for safety
  84. *
  85. * @param reference record to match against (unused)
  86. * @param entry record to match (unused)
  87. *
  88. * This function is registered in the matching functions
  89. * array on invalid types. Calling it will display an
  90. * error message, indicating an error in the component logic.
  91. *
  92. * @return false
  93. *
  94. * @internal
  95. */
  96. IX_ETH_DB_PUBLIC
  97. BOOL ixEthDBNullMatch(void *reference, void *entry)
  98. {
  99. /* display an error message */
  100. ixOsalLog(IX_OSAL_LOG_LVL_WARNING, IX_OSAL_LOG_DEV_STDOUT, "DB: (Search) The NullMatch function was called, wrong key type?\n", 0, 0, 0, 0, 0, 0);
  101. return false;
  102. }
  103. /**
  104. * @brief registers hash matching methods
  105. *
  106. * @param matchFunctions table of match functions to be populated
  107. *
  108. * This function registers the available record matching functions
  109. * by indexing them on record types into the given function array.
  110. *
  111. * Note that it is compulsory to call this in ixEthDBInit(),
  112. * otherwise hashtable searching and removal will not work
  113. *
  114. * @return number of registered functions
  115. *
  116. * @internal
  117. */
  118. IX_ETH_DB_PUBLIC
  119. UINT32 ixEthDBMatchMethodsRegister(MatchFunction *matchFunctions)
  120. {
  121. UINT32 i;
  122. /* safety first */
  123. for ( i = 0 ; i < IX_ETH_DB_MAX_KEY_INDEX + 1 ; i++)
  124. {
  125. matchFunctions[i] = ixEthDBNullMatch;
  126. }
  127. /* register MAC search method */
  128. matchFunctions[IX_ETH_DB_MAC_KEY] = ixEthDBAddressRecordMatch;
  129. /* register MAC/PortID search method */
  130. matchFunctions[IX_ETH_DB_MAC_PORT_KEY] = ixEthDBPortRecordMatch;
  131. /* register MAC/VLAN ID search method */
  132. matchFunctions[IX_ETH_DB_MAC_VLAN_KEY] = ixEthDBVlanRecordMatch;
  133. return 3; /* three methods */
  134. }
  135. /**
  136. * @brief search a record in the Ethernet datbase
  137. *
  138. * @param macAddress MAC address to perform the search on
  139. * @param typeFilter type of records to consider for matching
  140. *
  141. * @warning if searching is successful an implicit write lock
  142. * to the search result is granted, therefore unlock the
  143. * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
  144. *
  145. * @see ixEthDBReleaseHashNode()
  146. *
  147. * @return the search result, or NULL if a record with the given
  148. * MAC address was not found
  149. *
  150. * @internal
  151. */
  152. IX_ETH_DB_PUBLIC
  153. HashNode* ixEthDBSearch(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
  154. {
  155. HashNode *searchResult = NULL;
  156. MacDescriptor reference;
  157. TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
  158. if (macAddress == NULL)
  159. {
  160. return NULL;
  161. }
  162. /* fill search fields */
  163. memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
  164. /* set acceptable record types */
  165. reference.type = typeFilter;
  166. BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference, &searchResult));
  167. return searchResult;
  168. }
  169. IX_ETH_DB_PUBLIC
  170. IxEthDBStatus ixEthDBPeek(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
  171. {
  172. MacDescriptor reference;
  173. IxEthDBStatus result;
  174. TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
  175. if (macAddress == NULL)
  176. {
  177. return IX_ETH_DB_INVALID_ARG;
  178. }
  179. /* fill search fields */
  180. memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
  181. /* set acceptable record types */
  182. reference.type = typeFilter;
  183. result = ixEthDBPeekHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference);
  184. return result;
  185. }
  186. /**
  187. * @brief search a record in the Ethernet datbase
  188. *
  189. * @param macAddress MAC address to perform the search on
  190. * @param portID port ID to perform the search on
  191. * @param typeFilter type of records to consider for matching
  192. *
  193. * @warning if searching is successful an implicit write lock
  194. * to the search result is granted, therefore unlock the
  195. * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
  196. *
  197. * @see ixEthDBReleaseHashNode()
  198. *
  199. * @return the search result, or NULL if a record with the given
  200. * MAC address/port ID combination was not found
  201. *
  202. * @internal
  203. */
  204. IX_ETH_DB_PUBLIC
  205. HashNode* ixEthDBPortSearch(IxEthDBMacAddr *macAddress, IxEthDBPortId portID, IxEthDBRecordType typeFilter)
  206. {
  207. HashNode *searchResult = NULL;
  208. MacDescriptor reference;
  209. if (macAddress == NULL)
  210. {
  211. return NULL;
  212. }
  213. /* fill search fields */
  214. memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
  215. reference.portID = portID;
  216. /* set acceptable record types */
  217. reference.type = typeFilter;
  218. BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_PORT_KEY, &reference, &searchResult));
  219. return searchResult;
  220. }
  221. /**
  222. * @brief search a record in the Ethernet datbase
  223. *
  224. * @param macAddress MAC address to perform the search on
  225. * @param vlanID VLAN ID to perform the search on
  226. * @param typeFilter type of records to consider for matching
  227. *
  228. * @warning if searching is successful an implicit write lock
  229. * to the search result is granted, therefore unlock the
  230. * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
  231. *
  232. * @see ixEthDBReleaseHashNode()
  233. *
  234. * @return the search result, or NULL if a record with the given
  235. * MAC address/VLAN ID combination was not found
  236. *
  237. * @internal
  238. */
  239. IX_ETH_DB_PUBLIC
  240. HashNode* ixEthDBVlanSearch(IxEthDBMacAddr *macAddress, IxEthDBVlanId vlanID, IxEthDBRecordType typeFilter)
  241. {
  242. HashNode *searchResult = NULL;
  243. MacDescriptor reference;
  244. if (macAddress == NULL)
  245. {
  246. return NULL;
  247. }
  248. /* fill search fields */
  249. memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
  250. reference.recordData.filteringVlanData.ieee802_1qTag =
  251. IX_ETH_DB_SET_VLAN_ID(reference.recordData.filteringVlanData.ieee802_1qTag, vlanID);
  252. /* set acceptable record types */
  253. reference.type = typeFilter;
  254. BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_VLAN_KEY, &reference, &searchResult));
  255. return searchResult;
  256. }