IxEthDBMem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /**
  2. * @file IxEthDBDBMem.c
  3. *
  4. * @brief Memory handling routines for the MAC address database
  5. *
  6. * @par
  7. * IXP400 SW Release version 2.0
  8. *
  9. * -- Copyright Notice --
  10. *
  11. * @par
  12. * Copyright 2001-2005, Intel Corporation.
  13. * All rights reserved.
  14. *
  15. * @par
  16. * SPDX-License-Identifier: BSD-3-Clause
  17. * @par
  18. * -- End of Copyright Notice --
  19. */
  20. #include "IxEthDB_p.h"
  21. IX_ETH_DB_PRIVATE HashNode *nodePool = NULL;
  22. IX_ETH_DB_PRIVATE MacDescriptor *macPool = NULL;
  23. IX_ETH_DB_PRIVATE MacTreeNode *treePool = NULL;
  24. IX_ETH_DB_PRIVATE HashNode nodePoolArea[NODE_POOL_SIZE];
  25. IX_ETH_DB_PRIVATE MacDescriptor macPoolArea[MAC_POOL_SIZE];
  26. IX_ETH_DB_PRIVATE MacTreeNode treePoolArea[TREE_POOL_SIZE];
  27. IX_ETH_DB_PRIVATE IxOsalMutex nodePoolLock;
  28. IX_ETH_DB_PRIVATE IxOsalMutex macPoolLock;
  29. IX_ETH_DB_PRIVATE IxOsalMutex treePoolLock;
  30. #define LOCK_NODE_POOL { ixOsalMutexLock(&nodePoolLock, IX_OSAL_WAIT_FOREVER); }
  31. #define UNLOCK_NODE_POOL { ixOsalMutexUnlock(&nodePoolLock); }
  32. #define LOCK_MAC_POOL { ixOsalMutexLock(&macPoolLock, IX_OSAL_WAIT_FOREVER); }
  33. #define UNLOCK_MAC_POOL { ixOsalMutexUnlock(&macPoolLock); }
  34. #define LOCK_TREE_POOL { ixOsalMutexLock(&treePoolLock, IX_OSAL_WAIT_FOREVER); }
  35. #define UNLOCK_TREE_POOL { ixOsalMutexUnlock(&treePoolLock); }
  36. /* private function prototypes */
  37. IX_ETH_DB_PRIVATE MacDescriptor* ixEthDBPoolAllocMacDescriptor(void);
  38. IX_ETH_DB_PRIVATE void ixEthDBPoolFreeMacDescriptor(MacDescriptor *macDescriptor);
  39. /**
  40. * @addtogroup EthMemoryManagement
  41. *
  42. * @{
  43. */
  44. /**
  45. * @brief initializes the memory pools used by the ethernet database component
  46. *
  47. * Initializes the hash table node, mac descriptor and mac tree node pools.
  48. * Called at initialization time by @ref ixEthDBInit().
  49. *
  50. * @internal
  51. */
  52. IX_ETH_DB_PUBLIC
  53. void ixEthDBInitMemoryPools(void)
  54. {
  55. int local_index;
  56. /* HashNode pool */
  57. ixOsalMutexInit(&nodePoolLock);
  58. for (local_index = 0 ; local_index < NODE_POOL_SIZE ; local_index++)
  59. {
  60. HashNode *freeNode = &nodePoolArea[local_index];
  61. freeNode->nextFree = nodePool;
  62. nodePool = freeNode;
  63. }
  64. /* MacDescriptor pool */
  65. ixOsalMutexInit(&macPoolLock);
  66. for (local_index = 0 ; local_index < MAC_POOL_SIZE ; local_index++)
  67. {
  68. MacDescriptor *freeDescriptor = &macPoolArea[local_index];
  69. freeDescriptor->nextFree = macPool;
  70. macPool = freeDescriptor;
  71. }
  72. /* MacTreeNode pool */
  73. ixOsalMutexInit(&treePoolLock);
  74. for (local_index = 0 ; local_index < TREE_POOL_SIZE ; local_index++)
  75. {
  76. MacTreeNode *freeNode = &treePoolArea[local_index];
  77. freeNode->nextFree = treePool;
  78. treePool = freeNode;
  79. }
  80. }
  81. /**
  82. * @brief allocates a hash node from the pool
  83. *
  84. * Allocates a hash node and resets its value.
  85. *
  86. * @return the allocated hash node or NULL if the pool is empty
  87. *
  88. * @internal
  89. */
  90. IX_ETH_DB_PUBLIC
  91. HashNode* ixEthDBAllocHashNode(void)
  92. {
  93. HashNode *allocatedNode = NULL;
  94. if (nodePool != NULL)
  95. {
  96. LOCK_NODE_POOL;
  97. allocatedNode = nodePool;
  98. nodePool = nodePool->nextFree;
  99. UNLOCK_NODE_POOL;
  100. memset(allocatedNode, 0, sizeof(HashNode));
  101. }
  102. return allocatedNode;
  103. }
  104. /**
  105. * @brief frees a hash node into the pool
  106. *
  107. * @param hashNode node to be freed
  108. *
  109. * @internal
  110. */
  111. IX_ETH_DB_PUBLIC
  112. void ixEthDBFreeHashNode(HashNode *hashNode)
  113. {
  114. if (hashNode != NULL)
  115. {
  116. LOCK_NODE_POOL;
  117. hashNode->nextFree = nodePool;
  118. nodePool = hashNode;
  119. UNLOCK_NODE_POOL;
  120. }
  121. }
  122. /**
  123. * @brief allocates a mac descriptor from the pool
  124. *
  125. * Allocates a mac descriptor and resets its value.
  126. * This function is not used directly, instead @ref ixEthDBAllocMacDescriptor()
  127. * is used, which keeps track of the pointer reference count.
  128. *
  129. * @see ixEthDBAllocMacDescriptor()
  130. *
  131. * @warning this function is not used directly by any other function
  132. * apart from ixEthDBAllocMacDescriptor()
  133. *
  134. * @return the allocated mac descriptor or NULL if the pool is empty
  135. *
  136. * @internal
  137. */
  138. IX_ETH_DB_PRIVATE
  139. MacDescriptor* ixEthDBPoolAllocMacDescriptor(void)
  140. {
  141. MacDescriptor *allocatedDescriptor = NULL;
  142. if (macPool != NULL)
  143. {
  144. LOCK_MAC_POOL;
  145. allocatedDescriptor = macPool;
  146. macPool = macPool->nextFree;
  147. UNLOCK_MAC_POOL;
  148. memset(allocatedDescriptor, 0, sizeof(MacDescriptor));
  149. }
  150. return allocatedDescriptor;
  151. }
  152. /**
  153. * @brief allocates and initializes a mac descriptor smart pointer
  154. *
  155. * Uses @ref ixEthDBPoolAllocMacDescriptor() to allocate a mac descriptor
  156. * from the pool and initializes its reference count.
  157. *
  158. * @see ixEthDBPoolAllocMacDescriptor()
  159. *
  160. * @return the allocated mac descriptor or NULL if the pool is empty
  161. *
  162. * @internal
  163. */
  164. IX_ETH_DB_PUBLIC
  165. MacDescriptor* ixEthDBAllocMacDescriptor(void)
  166. {
  167. MacDescriptor *allocatedDescriptor = ixEthDBPoolAllocMacDescriptor();
  168. if (allocatedDescriptor != NULL)
  169. {
  170. LOCK_MAC_POOL;
  171. allocatedDescriptor->refCount++;
  172. UNLOCK_MAC_POOL;
  173. }
  174. return allocatedDescriptor;
  175. }
  176. /**
  177. * @brief frees a mac descriptor back into the pool
  178. *
  179. * @param macDescriptor mac descriptor to be freed
  180. *
  181. * @warning this function is not to be called by anyone but
  182. * ixEthDBFreeMacDescriptor()
  183. *
  184. * @see ixEthDBFreeMacDescriptor()
  185. *
  186. * @internal
  187. */
  188. IX_ETH_DB_PRIVATE
  189. void ixEthDBPoolFreeMacDescriptor(MacDescriptor *macDescriptor)
  190. {
  191. LOCK_MAC_POOL;
  192. macDescriptor->nextFree = macPool;
  193. macPool = macDescriptor;
  194. UNLOCK_MAC_POOL;
  195. }
  196. /**
  197. * @brief frees or reduces the usage count of a mac descriptor smart pointer
  198. *
  199. * If the reference count reaches 0 (structure is no longer used anywhere)
  200. * then the descriptor is freed back into the pool using ixEthDBPoolFreeMacDescriptor().
  201. *
  202. * @see ixEthDBPoolFreeMacDescriptor()
  203. *
  204. * @internal
  205. */
  206. IX_ETH_DB_PUBLIC
  207. void ixEthDBFreeMacDescriptor(MacDescriptor *macDescriptor)
  208. {
  209. if (macDescriptor != NULL)
  210. {
  211. LOCK_MAC_POOL;
  212. if (macDescriptor->refCount > 0)
  213. {
  214. macDescriptor->refCount--;
  215. if (macDescriptor->refCount == 0)
  216. {
  217. UNLOCK_MAC_POOL;
  218. ixEthDBPoolFreeMacDescriptor(macDescriptor);
  219. }
  220. else
  221. {
  222. UNLOCK_MAC_POOL;
  223. }
  224. }
  225. else
  226. {
  227. UNLOCK_MAC_POOL;
  228. }
  229. }
  230. }
  231. /**
  232. * @brief clones a mac descriptor smart pointer
  233. *
  234. * @param macDescriptor mac descriptor to clone
  235. *
  236. * Increments the usage count of the smart pointer
  237. *
  238. * @returns the cloned smart pointer
  239. *
  240. * @internal
  241. */
  242. IX_ETH_DB_PUBLIC
  243. MacDescriptor* ixEthDBCloneMacDescriptor(MacDescriptor *macDescriptor)
  244. {
  245. LOCK_MAC_POOL;
  246. if (macDescriptor->refCount == 0)
  247. {
  248. UNLOCK_MAC_POOL;
  249. return NULL;
  250. }
  251. macDescriptor->refCount++;
  252. UNLOCK_MAC_POOL;
  253. return macDescriptor;
  254. }
  255. /**
  256. * @brief allocates a mac tree node from the pool
  257. *
  258. * Allocates and initializes a mac tree node from the pool.
  259. *
  260. * @return the allocated mac tree node or NULL if the pool is empty
  261. *
  262. * @internal
  263. */
  264. IX_ETH_DB_PUBLIC
  265. MacTreeNode* ixEthDBAllocMacTreeNode(void)
  266. {
  267. MacTreeNode *allocatedNode = NULL;
  268. if (treePool != NULL)
  269. {
  270. LOCK_TREE_POOL;
  271. allocatedNode = treePool;
  272. treePool = treePool->nextFree;
  273. UNLOCK_TREE_POOL;
  274. memset(allocatedNode, 0, sizeof(MacTreeNode));
  275. }
  276. return allocatedNode;
  277. }
  278. /**
  279. * @brief frees a mac tree node back into the pool
  280. *
  281. * @param macNode mac tree node to be freed
  282. *
  283. * @warning not to be used except from ixEthDBFreeMacTreeNode().
  284. *
  285. * @see ixEthDBFreeMacTreeNode()
  286. *
  287. * @internal
  288. */
  289. void ixEthDBPoolFreeMacTreeNode(MacTreeNode *macNode)
  290. {
  291. if (macNode != NULL)
  292. {
  293. LOCK_TREE_POOL;
  294. macNode->nextFree = treePool;
  295. treePool = macNode;
  296. UNLOCK_TREE_POOL;
  297. }
  298. }
  299. /**
  300. * @brief frees or reduces the usage count of a mac tree node smart pointer
  301. *
  302. * @param macNode mac tree node to free
  303. *
  304. * Reduces the usage count of the given mac node. If the usage count
  305. * reaches 0 the node is freed back into the pool using ixEthDBPoolFreeMacTreeNode()
  306. *
  307. * @internal
  308. */
  309. IX_ETH_DB_PUBLIC
  310. void ixEthDBFreeMacTreeNode(MacTreeNode *macNode)
  311. {
  312. if (macNode->descriptor != NULL)
  313. {
  314. ixEthDBFreeMacDescriptor(macNode->descriptor);
  315. }
  316. if (macNode->left != NULL)
  317. {
  318. ixEthDBFreeMacTreeNode(macNode->left);
  319. }
  320. if (macNode->right != NULL)
  321. {
  322. ixEthDBFreeMacTreeNode(macNode->right);
  323. }
  324. ixEthDBPoolFreeMacTreeNode(macNode);
  325. }
  326. /**
  327. * @brief clones a mac tree node
  328. *
  329. * @param macNode mac tree node to be cloned
  330. *
  331. * Increments the usage count of the node, <i>its associated descriptor
  332. * and <b>recursively</b> of all its child nodes</i>.
  333. *
  334. * @warning this function is recursive and clones whole trees/subtrees, use only for
  335. * root nodes
  336. *
  337. * @internal
  338. */
  339. IX_ETH_DB_PUBLIC
  340. MacTreeNode* ixEthDBCloneMacTreeNode(MacTreeNode *macNode)
  341. {
  342. if (macNode != NULL)
  343. {
  344. MacTreeNode *clonedMacNode = ixEthDBAllocMacTreeNode();
  345. if (clonedMacNode != NULL)
  346. {
  347. if (macNode->right != NULL)
  348. {
  349. clonedMacNode->right = ixEthDBCloneMacTreeNode(macNode->right);
  350. }
  351. if (macNode->left != NULL)
  352. {
  353. clonedMacNode->left = ixEthDBCloneMacTreeNode(macNode->left);
  354. }
  355. if (macNode->descriptor != NULL)
  356. {
  357. clonedMacNode->descriptor = ixEthDBCloneMacDescriptor(macNode->descriptor);
  358. }
  359. }
  360. return clonedMacNode;
  361. }
  362. else
  363. {
  364. return NULL;
  365. }
  366. }
  367. #ifndef NDEBUG
  368. /* Debug statistical functions for memory usage */
  369. extern HashTable dbHashtable;
  370. int ixEthDBNumHashElements(void);
  371. int ixEthDBNumHashElements(void)
  372. {
  373. UINT32 bucketIndex;
  374. int numElements = 0;
  375. HashTable *hashTable = &dbHashtable;
  376. for (bucketIndex = 0 ; bucketIndex < hashTable->numBuckets ; bucketIndex++)
  377. {
  378. if (hashTable->hashBuckets[bucketIndex] != NULL)
  379. {
  380. HashNode *node = hashTable->hashBuckets[bucketIndex];
  381. while (node != NULL)
  382. {
  383. numElements++;
  384. node = node->next;
  385. }
  386. }
  387. }
  388. return numElements;
  389. }
  390. UINT32 ixEthDBSearchTreeUsageGet(MacTreeNode *tree)
  391. {
  392. if (tree == NULL)
  393. {
  394. return 0;
  395. }
  396. else
  397. {
  398. return 1 /* this node */ + ixEthDBSearchTreeUsageGet(tree->left) + ixEthDBSearchTreeUsageGet(tree->right);
  399. }
  400. }
  401. int ixEthDBShowMemoryStatus(void)
  402. {
  403. MacDescriptor *mac;
  404. MacTreeNode *tree;
  405. HashNode *node;
  406. int macCounter = 0;
  407. int treeCounter = 0;
  408. int nodeCounter = 0;
  409. int totalTreeUsage = 0;
  410. int totalDescriptorUsage = 0;
  411. int totalCloneDescriptorUsage = 0;
  412. int totalNodeUsage = 0;
  413. UINT32 portIndex;
  414. LOCK_NODE_POOL;
  415. LOCK_MAC_POOL;
  416. LOCK_TREE_POOL;
  417. mac = macPool;
  418. tree = treePool;
  419. node = nodePool;
  420. while (mac != NULL)
  421. {
  422. macCounter++;
  423. mac = mac->nextFree;
  424. if (macCounter > MAC_POOL_SIZE)
  425. {
  426. break;
  427. }
  428. }
  429. while (tree != NULL)
  430. {
  431. treeCounter++;
  432. tree = tree->nextFree;
  433. if (treeCounter > TREE_POOL_SIZE)
  434. {
  435. break;
  436. }
  437. }
  438. while (node != NULL)
  439. {
  440. nodeCounter++;
  441. node = node->nextFree;
  442. if (nodeCounter > NODE_POOL_SIZE)
  443. {
  444. break;
  445. }
  446. }
  447. for (portIndex = 0 ; portIndex < IX_ETH_DB_NUMBER_OF_PORTS ; portIndex++)
  448. {
  449. int treeUsage = ixEthDBSearchTreeUsageGet(ixEthDBPortInfo[portIndex].updateMethod.searchTree);
  450. totalTreeUsage += treeUsage;
  451. totalCloneDescriptorUsage += treeUsage; /* each tree node contains a descriptor */
  452. }
  453. totalNodeUsage = ixEthDBNumHashElements();
  454. totalDescriptorUsage += totalNodeUsage; /* each hash table entry contains a descriptor */
  455. UNLOCK_NODE_POOL;
  456. UNLOCK_MAC_POOL;
  457. UNLOCK_TREE_POOL;
  458. printf("Ethernet database memory usage stats:\n\n");
  459. if (macCounter <= MAC_POOL_SIZE)
  460. {
  461. printf("\tMAC descriptor pool : %d free out of %d entries (%d%%)\n", macCounter, MAC_POOL_SIZE, macCounter * 100 / MAC_POOL_SIZE);
  462. }
  463. else
  464. {
  465. printf("\tMAC descriptor pool : invalid state (ring within the pool), normally %d entries\n", MAC_POOL_SIZE);
  466. }
  467. if (treeCounter <= TREE_POOL_SIZE)
  468. {
  469. printf("\tTree node pool : %d free out of %d entries (%d%%)\n", treeCounter, TREE_POOL_SIZE, treeCounter * 100 / TREE_POOL_SIZE);
  470. }
  471. else
  472. {
  473. printf("\tTREE descriptor pool : invalid state (ring within the pool), normally %d entries\n", TREE_POOL_SIZE);
  474. }
  475. if (nodeCounter <= NODE_POOL_SIZE)
  476. {
  477. printf("\tHash node pool : %d free out of %d entries (%d%%)\n", nodeCounter, NODE_POOL_SIZE, nodeCounter * 100 / NODE_POOL_SIZE);
  478. }
  479. else
  480. {
  481. printf("\tNODE descriptor pool : invalid state (ring within the pool), normally %d entries\n", NODE_POOL_SIZE);
  482. }
  483. printf("\n");
  484. printf("\tMAC descriptor usage : %d entries, %d cloned\n", totalDescriptorUsage, totalCloneDescriptorUsage);
  485. printf("\tTree node usage : %d entries\n", totalTreeUsage);
  486. printf("\tHash node usage : %d entries\n", totalNodeUsage);
  487. printf("\n");
  488. /* search for duplicate nodes in the mac pool */
  489. {
  490. MacDescriptor *reference = macPool;
  491. while (reference != NULL)
  492. {
  493. MacDescriptor *comparison = reference->nextFree;
  494. while (comparison != NULL)
  495. {
  496. if (reference == comparison)
  497. {
  498. printf("Warning: reached a duplicate (%p), invalid MAC pool state\n", reference);
  499. return 1;
  500. }
  501. comparison = comparison->nextFree;
  502. }
  503. reference = reference->nextFree;
  504. }
  505. }
  506. printf("No duplicates found in the MAC pool (sanity check ok)\n");
  507. return 0;
  508. }
  509. #endif /* NDEBUG */
  510. /**
  511. * @} EthMemoryManagement
  512. */