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 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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
40 * -- End of Copyright Notice --
43 #include "IxEthDB_p.h"
45 extern HashTable dbHashtable;
48 * @brief matches two database records based on their MAC addresses
50 * @param untypedReference record to match against
51 * @param untypedEntry record to match
53 * @return TRUE if the match is successful or FALSE otherwise
58 BOOL ixEthDBAddressRecordMatch(void *untypedReference, void *untypedEntry)
60 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
61 MacDescriptor *reference = (MacDescriptor *) untypedReference;
63 /* check accepted record types */
64 if ((entry->type & reference->type) == 0) return FALSE;
66 return (ixEthDBAddressCompare((UINT8 *) entry->macAddress, (UINT8 *) reference->macAddress) == 0);
70 * @brief matches two database records based on their MAC addresses
73 * @param untypedReference record to match against
74 * @param untypedEntry record to match
76 * @return TRUE if the match is successful or FALSE otherwise
81 BOOL ixEthDBVlanRecordMatch(void *untypedReference, void *untypedEntry)
83 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
84 MacDescriptor *reference = (MacDescriptor *) untypedReference;
86 /* check accepted record types */
87 if ((entry->type & reference->type) == 0) return FALSE;
89 return (IX_ETH_DB_GET_VLAN_ID(entry->recordData.filteringVlanData.ieee802_1qTag) ==
90 IX_ETH_DB_GET_VLAN_ID(reference->recordData.filteringVlanData.ieee802_1qTag)) &&
91 (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
95 * @brief matches two database records based on their MAC addresses
98 * @param untypedReference record to match against
99 * @param untypedEntry record to match
101 * @return TRUE if the match is successful or FALSE otherwise
106 BOOL ixEthDBPortRecordMatch(void *untypedReference, void *untypedEntry)
108 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
109 MacDescriptor *reference = (MacDescriptor *) untypedReference;
111 /* check accepted record types */
112 if ((entry->type & reference->type) == 0) return FALSE;
114 return (entry->portID == reference->portID) &&
115 (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
119 * @brief dummy matching function, registered for safety
121 * @param reference record to match against (unused)
122 * @param entry record to match (unused)
124 * This function is registered in the matching functions
125 * array on invalid types. Calling it will display an
126 * error message, indicating an error in the component logic.
133 BOOL ixEthDBNullMatch(void *reference, void *entry)
135 /* display an error message */
137 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);
144 * @brief registers hash matching methods
146 * @param matchFunctions table of match functions to be populated
148 * This function registers the available record matching functions
149 * by indexing them on record types into the given function array.
151 * Note that it is compulsory to call this in ixEthDBInit(),
152 * otherwise hashtable searching and removal will not work
154 * @return number of registered functions
159 UINT32 ixEthDBMatchMethodsRegister(MatchFunction *matchFunctions)
164 for ( i = 0 ; i < IX_ETH_DB_MAX_KEY_INDEX + 1 ; i++)
166 matchFunctions[i] = ixEthDBNullMatch;
169 /* register MAC search method */
170 matchFunctions[IX_ETH_DB_MAC_KEY] = ixEthDBAddressRecordMatch;
172 /* register MAC/PortID search method */
173 matchFunctions[IX_ETH_DB_MAC_PORT_KEY] = ixEthDBPortRecordMatch;
175 /* register MAC/VLAN ID search method */
176 matchFunctions[IX_ETH_DB_MAC_VLAN_KEY] = ixEthDBVlanRecordMatch;
178 return 3; /* three methods */
182 * @brief search a record in the Ethernet datbase
184 * @param macAddress MAC address to perform the search on
185 * @param typeFilter type of records to consider for matching
187 * @warning if searching is successful an implicit write lock
188 * to the search result is granted, therefore unlock the
189 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
191 * @see ixEthDBReleaseHashNode()
193 * @return the search result, or NULL if a record with the given
194 * MAC address was not found
199 HashNode* ixEthDBSearch(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
201 HashNode *searchResult = NULL;
202 MacDescriptor reference;
204 TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
206 if (macAddress == NULL)
211 /* fill search fields */
212 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
214 /* set acceptable record types */
215 reference.type = typeFilter;
217 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference, &searchResult));
223 IxEthDBStatus ixEthDBPeek(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
225 MacDescriptor reference;
226 IxEthDBStatus result;
228 TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
230 if (macAddress == NULL)
232 return IX_ETH_DB_INVALID_ARG;
235 /* fill search fields */
236 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
238 /* set acceptable record types */
239 reference.type = typeFilter;
241 result = ixEthDBPeekHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference);
247 * @brief search a record in the Ethernet datbase
249 * @param macAddress MAC address to perform the search on
250 * @param portID port ID to perform the search on
251 * @param typeFilter type of records to consider for matching
253 * @warning if searching is successful an implicit write lock
254 * to the search result is granted, therefore unlock the
255 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
257 * @see ixEthDBReleaseHashNode()
259 * @return the search result, or NULL if a record with the given
260 * MAC address/port ID combination was not found
265 HashNode* ixEthDBPortSearch(IxEthDBMacAddr *macAddress, IxEthDBPortId portID, IxEthDBRecordType typeFilter)
267 HashNode *searchResult = NULL;
268 MacDescriptor reference;
270 if (macAddress == NULL)
275 /* fill search fields */
276 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
277 reference.portID = portID;
279 /* set acceptable record types */
280 reference.type = typeFilter;
282 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_PORT_KEY, &reference, &searchResult));
288 * @brief search a record in the Ethernet datbase
290 * @param macAddress MAC address to perform the search on
291 * @param vlanID VLAN ID to perform the search on
292 * @param typeFilter type of records to consider for matching
294 * @warning if searching is successful an implicit write lock
295 * to the search result is granted, therefore unlock the
296 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
298 * @see ixEthDBReleaseHashNode()
300 * @return the search result, or NULL if a record with the given
301 * MAC address/VLAN ID combination was not found
306 HashNode* ixEthDBVlanSearch(IxEthDBMacAddr *macAddress, IxEthDBVlanId vlanID, IxEthDBRecordType typeFilter)
308 HashNode *searchResult = NULL;
309 MacDescriptor reference;
311 if (macAddress == NULL)
316 /* fill search fields */
317 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
318 reference.recordData.filteringVlanData.ieee802_1qTag =
319 IX_ETH_DB_SET_VLAN_ID(reference.recordData.filteringVlanData.ieee802_1qTag, vlanID);
321 /* set acceptable record types */
322 reference.type = typeFilter;
324 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_VLAN_KEY, &reference, &searchResult));