IxEthDBCore.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /**
  2. * @file IxEthDBDBCore.c
  3. *
  4. * @brief Database support functions
  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. /* list of database hashtables */
  22. IX_ETH_DB_PUBLIC HashTable dbHashtable;
  23. IX_ETH_DB_PUBLIC MatchFunction matchFunctions[IX_ETH_DB_MAX_KEY_INDEX + 1];
  24. IX_ETH_DB_PUBLIC BOOL ixEthDBPortUpdateRequired[IX_ETH_DB_MAX_RECORD_TYPE_INDEX + 1];
  25. IX_ETH_DB_PUBLIC UINT32 ixEthDBKeyType[IX_ETH_DB_MAX_RECORD_TYPE_INDEX + 1];
  26. /* private initialization flag */
  27. IX_ETH_DB_PRIVATE BOOL ethDBInitializationComplete = false;
  28. /**
  29. * @brief initializes EthDB
  30. *
  31. * This function must be called to initialize the component.
  32. *
  33. * It does the following things:
  34. * - checks the port definition structure
  35. * - scans the capabilities of the NPE images and sets the
  36. * capabilities of the ports accordingly
  37. * - initializes the memory pools internally used in EthDB
  38. * for storing database records and handling data
  39. * - registers automatic update handlers for add and remove
  40. * operations
  41. * - registers hashing match functions, depending on key sets
  42. * - initializes the main database hashtable
  43. * - allocates contiguous memory zones to be used for NPE
  44. * updates
  45. * - registers the serialize methods used to convert data
  46. * into NPE-readable format
  47. * - starts the event processor
  48. *
  49. * Note that this function is documented in the public
  50. * component header file, IxEthDB.h.
  51. *
  52. * @return IX_ETH_DB_SUCCESS or an appropriate error if the
  53. * component failed to initialize correctly
  54. */
  55. IX_ETH_DB_PUBLIC
  56. IxEthDBStatus ixEthDBInit(void)
  57. {
  58. IxEthDBStatus result;
  59. if (ethDBInitializationComplete)
  60. {
  61. /* redundant */
  62. return IX_ETH_DB_SUCCESS;
  63. }
  64. /* trap an invalid port definition structure */
  65. IX_ETH_DB_PORTS_ASSERTION;
  66. /* memory management */
  67. ixEthDBInitMemoryPools();
  68. /* register hashing search methods */
  69. ixEthDBMatchMethodsRegister(matchFunctions);
  70. /* register type-based automatic port updates */
  71. ixEthDBUpdateTypeRegister(ixEthDBPortUpdateRequired);
  72. /* register record to key type mappings */
  73. ixEthDBKeyTypeRegister(ixEthDBKeyType);
  74. /* hash table */
  75. ixEthDBInitHash(&dbHashtable, NUM_BUCKETS, ixEthDBEntryXORHash, matchFunctions, (FreeFunction) ixEthDBFreeMacDescriptor);
  76. /* NPE update zones */
  77. ixEthDBNPEUpdateAreasInit();
  78. /* register record serialization methods */
  79. ixEthDBRecordSerializeMethodsRegister();
  80. /* start the event processor */
  81. result = ixEthDBEventProcessorInit();
  82. /* scan NPE features */
  83. if (result == IX_ETH_DB_SUCCESS)
  84. {
  85. ixEthDBFeatureCapabilityScan();
  86. }
  87. ethDBInitializationComplete = true;
  88. return result;
  89. }
  90. /**
  91. * @brief prepares EthDB for unloading
  92. *
  93. * This function must be called before removing the
  94. * EthDB component from memory (e.g. doing rmmod in
  95. * Linux) if the component is to be re-initialized again
  96. * without rebooting the platform.
  97. *
  98. * All the EthDB ports must be disabled before this
  99. * function is to be called. Failure to disable all
  100. * the ports will return the IX_ETH_DB_BUSY error.
  101. *
  102. * This function will destroy mutexes, deallocate
  103. * memory and stop the event processor.
  104. *
  105. * Note that this function is fully documented in the
  106. * main component header file, IxEthDB.h.
  107. *
  108. * @return IX_ETH_DB_SUCCESS if de-initialization
  109. * completed successfully or an appropriate error
  110. * message otherwise
  111. */
  112. IX_ETH_DB_PUBLIC
  113. IxEthDBStatus ixEthDBUnload(void)
  114. {
  115. IxEthDBPortId portIndex;
  116. if (!ethDBInitializationComplete)
  117. {
  118. /* redundant */
  119. return IX_ETH_DB_SUCCESS;
  120. }
  121. /* check if any ports are enabled */
  122. for (portIndex = 0 ; portIndex < IX_ETH_DB_NUMBER_OF_PORTS ; portIndex++)
  123. {
  124. if (ixEthDBPortInfo[portIndex].enabled)
  125. {
  126. return IX_ETH_DB_BUSY;
  127. }
  128. }
  129. /* free port resources */
  130. for (portIndex = 0 ; portIndex < IX_ETH_DB_NUMBER_OF_PORTS ; portIndex++)
  131. {
  132. if (ixEthDBPortDefinitions[portIndex].type == IX_ETH_NPE)
  133. {
  134. ixOsalMutexDestroy(&ixEthDBPortInfo[portIndex].npeAckLock);
  135. }
  136. ixEthDBPortInfo[portIndex].initialized = false;
  137. }
  138. /* shutdown event processor */
  139. ixEthDBStopLearningFunction();
  140. /* deallocate NPE update zones */
  141. ixEthDBNPEUpdateAreasUnload();
  142. ethDBInitializationComplete = false;
  143. return IX_ETH_DB_SUCCESS;
  144. }
  145. /**
  146. * @brief adds a new entry to the Ethernet database
  147. *
  148. * @param newRecordTemplate address of the record template to use
  149. * @param updateTrigger port map containing the update triggers
  150. * resulting from this update operation
  151. *
  152. * Creates a new database entry, populates it with the data
  153. * copied from the given template and adds the record to the
  154. * database hash table.
  155. * It also checks whether the new record type is registered to trigger
  156. * automatic updates; if it is, the update trigger will contain the
  157. * port on which the record insertion was performed, as well as the
  158. * old port in case the addition was a record migration (from one port
  159. * to the other). The caller can use the updateTrigger to trigger
  160. * automatic updates on the ports changed as a result of this addition.
  161. *
  162. * @retval IX_ETH_DB_SUCCESS addition successful
  163. * @retval IX_ETH_DB_NOMEM insertion failed, no memory left in the mac descriptor memory pool
  164. * @retval IX_ETH_DB_BUSY database busy, cannot insert due to locking
  165. *
  166. * @internal
  167. */
  168. IX_ETH_DB_PUBLIC
  169. IxEthDBStatus ixEthDBAdd(MacDescriptor *newRecordTemplate, IxEthDBPortMap updateTrigger)
  170. {
  171. IxEthDBStatus result;
  172. MacDescriptor *newDescriptor;
  173. IxEthDBPortId originalPortID;
  174. HashNode *node = NULL;
  175. BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, ixEthDBKeyType[newRecordTemplate->type], newRecordTemplate, &node));
  176. TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
  177. if (node == NULL)
  178. {
  179. /* not found, create a new one */
  180. newDescriptor = ixEthDBAllocMacDescriptor();
  181. if (newDescriptor == NULL)
  182. {
  183. return IX_ETH_DB_NOMEM; /* no memory */
  184. }
  185. /* old port does not exist, avoid unnecessary updates */
  186. originalPortID = newRecordTemplate->portID;
  187. }
  188. else
  189. {
  190. /* a node with the same key exists, will update node */
  191. newDescriptor = (MacDescriptor *) node->data;
  192. /* save original port id */
  193. originalPortID = newDescriptor->portID;
  194. }
  195. /* copy/update fields into new record */
  196. memcpy(newDescriptor->macAddress, newRecordTemplate->macAddress, sizeof (IxEthDBMacAddr));
  197. memcpy(&newDescriptor->recordData, &newRecordTemplate->recordData, sizeof (IxEthDBRecordData));
  198. newDescriptor->type = newRecordTemplate->type;
  199. newDescriptor->portID = newRecordTemplate->portID;
  200. newDescriptor->user = newRecordTemplate->user;
  201. if (node == NULL)
  202. {
  203. /* new record, insert into hashtable */
  204. BUSY_RETRY_WITH_RESULT(ixEthDBAddHashEntry(&dbHashtable, newDescriptor), result);
  205. if (result != IX_ETH_DB_SUCCESS)
  206. {
  207. ixEthDBFreeMacDescriptor(newDescriptor);
  208. return result; /* insertion failed */
  209. }
  210. }
  211. if (node != NULL)
  212. {
  213. /* release access */
  214. ixEthDBReleaseHashNode(node);
  215. }
  216. /* trigger add/remove update if required by type */
  217. if (updateTrigger != NULL &&
  218. ixEthDBPortUpdateRequired[newRecordTemplate->type])
  219. {
  220. /* add new port to update list */
  221. JOIN_PORT_TO_MAP(updateTrigger, newRecordTemplate->portID);
  222. /* check if record has moved, we'll need to update the old port as well */
  223. if (originalPortID != newDescriptor->portID)
  224. {
  225. JOIN_PORT_TO_MAP(updateTrigger, originalPortID);
  226. }
  227. }
  228. return IX_ETH_DB_SUCCESS;
  229. }
  230. /**
  231. * @brief remove a record from the Ethernet database
  232. *
  233. * @param templateRecord template record used to determine
  234. * what record is to be removed
  235. * @param updateTrigger port map containing the update triggers
  236. * resulting from this update operation
  237. *
  238. * This function will examine the template record it receives
  239. * and attempts to delete a record of the same type and containing
  240. * the same keys as the template record. If deletion is successful
  241. * and the record type is registered for automatic port updates the
  242. * port will also be set in the updateTrigger port map, so that the
  243. * client can perform an update of the port.
  244. *
  245. * @retval IX_ETH_DB_SUCCESS removal was successful
  246. * @retval IX_ETH_DB_NO_SUCH_ADDR the record with the given MAC address was not found
  247. * @retval IX_ETH_DB_BUSY database busy, cannot remove due to locking
  248. *
  249. * @internal
  250. */
  251. IX_ETH_DB_PUBLIC
  252. IxEthDBStatus ixEthDBRemove(MacDescriptor *templateRecord, IxEthDBPortMap updateTrigger)
  253. {
  254. IxEthDBStatus result;
  255. TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
  256. BUSY_RETRY_WITH_RESULT(ixEthDBRemoveHashEntry(&dbHashtable, ixEthDBKeyType[templateRecord->type], templateRecord), result);
  257. if (result != IX_ETH_DB_SUCCESS)
  258. {
  259. return IX_ETH_DB_NO_SUCH_ADDR; /* not found */
  260. }
  261. /* trigger add/remove update if required by type */
  262. if (updateTrigger != NULL
  263. &&ixEthDBPortUpdateRequired[templateRecord->type])
  264. {
  265. /* add new port to update list */
  266. JOIN_PORT_TO_MAP(updateTrigger, templateRecord->portID);
  267. }
  268. return IX_ETH_DB_SUCCESS;
  269. }
  270. /**
  271. * @brief register record key types
  272. *
  273. * This function registers the appropriate key types,
  274. * depending on record types.
  275. *
  276. * All filtering records use the MAC address as the key.
  277. * WiFi and Firewall records use a compound key consisting
  278. * in both the MAC address and the port ID.
  279. *
  280. * @return the number of registered record types
  281. */
  282. IX_ETH_DB_PUBLIC
  283. UINT32 ixEthDBKeyTypeRegister(UINT32 *keyType)
  284. {
  285. /* safety */
  286. memset(keyType, 0, sizeof (keyType));
  287. /* register all known record types */
  288. keyType[IX_ETH_DB_FILTERING_RECORD] = IX_ETH_DB_MAC_KEY;
  289. keyType[IX_ETH_DB_FILTERING_VLAN_RECORD] = IX_ETH_DB_MAC_KEY;
  290. keyType[IX_ETH_DB_ALL_FILTERING_RECORDS] = IX_ETH_DB_MAC_KEY;
  291. keyType[IX_ETH_DB_WIFI_RECORD] = IX_ETH_DB_MAC_PORT_KEY;
  292. keyType[IX_ETH_DB_FIREWALL_RECORD] = IX_ETH_DB_MAC_PORT_KEY;
  293. return 5;
  294. }
  295. /**
  296. * @brief Sets a user-defined field into a database record
  297. *
  298. * Note that this function is fully documented in the main component
  299. * header file.
  300. */
  301. IX_ETH_DB_PUBLIC
  302. IxEthDBStatus ixEthDBUserFieldSet(IxEthDBRecordType recordType, IxEthDBMacAddr *macAddr, IxEthDBPortId portID, IxEthDBVlanId vlanID, void *field)
  303. {
  304. HashNode *result = NULL;
  305. if (macAddr == NULL)
  306. {
  307. return IX_ETH_DB_INVALID_ARG;
  308. }
  309. if (recordType == IX_ETH_DB_FILTERING_RECORD)
  310. {
  311. result = ixEthDBSearch(macAddr, recordType);
  312. }
  313. else if (recordType == IX_ETH_DB_FILTERING_VLAN_RECORD)
  314. {
  315. result = ixEthDBVlanSearch(macAddr, vlanID, recordType);
  316. }
  317. else if (recordType == IX_ETH_DB_WIFI_RECORD || recordType == IX_ETH_DB_FIREWALL_RECORD)
  318. {
  319. IX_ETH_DB_CHECK_PORT_EXISTS(portID);
  320. result = ixEthDBPortSearch(macAddr, portID, recordType);
  321. }
  322. else
  323. {
  324. return IX_ETH_DB_INVALID_ARG;
  325. }
  326. if (result == NULL)
  327. {
  328. return IX_ETH_DB_NO_SUCH_ADDR;
  329. }
  330. ((MacDescriptor *) result->data)->user = field;
  331. ixEthDBReleaseHashNode(result);
  332. return IX_ETH_DB_SUCCESS;
  333. }
  334. /**
  335. * @brief Retrieves a user-defined field from a database record
  336. *
  337. * Note that this function is fully documented in the main component
  338. * header file.
  339. */
  340. IX_ETH_DB_PUBLIC
  341. IxEthDBStatus ixEthDBUserFieldGet(IxEthDBRecordType recordType, IxEthDBMacAddr *macAddr, IxEthDBPortId portID, IxEthDBVlanId vlanID, void **field)
  342. {
  343. HashNode *result = NULL;
  344. if (macAddr == NULL || field == NULL)
  345. {
  346. return IX_ETH_DB_INVALID_ARG;
  347. }
  348. if (recordType == IX_ETH_DB_FILTERING_RECORD)
  349. {
  350. result = ixEthDBSearch(macAddr, recordType);
  351. }
  352. else if (recordType == IX_ETH_DB_FILTERING_VLAN_RECORD)
  353. {
  354. result = ixEthDBVlanSearch(macAddr, vlanID, recordType);
  355. }
  356. else if (recordType == IX_ETH_DB_WIFI_RECORD || recordType == IX_ETH_DB_FIREWALL_RECORD)
  357. {
  358. IX_ETH_DB_CHECK_PORT_EXISTS(portID);
  359. result = ixEthDBPortSearch(macAddr, portID, recordType);
  360. }
  361. else
  362. {
  363. return IX_ETH_DB_INVALID_ARG;
  364. }
  365. if (result == NULL)
  366. {
  367. return IX_ETH_DB_NO_SUCH_ADDR;
  368. }
  369. *field = ((MacDescriptor *) result->data)->user;
  370. ixEthDBReleaseHashNode(result);
  371. return IX_ETH_DB_SUCCESS;
  372. }