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 |
Wolfgang Denk | cb3761e | 2013-07-28 22:12:47 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: BSD-3-Clause |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 15 | * @par |
| 16 | * -- End of Copyright Notice -- |
| 17 | */ |
| 18 | |
| 19 | #include "IxEthDB_p.h" |
| 20 | |
| 21 | /** |
| 22 | * @brief hashes the mac address in a mac descriptor with a XOR function |
| 23 | * |
| 24 | * @param entry pointer to a mac descriptor to be hashed |
| 25 | * |
| 26 | * This function only extracts the mac address and employs ixEthDBKeyXORHash() |
| 27 | * to do the actual hashing. |
| 28 | * Used only to add a whole entry to a hash table, as opposed to searching which |
| 29 | * takes only a key and uses the key hashing directly. |
| 30 | * |
| 31 | * @see ixEthDBKeyXORHash() |
| 32 | * |
| 33 | * @return the hash value |
| 34 | * |
| 35 | * @internal |
| 36 | */ |
| 37 | UINT32 ixEthDBEntryXORHash(void *entry) |
| 38 | { |
| 39 | MacDescriptor *descriptor = (MacDescriptor *) entry; |
| 40 | |
| 41 | return ixEthDBKeyXORHash(descriptor->macAddress); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @brief hashes a mac address |
| 46 | * |
| 47 | * @param key pointer to a 6 byte structure (typically an IxEthDBMacAddr pointer) |
| 48 | * to be hashed |
| 49 | * |
| 50 | * Given a 6 bytes MAC address, the hash used is: |
| 51 | * |
| 52 | * hash(MAC[0:5]) = MAC[0:1] ^ MAC[2:3] ^ MAC[4:5] |
| 53 | * |
| 54 | * Used by the hash table to search and remove entries based |
| 55 | * solely on their keys (mac addresses). |
| 56 | * |
| 57 | * @return the hash value |
| 58 | * |
| 59 | * @internal |
| 60 | */ |
| 61 | UINT32 ixEthDBKeyXORHash(void *key) |
| 62 | { |
| 63 | UINT32 hashValue; |
| 64 | UINT8 *value = (UINT8 *) key; |
| 65 | |
| 66 | hashValue = (value[5] << 8) | value[4]; |
| 67 | hashValue ^= (value[3] << 8) | value[2]; |
| 68 | hashValue ^= (value[1] << 8) | value[0]; |
| 69 | |
| 70 | return hashValue; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @brief mac descriptor match function |
| 75 | * |
| 76 | * @param reference mac address (typically an IxEthDBMacAddr pointer) structure |
| 77 | * @param entry pointer to a mac descriptor whose key (mac address) is to be |
| 78 | * matched against the reference key |
| 79 | * |
| 80 | * Used by the hash table to retrieve entries. Hashing entries can produce |
| 81 | * collisions, i.e. descriptors with different mac addresses and the same |
| 82 | * hash value, where this function is used to differentiate entries. |
| 83 | * |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 84 | * @retval true if the entry matches the reference key (equal addresses) |
| 85 | * @retval false if the entry does not match the reference key |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 86 | * |
| 87 | * @internal |
| 88 | */ |
| 89 | BOOL ixEthDBAddressMatch(void *reference, void *entry) |
| 90 | { |
| 91 | return (ixEthDBAddressCompare(reference, ((MacDescriptor *) entry)->macAddress) == 0); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @brief compares two mac addresses |
| 96 | * |
| 97 | * @param mac1 first mac address to compare |
| 98 | * @param mac2 second mac address to compare |
| 99 | * |
| 100 | * This comparison works in a similar way to strcmp, producing similar results. |
| 101 | * Used to insert values keyed on mac addresses into binary search trees. |
| 102 | * |
| 103 | * @retval -1 if mac1 < mac2 |
| 104 | * @retval 0 if ma1 == mac2 |
| 105 | * @retval 1 if mac1 > mac2 |
| 106 | */ |
| 107 | UINT32 ixEthDBAddressCompare(UINT8 *mac1, UINT8 *mac2) |
| 108 | { |
| 109 | UINT32 local_index; |
| 110 | |
| 111 | for (local_index = 0 ; local_index < IX_IEEE803_MAC_ADDRESS_SIZE ; local_index++) |
| 112 | { |
| 113 | if (mac1[local_index] > mac2[local_index]) |
| 114 | { |
| 115 | return 1; |
| 116 | } |
| 117 | else if (mac1[local_index] < mac2[local_index]) |
| 118 | { |
| 119 | return -1; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |