IxEthDBSearch.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. Neither the name of the Intel Corporation nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * @par
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. *
  39. * @par
  40. * -- End of Copyright Notice --
  41. */
  42. #include "IxEthDB_p.h"
  43. extern HashTable dbHashtable;
  44. /**
  45. * @brief matches two database records based on their MAC addresses
  46. *
  47. * @param untypedReference record to match against
  48. * @param untypedEntry record to match
  49. *
  50. * @return true if the match is successful or false otherwise
  51. *
  52. * @internal
  53. */
  54. IX_ETH_DB_PUBLIC
  55. BOOL ixEthDBAddressRecordMatch(void *untypedReference, void *untypedEntry)
  56. {
  57. MacDescriptor *entry = (MacDescriptor *) untypedEntry;
  58. MacDescriptor *reference = (MacDescriptor *) untypedReference;
  59. /* check accepted record types */
  60. if ((entry->type & reference->type) == 0) return false;
  61. return (ixEthDBAddressCompare((UINT8 *) entry->macAddress, (UINT8 *) reference->macAddress) == 0);
  62. }
  63. /**
  64. * @brief matches two database records based on their MAC addresses
  65. * and VLAN IDs
  66. *
  67. * @param untypedReference record to match against
  68. * @param untypedEntry record to match
  69. *
  70. * @return true if the match is successful or false otherwise
  71. *
  72. * @internal
  73. */
  74. IX_ETH_DB_PUBLIC
  75. BOOL ixEthDBVlanRecordMatch(void *untypedReference, void *untypedEntry)
  76. {
  77. MacDescriptor *entry = (MacDescriptor *) untypedEntry;
  78. MacDescriptor *reference = (MacDescriptor *) untypedReference;
  79. /* check accepted record types */
  80. if ((entry->type & reference->type) == 0) return false;
  81. return (IX_ETH_DB_GET_VLAN_ID(entry->recordData.filteringVlanData.ieee802_1qTag) ==
  82. IX_ETH_DB_GET_VLAN_ID(reference->recordData.filteringVlanData.ieee802_1qTag)) &&
  83. (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
  84. }
  85. /**
  86. * @brief matches two database records based on their MAC addresses
  87. * and port IDs
  88. *
  89. * @param untypedReference record to match against
  90. * @param untypedEntry record to match
  91. *
  92. * @return true if the match is successful or false otherwise
  93. *
  94. * @internal
  95. */
  96. IX_ETH_DB_PUBLIC
  97. BOOL ixEthDBPortRecordMatch(void *untypedReference, void *untypedEntry)
  98. {
  99. MacDescriptor *entry = (MacDescriptor *) untypedEntry;
  100. MacDescriptor *reference = (MacDescriptor *) untypedReference;
  101. /* check accepted record types */
  102. if ((entry->type & reference->type) == 0) return false;
  103. return (entry->portID == reference->portID) &&
  104. (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
  105. }
  106. /**
  107. * @brief dummy matching function, registered for safety
  108. *
  109. * @param reference record to match against (unused)
  110. * @param entry record to match (unused)
  111. *
  112. * This function is registered in the matching functions
  113. * array on invalid types. Calling it will display an
  114. * error message, indicating an error in the component logic.
  115. *
  116. * @return false
  117. *
  118. * @internal
  119. */
  120. IX_ETH_DB_PUBLIC
  121. BOOL ixEthDBNullMatch(void *reference, void *entry)
  122. {
  123. /* display an error message */
  124. 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);
  125. return false;
  126. }
  127. /**
  128. * @brief registers hash matching methods
  129. *
  130. * @param matchFunctions table of match functions to be populated
  131. *
  132. * This function registers the available record matching functions
  133. * by indexing them on record types into the given function array.
  134. *
  135. * Note that it is compulsory to call this in ixEthDBInit(),
  136. * otherwise hashtable searching and removal will not work
  137. *
  138. * @return number of registered functions
  139. *
  140. * @internal
  141. */
  142. IX_ETH_DB_PUBLIC
  143. UINT32 ixEthDBMatchMethodsRegister(MatchFunction *matchFunctions)
  144. {
  145. UINT32 i;
  146. /* safety first */
  147. for ( i = 0 ; i < IX_ETH_DB_MAX_KEY_INDEX + 1 ; i++)
  148. {
  149. matchFunctions[i] = ixEthDBNullMatch;
  150. }
  151. /* register MAC search method */
  152. matchFunctions[IX_ETH_DB_MAC_KEY] = ixEthDBAddressRecordMatch;
  153. /* register MAC/PortID search method */
  154. matchFunctions[IX_ETH_DB_MAC_PORT_KEY] = ixEthDBPortRecordMatch;
  155. /* register MAC/VLAN ID search method */
  156. matchFunctions[IX_ETH_DB_MAC_VLAN_KEY] = ixEthDBVlanRecordMatch;
  157. return 3; /* three methods */
  158. }
  159. /**
  160. * @brief search a record in the Ethernet datbase
  161. *
  162. * @param macAddress MAC address to perform the search on
  163. * @param typeFilter type of records to consider for matching
  164. *
  165. * @warning if searching is successful an implicit write lock
  166. * to the search result is granted, therefore unlock the
  167. * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
  168. *
  169. * @see ixEthDBReleaseHashNode()
  170. *
  171. * @return the search result, or NULL if a record with the given
  172. * MAC address was not found
  173. *
  174. * @internal
  175. */
  176. IX_ETH_DB_PUBLIC
  177. HashNode* ixEthDBSearch(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
  178. {
  179. HashNode *searchResult = NULL;
  180. MacDescriptor reference;
  181. TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
  182. if (macAddress == NULL)
  183. {
  184. return NULL;
  185. }
  186. /* fill search fields */
  187. memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
  188. /* set acceptable record types */
  189. reference.type = typeFilter;
  190. BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference, &searchResult));
  191. return searchResult;
  192. }
  193. IX_ETH_DB_PUBLIC
  194. IxEthDBStatus ixEthDBPeek(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
  195. {
  196. MacDescriptor reference;
  197. IxEthDBStatus result;
  198. TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
  199. if (macAddress == NULL)
  200. {
  201. return IX_ETH_DB_INVALID_ARG;
  202. }
  203. /* fill search fields */
  204. memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
  205. /* set acceptable record types */
  206. reference.type = typeFilter;
  207. result = ixEthDBPeekHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference);
  208. return result;
  209. }
  210. /**
  211. * @brief search a record in the Ethernet datbase
  212. *
  213. * @param macAddress MAC address to perform the search on
  214. * @param portID port ID to perform the search on
  215. * @param typeFilter type of records to consider for matching
  216. *
  217. * @warning if searching is successful an implicit write lock
  218. * to the search result is granted, therefore unlock the
  219. * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
  220. *
  221. * @see ixEthDBReleaseHashNode()
  222. *
  223. * @return the search result, or NULL if a record with the given
  224. * MAC address/port ID combination was not found
  225. *
  226. * @internal
  227. */
  228. IX_ETH_DB_PUBLIC
  229. HashNode* ixEthDBPortSearch(IxEthDBMacAddr *macAddress, IxEthDBPortId portID, IxEthDBRecordType typeFilter)
  230. {
  231. HashNode *searchResult = NULL;
  232. MacDescriptor reference;
  233. if (macAddress == NULL)
  234. {
  235. return NULL;
  236. }
  237. /* fill search fields */
  238. memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
  239. reference.portID = portID;
  240. /* set acceptable record types */
  241. reference.type = typeFilter;
  242. BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_PORT_KEY, &reference, &searchResult));
  243. return searchResult;
  244. }
  245. /**
  246. * @brief search a record in the Ethernet datbase
  247. *
  248. * @param macAddress MAC address to perform the search on
  249. * @param vlanID VLAN ID to perform the search on
  250. * @param typeFilter type of records to consider for matching
  251. *
  252. * @warning if searching is successful an implicit write lock
  253. * to the search result is granted, therefore unlock the
  254. * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
  255. *
  256. * @see ixEthDBReleaseHashNode()
  257. *
  258. * @return the search result, or NULL if a record with the given
  259. * MAC address/VLAN ID combination was not found
  260. *
  261. * @internal
  262. */
  263. IX_ETH_DB_PUBLIC
  264. HashNode* ixEthDBVlanSearch(IxEthDBMacAddr *macAddress, IxEthDBVlanId vlanID, IxEthDBRecordType typeFilter)
  265. {
  266. HashNode *searchResult = NULL;
  267. MacDescriptor reference;
  268. if (macAddress == NULL)
  269. {
  270. return NULL;
  271. }
  272. /* fill search fields */
  273. memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
  274. reference.recordData.filteringVlanData.ieee802_1qTag =
  275. IX_ETH_DB_SET_VLAN_ID(reference.recordData.filteringVlanData.ieee802_1qTag, vlanID);
  276. /* set acceptable record types */
  277. reference.type = typeFilter;
  278. BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_VLAN_KEY, &reference, &searchResult));
  279. return searchResult;
  280. }