ipa_nat_map.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #include <map>
  30. #include <iterator>
  31. #include "ipa_nat_utils.h"
  32. #include "ipa_nat_map.h"
  33. static std::map<uint32_t, uint32_t> map_array[MAP_NUM_MAX];
  34. /******************************************************************************/
  35. int ipa_nat_map_add(
  36. ipa_which_map which,
  37. uint32_t key,
  38. uint32_t val )
  39. {
  40. int ret_val = 0;
  41. std::pair<std::map<uint32_t, uint32_t>::iterator, bool> ret;
  42. IPADBG("In\n");
  43. if ( ! VALID_IPA_USE_MAP(which) )
  44. {
  45. IPAERR("Bad arg which(%u)\n", which);
  46. ret_val = -1;
  47. goto bail;
  48. }
  49. IPADBG("[%s] key(%u) -> val(%u)\n",
  50. ipa_which_map_as_str(which), key, val);
  51. ret = map_array[which].insert(std::pair<uint32_t, uint32_t>(key, val));
  52. if ( ret.second == false )
  53. {
  54. IPAERR("[%s] key(%u) already exists in map\n",
  55. ipa_which_map_as_str(which),
  56. key);
  57. ret_val = -1;
  58. }
  59. bail:
  60. IPADBG("Out\n");
  61. return ret_val;
  62. }
  63. /******************************************************************************/
  64. int ipa_nat_map_find(
  65. ipa_which_map which,
  66. uint32_t key,
  67. uint32_t* val_ptr )
  68. {
  69. int ret_val = 0;
  70. std::map<uint32_t, uint32_t>::iterator it;
  71. IPADBG("In\n");
  72. if ( ! VALID_IPA_USE_MAP(which) )
  73. {
  74. IPAERR("Bad arg which(%u)\n", which);
  75. ret_val = -1;
  76. goto bail;
  77. }
  78. IPADBG("[%s] key(%u)\n",
  79. ipa_which_map_as_str(which), key);
  80. it = map_array[which].find(key);
  81. if ( it == map_array[which].end() )
  82. {
  83. IPAERR("[%s] key(%u) not found in map\n",
  84. ipa_which_map_as_str(which),
  85. key);
  86. ret_val = -1;
  87. }
  88. else
  89. {
  90. if ( val_ptr )
  91. {
  92. *val_ptr = it->second;
  93. IPADBG("[%s] key(%u) -> val(%u)\n",
  94. ipa_which_map_as_str(which),
  95. key, *val_ptr);
  96. }
  97. }
  98. bail:
  99. IPADBG("Out\n");
  100. return ret_val;
  101. }
  102. /******************************************************************************/
  103. int ipa_nat_map_del(
  104. ipa_which_map which,
  105. uint32_t key,
  106. uint32_t* val_ptr )
  107. {
  108. int ret_val = 0;
  109. std::map<uint32_t, uint32_t>::iterator it;
  110. IPADBG("In\n");
  111. if ( ! VALID_IPA_USE_MAP(which) )
  112. {
  113. IPAERR("Bad arg which(%u)\n", which);
  114. ret_val = -1;
  115. goto bail;
  116. }
  117. IPADBG("[%s] key(%u)\n",
  118. ipa_which_map_as_str(which), key);
  119. it = map_array[which].find(key);
  120. if ( it == map_array[which].end() )
  121. {
  122. IPAERR("[%s] key(%u) not found in map\n",
  123. ipa_which_map_as_str(which),
  124. key);
  125. ret_val = -1;
  126. }
  127. else
  128. {
  129. if ( val_ptr )
  130. {
  131. *val_ptr = it->second;
  132. IPADBG("[%s] key(%u) -> val(%u)\n",
  133. ipa_which_map_as_str(which),
  134. key, *val_ptr);
  135. }
  136. map_array[which].erase(it);
  137. }
  138. bail:
  139. IPADBG("Out\n");
  140. return ret_val;
  141. }
  142. int ipa_nat_map_clear(
  143. ipa_which_map which )
  144. {
  145. int ret_val = 0;
  146. IPADBG("In\n");
  147. if ( ! VALID_IPA_USE_MAP(which) )
  148. {
  149. IPAERR("Bad arg which(%u)\n", which);
  150. ret_val = -1;
  151. goto bail;
  152. }
  153. map_array[which].clear();
  154. bail:
  155. IPADBG("Out\n");
  156. return ret_val;
  157. }
  158. int ipa_nat_map_dump(
  159. ipa_which_map which )
  160. {
  161. std::map<uint32_t, uint32_t>::iterator it;
  162. int ret_val = 0;
  163. IPADBG("In\n");
  164. if ( ! VALID_IPA_USE_MAP(which) )
  165. {
  166. IPAERR("Bad arg which(%u)\n", which);
  167. ret_val = -1;
  168. goto bail;
  169. }
  170. printf("Dumping: %s\n", ipa_which_map_as_str(which));
  171. for ( it = map_array[which].begin();
  172. it != map_array[which].end();
  173. it++ )
  174. {
  175. printf(" Key[%u|0x%08X] -> Value[%u|0x%08X]\n",
  176. it->first,
  177. it->first,
  178. it->second,
  179. it->second);
  180. }
  181. bail:
  182. IPADBG("Out\n");
  183. return ret_val;
  184. }