Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file IxEthDBLearning.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 | |
| 43 | #include "IxEthDB_p.h" |
| 44 | |
| 45 | /** |
| 46 | * @brief hashes the mac address in a mac descriptor with a XOR function |
| 47 | * |
| 48 | * @param entry pointer to a mac descriptor to be hashed |
| 49 | * |
| 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. |
| 54 | * |
| 55 | * @see ixEthDBKeyXORHash() |
| 56 | * |
| 57 | * @return the hash value |
| 58 | * |
| 59 | * @internal |
| 60 | */ |
| 61 | UINT32 ixEthDBEntryXORHash(void *entry) |
| 62 | { |
| 63 | MacDescriptor *descriptor = (MacDescriptor *) entry; |
| 64 | |
| 65 | return ixEthDBKeyXORHash(descriptor->macAddress); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @brief hashes a mac address |
| 70 | * |
| 71 | * @param key pointer to a 6 byte structure (typically an IxEthDBMacAddr pointer) |
| 72 | * to be hashed |
| 73 | * |
| 74 | * Given a 6 bytes MAC address, the hash used is: |
| 75 | * |
| 76 | * hash(MAC[0:5]) = MAC[0:1] ^ MAC[2:3] ^ MAC[4:5] |
| 77 | * |
| 78 | * Used by the hash table to search and remove entries based |
| 79 | * solely on their keys (mac addresses). |
| 80 | * |
| 81 | * @return the hash value |
| 82 | * |
| 83 | * @internal |
| 84 | */ |
| 85 | UINT32 ixEthDBKeyXORHash(void *key) |
| 86 | { |
| 87 | UINT32 hashValue; |
| 88 | UINT8 *value = (UINT8 *) key; |
| 89 | |
| 90 | hashValue = (value[5] << 8) | value[4]; |
| 91 | hashValue ^= (value[3] << 8) | value[2]; |
| 92 | hashValue ^= (value[1] << 8) | value[0]; |
| 93 | |
| 94 | return hashValue; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @brief mac descriptor match function |
| 99 | * |
| 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 |
| 103 | * |
| 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. |
| 107 | * |
| 108 | * @retval TRUE if the entry matches the reference key (equal addresses) |
| 109 | * @retval FALSE if the entry does not match the reference key |
| 110 | * |
| 111 | * @internal |
| 112 | */ |
| 113 | BOOL ixEthDBAddressMatch(void *reference, void *entry) |
| 114 | { |
| 115 | return (ixEthDBAddressCompare(reference, ((MacDescriptor *) entry)->macAddress) == 0); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @brief compares two mac addresses |
| 120 | * |
| 121 | * @param mac1 first mac address to compare |
| 122 | * @param mac2 second mac address to compare |
| 123 | * |
| 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. |
| 126 | * |
| 127 | * @retval -1 if mac1 < mac2 |
| 128 | * @retval 0 if ma1 == mac2 |
| 129 | * @retval 1 if mac1 > mac2 |
| 130 | */ |
| 131 | UINT32 ixEthDBAddressCompare(UINT8 *mac1, UINT8 *mac2) |
| 132 | { |
| 133 | UINT32 local_index; |
| 134 | |
| 135 | for (local_index = 0 ; local_index < IX_IEEE803_MAC_ADDRESS_SIZE ; local_index++) |
| 136 | { |
| 137 | if (mac1[local_index] > mac2[local_index]) |
| 138 | { |
| 139 | return 1; |
| 140 | } |
| 141 | else if (mac1[local_index] < mac2[local_index]) |
| 142 | { |
| 143 | return -1; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |