IxEthDBLearning.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "IxEthDB_p.h"
  43. /**
  44. * @brief hashes the mac address in a mac descriptor with a XOR function
  45. *
  46. * @param entry pointer to a mac descriptor to be hashed
  47. *
  48. * This function only extracts the mac address and employs ixEthDBKeyXORHash()
  49. * to do the actual hashing.
  50. * Used only to add a whole entry to a hash table, as opposed to searching which
  51. * takes only a key and uses the key hashing directly.
  52. *
  53. * @see ixEthDBKeyXORHash()
  54. *
  55. * @return the hash value
  56. *
  57. * @internal
  58. */
  59. UINT32 ixEthDBEntryXORHash(void *entry)
  60. {
  61. MacDescriptor *descriptor = (MacDescriptor *) entry;
  62. return ixEthDBKeyXORHash(descriptor->macAddress);
  63. }
  64. /**
  65. * @brief hashes a mac address
  66. *
  67. * @param key pointer to a 6 byte structure (typically an IxEthDBMacAddr pointer)
  68. * to be hashed
  69. *
  70. * Given a 6 bytes MAC address, the hash used is:
  71. *
  72. * hash(MAC[0:5]) = MAC[0:1] ^ MAC[2:3] ^ MAC[4:5]
  73. *
  74. * Used by the hash table to search and remove entries based
  75. * solely on their keys (mac addresses).
  76. *
  77. * @return the hash value
  78. *
  79. * @internal
  80. */
  81. UINT32 ixEthDBKeyXORHash(void *key)
  82. {
  83. UINT32 hashValue;
  84. UINT8 *value = (UINT8 *) key;
  85. hashValue = (value[5] << 8) | value[4];
  86. hashValue ^= (value[3] << 8) | value[2];
  87. hashValue ^= (value[1] << 8) | value[0];
  88. return hashValue;
  89. }
  90. /**
  91. * @brief mac descriptor match function
  92. *
  93. * @param reference mac address (typically an IxEthDBMacAddr pointer) structure
  94. * @param entry pointer to a mac descriptor whose key (mac address) is to be
  95. * matched against the reference key
  96. *
  97. * Used by the hash table to retrieve entries. Hashing entries can produce
  98. * collisions, i.e. descriptors with different mac addresses and the same
  99. * hash value, where this function is used to differentiate entries.
  100. *
  101. * @retval true if the entry matches the reference key (equal addresses)
  102. * @retval false if the entry does not match the reference key
  103. *
  104. * @internal
  105. */
  106. BOOL ixEthDBAddressMatch(void *reference, void *entry)
  107. {
  108. return (ixEthDBAddressCompare(reference, ((MacDescriptor *) entry)->macAddress) == 0);
  109. }
  110. /**
  111. * @brief compares two mac addresses
  112. *
  113. * @param mac1 first mac address to compare
  114. * @param mac2 second mac address to compare
  115. *
  116. * This comparison works in a similar way to strcmp, producing similar results.
  117. * Used to insert values keyed on mac addresses into binary search trees.
  118. *
  119. * @retval -1 if mac1 < mac2
  120. * @retval 0 if ma1 == mac2
  121. * @retval 1 if mac1 > mac2
  122. */
  123. UINT32 ixEthDBAddressCompare(UINT8 *mac1, UINT8 *mac2)
  124. {
  125. UINT32 local_index;
  126. for (local_index = 0 ; local_index < IX_IEEE803_MAC_ADDRESS_SIZE ; local_index++)
  127. {
  128. if (mac1[local_index] > mac2[local_index])
  129. {
  130. return 1;
  131. }
  132. else if (mac1[local_index] < mac2[local_index])
  133. {
  134. return -1;
  135. }
  136. }
  137. return 0;
  138. }