ipa_nat_test025.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*=========================================================================*/
  30. /*!
  31. @file
  32. ipa_nat_test025.c
  33. @brief
  34. Note: Verify the following scenario:
  35. 1. Similare to test022, but with random deletes during adds
  36. */
  37. /*=========================================================================*/
  38. #include "ipa_nat_test.h"
  39. #undef VALID_RULE
  40. #define VALID_RULE(r) ((r) != 0 && (r) != 0xFFFFFFFF)
  41. #undef GET_MAX
  42. #define GET_MAX(ram, rdm) \
  43. do { \
  44. while ( (ram = rand() % 20) < 4); \
  45. while ( (rdm = rand() % 10) >= ram || rdm == 0 ); \
  46. IPADBG("rand_adds_max(%u) rand_dels_max(%u)\n", ram, rdm); \
  47. } while (0)
  48. int ipa_nat_test025(
  49. const char* nat_mem_type,
  50. u32 pub_ip_add,
  51. int total_entries,
  52. u32 tbl_hdl,
  53. int sep,
  54. void* arb_data_ptr)
  55. {
  56. int* tbl_hdl_ptr = (int*) arb_data_ptr;
  57. ipa_nat_ipv4_rule ipv4_rule;
  58. u32 rule_hdls[1024];
  59. ipa_nati_tbl_stats nstats, last_nstats;
  60. ipa_nati_tbl_stats istats, last_istats;
  61. u32 i;
  62. u32 rand_adds_max, rand_dels_max;
  63. u32 tot, tot_added, tot_deleted;
  64. bool switched = false;
  65. const char* mem_type;
  66. int ret;
  67. IPADBG("In\n");
  68. if ( sep )
  69. {
  70. ret = ipa_nat_add_ipv4_tbl(pub_ip_add, nat_mem_type, total_entries, &tbl_hdl);
  71. CHECK_ERR_TBL_STOP(ret, tbl_hdl);
  72. }
  73. ret = ipa_nati_clear_ipv4_tbl(tbl_hdl);
  74. CHECK_ERR_TBL_STOP(ret, tbl_hdl);
  75. ret = ipa_nati_ipv4_tbl_stats(tbl_hdl, &nstats, &istats);
  76. CHECK_ERR_TBL_STOP(ret, tbl_hdl);
  77. IPAINFO("Attempting rule adds to %s table of size: (%u)\n",
  78. ipa3_nat_mem_in_as_str(nstats.nmi),
  79. nstats.tot_ents);
  80. last_nstats = nstats;
  81. last_istats = istats;
  82. memset(rule_hdls, 0, sizeof(rule_hdls));
  83. GET_MAX(rand_adds_max, rand_dels_max);
  84. tot = tot_added = tot_deleted = 0;
  85. for ( i = 0; i < array_sz(rule_hdls); i++ )
  86. {
  87. IPADBG("Trying %u ipa_nat_add_ipv4_rule()\n", i);
  88. memset(&ipv4_rule, 0, sizeof(ipv4_rule));
  89. ipv4_rule.protocol = IPPROTO_TCP;
  90. ipv4_rule.public_port = RAN_PORT;
  91. ipv4_rule.target_ip = RAN_ADDR;
  92. ipv4_rule.target_port = RAN_PORT;
  93. ipv4_rule.private_ip = RAN_ADDR;
  94. ipv4_rule.private_port = RAN_PORT;
  95. ret = ipa_nat_add_ipv4_rule(tbl_hdl, &ipv4_rule, &rule_hdls[i]);
  96. CHECK_ERR_TBL_ACTION(ret, tbl_hdl, break);
  97. IPADBG("Success %u ipa_nat_add_ipv4_rule() -> rule_hdl(0x%08X)\n",
  98. i, rule_hdls[i]);
  99. ret = ipa_nati_ipv4_tbl_stats(tbl_hdl, &nstats, &istats);
  100. CHECK_ERR_TBL_ACTION(ret, tbl_hdl, break);
  101. /*
  102. * Are we in hybrid mode and have we switched memory type?
  103. * Check for it and print the appropriate stats.
  104. */
  105. if ( nstats.nmi != last_nstats.nmi )
  106. {
  107. mem_type = ipa3_nat_mem_in_as_str(last_nstats.nmi);
  108. switched = true;
  109. /*
  110. * NAT table stats...
  111. */
  112. IPAINFO("Able to add (%u) records to %s "
  113. "NAT table of size (%u) or (%f) percent\n",
  114. tot,
  115. mem_type,
  116. last_nstats.tot_ents,
  117. ((float) tot / (float) last_nstats.tot_ents) * 100.0);
  118. IPAINFO("Able to add (%u) records to %s "
  119. "NAT BASE table of size (%u) or (%f) percent\n",
  120. last_nstats.tot_base_ents_filled,
  121. mem_type,
  122. last_nstats.tot_base_ents,
  123. ((float) last_nstats.tot_base_ents_filled /
  124. (float) last_nstats.tot_base_ents) * 100.0);
  125. IPAINFO("Able to add (%u) records to %s "
  126. "NAT EXPN table of size (%u) or (%f) percent\n",
  127. last_nstats.tot_expn_ents_filled,
  128. mem_type,
  129. last_nstats.tot_expn_ents,
  130. ((float) last_nstats.tot_expn_ents_filled /
  131. (float) last_nstats.tot_expn_ents) * 100.0);
  132. IPAINFO("%s NAT table chains: tot_chains(%u) min_len(%u) max_len(%u) avg_len(%f)\n",
  133. mem_type,
  134. last_nstats.tot_chains,
  135. last_nstats.min_chain_len,
  136. last_nstats.max_chain_len,
  137. last_nstats.avg_chain_len);
  138. /*
  139. * INDEX table stats...
  140. */
  141. IPAINFO("Able to add (%u) records to %s "
  142. "IDX table of size (%u) or (%f) percent\n",
  143. tot,
  144. mem_type,
  145. last_istats.tot_ents,
  146. ((float) tot / (float) last_istats.tot_ents) * 100.0);
  147. IPAINFO("Able to add (%u) records to %s "
  148. "IDX BASE table of size (%u) or (%f) percent\n",
  149. last_istats.tot_base_ents_filled,
  150. mem_type,
  151. last_istats.tot_base_ents,
  152. ((float) last_istats.tot_base_ents_filled /
  153. (float) last_istats.tot_base_ents) * 100.0);
  154. IPAINFO("Able to add (%u) records to %s "
  155. "IDX EXPN table of size (%u) or (%f) percent\n",
  156. last_istats.tot_expn_ents_filled,
  157. mem_type,
  158. last_istats.tot_expn_ents,
  159. ((float) last_istats.tot_expn_ents_filled /
  160. (float) last_istats.tot_expn_ents) * 100.0);
  161. IPAINFO("%s IDX table chains: tot_chains(%u) min_len(%u) max_len(%u) avg_len(%f)\n",
  162. mem_type,
  163. last_istats.tot_chains,
  164. last_istats.min_chain_len,
  165. last_istats.max_chain_len,
  166. last_istats.avg_chain_len);
  167. }
  168. last_nstats = nstats;
  169. last_istats = istats;
  170. tot++;
  171. if ( ++tot_added == rand_adds_max )
  172. {
  173. u32 j, k;
  174. u32* hdl_ptr[tot];
  175. for ( j = k = 0; j < array_sz(rule_hdls); j++ )
  176. {
  177. if ( VALID_RULE(rule_hdls[j]) )
  178. {
  179. hdl_ptr[k] = &(rule_hdls[j]);
  180. if ( ++k == tot )
  181. {
  182. break;
  183. }
  184. }
  185. }
  186. IPADBG("About to delete %u rules\n", rand_dels_max);
  187. while ( k )
  188. {
  189. while ( j = rand() % k, ! VALID_RULE(*(hdl_ptr[j])) );
  190. IPADBG("Trying ipa_nat_del_ipv4_rule(0x%08X)\n",
  191. *(hdl_ptr[j]));
  192. ret = ipa_nat_del_ipv4_rule(tbl_hdl, *(hdl_ptr[j]));
  193. CHECK_ERR_TBL_STOP(ret, tbl_hdl);
  194. IPADBG("Success ipa_nat_del_ipv4_rule(0x%08X)\n", *(hdl_ptr[j]));
  195. *(hdl_ptr[j]) = 0xFFFFFFFF;
  196. --tot;
  197. if ( ++tot_deleted == rand_dels_max )
  198. {
  199. break;
  200. }
  201. }
  202. GET_MAX(rand_adds_max, rand_dels_max);
  203. tot_added = tot_deleted = 0;
  204. }
  205. if ( switched )
  206. {
  207. switched = false;
  208. IPAINFO("Continuing rule adds to %s table of size: (%u)\n",
  209. ipa3_nat_mem_in_as_str(nstats.nmi),
  210. nstats.tot_ents);
  211. }
  212. }
  213. ret = ipa_nati_ipv4_tbl_stats(tbl_hdl, &nstats, &istats);
  214. CHECK_ERR_TBL_STOP(ret, tbl_hdl);
  215. mem_type = ipa3_nat_mem_in_as_str(nstats.nmi);
  216. /*
  217. * NAT table stats...
  218. */
  219. IPAINFO("Able to add (%u) records to %s "
  220. "NAT table of size (%u) or (%f) percent\n",
  221. tot,
  222. mem_type,
  223. nstats.tot_ents,
  224. ((float) tot / (float) nstats.tot_ents) * 100.0);
  225. IPAINFO("Able to add (%u) records to %s "
  226. "NAT BASE table of size (%u) or (%f) percent\n",
  227. nstats.tot_base_ents_filled,
  228. mem_type,
  229. nstats.tot_base_ents,
  230. ((float) nstats.tot_base_ents_filled /
  231. (float) nstats.tot_base_ents) * 100.0);
  232. IPAINFO("Able to add (%u) records to %s "
  233. "NAT EXPN table of size (%u) or (%f) percent\n",
  234. nstats.tot_expn_ents_filled,
  235. mem_type,
  236. nstats.tot_expn_ents,
  237. ((float) nstats.tot_expn_ents_filled /
  238. (float) nstats.tot_expn_ents) * 100.0);
  239. IPAINFO("%s NAT table chains: tot_chains(%u) min_len(%u) max_len(%u) avg_len(%f)\n",
  240. mem_type,
  241. nstats.tot_chains,
  242. nstats.min_chain_len,
  243. nstats.max_chain_len,
  244. nstats.avg_chain_len);
  245. /*
  246. * INDEX table stats...
  247. */
  248. IPAINFO("Able to add (%u) records to %s "
  249. "IDX table of size (%u) or (%f) percent\n",
  250. tot,
  251. mem_type,
  252. istats.tot_ents,
  253. ((float) tot / (float) istats.tot_ents) * 100.0);
  254. IPAINFO("Able to add (%u) records to %s "
  255. "IDX BASE table of size (%u) or (%f) percent\n",
  256. istats.tot_base_ents_filled,
  257. mem_type,
  258. istats.tot_base_ents,
  259. ((float) istats.tot_base_ents_filled /
  260. (float) istats.tot_base_ents) * 100.0);
  261. IPAINFO("Able to add (%u) records to %s "
  262. "IDX EXPN table of size (%u) or (%f) percent\n",
  263. istats.tot_expn_ents_filled,
  264. mem_type,
  265. istats.tot_expn_ents,
  266. ((float) istats.tot_expn_ents_filled /
  267. (float) istats.tot_expn_ents) * 100.0);
  268. IPAINFO("%s IDX table chains: tot_chains(%u) min_len(%u) max_len(%u) avg_len(%f)\n",
  269. mem_type,
  270. istats.tot_chains,
  271. istats.min_chain_len,
  272. istats.max_chain_len,
  273. istats.avg_chain_len);
  274. IPAINFO("Deleting remaining rules\n");
  275. for ( i = 0; i < array_sz(rule_hdls); i++ )
  276. {
  277. if ( VALID_RULE(rule_hdls[i]) )
  278. {
  279. IPADBG("Trying ipa_nat_del_ipv4_rule(0x%08X)\n",
  280. rule_hdls[i]);
  281. ret = ipa_nat_del_ipv4_rule(tbl_hdl, rule_hdls[i]);
  282. CHECK_ERR_TBL_STOP(ret, tbl_hdl);
  283. IPADBG("Success ipa_nat_del_ipv4_rule(%u)\n", rule_hdls[i]);
  284. }
  285. }
  286. if ( sep )
  287. {
  288. ret = ipa_nat_del_ipv4_tbl(tbl_hdl);
  289. *tbl_hdl_ptr = 0;
  290. CHECK_ERR(ret);
  291. }
  292. IPADBG("Out\n");
  293. return 0;
  294. }