IxEthDBLearning.c 3.1 KB

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