2 * @file IxEthDBLearning.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"
46 * @brief hashes the mac address in a mac descriptor with a XOR function
48 * @param entry pointer to a mac descriptor to be hashed
50 * This function only extracts the mac address and employs ixEthDBKeyXORHash()
51 * to do the actual hashing.
52 * Used only to add a whole entry to a hash table, as opposed to searching which
53 * takes only a key and uses the key hashing directly.
55 * @see ixEthDBKeyXORHash()
57 * @return the hash value
61 UINT32 ixEthDBEntryXORHash(void *entry)
63 MacDescriptor *descriptor = (MacDescriptor *) entry;
65 return ixEthDBKeyXORHash(descriptor->macAddress);
69 * @brief hashes a mac address
71 * @param key pointer to a 6 byte structure (typically an IxEthDBMacAddr pointer)
74 * Given a 6 bytes MAC address, the hash used is:
76 * hash(MAC[0:5]) = MAC[0:1] ^ MAC[2:3] ^ MAC[4:5]
78 * Used by the hash table to search and remove entries based
79 * solely on their keys (mac addresses).
81 * @return the hash value
85 UINT32 ixEthDBKeyXORHash(void *key)
88 UINT8 *value = (UINT8 *) key;
90 hashValue = (value[5] << 8) | value[4];
91 hashValue ^= (value[3] << 8) | value[2];
92 hashValue ^= (value[1] << 8) | value[0];
98 * @brief mac descriptor match function
100 * @param reference mac address (typically an IxEthDBMacAddr pointer) structure
101 * @param entry pointer to a mac descriptor whose key (mac address) is to be
102 * matched against the reference key
104 * Used by the hash table to retrieve entries. Hashing entries can produce
105 * collisions, i.e. descriptors with different mac addresses and the same
106 * hash value, where this function is used to differentiate entries.
108 * @retval TRUE if the entry matches the reference key (equal addresses)
109 * @retval FALSE if the entry does not match the reference key
113 BOOL ixEthDBAddressMatch(void *reference, void *entry)
115 return (ixEthDBAddressCompare(reference, ((MacDescriptor *) entry)->macAddress) == 0);
119 * @brief compares two mac addresses
121 * @param mac1 first mac address to compare
122 * @param mac2 second mac address to compare
124 * This comparison works in a similar way to strcmp, producing similar results.
125 * Used to insert values keyed on mac addresses into binary search trees.
127 * @retval -1 if mac1 < mac2
128 * @retval 0 if ma1 == mac2
129 * @retval 1 if mac1 > mac2
131 UINT32 ixEthDBAddressCompare(UINT8 *mac1, UINT8 *mac2)
135 for (local_index = 0 ; local_index < IX_IEEE803_MAC_ADDRESS_SIZE ; local_index++)
137 if (mac1[local_index] > mac2[local_index])
141 else if (mac1[local_index] < mac2[local_index])