2 * @file IxEthDBSearch.c
5 * IXP400 SW Release version 2.0
7 * -- Copyright Notice --
10 * Copyright 2001-2005, Intel Corporation.
11 * All rights reserved.
14 * SPDX-License-Identifier: BSD-3-Clause
16 * -- End of Copyright Notice --
19 #include "IxEthDB_p.h"
21 extern HashTable dbHashtable;
24 * @brief matches two database records based on their MAC addresses
26 * @param untypedReference record to match against
27 * @param untypedEntry record to match
29 * @return true if the match is successful or false otherwise
34 BOOL ixEthDBAddressRecordMatch(void *untypedReference, void *untypedEntry)
36 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
37 MacDescriptor *reference = (MacDescriptor *) untypedReference;
39 /* check accepted record types */
40 if ((entry->type & reference->type) == 0) return false;
42 return (ixEthDBAddressCompare((UINT8 *) entry->macAddress, (UINT8 *) reference->macAddress) == 0);
46 * @brief matches two database records based on their MAC addresses
49 * @param untypedReference record to match against
50 * @param untypedEntry record to match
52 * @return true if the match is successful or false otherwise
57 BOOL ixEthDBVlanRecordMatch(void *untypedReference, void *untypedEntry)
59 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
60 MacDescriptor *reference = (MacDescriptor *) untypedReference;
62 /* check accepted record types */
63 if ((entry->type & reference->type) == 0) return false;
65 return (IX_ETH_DB_GET_VLAN_ID(entry->recordData.filteringVlanData.ieee802_1qTag) ==
66 IX_ETH_DB_GET_VLAN_ID(reference->recordData.filteringVlanData.ieee802_1qTag)) &&
67 (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
71 * @brief matches two database records based on their MAC addresses
74 * @param untypedReference record to match against
75 * @param untypedEntry record to match
77 * @return true if the match is successful or false otherwise
82 BOOL ixEthDBPortRecordMatch(void *untypedReference, void *untypedEntry)
84 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
85 MacDescriptor *reference = (MacDescriptor *) untypedReference;
87 /* check accepted record types */
88 if ((entry->type & reference->type) == 0) return false;
90 return (entry->portID == reference->portID) &&
91 (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
95 * @brief dummy matching function, registered for safety
97 * @param reference record to match against (unused)
98 * @param entry record to match (unused)
100 * This function is registered in the matching functions
101 * array on invalid types. Calling it will display an
102 * error message, indicating an error in the component logic.
109 BOOL ixEthDBNullMatch(void *reference, void *entry)
111 /* display an error message */
113 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);
120 * @brief registers hash matching methods
122 * @param matchFunctions table of match functions to be populated
124 * This function registers the available record matching functions
125 * by indexing them on record types into the given function array.
127 * Note that it is compulsory to call this in ixEthDBInit(),
128 * otherwise hashtable searching and removal will not work
130 * @return number of registered functions
135 UINT32 ixEthDBMatchMethodsRegister(MatchFunction *matchFunctions)
140 for ( i = 0 ; i < IX_ETH_DB_MAX_KEY_INDEX + 1 ; i++)
142 matchFunctions[i] = ixEthDBNullMatch;
145 /* register MAC search method */
146 matchFunctions[IX_ETH_DB_MAC_KEY] = ixEthDBAddressRecordMatch;
148 /* register MAC/PortID search method */
149 matchFunctions[IX_ETH_DB_MAC_PORT_KEY] = ixEthDBPortRecordMatch;
151 /* register MAC/VLAN ID search method */
152 matchFunctions[IX_ETH_DB_MAC_VLAN_KEY] = ixEthDBVlanRecordMatch;
154 return 3; /* three methods */
158 * @brief search a record in the Ethernet datbase
160 * @param macAddress MAC address to perform the search on
161 * @param typeFilter type of records to consider for matching
163 * @warning if searching is successful an implicit write lock
164 * to the search result is granted, therefore unlock the
165 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
167 * @see ixEthDBReleaseHashNode()
169 * @return the search result, or NULL if a record with the given
170 * MAC address was not found
175 HashNode* ixEthDBSearch(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
177 HashNode *searchResult = NULL;
178 MacDescriptor reference;
180 TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
182 if (macAddress == NULL)
187 /* fill search fields */
188 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
190 /* set acceptable record types */
191 reference.type = typeFilter;
193 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference, &searchResult));
199 IxEthDBStatus ixEthDBPeek(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
201 MacDescriptor reference;
202 IxEthDBStatus result;
204 TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
206 if (macAddress == NULL)
208 return IX_ETH_DB_INVALID_ARG;
211 /* fill search fields */
212 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
214 /* set acceptable record types */
215 reference.type = typeFilter;
217 result = ixEthDBPeekHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference);
223 * @brief search a record in the Ethernet datbase
225 * @param macAddress MAC address to perform the search on
226 * @param portID port ID to perform the search on
227 * @param typeFilter type of records to consider for matching
229 * @warning if searching is successful an implicit write lock
230 * to the search result is granted, therefore unlock the
231 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
233 * @see ixEthDBReleaseHashNode()
235 * @return the search result, or NULL if a record with the given
236 * MAC address/port ID combination was not found
241 HashNode* ixEthDBPortSearch(IxEthDBMacAddr *macAddress, IxEthDBPortId portID, IxEthDBRecordType typeFilter)
243 HashNode *searchResult = NULL;
244 MacDescriptor reference;
246 if (macAddress == NULL)
251 /* fill search fields */
252 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
253 reference.portID = portID;
255 /* set acceptable record types */
256 reference.type = typeFilter;
258 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_PORT_KEY, &reference, &searchResult));
264 * @brief search a record in the Ethernet datbase
266 * @param macAddress MAC address to perform the search on
267 * @param vlanID VLAN ID to perform the search on
268 * @param typeFilter type of records to consider for matching
270 * @warning if searching is successful an implicit write lock
271 * to the search result is granted, therefore unlock the
272 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
274 * @see ixEthDBReleaseHashNode()
276 * @return the search result, or NULL if a record with the given
277 * MAC address/VLAN ID combination was not found
282 HashNode* ixEthDBVlanSearch(IxEthDBMacAddr *macAddress, IxEthDBVlanId vlanID, IxEthDBRecordType typeFilter)
284 HashNode *searchResult = NULL;
285 MacDescriptor reference;
287 if (macAddress == NULL)
292 /* fill search fields */
293 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
294 reference.recordData.filteringVlanData.ieee802_1qTag =
295 IX_ETH_DB_SET_VLAN_ID(reference.recordData.filteringVlanData.ieee802_1qTag, vlanID);
297 /* set acceptable record types */
298 reference.type = typeFilter;
300 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_VLAN_KEY, &reference, &searchResult));