IxEthDBMem.c 15 KB

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